re PR c++/29024 (storage class specifier accepted for typedef (clause 7.1.1 ; 1))
[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 /* The parser. */
969
970 /* Overview
971 --------
972
973 A cp_parser parses the token stream as specified by the C++
974 grammar. Its job is purely parsing, not semantic analysis. For
975 example, the parser breaks the token stream into declarators,
976 expressions, statements, and other similar syntactic constructs.
977 It does not check that the types of the expressions on either side
978 of an assignment-statement are compatible, or that a function is
979 not declared with a parameter of type `void'.
980
981 The parser invokes routines elsewhere in the compiler to perform
982 semantic analysis and to build up the abstract syntax tree for the
983 code processed.
984
985 The parser (and the template instantiation code, which is, in a
986 way, a close relative of parsing) are the only parts of the
987 compiler that should be calling push_scope and pop_scope, or
988 related functions. The parser (and template instantiation code)
989 keeps track of what scope is presently active; everything else
990 should simply honor that. (The code that generates static
991 initializers may also need to set the scope, in order to check
992 access control correctly when emitting the initializers.)
993
994 Methodology
995 -----------
996
997 The parser is of the standard recursive-descent variety. Upcoming
998 tokens in the token stream are examined in order to determine which
999 production to use when parsing a non-terminal. Some C++ constructs
1000 require arbitrary look ahead to disambiguate. For example, it is
1001 impossible, in the general case, to tell whether a statement is an
1002 expression or declaration without scanning the entire statement.
1003 Therefore, the parser is capable of "parsing tentatively." When the
1004 parser is not sure what construct comes next, it enters this mode.
1005 Then, while we attempt to parse the construct, the parser queues up
1006 error messages, rather than issuing them immediately, and saves the
1007 tokens it consumes. If the construct is parsed successfully, the
1008 parser "commits", i.e., it issues any queued error messages and
1009 the tokens that were being preserved are permanently discarded.
1010 If, however, the construct is not parsed successfully, the parser
1011 rolls back its state completely so that it can resume parsing using
1012 a different alternative.
1013
1014 Future Improvements
1015 -------------------
1016
1017 The performance of the parser could probably be improved substantially.
1018 We could often eliminate the need to parse tentatively by looking ahead
1019 a little bit. In some places, this approach might not entirely eliminate
1020 the need to parse tentatively, but it might still speed up the average
1021 case. */
1022
1023 /* Flags that are passed to some parsing functions. These values can
1024 be bitwise-ored together. */
1025
1026 typedef enum cp_parser_flags
1027 {
1028 /* No flags. */
1029 CP_PARSER_FLAGS_NONE = 0x0,
1030 /* The construct is optional. If it is not present, then no error
1031 should be issued. */
1032 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1033 /* When parsing a type-specifier, do not allow user-defined types. */
1034 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1035 } cp_parser_flags;
1036
1037 /* The different kinds of declarators we want to parse. */
1038
1039 typedef enum cp_parser_declarator_kind
1040 {
1041 /* We want an abstract declarator. */
1042 CP_PARSER_DECLARATOR_ABSTRACT,
1043 /* We want a named declarator. */
1044 CP_PARSER_DECLARATOR_NAMED,
1045 /* We don't mind, but the name must be an unqualified-id. */
1046 CP_PARSER_DECLARATOR_EITHER
1047 } cp_parser_declarator_kind;
1048
1049 /* The precedence values used to parse binary expressions. The minimum value
1050 of PREC must be 1, because zero is reserved to quickly discriminate
1051 binary operators from other tokens. */
1052
1053 enum cp_parser_prec
1054 {
1055 PREC_NOT_OPERATOR,
1056 PREC_LOGICAL_OR_EXPRESSION,
1057 PREC_LOGICAL_AND_EXPRESSION,
1058 PREC_INCLUSIVE_OR_EXPRESSION,
1059 PREC_EXCLUSIVE_OR_EXPRESSION,
1060 PREC_AND_EXPRESSION,
1061 PREC_EQUALITY_EXPRESSION,
1062 PREC_RELATIONAL_EXPRESSION,
1063 PREC_SHIFT_EXPRESSION,
1064 PREC_ADDITIVE_EXPRESSION,
1065 PREC_MULTIPLICATIVE_EXPRESSION,
1066 PREC_PM_EXPRESSION,
1067 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1068 };
1069
1070 /* A mapping from a token type to a corresponding tree node type, with a
1071 precedence value. */
1072
1073 typedef struct cp_parser_binary_operations_map_node
1074 {
1075 /* The token type. */
1076 enum cpp_ttype token_type;
1077 /* The corresponding tree code. */
1078 enum tree_code tree_type;
1079 /* The precedence of this operator. */
1080 enum cp_parser_prec prec;
1081 } cp_parser_binary_operations_map_node;
1082
1083 /* The status of a tentative parse. */
1084
1085 typedef enum cp_parser_status_kind
1086 {
1087 /* No errors have occurred. */
1088 CP_PARSER_STATUS_KIND_NO_ERROR,
1089 /* An error has occurred. */
1090 CP_PARSER_STATUS_KIND_ERROR,
1091 /* We are committed to this tentative parse, whether or not an error
1092 has occurred. */
1093 CP_PARSER_STATUS_KIND_COMMITTED
1094 } cp_parser_status_kind;
1095
1096 typedef struct cp_parser_expression_stack_entry
1097 {
1098 tree lhs;
1099 enum tree_code tree_type;
1100 int prec;
1101 } cp_parser_expression_stack_entry;
1102
1103 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1104 entries because precedence levels on the stack are monotonically
1105 increasing. */
1106 typedef struct cp_parser_expression_stack_entry
1107 cp_parser_expression_stack[NUM_PREC_VALUES];
1108
1109 /* Context that is saved and restored when parsing tentatively. */
1110 typedef struct cp_parser_context GTY (())
1111 {
1112 /* If this is a tentative parsing context, the status of the
1113 tentative parse. */
1114 enum cp_parser_status_kind status;
1115 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1116 that are looked up in this context must be looked up both in the
1117 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1118 the context of the containing expression. */
1119 tree object_type;
1120
1121 /* The next parsing context in the stack. */
1122 struct cp_parser_context *next;
1123 } cp_parser_context;
1124
1125 /* Prototypes. */
1126
1127 /* Constructors and destructors. */
1128
1129 static cp_parser_context *cp_parser_context_new
1130 (cp_parser_context *);
1131
1132 /* Class variables. */
1133
1134 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1135
1136 /* The operator-precedence table used by cp_parser_binary_expression.
1137 Transformed into an associative array (binops_by_token) by
1138 cp_parser_new. */
1139
1140 static const cp_parser_binary_operations_map_node binops[] = {
1141 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1142 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1143
1144 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1145 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1147
1148 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1149 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1150
1151 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1152 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1153
1154 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1157 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1158
1159 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1160 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1161
1162 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1163
1164 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1165
1166 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1167
1168 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1169
1170 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1171 };
1172
1173 /* The same as binops, but initialized by cp_parser_new so that
1174 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1175 for speed. */
1176 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1177
1178 /* Constructors and destructors. */
1179
1180 /* Construct a new context. The context below this one on the stack
1181 is given by NEXT. */
1182
1183 static cp_parser_context *
1184 cp_parser_context_new (cp_parser_context* next)
1185 {
1186 cp_parser_context *context;
1187
1188 /* Allocate the storage. */
1189 if (cp_parser_context_free_list != NULL)
1190 {
1191 /* Pull the first entry from the free list. */
1192 context = cp_parser_context_free_list;
1193 cp_parser_context_free_list = context->next;
1194 memset (context, 0, sizeof (*context));
1195 }
1196 else
1197 context = GGC_CNEW (cp_parser_context);
1198
1199 /* No errors have occurred yet in this context. */
1200 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1201 /* If this is not the bottomost context, copy information that we
1202 need from the previous context. */
1203 if (next)
1204 {
1205 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1206 expression, then we are parsing one in this context, too. */
1207 context->object_type = next->object_type;
1208 /* Thread the stack. */
1209 context->next = next;
1210 }
1211
1212 return context;
1213 }
1214
1215 /* The cp_parser structure represents the C++ parser. */
1216
1217 typedef struct cp_parser GTY(())
1218 {
1219 /* The lexer from which we are obtaining tokens. */
1220 cp_lexer *lexer;
1221
1222 /* The scope in which names should be looked up. If NULL_TREE, then
1223 we look up names in the scope that is currently open in the
1224 source program. If non-NULL, this is either a TYPE or
1225 NAMESPACE_DECL for the scope in which we should look. It can
1226 also be ERROR_MARK, when we've parsed a bogus scope.
1227
1228 This value is not cleared automatically after a name is looked
1229 up, so we must be careful to clear it before starting a new look
1230 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1231 will look up `Z' in the scope of `X', rather than the current
1232 scope.) Unfortunately, it is difficult to tell when name lookup
1233 is complete, because we sometimes peek at a token, look it up,
1234 and then decide not to consume it. */
1235 tree scope;
1236
1237 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1238 last lookup took place. OBJECT_SCOPE is used if an expression
1239 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1240 respectively. QUALIFYING_SCOPE is used for an expression of the
1241 form "X::Y"; it refers to X. */
1242 tree object_scope;
1243 tree qualifying_scope;
1244
1245 /* A stack of parsing contexts. All but the bottom entry on the
1246 stack will be tentative contexts.
1247
1248 We parse tentatively in order to determine which construct is in
1249 use in some situations. For example, in order to determine
1250 whether a statement is an expression-statement or a
1251 declaration-statement we parse it tentatively as a
1252 declaration-statement. If that fails, we then reparse the same
1253 token stream as an expression-statement. */
1254 cp_parser_context *context;
1255
1256 /* True if we are parsing GNU C++. If this flag is not set, then
1257 GNU extensions are not recognized. */
1258 bool allow_gnu_extensions_p;
1259
1260 /* TRUE if the `>' token should be interpreted as the greater-than
1261 operator. FALSE if it is the end of a template-id or
1262 template-parameter-list. */
1263 bool greater_than_is_operator_p;
1264
1265 /* TRUE if default arguments are allowed within a parameter list
1266 that starts at this point. FALSE if only a gnu extension makes
1267 them permissible. */
1268 bool default_arg_ok_p;
1269
1270 /* TRUE if we are parsing an integral constant-expression. See
1271 [expr.const] for a precise definition. */
1272 bool integral_constant_expression_p;
1273
1274 /* TRUE if we are parsing an integral constant-expression -- but a
1275 non-constant expression should be permitted as well. This flag
1276 is used when parsing an array bound so that GNU variable-length
1277 arrays are tolerated. */
1278 bool allow_non_integral_constant_expression_p;
1279
1280 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1281 been seen that makes the expression non-constant. */
1282 bool non_integral_constant_expression_p;
1283
1284 /* TRUE if local variable names and `this' are forbidden in the
1285 current context. */
1286 bool local_variables_forbidden_p;
1287
1288 /* TRUE if the declaration we are parsing is part of a
1289 linkage-specification of the form `extern string-literal
1290 declaration'. */
1291 bool in_unbraced_linkage_specification_p;
1292
1293 /* TRUE if we are presently parsing a declarator, after the
1294 direct-declarator. */
1295 bool in_declarator_p;
1296
1297 /* TRUE if we are presently parsing a template-argument-list. */
1298 bool in_template_argument_list_p;
1299
1300 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1301 to IN_OMP_BLOCK if parsing OpenMP structured block and
1302 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1303 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1304 iteration-statement, OpenMP block or loop within that switch. */
1305 #define IN_SWITCH_STMT 1
1306 #define IN_ITERATION_STMT 2
1307 #define IN_OMP_BLOCK 4
1308 #define IN_OMP_FOR 8
1309 unsigned char in_statement;
1310
1311 /* TRUE if we are presently parsing the body of a switch statement.
1312 Note that this doesn't quite overlap with in_statement above.
1313 The difference relates to giving the right sets of error messages:
1314 "case not in switch" vs "break statement used with OpenMP...". */
1315 bool in_switch_statement_p;
1316
1317 /* TRUE if we are parsing a type-id in an expression context. In
1318 such a situation, both "type (expr)" and "type (type)" are valid
1319 alternatives. */
1320 bool in_type_id_in_expr_p;
1321
1322 /* TRUE if we are currently in a header file where declarations are
1323 implicitly extern "C". */
1324 bool implicit_extern_c;
1325
1326 /* TRUE if strings in expressions should be translated to the execution
1327 character set. */
1328 bool translate_strings_p;
1329
1330 /* If non-NULL, then we are parsing a construct where new type
1331 definitions are not permitted. The string stored here will be
1332 issued as an error message if a type is defined. */
1333 const char *type_definition_forbidden_message;
1334
1335 /* A list of lists. The outer list is a stack, used for member
1336 functions of local classes. At each level there are two sub-list,
1337 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1338 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1339 TREE_VALUE's. The functions are chained in reverse declaration
1340 order.
1341
1342 The TREE_PURPOSE sublist contains those functions with default
1343 arguments that need post processing, and the TREE_VALUE sublist
1344 contains those functions with definitions that need post
1345 processing.
1346
1347 These lists can only be processed once the outermost class being
1348 defined is complete. */
1349 tree unparsed_functions_queues;
1350
1351 /* The number of classes whose definitions are currently in
1352 progress. */
1353 unsigned num_classes_being_defined;
1354
1355 /* The number of template parameter lists that apply directly to the
1356 current declaration. */
1357 unsigned num_template_parameter_lists;
1358 } cp_parser;
1359
1360 /* Prototypes. */
1361
1362 /* Constructors and destructors. */
1363
1364 static cp_parser *cp_parser_new
1365 (void);
1366
1367 /* Routines to parse various constructs.
1368
1369 Those that return `tree' will return the error_mark_node (rather
1370 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1371 Sometimes, they will return an ordinary node if error-recovery was
1372 attempted, even though a parse error occurred. So, to check
1373 whether or not a parse error occurred, you should always use
1374 cp_parser_error_occurred. If the construct is optional (indicated
1375 either by an `_opt' in the name of the function that does the
1376 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1377 the construct is not present. */
1378
1379 /* Lexical conventions [gram.lex] */
1380
1381 static tree cp_parser_identifier
1382 (cp_parser *);
1383 static tree cp_parser_string_literal
1384 (cp_parser *, bool, bool);
1385
1386 /* Basic concepts [gram.basic] */
1387
1388 static bool cp_parser_translation_unit
1389 (cp_parser *);
1390
1391 /* Expressions [gram.expr] */
1392
1393 static tree cp_parser_primary_expression
1394 (cp_parser *, bool, bool, bool, cp_id_kind *);
1395 static tree cp_parser_id_expression
1396 (cp_parser *, bool, bool, bool *, bool, bool);
1397 static tree cp_parser_unqualified_id
1398 (cp_parser *, bool, bool, bool, bool);
1399 static tree cp_parser_nested_name_specifier_opt
1400 (cp_parser *, bool, bool, bool, bool);
1401 static tree cp_parser_nested_name_specifier
1402 (cp_parser *, bool, bool, bool, bool);
1403 static tree cp_parser_class_or_namespace_name
1404 (cp_parser *, bool, bool, bool, bool, bool);
1405 static tree cp_parser_postfix_expression
1406 (cp_parser *, bool, bool);
1407 static tree cp_parser_postfix_open_square_expression
1408 (cp_parser *, tree, bool);
1409 static tree cp_parser_postfix_dot_deref_expression
1410 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1411 static tree cp_parser_parenthesized_expression_list
1412 (cp_parser *, bool, bool, bool *);
1413 static void cp_parser_pseudo_destructor_name
1414 (cp_parser *, tree *, tree *);
1415 static tree cp_parser_unary_expression
1416 (cp_parser *, bool, bool);
1417 static enum tree_code cp_parser_unary_operator
1418 (cp_token *);
1419 static tree cp_parser_new_expression
1420 (cp_parser *);
1421 static tree cp_parser_new_placement
1422 (cp_parser *);
1423 static tree cp_parser_new_type_id
1424 (cp_parser *, tree *);
1425 static cp_declarator *cp_parser_new_declarator_opt
1426 (cp_parser *);
1427 static cp_declarator *cp_parser_direct_new_declarator
1428 (cp_parser *);
1429 static tree cp_parser_new_initializer
1430 (cp_parser *);
1431 static tree cp_parser_delete_expression
1432 (cp_parser *);
1433 static tree cp_parser_cast_expression
1434 (cp_parser *, bool, bool);
1435 static tree cp_parser_binary_expression
1436 (cp_parser *, bool);
1437 static tree cp_parser_question_colon_clause
1438 (cp_parser *, tree);
1439 static tree cp_parser_assignment_expression
1440 (cp_parser *, bool);
1441 static enum tree_code cp_parser_assignment_operator_opt
1442 (cp_parser *);
1443 static tree cp_parser_expression
1444 (cp_parser *, bool);
1445 static tree cp_parser_constant_expression
1446 (cp_parser *, bool, bool *);
1447 static tree cp_parser_builtin_offsetof
1448 (cp_parser *);
1449
1450 /* Statements [gram.stmt.stmt] */
1451
1452 static void cp_parser_statement
1453 (cp_parser *, tree, bool);
1454 static void cp_parser_label_for_labeled_statement
1455 (cp_parser *);
1456 static tree cp_parser_expression_statement
1457 (cp_parser *, tree);
1458 static tree cp_parser_compound_statement
1459 (cp_parser *, tree, bool);
1460 static void cp_parser_statement_seq_opt
1461 (cp_parser *, tree);
1462 static tree cp_parser_selection_statement
1463 (cp_parser *);
1464 static tree cp_parser_condition
1465 (cp_parser *);
1466 static tree cp_parser_iteration_statement
1467 (cp_parser *);
1468 static void cp_parser_for_init_statement
1469 (cp_parser *);
1470 static tree cp_parser_jump_statement
1471 (cp_parser *);
1472 static void cp_parser_declaration_statement
1473 (cp_parser *);
1474
1475 static tree cp_parser_implicitly_scoped_statement
1476 (cp_parser *);
1477 static void cp_parser_already_scoped_statement
1478 (cp_parser *);
1479
1480 /* Declarations [gram.dcl.dcl] */
1481
1482 static void cp_parser_declaration_seq_opt
1483 (cp_parser *);
1484 static void cp_parser_declaration
1485 (cp_parser *);
1486 static void cp_parser_block_declaration
1487 (cp_parser *, bool);
1488 static void cp_parser_simple_declaration
1489 (cp_parser *, bool);
1490 static void cp_parser_decl_specifier_seq
1491 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1492 static tree cp_parser_storage_class_specifier_opt
1493 (cp_parser *);
1494 static tree cp_parser_function_specifier_opt
1495 (cp_parser *, cp_decl_specifier_seq *);
1496 static tree cp_parser_type_specifier
1497 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1498 int *, bool *);
1499 static tree cp_parser_simple_type_specifier
1500 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1501 static tree cp_parser_type_name
1502 (cp_parser *);
1503 static tree cp_parser_elaborated_type_specifier
1504 (cp_parser *, bool, bool);
1505 static tree cp_parser_enum_specifier
1506 (cp_parser *);
1507 static void cp_parser_enumerator_list
1508 (cp_parser *, tree);
1509 static void cp_parser_enumerator_definition
1510 (cp_parser *, tree);
1511 static tree cp_parser_namespace_name
1512 (cp_parser *);
1513 static void cp_parser_namespace_definition
1514 (cp_parser *);
1515 static void cp_parser_namespace_body
1516 (cp_parser *);
1517 static tree cp_parser_qualified_namespace_specifier
1518 (cp_parser *);
1519 static void cp_parser_namespace_alias_definition
1520 (cp_parser *);
1521 static bool cp_parser_using_declaration
1522 (cp_parser *, bool);
1523 static void cp_parser_using_directive
1524 (cp_parser *);
1525 static void cp_parser_asm_definition
1526 (cp_parser *);
1527 static void cp_parser_linkage_specification
1528 (cp_parser *);
1529
1530 /* Declarators [gram.dcl.decl] */
1531
1532 static tree cp_parser_init_declarator
1533 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1534 static cp_declarator *cp_parser_declarator
1535 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1536 static cp_declarator *cp_parser_direct_declarator
1537 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1538 static enum tree_code cp_parser_ptr_operator
1539 (cp_parser *, tree *, cp_cv_quals *);
1540 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1541 (cp_parser *);
1542 static tree cp_parser_declarator_id
1543 (cp_parser *, bool);
1544 static tree cp_parser_type_id
1545 (cp_parser *);
1546 static void cp_parser_type_specifier_seq
1547 (cp_parser *, bool, cp_decl_specifier_seq *);
1548 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1549 (cp_parser *);
1550 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1551 (cp_parser *, bool *);
1552 static cp_parameter_declarator *cp_parser_parameter_declaration
1553 (cp_parser *, bool, bool *);
1554 static void cp_parser_function_body
1555 (cp_parser *);
1556 static tree cp_parser_initializer
1557 (cp_parser *, bool *, bool *);
1558 static tree cp_parser_initializer_clause
1559 (cp_parser *, bool *);
1560 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1561 (cp_parser *, bool *);
1562
1563 static bool cp_parser_ctor_initializer_opt_and_function_body
1564 (cp_parser *);
1565
1566 /* Classes [gram.class] */
1567
1568 static tree cp_parser_class_name
1569 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1570 static tree cp_parser_class_specifier
1571 (cp_parser *);
1572 static tree cp_parser_class_head
1573 (cp_parser *, bool *, tree *);
1574 static enum tag_types cp_parser_class_key
1575 (cp_parser *);
1576 static void cp_parser_member_specification_opt
1577 (cp_parser *);
1578 static void cp_parser_member_declaration
1579 (cp_parser *);
1580 static tree cp_parser_pure_specifier
1581 (cp_parser *);
1582 static tree cp_parser_constant_initializer
1583 (cp_parser *);
1584
1585 /* Derived classes [gram.class.derived] */
1586
1587 static tree cp_parser_base_clause
1588 (cp_parser *);
1589 static tree cp_parser_base_specifier
1590 (cp_parser *);
1591
1592 /* Special member functions [gram.special] */
1593
1594 static tree cp_parser_conversion_function_id
1595 (cp_parser *);
1596 static tree cp_parser_conversion_type_id
1597 (cp_parser *);
1598 static cp_declarator *cp_parser_conversion_declarator_opt
1599 (cp_parser *);
1600 static bool cp_parser_ctor_initializer_opt
1601 (cp_parser *);
1602 static void cp_parser_mem_initializer_list
1603 (cp_parser *);
1604 static tree cp_parser_mem_initializer
1605 (cp_parser *);
1606 static tree cp_parser_mem_initializer_id
1607 (cp_parser *);
1608
1609 /* Overloading [gram.over] */
1610
1611 static tree cp_parser_operator_function_id
1612 (cp_parser *);
1613 static tree cp_parser_operator
1614 (cp_parser *);
1615
1616 /* Templates [gram.temp] */
1617
1618 static void cp_parser_template_declaration
1619 (cp_parser *, bool);
1620 static tree cp_parser_template_parameter_list
1621 (cp_parser *);
1622 static tree cp_parser_template_parameter
1623 (cp_parser *, bool *);
1624 static tree cp_parser_type_parameter
1625 (cp_parser *);
1626 static tree cp_parser_template_id
1627 (cp_parser *, bool, bool, bool);
1628 static tree cp_parser_template_name
1629 (cp_parser *, bool, bool, bool, bool *);
1630 static tree cp_parser_template_argument_list
1631 (cp_parser *);
1632 static tree cp_parser_template_argument
1633 (cp_parser *);
1634 static void cp_parser_explicit_instantiation
1635 (cp_parser *);
1636 static void cp_parser_explicit_specialization
1637 (cp_parser *);
1638
1639 /* Exception handling [gram.exception] */
1640
1641 static tree cp_parser_try_block
1642 (cp_parser *);
1643 static bool cp_parser_function_try_block
1644 (cp_parser *);
1645 static void cp_parser_handler_seq
1646 (cp_parser *);
1647 static void cp_parser_handler
1648 (cp_parser *);
1649 static tree cp_parser_exception_declaration
1650 (cp_parser *);
1651 static tree cp_parser_throw_expression
1652 (cp_parser *);
1653 static tree cp_parser_exception_specification_opt
1654 (cp_parser *);
1655 static tree cp_parser_type_id_list
1656 (cp_parser *);
1657
1658 /* GNU Extensions */
1659
1660 static tree cp_parser_asm_specification_opt
1661 (cp_parser *);
1662 static tree cp_parser_asm_operand_list
1663 (cp_parser *);
1664 static tree cp_parser_asm_clobber_list
1665 (cp_parser *);
1666 static tree cp_parser_attributes_opt
1667 (cp_parser *);
1668 static tree cp_parser_attribute_list
1669 (cp_parser *);
1670 static bool cp_parser_extension_opt
1671 (cp_parser *, int *);
1672 static void cp_parser_label_declaration
1673 (cp_parser *);
1674
1675 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1676 static bool cp_parser_pragma
1677 (cp_parser *, enum pragma_context);
1678
1679 /* Objective-C++ Productions */
1680
1681 static tree cp_parser_objc_message_receiver
1682 (cp_parser *);
1683 static tree cp_parser_objc_message_args
1684 (cp_parser *);
1685 static tree cp_parser_objc_message_expression
1686 (cp_parser *);
1687 static tree cp_parser_objc_encode_expression
1688 (cp_parser *);
1689 static tree cp_parser_objc_defs_expression
1690 (cp_parser *);
1691 static tree cp_parser_objc_protocol_expression
1692 (cp_parser *);
1693 static tree cp_parser_objc_selector_expression
1694 (cp_parser *);
1695 static tree cp_parser_objc_expression
1696 (cp_parser *);
1697 static bool cp_parser_objc_selector_p
1698 (enum cpp_ttype);
1699 static tree cp_parser_objc_selector
1700 (cp_parser *);
1701 static tree cp_parser_objc_protocol_refs_opt
1702 (cp_parser *);
1703 static void cp_parser_objc_declaration
1704 (cp_parser *);
1705 static tree cp_parser_objc_statement
1706 (cp_parser *);
1707
1708 /* Utility Routines */
1709
1710 static tree cp_parser_lookup_name
1711 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1712 static tree cp_parser_lookup_name_simple
1713 (cp_parser *, tree);
1714 static tree cp_parser_maybe_treat_template_as_class
1715 (tree, bool);
1716 static bool cp_parser_check_declarator_template_parameters
1717 (cp_parser *, cp_declarator *);
1718 static bool cp_parser_check_template_parameters
1719 (cp_parser *, unsigned);
1720 static tree cp_parser_simple_cast_expression
1721 (cp_parser *);
1722 static tree cp_parser_global_scope_opt
1723 (cp_parser *, bool);
1724 static bool cp_parser_constructor_declarator_p
1725 (cp_parser *, bool);
1726 static tree cp_parser_function_definition_from_specifiers_and_declarator
1727 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1728 static tree cp_parser_function_definition_after_declarator
1729 (cp_parser *, bool);
1730 static void cp_parser_template_declaration_after_export
1731 (cp_parser *, bool);
1732 static void cp_parser_perform_template_parameter_access_checks
1733 (tree);
1734 static tree cp_parser_single_declaration
1735 (cp_parser *, tree, bool, bool *);
1736 static tree cp_parser_functional_cast
1737 (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1741 (cp_parser *);
1742 static void cp_parser_save_default_args
1743 (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745 (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747 (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749 (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1751 (cp_parser *);
1752 static void cp_parser_set_storage_class
1753 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1754 static void cp_parser_set_decl_spec_type
1755 (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757 (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759 (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761 (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1763 (cp_token *);
1764 static bool cp_parser_next_token_starts_class_definition_p
1765 (cp_parser *);
1766 static bool cp_parser_next_token_ends_template_argument_p
1767 (cp_parser *);
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769 (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1771 (cp_token *);
1772 static void cp_parser_check_class_key
1773 (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1775 (tree type);
1776 static bool cp_parser_optional_template_keyword
1777 (cp_parser *);
1778 static void cp_parser_pre_parsed_nested_name_specifier
1779 (cp_parser *);
1780 static void cp_parser_cache_group
1781 (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1783 (cp_parser *);
1784 static void cp_parser_commit_to_tentative_parse
1785 (cp_parser *);
1786 static void cp_parser_abort_tentative_parse
1787 (cp_parser *);
1788 static bool cp_parser_parse_definitely
1789 (cp_parser *);
1790 static inline bool cp_parser_parsing_tentatively
1791 (cp_parser *);
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1793 (cp_parser *);
1794 static void cp_parser_error
1795 (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797 (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1799 (cp_parser *);
1800 static void cp_parser_check_type_definition
1801 (cp_parser *);
1802 static void cp_parser_check_for_definition_in_return_type
1803 (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805 (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807 (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809 (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1811 (cp_parser *);
1812 static int cp_parser_skip_to_closing_parenthesis
1813 (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1815 (cp_parser *);
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1817 (cp_parser *);
1818 static void cp_parser_skip_to_end_of_block_or_statement
1819 (cp_parser *);
1820 static void cp_parser_skip_to_closing_brace
1821 (cp_parser *);
1822 static void cp_parser_skip_to_end_of_template_parameter_list
1823 (cp_parser *);
1824 static void cp_parser_skip_to_pragma_eol
1825 (cp_parser*, cp_token *);
1826 static bool cp_parser_error_occurred
1827 (cp_parser *);
1828 static bool cp_parser_allow_gnu_extensions_p
1829 (cp_parser *);
1830 static bool cp_parser_is_string_literal
1831 (cp_token *);
1832 static bool cp_parser_is_keyword
1833 (cp_token *, enum rid);
1834 static tree cp_parser_make_typename_type
1835 (cp_parser *, tree, tree);
1836
1837 /* Returns nonzero if we are parsing tentatively. */
1838
1839 static inline bool
1840 cp_parser_parsing_tentatively (cp_parser* parser)
1841 {
1842 return parser->context->next != NULL;
1843 }
1844
1845 /* Returns nonzero if TOKEN is a string literal. */
1846
1847 static bool
1848 cp_parser_is_string_literal (cp_token* token)
1849 {
1850 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1851 }
1852
1853 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1854
1855 static bool
1856 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1857 {
1858 return token->keyword == keyword;
1859 }
1860
1861 /* If not parsing tentatively, issue a diagnostic of the form
1862 FILE:LINE: MESSAGE before TOKEN
1863 where TOKEN is the next token in the input stream. MESSAGE
1864 (specified by the caller) is usually of the form "expected
1865 OTHER-TOKEN". */
1866
1867 static void
1868 cp_parser_error (cp_parser* parser, const char* message)
1869 {
1870 if (!cp_parser_simulate_error (parser))
1871 {
1872 cp_token *token = cp_lexer_peek_token (parser->lexer);
1873 /* This diagnostic makes more sense if it is tagged to the line
1874 of the token we just peeked at. */
1875 cp_lexer_set_source_position_from_token (token);
1876
1877 if (token->type == CPP_PRAGMA)
1878 {
1879 error ("%<#pragma%> is not allowed here");
1880 cp_parser_skip_to_pragma_eol (parser, token);
1881 return;
1882 }
1883
1884 c_parse_error (message,
1885 /* Because c_parser_error does not understand
1886 CPP_KEYWORD, keywords are treated like
1887 identifiers. */
1888 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1889 token->value);
1890 }
1891 }
1892
1893 /* Issue an error about name-lookup failing. NAME is the
1894 IDENTIFIER_NODE DECL is the result of
1895 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1896 the thing that we hoped to find. */
1897
1898 static void
1899 cp_parser_name_lookup_error (cp_parser* parser,
1900 tree name,
1901 tree decl,
1902 const char* desired)
1903 {
1904 /* If name lookup completely failed, tell the user that NAME was not
1905 declared. */
1906 if (decl == error_mark_node)
1907 {
1908 if (parser->scope && parser->scope != global_namespace)
1909 error ("%<%D::%D%> has not been declared",
1910 parser->scope, name);
1911 else if (parser->scope == global_namespace)
1912 error ("%<::%D%> has not been declared", name);
1913 else if (parser->object_scope
1914 && !CLASS_TYPE_P (parser->object_scope))
1915 error ("request for member %qD in non-class type %qT",
1916 name, parser->object_scope);
1917 else if (parser->object_scope)
1918 error ("%<%T::%D%> has not been declared",
1919 parser->object_scope, name);
1920 else
1921 error ("%qD has not been declared", name);
1922 }
1923 else if (parser->scope && parser->scope != global_namespace)
1924 error ("%<%D::%D%> %s", parser->scope, name, desired);
1925 else if (parser->scope == global_namespace)
1926 error ("%<::%D%> %s", name, desired);
1927 else
1928 error ("%qD %s", name, desired);
1929 }
1930
1931 /* If we are parsing tentatively, remember that an error has occurred
1932 during this tentative parse. Returns true if the error was
1933 simulated; false if a message should be issued by the caller. */
1934
1935 static bool
1936 cp_parser_simulate_error (cp_parser* parser)
1937 {
1938 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1939 {
1940 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1941 return true;
1942 }
1943 return false;
1944 }
1945
1946 /* Check for repeated decl-specifiers. */
1947
1948 static void
1949 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1950 {
1951 cp_decl_spec ds;
1952
1953 for (ds = ds_first; ds != ds_last; ++ds)
1954 {
1955 unsigned count = decl_specs->specs[(int)ds];
1956 if (count < 2)
1957 continue;
1958 /* The "long" specifier is a special case because of "long long". */
1959 if (ds == ds_long)
1960 {
1961 if (count > 2)
1962 error ("%<long long long%> is too long for GCC");
1963 else if (pedantic && !in_system_header && warn_long_long)
1964 pedwarn ("ISO C++ does not support %<long long%>");
1965 }
1966 else if (count > 1)
1967 {
1968 static const char *const decl_spec_names[] = {
1969 "signed",
1970 "unsigned",
1971 "short",
1972 "long",
1973 "const",
1974 "volatile",
1975 "restrict",
1976 "inline",
1977 "virtual",
1978 "explicit",
1979 "friend",
1980 "typedef",
1981 "__complex",
1982 "__thread"
1983 };
1984 error ("duplicate %qs", decl_spec_names[(int)ds]);
1985 }
1986 }
1987 }
1988
1989 /* This function is called when a type is defined. If type
1990 definitions are forbidden at this point, an error message is
1991 issued. */
1992
1993 static void
1994 cp_parser_check_type_definition (cp_parser* parser)
1995 {
1996 /* If types are forbidden here, issue a message. */
1997 if (parser->type_definition_forbidden_message)
1998 /* Use `%s' to print the string in case there are any escape
1999 characters in the message. */
2000 error ("%s", parser->type_definition_forbidden_message);
2001 }
2002
2003 /* This function is called when the DECLARATOR is processed. The TYPE
2004 was a type defined in the decl-specifiers. If it is invalid to
2005 define a type in the decl-specifiers for DECLARATOR, an error is
2006 issued. */
2007
2008 static void
2009 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2010 tree type)
2011 {
2012 /* [dcl.fct] forbids type definitions in return types.
2013 Unfortunately, it's not easy to know whether or not we are
2014 processing a return type until after the fact. */
2015 while (declarator
2016 && (declarator->kind == cdk_pointer
2017 || declarator->kind == cdk_reference
2018 || declarator->kind == cdk_ptrmem))
2019 declarator = declarator->declarator;
2020 if (declarator
2021 && declarator->kind == cdk_function)
2022 {
2023 error ("new types may not be defined in a return type");
2024 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2025 type);
2026 }
2027 }
2028
2029 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2030 "<" in any valid C++ program. If the next token is indeed "<",
2031 issue a message warning the user about what appears to be an
2032 invalid attempt to form a template-id. */
2033
2034 static void
2035 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2036 tree type)
2037 {
2038 cp_token_position start = 0;
2039
2040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2041 {
2042 if (TYPE_P (type))
2043 error ("%qT is not a template", type);
2044 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2045 error ("%qE is not a template", type);
2046 else
2047 error ("invalid template-id");
2048 /* Remember the location of the invalid "<". */
2049 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2050 start = cp_lexer_token_position (parser->lexer, true);
2051 /* Consume the "<". */
2052 cp_lexer_consume_token (parser->lexer);
2053 /* Parse the template arguments. */
2054 cp_parser_enclosed_template_argument_list (parser);
2055 /* Permanently remove the invalid template arguments so that
2056 this error message is not issued again. */
2057 if (start)
2058 cp_lexer_purge_tokens_after (parser->lexer, start);
2059 }
2060 }
2061
2062 /* If parsing an integral constant-expression, issue an error message
2063 about the fact that THING appeared and return true. Otherwise,
2064 return false. In either case, set
2065 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2066
2067 static bool
2068 cp_parser_non_integral_constant_expression (cp_parser *parser,
2069 const char *thing)
2070 {
2071 parser->non_integral_constant_expression_p = true;
2072 if (parser->integral_constant_expression_p)
2073 {
2074 if (!parser->allow_non_integral_constant_expression_p)
2075 {
2076 error ("%s cannot appear in a constant-expression", thing);
2077 return true;
2078 }
2079 }
2080 return false;
2081 }
2082
2083 /* Emit a diagnostic for an invalid type name. SCOPE is the
2084 qualifying scope (or NULL, if none) for ID. This function commits
2085 to the current active tentative parse, if any. (Otherwise, the
2086 problematic construct might be encountered again later, resulting
2087 in duplicate error messages.) */
2088
2089 static void
2090 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2091 {
2092 tree decl, old_scope;
2093 /* Try to lookup the identifier. */
2094 old_scope = parser->scope;
2095 parser->scope = scope;
2096 decl = cp_parser_lookup_name_simple (parser, id);
2097 parser->scope = old_scope;
2098 /* If the lookup found a template-name, it means that the user forgot
2099 to specify an argument list. Emit a useful error message. */
2100 if (TREE_CODE (decl) == TEMPLATE_DECL)
2101 error ("invalid use of template-name %qE without an argument list", decl);
2102 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2103 error ("invalid use of destructor %qD as a type", id);
2104 else if (TREE_CODE (decl) == TYPE_DECL)
2105 /* Something like 'unsigned A a;' */
2106 error ("invalid combination of multiple type-specifiers");
2107 else if (!parser->scope)
2108 {
2109 /* Issue an error message. */
2110 error ("%qE does not name a type", id);
2111 /* If we're in a template class, it's possible that the user was
2112 referring to a type from a base class. For example:
2113
2114 template <typename T> struct A { typedef T X; };
2115 template <typename T> struct B : public A<T> { X x; };
2116
2117 The user should have said "typename A<T>::X". */
2118 if (processing_template_decl && current_class_type
2119 && TYPE_BINFO (current_class_type))
2120 {
2121 tree b;
2122
2123 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2124 b;
2125 b = TREE_CHAIN (b))
2126 {
2127 tree base_type = BINFO_TYPE (b);
2128 if (CLASS_TYPE_P (base_type)
2129 && dependent_type_p (base_type))
2130 {
2131 tree field;
2132 /* Go from a particular instantiation of the
2133 template (which will have an empty TYPE_FIELDs),
2134 to the main version. */
2135 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2136 for (field = TYPE_FIELDS (base_type);
2137 field;
2138 field = TREE_CHAIN (field))
2139 if (TREE_CODE (field) == TYPE_DECL
2140 && DECL_NAME (field) == id)
2141 {
2142 inform ("(perhaps %<typename %T::%E%> was intended)",
2143 BINFO_TYPE (b), id);
2144 break;
2145 }
2146 if (field)
2147 break;
2148 }
2149 }
2150 }
2151 }
2152 /* Here we diagnose qualified-ids where the scope is actually correct,
2153 but the identifier does not resolve to a valid type name. */
2154 else if (parser->scope != error_mark_node)
2155 {
2156 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2157 error ("%qE in namespace %qE does not name a type",
2158 id, parser->scope);
2159 else if (TYPE_P (parser->scope))
2160 error ("%qE in class %qT does not name a type", id, parser->scope);
2161 else
2162 gcc_unreachable ();
2163 }
2164 cp_parser_commit_to_tentative_parse (parser);
2165 }
2166
2167 /* Check for a common situation where a type-name should be present,
2168 but is not, and issue a sensible error message. Returns true if an
2169 invalid type-name was detected.
2170
2171 The situation handled by this function are variable declarations of the
2172 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2173 Usually, `ID' should name a type, but if we got here it means that it
2174 does not. We try to emit the best possible error message depending on
2175 how exactly the id-expression looks like. */
2176
2177 static bool
2178 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2179 {
2180 tree id;
2181
2182 cp_parser_parse_tentatively (parser);
2183 id = cp_parser_id_expression (parser,
2184 /*template_keyword_p=*/false,
2185 /*check_dependency_p=*/true,
2186 /*template_p=*/NULL,
2187 /*declarator_p=*/true,
2188 /*optional_p=*/false);
2189 /* After the id-expression, there should be a plain identifier,
2190 otherwise this is not a simple variable declaration. Also, if
2191 the scope is dependent, we cannot do much. */
2192 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2193 || (parser->scope && TYPE_P (parser->scope)
2194 && dependent_type_p (parser->scope)))
2195 {
2196 cp_parser_abort_tentative_parse (parser);
2197 return false;
2198 }
2199 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2200 return false;
2201
2202 /* Emit a diagnostic for the invalid type. */
2203 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2204 /* Skip to the end of the declaration; there's no point in
2205 trying to process it. */
2206 cp_parser_skip_to_end_of_block_or_statement (parser);
2207 return true;
2208 }
2209
2210 /* Consume tokens up to, and including, the next non-nested closing `)'.
2211 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2212 are doing error recovery. Returns -1 if OR_COMMA is true and we
2213 found an unnested comma. */
2214
2215 static int
2216 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2217 bool recovering,
2218 bool or_comma,
2219 bool consume_paren)
2220 {
2221 unsigned paren_depth = 0;
2222 unsigned brace_depth = 0;
2223
2224 if (recovering && !or_comma
2225 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2226 return 0;
2227
2228 while (true)
2229 {
2230 cp_token * token = cp_lexer_peek_token (parser->lexer);
2231
2232 switch (token->type)
2233 {
2234 case CPP_EOF:
2235 case CPP_PRAGMA_EOL:
2236 /* If we've run out of tokens, then there is no closing `)'. */
2237 return 0;
2238
2239 case CPP_SEMICOLON:
2240 /* This matches the processing in skip_to_end_of_statement. */
2241 if (!brace_depth)
2242 return 0;
2243 break;
2244
2245 case CPP_OPEN_BRACE:
2246 ++brace_depth;
2247 break;
2248 case CPP_CLOSE_BRACE:
2249 if (!brace_depth--)
2250 return 0;
2251 break;
2252
2253 case CPP_COMMA:
2254 if (recovering && or_comma && !brace_depth && !paren_depth)
2255 return -1;
2256 break;
2257
2258 case CPP_OPEN_PAREN:
2259 if (!brace_depth)
2260 ++paren_depth;
2261 break;
2262
2263 case CPP_CLOSE_PAREN:
2264 if (!brace_depth && !paren_depth--)
2265 {
2266 if (consume_paren)
2267 cp_lexer_consume_token (parser->lexer);
2268 return 1;
2269 }
2270 break;
2271
2272 default:
2273 break;
2274 }
2275
2276 /* Consume the token. */
2277 cp_lexer_consume_token (parser->lexer);
2278 }
2279 }
2280
2281 /* Consume tokens until we reach the end of the current statement.
2282 Normally, that will be just before consuming a `;'. However, if a
2283 non-nested `}' comes first, then we stop before consuming that. */
2284
2285 static void
2286 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2287 {
2288 unsigned nesting_depth = 0;
2289
2290 while (true)
2291 {
2292 cp_token *token = cp_lexer_peek_token (parser->lexer);
2293
2294 switch (token->type)
2295 {
2296 case CPP_EOF:
2297 case CPP_PRAGMA_EOL:
2298 /* If we've run out of tokens, stop. */
2299 return;
2300
2301 case CPP_SEMICOLON:
2302 /* If the next token is a `;', we have reached the end of the
2303 statement. */
2304 if (!nesting_depth)
2305 return;
2306 break;
2307
2308 case CPP_CLOSE_BRACE:
2309 /* If this is a non-nested '}', stop before consuming it.
2310 That way, when confronted with something like:
2311
2312 { 3 + }
2313
2314 we stop before consuming the closing '}', even though we
2315 have not yet reached a `;'. */
2316 if (nesting_depth == 0)
2317 return;
2318
2319 /* If it is the closing '}' for a block that we have
2320 scanned, stop -- but only after consuming the token.
2321 That way given:
2322
2323 void f g () { ... }
2324 typedef int I;
2325
2326 we will stop after the body of the erroneously declared
2327 function, but before consuming the following `typedef'
2328 declaration. */
2329 if (--nesting_depth == 0)
2330 {
2331 cp_lexer_consume_token (parser->lexer);
2332 return;
2333 }
2334
2335 case CPP_OPEN_BRACE:
2336 ++nesting_depth;
2337 break;
2338
2339 default:
2340 break;
2341 }
2342
2343 /* Consume the token. */
2344 cp_lexer_consume_token (parser->lexer);
2345 }
2346 }
2347
2348 /* This function is called at the end of a statement or declaration.
2349 If the next token is a semicolon, it is consumed; otherwise, error
2350 recovery is attempted. */
2351
2352 static void
2353 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2354 {
2355 /* Look for the trailing `;'. */
2356 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2357 {
2358 /* If there is additional (erroneous) input, skip to the end of
2359 the statement. */
2360 cp_parser_skip_to_end_of_statement (parser);
2361 /* If the next token is now a `;', consume it. */
2362 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2363 cp_lexer_consume_token (parser->lexer);
2364 }
2365 }
2366
2367 /* Skip tokens until we have consumed an entire block, or until we
2368 have consumed a non-nested `;'. */
2369
2370 static void
2371 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2372 {
2373 int nesting_depth = 0;
2374
2375 while (nesting_depth >= 0)
2376 {
2377 cp_token *token = cp_lexer_peek_token (parser->lexer);
2378
2379 switch (token->type)
2380 {
2381 case CPP_EOF:
2382 case CPP_PRAGMA_EOL:
2383 /* If we've run out of tokens, stop. */
2384 return;
2385
2386 case CPP_SEMICOLON:
2387 /* Stop if this is an unnested ';'. */
2388 if (!nesting_depth)
2389 nesting_depth = -1;
2390 break;
2391
2392 case CPP_CLOSE_BRACE:
2393 /* Stop if this is an unnested '}', or closes the outermost
2394 nesting level. */
2395 nesting_depth--;
2396 if (!nesting_depth)
2397 nesting_depth = -1;
2398 break;
2399
2400 case CPP_OPEN_BRACE:
2401 /* Nest. */
2402 nesting_depth++;
2403 break;
2404
2405 default:
2406 break;
2407 }
2408
2409 /* Consume the token. */
2410 cp_lexer_consume_token (parser->lexer);
2411 }
2412 }
2413
2414 /* Skip tokens until a non-nested closing curly brace is the next
2415 token. */
2416
2417 static void
2418 cp_parser_skip_to_closing_brace (cp_parser *parser)
2419 {
2420 unsigned nesting_depth = 0;
2421
2422 while (true)
2423 {
2424 cp_token *token = cp_lexer_peek_token (parser->lexer);
2425
2426 switch (token->type)
2427 {
2428 case CPP_EOF:
2429 case CPP_PRAGMA_EOL:
2430 /* If we've run out of tokens, stop. */
2431 return;
2432
2433 case CPP_CLOSE_BRACE:
2434 /* If the next token is a non-nested `}', then we have reached
2435 the end of the current block. */
2436 if (nesting_depth-- == 0)
2437 return;
2438 break;
2439
2440 case CPP_OPEN_BRACE:
2441 /* If it the next token is a `{', then we are entering a new
2442 block. Consume the entire block. */
2443 ++nesting_depth;
2444 break;
2445
2446 default:
2447 break;
2448 }
2449
2450 /* Consume the token. */
2451 cp_lexer_consume_token (parser->lexer);
2452 }
2453 }
2454
2455 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2456 parameter is the PRAGMA token, allowing us to purge the entire pragma
2457 sequence. */
2458
2459 static void
2460 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2461 {
2462 cp_token *token;
2463
2464 parser->lexer->in_pragma = false;
2465
2466 do
2467 token = cp_lexer_consume_token (parser->lexer);
2468 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2469
2470 /* Ensure that the pragma is not parsed again. */
2471 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2472 }
2473
2474 /* Require pragma end of line, resyncing with it as necessary. The
2475 arguments are as for cp_parser_skip_to_pragma_eol. */
2476
2477 static void
2478 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2479 {
2480 parser->lexer->in_pragma = false;
2481 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2483 }
2484
2485 /* This is a simple wrapper around make_typename_type. When the id is
2486 an unresolved identifier node, we can provide a superior diagnostic
2487 using cp_parser_diagnose_invalid_type_name. */
2488
2489 static tree
2490 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2491 {
2492 tree result;
2493 if (TREE_CODE (id) == IDENTIFIER_NODE)
2494 {
2495 result = make_typename_type (scope, id, typename_type,
2496 /*complain=*/tf_none);
2497 if (result == error_mark_node)
2498 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2499 return result;
2500 }
2501 return make_typename_type (scope, id, typename_type, tf_error);
2502 }
2503
2504
2505 /* Create a new C++ parser. */
2506
2507 static cp_parser *
2508 cp_parser_new (void)
2509 {
2510 cp_parser *parser;
2511 cp_lexer *lexer;
2512 unsigned i;
2513
2514 /* cp_lexer_new_main is called before calling ggc_alloc because
2515 cp_lexer_new_main might load a PCH file. */
2516 lexer = cp_lexer_new_main ();
2517
2518 /* Initialize the binops_by_token so that we can get the tree
2519 directly from the token. */
2520 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2521 binops_by_token[binops[i].token_type] = binops[i];
2522
2523 parser = GGC_CNEW (cp_parser);
2524 parser->lexer = lexer;
2525 parser->context = cp_parser_context_new (NULL);
2526
2527 /* For now, we always accept GNU extensions. */
2528 parser->allow_gnu_extensions_p = 1;
2529
2530 /* The `>' token is a greater-than operator, not the end of a
2531 template-id. */
2532 parser->greater_than_is_operator_p = true;
2533
2534 parser->default_arg_ok_p = true;
2535
2536 /* We are not parsing a constant-expression. */
2537 parser->integral_constant_expression_p = false;
2538 parser->allow_non_integral_constant_expression_p = false;
2539 parser->non_integral_constant_expression_p = false;
2540
2541 /* Local variable names are not forbidden. */
2542 parser->local_variables_forbidden_p = false;
2543
2544 /* We are not processing an `extern "C"' declaration. */
2545 parser->in_unbraced_linkage_specification_p = false;
2546
2547 /* We are not processing a declarator. */
2548 parser->in_declarator_p = false;
2549
2550 /* We are not processing a template-argument-list. */
2551 parser->in_template_argument_list_p = false;
2552
2553 /* We are not in an iteration statement. */
2554 parser->in_statement = 0;
2555
2556 /* We are not in a switch statement. */
2557 parser->in_switch_statement_p = false;
2558
2559 /* We are not parsing a type-id inside an expression. */
2560 parser->in_type_id_in_expr_p = false;
2561
2562 /* Declarations aren't implicitly extern "C". */
2563 parser->implicit_extern_c = false;
2564
2565 /* String literals should be translated to the execution character set. */
2566 parser->translate_strings_p = true;
2567
2568 /* The unparsed function queue is empty. */
2569 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2570
2571 /* There are no classes being defined. */
2572 parser->num_classes_being_defined = 0;
2573
2574 /* No template parameters apply. */
2575 parser->num_template_parameter_lists = 0;
2576
2577 return parser;
2578 }
2579
2580 /* Create a cp_lexer structure which will emit the tokens in CACHE
2581 and push it onto the parser's lexer stack. This is used for delayed
2582 parsing of in-class method bodies and default arguments, and should
2583 not be confused with tentative parsing. */
2584 static void
2585 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2586 {
2587 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2588 lexer->next = parser->lexer;
2589 parser->lexer = lexer;
2590
2591 /* Move the current source position to that of the first token in the
2592 new lexer. */
2593 cp_lexer_set_source_position_from_token (lexer->next_token);
2594 }
2595
2596 /* Pop the top lexer off the parser stack. This is never used for the
2597 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2598 static void
2599 cp_parser_pop_lexer (cp_parser *parser)
2600 {
2601 cp_lexer *lexer = parser->lexer;
2602 parser->lexer = lexer->next;
2603 cp_lexer_destroy (lexer);
2604
2605 /* Put the current source position back where it was before this
2606 lexer was pushed. */
2607 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2608 }
2609
2610 /* Lexical conventions [gram.lex] */
2611
2612 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2613 identifier. */
2614
2615 static tree
2616 cp_parser_identifier (cp_parser* parser)
2617 {
2618 cp_token *token;
2619
2620 /* Look for the identifier. */
2621 token = cp_parser_require (parser, CPP_NAME, "identifier");
2622 /* Return the value. */
2623 return token ? token->value : error_mark_node;
2624 }
2625
2626 /* Parse a sequence of adjacent string constants. Returns a
2627 TREE_STRING representing the combined, nul-terminated string
2628 constant. If TRANSLATE is true, translate the string to the
2629 execution character set. If WIDE_OK is true, a wide string is
2630 invalid here.
2631
2632 C++98 [lex.string] says that if a narrow string literal token is
2633 adjacent to a wide string literal token, the behavior is undefined.
2634 However, C99 6.4.5p4 says that this results in a wide string literal.
2635 We follow C99 here, for consistency with the C front end.
2636
2637 This code is largely lifted from lex_string() in c-lex.c.
2638
2639 FUTURE: ObjC++ will need to handle @-strings here. */
2640 static tree
2641 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2642 {
2643 tree value;
2644 bool wide = false;
2645 size_t count;
2646 struct obstack str_ob;
2647 cpp_string str, istr, *strs;
2648 cp_token *tok;
2649
2650 tok = cp_lexer_peek_token (parser->lexer);
2651 if (!cp_parser_is_string_literal (tok))
2652 {
2653 cp_parser_error (parser, "expected string-literal");
2654 return error_mark_node;
2655 }
2656
2657 /* Try to avoid the overhead of creating and destroying an obstack
2658 for the common case of just one string. */
2659 if (!cp_parser_is_string_literal
2660 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2661 {
2662 cp_lexer_consume_token (parser->lexer);
2663
2664 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2665 str.len = TREE_STRING_LENGTH (tok->value);
2666 count = 1;
2667 if (tok->type == CPP_WSTRING)
2668 wide = true;
2669
2670 strs = &str;
2671 }
2672 else
2673 {
2674 gcc_obstack_init (&str_ob);
2675 count = 0;
2676
2677 do
2678 {
2679 cp_lexer_consume_token (parser->lexer);
2680 count++;
2681 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2682 str.len = TREE_STRING_LENGTH (tok->value);
2683 if (tok->type == CPP_WSTRING)
2684 wide = true;
2685
2686 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2687
2688 tok = cp_lexer_peek_token (parser->lexer);
2689 }
2690 while (cp_parser_is_string_literal (tok));
2691
2692 strs = (cpp_string *) obstack_finish (&str_ob);
2693 }
2694
2695 if (wide && !wide_ok)
2696 {
2697 cp_parser_error (parser, "a wide string is invalid in this context");
2698 wide = false;
2699 }
2700
2701 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2702 (parse_in, strs, count, &istr, wide))
2703 {
2704 value = build_string (istr.len, (char *)istr.text);
2705 free ((void *)istr.text);
2706
2707 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2708 value = fix_string_type (value);
2709 }
2710 else
2711 /* cpp_interpret_string has issued an error. */
2712 value = error_mark_node;
2713
2714 if (count > 1)
2715 obstack_free (&str_ob, 0);
2716
2717 return value;
2718 }
2719
2720
2721 /* Basic concepts [gram.basic] */
2722
2723 /* Parse a translation-unit.
2724
2725 translation-unit:
2726 declaration-seq [opt]
2727
2728 Returns TRUE if all went well. */
2729
2730 static bool
2731 cp_parser_translation_unit (cp_parser* parser)
2732 {
2733 /* The address of the first non-permanent object on the declarator
2734 obstack. */
2735 static void *declarator_obstack_base;
2736
2737 bool success;
2738
2739 /* Create the declarator obstack, if necessary. */
2740 if (!cp_error_declarator)
2741 {
2742 gcc_obstack_init (&declarator_obstack);
2743 /* Create the error declarator. */
2744 cp_error_declarator = make_declarator (cdk_error);
2745 /* Create the empty parameter list. */
2746 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2747 /* Remember where the base of the declarator obstack lies. */
2748 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2749 }
2750
2751 cp_parser_declaration_seq_opt (parser);
2752
2753 /* If there are no tokens left then all went well. */
2754 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2755 {
2756 /* Get rid of the token array; we don't need it any more. */
2757 cp_lexer_destroy (parser->lexer);
2758 parser->lexer = NULL;
2759
2760 /* This file might have been a context that's implicitly extern
2761 "C". If so, pop the lang context. (Only relevant for PCH.) */
2762 if (parser->implicit_extern_c)
2763 {
2764 pop_lang_context ();
2765 parser->implicit_extern_c = false;
2766 }
2767
2768 /* Finish up. */
2769 finish_translation_unit ();
2770
2771 success = true;
2772 }
2773 else
2774 {
2775 cp_parser_error (parser, "expected declaration");
2776 success = false;
2777 }
2778
2779 /* Make sure the declarator obstack was fully cleaned up. */
2780 gcc_assert (obstack_next_free (&declarator_obstack)
2781 == declarator_obstack_base);
2782
2783 /* All went well. */
2784 return success;
2785 }
2786
2787 /* Expressions [gram.expr] */
2788
2789 /* Parse a primary-expression.
2790
2791 primary-expression:
2792 literal
2793 this
2794 ( expression )
2795 id-expression
2796
2797 GNU Extensions:
2798
2799 primary-expression:
2800 ( compound-statement )
2801 __builtin_va_arg ( assignment-expression , type-id )
2802 __builtin_offsetof ( type-id , offsetof-expression )
2803
2804 Objective-C++ Extension:
2805
2806 primary-expression:
2807 objc-expression
2808
2809 literal:
2810 __null
2811
2812 ADDRESS_P is true iff this expression was immediately preceded by
2813 "&" and therefore might denote a pointer-to-member. CAST_P is true
2814 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2815 true iff this expression is a template argument.
2816
2817 Returns a representation of the expression. Upon return, *IDK
2818 indicates what kind of id-expression (if any) was present. */
2819
2820 static tree
2821 cp_parser_primary_expression (cp_parser *parser,
2822 bool address_p,
2823 bool cast_p,
2824 bool template_arg_p,
2825 cp_id_kind *idk)
2826 {
2827 cp_token *token;
2828
2829 /* Assume the primary expression is not an id-expression. */
2830 *idk = CP_ID_KIND_NONE;
2831
2832 /* Peek at the next token. */
2833 token = cp_lexer_peek_token (parser->lexer);
2834 switch (token->type)
2835 {
2836 /* literal:
2837 integer-literal
2838 character-literal
2839 floating-literal
2840 string-literal
2841 boolean-literal */
2842 case CPP_CHAR:
2843 case CPP_WCHAR:
2844 case CPP_NUMBER:
2845 token = cp_lexer_consume_token (parser->lexer);
2846 /* Floating-point literals are only allowed in an integral
2847 constant expression if they are cast to an integral or
2848 enumeration type. */
2849 if (TREE_CODE (token->value) == REAL_CST
2850 && parser->integral_constant_expression_p
2851 && pedantic)
2852 {
2853 /* CAST_P will be set even in invalid code like "int(2.7 +
2854 ...)". Therefore, we have to check that the next token
2855 is sure to end the cast. */
2856 if (cast_p)
2857 {
2858 cp_token *next_token;
2859
2860 next_token = cp_lexer_peek_token (parser->lexer);
2861 if (/* The comma at the end of an
2862 enumerator-definition. */
2863 next_token->type != CPP_COMMA
2864 /* The curly brace at the end of an enum-specifier. */
2865 && next_token->type != CPP_CLOSE_BRACE
2866 /* The end of a statement. */
2867 && next_token->type != CPP_SEMICOLON
2868 /* The end of the cast-expression. */
2869 && next_token->type != CPP_CLOSE_PAREN
2870 /* The end of an array bound. */
2871 && next_token->type != CPP_CLOSE_SQUARE
2872 /* The closing ">" in a template-argument-list. */
2873 && (next_token->type != CPP_GREATER
2874 || parser->greater_than_is_operator_p))
2875 cast_p = false;
2876 }
2877
2878 /* If we are within a cast, then the constraint that the
2879 cast is to an integral or enumeration type will be
2880 checked at that point. If we are not within a cast, then
2881 this code is invalid. */
2882 if (!cast_p)
2883 cp_parser_non_integral_constant_expression
2884 (parser, "floating-point literal");
2885 }
2886 return token->value;
2887
2888 case CPP_STRING:
2889 case CPP_WSTRING:
2890 /* ??? Should wide strings be allowed when parser->translate_strings_p
2891 is false (i.e. in attributes)? If not, we can kill the third
2892 argument to cp_parser_string_literal. */
2893 return cp_parser_string_literal (parser,
2894 parser->translate_strings_p,
2895 true);
2896
2897 case CPP_OPEN_PAREN:
2898 {
2899 tree expr;
2900 bool saved_greater_than_is_operator_p;
2901
2902 /* Consume the `('. */
2903 cp_lexer_consume_token (parser->lexer);
2904 /* Within a parenthesized expression, a `>' token is always
2905 the greater-than operator. */
2906 saved_greater_than_is_operator_p
2907 = parser->greater_than_is_operator_p;
2908 parser->greater_than_is_operator_p = true;
2909 /* If we see `( { ' then we are looking at the beginning of
2910 a GNU statement-expression. */
2911 if (cp_parser_allow_gnu_extensions_p (parser)
2912 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2913 {
2914 /* Statement-expressions are not allowed by the standard. */
2915 if (pedantic)
2916 pedwarn ("ISO C++ forbids braced-groups within expressions");
2917
2918 /* And they're not allowed outside of a function-body; you
2919 cannot, for example, write:
2920
2921 int i = ({ int j = 3; j + 1; });
2922
2923 at class or namespace scope. */
2924 if (!at_function_scope_p ())
2925 error ("statement-expressions are allowed only inside functions");
2926 /* Start the statement-expression. */
2927 expr = begin_stmt_expr ();
2928 /* Parse the compound-statement. */
2929 cp_parser_compound_statement (parser, expr, false);
2930 /* Finish up. */
2931 expr = finish_stmt_expr (expr, false);
2932 }
2933 else
2934 {
2935 /* Parse the parenthesized expression. */
2936 expr = cp_parser_expression (parser, cast_p);
2937 /* Let the front end know that this expression was
2938 enclosed in parentheses. This matters in case, for
2939 example, the expression is of the form `A::B', since
2940 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2941 not. */
2942 finish_parenthesized_expr (expr);
2943 }
2944 /* The `>' token might be the end of a template-id or
2945 template-parameter-list now. */
2946 parser->greater_than_is_operator_p
2947 = saved_greater_than_is_operator_p;
2948 /* Consume the `)'. */
2949 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2950 cp_parser_skip_to_end_of_statement (parser);
2951
2952 return expr;
2953 }
2954
2955 case CPP_KEYWORD:
2956 switch (token->keyword)
2957 {
2958 /* These two are the boolean literals. */
2959 case RID_TRUE:
2960 cp_lexer_consume_token (parser->lexer);
2961 return boolean_true_node;
2962 case RID_FALSE:
2963 cp_lexer_consume_token (parser->lexer);
2964 return boolean_false_node;
2965
2966 /* The `__null' literal. */
2967 case RID_NULL:
2968 cp_lexer_consume_token (parser->lexer);
2969 return null_node;
2970
2971 /* Recognize the `this' keyword. */
2972 case RID_THIS:
2973 cp_lexer_consume_token (parser->lexer);
2974 if (parser->local_variables_forbidden_p)
2975 {
2976 error ("%<this%> may not be used in this context");
2977 return error_mark_node;
2978 }
2979 /* Pointers cannot appear in constant-expressions. */
2980 if (cp_parser_non_integral_constant_expression (parser,
2981 "`this'"))
2982 return error_mark_node;
2983 return finish_this_expr ();
2984
2985 /* The `operator' keyword can be the beginning of an
2986 id-expression. */
2987 case RID_OPERATOR:
2988 goto id_expression;
2989
2990 case RID_FUNCTION_NAME:
2991 case RID_PRETTY_FUNCTION_NAME:
2992 case RID_C99_FUNCTION_NAME:
2993 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2994 __func__ are the names of variables -- but they are
2995 treated specially. Therefore, they are handled here,
2996 rather than relying on the generic id-expression logic
2997 below. Grammatically, these names are id-expressions.
2998
2999 Consume the token. */
3000 token = cp_lexer_consume_token (parser->lexer);
3001 /* Look up the name. */
3002 return finish_fname (token->value);
3003
3004 case RID_VA_ARG:
3005 {
3006 tree expression;
3007 tree type;
3008
3009 /* The `__builtin_va_arg' construct is used to handle
3010 `va_arg'. Consume the `__builtin_va_arg' token. */
3011 cp_lexer_consume_token (parser->lexer);
3012 /* Look for the opening `('. */
3013 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3014 /* Now, parse the assignment-expression. */
3015 expression = cp_parser_assignment_expression (parser,
3016 /*cast_p=*/false);
3017 /* Look for the `,'. */
3018 cp_parser_require (parser, CPP_COMMA, "`,'");
3019 /* Parse the type-id. */
3020 type = cp_parser_type_id (parser);
3021 /* Look for the closing `)'. */
3022 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3023 /* Using `va_arg' in a constant-expression is not
3024 allowed. */
3025 if (cp_parser_non_integral_constant_expression (parser,
3026 "`va_arg'"))
3027 return error_mark_node;
3028 return build_x_va_arg (expression, type);
3029 }
3030
3031 case RID_OFFSETOF:
3032 return cp_parser_builtin_offsetof (parser);
3033
3034 /* Objective-C++ expressions. */
3035 case RID_AT_ENCODE:
3036 case RID_AT_PROTOCOL:
3037 case RID_AT_SELECTOR:
3038 return cp_parser_objc_expression (parser);
3039
3040 default:
3041 cp_parser_error (parser, "expected primary-expression");
3042 return error_mark_node;
3043 }
3044
3045 /* An id-expression can start with either an identifier, a
3046 `::' as the beginning of a qualified-id, or the "operator"
3047 keyword. */
3048 case CPP_NAME:
3049 case CPP_SCOPE:
3050 case CPP_TEMPLATE_ID:
3051 case CPP_NESTED_NAME_SPECIFIER:
3052 {
3053 tree id_expression;
3054 tree decl;
3055 const char *error_msg;
3056 bool template_p;
3057 bool done;
3058
3059 id_expression:
3060 /* Parse the id-expression. */
3061 id_expression
3062 = cp_parser_id_expression (parser,
3063 /*template_keyword_p=*/false,
3064 /*check_dependency_p=*/true,
3065 &template_p,
3066 /*declarator_p=*/false,
3067 /*optional_p=*/false);
3068 if (id_expression == error_mark_node)
3069 return error_mark_node;
3070 token = cp_lexer_peek_token (parser->lexer);
3071 done = (token->type != CPP_OPEN_SQUARE
3072 && token->type != CPP_OPEN_PAREN
3073 && token->type != CPP_DOT
3074 && token->type != CPP_DEREF
3075 && token->type != CPP_PLUS_PLUS
3076 && token->type != CPP_MINUS_MINUS);
3077 /* If we have a template-id, then no further lookup is
3078 required. If the template-id was for a template-class, we
3079 will sometimes have a TYPE_DECL at this point. */
3080 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3081 || TREE_CODE (id_expression) == TYPE_DECL)
3082 decl = id_expression;
3083 /* Look up the name. */
3084 else
3085 {
3086 tree ambiguous_decls;
3087
3088 decl = cp_parser_lookup_name (parser, id_expression,
3089 none_type,
3090 template_p,
3091 /*is_namespace=*/false,
3092 /*check_dependency=*/true,
3093 &ambiguous_decls);
3094 /* If the lookup was ambiguous, an error will already have
3095 been issued. */
3096 if (ambiguous_decls)
3097 return error_mark_node;
3098
3099 /* In Objective-C++, an instance variable (ivar) may be preferred
3100 to whatever cp_parser_lookup_name() found. */
3101 decl = objc_lookup_ivar (decl, id_expression);
3102
3103 /* If name lookup gives us a SCOPE_REF, then the
3104 qualifying scope was dependent. */
3105 if (TREE_CODE (decl) == SCOPE_REF)
3106 return decl;
3107 /* Check to see if DECL is a local variable in a context
3108 where that is forbidden. */
3109 if (parser->local_variables_forbidden_p
3110 && local_variable_p (decl))
3111 {
3112 /* It might be that we only found DECL because we are
3113 trying to be generous with pre-ISO scoping rules.
3114 For example, consider:
3115
3116 int i;
3117 void g() {
3118 for (int i = 0; i < 10; ++i) {}
3119 extern void f(int j = i);
3120 }
3121
3122 Here, name look up will originally find the out
3123 of scope `i'. We need to issue a warning message,
3124 but then use the global `i'. */
3125 decl = check_for_out_of_scope_variable (decl);
3126 if (local_variable_p (decl))
3127 {
3128 error ("local variable %qD may not appear in this context",
3129 decl);
3130 return error_mark_node;
3131 }
3132 }
3133 }
3134
3135 decl = (finish_id_expression
3136 (id_expression, decl, parser->scope,
3137 idk,
3138 parser->integral_constant_expression_p,
3139 parser->allow_non_integral_constant_expression_p,
3140 &parser->non_integral_constant_expression_p,
3141 template_p, done, address_p,
3142 template_arg_p,
3143 &error_msg));
3144 if (error_msg)
3145 cp_parser_error (parser, error_msg);
3146 return decl;
3147 }
3148
3149 /* Anything else is an error. */
3150 default:
3151 /* ...unless we have an Objective-C++ message or string literal, that is. */
3152 if (c_dialect_objc ()
3153 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3154 return cp_parser_objc_expression (parser);
3155
3156 cp_parser_error (parser, "expected primary-expression");
3157 return error_mark_node;
3158 }
3159 }
3160
3161 /* Parse an id-expression.
3162
3163 id-expression:
3164 unqualified-id
3165 qualified-id
3166
3167 qualified-id:
3168 :: [opt] nested-name-specifier template [opt] unqualified-id
3169 :: identifier
3170 :: operator-function-id
3171 :: template-id
3172
3173 Return a representation of the unqualified portion of the
3174 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3175 a `::' or nested-name-specifier.
3176
3177 Often, if the id-expression was a qualified-id, the caller will
3178 want to make a SCOPE_REF to represent the qualified-id. This
3179 function does not do this in order to avoid wastefully creating
3180 SCOPE_REFs when they are not required.
3181
3182 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3183 `template' keyword.
3184
3185 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3186 uninstantiated templates.
3187
3188 If *TEMPLATE_P is non-NULL, it is set to true iff the
3189 `template' keyword is used to explicitly indicate that the entity
3190 named is a template.
3191
3192 If DECLARATOR_P is true, the id-expression is appearing as part of
3193 a declarator, rather than as part of an expression. */
3194
3195 static tree
3196 cp_parser_id_expression (cp_parser *parser,
3197 bool template_keyword_p,
3198 bool check_dependency_p,
3199 bool *template_p,
3200 bool declarator_p,
3201 bool optional_p)
3202 {
3203 bool global_scope_p;
3204 bool nested_name_specifier_p;
3205
3206 /* Assume the `template' keyword was not used. */
3207 if (template_p)
3208 *template_p = template_keyword_p;
3209
3210 /* Look for the optional `::' operator. */
3211 global_scope_p
3212 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3213 != NULL_TREE);
3214 /* Look for the optional nested-name-specifier. */
3215 nested_name_specifier_p
3216 = (cp_parser_nested_name_specifier_opt (parser,
3217 /*typename_keyword_p=*/false,
3218 check_dependency_p,
3219 /*type_p=*/false,
3220 declarator_p)
3221 != NULL_TREE);
3222 /* If there is a nested-name-specifier, then we are looking at
3223 the first qualified-id production. */
3224 if (nested_name_specifier_p)
3225 {
3226 tree saved_scope;
3227 tree saved_object_scope;
3228 tree saved_qualifying_scope;
3229 tree unqualified_id;
3230 bool is_template;
3231
3232 /* See if the next token is the `template' keyword. */
3233 if (!template_p)
3234 template_p = &is_template;
3235 *template_p = cp_parser_optional_template_keyword (parser);
3236 /* Name lookup we do during the processing of the
3237 unqualified-id might obliterate SCOPE. */
3238 saved_scope = parser->scope;
3239 saved_object_scope = parser->object_scope;
3240 saved_qualifying_scope = parser->qualifying_scope;
3241 /* Process the final unqualified-id. */
3242 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3243 check_dependency_p,
3244 declarator_p,
3245 /*optional_p=*/false);
3246 /* Restore the SAVED_SCOPE for our caller. */
3247 parser->scope = saved_scope;
3248 parser->object_scope = saved_object_scope;
3249 parser->qualifying_scope = saved_qualifying_scope;
3250
3251 return unqualified_id;
3252 }
3253 /* Otherwise, if we are in global scope, then we are looking at one
3254 of the other qualified-id productions. */
3255 else if (global_scope_p)
3256 {
3257 cp_token *token;
3258 tree id;
3259
3260 /* Peek at the next token. */
3261 token = cp_lexer_peek_token (parser->lexer);
3262
3263 /* If it's an identifier, and the next token is not a "<", then
3264 we can avoid the template-id case. This is an optimization
3265 for this common case. */
3266 if (token->type == CPP_NAME
3267 && !cp_parser_nth_token_starts_template_argument_list_p
3268 (parser, 2))
3269 return cp_parser_identifier (parser);
3270
3271 cp_parser_parse_tentatively (parser);
3272 /* Try a template-id. */
3273 id = cp_parser_template_id (parser,
3274 /*template_keyword_p=*/false,
3275 /*check_dependency_p=*/true,
3276 declarator_p);
3277 /* If that worked, we're done. */
3278 if (cp_parser_parse_definitely (parser))
3279 return id;
3280
3281 /* Peek at the next token. (Changes in the token buffer may
3282 have invalidated the pointer obtained above.) */
3283 token = cp_lexer_peek_token (parser->lexer);
3284
3285 switch (token->type)
3286 {
3287 case CPP_NAME:
3288 return cp_parser_identifier (parser);
3289
3290 case CPP_KEYWORD:
3291 if (token->keyword == RID_OPERATOR)
3292 return cp_parser_operator_function_id (parser);
3293 /* Fall through. */
3294
3295 default:
3296 cp_parser_error (parser, "expected id-expression");
3297 return error_mark_node;
3298 }
3299 }
3300 else
3301 return cp_parser_unqualified_id (parser, template_keyword_p,
3302 /*check_dependency_p=*/true,
3303 declarator_p,
3304 optional_p);
3305 }
3306
3307 /* Parse an unqualified-id.
3308
3309 unqualified-id:
3310 identifier
3311 operator-function-id
3312 conversion-function-id
3313 ~ class-name
3314 template-id
3315
3316 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3317 keyword, in a construct like `A::template ...'.
3318
3319 Returns a representation of unqualified-id. For the `identifier'
3320 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3321 production a BIT_NOT_EXPR is returned; the operand of the
3322 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3323 other productions, see the documentation accompanying the
3324 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3325 names are looked up in uninstantiated templates. If DECLARATOR_P
3326 is true, the unqualified-id is appearing as part of a declarator,
3327 rather than as part of an expression. */
3328
3329 static tree
3330 cp_parser_unqualified_id (cp_parser* parser,
3331 bool template_keyword_p,
3332 bool check_dependency_p,
3333 bool declarator_p,
3334 bool optional_p)
3335 {
3336 cp_token *token;
3337
3338 /* Peek at the next token. */
3339 token = cp_lexer_peek_token (parser->lexer);
3340
3341 switch (token->type)
3342 {
3343 case CPP_NAME:
3344 {
3345 tree id;
3346
3347 /* We don't know yet whether or not this will be a
3348 template-id. */
3349 cp_parser_parse_tentatively (parser);
3350 /* Try a template-id. */
3351 id = cp_parser_template_id (parser, template_keyword_p,
3352 check_dependency_p,
3353 declarator_p);
3354 /* If it worked, we're done. */
3355 if (cp_parser_parse_definitely (parser))
3356 return id;
3357 /* Otherwise, it's an ordinary identifier. */
3358 return cp_parser_identifier (parser);
3359 }
3360
3361 case CPP_TEMPLATE_ID:
3362 return cp_parser_template_id (parser, template_keyword_p,
3363 check_dependency_p,
3364 declarator_p);
3365
3366 case CPP_COMPL:
3367 {
3368 tree type_decl;
3369 tree qualifying_scope;
3370 tree object_scope;
3371 tree scope;
3372 bool done;
3373
3374 /* Consume the `~' token. */
3375 cp_lexer_consume_token (parser->lexer);
3376 /* Parse the class-name. The standard, as written, seems to
3377 say that:
3378
3379 template <typename T> struct S { ~S (); };
3380 template <typename T> S<T>::~S() {}
3381
3382 is invalid, since `~' must be followed by a class-name, but
3383 `S<T>' is dependent, and so not known to be a class.
3384 That's not right; we need to look in uninstantiated
3385 templates. A further complication arises from:
3386
3387 template <typename T> void f(T t) {
3388 t.T::~T();
3389 }
3390
3391 Here, it is not possible to look up `T' in the scope of `T'
3392 itself. We must look in both the current scope, and the
3393 scope of the containing complete expression.
3394
3395 Yet another issue is:
3396
3397 struct S {
3398 int S;
3399 ~S();
3400 };
3401
3402 S::~S() {}
3403
3404 The standard does not seem to say that the `S' in `~S'
3405 should refer to the type `S' and not the data member
3406 `S::S'. */
3407
3408 /* DR 244 says that we look up the name after the "~" in the
3409 same scope as we looked up the qualifying name. That idea
3410 isn't fully worked out; it's more complicated than that. */
3411 scope = parser->scope;
3412 object_scope = parser->object_scope;
3413 qualifying_scope = parser->qualifying_scope;
3414
3415 /* Check for invalid scopes. */
3416 if (scope == error_mark_node)
3417 {
3418 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3419 cp_lexer_consume_token (parser->lexer);
3420 return error_mark_node;
3421 }
3422 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3423 {
3424 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3425 error ("scope %qT before %<~%> is not a class-name", scope);
3426 cp_parser_simulate_error (parser);
3427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3428 cp_lexer_consume_token (parser->lexer);
3429 return error_mark_node;
3430 }
3431 gcc_assert (!scope || TYPE_P (scope));
3432
3433 /* If the name is of the form "X::~X" it's OK. */
3434 token = cp_lexer_peek_token (parser->lexer);
3435 if (scope
3436 && token->type == CPP_NAME
3437 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3438 == CPP_OPEN_PAREN)
3439 && constructor_name_p (token->value, scope))
3440 {
3441 cp_lexer_consume_token (parser->lexer);
3442 return build_nt (BIT_NOT_EXPR, scope);
3443 }
3444
3445 /* If there was an explicit qualification (S::~T), first look
3446 in the scope given by the qualification (i.e., S). */
3447 done = false;
3448 type_decl = NULL_TREE;
3449 if (scope)
3450 {
3451 cp_parser_parse_tentatively (parser);
3452 type_decl = cp_parser_class_name (parser,
3453 /*typename_keyword_p=*/false,
3454 /*template_keyword_p=*/false,
3455 none_type,
3456 /*check_dependency=*/false,
3457 /*class_head_p=*/false,
3458 declarator_p);
3459 if (cp_parser_parse_definitely (parser))
3460 done = true;
3461 }
3462 /* In "N::S::~S", look in "N" as well. */
3463 if (!done && scope && qualifying_scope)
3464 {
3465 cp_parser_parse_tentatively (parser);
3466 parser->scope = qualifying_scope;
3467 parser->object_scope = NULL_TREE;
3468 parser->qualifying_scope = NULL_TREE;
3469 type_decl
3470 = 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 "p->S::~T", look in the scope given by "*p" as well. */
3481 else if (!done && object_scope)
3482 {
3483 cp_parser_parse_tentatively (parser);
3484 parser->scope = object_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 /* Look in the surrounding context. */
3499 if (!done)
3500 {
3501 parser->scope = NULL_TREE;
3502 parser->object_scope = NULL_TREE;
3503 parser->qualifying_scope = NULL_TREE;
3504 type_decl
3505 = cp_parser_class_name (parser,
3506 /*typename_keyword_p=*/false,
3507 /*template_keyword_p=*/false,
3508 none_type,
3509 /*check_dependency=*/false,
3510 /*class_head_p=*/false,
3511 declarator_p);
3512 }
3513 /* If an error occurred, assume that the name of the
3514 destructor is the same as the name of the qualifying
3515 class. That allows us to keep parsing after running
3516 into ill-formed destructor names. */
3517 if (type_decl == error_mark_node && scope)
3518 return build_nt (BIT_NOT_EXPR, scope);
3519 else if (type_decl == error_mark_node)
3520 return error_mark_node;
3521
3522 /* Check that destructor name and scope match. */
3523 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3524 {
3525 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3526 error ("declaration of %<~%T%> as member of %qT",
3527 type_decl, scope);
3528 cp_parser_simulate_error (parser);
3529 return error_mark_node;
3530 }
3531
3532 /* [class.dtor]
3533
3534 A typedef-name that names a class shall not be used as the
3535 identifier in the declarator for a destructor declaration. */
3536 if (declarator_p
3537 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3538 && !DECL_SELF_REFERENCE_P (type_decl)
3539 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3540 error ("typedef-name %qD used as destructor declarator",
3541 type_decl);
3542
3543 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3544 }
3545
3546 case CPP_KEYWORD:
3547 if (token->keyword == RID_OPERATOR)
3548 {
3549 tree id;
3550
3551 /* This could be a template-id, so we try that first. */
3552 cp_parser_parse_tentatively (parser);
3553 /* Try a template-id. */
3554 id = cp_parser_template_id (parser, template_keyword_p,
3555 /*check_dependency_p=*/true,
3556 declarator_p);
3557 /* If that worked, we're done. */
3558 if (cp_parser_parse_definitely (parser))
3559 return id;
3560 /* We still don't know whether we're looking at an
3561 operator-function-id or a conversion-function-id. */
3562 cp_parser_parse_tentatively (parser);
3563 /* Try an operator-function-id. */
3564 id = cp_parser_operator_function_id (parser);
3565 /* If that didn't work, try a conversion-function-id. */
3566 if (!cp_parser_parse_definitely (parser))
3567 id = cp_parser_conversion_function_id (parser);
3568
3569 return id;
3570 }
3571 /* Fall through. */
3572
3573 default:
3574 if (optional_p)
3575 return NULL_TREE;
3576 cp_parser_error (parser, "expected unqualified-id");
3577 return error_mark_node;
3578 }
3579 }
3580
3581 /* Parse an (optional) nested-name-specifier.
3582
3583 nested-name-specifier:
3584 class-or-namespace-name :: nested-name-specifier [opt]
3585 class-or-namespace-name :: template nested-name-specifier [opt]
3586
3587 PARSER->SCOPE should be set appropriately before this function is
3588 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3589 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3590 in name lookups.
3591
3592 Sets PARSER->SCOPE to the class (TYPE) or namespace
3593 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3594 it unchanged if there is no nested-name-specifier. Returns the new
3595 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3596
3597 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3598 part of a declaration and/or decl-specifier. */
3599
3600 static tree
3601 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3602 bool typename_keyword_p,
3603 bool check_dependency_p,
3604 bool type_p,
3605 bool is_declaration)
3606 {
3607 bool success = false;
3608 cp_token_position start = 0;
3609 cp_token *token;
3610
3611 /* Remember where the nested-name-specifier starts. */
3612 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3613 {
3614 start = cp_lexer_token_position (parser->lexer, false);
3615 push_deferring_access_checks (dk_deferred);
3616 }
3617
3618 while (true)
3619 {
3620 tree new_scope;
3621 tree old_scope;
3622 tree saved_qualifying_scope;
3623 bool template_keyword_p;
3624
3625 /* Spot cases that cannot be the beginning of a
3626 nested-name-specifier. */
3627 token = cp_lexer_peek_token (parser->lexer);
3628
3629 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3630 the already parsed nested-name-specifier. */
3631 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3632 {
3633 /* Grab the nested-name-specifier and continue the loop. */
3634 cp_parser_pre_parsed_nested_name_specifier (parser);
3635 /* If we originally encountered this nested-name-specifier
3636 with IS_DECLARATION set to false, we will not have
3637 resolved TYPENAME_TYPEs, so we must do so here. */
3638 if (is_declaration
3639 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3640 {
3641 new_scope = resolve_typename_type (parser->scope,
3642 /*only_current_p=*/false);
3643 if (new_scope != error_mark_node)
3644 parser->scope = new_scope;
3645 }
3646 success = true;
3647 continue;
3648 }
3649
3650 /* Spot cases that cannot be the beginning of a
3651 nested-name-specifier. On the second and subsequent times
3652 through the loop, we look for the `template' keyword. */
3653 if (success && token->keyword == RID_TEMPLATE)
3654 ;
3655 /* A template-id can start a nested-name-specifier. */
3656 else if (token->type == CPP_TEMPLATE_ID)
3657 ;
3658 else
3659 {
3660 /* If the next token is not an identifier, then it is
3661 definitely not a class-or-namespace-name. */
3662 if (token->type != CPP_NAME)
3663 break;
3664 /* If the following token is neither a `<' (to begin a
3665 template-id), nor a `::', then we are not looking at a
3666 nested-name-specifier. */
3667 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3668 if (token->type != CPP_SCOPE
3669 && !cp_parser_nth_token_starts_template_argument_list_p
3670 (parser, 2))
3671 break;
3672 }
3673
3674 /* The nested-name-specifier is optional, so we parse
3675 tentatively. */
3676 cp_parser_parse_tentatively (parser);
3677
3678 /* Look for the optional `template' keyword, if this isn't the
3679 first time through the loop. */
3680 if (success)
3681 template_keyword_p = cp_parser_optional_template_keyword (parser);
3682 else
3683 template_keyword_p = false;
3684
3685 /* Save the old scope since the name lookup we are about to do
3686 might destroy it. */
3687 old_scope = parser->scope;
3688 saved_qualifying_scope = parser->qualifying_scope;
3689 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3690 look up names in "X<T>::I" in order to determine that "Y" is
3691 a template. So, if we have a typename at this point, we make
3692 an effort to look through it. */
3693 if (is_declaration
3694 && !typename_keyword_p
3695 && parser->scope
3696 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3697 parser->scope = resolve_typename_type (parser->scope,
3698 /*only_current_p=*/false);
3699 /* Parse the qualifying entity. */
3700 new_scope
3701 = cp_parser_class_or_namespace_name (parser,
3702 typename_keyword_p,
3703 template_keyword_p,
3704 check_dependency_p,
3705 type_p,
3706 is_declaration);
3707 /* Look for the `::' token. */
3708 cp_parser_require (parser, CPP_SCOPE, "`::'");
3709
3710 /* If we found what we wanted, we keep going; otherwise, we're
3711 done. */
3712 if (!cp_parser_parse_definitely (parser))
3713 {
3714 bool error_p = false;
3715
3716 /* Restore the OLD_SCOPE since it was valid before the
3717 failed attempt at finding the last
3718 class-or-namespace-name. */
3719 parser->scope = old_scope;
3720 parser->qualifying_scope = saved_qualifying_scope;
3721 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3722 break;
3723 /* If the next token is an identifier, and the one after
3724 that is a `::', then any valid interpretation would have
3725 found a class-or-namespace-name. */
3726 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3727 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3728 == CPP_SCOPE)
3729 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3730 != CPP_COMPL))
3731 {
3732 token = cp_lexer_consume_token (parser->lexer);
3733 if (!error_p)
3734 {
3735 if (!token->ambiguous_p)
3736 {
3737 tree decl;
3738 tree ambiguous_decls;
3739
3740 decl = cp_parser_lookup_name (parser, token->value,
3741 none_type,
3742 /*is_template=*/false,
3743 /*is_namespace=*/false,
3744 /*check_dependency=*/true,
3745 &ambiguous_decls);
3746 if (TREE_CODE (decl) == TEMPLATE_DECL)
3747 error ("%qD used without template parameters", decl);
3748 else if (ambiguous_decls)
3749 {
3750 error ("reference to %qD is ambiguous",
3751 token->value);
3752 print_candidates (ambiguous_decls);
3753 decl = error_mark_node;
3754 }
3755 else
3756 cp_parser_name_lookup_error
3757 (parser, token->value, decl,
3758 "is not a class or namespace");
3759 }
3760 parser->scope = error_mark_node;
3761 error_p = true;
3762 /* Treat this as a successful nested-name-specifier
3763 due to:
3764
3765 [basic.lookup.qual]
3766
3767 If the name found is not a class-name (clause
3768 _class_) or namespace-name (_namespace.def_), the
3769 program is ill-formed. */
3770 success = true;
3771 }
3772 cp_lexer_consume_token (parser->lexer);
3773 }
3774 break;
3775 }
3776 /* We've found one valid nested-name-specifier. */
3777 success = true;
3778 /* Name lookup always gives us a DECL. */
3779 if (TREE_CODE (new_scope) == TYPE_DECL)
3780 new_scope = TREE_TYPE (new_scope);
3781 /* Uses of "template" must be followed by actual templates. */
3782 if (template_keyword_p
3783 && !(CLASS_TYPE_P (new_scope)
3784 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3785 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3786 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3787 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3788 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3789 == TEMPLATE_ID_EXPR)))
3790 pedwarn (TYPE_P (new_scope)
3791 ? "%qT is not a template"
3792 : "%qD is not a template",
3793 new_scope);
3794 /* If it is a class scope, try to complete it; we are about to
3795 be looking up names inside the class. */
3796 if (TYPE_P (new_scope)
3797 /* Since checking types for dependency can be expensive,
3798 avoid doing it if the type is already complete. */
3799 && !COMPLETE_TYPE_P (new_scope)
3800 /* Do not try to complete dependent types. */
3801 && !dependent_type_p (new_scope))
3802 new_scope = complete_type (new_scope);
3803 /* Make sure we look in the right scope the next time through
3804 the loop. */
3805 parser->scope = new_scope;
3806 }
3807
3808 /* If parsing tentatively, replace the sequence of tokens that makes
3809 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3810 token. That way, should we re-parse the token stream, we will
3811 not have to repeat the effort required to do the parse, nor will
3812 we issue duplicate error messages. */
3813 if (success && start)
3814 {
3815 cp_token *token;
3816 tree access_checks;
3817
3818 token = cp_lexer_token_at (parser->lexer, start);
3819 /* Reset the contents of the START token. */
3820 token->type = CPP_NESTED_NAME_SPECIFIER;
3821 /* Retrieve any deferred checks. Do not pop this access checks yet
3822 so the memory will not be reclaimed during token replacing below. */
3823 access_checks = get_deferred_access_checks ();
3824 token->value = build_tree_list (copy_list (access_checks),
3825 parser->scope);
3826 TREE_TYPE (token->value) = parser->qualifying_scope;
3827 token->keyword = RID_MAX;
3828
3829 /* Purge all subsequent tokens. */
3830 cp_lexer_purge_tokens_after (parser->lexer, start);
3831 }
3832
3833 if (start)
3834 pop_to_parent_deferring_access_checks ();
3835
3836 return success ? parser->scope : NULL_TREE;
3837 }
3838
3839 /* Parse a nested-name-specifier. See
3840 cp_parser_nested_name_specifier_opt for details. This function
3841 behaves identically, except that it will an issue an error if no
3842 nested-name-specifier is present. */
3843
3844 static tree
3845 cp_parser_nested_name_specifier (cp_parser *parser,
3846 bool typename_keyword_p,
3847 bool check_dependency_p,
3848 bool type_p,
3849 bool is_declaration)
3850 {
3851 tree scope;
3852
3853 /* Look for the nested-name-specifier. */
3854 scope = cp_parser_nested_name_specifier_opt (parser,
3855 typename_keyword_p,
3856 check_dependency_p,
3857 type_p,
3858 is_declaration);
3859 /* If it was not present, issue an error message. */
3860 if (!scope)
3861 {
3862 cp_parser_error (parser, "expected nested-name-specifier");
3863 parser->scope = NULL_TREE;
3864 }
3865
3866 return scope;
3867 }
3868
3869 /* Parse a class-or-namespace-name.
3870
3871 class-or-namespace-name:
3872 class-name
3873 namespace-name
3874
3875 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3876 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3877 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3878 TYPE_P is TRUE iff the next name should be taken as a class-name,
3879 even the same name is declared to be another entity in the same
3880 scope.
3881
3882 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3883 specified by the class-or-namespace-name. If neither is found the
3884 ERROR_MARK_NODE is returned. */
3885
3886 static tree
3887 cp_parser_class_or_namespace_name (cp_parser *parser,
3888 bool typename_keyword_p,
3889 bool template_keyword_p,
3890 bool check_dependency_p,
3891 bool type_p,
3892 bool is_declaration)
3893 {
3894 tree saved_scope;
3895 tree saved_qualifying_scope;
3896 tree saved_object_scope;
3897 tree scope;
3898 bool only_class_p;
3899
3900 /* Before we try to parse the class-name, we must save away the
3901 current PARSER->SCOPE since cp_parser_class_name will destroy
3902 it. */
3903 saved_scope = parser->scope;
3904 saved_qualifying_scope = parser->qualifying_scope;
3905 saved_object_scope = parser->object_scope;
3906 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3907 there is no need to look for a namespace-name. */
3908 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3909 if (!only_class_p)
3910 cp_parser_parse_tentatively (parser);
3911 scope = cp_parser_class_name (parser,
3912 typename_keyword_p,
3913 template_keyword_p,
3914 type_p ? class_type : none_type,
3915 check_dependency_p,
3916 /*class_head_p=*/false,
3917 is_declaration);
3918 /* If that didn't work, try for a namespace-name. */
3919 if (!only_class_p && !cp_parser_parse_definitely (parser))
3920 {
3921 /* Restore the saved scope. */
3922 parser->scope = saved_scope;
3923 parser->qualifying_scope = saved_qualifying_scope;
3924 parser->object_scope = saved_object_scope;
3925 /* If we are not looking at an identifier followed by the scope
3926 resolution operator, then this is not part of a
3927 nested-name-specifier. (Note that this function is only used
3928 to parse the components of a nested-name-specifier.) */
3929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3930 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3931 return error_mark_node;
3932 scope = cp_parser_namespace_name (parser);
3933 }
3934
3935 return scope;
3936 }
3937
3938 /* Parse a postfix-expression.
3939
3940 postfix-expression:
3941 primary-expression
3942 postfix-expression [ expression ]
3943 postfix-expression ( expression-list [opt] )
3944 simple-type-specifier ( expression-list [opt] )
3945 typename :: [opt] nested-name-specifier identifier
3946 ( expression-list [opt] )
3947 typename :: [opt] nested-name-specifier template [opt] template-id
3948 ( expression-list [opt] )
3949 postfix-expression . template [opt] id-expression
3950 postfix-expression -> template [opt] id-expression
3951 postfix-expression . pseudo-destructor-name
3952 postfix-expression -> pseudo-destructor-name
3953 postfix-expression ++
3954 postfix-expression --
3955 dynamic_cast < type-id > ( expression )
3956 static_cast < type-id > ( expression )
3957 reinterpret_cast < type-id > ( expression )
3958 const_cast < type-id > ( expression )
3959 typeid ( expression )
3960 typeid ( type-id )
3961
3962 GNU Extension:
3963
3964 postfix-expression:
3965 ( type-id ) { initializer-list , [opt] }
3966
3967 This extension is a GNU version of the C99 compound-literal
3968 construct. (The C99 grammar uses `type-name' instead of `type-id',
3969 but they are essentially the same concept.)
3970
3971 If ADDRESS_P is true, the postfix expression is the operand of the
3972 `&' operator. CAST_P is true if this expression is the target of a
3973 cast.
3974
3975 Returns a representation of the expression. */
3976
3977 static tree
3978 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3979 {
3980 cp_token *token;
3981 enum rid keyword;
3982 cp_id_kind idk = CP_ID_KIND_NONE;
3983 tree postfix_expression = NULL_TREE;
3984
3985 /* Peek at the next token. */
3986 token = cp_lexer_peek_token (parser->lexer);
3987 /* Some of the productions are determined by keywords. */
3988 keyword = token->keyword;
3989 switch (keyword)
3990 {
3991 case RID_DYNCAST:
3992 case RID_STATCAST:
3993 case RID_REINTCAST:
3994 case RID_CONSTCAST:
3995 {
3996 tree type;
3997 tree expression;
3998 const char *saved_message;
3999
4000 /* All of these can be handled in the same way from the point
4001 of view of parsing. Begin by consuming the token
4002 identifying the cast. */
4003 cp_lexer_consume_token (parser->lexer);
4004
4005 /* New types cannot be defined in the cast. */
4006 saved_message = parser->type_definition_forbidden_message;
4007 parser->type_definition_forbidden_message
4008 = "types may not be defined in casts";
4009
4010 /* Look for the opening `<'. */
4011 cp_parser_require (parser, CPP_LESS, "`<'");
4012 /* Parse the type to which we are casting. */
4013 type = cp_parser_type_id (parser);
4014 /* Look for the closing `>'. */
4015 cp_parser_require (parser, CPP_GREATER, "`>'");
4016 /* Restore the old message. */
4017 parser->type_definition_forbidden_message = saved_message;
4018
4019 /* And the expression which is being cast. */
4020 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4021 expression = cp_parser_expression (parser, /*cast_p=*/true);
4022 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4023
4024 /* Only type conversions to integral or enumeration types
4025 can be used in constant-expressions. */
4026 if (!cast_valid_in_integral_constant_expression_p (type)
4027 && (cp_parser_non_integral_constant_expression
4028 (parser,
4029 "a cast to a type other than an integral or "
4030 "enumeration type")))
4031 return error_mark_node;
4032
4033 switch (keyword)
4034 {
4035 case RID_DYNCAST:
4036 postfix_expression
4037 = build_dynamic_cast (type, expression);
4038 break;
4039 case RID_STATCAST:
4040 postfix_expression
4041 = build_static_cast (type, expression);
4042 break;
4043 case RID_REINTCAST:
4044 postfix_expression
4045 = build_reinterpret_cast (type, expression);
4046 break;
4047 case RID_CONSTCAST:
4048 postfix_expression
4049 = build_const_cast (type, expression);
4050 break;
4051 default:
4052 gcc_unreachable ();
4053 }
4054 }
4055 break;
4056
4057 case RID_TYPEID:
4058 {
4059 tree type;
4060 const char *saved_message;
4061 bool saved_in_type_id_in_expr_p;
4062
4063 /* Consume the `typeid' token. */
4064 cp_lexer_consume_token (parser->lexer);
4065 /* Look for the `(' token. */
4066 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4067 /* Types cannot be defined in a `typeid' expression. */
4068 saved_message = parser->type_definition_forbidden_message;
4069 parser->type_definition_forbidden_message
4070 = "types may not be defined in a `typeid\' expression";
4071 /* We can't be sure yet whether we're looking at a type-id or an
4072 expression. */
4073 cp_parser_parse_tentatively (parser);
4074 /* Try a type-id first. */
4075 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4076 parser->in_type_id_in_expr_p = true;
4077 type = cp_parser_type_id (parser);
4078 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4079 /* Look for the `)' token. Otherwise, we can't be sure that
4080 we're not looking at an expression: consider `typeid (int
4081 (3))', for example. */
4082 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4083 /* If all went well, simply lookup the type-id. */
4084 if (cp_parser_parse_definitely (parser))
4085 postfix_expression = get_typeid (type);
4086 /* Otherwise, fall back to the expression variant. */
4087 else
4088 {
4089 tree expression;
4090
4091 /* Look for an expression. */
4092 expression = cp_parser_expression (parser, /*cast_p=*/false);
4093 /* Compute its typeid. */
4094 postfix_expression = build_typeid (expression);
4095 /* Look for the `)' token. */
4096 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4097 }
4098 /* Restore the saved message. */
4099 parser->type_definition_forbidden_message = saved_message;
4100 /* `typeid' may not appear in an integral constant expression. */
4101 if (cp_parser_non_integral_constant_expression(parser,
4102 "`typeid' operator"))
4103 return error_mark_node;
4104 }
4105 break;
4106
4107 case RID_TYPENAME:
4108 {
4109 tree type;
4110 /* The syntax permitted here is the same permitted for an
4111 elaborated-type-specifier. */
4112 type = cp_parser_elaborated_type_specifier (parser,
4113 /*is_friend=*/false,
4114 /*is_declaration=*/false);
4115 postfix_expression = cp_parser_functional_cast (parser, type);
4116 }
4117 break;
4118
4119 default:
4120 {
4121 tree type;
4122
4123 /* If the next thing is a simple-type-specifier, we may be
4124 looking at a functional cast. We could also be looking at
4125 an id-expression. So, we try the functional cast, and if
4126 that doesn't work we fall back to the primary-expression. */
4127 cp_parser_parse_tentatively (parser);
4128 /* Look for the simple-type-specifier. */
4129 type = cp_parser_simple_type_specifier (parser,
4130 /*decl_specs=*/NULL,
4131 CP_PARSER_FLAGS_NONE);
4132 /* Parse the cast itself. */
4133 if (!cp_parser_error_occurred (parser))
4134 postfix_expression
4135 = cp_parser_functional_cast (parser, type);
4136 /* If that worked, we're done. */
4137 if (cp_parser_parse_definitely (parser))
4138 break;
4139
4140 /* If the functional-cast didn't work out, try a
4141 compound-literal. */
4142 if (cp_parser_allow_gnu_extensions_p (parser)
4143 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4144 {
4145 VEC(constructor_elt,gc) *initializer_list = NULL;
4146 bool saved_in_type_id_in_expr_p;
4147
4148 cp_parser_parse_tentatively (parser);
4149 /* Consume the `('. */
4150 cp_lexer_consume_token (parser->lexer);
4151 /* Parse the type. */
4152 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4153 parser->in_type_id_in_expr_p = true;
4154 type = cp_parser_type_id (parser);
4155 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4156 /* Look for the `)'. */
4157 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4158 /* Look for the `{'. */
4159 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4160 /* If things aren't going well, there's no need to
4161 keep going. */
4162 if (!cp_parser_error_occurred (parser))
4163 {
4164 bool non_constant_p;
4165 /* Parse the initializer-list. */
4166 initializer_list
4167 = cp_parser_initializer_list (parser, &non_constant_p);
4168 /* Allow a trailing `,'. */
4169 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4170 cp_lexer_consume_token (parser->lexer);
4171 /* Look for the final `}'. */
4172 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4173 }
4174 /* If that worked, we're definitely looking at a
4175 compound-literal expression. */
4176 if (cp_parser_parse_definitely (parser))
4177 {
4178 /* Warn the user that a compound literal is not
4179 allowed in standard C++. */
4180 if (pedantic)
4181 pedwarn ("ISO C++ forbids compound-literals");
4182 /* Form the representation of the compound-literal. */
4183 postfix_expression
4184 = finish_compound_literal (type, initializer_list);
4185 break;
4186 }
4187 }
4188
4189 /* It must be a primary-expression. */
4190 postfix_expression
4191 = cp_parser_primary_expression (parser, address_p, cast_p,
4192 /*template_arg_p=*/false,
4193 &idk);
4194 }
4195 break;
4196 }
4197
4198 /* Keep looping until the postfix-expression is complete. */
4199 while (true)
4200 {
4201 if (idk == CP_ID_KIND_UNQUALIFIED
4202 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4203 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4204 /* It is not a Koenig lookup function call. */
4205 postfix_expression
4206 = unqualified_name_lookup_error (postfix_expression);
4207
4208 /* Peek at the next token. */
4209 token = cp_lexer_peek_token (parser->lexer);
4210
4211 switch (token->type)
4212 {
4213 case CPP_OPEN_SQUARE:
4214 postfix_expression
4215 = cp_parser_postfix_open_square_expression (parser,
4216 postfix_expression,
4217 false);
4218 idk = CP_ID_KIND_NONE;
4219 break;
4220
4221 case CPP_OPEN_PAREN:
4222 /* postfix-expression ( expression-list [opt] ) */
4223 {
4224 bool koenig_p;
4225 bool is_builtin_constant_p;
4226 bool saved_integral_constant_expression_p = false;
4227 bool saved_non_integral_constant_expression_p = false;
4228 tree args;
4229
4230 is_builtin_constant_p
4231 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4232 if (is_builtin_constant_p)
4233 {
4234 /* The whole point of __builtin_constant_p is to allow
4235 non-constant expressions to appear as arguments. */
4236 saved_integral_constant_expression_p
4237 = parser->integral_constant_expression_p;
4238 saved_non_integral_constant_expression_p
4239 = parser->non_integral_constant_expression_p;
4240 parser->integral_constant_expression_p = false;
4241 }
4242 args = (cp_parser_parenthesized_expression_list
4243 (parser, /*is_attribute_list=*/false,
4244 /*cast_p=*/false,
4245 /*non_constant_p=*/NULL));
4246 if (is_builtin_constant_p)
4247 {
4248 parser->integral_constant_expression_p
4249 = saved_integral_constant_expression_p;
4250 parser->non_integral_constant_expression_p
4251 = saved_non_integral_constant_expression_p;
4252 }
4253
4254 if (args == error_mark_node)
4255 {
4256 postfix_expression = error_mark_node;
4257 break;
4258 }
4259
4260 /* Function calls are not permitted in
4261 constant-expressions. */
4262 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4263 && cp_parser_non_integral_constant_expression (parser,
4264 "a function call"))
4265 {
4266 postfix_expression = error_mark_node;
4267 break;
4268 }
4269
4270 koenig_p = false;
4271 if (idk == CP_ID_KIND_UNQUALIFIED)
4272 {
4273 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4274 {
4275 if (args)
4276 {
4277 koenig_p = true;
4278 postfix_expression
4279 = perform_koenig_lookup (postfix_expression, args);
4280 }
4281 else
4282 postfix_expression
4283 = unqualified_fn_lookup_error (postfix_expression);
4284 }
4285 /* We do not perform argument-dependent lookup if
4286 normal lookup finds a non-function, in accordance
4287 with the expected resolution of DR 218. */
4288 else if (args && is_overloaded_fn (postfix_expression))
4289 {
4290 tree fn = get_first_fn (postfix_expression);
4291
4292 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4293 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4294
4295 /* Only do argument dependent lookup if regular
4296 lookup does not find a set of member functions.
4297 [basic.lookup.koenig]/2a */
4298 if (!DECL_FUNCTION_MEMBER_P (fn))
4299 {
4300 koenig_p = true;
4301 postfix_expression
4302 = perform_koenig_lookup (postfix_expression, args);
4303 }
4304 }
4305 }
4306
4307 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4308 {
4309 tree instance = TREE_OPERAND (postfix_expression, 0);
4310 tree fn = TREE_OPERAND (postfix_expression, 1);
4311
4312 if (processing_template_decl
4313 && (type_dependent_expression_p (instance)
4314 || (!BASELINK_P (fn)
4315 && TREE_CODE (fn) != FIELD_DECL)
4316 || type_dependent_expression_p (fn)
4317 || any_type_dependent_arguments_p (args)))
4318 {
4319 postfix_expression
4320 = build_min_nt (CALL_EXPR, postfix_expression,
4321 args, NULL_TREE);
4322 break;
4323 }
4324
4325 if (BASELINK_P (fn))
4326 postfix_expression
4327 = (build_new_method_call
4328 (instance, fn, args, NULL_TREE,
4329 (idk == CP_ID_KIND_QUALIFIED
4330 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4331 /*fn_p=*/NULL));
4332 else
4333 postfix_expression
4334 = finish_call_expr (postfix_expression, args,
4335 /*disallow_virtual=*/false,
4336 /*koenig_p=*/false);
4337 }
4338 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4339 || TREE_CODE (postfix_expression) == MEMBER_REF
4340 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4341 postfix_expression = (build_offset_ref_call_from_tree
4342 (postfix_expression, args));
4343 else if (idk == CP_ID_KIND_QUALIFIED)
4344 /* A call to a static class member, or a namespace-scope
4345 function. */
4346 postfix_expression
4347 = finish_call_expr (postfix_expression, args,
4348 /*disallow_virtual=*/true,
4349 koenig_p);
4350 else
4351 /* All other function calls. */
4352 postfix_expression
4353 = finish_call_expr (postfix_expression, args,
4354 /*disallow_virtual=*/false,
4355 koenig_p);
4356
4357 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4358 idk = CP_ID_KIND_NONE;
4359 }
4360 break;
4361
4362 case CPP_DOT:
4363 case CPP_DEREF:
4364 /* postfix-expression . template [opt] id-expression
4365 postfix-expression . pseudo-destructor-name
4366 postfix-expression -> template [opt] id-expression
4367 postfix-expression -> pseudo-destructor-name */
4368
4369 /* Consume the `.' or `->' operator. */
4370 cp_lexer_consume_token (parser->lexer);
4371
4372 postfix_expression
4373 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4374 postfix_expression,
4375 false, &idk);
4376 break;
4377
4378 case CPP_PLUS_PLUS:
4379 /* postfix-expression ++ */
4380 /* Consume the `++' token. */
4381 cp_lexer_consume_token (parser->lexer);
4382 /* Generate a representation for the complete expression. */
4383 postfix_expression
4384 = finish_increment_expr (postfix_expression,
4385 POSTINCREMENT_EXPR);
4386 /* Increments may not appear in constant-expressions. */
4387 if (cp_parser_non_integral_constant_expression (parser,
4388 "an increment"))
4389 postfix_expression = error_mark_node;
4390 idk = CP_ID_KIND_NONE;
4391 break;
4392
4393 case CPP_MINUS_MINUS:
4394 /* postfix-expression -- */
4395 /* Consume the `--' token. */
4396 cp_lexer_consume_token (parser->lexer);
4397 /* Generate a representation for the complete expression. */
4398 postfix_expression
4399 = finish_increment_expr (postfix_expression,
4400 POSTDECREMENT_EXPR);
4401 /* Decrements may not appear in constant-expressions. */
4402 if (cp_parser_non_integral_constant_expression (parser,
4403 "a decrement"))
4404 postfix_expression = error_mark_node;
4405 idk = CP_ID_KIND_NONE;
4406 break;
4407
4408 default:
4409 return postfix_expression;
4410 }
4411 }
4412
4413 /* We should never get here. */
4414 gcc_unreachable ();
4415 return error_mark_node;
4416 }
4417
4418 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4419 by cp_parser_builtin_offsetof. We're looking for
4420
4421 postfix-expression [ expression ]
4422
4423 FOR_OFFSETOF is set if we're being called in that context, which
4424 changes how we deal with integer constant expressions. */
4425
4426 static tree
4427 cp_parser_postfix_open_square_expression (cp_parser *parser,
4428 tree postfix_expression,
4429 bool for_offsetof)
4430 {
4431 tree index;
4432
4433 /* Consume the `[' token. */
4434 cp_lexer_consume_token (parser->lexer);
4435
4436 /* Parse the index expression. */
4437 /* ??? For offsetof, there is a question of what to allow here. If
4438 offsetof is not being used in an integral constant expression context,
4439 then we *could* get the right answer by computing the value at runtime.
4440 If we are in an integral constant expression context, then we might
4441 could accept any constant expression; hard to say without analysis.
4442 Rather than open the barn door too wide right away, allow only integer
4443 constant expressions here. */
4444 if (for_offsetof)
4445 index = cp_parser_constant_expression (parser, false, NULL);
4446 else
4447 index = cp_parser_expression (parser, /*cast_p=*/false);
4448
4449 /* Look for the closing `]'. */
4450 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4451
4452 /* Build the ARRAY_REF. */
4453 postfix_expression = grok_array_decl (postfix_expression, index);
4454
4455 /* When not doing offsetof, array references are not permitted in
4456 constant-expressions. */
4457 if (!for_offsetof
4458 && (cp_parser_non_integral_constant_expression
4459 (parser, "an array reference")))
4460 postfix_expression = error_mark_node;
4461
4462 return postfix_expression;
4463 }
4464
4465 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4466 by cp_parser_builtin_offsetof. We're looking for
4467
4468 postfix-expression . template [opt] id-expression
4469 postfix-expression . pseudo-destructor-name
4470 postfix-expression -> template [opt] id-expression
4471 postfix-expression -> pseudo-destructor-name
4472
4473 FOR_OFFSETOF is set if we're being called in that context. That sorta
4474 limits what of the above we'll actually accept, but nevermind.
4475 TOKEN_TYPE is the "." or "->" token, which will already have been
4476 removed from the stream. */
4477
4478 static tree
4479 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4480 enum cpp_ttype token_type,
4481 tree postfix_expression,
4482 bool for_offsetof, cp_id_kind *idk)
4483 {
4484 tree name;
4485 bool dependent_p;
4486 bool pseudo_destructor_p;
4487 tree scope = NULL_TREE;
4488
4489 /* If this is a `->' operator, dereference the pointer. */
4490 if (token_type == CPP_DEREF)
4491 postfix_expression = build_x_arrow (postfix_expression);
4492 /* Check to see whether or not the expression is type-dependent. */
4493 dependent_p = type_dependent_expression_p (postfix_expression);
4494 /* The identifier following the `->' or `.' is not qualified. */
4495 parser->scope = NULL_TREE;
4496 parser->qualifying_scope = NULL_TREE;
4497 parser->object_scope = NULL_TREE;
4498 *idk = CP_ID_KIND_NONE;
4499 /* Enter the scope corresponding to the type of the object
4500 given by the POSTFIX_EXPRESSION. */
4501 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4502 {
4503 scope = TREE_TYPE (postfix_expression);
4504 /* According to the standard, no expression should ever have
4505 reference type. Unfortunately, we do not currently match
4506 the standard in this respect in that our internal representation
4507 of an expression may have reference type even when the standard
4508 says it does not. Therefore, we have to manually obtain the
4509 underlying type here. */
4510 scope = non_reference (scope);
4511 /* The type of the POSTFIX_EXPRESSION must be complete. */
4512 if (scope == unknown_type_node)
4513 {
4514 error ("%qE does not have class type", postfix_expression);
4515 scope = NULL_TREE;
4516 }
4517 else
4518 scope = complete_type_or_else (scope, NULL_TREE);
4519 /* Let the name lookup machinery know that we are processing a
4520 class member access expression. */
4521 parser->context->object_type = scope;
4522 /* If something went wrong, we want to be able to discern that case,
4523 as opposed to the case where there was no SCOPE due to the type
4524 of expression being dependent. */
4525 if (!scope)
4526 scope = error_mark_node;
4527 /* If the SCOPE was erroneous, make the various semantic analysis
4528 functions exit quickly -- and without issuing additional error
4529 messages. */
4530 if (scope == error_mark_node)
4531 postfix_expression = error_mark_node;
4532 }
4533
4534 /* Assume this expression is not a pseudo-destructor access. */
4535 pseudo_destructor_p = false;
4536
4537 /* If the SCOPE is a scalar type, then, if this is a valid program,
4538 we must be looking at a pseudo-destructor-name. */
4539 if (scope && SCALAR_TYPE_P (scope))
4540 {
4541 tree s;
4542 tree type;
4543
4544 cp_parser_parse_tentatively (parser);
4545 /* Parse the pseudo-destructor-name. */
4546 s = NULL_TREE;
4547 cp_parser_pseudo_destructor_name (parser, &s, &type);
4548 if (cp_parser_parse_definitely (parser))
4549 {
4550 pseudo_destructor_p = true;
4551 postfix_expression
4552 = finish_pseudo_destructor_expr (postfix_expression,
4553 s, TREE_TYPE (type));
4554 }
4555 }
4556
4557 if (!pseudo_destructor_p)
4558 {
4559 /* If the SCOPE is not a scalar type, we are looking at an
4560 ordinary class member access expression, rather than a
4561 pseudo-destructor-name. */
4562 bool template_p;
4563 /* Parse the id-expression. */
4564 name = (cp_parser_id_expression
4565 (parser,
4566 cp_parser_optional_template_keyword (parser),
4567 /*check_dependency_p=*/true,
4568 &template_p,
4569 /*declarator_p=*/false,
4570 /*optional_p=*/false));
4571 /* In general, build a SCOPE_REF if the member name is qualified.
4572 However, if the name was not dependent and has already been
4573 resolved; there is no need to build the SCOPE_REF. For example;
4574
4575 struct X { void f(); };
4576 template <typename T> void f(T* t) { t->X::f(); }
4577
4578 Even though "t" is dependent, "X::f" is not and has been resolved
4579 to a BASELINK; there is no need to include scope information. */
4580
4581 /* But we do need to remember that there was an explicit scope for
4582 virtual function calls. */
4583 if (parser->scope)
4584 *idk = CP_ID_KIND_QUALIFIED;
4585
4586 /* If the name is a template-id that names a type, we will get a
4587 TYPE_DECL here. That is invalid code. */
4588 if (TREE_CODE (name) == TYPE_DECL)
4589 {
4590 error ("invalid use of %qD", name);
4591 postfix_expression = error_mark_node;
4592 }
4593 else
4594 {
4595 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4596 {
4597 name = build_qualified_name (/*type=*/NULL_TREE,
4598 parser->scope,
4599 name,
4600 template_p);
4601 parser->scope = NULL_TREE;
4602 parser->qualifying_scope = NULL_TREE;
4603 parser->object_scope = NULL_TREE;
4604 }
4605 if (scope && name && BASELINK_P (name))
4606 adjust_result_of_qualified_name_lookup
4607 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4608 postfix_expression
4609 = finish_class_member_access_expr (postfix_expression, name,
4610 template_p);
4611 }
4612 }
4613
4614 /* We no longer need to look up names in the scope of the object on
4615 the left-hand side of the `.' or `->' operator. */
4616 parser->context->object_type = NULL_TREE;
4617
4618 /* Outside of offsetof, these operators may not appear in
4619 constant-expressions. */
4620 if (!for_offsetof
4621 && (cp_parser_non_integral_constant_expression
4622 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4623 postfix_expression = error_mark_node;
4624
4625 return postfix_expression;
4626 }
4627
4628 /* Parse a parenthesized expression-list.
4629
4630 expression-list:
4631 assignment-expression
4632 expression-list, assignment-expression
4633
4634 attribute-list:
4635 expression-list
4636 identifier
4637 identifier, expression-list
4638
4639 CAST_P is true if this expression is the target of a cast.
4640
4641 Returns a TREE_LIST. The TREE_VALUE of each node is a
4642 representation of an assignment-expression. Note that a TREE_LIST
4643 is returned even if there is only a single expression in the list.
4644 error_mark_node is returned if the ( and or ) are
4645 missing. NULL_TREE is returned on no expressions. The parentheses
4646 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4647 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4648 indicates whether or not all of the expressions in the list were
4649 constant. */
4650
4651 static tree
4652 cp_parser_parenthesized_expression_list (cp_parser* parser,
4653 bool is_attribute_list,
4654 bool cast_p,
4655 bool *non_constant_p)
4656 {
4657 tree expression_list = NULL_TREE;
4658 bool fold_expr_p = is_attribute_list;
4659 tree identifier = NULL_TREE;
4660
4661 /* Assume all the expressions will be constant. */
4662 if (non_constant_p)
4663 *non_constant_p = false;
4664
4665 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4666 return error_mark_node;
4667
4668 /* Consume expressions until there are no more. */
4669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4670 while (true)
4671 {
4672 tree expr;
4673
4674 /* At the beginning of attribute lists, check to see if the
4675 next token is an identifier. */
4676 if (is_attribute_list
4677 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4678 {
4679 cp_token *token;
4680
4681 /* Consume the identifier. */
4682 token = cp_lexer_consume_token (parser->lexer);
4683 /* Save the identifier. */
4684 identifier = token->value;
4685 }
4686 else
4687 {
4688 /* Parse the next assignment-expression. */
4689 if (non_constant_p)
4690 {
4691 bool expr_non_constant_p;
4692 expr = (cp_parser_constant_expression
4693 (parser, /*allow_non_constant_p=*/true,
4694 &expr_non_constant_p));
4695 if (expr_non_constant_p)
4696 *non_constant_p = true;
4697 }
4698 else
4699 expr = cp_parser_assignment_expression (parser, cast_p);
4700
4701 if (fold_expr_p)
4702 expr = fold_non_dependent_expr (expr);
4703
4704 /* Add it to the list. We add error_mark_node
4705 expressions to the list, so that we can still tell if
4706 the correct form for a parenthesized expression-list
4707 is found. That gives better errors. */
4708 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4709
4710 if (expr == error_mark_node)
4711 goto skip_comma;
4712 }
4713
4714 /* After the first item, attribute lists look the same as
4715 expression lists. */
4716 is_attribute_list = false;
4717
4718 get_comma:;
4719 /* If the next token isn't a `,', then we are done. */
4720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4721 break;
4722
4723 /* Otherwise, consume the `,' and keep going. */
4724 cp_lexer_consume_token (parser->lexer);
4725 }
4726
4727 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4728 {
4729 int ending;
4730
4731 skip_comma:;
4732 /* We try and resync to an unnested comma, as that will give the
4733 user better diagnostics. */
4734 ending = cp_parser_skip_to_closing_parenthesis (parser,
4735 /*recovering=*/true,
4736 /*or_comma=*/true,
4737 /*consume_paren=*/true);
4738 if (ending < 0)
4739 goto get_comma;
4740 if (!ending)
4741 return error_mark_node;
4742 }
4743
4744 /* We built up the list in reverse order so we must reverse it now. */
4745 expression_list = nreverse (expression_list);
4746 if (identifier)
4747 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4748
4749 return expression_list;
4750 }
4751
4752 /* Parse a pseudo-destructor-name.
4753
4754 pseudo-destructor-name:
4755 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4756 :: [opt] nested-name-specifier template template-id :: ~ type-name
4757 :: [opt] nested-name-specifier [opt] ~ type-name
4758
4759 If either of the first two productions is used, sets *SCOPE to the
4760 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4761 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4762 or ERROR_MARK_NODE if the parse fails. */
4763
4764 static void
4765 cp_parser_pseudo_destructor_name (cp_parser* parser,
4766 tree* scope,
4767 tree* type)
4768 {
4769 bool nested_name_specifier_p;
4770
4771 /* Assume that things will not work out. */
4772 *type = error_mark_node;
4773
4774 /* Look for the optional `::' operator. */
4775 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4776 /* Look for the optional nested-name-specifier. */
4777 nested_name_specifier_p
4778 = (cp_parser_nested_name_specifier_opt (parser,
4779 /*typename_keyword_p=*/false,
4780 /*check_dependency_p=*/true,
4781 /*type_p=*/false,
4782 /*is_declaration=*/true)
4783 != NULL_TREE);
4784 /* Now, if we saw a nested-name-specifier, we might be doing the
4785 second production. */
4786 if (nested_name_specifier_p
4787 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4788 {
4789 /* Consume the `template' keyword. */
4790 cp_lexer_consume_token (parser->lexer);
4791 /* Parse the template-id. */
4792 cp_parser_template_id (parser,
4793 /*template_keyword_p=*/true,
4794 /*check_dependency_p=*/false,
4795 /*is_declaration=*/true);
4796 /* Look for the `::' token. */
4797 cp_parser_require (parser, CPP_SCOPE, "`::'");
4798 }
4799 /* If the next token is not a `~', then there might be some
4800 additional qualification. */
4801 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4802 {
4803 /* Look for the type-name. */
4804 *scope = TREE_TYPE (cp_parser_type_name (parser));
4805
4806 if (*scope == error_mark_node)
4807 return;
4808
4809 /* If we don't have ::~, then something has gone wrong. Since
4810 the only caller of this function is looking for something
4811 after `.' or `->' after a scalar type, most likely the
4812 program is trying to get a member of a non-aggregate
4813 type. */
4814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4815 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4816 {
4817 cp_parser_error (parser, "request for member of non-aggregate type");
4818 return;
4819 }
4820
4821 /* Look for the `::' token. */
4822 cp_parser_require (parser, CPP_SCOPE, "`::'");
4823 }
4824 else
4825 *scope = NULL_TREE;
4826
4827 /* Look for the `~'. */
4828 cp_parser_require (parser, CPP_COMPL, "`~'");
4829 /* Look for the type-name again. We are not responsible for
4830 checking that it matches the first type-name. */
4831 *type = cp_parser_type_name (parser);
4832 }
4833
4834 /* Parse a unary-expression.
4835
4836 unary-expression:
4837 postfix-expression
4838 ++ cast-expression
4839 -- cast-expression
4840 unary-operator cast-expression
4841 sizeof unary-expression
4842 sizeof ( type-id )
4843 new-expression
4844 delete-expression
4845
4846 GNU Extensions:
4847
4848 unary-expression:
4849 __extension__ cast-expression
4850 __alignof__ unary-expression
4851 __alignof__ ( type-id )
4852 __real__ cast-expression
4853 __imag__ cast-expression
4854 && identifier
4855
4856 ADDRESS_P is true iff the unary-expression is appearing as the
4857 operand of the `&' operator. CAST_P is true if this expression is
4858 the target of a cast.
4859
4860 Returns a representation of the expression. */
4861
4862 static tree
4863 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4864 {
4865 cp_token *token;
4866 enum tree_code unary_operator;
4867
4868 /* Peek at the next token. */
4869 token = cp_lexer_peek_token (parser->lexer);
4870 /* Some keywords give away the kind of expression. */
4871 if (token->type == CPP_KEYWORD)
4872 {
4873 enum rid keyword = token->keyword;
4874
4875 switch (keyword)
4876 {
4877 case RID_ALIGNOF:
4878 case RID_SIZEOF:
4879 {
4880 tree operand;
4881 enum tree_code op;
4882
4883 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4884 /* Consume the token. */
4885 cp_lexer_consume_token (parser->lexer);
4886 /* Parse the operand. */
4887 operand = cp_parser_sizeof_operand (parser, keyword);
4888
4889 if (TYPE_P (operand))
4890 return cxx_sizeof_or_alignof_type (operand, op, true);
4891 else
4892 return cxx_sizeof_or_alignof_expr (operand, op);
4893 }
4894
4895 case RID_NEW:
4896 return cp_parser_new_expression (parser);
4897
4898 case RID_DELETE:
4899 return cp_parser_delete_expression (parser);
4900
4901 case RID_EXTENSION:
4902 {
4903 /* The saved value of the PEDANTIC flag. */
4904 int saved_pedantic;
4905 tree expr;
4906
4907 /* Save away the PEDANTIC flag. */
4908 cp_parser_extension_opt (parser, &saved_pedantic);
4909 /* Parse the cast-expression. */
4910 expr = cp_parser_simple_cast_expression (parser);
4911 /* Restore the PEDANTIC flag. */
4912 pedantic = saved_pedantic;
4913
4914 return expr;
4915 }
4916
4917 case RID_REALPART:
4918 case RID_IMAGPART:
4919 {
4920 tree expression;
4921
4922 /* Consume the `__real__' or `__imag__' token. */
4923 cp_lexer_consume_token (parser->lexer);
4924 /* Parse the cast-expression. */
4925 expression = cp_parser_simple_cast_expression (parser);
4926 /* Create the complete representation. */
4927 return build_x_unary_op ((keyword == RID_REALPART
4928 ? REALPART_EXPR : IMAGPART_EXPR),
4929 expression);
4930 }
4931 break;
4932
4933 default:
4934 break;
4935 }
4936 }
4937
4938 /* Look for the `:: new' and `:: delete', which also signal the
4939 beginning of a new-expression, or delete-expression,
4940 respectively. If the next token is `::', then it might be one of
4941 these. */
4942 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4943 {
4944 enum rid keyword;
4945
4946 /* See if the token after the `::' is one of the keywords in
4947 which we're interested. */
4948 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4949 /* If it's `new', we have a new-expression. */
4950 if (keyword == RID_NEW)
4951 return cp_parser_new_expression (parser);
4952 /* Similarly, for `delete'. */
4953 else if (keyword == RID_DELETE)
4954 return cp_parser_delete_expression (parser);
4955 }
4956
4957 /* Look for a unary operator. */
4958 unary_operator = cp_parser_unary_operator (token);
4959 /* The `++' and `--' operators can be handled similarly, even though
4960 they are not technically unary-operators in the grammar. */
4961 if (unary_operator == ERROR_MARK)
4962 {
4963 if (token->type == CPP_PLUS_PLUS)
4964 unary_operator = PREINCREMENT_EXPR;
4965 else if (token->type == CPP_MINUS_MINUS)
4966 unary_operator = PREDECREMENT_EXPR;
4967 /* Handle the GNU address-of-label extension. */
4968 else if (cp_parser_allow_gnu_extensions_p (parser)
4969 && token->type == CPP_AND_AND)
4970 {
4971 tree identifier;
4972
4973 /* Consume the '&&' token. */
4974 cp_lexer_consume_token (parser->lexer);
4975 /* Look for the identifier. */
4976 identifier = cp_parser_identifier (parser);
4977 /* Create an expression representing the address. */
4978 return finish_label_address_expr (identifier);
4979 }
4980 }
4981 if (unary_operator != ERROR_MARK)
4982 {
4983 tree cast_expression;
4984 tree expression = error_mark_node;
4985 const char *non_constant_p = NULL;
4986
4987 /* Consume the operator token. */
4988 token = cp_lexer_consume_token (parser->lexer);
4989 /* Parse the cast-expression. */
4990 cast_expression
4991 = cp_parser_cast_expression (parser,
4992 unary_operator == ADDR_EXPR,
4993 /*cast_p=*/false);
4994 /* Now, build an appropriate representation. */
4995 switch (unary_operator)
4996 {
4997 case INDIRECT_REF:
4998 non_constant_p = "`*'";
4999 expression = build_x_indirect_ref (cast_expression, "unary *");
5000 break;
5001
5002 case ADDR_EXPR:
5003 non_constant_p = "`&'";
5004 /* Fall through. */
5005 case BIT_NOT_EXPR:
5006 expression = build_x_unary_op (unary_operator, cast_expression);
5007 break;
5008
5009 case PREINCREMENT_EXPR:
5010 case PREDECREMENT_EXPR:
5011 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5012 ? "`++'" : "`--'");
5013 /* Fall through. */
5014 case UNARY_PLUS_EXPR:
5015 case NEGATE_EXPR:
5016 case TRUTH_NOT_EXPR:
5017 expression = finish_unary_op_expr (unary_operator, cast_expression);
5018 break;
5019
5020 default:
5021 gcc_unreachable ();
5022 }
5023
5024 if (non_constant_p
5025 && cp_parser_non_integral_constant_expression (parser,
5026 non_constant_p))
5027 expression = error_mark_node;
5028
5029 return expression;
5030 }
5031
5032 return cp_parser_postfix_expression (parser, address_p, cast_p);
5033 }
5034
5035 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5036 unary-operator, the corresponding tree code is returned. */
5037
5038 static enum tree_code
5039 cp_parser_unary_operator (cp_token* token)
5040 {
5041 switch (token->type)
5042 {
5043 case CPP_MULT:
5044 return INDIRECT_REF;
5045
5046 case CPP_AND:
5047 return ADDR_EXPR;
5048
5049 case CPP_PLUS:
5050 return UNARY_PLUS_EXPR;
5051
5052 case CPP_MINUS:
5053 return NEGATE_EXPR;
5054
5055 case CPP_NOT:
5056 return TRUTH_NOT_EXPR;
5057
5058 case CPP_COMPL:
5059 return BIT_NOT_EXPR;
5060
5061 default:
5062 return ERROR_MARK;
5063 }
5064 }
5065
5066 /* Parse a new-expression.
5067
5068 new-expression:
5069 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5070 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5071
5072 Returns a representation of the expression. */
5073
5074 static tree
5075 cp_parser_new_expression (cp_parser* parser)
5076 {
5077 bool global_scope_p;
5078 tree placement;
5079 tree type;
5080 tree initializer;
5081 tree nelts;
5082
5083 /* Look for the optional `::' operator. */
5084 global_scope_p
5085 = (cp_parser_global_scope_opt (parser,
5086 /*current_scope_valid_p=*/false)
5087 != NULL_TREE);
5088 /* Look for the `new' operator. */
5089 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5090 /* There's no easy way to tell a new-placement from the
5091 `( type-id )' construct. */
5092 cp_parser_parse_tentatively (parser);
5093 /* Look for a new-placement. */
5094 placement = cp_parser_new_placement (parser);
5095 /* If that didn't work out, there's no new-placement. */
5096 if (!cp_parser_parse_definitely (parser))
5097 placement = NULL_TREE;
5098
5099 /* If the next token is a `(', then we have a parenthesized
5100 type-id. */
5101 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5102 {
5103 /* Consume the `('. */
5104 cp_lexer_consume_token (parser->lexer);
5105 /* Parse the type-id. */
5106 type = cp_parser_type_id (parser);
5107 /* Look for the closing `)'. */
5108 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5109 /* There should not be a direct-new-declarator in this production,
5110 but GCC used to allowed this, so we check and emit a sensible error
5111 message for this case. */
5112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5113 {
5114 error ("array bound forbidden after parenthesized type-id");
5115 inform ("try removing the parentheses around the type-id");
5116 cp_parser_direct_new_declarator (parser);
5117 }
5118 nelts = NULL_TREE;
5119 }
5120 /* Otherwise, there must be a new-type-id. */
5121 else
5122 type = cp_parser_new_type_id (parser, &nelts);
5123
5124 /* If the next token is a `(', then we have a new-initializer. */
5125 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5126 initializer = cp_parser_new_initializer (parser);
5127 else
5128 initializer = NULL_TREE;
5129
5130 /* A new-expression may not appear in an integral constant
5131 expression. */
5132 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5133 return error_mark_node;
5134
5135 /* Create a representation of the new-expression. */
5136 return build_new (placement, type, nelts, initializer, global_scope_p);
5137 }
5138
5139 /* Parse a new-placement.
5140
5141 new-placement:
5142 ( expression-list )
5143
5144 Returns the same representation as for an expression-list. */
5145
5146 static tree
5147 cp_parser_new_placement (cp_parser* parser)
5148 {
5149 tree expression_list;
5150
5151 /* Parse the expression-list. */
5152 expression_list = (cp_parser_parenthesized_expression_list
5153 (parser, false, /*cast_p=*/false,
5154 /*non_constant_p=*/NULL));
5155
5156 return expression_list;
5157 }
5158
5159 /* Parse a new-type-id.
5160
5161 new-type-id:
5162 type-specifier-seq new-declarator [opt]
5163
5164 Returns the TYPE allocated. If the new-type-id indicates an array
5165 type, *NELTS is set to the number of elements in the last array
5166 bound; the TYPE will not include the last array bound. */
5167
5168 static tree
5169 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5170 {
5171 cp_decl_specifier_seq type_specifier_seq;
5172 cp_declarator *new_declarator;
5173 cp_declarator *declarator;
5174 cp_declarator *outer_declarator;
5175 const char *saved_message;
5176 tree type;
5177
5178 /* The type-specifier sequence must not contain type definitions.
5179 (It cannot contain declarations of new types either, but if they
5180 are not definitions we will catch that because they are not
5181 complete.) */
5182 saved_message = parser->type_definition_forbidden_message;
5183 parser->type_definition_forbidden_message
5184 = "types may not be defined in a new-type-id";
5185 /* Parse the type-specifier-seq. */
5186 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5187 &type_specifier_seq);
5188 /* Restore the old message. */
5189 parser->type_definition_forbidden_message = saved_message;
5190 /* Parse the new-declarator. */
5191 new_declarator = cp_parser_new_declarator_opt (parser);
5192
5193 /* Determine the number of elements in the last array dimension, if
5194 any. */
5195 *nelts = NULL_TREE;
5196 /* Skip down to the last array dimension. */
5197 declarator = new_declarator;
5198 outer_declarator = NULL;
5199 while (declarator && (declarator->kind == cdk_pointer
5200 || declarator->kind == cdk_ptrmem))
5201 {
5202 outer_declarator = declarator;
5203 declarator = declarator->declarator;
5204 }
5205 while (declarator
5206 && declarator->kind == cdk_array
5207 && declarator->declarator
5208 && declarator->declarator->kind == cdk_array)
5209 {
5210 outer_declarator = declarator;
5211 declarator = declarator->declarator;
5212 }
5213
5214 if (declarator && declarator->kind == cdk_array)
5215 {
5216 *nelts = declarator->u.array.bounds;
5217 if (*nelts == error_mark_node)
5218 *nelts = integer_one_node;
5219
5220 if (outer_declarator)
5221 outer_declarator->declarator = declarator->declarator;
5222 else
5223 new_declarator = NULL;
5224 }
5225
5226 type = groktypename (&type_specifier_seq, new_declarator);
5227 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5228 {
5229 *nelts = array_type_nelts_top (type);
5230 type = TREE_TYPE (type);
5231 }
5232 return type;
5233 }
5234
5235 /* Parse an (optional) new-declarator.
5236
5237 new-declarator:
5238 ptr-operator new-declarator [opt]
5239 direct-new-declarator
5240
5241 Returns the declarator. */
5242
5243 static cp_declarator *
5244 cp_parser_new_declarator_opt (cp_parser* parser)
5245 {
5246 enum tree_code code;
5247 tree type;
5248 cp_cv_quals cv_quals;
5249
5250 /* We don't know if there's a ptr-operator next, or not. */
5251 cp_parser_parse_tentatively (parser);
5252 /* Look for a ptr-operator. */
5253 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5254 /* If that worked, look for more new-declarators. */
5255 if (cp_parser_parse_definitely (parser))
5256 {
5257 cp_declarator *declarator;
5258
5259 /* Parse another optional declarator. */
5260 declarator = cp_parser_new_declarator_opt (parser);
5261
5262 /* Create the representation of the declarator. */
5263 if (type)
5264 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5265 else if (code == INDIRECT_REF)
5266 declarator = make_pointer_declarator (cv_quals, declarator);
5267 else
5268 declarator = make_reference_declarator (cv_quals, declarator);
5269
5270 return declarator;
5271 }
5272
5273 /* If the next token is a `[', there is a direct-new-declarator. */
5274 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5275 return cp_parser_direct_new_declarator (parser);
5276
5277 return NULL;
5278 }
5279
5280 /* Parse a direct-new-declarator.
5281
5282 direct-new-declarator:
5283 [ expression ]
5284 direct-new-declarator [constant-expression]
5285
5286 */
5287
5288 static cp_declarator *
5289 cp_parser_direct_new_declarator (cp_parser* parser)
5290 {
5291 cp_declarator *declarator = NULL;
5292
5293 while (true)
5294 {
5295 tree expression;
5296
5297 /* Look for the opening `['. */
5298 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5299 /* The first expression is not required to be constant. */
5300 if (!declarator)
5301 {
5302 expression = cp_parser_expression (parser, /*cast_p=*/false);
5303 /* The standard requires that the expression have integral
5304 type. DR 74 adds enumeration types. We believe that the
5305 real intent is that these expressions be handled like the
5306 expression in a `switch' condition, which also allows
5307 classes with a single conversion to integral or
5308 enumeration type. */
5309 if (!processing_template_decl)
5310 {
5311 expression
5312 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5313 expression,
5314 /*complain=*/true);
5315 if (!expression)
5316 {
5317 error ("expression in new-declarator must have integral "
5318 "or enumeration type");
5319 expression = error_mark_node;
5320 }
5321 }
5322 }
5323 /* But all the other expressions must be. */
5324 else
5325 expression
5326 = cp_parser_constant_expression (parser,
5327 /*allow_non_constant=*/false,
5328 NULL);
5329 /* Look for the closing `]'. */
5330 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5331
5332 /* Add this bound to the declarator. */
5333 declarator = make_array_declarator (declarator, expression);
5334
5335 /* If the next token is not a `[', then there are no more
5336 bounds. */
5337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5338 break;
5339 }
5340
5341 return declarator;
5342 }
5343
5344 /* Parse a new-initializer.
5345
5346 new-initializer:
5347 ( expression-list [opt] )
5348
5349 Returns a representation of the expression-list. If there is no
5350 expression-list, VOID_ZERO_NODE is returned. */
5351
5352 static tree
5353 cp_parser_new_initializer (cp_parser* parser)
5354 {
5355 tree expression_list;
5356
5357 expression_list = (cp_parser_parenthesized_expression_list
5358 (parser, false, /*cast_p=*/false,
5359 /*non_constant_p=*/NULL));
5360 if (!expression_list)
5361 expression_list = void_zero_node;
5362
5363 return expression_list;
5364 }
5365
5366 /* Parse a delete-expression.
5367
5368 delete-expression:
5369 :: [opt] delete cast-expression
5370 :: [opt] delete [ ] cast-expression
5371
5372 Returns a representation of the expression. */
5373
5374 static tree
5375 cp_parser_delete_expression (cp_parser* parser)
5376 {
5377 bool global_scope_p;
5378 bool array_p;
5379 tree expression;
5380
5381 /* Look for the optional `::' operator. */
5382 global_scope_p
5383 = (cp_parser_global_scope_opt (parser,
5384 /*current_scope_valid_p=*/false)
5385 != NULL_TREE);
5386 /* Look for the `delete' keyword. */
5387 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5388 /* See if the array syntax is in use. */
5389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5390 {
5391 /* Consume the `[' token. */
5392 cp_lexer_consume_token (parser->lexer);
5393 /* Look for the `]' token. */
5394 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5395 /* Remember that this is the `[]' construct. */
5396 array_p = true;
5397 }
5398 else
5399 array_p = false;
5400
5401 /* Parse the cast-expression. */
5402 expression = cp_parser_simple_cast_expression (parser);
5403
5404 /* A delete-expression may not appear in an integral constant
5405 expression. */
5406 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5407 return error_mark_node;
5408
5409 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5410 }
5411
5412 /* Parse a cast-expression.
5413
5414 cast-expression:
5415 unary-expression
5416 ( type-id ) cast-expression
5417
5418 ADDRESS_P is true iff the unary-expression is appearing as the
5419 operand of the `&' operator. CAST_P is true if this expression is
5420 the target of a cast.
5421
5422 Returns a representation of the expression. */
5423
5424 static tree
5425 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5426 {
5427 /* If it's a `(', then we might be looking at a cast. */
5428 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5429 {
5430 tree type = NULL_TREE;
5431 tree expr = NULL_TREE;
5432 bool compound_literal_p;
5433 const char *saved_message;
5434
5435 /* There's no way to know yet whether or not this is a cast.
5436 For example, `(int (3))' is a unary-expression, while `(int)
5437 3' is a cast. So, we resort to parsing tentatively. */
5438 cp_parser_parse_tentatively (parser);
5439 /* Types may not be defined in a cast. */
5440 saved_message = parser->type_definition_forbidden_message;
5441 parser->type_definition_forbidden_message
5442 = "types may not be defined in casts";
5443 /* Consume the `('. */
5444 cp_lexer_consume_token (parser->lexer);
5445 /* A very tricky bit is that `(struct S) { 3 }' is a
5446 compound-literal (which we permit in C++ as an extension).
5447 But, that construct is not a cast-expression -- it is a
5448 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5449 is legal; if the compound-literal were a cast-expression,
5450 you'd need an extra set of parentheses.) But, if we parse
5451 the type-id, and it happens to be a class-specifier, then we
5452 will commit to the parse at that point, because we cannot
5453 undo the action that is done when creating a new class. So,
5454 then we cannot back up and do a postfix-expression.
5455
5456 Therefore, we scan ahead to the closing `)', and check to see
5457 if the token after the `)' is a `{'. If so, we are not
5458 looking at a cast-expression.
5459
5460 Save tokens so that we can put them back. */
5461 cp_lexer_save_tokens (parser->lexer);
5462 /* Skip tokens until the next token is a closing parenthesis.
5463 If we find the closing `)', and the next token is a `{', then
5464 we are looking at a compound-literal. */
5465 compound_literal_p
5466 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5467 /*consume_paren=*/true)
5468 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5469 /* Roll back the tokens we skipped. */
5470 cp_lexer_rollback_tokens (parser->lexer);
5471 /* If we were looking at a compound-literal, simulate an error
5472 so that the call to cp_parser_parse_definitely below will
5473 fail. */
5474 if (compound_literal_p)
5475 cp_parser_simulate_error (parser);
5476 else
5477 {
5478 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5479 parser->in_type_id_in_expr_p = true;
5480 /* Look for the type-id. */
5481 type = cp_parser_type_id (parser);
5482 /* Look for the closing `)'. */
5483 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5484 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5485 }
5486
5487 /* Restore the saved message. */
5488 parser->type_definition_forbidden_message = saved_message;
5489
5490 /* If ok so far, parse the dependent expression. We cannot be
5491 sure it is a cast. Consider `(T ())'. It is a parenthesized
5492 ctor of T, but looks like a cast to function returning T
5493 without a dependent expression. */
5494 if (!cp_parser_error_occurred (parser))
5495 expr = cp_parser_cast_expression (parser,
5496 /*address_p=*/false,
5497 /*cast_p=*/true);
5498
5499 if (cp_parser_parse_definitely (parser))
5500 {
5501 /* Warn about old-style casts, if so requested. */
5502 if (warn_old_style_cast
5503 && !in_system_header
5504 && !VOID_TYPE_P (type)
5505 && current_lang_name != lang_name_c)
5506 warning (OPT_Wold_style_cast, "use of old-style cast");
5507
5508 /* Only type conversions to integral or enumeration types
5509 can be used in constant-expressions. */
5510 if (!cast_valid_in_integral_constant_expression_p (type)
5511 && (cp_parser_non_integral_constant_expression
5512 (parser,
5513 "a cast to a type other than an integral or "
5514 "enumeration type")))
5515 return error_mark_node;
5516
5517 /* Perform the cast. */
5518 expr = build_c_cast (type, expr);
5519 return expr;
5520 }
5521 }
5522
5523 /* If we get here, then it's not a cast, so it must be a
5524 unary-expression. */
5525 return cp_parser_unary_expression (parser, address_p, cast_p);
5526 }
5527
5528 /* Parse a binary expression of the general form:
5529
5530 pm-expression:
5531 cast-expression
5532 pm-expression .* cast-expression
5533 pm-expression ->* cast-expression
5534
5535 multiplicative-expression:
5536 pm-expression
5537 multiplicative-expression * pm-expression
5538 multiplicative-expression / pm-expression
5539 multiplicative-expression % pm-expression
5540
5541 additive-expression:
5542 multiplicative-expression
5543 additive-expression + multiplicative-expression
5544 additive-expression - multiplicative-expression
5545
5546 shift-expression:
5547 additive-expression
5548 shift-expression << additive-expression
5549 shift-expression >> additive-expression
5550
5551 relational-expression:
5552 shift-expression
5553 relational-expression < shift-expression
5554 relational-expression > shift-expression
5555 relational-expression <= shift-expression
5556 relational-expression >= shift-expression
5557
5558 GNU Extension:
5559
5560 relational-expression:
5561 relational-expression <? shift-expression
5562 relational-expression >? shift-expression
5563
5564 equality-expression:
5565 relational-expression
5566 equality-expression == relational-expression
5567 equality-expression != relational-expression
5568
5569 and-expression:
5570 equality-expression
5571 and-expression & equality-expression
5572
5573 exclusive-or-expression:
5574 and-expression
5575 exclusive-or-expression ^ and-expression
5576
5577 inclusive-or-expression:
5578 exclusive-or-expression
5579 inclusive-or-expression | exclusive-or-expression
5580
5581 logical-and-expression:
5582 inclusive-or-expression
5583 logical-and-expression && inclusive-or-expression
5584
5585 logical-or-expression:
5586 logical-and-expression
5587 logical-or-expression || logical-and-expression
5588
5589 All these are implemented with a single function like:
5590
5591 binary-expression:
5592 simple-cast-expression
5593 binary-expression <token> binary-expression
5594
5595 CAST_P is true if this expression is the target of a cast.
5596
5597 The binops_by_token map is used to get the tree codes for each <token> type.
5598 binary-expressions are associated according to a precedence table. */
5599
5600 #define TOKEN_PRECEDENCE(token) \
5601 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5602 ? PREC_NOT_OPERATOR \
5603 : binops_by_token[token->type].prec)
5604
5605 static tree
5606 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5607 {
5608 cp_parser_expression_stack stack;
5609 cp_parser_expression_stack_entry *sp = &stack[0];
5610 tree lhs, rhs;
5611 cp_token *token;
5612 enum tree_code tree_type;
5613 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5614 bool overloaded_p;
5615
5616 /* Parse the first expression. */
5617 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5618
5619 for (;;)
5620 {
5621 /* Get an operator token. */
5622 token = cp_lexer_peek_token (parser->lexer);
5623
5624 new_prec = TOKEN_PRECEDENCE (token);
5625
5626 /* Popping an entry off the stack means we completed a subexpression:
5627 - either we found a token which is not an operator (`>' where it is not
5628 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5629 will happen repeatedly;
5630 - or, we found an operator which has lower priority. This is the case
5631 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5632 parsing `3 * 4'. */
5633 if (new_prec <= prec)
5634 {
5635 if (sp == stack)
5636 break;
5637 else
5638 goto pop;
5639 }
5640
5641 get_rhs:
5642 tree_type = binops_by_token[token->type].tree_type;
5643
5644 /* We used the operator token. */
5645 cp_lexer_consume_token (parser->lexer);
5646
5647 /* Extract another operand. It may be the RHS of this expression
5648 or the LHS of a new, higher priority expression. */
5649 rhs = cp_parser_simple_cast_expression (parser);
5650
5651 /* Get another operator token. Look up its precedence to avoid
5652 building a useless (immediately popped) stack entry for common
5653 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5654 token = cp_lexer_peek_token (parser->lexer);
5655 lookahead_prec = TOKEN_PRECEDENCE (token);
5656 if (lookahead_prec > new_prec)
5657 {
5658 /* ... and prepare to parse the RHS of the new, higher priority
5659 expression. Since precedence levels on the stack are
5660 monotonically increasing, we do not have to care about
5661 stack overflows. */
5662 sp->prec = prec;
5663 sp->tree_type = tree_type;
5664 sp->lhs = lhs;
5665 sp++;
5666 lhs = rhs;
5667 prec = new_prec;
5668 new_prec = lookahead_prec;
5669 goto get_rhs;
5670
5671 pop:
5672 /* If the stack is not empty, we have parsed into LHS the right side
5673 (`4' in the example above) of an expression we had suspended.
5674 We can use the information on the stack to recover the LHS (`3')
5675 from the stack together with the tree code (`MULT_EXPR'), and
5676 the precedence of the higher level subexpression
5677 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5678 which will be used to actually build the additive expression. */
5679 --sp;
5680 prec = sp->prec;
5681 tree_type = sp->tree_type;
5682 rhs = lhs;
5683 lhs = sp->lhs;
5684 }
5685
5686 overloaded_p = false;
5687 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5688
5689 /* If the binary operator required the use of an overloaded operator,
5690 then this expression cannot be an integral constant-expression.
5691 An overloaded operator can be used even if both operands are
5692 otherwise permissible in an integral constant-expression if at
5693 least one of the operands is of enumeration type. */
5694
5695 if (overloaded_p
5696 && (cp_parser_non_integral_constant_expression
5697 (parser, "calls to overloaded operators")))
5698 return error_mark_node;
5699 }
5700
5701 return lhs;
5702 }
5703
5704
5705 /* Parse the `? expression : assignment-expression' part of a
5706 conditional-expression. The LOGICAL_OR_EXPR is the
5707 logical-or-expression that started the conditional-expression.
5708 Returns a representation of the entire conditional-expression.
5709
5710 This routine is used by cp_parser_assignment_expression.
5711
5712 ? expression : assignment-expression
5713
5714 GNU Extensions:
5715
5716 ? : assignment-expression */
5717
5718 static tree
5719 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5720 {
5721 tree expr;
5722 tree assignment_expr;
5723
5724 /* Consume the `?' token. */
5725 cp_lexer_consume_token (parser->lexer);
5726 if (cp_parser_allow_gnu_extensions_p (parser)
5727 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5728 /* Implicit true clause. */
5729 expr = NULL_TREE;
5730 else
5731 /* Parse the expression. */
5732 expr = cp_parser_expression (parser, /*cast_p=*/false);
5733
5734 /* The next token should be a `:'. */
5735 cp_parser_require (parser, CPP_COLON, "`:'");
5736 /* Parse the assignment-expression. */
5737 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5738
5739 /* Build the conditional-expression. */
5740 return build_x_conditional_expr (logical_or_expr,
5741 expr,
5742 assignment_expr);
5743 }
5744
5745 /* Parse an assignment-expression.
5746
5747 assignment-expression:
5748 conditional-expression
5749 logical-or-expression assignment-operator assignment_expression
5750 throw-expression
5751
5752 CAST_P is true if this expression is the target of a cast.
5753
5754 Returns a representation for the expression. */
5755
5756 static tree
5757 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5758 {
5759 tree expr;
5760
5761 /* If the next token is the `throw' keyword, then we're looking at
5762 a throw-expression. */
5763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5764 expr = cp_parser_throw_expression (parser);
5765 /* Otherwise, it must be that we are looking at a
5766 logical-or-expression. */
5767 else
5768 {
5769 /* Parse the binary expressions (logical-or-expression). */
5770 expr = cp_parser_binary_expression (parser, cast_p);
5771 /* If the next token is a `?' then we're actually looking at a
5772 conditional-expression. */
5773 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5774 return cp_parser_question_colon_clause (parser, expr);
5775 else
5776 {
5777 enum tree_code assignment_operator;
5778
5779 /* If it's an assignment-operator, we're using the second
5780 production. */
5781 assignment_operator
5782 = cp_parser_assignment_operator_opt (parser);
5783 if (assignment_operator != ERROR_MARK)
5784 {
5785 tree rhs;
5786
5787 /* Parse the right-hand side of the assignment. */
5788 rhs = cp_parser_assignment_expression (parser, cast_p);
5789 /* An assignment may not appear in a
5790 constant-expression. */
5791 if (cp_parser_non_integral_constant_expression (parser,
5792 "an assignment"))
5793 return error_mark_node;
5794 /* Build the assignment expression. */
5795 expr = build_x_modify_expr (expr,
5796 assignment_operator,
5797 rhs);
5798 }
5799 }
5800 }
5801
5802 return expr;
5803 }
5804
5805 /* Parse an (optional) assignment-operator.
5806
5807 assignment-operator: one of
5808 = *= /= %= += -= >>= <<= &= ^= |=
5809
5810 GNU Extension:
5811
5812 assignment-operator: one of
5813 <?= >?=
5814
5815 If the next token is an assignment operator, the corresponding tree
5816 code is returned, and the token is consumed. For example, for
5817 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5818 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5819 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5820 operator, ERROR_MARK is returned. */
5821
5822 static enum tree_code
5823 cp_parser_assignment_operator_opt (cp_parser* parser)
5824 {
5825 enum tree_code op;
5826 cp_token *token;
5827
5828 /* Peek at the next toen. */
5829 token = cp_lexer_peek_token (parser->lexer);
5830
5831 switch (token->type)
5832 {
5833 case CPP_EQ:
5834 op = NOP_EXPR;
5835 break;
5836
5837 case CPP_MULT_EQ:
5838 op = MULT_EXPR;
5839 break;
5840
5841 case CPP_DIV_EQ:
5842 op = TRUNC_DIV_EXPR;
5843 break;
5844
5845 case CPP_MOD_EQ:
5846 op = TRUNC_MOD_EXPR;
5847 break;
5848
5849 case CPP_PLUS_EQ:
5850 op = PLUS_EXPR;
5851 break;
5852
5853 case CPP_MINUS_EQ:
5854 op = MINUS_EXPR;
5855 break;
5856
5857 case CPP_RSHIFT_EQ:
5858 op = RSHIFT_EXPR;
5859 break;
5860
5861 case CPP_LSHIFT_EQ:
5862 op = LSHIFT_EXPR;
5863 break;
5864
5865 case CPP_AND_EQ:
5866 op = BIT_AND_EXPR;
5867 break;
5868
5869 case CPP_XOR_EQ:
5870 op = BIT_XOR_EXPR;
5871 break;
5872
5873 case CPP_OR_EQ:
5874 op = BIT_IOR_EXPR;
5875 break;
5876
5877 default:
5878 /* Nothing else is an assignment operator. */
5879 op = ERROR_MARK;
5880 }
5881
5882 /* If it was an assignment operator, consume it. */
5883 if (op != ERROR_MARK)
5884 cp_lexer_consume_token (parser->lexer);
5885
5886 return op;
5887 }
5888
5889 /* Parse an expression.
5890
5891 expression:
5892 assignment-expression
5893 expression , assignment-expression
5894
5895 CAST_P is true if this expression is the target of a cast.
5896
5897 Returns a representation of the expression. */
5898
5899 static tree
5900 cp_parser_expression (cp_parser* parser, bool cast_p)
5901 {
5902 tree expression = NULL_TREE;
5903
5904 while (true)
5905 {
5906 tree assignment_expression;
5907
5908 /* Parse the next assignment-expression. */
5909 assignment_expression
5910 = cp_parser_assignment_expression (parser, cast_p);
5911 /* If this is the first assignment-expression, we can just
5912 save it away. */
5913 if (!expression)
5914 expression = assignment_expression;
5915 else
5916 expression = build_x_compound_expr (expression,
5917 assignment_expression);
5918 /* If the next token is not a comma, then we are done with the
5919 expression. */
5920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5921 break;
5922 /* Consume the `,'. */
5923 cp_lexer_consume_token (parser->lexer);
5924 /* A comma operator cannot appear in a constant-expression. */
5925 if (cp_parser_non_integral_constant_expression (parser,
5926 "a comma operator"))
5927 expression = error_mark_node;
5928 }
5929
5930 return expression;
5931 }
5932
5933 /* Parse a constant-expression.
5934
5935 constant-expression:
5936 conditional-expression
5937
5938 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5939 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5940 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5941 is false, NON_CONSTANT_P should be NULL. */
5942
5943 static tree
5944 cp_parser_constant_expression (cp_parser* parser,
5945 bool allow_non_constant_p,
5946 bool *non_constant_p)
5947 {
5948 bool saved_integral_constant_expression_p;
5949 bool saved_allow_non_integral_constant_expression_p;
5950 bool saved_non_integral_constant_expression_p;
5951 tree expression;
5952
5953 /* It might seem that we could simply parse the
5954 conditional-expression, and then check to see if it were
5955 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5956 one that the compiler can figure out is constant, possibly after
5957 doing some simplifications or optimizations. The standard has a
5958 precise definition of constant-expression, and we must honor
5959 that, even though it is somewhat more restrictive.
5960
5961 For example:
5962
5963 int i[(2, 3)];
5964
5965 is not a legal declaration, because `(2, 3)' is not a
5966 constant-expression. The `,' operator is forbidden in a
5967 constant-expression. However, GCC's constant-folding machinery
5968 will fold this operation to an INTEGER_CST for `3'. */
5969
5970 /* Save the old settings. */
5971 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5972 saved_allow_non_integral_constant_expression_p
5973 = parser->allow_non_integral_constant_expression_p;
5974 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5975 /* We are now parsing a constant-expression. */
5976 parser->integral_constant_expression_p = true;
5977 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5978 parser->non_integral_constant_expression_p = false;
5979 /* Although the grammar says "conditional-expression", we parse an
5980 "assignment-expression", which also permits "throw-expression"
5981 and the use of assignment operators. In the case that
5982 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5983 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5984 actually essential that we look for an assignment-expression.
5985 For example, cp_parser_initializer_clauses uses this function to
5986 determine whether a particular assignment-expression is in fact
5987 constant. */
5988 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5989 /* Restore the old settings. */
5990 parser->integral_constant_expression_p
5991 = saved_integral_constant_expression_p;
5992 parser->allow_non_integral_constant_expression_p
5993 = saved_allow_non_integral_constant_expression_p;
5994 if (allow_non_constant_p)
5995 *non_constant_p = parser->non_integral_constant_expression_p;
5996 else if (parser->non_integral_constant_expression_p)
5997 expression = error_mark_node;
5998 parser->non_integral_constant_expression_p
5999 = saved_non_integral_constant_expression_p;
6000
6001 return expression;
6002 }
6003
6004 /* Parse __builtin_offsetof.
6005
6006 offsetof-expression:
6007 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6008
6009 offsetof-member-designator:
6010 id-expression
6011 | offsetof-member-designator "." id-expression
6012 | offsetof-member-designator "[" expression "]" */
6013
6014 static tree
6015 cp_parser_builtin_offsetof (cp_parser *parser)
6016 {
6017 int save_ice_p, save_non_ice_p;
6018 tree type, expr;
6019 cp_id_kind dummy;
6020
6021 /* We're about to accept non-integral-constant things, but will
6022 definitely yield an integral constant expression. Save and
6023 restore these values around our local parsing. */
6024 save_ice_p = parser->integral_constant_expression_p;
6025 save_non_ice_p = parser->non_integral_constant_expression_p;
6026
6027 /* Consume the "__builtin_offsetof" token. */
6028 cp_lexer_consume_token (parser->lexer);
6029 /* Consume the opening `('. */
6030 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6031 /* Parse the type-id. */
6032 type = cp_parser_type_id (parser);
6033 /* Look for the `,'. */
6034 cp_parser_require (parser, CPP_COMMA, "`,'");
6035
6036 /* Build the (type *)null that begins the traditional offsetof macro. */
6037 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6038
6039 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6040 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6041 true, &dummy);
6042 while (true)
6043 {
6044 cp_token *token = cp_lexer_peek_token (parser->lexer);
6045 switch (token->type)
6046 {
6047 case CPP_OPEN_SQUARE:
6048 /* offsetof-member-designator "[" expression "]" */
6049 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6050 break;
6051
6052 case CPP_DOT:
6053 /* offsetof-member-designator "." identifier */
6054 cp_lexer_consume_token (parser->lexer);
6055 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6056 true, &dummy);
6057 break;
6058
6059 case CPP_CLOSE_PAREN:
6060 /* Consume the ")" token. */
6061 cp_lexer_consume_token (parser->lexer);
6062 goto success;
6063
6064 default:
6065 /* Error. We know the following require will fail, but
6066 that gives the proper error message. */
6067 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6068 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6069 expr = error_mark_node;
6070 goto failure;
6071 }
6072 }
6073
6074 success:
6075 /* If we're processing a template, we can't finish the semantics yet.
6076 Otherwise we can fold the entire expression now. */
6077 if (processing_template_decl)
6078 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6079 else
6080 expr = finish_offsetof (expr);
6081
6082 failure:
6083 parser->integral_constant_expression_p = save_ice_p;
6084 parser->non_integral_constant_expression_p = save_non_ice_p;
6085
6086 return expr;
6087 }
6088
6089 /* Statements [gram.stmt.stmt] */
6090
6091 /* Parse a statement.
6092
6093 statement:
6094 labeled-statement
6095 expression-statement
6096 compound-statement
6097 selection-statement
6098 iteration-statement
6099 jump-statement
6100 declaration-statement
6101 try-block
6102
6103 IN_COMPOUND is true when the statement is nested inside a
6104 cp_parser_compound_statement; this matters for certain pragmas. */
6105
6106 static void
6107 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6108 bool in_compound)
6109 {
6110 tree statement;
6111 cp_token *token;
6112 location_t statement_location;
6113
6114 restart:
6115 /* There is no statement yet. */
6116 statement = NULL_TREE;
6117 /* Peek at the next token. */
6118 token = cp_lexer_peek_token (parser->lexer);
6119 /* Remember the location of the first token in the statement. */
6120 statement_location = token->location;
6121 /* If this is a keyword, then that will often determine what kind of
6122 statement we have. */
6123 if (token->type == CPP_KEYWORD)
6124 {
6125 enum rid keyword = token->keyword;
6126
6127 switch (keyword)
6128 {
6129 case RID_CASE:
6130 case RID_DEFAULT:
6131 /* Looks like a labeled-statement with a case label.
6132 Parse the label, and then use tail recursion to parse
6133 the statement. */
6134 cp_parser_label_for_labeled_statement (parser);
6135 goto restart;
6136
6137 case RID_IF:
6138 case RID_SWITCH:
6139 statement = cp_parser_selection_statement (parser);
6140 break;
6141
6142 case RID_WHILE:
6143 case RID_DO:
6144 case RID_FOR:
6145 statement = cp_parser_iteration_statement (parser);
6146 break;
6147
6148 case RID_BREAK:
6149 case RID_CONTINUE:
6150 case RID_RETURN:
6151 case RID_GOTO:
6152 statement = cp_parser_jump_statement (parser);
6153 break;
6154
6155 /* Objective-C++ exception-handling constructs. */
6156 case RID_AT_TRY:
6157 case RID_AT_CATCH:
6158 case RID_AT_FINALLY:
6159 case RID_AT_SYNCHRONIZED:
6160 case RID_AT_THROW:
6161 statement = cp_parser_objc_statement (parser);
6162 break;
6163
6164 case RID_TRY:
6165 statement = cp_parser_try_block (parser);
6166 break;
6167
6168 default:
6169 /* It might be a keyword like `int' that can start a
6170 declaration-statement. */
6171 break;
6172 }
6173 }
6174 else if (token->type == CPP_NAME)
6175 {
6176 /* If the next token is a `:', then we are looking at a
6177 labeled-statement. */
6178 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6179 if (token->type == CPP_COLON)
6180 {
6181 /* Looks like a labeled-statement with an ordinary label.
6182 Parse the label, and then use tail recursion to parse
6183 the statement. */
6184 cp_parser_label_for_labeled_statement (parser);
6185 goto restart;
6186 }
6187 }
6188 /* Anything that starts with a `{' must be a compound-statement. */
6189 else if (token->type == CPP_OPEN_BRACE)
6190 statement = cp_parser_compound_statement (parser, NULL, false);
6191 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6192 a statement all its own. */
6193 else if (token->type == CPP_PRAGMA)
6194 {
6195 /* Only certain OpenMP pragmas are attached to statements, and thus
6196 are considered statements themselves. All others are not. In
6197 the context of a compound, accept the pragma as a "statement" and
6198 return so that we can check for a close brace. Otherwise we
6199 require a real statement and must go back and read one. */
6200 if (in_compound)
6201 cp_parser_pragma (parser, pragma_compound);
6202 else if (!cp_parser_pragma (parser, pragma_stmt))
6203 goto restart;
6204 return;
6205 }
6206 else if (token->type == CPP_EOF)
6207 {
6208 cp_parser_error (parser, "expected statement");
6209 return;
6210 }
6211
6212 /* Everything else must be a declaration-statement or an
6213 expression-statement. Try for the declaration-statement
6214 first, unless we are looking at a `;', in which case we know that
6215 we have an expression-statement. */
6216 if (!statement)
6217 {
6218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6219 {
6220 cp_parser_parse_tentatively (parser);
6221 /* Try to parse the declaration-statement. */
6222 cp_parser_declaration_statement (parser);
6223 /* If that worked, we're done. */
6224 if (cp_parser_parse_definitely (parser))
6225 return;
6226 }
6227 /* Look for an expression-statement instead. */
6228 statement = cp_parser_expression_statement (parser, in_statement_expr);
6229 }
6230
6231 /* Set the line number for the statement. */
6232 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6233 SET_EXPR_LOCATION (statement, statement_location);
6234 }
6235
6236 /* Parse the label for a labeled-statement, i.e.
6237
6238 identifier :
6239 case constant-expression :
6240 default :
6241
6242 GNU Extension:
6243 case constant-expression ... constant-expression : statement
6244
6245 When a label is parsed without errors, the label is added to the
6246 parse tree by the finish_* functions, so this function doesn't
6247 have to return the label. */
6248
6249 static void
6250 cp_parser_label_for_labeled_statement (cp_parser* parser)
6251 {
6252 cp_token *token;
6253
6254 /* The next token should be an identifier. */
6255 token = cp_lexer_peek_token (parser->lexer);
6256 if (token->type != CPP_NAME
6257 && token->type != CPP_KEYWORD)
6258 {
6259 cp_parser_error (parser, "expected labeled-statement");
6260 return;
6261 }
6262
6263 switch (token->keyword)
6264 {
6265 case RID_CASE:
6266 {
6267 tree expr, expr_hi;
6268 cp_token *ellipsis;
6269
6270 /* Consume the `case' token. */
6271 cp_lexer_consume_token (parser->lexer);
6272 /* Parse the constant-expression. */
6273 expr = cp_parser_constant_expression (parser,
6274 /*allow_non_constant_p=*/false,
6275 NULL);
6276
6277 ellipsis = cp_lexer_peek_token (parser->lexer);
6278 if (ellipsis->type == CPP_ELLIPSIS)
6279 {
6280 /* Consume the `...' token. */
6281 cp_lexer_consume_token (parser->lexer);
6282 expr_hi =
6283 cp_parser_constant_expression (parser,
6284 /*allow_non_constant_p=*/false,
6285 NULL);
6286 /* We don't need to emit warnings here, as the common code
6287 will do this for us. */
6288 }
6289 else
6290 expr_hi = NULL_TREE;
6291
6292 if (parser->in_switch_statement_p)
6293 finish_case_label (expr, expr_hi);
6294 else
6295 error ("case label %qE not within a switch statement", expr);
6296 }
6297 break;
6298
6299 case RID_DEFAULT:
6300 /* Consume the `default' token. */
6301 cp_lexer_consume_token (parser->lexer);
6302
6303 if (parser->in_switch_statement_p)
6304 finish_case_label (NULL_TREE, NULL_TREE);
6305 else
6306 error ("case label not within a switch statement");
6307 break;
6308
6309 default:
6310 /* Anything else must be an ordinary label. */
6311 finish_label_stmt (cp_parser_identifier (parser));
6312 break;
6313 }
6314
6315 /* Require the `:' token. */
6316 cp_parser_require (parser, CPP_COLON, "`:'");
6317 }
6318
6319 /* Parse an expression-statement.
6320
6321 expression-statement:
6322 expression [opt] ;
6323
6324 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6325 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6326 indicates whether this expression-statement is part of an
6327 expression statement. */
6328
6329 static tree
6330 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6331 {
6332 tree statement = NULL_TREE;
6333
6334 /* If the next token is a ';', then there is no expression
6335 statement. */
6336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6337 statement = cp_parser_expression (parser, /*cast_p=*/false);
6338
6339 /* Consume the final `;'. */
6340 cp_parser_consume_semicolon_at_end_of_statement (parser);
6341
6342 if (in_statement_expr
6343 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6344 /* This is the final expression statement of a statement
6345 expression. */
6346 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6347 else if (statement)
6348 statement = finish_expr_stmt (statement);
6349 else
6350 finish_stmt ();
6351
6352 return statement;
6353 }
6354
6355 /* Parse a compound-statement.
6356
6357 compound-statement:
6358 { statement-seq [opt] }
6359
6360 Returns a tree representing the statement. */
6361
6362 static tree
6363 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6364 bool in_try)
6365 {
6366 tree compound_stmt;
6367
6368 /* Consume the `{'. */
6369 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6370 return error_mark_node;
6371 /* Begin the compound-statement. */
6372 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6373 /* Parse an (optional) statement-seq. */
6374 cp_parser_statement_seq_opt (parser, in_statement_expr);
6375 /* Finish the compound-statement. */
6376 finish_compound_stmt (compound_stmt);
6377 /* Consume the `}'. */
6378 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6379
6380 return compound_stmt;
6381 }
6382
6383 /* Parse an (optional) statement-seq.
6384
6385 statement-seq:
6386 statement
6387 statement-seq [opt] statement */
6388
6389 static void
6390 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6391 {
6392 /* Scan statements until there aren't any more. */
6393 while (true)
6394 {
6395 cp_token *token = cp_lexer_peek_token (parser->lexer);
6396
6397 /* If we're looking at a `}', then we've run out of statements. */
6398 if (token->type == CPP_CLOSE_BRACE
6399 || token->type == CPP_EOF
6400 || token->type == CPP_PRAGMA_EOL)
6401 break;
6402
6403 /* Parse the statement. */
6404 cp_parser_statement (parser, in_statement_expr, true);
6405 }
6406 }
6407
6408 /* Parse a selection-statement.
6409
6410 selection-statement:
6411 if ( condition ) statement
6412 if ( condition ) statement else statement
6413 switch ( condition ) statement
6414
6415 Returns the new IF_STMT or SWITCH_STMT. */
6416
6417 static tree
6418 cp_parser_selection_statement (cp_parser* parser)
6419 {
6420 cp_token *token;
6421 enum rid keyword;
6422
6423 /* Peek at the next token. */
6424 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6425
6426 /* See what kind of keyword it is. */
6427 keyword = token->keyword;
6428 switch (keyword)
6429 {
6430 case RID_IF:
6431 case RID_SWITCH:
6432 {
6433 tree statement;
6434 tree condition;
6435
6436 /* Look for the `('. */
6437 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6438 {
6439 cp_parser_skip_to_end_of_statement (parser);
6440 return error_mark_node;
6441 }
6442
6443 /* Begin the selection-statement. */
6444 if (keyword == RID_IF)
6445 statement = begin_if_stmt ();
6446 else
6447 statement = begin_switch_stmt ();
6448
6449 /* Parse the condition. */
6450 condition = cp_parser_condition (parser);
6451 /* Look for the `)'. */
6452 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6453 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6454 /*consume_paren=*/true);
6455
6456 if (keyword == RID_IF)
6457 {
6458 /* Add the condition. */
6459 finish_if_stmt_cond (condition, statement);
6460
6461 /* Parse the then-clause. */
6462 cp_parser_implicitly_scoped_statement (parser);
6463 finish_then_clause (statement);
6464
6465 /* If the next token is `else', parse the else-clause. */
6466 if (cp_lexer_next_token_is_keyword (parser->lexer,
6467 RID_ELSE))
6468 {
6469 /* Consume the `else' keyword. */
6470 cp_lexer_consume_token (parser->lexer);
6471 begin_else_clause (statement);
6472 /* Parse the else-clause. */
6473 cp_parser_implicitly_scoped_statement (parser);
6474 finish_else_clause (statement);
6475 }
6476
6477 /* Now we're all done with the if-statement. */
6478 finish_if_stmt (statement);
6479 }
6480 else
6481 {
6482 bool in_switch_statement_p;
6483 unsigned char in_statement;
6484
6485 /* Add the condition. */
6486 finish_switch_cond (condition, statement);
6487
6488 /* Parse the body of the switch-statement. */
6489 in_switch_statement_p = parser->in_switch_statement_p;
6490 in_statement = parser->in_statement;
6491 parser->in_switch_statement_p = true;
6492 parser->in_statement |= IN_SWITCH_STMT;
6493 cp_parser_implicitly_scoped_statement (parser);
6494 parser->in_switch_statement_p = in_switch_statement_p;
6495 parser->in_statement = in_statement;
6496
6497 /* Now we're all done with the switch-statement. */
6498 finish_switch_stmt (statement);
6499 }
6500
6501 return statement;
6502 }
6503 break;
6504
6505 default:
6506 cp_parser_error (parser, "expected selection-statement");
6507 return error_mark_node;
6508 }
6509 }
6510
6511 /* Parse a condition.
6512
6513 condition:
6514 expression
6515 type-specifier-seq declarator = assignment-expression
6516
6517 GNU Extension:
6518
6519 condition:
6520 type-specifier-seq declarator asm-specification [opt]
6521 attributes [opt] = assignment-expression
6522
6523 Returns the expression that should be tested. */
6524
6525 static tree
6526 cp_parser_condition (cp_parser* parser)
6527 {
6528 cp_decl_specifier_seq type_specifiers;
6529 const char *saved_message;
6530
6531 /* Try the declaration first. */
6532 cp_parser_parse_tentatively (parser);
6533 /* New types are not allowed in the type-specifier-seq for a
6534 condition. */
6535 saved_message = parser->type_definition_forbidden_message;
6536 parser->type_definition_forbidden_message
6537 = "types may not be defined in conditions";
6538 /* Parse the type-specifier-seq. */
6539 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6540 &type_specifiers);
6541 /* Restore the saved message. */
6542 parser->type_definition_forbidden_message = saved_message;
6543 /* If all is well, we might be looking at a declaration. */
6544 if (!cp_parser_error_occurred (parser))
6545 {
6546 tree decl;
6547 tree asm_specification;
6548 tree attributes;
6549 cp_declarator *declarator;
6550 tree initializer = NULL_TREE;
6551
6552 /* Parse the declarator. */
6553 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6554 /*ctor_dtor_or_conv_p=*/NULL,
6555 /*parenthesized_p=*/NULL,
6556 /*member_p=*/false);
6557 /* Parse the attributes. */
6558 attributes = cp_parser_attributes_opt (parser);
6559 /* Parse the asm-specification. */
6560 asm_specification = cp_parser_asm_specification_opt (parser);
6561 /* If the next token is not an `=', then we might still be
6562 looking at an expression. For example:
6563
6564 if (A(a).x)
6565
6566 looks like a decl-specifier-seq and a declarator -- but then
6567 there is no `=', so this is an expression. */
6568 cp_parser_require (parser, CPP_EQ, "`='");
6569 /* If we did see an `=', then we are looking at a declaration
6570 for sure. */
6571 if (cp_parser_parse_definitely (parser))
6572 {
6573 tree pushed_scope;
6574 bool non_constant_p;
6575
6576 /* Create the declaration. */
6577 decl = start_decl (declarator, &type_specifiers,
6578 /*initialized_p=*/true,
6579 attributes, /*prefix_attributes=*/NULL_TREE,
6580 &pushed_scope);
6581 /* Parse the assignment-expression. */
6582 initializer
6583 = cp_parser_constant_expression (parser,
6584 /*allow_non_constant_p=*/true,
6585 &non_constant_p);
6586 if (!non_constant_p)
6587 initializer = fold_non_dependent_expr (initializer);
6588
6589 /* Process the initializer. */
6590 cp_finish_decl (decl,
6591 initializer, !non_constant_p,
6592 asm_specification,
6593 LOOKUP_ONLYCONVERTING);
6594
6595 if (pushed_scope)
6596 pop_scope (pushed_scope);
6597
6598 return convert_from_reference (decl);
6599 }
6600 }
6601 /* If we didn't even get past the declarator successfully, we are
6602 definitely not looking at a declaration. */
6603 else
6604 cp_parser_abort_tentative_parse (parser);
6605
6606 /* Otherwise, we are looking at an expression. */
6607 return cp_parser_expression (parser, /*cast_p=*/false);
6608 }
6609
6610 /* Parse an iteration-statement.
6611
6612 iteration-statement:
6613 while ( condition ) statement
6614 do statement while ( expression ) ;
6615 for ( for-init-statement condition [opt] ; expression [opt] )
6616 statement
6617
6618 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6619
6620 static tree
6621 cp_parser_iteration_statement (cp_parser* parser)
6622 {
6623 cp_token *token;
6624 enum rid keyword;
6625 tree statement;
6626 unsigned char in_statement;
6627
6628 /* Peek at the next token. */
6629 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6630 if (!token)
6631 return error_mark_node;
6632
6633 /* Remember whether or not we are already within an iteration
6634 statement. */
6635 in_statement = parser->in_statement;
6636
6637 /* See what kind of keyword it is. */
6638 keyword = token->keyword;
6639 switch (keyword)
6640 {
6641 case RID_WHILE:
6642 {
6643 tree condition;
6644
6645 /* Begin the while-statement. */
6646 statement = begin_while_stmt ();
6647 /* Look for the `('. */
6648 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6649 /* Parse the condition. */
6650 condition = cp_parser_condition (parser);
6651 finish_while_stmt_cond (condition, statement);
6652 /* Look for the `)'. */
6653 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6654 /* Parse the dependent statement. */
6655 parser->in_statement = IN_ITERATION_STMT;
6656 cp_parser_already_scoped_statement (parser);
6657 parser->in_statement = in_statement;
6658 /* We're done with the while-statement. */
6659 finish_while_stmt (statement);
6660 }
6661 break;
6662
6663 case RID_DO:
6664 {
6665 tree expression;
6666
6667 /* Begin the do-statement. */
6668 statement = begin_do_stmt ();
6669 /* Parse the body of the do-statement. */
6670 parser->in_statement = IN_ITERATION_STMT;
6671 cp_parser_implicitly_scoped_statement (parser);
6672 parser->in_statement = in_statement;
6673 finish_do_body (statement);
6674 /* Look for the `while' keyword. */
6675 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6676 /* Look for the `('. */
6677 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6678 /* Parse the expression. */
6679 expression = cp_parser_expression (parser, /*cast_p=*/false);
6680 /* We're done with the do-statement. */
6681 finish_do_stmt (expression, statement);
6682 /* Look for the `)'. */
6683 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6684 /* Look for the `;'. */
6685 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6686 }
6687 break;
6688
6689 case RID_FOR:
6690 {
6691 tree condition = NULL_TREE;
6692 tree expression = NULL_TREE;
6693
6694 /* Begin the for-statement. */
6695 statement = begin_for_stmt ();
6696 /* Look for the `('. */
6697 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6698 /* Parse the initialization. */
6699 cp_parser_for_init_statement (parser);
6700 finish_for_init_stmt (statement);
6701
6702 /* If there's a condition, process it. */
6703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6704 condition = cp_parser_condition (parser);
6705 finish_for_cond (condition, statement);
6706 /* Look for the `;'. */
6707 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6708
6709 /* If there's an expression, process it. */
6710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6711 expression = cp_parser_expression (parser, /*cast_p=*/false);
6712 finish_for_expr (expression, statement);
6713 /* Look for the `)'. */
6714 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6715
6716 /* Parse the body of the for-statement. */
6717 parser->in_statement = IN_ITERATION_STMT;
6718 cp_parser_already_scoped_statement (parser);
6719 parser->in_statement = in_statement;
6720
6721 /* We're done with the for-statement. */
6722 finish_for_stmt (statement);
6723 }
6724 break;
6725
6726 default:
6727 cp_parser_error (parser, "expected iteration-statement");
6728 statement = error_mark_node;
6729 break;
6730 }
6731
6732 return statement;
6733 }
6734
6735 /* Parse a for-init-statement.
6736
6737 for-init-statement:
6738 expression-statement
6739 simple-declaration */
6740
6741 static void
6742 cp_parser_for_init_statement (cp_parser* parser)
6743 {
6744 /* If the next token is a `;', then we have an empty
6745 expression-statement. Grammatically, this is also a
6746 simple-declaration, but an invalid one, because it does not
6747 declare anything. Therefore, if we did not handle this case
6748 specially, we would issue an error message about an invalid
6749 declaration. */
6750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6751 {
6752 /* We're going to speculatively look for a declaration, falling back
6753 to an expression, if necessary. */
6754 cp_parser_parse_tentatively (parser);
6755 /* Parse the declaration. */
6756 cp_parser_simple_declaration (parser,
6757 /*function_definition_allowed_p=*/false);
6758 /* If the tentative parse failed, then we shall need to look for an
6759 expression-statement. */
6760 if (cp_parser_parse_definitely (parser))
6761 return;
6762 }
6763
6764 cp_parser_expression_statement (parser, false);
6765 }
6766
6767 /* Parse a jump-statement.
6768
6769 jump-statement:
6770 break ;
6771 continue ;
6772 return expression [opt] ;
6773 goto identifier ;
6774
6775 GNU extension:
6776
6777 jump-statement:
6778 goto * expression ;
6779
6780 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6781
6782 static tree
6783 cp_parser_jump_statement (cp_parser* parser)
6784 {
6785 tree statement = error_mark_node;
6786 cp_token *token;
6787 enum rid keyword;
6788
6789 /* Peek at the next token. */
6790 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6791 if (!token)
6792 return error_mark_node;
6793
6794 /* See what kind of keyword it is. */
6795 keyword = token->keyword;
6796 switch (keyword)
6797 {
6798 case RID_BREAK:
6799 switch (parser->in_statement)
6800 {
6801 case 0:
6802 error ("break statement not within loop or switch");
6803 break;
6804 default:
6805 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6806 || parser->in_statement == IN_ITERATION_STMT);
6807 statement = finish_break_stmt ();
6808 break;
6809 case IN_OMP_BLOCK:
6810 error ("invalid exit from OpenMP structured block");
6811 break;
6812 case IN_OMP_FOR:
6813 error ("break statement used with OpenMP for loop");
6814 break;
6815 }
6816 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6817 break;
6818
6819 case RID_CONTINUE:
6820 switch (parser->in_statement & ~IN_SWITCH_STMT)
6821 {
6822 case 0:
6823 error ("continue statement not within a loop");
6824 break;
6825 case IN_ITERATION_STMT:
6826 case IN_OMP_FOR:
6827 statement = finish_continue_stmt ();
6828 break;
6829 case IN_OMP_BLOCK:
6830 error ("invalid exit from OpenMP structured block");
6831 break;
6832 default:
6833 gcc_unreachable ();
6834 }
6835 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6836 break;
6837
6838 case RID_RETURN:
6839 {
6840 tree expr;
6841
6842 /* If the next token is a `;', then there is no
6843 expression. */
6844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6845 expr = cp_parser_expression (parser, /*cast_p=*/false);
6846 else
6847 expr = NULL_TREE;
6848 /* Build the return-statement. */
6849 statement = finish_return_stmt (expr);
6850 /* Look for the final `;'. */
6851 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6852 }
6853 break;
6854
6855 case RID_GOTO:
6856 /* Create the goto-statement. */
6857 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6858 {
6859 /* Issue a warning about this use of a GNU extension. */
6860 if (pedantic)
6861 pedwarn ("ISO C++ forbids computed gotos");
6862 /* Consume the '*' token. */
6863 cp_lexer_consume_token (parser->lexer);
6864 /* Parse the dependent expression. */
6865 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6866 }
6867 else
6868 finish_goto_stmt (cp_parser_identifier (parser));
6869 /* Look for the final `;'. */
6870 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6871 break;
6872
6873 default:
6874 cp_parser_error (parser, "expected jump-statement");
6875 break;
6876 }
6877
6878 return statement;
6879 }
6880
6881 /* Parse a declaration-statement.
6882
6883 declaration-statement:
6884 block-declaration */
6885
6886 static void
6887 cp_parser_declaration_statement (cp_parser* parser)
6888 {
6889 void *p;
6890
6891 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6892 p = obstack_alloc (&declarator_obstack, 0);
6893
6894 /* Parse the block-declaration. */
6895 cp_parser_block_declaration (parser, /*statement_p=*/true);
6896
6897 /* Free any declarators allocated. */
6898 obstack_free (&declarator_obstack, p);
6899
6900 /* Finish off the statement. */
6901 finish_stmt ();
6902 }
6903
6904 /* Some dependent statements (like `if (cond) statement'), are
6905 implicitly in their own scope. In other words, if the statement is
6906 a single statement (as opposed to a compound-statement), it is
6907 none-the-less treated as if it were enclosed in braces. Any
6908 declarations appearing in the dependent statement are out of scope
6909 after control passes that point. This function parses a statement,
6910 but ensures that is in its own scope, even if it is not a
6911 compound-statement.
6912
6913 Returns the new statement. */
6914
6915 static tree
6916 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6917 {
6918 tree statement;
6919
6920 /* Mark if () ; with a special NOP_EXPR. */
6921 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6922 {
6923 cp_lexer_consume_token (parser->lexer);
6924 statement = add_stmt (build_empty_stmt ());
6925 }
6926 /* if a compound is opened, we simply parse the statement directly. */
6927 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6928 statement = cp_parser_compound_statement (parser, NULL, false);
6929 /* If the token is not a `{', then we must take special action. */
6930 else
6931 {
6932 /* Create a compound-statement. */
6933 statement = begin_compound_stmt (0);
6934 /* Parse the dependent-statement. */
6935 cp_parser_statement (parser, NULL_TREE, false);
6936 /* Finish the dummy compound-statement. */
6937 finish_compound_stmt (statement);
6938 }
6939
6940 /* Return the statement. */
6941 return statement;
6942 }
6943
6944 /* For some dependent statements (like `while (cond) statement'), we
6945 have already created a scope. Therefore, even if the dependent
6946 statement is a compound-statement, we do not want to create another
6947 scope. */
6948
6949 static void
6950 cp_parser_already_scoped_statement (cp_parser* parser)
6951 {
6952 /* If the token is a `{', then we must take special action. */
6953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6954 cp_parser_statement (parser, NULL_TREE, false);
6955 else
6956 {
6957 /* Avoid calling cp_parser_compound_statement, so that we
6958 don't create a new scope. Do everything else by hand. */
6959 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6960 cp_parser_statement_seq_opt (parser, NULL_TREE);
6961 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6962 }
6963 }
6964
6965 /* Declarations [gram.dcl.dcl] */
6966
6967 /* Parse an optional declaration-sequence.
6968
6969 declaration-seq:
6970 declaration
6971 declaration-seq declaration */
6972
6973 static void
6974 cp_parser_declaration_seq_opt (cp_parser* parser)
6975 {
6976 while (true)
6977 {
6978 cp_token *token;
6979
6980 token = cp_lexer_peek_token (parser->lexer);
6981
6982 if (token->type == CPP_CLOSE_BRACE
6983 || token->type == CPP_EOF
6984 || token->type == CPP_PRAGMA_EOL)
6985 break;
6986
6987 if (token->type == CPP_SEMICOLON)
6988 {
6989 /* A declaration consisting of a single semicolon is
6990 invalid. Allow it unless we're being pedantic. */
6991 cp_lexer_consume_token (parser->lexer);
6992 if (pedantic && !in_system_header)
6993 pedwarn ("extra %<;%>");
6994 continue;
6995 }
6996
6997 /* If we're entering or exiting a region that's implicitly
6998 extern "C", modify the lang context appropriately. */
6999 if (!parser->implicit_extern_c && token->implicit_extern_c)
7000 {
7001 push_lang_context (lang_name_c);
7002 parser->implicit_extern_c = true;
7003 }
7004 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7005 {
7006 pop_lang_context ();
7007 parser->implicit_extern_c = false;
7008 }
7009
7010 if (token->type == CPP_PRAGMA)
7011 {
7012 /* A top-level declaration can consist solely of a #pragma.
7013 A nested declaration cannot, so this is done here and not
7014 in cp_parser_declaration. (A #pragma at block scope is
7015 handled in cp_parser_statement.) */
7016 cp_parser_pragma (parser, pragma_external);
7017 continue;
7018 }
7019
7020 /* Parse the declaration itself. */
7021 cp_parser_declaration (parser);
7022 }
7023 }
7024
7025 /* Parse a declaration.
7026
7027 declaration:
7028 block-declaration
7029 function-definition
7030 template-declaration
7031 explicit-instantiation
7032 explicit-specialization
7033 linkage-specification
7034 namespace-definition
7035
7036 GNU extension:
7037
7038 declaration:
7039 __extension__ declaration */
7040
7041 static void
7042 cp_parser_declaration (cp_parser* parser)
7043 {
7044 cp_token token1;
7045 cp_token token2;
7046 int saved_pedantic;
7047 void *p;
7048
7049 /* Check for the `__extension__' keyword. */
7050 if (cp_parser_extension_opt (parser, &saved_pedantic))
7051 {
7052 /* Parse the qualified declaration. */
7053 cp_parser_declaration (parser);
7054 /* Restore the PEDANTIC flag. */
7055 pedantic = saved_pedantic;
7056
7057 return;
7058 }
7059
7060 /* Try to figure out what kind of declaration is present. */
7061 token1 = *cp_lexer_peek_token (parser->lexer);
7062
7063 if (token1.type != CPP_EOF)
7064 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7065 else
7066 {
7067 token2.type = CPP_EOF;
7068 token2.keyword = RID_MAX;
7069 }
7070
7071 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7072 p = obstack_alloc (&declarator_obstack, 0);
7073
7074 /* If the next token is `extern' and the following token is a string
7075 literal, then we have a linkage specification. */
7076 if (token1.keyword == RID_EXTERN
7077 && cp_parser_is_string_literal (&token2))
7078 cp_parser_linkage_specification (parser);
7079 /* If the next token is `template', then we have either a template
7080 declaration, an explicit instantiation, or an explicit
7081 specialization. */
7082 else if (token1.keyword == RID_TEMPLATE)
7083 {
7084 /* `template <>' indicates a template specialization. */
7085 if (token2.type == CPP_LESS
7086 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7087 cp_parser_explicit_specialization (parser);
7088 /* `template <' indicates a template declaration. */
7089 else if (token2.type == CPP_LESS)
7090 cp_parser_template_declaration (parser, /*member_p=*/false);
7091 /* Anything else must be an explicit instantiation. */
7092 else
7093 cp_parser_explicit_instantiation (parser);
7094 }
7095 /* If the next token is `export', then we have a template
7096 declaration. */
7097 else if (token1.keyword == RID_EXPORT)
7098 cp_parser_template_declaration (parser, /*member_p=*/false);
7099 /* If the next token is `extern', 'static' or 'inline' and the one
7100 after that is `template', we have a GNU extended explicit
7101 instantiation directive. */
7102 else if (cp_parser_allow_gnu_extensions_p (parser)
7103 && (token1.keyword == RID_EXTERN
7104 || token1.keyword == RID_STATIC
7105 || token1.keyword == RID_INLINE)
7106 && token2.keyword == RID_TEMPLATE)
7107 cp_parser_explicit_instantiation (parser);
7108 /* If the next token is `namespace', check for a named or unnamed
7109 namespace definition. */
7110 else if (token1.keyword == RID_NAMESPACE
7111 && (/* A named namespace definition. */
7112 (token2.type == CPP_NAME
7113 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7114 != CPP_EQ))
7115 /* An unnamed namespace definition. */
7116 || token2.type == CPP_OPEN_BRACE
7117 || token2.keyword == RID_ATTRIBUTE))
7118 cp_parser_namespace_definition (parser);
7119 /* Objective-C++ declaration/definition. */
7120 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7121 cp_parser_objc_declaration (parser);
7122 /* We must have either a block declaration or a function
7123 definition. */
7124 else
7125 /* Try to parse a block-declaration, or a function-definition. */
7126 cp_parser_block_declaration (parser, /*statement_p=*/false);
7127
7128 /* Free any declarators allocated. */
7129 obstack_free (&declarator_obstack, p);
7130 }
7131
7132 /* Parse a block-declaration.
7133
7134 block-declaration:
7135 simple-declaration
7136 asm-definition
7137 namespace-alias-definition
7138 using-declaration
7139 using-directive
7140
7141 GNU Extension:
7142
7143 block-declaration:
7144 __extension__ block-declaration
7145 label-declaration
7146
7147 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7148 part of a declaration-statement. */
7149
7150 static void
7151 cp_parser_block_declaration (cp_parser *parser,
7152 bool statement_p)
7153 {
7154 cp_token *token1;
7155 int saved_pedantic;
7156
7157 /* Check for the `__extension__' keyword. */
7158 if (cp_parser_extension_opt (parser, &saved_pedantic))
7159 {
7160 /* Parse the qualified declaration. */
7161 cp_parser_block_declaration (parser, statement_p);
7162 /* Restore the PEDANTIC flag. */
7163 pedantic = saved_pedantic;
7164
7165 return;
7166 }
7167
7168 /* Peek at the next token to figure out which kind of declaration is
7169 present. */
7170 token1 = cp_lexer_peek_token (parser->lexer);
7171
7172 /* If the next keyword is `asm', we have an asm-definition. */
7173 if (token1->keyword == RID_ASM)
7174 {
7175 if (statement_p)
7176 cp_parser_commit_to_tentative_parse (parser);
7177 cp_parser_asm_definition (parser);
7178 }
7179 /* If the next keyword is `namespace', we have a
7180 namespace-alias-definition. */
7181 else if (token1->keyword == RID_NAMESPACE)
7182 cp_parser_namespace_alias_definition (parser);
7183 /* If the next keyword is `using', we have either a
7184 using-declaration or a using-directive. */
7185 else if (token1->keyword == RID_USING)
7186 {
7187 cp_token *token2;
7188
7189 if (statement_p)
7190 cp_parser_commit_to_tentative_parse (parser);
7191 /* If the token after `using' is `namespace', then we have a
7192 using-directive. */
7193 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7194 if (token2->keyword == RID_NAMESPACE)
7195 cp_parser_using_directive (parser);
7196 /* Otherwise, it's a using-declaration. */
7197 else
7198 cp_parser_using_declaration (parser,
7199 /*access_declaration_p=*/false);
7200 }
7201 /* If the next keyword is `__label__' we have a label declaration. */
7202 else if (token1->keyword == RID_LABEL)
7203 {
7204 if (statement_p)
7205 cp_parser_commit_to_tentative_parse (parser);
7206 cp_parser_label_declaration (parser);
7207 }
7208 /* Anything else must be a simple-declaration. */
7209 else
7210 cp_parser_simple_declaration (parser, !statement_p);
7211 }
7212
7213 /* Parse a simple-declaration.
7214
7215 simple-declaration:
7216 decl-specifier-seq [opt] init-declarator-list [opt] ;
7217
7218 init-declarator-list:
7219 init-declarator
7220 init-declarator-list , init-declarator
7221
7222 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7223 function-definition as a simple-declaration. */
7224
7225 static void
7226 cp_parser_simple_declaration (cp_parser* parser,
7227 bool function_definition_allowed_p)
7228 {
7229 cp_decl_specifier_seq decl_specifiers;
7230 int declares_class_or_enum;
7231 bool saw_declarator;
7232
7233 /* Defer access checks until we know what is being declared; the
7234 checks for names appearing in the decl-specifier-seq should be
7235 done as if we were in the scope of the thing being declared. */
7236 push_deferring_access_checks (dk_deferred);
7237
7238 /* Parse the decl-specifier-seq. We have to keep track of whether
7239 or not the decl-specifier-seq declares a named class or
7240 enumeration type, since that is the only case in which the
7241 init-declarator-list is allowed to be empty.
7242
7243 [dcl.dcl]
7244
7245 In a simple-declaration, the optional init-declarator-list can be
7246 omitted only when declaring a class or enumeration, that is when
7247 the decl-specifier-seq contains either a class-specifier, an
7248 elaborated-type-specifier, or an enum-specifier. */
7249 cp_parser_decl_specifier_seq (parser,
7250 CP_PARSER_FLAGS_OPTIONAL,
7251 &decl_specifiers,
7252 &declares_class_or_enum);
7253 /* We no longer need to defer access checks. */
7254 stop_deferring_access_checks ();
7255
7256 /* In a block scope, a valid declaration must always have a
7257 decl-specifier-seq. By not trying to parse declarators, we can
7258 resolve the declaration/expression ambiguity more quickly. */
7259 if (!function_definition_allowed_p
7260 && !decl_specifiers.any_specifiers_p)
7261 {
7262 cp_parser_error (parser, "expected declaration");
7263 goto done;
7264 }
7265
7266 /* If the next two tokens are both identifiers, the code is
7267 erroneous. The usual cause of this situation is code like:
7268
7269 T t;
7270
7271 where "T" should name a type -- but does not. */
7272 if (!decl_specifiers.type
7273 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7274 {
7275 /* If parsing tentatively, we should commit; we really are
7276 looking at a declaration. */
7277 cp_parser_commit_to_tentative_parse (parser);
7278 /* Give up. */
7279 goto done;
7280 }
7281
7282 /* If we have seen at least one decl-specifier, and the next token
7283 is not a parenthesis, then we must be looking at a declaration.
7284 (After "int (" we might be looking at a functional cast.) */
7285 if (decl_specifiers.any_specifiers_p
7286 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7287 cp_parser_commit_to_tentative_parse (parser);
7288
7289 /* Keep going until we hit the `;' at the end of the simple
7290 declaration. */
7291 saw_declarator = false;
7292 while (cp_lexer_next_token_is_not (parser->lexer,
7293 CPP_SEMICOLON))
7294 {
7295 cp_token *token;
7296 bool function_definition_p;
7297 tree decl;
7298
7299 if (saw_declarator)
7300 {
7301 /* If we are processing next declarator, coma is expected */
7302 token = cp_lexer_peek_token (parser->lexer);
7303 gcc_assert (token->type == CPP_COMMA);
7304 cp_lexer_consume_token (parser->lexer);
7305 }
7306 else
7307 saw_declarator = true;
7308
7309 /* Parse the init-declarator. */
7310 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7311 /*checks=*/NULL_TREE,
7312 function_definition_allowed_p,
7313 /*member_p=*/false,
7314 declares_class_or_enum,
7315 &function_definition_p);
7316 /* If an error occurred while parsing tentatively, exit quickly.
7317 (That usually happens when in the body of a function; each
7318 statement is treated as a declaration-statement until proven
7319 otherwise.) */
7320 if (cp_parser_error_occurred (parser))
7321 goto done;
7322 /* Handle function definitions specially. */
7323 if (function_definition_p)
7324 {
7325 /* If the next token is a `,', then we are probably
7326 processing something like:
7327
7328 void f() {}, *p;
7329
7330 which is erroneous. */
7331 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7332 error ("mixing declarations and function-definitions is forbidden");
7333 /* Otherwise, we're done with the list of declarators. */
7334 else
7335 {
7336 pop_deferring_access_checks ();
7337 return;
7338 }
7339 }
7340 /* The next token should be either a `,' or a `;'. */
7341 token = cp_lexer_peek_token (parser->lexer);
7342 /* If it's a `,', there are more declarators to come. */
7343 if (token->type == CPP_COMMA)
7344 /* will be consumed next time around */;
7345 /* If it's a `;', we are done. */
7346 else if (token->type == CPP_SEMICOLON)
7347 break;
7348 /* Anything else is an error. */
7349 else
7350 {
7351 /* If we have already issued an error message we don't need
7352 to issue another one. */
7353 if (decl != error_mark_node
7354 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7355 cp_parser_error (parser, "expected %<,%> or %<;%>");
7356 /* Skip tokens until we reach the end of the statement. */
7357 cp_parser_skip_to_end_of_statement (parser);
7358 /* If the next token is now a `;', consume it. */
7359 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7360 cp_lexer_consume_token (parser->lexer);
7361 goto done;
7362 }
7363 /* After the first time around, a function-definition is not
7364 allowed -- even if it was OK at first. For example:
7365
7366 int i, f() {}
7367
7368 is not valid. */
7369 function_definition_allowed_p = false;
7370 }
7371
7372 /* Issue an error message if no declarators are present, and the
7373 decl-specifier-seq does not itself declare a class or
7374 enumeration. */
7375 if (!saw_declarator)
7376 {
7377 if (cp_parser_declares_only_class_p (parser))
7378 shadow_tag (&decl_specifiers);
7379 /* Perform any deferred access checks. */
7380 perform_deferred_access_checks ();
7381 }
7382
7383 /* Consume the `;'. */
7384 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7385
7386 done:
7387 pop_deferring_access_checks ();
7388 }
7389
7390 /* Parse a decl-specifier-seq.
7391
7392 decl-specifier-seq:
7393 decl-specifier-seq [opt] decl-specifier
7394
7395 decl-specifier:
7396 storage-class-specifier
7397 type-specifier
7398 function-specifier
7399 friend
7400 typedef
7401
7402 GNU Extension:
7403
7404 decl-specifier:
7405 attributes
7406
7407 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7408
7409 The parser flags FLAGS is used to control type-specifier parsing.
7410
7411 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7412 flags:
7413
7414 1: one of the decl-specifiers is an elaborated-type-specifier
7415 (i.e., a type declaration)
7416 2: one of the decl-specifiers is an enum-specifier or a
7417 class-specifier (i.e., a type definition)
7418
7419 */
7420
7421 static void
7422 cp_parser_decl_specifier_seq (cp_parser* parser,
7423 cp_parser_flags flags,
7424 cp_decl_specifier_seq *decl_specs,
7425 int* declares_class_or_enum)
7426 {
7427 bool constructor_possible_p = !parser->in_declarator_p;
7428
7429 /* Clear DECL_SPECS. */
7430 clear_decl_specs (decl_specs);
7431
7432 /* Assume no class or enumeration type is declared. */
7433 *declares_class_or_enum = 0;
7434
7435 /* Keep reading specifiers until there are no more to read. */
7436 while (true)
7437 {
7438 bool constructor_p;
7439 bool found_decl_spec;
7440 cp_token *token;
7441
7442 /* Peek at the next token. */
7443 token = cp_lexer_peek_token (parser->lexer);
7444 /* Handle attributes. */
7445 if (token->keyword == RID_ATTRIBUTE)
7446 {
7447 /* Parse the attributes. */
7448 decl_specs->attributes
7449 = chainon (decl_specs->attributes,
7450 cp_parser_attributes_opt (parser));
7451 continue;
7452 }
7453 /* Assume we will find a decl-specifier keyword. */
7454 found_decl_spec = true;
7455 /* If the next token is an appropriate keyword, we can simply
7456 add it to the list. */
7457 switch (token->keyword)
7458 {
7459 /* decl-specifier:
7460 friend */
7461 case RID_FRIEND:
7462 if (!at_class_scope_p ())
7463 {
7464 error ("%<friend%> used outside of class");
7465 cp_lexer_purge_token (parser->lexer);
7466 }
7467 else
7468 {
7469 ++decl_specs->specs[(int) ds_friend];
7470 /* Consume the token. */
7471 cp_lexer_consume_token (parser->lexer);
7472 }
7473 break;
7474
7475 /* function-specifier:
7476 inline
7477 virtual
7478 explicit */
7479 case RID_INLINE:
7480 case RID_VIRTUAL:
7481 case RID_EXPLICIT:
7482 cp_parser_function_specifier_opt (parser, decl_specs);
7483 break;
7484
7485 /* decl-specifier:
7486 typedef */
7487 case RID_TYPEDEF:
7488 ++decl_specs->specs[(int) ds_typedef];
7489 /* Consume the token. */
7490 cp_lexer_consume_token (parser->lexer);
7491 /* A constructor declarator cannot appear in a typedef. */
7492 constructor_possible_p = false;
7493 /* The "typedef" keyword can only occur in a declaration; we
7494 may as well commit at this point. */
7495 cp_parser_commit_to_tentative_parse (parser);
7496
7497 if (decl_specs->storage_class != sc_none)
7498 decl_specs->conflicting_specifiers_p = true;
7499 break;
7500
7501 /* storage-class-specifier:
7502 auto
7503 register
7504 static
7505 extern
7506 mutable
7507
7508 GNU Extension:
7509 thread */
7510 case RID_AUTO:
7511 case RID_REGISTER:
7512 case RID_STATIC:
7513 case RID_EXTERN:
7514 case RID_MUTABLE:
7515 /* Consume the token. */
7516 cp_lexer_consume_token (parser->lexer);
7517 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7518 break;
7519 case RID_THREAD:
7520 /* Consume the token. */
7521 cp_lexer_consume_token (parser->lexer);
7522 ++decl_specs->specs[(int) ds_thread];
7523 break;
7524
7525 default:
7526 /* We did not yet find a decl-specifier yet. */
7527 found_decl_spec = false;
7528 break;
7529 }
7530
7531 /* Constructors are a special case. The `S' in `S()' is not a
7532 decl-specifier; it is the beginning of the declarator. */
7533 constructor_p
7534 = (!found_decl_spec
7535 && constructor_possible_p
7536 && (cp_parser_constructor_declarator_p
7537 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7538
7539 /* If we don't have a DECL_SPEC yet, then we must be looking at
7540 a type-specifier. */
7541 if (!found_decl_spec && !constructor_p)
7542 {
7543 int decl_spec_declares_class_or_enum;
7544 bool is_cv_qualifier;
7545 tree type_spec;
7546
7547 type_spec
7548 = cp_parser_type_specifier (parser, flags,
7549 decl_specs,
7550 /*is_declaration=*/true,
7551 &decl_spec_declares_class_or_enum,
7552 &is_cv_qualifier);
7553
7554 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7555
7556 /* If this type-specifier referenced a user-defined type
7557 (a typedef, class-name, etc.), then we can't allow any
7558 more such type-specifiers henceforth.
7559
7560 [dcl.spec]
7561
7562 The longest sequence of decl-specifiers that could
7563 possibly be a type name is taken as the
7564 decl-specifier-seq of a declaration. The sequence shall
7565 be self-consistent as described below.
7566
7567 [dcl.type]
7568
7569 As a general rule, at most one type-specifier is allowed
7570 in the complete decl-specifier-seq of a declaration. The
7571 only exceptions are the following:
7572
7573 -- const or volatile can be combined with any other
7574 type-specifier.
7575
7576 -- signed or unsigned can be combined with char, long,
7577 short, or int.
7578
7579 -- ..
7580
7581 Example:
7582
7583 typedef char* Pc;
7584 void g (const int Pc);
7585
7586 Here, Pc is *not* part of the decl-specifier seq; it's
7587 the declarator. Therefore, once we see a type-specifier
7588 (other than a cv-qualifier), we forbid any additional
7589 user-defined types. We *do* still allow things like `int
7590 int' to be considered a decl-specifier-seq, and issue the
7591 error message later. */
7592 if (type_spec && !is_cv_qualifier)
7593 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7594 /* A constructor declarator cannot follow a type-specifier. */
7595 if (type_spec)
7596 {
7597 constructor_possible_p = false;
7598 found_decl_spec = true;
7599 }
7600 }
7601
7602 /* If we still do not have a DECL_SPEC, then there are no more
7603 decl-specifiers. */
7604 if (!found_decl_spec)
7605 break;
7606
7607 decl_specs->any_specifiers_p = true;
7608 /* After we see one decl-specifier, further decl-specifiers are
7609 always optional. */
7610 flags |= CP_PARSER_FLAGS_OPTIONAL;
7611 }
7612
7613 cp_parser_check_decl_spec (decl_specs);
7614
7615 /* Don't allow a friend specifier with a class definition. */
7616 if (decl_specs->specs[(int) ds_friend] != 0
7617 && (*declares_class_or_enum & 2))
7618 error ("class definition may not be declared a friend");
7619 }
7620
7621 /* Parse an (optional) storage-class-specifier.
7622
7623 storage-class-specifier:
7624 auto
7625 register
7626 static
7627 extern
7628 mutable
7629
7630 GNU Extension:
7631
7632 storage-class-specifier:
7633 thread
7634
7635 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7636
7637 static tree
7638 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7639 {
7640 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7641 {
7642 case RID_AUTO:
7643 case RID_REGISTER:
7644 case RID_STATIC:
7645 case RID_EXTERN:
7646 case RID_MUTABLE:
7647 case RID_THREAD:
7648 /* Consume the token. */
7649 return cp_lexer_consume_token (parser->lexer)->value;
7650
7651 default:
7652 return NULL_TREE;
7653 }
7654 }
7655
7656 /* Parse an (optional) function-specifier.
7657
7658 function-specifier:
7659 inline
7660 virtual
7661 explicit
7662
7663 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7664 Updates DECL_SPECS, if it is non-NULL. */
7665
7666 static tree
7667 cp_parser_function_specifier_opt (cp_parser* parser,
7668 cp_decl_specifier_seq *decl_specs)
7669 {
7670 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7671 {
7672 case RID_INLINE:
7673 if (decl_specs)
7674 ++decl_specs->specs[(int) ds_inline];
7675 break;
7676
7677 case RID_VIRTUAL:
7678 /* 14.5.2.3 [temp.mem]
7679
7680 A member function template shall not be virtual. */
7681 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7682 error ("templates may not be %<virtual%>");
7683 else if (decl_specs)
7684 ++decl_specs->specs[(int) ds_virtual];
7685 break;
7686
7687 case RID_EXPLICIT:
7688 if (decl_specs)
7689 ++decl_specs->specs[(int) ds_explicit];
7690 break;
7691
7692 default:
7693 return NULL_TREE;
7694 }
7695
7696 /* Consume the token. */
7697 return cp_lexer_consume_token (parser->lexer)->value;
7698 }
7699
7700 /* Parse a linkage-specification.
7701
7702 linkage-specification:
7703 extern string-literal { declaration-seq [opt] }
7704 extern string-literal declaration */
7705
7706 static void
7707 cp_parser_linkage_specification (cp_parser* parser)
7708 {
7709 tree linkage;
7710
7711 /* Look for the `extern' keyword. */
7712 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7713
7714 /* Look for the string-literal. */
7715 linkage = cp_parser_string_literal (parser, false, false);
7716
7717 /* Transform the literal into an identifier. If the literal is a
7718 wide-character string, or contains embedded NULs, then we can't
7719 handle it as the user wants. */
7720 if (strlen (TREE_STRING_POINTER (linkage))
7721 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7722 {
7723 cp_parser_error (parser, "invalid linkage-specification");
7724 /* Assume C++ linkage. */
7725 linkage = lang_name_cplusplus;
7726 }
7727 else
7728 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7729
7730 /* We're now using the new linkage. */
7731 push_lang_context (linkage);
7732
7733 /* If the next token is a `{', then we're using the first
7734 production. */
7735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7736 {
7737 /* Consume the `{' token. */
7738 cp_lexer_consume_token (parser->lexer);
7739 /* Parse the declarations. */
7740 cp_parser_declaration_seq_opt (parser);
7741 /* Look for the closing `}'. */
7742 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7743 }
7744 /* Otherwise, there's just one declaration. */
7745 else
7746 {
7747 bool saved_in_unbraced_linkage_specification_p;
7748
7749 saved_in_unbraced_linkage_specification_p
7750 = parser->in_unbraced_linkage_specification_p;
7751 parser->in_unbraced_linkage_specification_p = true;
7752 cp_parser_declaration (parser);
7753 parser->in_unbraced_linkage_specification_p
7754 = saved_in_unbraced_linkage_specification_p;
7755 }
7756
7757 /* We're done with the linkage-specification. */
7758 pop_lang_context ();
7759 }
7760
7761 /* Special member functions [gram.special] */
7762
7763 /* Parse a conversion-function-id.
7764
7765 conversion-function-id:
7766 operator conversion-type-id
7767
7768 Returns an IDENTIFIER_NODE representing the operator. */
7769
7770 static tree
7771 cp_parser_conversion_function_id (cp_parser* parser)
7772 {
7773 tree type;
7774 tree saved_scope;
7775 tree saved_qualifying_scope;
7776 tree saved_object_scope;
7777 tree pushed_scope = NULL_TREE;
7778
7779 /* Look for the `operator' token. */
7780 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7781 return error_mark_node;
7782 /* When we parse the conversion-type-id, the current scope will be
7783 reset. However, we need that information in able to look up the
7784 conversion function later, so we save it here. */
7785 saved_scope = parser->scope;
7786 saved_qualifying_scope = parser->qualifying_scope;
7787 saved_object_scope = parser->object_scope;
7788 /* We must enter the scope of the class so that the names of
7789 entities declared within the class are available in the
7790 conversion-type-id. For example, consider:
7791
7792 struct S {
7793 typedef int I;
7794 operator I();
7795 };
7796
7797 S::operator I() { ... }
7798
7799 In order to see that `I' is a type-name in the definition, we
7800 must be in the scope of `S'. */
7801 if (saved_scope)
7802 pushed_scope = push_scope (saved_scope);
7803 /* Parse the conversion-type-id. */
7804 type = cp_parser_conversion_type_id (parser);
7805 /* Leave the scope of the class, if any. */
7806 if (pushed_scope)
7807 pop_scope (pushed_scope);
7808 /* Restore the saved scope. */
7809 parser->scope = saved_scope;
7810 parser->qualifying_scope = saved_qualifying_scope;
7811 parser->object_scope = saved_object_scope;
7812 /* If the TYPE is invalid, indicate failure. */
7813 if (type == error_mark_node)
7814 return error_mark_node;
7815 return mangle_conv_op_name_for_type (type);
7816 }
7817
7818 /* Parse a conversion-type-id:
7819
7820 conversion-type-id:
7821 type-specifier-seq conversion-declarator [opt]
7822
7823 Returns the TYPE specified. */
7824
7825 static tree
7826 cp_parser_conversion_type_id (cp_parser* parser)
7827 {
7828 tree attributes;
7829 cp_decl_specifier_seq type_specifiers;
7830 cp_declarator *declarator;
7831 tree type_specified;
7832
7833 /* Parse the attributes. */
7834 attributes = cp_parser_attributes_opt (parser);
7835 /* Parse the type-specifiers. */
7836 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7837 &type_specifiers);
7838 /* If that didn't work, stop. */
7839 if (type_specifiers.type == error_mark_node)
7840 return error_mark_node;
7841 /* Parse the conversion-declarator. */
7842 declarator = cp_parser_conversion_declarator_opt (parser);
7843
7844 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7845 /*initialized=*/0, &attributes);
7846 if (attributes)
7847 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7848 return type_specified;
7849 }
7850
7851 /* Parse an (optional) conversion-declarator.
7852
7853 conversion-declarator:
7854 ptr-operator conversion-declarator [opt]
7855
7856 */
7857
7858 static cp_declarator *
7859 cp_parser_conversion_declarator_opt (cp_parser* parser)
7860 {
7861 enum tree_code code;
7862 tree class_type;
7863 cp_cv_quals cv_quals;
7864
7865 /* We don't know if there's a ptr-operator next, or not. */
7866 cp_parser_parse_tentatively (parser);
7867 /* Try the ptr-operator. */
7868 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7869 /* If it worked, look for more conversion-declarators. */
7870 if (cp_parser_parse_definitely (parser))
7871 {
7872 cp_declarator *declarator;
7873
7874 /* Parse another optional declarator. */
7875 declarator = cp_parser_conversion_declarator_opt (parser);
7876
7877 /* Create the representation of the declarator. */
7878 if (class_type)
7879 declarator = make_ptrmem_declarator (cv_quals, class_type,
7880 declarator);
7881 else if (code == INDIRECT_REF)
7882 declarator = make_pointer_declarator (cv_quals, declarator);
7883 else
7884 declarator = make_reference_declarator (cv_quals, declarator);
7885
7886 return declarator;
7887 }
7888
7889 return NULL;
7890 }
7891
7892 /* Parse an (optional) ctor-initializer.
7893
7894 ctor-initializer:
7895 : mem-initializer-list
7896
7897 Returns TRUE iff the ctor-initializer was actually present. */
7898
7899 static bool
7900 cp_parser_ctor_initializer_opt (cp_parser* parser)
7901 {
7902 /* If the next token is not a `:', then there is no
7903 ctor-initializer. */
7904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7905 {
7906 /* Do default initialization of any bases and members. */
7907 if (DECL_CONSTRUCTOR_P (current_function_decl))
7908 finish_mem_initializers (NULL_TREE);
7909
7910 return false;
7911 }
7912
7913 /* Consume the `:' token. */
7914 cp_lexer_consume_token (parser->lexer);
7915 /* And the mem-initializer-list. */
7916 cp_parser_mem_initializer_list (parser);
7917
7918 return true;
7919 }
7920
7921 /* Parse a mem-initializer-list.
7922
7923 mem-initializer-list:
7924 mem-initializer
7925 mem-initializer , mem-initializer-list */
7926
7927 static void
7928 cp_parser_mem_initializer_list (cp_parser* parser)
7929 {
7930 tree mem_initializer_list = NULL_TREE;
7931
7932 /* Let the semantic analysis code know that we are starting the
7933 mem-initializer-list. */
7934 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7935 error ("only constructors take base initializers");
7936
7937 /* Loop through the list. */
7938 while (true)
7939 {
7940 tree mem_initializer;
7941
7942 /* Parse the mem-initializer. */
7943 mem_initializer = cp_parser_mem_initializer (parser);
7944 /* Add it to the list, unless it was erroneous. */
7945 if (mem_initializer != error_mark_node)
7946 {
7947 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7948 mem_initializer_list = mem_initializer;
7949 }
7950 /* If the next token is not a `,', we're done. */
7951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7952 break;
7953 /* Consume the `,' token. */
7954 cp_lexer_consume_token (parser->lexer);
7955 }
7956
7957 /* Perform semantic analysis. */
7958 if (DECL_CONSTRUCTOR_P (current_function_decl))
7959 finish_mem_initializers (mem_initializer_list);
7960 }
7961
7962 /* Parse a mem-initializer.
7963
7964 mem-initializer:
7965 mem-initializer-id ( expression-list [opt] )
7966
7967 GNU extension:
7968
7969 mem-initializer:
7970 ( expression-list [opt] )
7971
7972 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7973 class) or FIELD_DECL (for a non-static data member) to initialize;
7974 the TREE_VALUE is the expression-list. An empty initialization
7975 list is represented by void_list_node. */
7976
7977 static tree
7978 cp_parser_mem_initializer (cp_parser* parser)
7979 {
7980 tree mem_initializer_id;
7981 tree expression_list;
7982 tree member;
7983
7984 /* Find out what is being initialized. */
7985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7986 {
7987 pedwarn ("anachronistic old-style base class initializer");
7988 mem_initializer_id = NULL_TREE;
7989 }
7990 else
7991 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7992 member = expand_member_init (mem_initializer_id);
7993 if (member && !DECL_P (member))
7994 in_base_initializer = 1;
7995
7996 expression_list
7997 = cp_parser_parenthesized_expression_list (parser, false,
7998 /*cast_p=*/false,
7999 /*non_constant_p=*/NULL);
8000 if (expression_list == error_mark_node)
8001 return error_mark_node;
8002 if (!expression_list)
8003 expression_list = void_type_node;
8004
8005 in_base_initializer = 0;
8006
8007 return member ? build_tree_list (member, expression_list) : error_mark_node;
8008 }
8009
8010 /* Parse a mem-initializer-id.
8011
8012 mem-initializer-id:
8013 :: [opt] nested-name-specifier [opt] class-name
8014 identifier
8015
8016 Returns a TYPE indicating the class to be initializer for the first
8017 production. Returns an IDENTIFIER_NODE indicating the data member
8018 to be initialized for the second production. */
8019
8020 static tree
8021 cp_parser_mem_initializer_id (cp_parser* parser)
8022 {
8023 bool global_scope_p;
8024 bool nested_name_specifier_p;
8025 bool template_p = false;
8026 tree id;
8027
8028 /* `typename' is not allowed in this context ([temp.res]). */
8029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8030 {
8031 error ("keyword %<typename%> not allowed in this context (a qualified "
8032 "member initializer is implicitly a type)");
8033 cp_lexer_consume_token (parser->lexer);
8034 }
8035 /* Look for the optional `::' operator. */
8036 global_scope_p
8037 = (cp_parser_global_scope_opt (parser,
8038 /*current_scope_valid_p=*/false)
8039 != NULL_TREE);
8040 /* Look for the optional nested-name-specifier. The simplest way to
8041 implement:
8042
8043 [temp.res]
8044
8045 The keyword `typename' is not permitted in a base-specifier or
8046 mem-initializer; in these contexts a qualified name that
8047 depends on a template-parameter is implicitly assumed to be a
8048 type name.
8049
8050 is to assume that we have seen the `typename' keyword at this
8051 point. */
8052 nested_name_specifier_p
8053 = (cp_parser_nested_name_specifier_opt (parser,
8054 /*typename_keyword_p=*/true,
8055 /*check_dependency_p=*/true,
8056 /*type_p=*/true,
8057 /*is_declaration=*/true)
8058 != NULL_TREE);
8059 if (nested_name_specifier_p)
8060 template_p = cp_parser_optional_template_keyword (parser);
8061 /* If there is a `::' operator or a nested-name-specifier, then we
8062 are definitely looking for a class-name. */
8063 if (global_scope_p || nested_name_specifier_p)
8064 return cp_parser_class_name (parser,
8065 /*typename_keyword_p=*/true,
8066 /*template_keyword_p=*/template_p,
8067 none_type,
8068 /*check_dependency_p=*/true,
8069 /*class_head_p=*/false,
8070 /*is_declaration=*/true);
8071 /* Otherwise, we could also be looking for an ordinary identifier. */
8072 cp_parser_parse_tentatively (parser);
8073 /* Try a class-name. */
8074 id = cp_parser_class_name (parser,
8075 /*typename_keyword_p=*/true,
8076 /*template_keyword_p=*/false,
8077 none_type,
8078 /*check_dependency_p=*/true,
8079 /*class_head_p=*/false,
8080 /*is_declaration=*/true);
8081 /* If we found one, we're done. */
8082 if (cp_parser_parse_definitely (parser))
8083 return id;
8084 /* Otherwise, look for an ordinary identifier. */
8085 return cp_parser_identifier (parser);
8086 }
8087
8088 /* Overloading [gram.over] */
8089
8090 /* Parse an operator-function-id.
8091
8092 operator-function-id:
8093 operator operator
8094
8095 Returns an IDENTIFIER_NODE for the operator which is a
8096 human-readable spelling of the identifier, e.g., `operator +'. */
8097
8098 static tree
8099 cp_parser_operator_function_id (cp_parser* parser)
8100 {
8101 /* Look for the `operator' keyword. */
8102 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8103 return error_mark_node;
8104 /* And then the name of the operator itself. */
8105 return cp_parser_operator (parser);
8106 }
8107
8108 /* Parse an operator.
8109
8110 operator:
8111 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8112 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8113 || ++ -- , ->* -> () []
8114
8115 GNU Extensions:
8116
8117 operator:
8118 <? >? <?= >?=
8119
8120 Returns an IDENTIFIER_NODE for the operator which is a
8121 human-readable spelling of the identifier, e.g., `operator +'. */
8122
8123 static tree
8124 cp_parser_operator (cp_parser* parser)
8125 {
8126 tree id = NULL_TREE;
8127 cp_token *token;
8128
8129 /* Peek at the next token. */
8130 token = cp_lexer_peek_token (parser->lexer);
8131 /* Figure out which operator we have. */
8132 switch (token->type)
8133 {
8134 case CPP_KEYWORD:
8135 {
8136 enum tree_code op;
8137
8138 /* The keyword should be either `new' or `delete'. */
8139 if (token->keyword == RID_NEW)
8140 op = NEW_EXPR;
8141 else if (token->keyword == RID_DELETE)
8142 op = DELETE_EXPR;
8143 else
8144 break;
8145
8146 /* Consume the `new' or `delete' token. */
8147 cp_lexer_consume_token (parser->lexer);
8148
8149 /* Peek at the next token. */
8150 token = cp_lexer_peek_token (parser->lexer);
8151 /* If it's a `[' token then this is the array variant of the
8152 operator. */
8153 if (token->type == CPP_OPEN_SQUARE)
8154 {
8155 /* Consume the `[' token. */
8156 cp_lexer_consume_token (parser->lexer);
8157 /* Look for the `]' token. */
8158 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8159 id = ansi_opname (op == NEW_EXPR
8160 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8161 }
8162 /* Otherwise, we have the non-array variant. */
8163 else
8164 id = ansi_opname (op);
8165
8166 return id;
8167 }
8168
8169 case CPP_PLUS:
8170 id = ansi_opname (PLUS_EXPR);
8171 break;
8172
8173 case CPP_MINUS:
8174 id = ansi_opname (MINUS_EXPR);
8175 break;
8176
8177 case CPP_MULT:
8178 id = ansi_opname (MULT_EXPR);
8179 break;
8180
8181 case CPP_DIV:
8182 id = ansi_opname (TRUNC_DIV_EXPR);
8183 break;
8184
8185 case CPP_MOD:
8186 id = ansi_opname (TRUNC_MOD_EXPR);
8187 break;
8188
8189 case CPP_XOR:
8190 id = ansi_opname (BIT_XOR_EXPR);
8191 break;
8192
8193 case CPP_AND:
8194 id = ansi_opname (BIT_AND_EXPR);
8195 break;
8196
8197 case CPP_OR:
8198 id = ansi_opname (BIT_IOR_EXPR);
8199 break;
8200
8201 case CPP_COMPL:
8202 id = ansi_opname (BIT_NOT_EXPR);
8203 break;
8204
8205 case CPP_NOT:
8206 id = ansi_opname (TRUTH_NOT_EXPR);
8207 break;
8208
8209 case CPP_EQ:
8210 id = ansi_assopname (NOP_EXPR);
8211 break;
8212
8213 case CPP_LESS:
8214 id = ansi_opname (LT_EXPR);
8215 break;
8216
8217 case CPP_GREATER:
8218 id = ansi_opname (GT_EXPR);
8219 break;
8220
8221 case CPP_PLUS_EQ:
8222 id = ansi_assopname (PLUS_EXPR);
8223 break;
8224
8225 case CPP_MINUS_EQ:
8226 id = ansi_assopname (MINUS_EXPR);
8227 break;
8228
8229 case CPP_MULT_EQ:
8230 id = ansi_assopname (MULT_EXPR);
8231 break;
8232
8233 case CPP_DIV_EQ:
8234 id = ansi_assopname (TRUNC_DIV_EXPR);
8235 break;
8236
8237 case CPP_MOD_EQ:
8238 id = ansi_assopname (TRUNC_MOD_EXPR);
8239 break;
8240
8241 case CPP_XOR_EQ:
8242 id = ansi_assopname (BIT_XOR_EXPR);
8243 break;
8244
8245 case CPP_AND_EQ:
8246 id = ansi_assopname (BIT_AND_EXPR);
8247 break;
8248
8249 case CPP_OR_EQ:
8250 id = ansi_assopname (BIT_IOR_EXPR);
8251 break;
8252
8253 case CPP_LSHIFT:
8254 id = ansi_opname (LSHIFT_EXPR);
8255 break;
8256
8257 case CPP_RSHIFT:
8258 id = ansi_opname (RSHIFT_EXPR);
8259 break;
8260
8261 case CPP_LSHIFT_EQ:
8262 id = ansi_assopname (LSHIFT_EXPR);
8263 break;
8264
8265 case CPP_RSHIFT_EQ:
8266 id = ansi_assopname (RSHIFT_EXPR);
8267 break;
8268
8269 case CPP_EQ_EQ:
8270 id = ansi_opname (EQ_EXPR);
8271 break;
8272
8273 case CPP_NOT_EQ:
8274 id = ansi_opname (NE_EXPR);
8275 break;
8276
8277 case CPP_LESS_EQ:
8278 id = ansi_opname (LE_EXPR);
8279 break;
8280
8281 case CPP_GREATER_EQ:
8282 id = ansi_opname (GE_EXPR);
8283 break;
8284
8285 case CPP_AND_AND:
8286 id = ansi_opname (TRUTH_ANDIF_EXPR);
8287 break;
8288
8289 case CPP_OR_OR:
8290 id = ansi_opname (TRUTH_ORIF_EXPR);
8291 break;
8292
8293 case CPP_PLUS_PLUS:
8294 id = ansi_opname (POSTINCREMENT_EXPR);
8295 break;
8296
8297 case CPP_MINUS_MINUS:
8298 id = ansi_opname (PREDECREMENT_EXPR);
8299 break;
8300
8301 case CPP_COMMA:
8302 id = ansi_opname (COMPOUND_EXPR);
8303 break;
8304
8305 case CPP_DEREF_STAR:
8306 id = ansi_opname (MEMBER_REF);
8307 break;
8308
8309 case CPP_DEREF:
8310 id = ansi_opname (COMPONENT_REF);
8311 break;
8312
8313 case CPP_OPEN_PAREN:
8314 /* Consume the `('. */
8315 cp_lexer_consume_token (parser->lexer);
8316 /* Look for the matching `)'. */
8317 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8318 return ansi_opname (CALL_EXPR);
8319
8320 case CPP_OPEN_SQUARE:
8321 /* Consume the `['. */
8322 cp_lexer_consume_token (parser->lexer);
8323 /* Look for the matching `]'. */
8324 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8325 return ansi_opname (ARRAY_REF);
8326
8327 default:
8328 /* Anything else is an error. */
8329 break;
8330 }
8331
8332 /* If we have selected an identifier, we need to consume the
8333 operator token. */
8334 if (id)
8335 cp_lexer_consume_token (parser->lexer);
8336 /* Otherwise, no valid operator name was present. */
8337 else
8338 {
8339 cp_parser_error (parser, "expected operator");
8340 id = error_mark_node;
8341 }
8342
8343 return id;
8344 }
8345
8346 /* Parse a template-declaration.
8347
8348 template-declaration:
8349 export [opt] template < template-parameter-list > declaration
8350
8351 If MEMBER_P is TRUE, this template-declaration occurs within a
8352 class-specifier.
8353
8354 The grammar rule given by the standard isn't correct. What
8355 is really meant is:
8356
8357 template-declaration:
8358 export [opt] template-parameter-list-seq
8359 decl-specifier-seq [opt] init-declarator [opt] ;
8360 export [opt] template-parameter-list-seq
8361 function-definition
8362
8363 template-parameter-list-seq:
8364 template-parameter-list-seq [opt]
8365 template < template-parameter-list > */
8366
8367 static void
8368 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8369 {
8370 /* Check for `export'. */
8371 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8372 {
8373 /* Consume the `export' token. */
8374 cp_lexer_consume_token (parser->lexer);
8375 /* Warn that we do not support `export'. */
8376 warning (0, "keyword %<export%> not implemented, and will be ignored");
8377 }
8378
8379 cp_parser_template_declaration_after_export (parser, member_p);
8380 }
8381
8382 /* Parse a template-parameter-list.
8383
8384 template-parameter-list:
8385 template-parameter
8386 template-parameter-list , template-parameter
8387
8388 Returns a TREE_LIST. Each node represents a template parameter.
8389 The nodes are connected via their TREE_CHAINs. */
8390
8391 static tree
8392 cp_parser_template_parameter_list (cp_parser* parser)
8393 {
8394 tree parameter_list = NULL_TREE;
8395
8396 begin_template_parm_list ();
8397 while (true)
8398 {
8399 tree parameter;
8400 cp_token *token;
8401 bool is_non_type;
8402
8403 /* Parse the template-parameter. */
8404 parameter = cp_parser_template_parameter (parser, &is_non_type);
8405 /* Add it to the list. */
8406 if (parameter != error_mark_node)
8407 parameter_list = process_template_parm (parameter_list,
8408 parameter,
8409 is_non_type);
8410 else
8411 {
8412 tree err_parm = build_tree_list (parameter, parameter);
8413 TREE_VALUE (err_parm) = error_mark_node;
8414 parameter_list = chainon (parameter_list, err_parm);
8415 }
8416
8417 /* Peek at the next token. */
8418 token = cp_lexer_peek_token (parser->lexer);
8419 /* If it's not a `,', we're done. */
8420 if (token->type != CPP_COMMA)
8421 break;
8422 /* Otherwise, consume the `,' token. */
8423 cp_lexer_consume_token (parser->lexer);
8424 }
8425
8426 return end_template_parm_list (parameter_list);
8427 }
8428
8429 /* Parse a template-parameter.
8430
8431 template-parameter:
8432 type-parameter
8433 parameter-declaration
8434
8435 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8436 the parameter. The TREE_PURPOSE is the default value, if any.
8437 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8438 iff this parameter is a non-type parameter. */
8439
8440 static tree
8441 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8442 {
8443 cp_token *token;
8444 cp_parameter_declarator *parameter_declarator;
8445 tree parm;
8446
8447 /* Assume it is a type parameter or a template parameter. */
8448 *is_non_type = false;
8449 /* Peek at the next token. */
8450 token = cp_lexer_peek_token (parser->lexer);
8451 /* If it is `class' or `template', we have a type-parameter. */
8452 if (token->keyword == RID_TEMPLATE)
8453 return cp_parser_type_parameter (parser);
8454 /* If it is `class' or `typename' we do not know yet whether it is a
8455 type parameter or a non-type parameter. Consider:
8456
8457 template <typename T, typename T::X X> ...
8458
8459 or:
8460
8461 template <class C, class D*> ...
8462
8463 Here, the first parameter is a type parameter, and the second is
8464 a non-type parameter. We can tell by looking at the token after
8465 the identifier -- if it is a `,', `=', or `>' then we have a type
8466 parameter. */
8467 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8468 {
8469 /* Peek at the token after `class' or `typename'. */
8470 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8471 /* If it's an identifier, skip it. */
8472 if (token->type == CPP_NAME)
8473 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8474 /* Now, see if the token looks like the end of a template
8475 parameter. */
8476 if (token->type == CPP_COMMA
8477 || token->type == CPP_EQ
8478 || token->type == CPP_GREATER)
8479 return cp_parser_type_parameter (parser);
8480 }
8481
8482 /* Otherwise, it is a non-type parameter.
8483
8484 [temp.param]
8485
8486 When parsing a default template-argument for a non-type
8487 template-parameter, the first non-nested `>' is taken as the end
8488 of the template parameter-list rather than a greater-than
8489 operator. */
8490 *is_non_type = true;
8491 parameter_declarator
8492 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8493 /*parenthesized_p=*/NULL);
8494 parm = grokdeclarator (parameter_declarator->declarator,
8495 &parameter_declarator->decl_specifiers,
8496 PARM, /*initialized=*/0,
8497 /*attrlist=*/NULL);
8498 if (parm == error_mark_node)
8499 return error_mark_node;
8500 return build_tree_list (parameter_declarator->default_argument, parm);
8501 }
8502
8503 /* Parse a type-parameter.
8504
8505 type-parameter:
8506 class identifier [opt]
8507 class identifier [opt] = type-id
8508 typename identifier [opt]
8509 typename identifier [opt] = type-id
8510 template < template-parameter-list > class identifier [opt]
8511 template < template-parameter-list > class identifier [opt]
8512 = id-expression
8513
8514 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8515 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8516 the declaration of the parameter. */
8517
8518 static tree
8519 cp_parser_type_parameter (cp_parser* parser)
8520 {
8521 cp_token *token;
8522 tree parameter;
8523
8524 /* Look for a keyword to tell us what kind of parameter this is. */
8525 token = cp_parser_require (parser, CPP_KEYWORD,
8526 "`class', `typename', or `template'");
8527 if (!token)
8528 return error_mark_node;
8529
8530 switch (token->keyword)
8531 {
8532 case RID_CLASS:
8533 case RID_TYPENAME:
8534 {
8535 tree identifier;
8536 tree default_argument;
8537
8538 /* If the next token is an identifier, then it names the
8539 parameter. */
8540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8541 identifier = cp_parser_identifier (parser);
8542 else
8543 identifier = NULL_TREE;
8544
8545 /* Create the parameter. */
8546 parameter = finish_template_type_parm (class_type_node, identifier);
8547
8548 /* If the next token is an `=', we have a default argument. */
8549 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8550 {
8551 /* Consume the `=' token. */
8552 cp_lexer_consume_token (parser->lexer);
8553 /* Parse the default-argument. */
8554 push_deferring_access_checks (dk_no_deferred);
8555 default_argument = cp_parser_type_id (parser);
8556 pop_deferring_access_checks ();
8557 }
8558 else
8559 default_argument = NULL_TREE;
8560
8561 /* Create the combined representation of the parameter and the
8562 default argument. */
8563 parameter = build_tree_list (default_argument, parameter);
8564 }
8565 break;
8566
8567 case RID_TEMPLATE:
8568 {
8569 tree parameter_list;
8570 tree identifier;
8571 tree default_argument;
8572
8573 /* Look for the `<'. */
8574 cp_parser_require (parser, CPP_LESS, "`<'");
8575 /* Parse the template-parameter-list. */
8576 parameter_list = cp_parser_template_parameter_list (parser);
8577 /* Look for the `>'. */
8578 cp_parser_require (parser, CPP_GREATER, "`>'");
8579 /* Look for the `class' keyword. */
8580 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8581 /* If the next token is an `=', then there is a
8582 default-argument. If the next token is a `>', we are at
8583 the end of the parameter-list. If the next token is a `,',
8584 then we are at the end of this parameter. */
8585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8586 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8587 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8588 {
8589 identifier = cp_parser_identifier (parser);
8590 /* Treat invalid names as if the parameter were nameless. */
8591 if (identifier == error_mark_node)
8592 identifier = NULL_TREE;
8593 }
8594 else
8595 identifier = NULL_TREE;
8596
8597 /* Create the template parameter. */
8598 parameter = finish_template_template_parm (class_type_node,
8599 identifier);
8600
8601 /* If the next token is an `=', then there is a
8602 default-argument. */
8603 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8604 {
8605 bool is_template;
8606
8607 /* Consume the `='. */
8608 cp_lexer_consume_token (parser->lexer);
8609 /* Parse the id-expression. */
8610 push_deferring_access_checks (dk_no_deferred);
8611 default_argument
8612 = cp_parser_id_expression (parser,
8613 /*template_keyword_p=*/false,
8614 /*check_dependency_p=*/true,
8615 /*template_p=*/&is_template,
8616 /*declarator_p=*/false,
8617 /*optional_p=*/false);
8618 if (TREE_CODE (default_argument) == TYPE_DECL)
8619 /* If the id-expression was a template-id that refers to
8620 a template-class, we already have the declaration here,
8621 so no further lookup is needed. */
8622 ;
8623 else
8624 /* Look up the name. */
8625 default_argument
8626 = cp_parser_lookup_name (parser, default_argument,
8627 none_type,
8628 /*is_template=*/is_template,
8629 /*is_namespace=*/false,
8630 /*check_dependency=*/true,
8631 /*ambiguous_decls=*/NULL);
8632 /* See if the default argument is valid. */
8633 default_argument
8634 = check_template_template_default_arg (default_argument);
8635 pop_deferring_access_checks ();
8636 }
8637 else
8638 default_argument = NULL_TREE;
8639
8640 /* Create the combined representation of the parameter and the
8641 default argument. */
8642 parameter = build_tree_list (default_argument, parameter);
8643 }
8644 break;
8645
8646 default:
8647 gcc_unreachable ();
8648 break;
8649 }
8650
8651 return parameter;
8652 }
8653
8654 /* Parse a template-id.
8655
8656 template-id:
8657 template-name < template-argument-list [opt] >
8658
8659 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8660 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8661 returned. Otherwise, if the template-name names a function, or set
8662 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8663 names a class, returns a TYPE_DECL for the specialization.
8664
8665 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8666 uninstantiated templates. */
8667
8668 static tree
8669 cp_parser_template_id (cp_parser *parser,
8670 bool template_keyword_p,
8671 bool check_dependency_p,
8672 bool is_declaration)
8673 {
8674 tree template;
8675 tree arguments;
8676 tree template_id;
8677 cp_token_position start_of_id = 0;
8678 tree access_check = NULL_TREE;
8679 cp_token *next_token, *next_token_2;
8680 bool is_identifier;
8681
8682 /* If the next token corresponds to a template-id, there is no need
8683 to reparse it. */
8684 next_token = cp_lexer_peek_token (parser->lexer);
8685 if (next_token->type == CPP_TEMPLATE_ID)
8686 {
8687 tree value;
8688 tree check;
8689
8690 /* Get the stored value. */
8691 value = cp_lexer_consume_token (parser->lexer)->value;
8692 /* Perform any access checks that were deferred. */
8693 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8694 perform_or_defer_access_check (TREE_PURPOSE (check),
8695 TREE_VALUE (check));
8696 /* Return the stored value. */
8697 return TREE_VALUE (value);
8698 }
8699
8700 /* Avoid performing name lookup if there is no possibility of
8701 finding a template-id. */
8702 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8703 || (next_token->type == CPP_NAME
8704 && !cp_parser_nth_token_starts_template_argument_list_p
8705 (parser, 2)))
8706 {
8707 cp_parser_error (parser, "expected template-id");
8708 return error_mark_node;
8709 }
8710
8711 /* Remember where the template-id starts. */
8712 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8713 start_of_id = cp_lexer_token_position (parser->lexer, false);
8714
8715 push_deferring_access_checks (dk_deferred);
8716
8717 /* Parse the template-name. */
8718 is_identifier = false;
8719 template = cp_parser_template_name (parser, template_keyword_p,
8720 check_dependency_p,
8721 is_declaration,
8722 &is_identifier);
8723 if (template == error_mark_node || is_identifier)
8724 {
8725 pop_deferring_access_checks ();
8726 return template;
8727 }
8728
8729 /* If we find the sequence `[:' after a template-name, it's probably
8730 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8731 parse correctly the argument list. */
8732 next_token = cp_lexer_peek_token (parser->lexer);
8733 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8734 if (next_token->type == CPP_OPEN_SQUARE
8735 && next_token->flags & DIGRAPH
8736 && next_token_2->type == CPP_COLON
8737 && !(next_token_2->flags & PREV_WHITE))
8738 {
8739 cp_parser_parse_tentatively (parser);
8740 /* Change `:' into `::'. */
8741 next_token_2->type = CPP_SCOPE;
8742 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8743 CPP_LESS. */
8744 cp_lexer_consume_token (parser->lexer);
8745 /* Parse the arguments. */
8746 arguments = cp_parser_enclosed_template_argument_list (parser);
8747 if (!cp_parser_parse_definitely (parser))
8748 {
8749 /* If we couldn't parse an argument list, then we revert our changes
8750 and return simply an error. Maybe this is not a template-id
8751 after all. */
8752 next_token_2->type = CPP_COLON;
8753 cp_parser_error (parser, "expected %<<%>");
8754 pop_deferring_access_checks ();
8755 return error_mark_node;
8756 }
8757 /* Otherwise, emit an error about the invalid digraph, but continue
8758 parsing because we got our argument list. */
8759 pedwarn ("%<<::%> cannot begin a template-argument list");
8760 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8761 "between %<<%> and %<::%>");
8762 if (!flag_permissive)
8763 {
8764 static bool hint;
8765 if (!hint)
8766 {
8767 inform ("(if you use -fpermissive G++ will accept your code)");
8768 hint = true;
8769 }
8770 }
8771 }
8772 else
8773 {
8774 /* Look for the `<' that starts the template-argument-list. */
8775 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8776 {
8777 pop_deferring_access_checks ();
8778 return error_mark_node;
8779 }
8780 /* Parse the arguments. */
8781 arguments = cp_parser_enclosed_template_argument_list (parser);
8782 }
8783
8784 /* Build a representation of the specialization. */
8785 if (TREE_CODE (template) == IDENTIFIER_NODE)
8786 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8787 else if (DECL_CLASS_TEMPLATE_P (template)
8788 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8789 {
8790 bool entering_scope;
8791 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8792 template (rather than some instantiation thereof) only if
8793 is not nested within some other construct. For example, in
8794 "template <typename T> void f(T) { A<T>::", A<T> is just an
8795 instantiation of A. */
8796 entering_scope = (template_parm_scope_p ()
8797 && cp_lexer_next_token_is (parser->lexer,
8798 CPP_SCOPE));
8799 template_id
8800 = finish_template_type (template, arguments, entering_scope);
8801 }
8802 else
8803 {
8804 /* If it's not a class-template or a template-template, it should be
8805 a function-template. */
8806 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8807 || TREE_CODE (template) == OVERLOAD
8808 || BASELINK_P (template)));
8809
8810 template_id = lookup_template_function (template, arguments);
8811 }
8812
8813 /* Retrieve any deferred checks. Do not pop this access checks yet
8814 so the memory will not be reclaimed during token replacing below. */
8815 access_check = get_deferred_access_checks ();
8816
8817 /* If parsing tentatively, replace the sequence of tokens that makes
8818 up the template-id with a CPP_TEMPLATE_ID token. That way,
8819 should we re-parse the token stream, we will not have to repeat
8820 the effort required to do the parse, nor will we issue duplicate
8821 error messages about problems during instantiation of the
8822 template. */
8823 if (start_of_id)
8824 {
8825 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8826
8827 /* Reset the contents of the START_OF_ID token. */
8828 token->type = CPP_TEMPLATE_ID;
8829 token->value = build_tree_list (access_check, template_id);
8830 token->keyword = RID_MAX;
8831
8832 /* Purge all subsequent tokens. */
8833 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8834
8835 /* ??? Can we actually assume that, if template_id ==
8836 error_mark_node, we will have issued a diagnostic to the
8837 user, as opposed to simply marking the tentative parse as
8838 failed? */
8839 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8840 error ("parse error in template argument list");
8841 }
8842
8843 pop_deferring_access_checks ();
8844 return template_id;
8845 }
8846
8847 /* Parse a template-name.
8848
8849 template-name:
8850 identifier
8851
8852 The standard should actually say:
8853
8854 template-name:
8855 identifier
8856 operator-function-id
8857
8858 A defect report has been filed about this issue.
8859
8860 A conversion-function-id cannot be a template name because they cannot
8861 be part of a template-id. In fact, looking at this code:
8862
8863 a.operator K<int>()
8864
8865 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8866 It is impossible to call a templated conversion-function-id with an
8867 explicit argument list, since the only allowed template parameter is
8868 the type to which it is converting.
8869
8870 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8871 `template' keyword, in a construction like:
8872
8873 T::template f<3>()
8874
8875 In that case `f' is taken to be a template-name, even though there
8876 is no way of knowing for sure.
8877
8878 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8879 name refers to a set of overloaded functions, at least one of which
8880 is a template, or an IDENTIFIER_NODE with the name of the template,
8881 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8882 names are looked up inside uninstantiated templates. */
8883
8884 static tree
8885 cp_parser_template_name (cp_parser* parser,
8886 bool template_keyword_p,
8887 bool check_dependency_p,
8888 bool is_declaration,
8889 bool *is_identifier)
8890 {
8891 tree identifier;
8892 tree decl;
8893 tree fns;
8894
8895 /* If the next token is `operator', then we have either an
8896 operator-function-id or a conversion-function-id. */
8897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8898 {
8899 /* We don't know whether we're looking at an
8900 operator-function-id or a conversion-function-id. */
8901 cp_parser_parse_tentatively (parser);
8902 /* Try an operator-function-id. */
8903 identifier = cp_parser_operator_function_id (parser);
8904 /* If that didn't work, try a conversion-function-id. */
8905 if (!cp_parser_parse_definitely (parser))
8906 {
8907 cp_parser_error (parser, "expected template-name");
8908 return error_mark_node;
8909 }
8910 }
8911 /* Look for the identifier. */
8912 else
8913 identifier = cp_parser_identifier (parser);
8914
8915 /* If we didn't find an identifier, we don't have a template-id. */
8916 if (identifier == error_mark_node)
8917 return error_mark_node;
8918
8919 /* If the name immediately followed the `template' keyword, then it
8920 is a template-name. However, if the next token is not `<', then
8921 we do not treat it as a template-name, since it is not being used
8922 as part of a template-id. This enables us to handle constructs
8923 like:
8924
8925 template <typename T> struct S { S(); };
8926 template <typename T> S<T>::S();
8927
8928 correctly. We would treat `S' as a template -- if it were `S<T>'
8929 -- but we do not if there is no `<'. */
8930
8931 if (processing_template_decl
8932 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8933 {
8934 /* In a declaration, in a dependent context, we pretend that the
8935 "template" keyword was present in order to improve error
8936 recovery. For example, given:
8937
8938 template <typename T> void f(T::X<int>);
8939
8940 we want to treat "X<int>" as a template-id. */
8941 if (is_declaration
8942 && !template_keyword_p
8943 && parser->scope && TYPE_P (parser->scope)
8944 && check_dependency_p
8945 && dependent_type_p (parser->scope)
8946 /* Do not do this for dtors (or ctors), since they never
8947 need the template keyword before their name. */
8948 && !constructor_name_p (identifier, parser->scope))
8949 {
8950 cp_token_position start = 0;
8951
8952 /* Explain what went wrong. */
8953 error ("non-template %qD used as template", identifier);
8954 inform ("use %<%T::template %D%> to indicate that it is a template",
8955 parser->scope, identifier);
8956 /* If parsing tentatively, find the location of the "<" token. */
8957 if (cp_parser_simulate_error (parser))
8958 start = cp_lexer_token_position (parser->lexer, true);
8959 /* Parse the template arguments so that we can issue error
8960 messages about them. */
8961 cp_lexer_consume_token (parser->lexer);
8962 cp_parser_enclosed_template_argument_list (parser);
8963 /* Skip tokens until we find a good place from which to
8964 continue parsing. */
8965 cp_parser_skip_to_closing_parenthesis (parser,
8966 /*recovering=*/true,
8967 /*or_comma=*/true,
8968 /*consume_paren=*/false);
8969 /* If parsing tentatively, permanently remove the
8970 template argument list. That will prevent duplicate
8971 error messages from being issued about the missing
8972 "template" keyword. */
8973 if (start)
8974 cp_lexer_purge_tokens_after (parser->lexer, start);
8975 if (is_identifier)
8976 *is_identifier = true;
8977 return identifier;
8978 }
8979
8980 /* If the "template" keyword is present, then there is generally
8981 no point in doing name-lookup, so we just return IDENTIFIER.
8982 But, if the qualifying scope is non-dependent then we can
8983 (and must) do name-lookup normally. */
8984 if (template_keyword_p
8985 && (!parser->scope
8986 || (TYPE_P (parser->scope)
8987 && dependent_type_p (parser->scope))))
8988 return identifier;
8989 }
8990
8991 /* Look up the name. */
8992 decl = cp_parser_lookup_name (parser, identifier,
8993 none_type,
8994 /*is_template=*/false,
8995 /*is_namespace=*/false,
8996 check_dependency_p,
8997 /*ambiguous_decls=*/NULL);
8998 decl = maybe_get_template_decl_from_type_decl (decl);
8999
9000 /* If DECL is a template, then the name was a template-name. */
9001 if (TREE_CODE (decl) == TEMPLATE_DECL)
9002 ;
9003 else
9004 {
9005 tree fn = NULL_TREE;
9006
9007 /* The standard does not explicitly indicate whether a name that
9008 names a set of overloaded declarations, some of which are
9009 templates, is a template-name. However, such a name should
9010 be a template-name; otherwise, there is no way to form a
9011 template-id for the overloaded templates. */
9012 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9013 if (TREE_CODE (fns) == OVERLOAD)
9014 for (fn = fns; fn; fn = OVL_NEXT (fn))
9015 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9016 break;
9017
9018 if (!fn)
9019 {
9020 /* The name does not name a template. */
9021 cp_parser_error (parser, "expected template-name");
9022 return error_mark_node;
9023 }
9024 }
9025
9026 /* If DECL is dependent, and refers to a function, then just return
9027 its name; we will look it up again during template instantiation. */
9028 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9029 {
9030 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9031 if (TYPE_P (scope) && dependent_type_p (scope))
9032 return identifier;
9033 }
9034
9035 return decl;
9036 }
9037
9038 /* Parse a template-argument-list.
9039
9040 template-argument-list:
9041 template-argument
9042 template-argument-list , template-argument
9043
9044 Returns a TREE_VEC containing the arguments. */
9045
9046 static tree
9047 cp_parser_template_argument_list (cp_parser* parser)
9048 {
9049 tree fixed_args[10];
9050 unsigned n_args = 0;
9051 unsigned alloced = 10;
9052 tree *arg_ary = fixed_args;
9053 tree vec;
9054 bool saved_in_template_argument_list_p;
9055 bool saved_ice_p;
9056 bool saved_non_ice_p;
9057
9058 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9059 parser->in_template_argument_list_p = true;
9060 /* Even if the template-id appears in an integral
9061 constant-expression, the contents of the argument list do
9062 not. */
9063 saved_ice_p = parser->integral_constant_expression_p;
9064 parser->integral_constant_expression_p = false;
9065 saved_non_ice_p = parser->non_integral_constant_expression_p;
9066 parser->non_integral_constant_expression_p = false;
9067 /* Parse the arguments. */
9068 do
9069 {
9070 tree argument;
9071
9072 if (n_args)
9073 /* Consume the comma. */
9074 cp_lexer_consume_token (parser->lexer);
9075
9076 /* Parse the template-argument. */
9077 argument = cp_parser_template_argument (parser);
9078 if (n_args == alloced)
9079 {
9080 alloced *= 2;
9081
9082 if (arg_ary == fixed_args)
9083 {
9084 arg_ary = XNEWVEC (tree, alloced);
9085 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9086 }
9087 else
9088 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9089 }
9090 arg_ary[n_args++] = argument;
9091 }
9092 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9093
9094 vec = make_tree_vec (n_args);
9095
9096 while (n_args--)
9097 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9098
9099 if (arg_ary != fixed_args)
9100 free (arg_ary);
9101 parser->non_integral_constant_expression_p = saved_non_ice_p;
9102 parser->integral_constant_expression_p = saved_ice_p;
9103 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9104 return vec;
9105 }
9106
9107 /* Parse a template-argument.
9108
9109 template-argument:
9110 assignment-expression
9111 type-id
9112 id-expression
9113
9114 The representation is that of an assignment-expression, type-id, or
9115 id-expression -- except that the qualified id-expression is
9116 evaluated, so that the value returned is either a DECL or an
9117 OVERLOAD.
9118
9119 Although the standard says "assignment-expression", it forbids
9120 throw-expressions or assignments in the template argument.
9121 Therefore, we use "conditional-expression" instead. */
9122
9123 static tree
9124 cp_parser_template_argument (cp_parser* parser)
9125 {
9126 tree argument;
9127 bool template_p;
9128 bool address_p;
9129 bool maybe_type_id = false;
9130 cp_token *token;
9131 cp_id_kind idk;
9132
9133 /* There's really no way to know what we're looking at, so we just
9134 try each alternative in order.
9135
9136 [temp.arg]
9137
9138 In a template-argument, an ambiguity between a type-id and an
9139 expression is resolved to a type-id, regardless of the form of
9140 the corresponding template-parameter.
9141
9142 Therefore, we try a type-id first. */
9143 cp_parser_parse_tentatively (parser);
9144 argument = cp_parser_type_id (parser);
9145 /* If there was no error parsing the type-id but the next token is a '>>',
9146 we probably found a typo for '> >'. But there are type-id which are
9147 also valid expressions. For instance:
9148
9149 struct X { int operator >> (int); };
9150 template <int V> struct Foo {};
9151 Foo<X () >> 5> r;
9152
9153 Here 'X()' is a valid type-id of a function type, but the user just
9154 wanted to write the expression "X() >> 5". Thus, we remember that we
9155 found a valid type-id, but we still try to parse the argument as an
9156 expression to see what happens. */
9157 if (!cp_parser_error_occurred (parser)
9158 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9159 {
9160 maybe_type_id = true;
9161 cp_parser_abort_tentative_parse (parser);
9162 }
9163 else
9164 {
9165 /* If the next token isn't a `,' or a `>', then this argument wasn't
9166 really finished. This means that the argument is not a valid
9167 type-id. */
9168 if (!cp_parser_next_token_ends_template_argument_p (parser))
9169 cp_parser_error (parser, "expected template-argument");
9170 /* If that worked, we're done. */
9171 if (cp_parser_parse_definitely (parser))
9172 return argument;
9173 }
9174 /* We're still not sure what the argument will be. */
9175 cp_parser_parse_tentatively (parser);
9176 /* Try a template. */
9177 argument = cp_parser_id_expression (parser,
9178 /*template_keyword_p=*/false,
9179 /*check_dependency_p=*/true,
9180 &template_p,
9181 /*declarator_p=*/false,
9182 /*optional_p=*/false);
9183 /* If the next token isn't a `,' or a `>', then this argument wasn't
9184 really finished. */
9185 if (!cp_parser_next_token_ends_template_argument_p (parser))
9186 cp_parser_error (parser, "expected template-argument");
9187 if (!cp_parser_error_occurred (parser))
9188 {
9189 /* Figure out what is being referred to. If the id-expression
9190 was for a class template specialization, then we will have a
9191 TYPE_DECL at this point. There is no need to do name lookup
9192 at this point in that case. */
9193 if (TREE_CODE (argument) != TYPE_DECL)
9194 argument = cp_parser_lookup_name (parser, argument,
9195 none_type,
9196 /*is_template=*/template_p,
9197 /*is_namespace=*/false,
9198 /*check_dependency=*/true,
9199 /*ambiguous_decls=*/NULL);
9200 if (TREE_CODE (argument) != TEMPLATE_DECL
9201 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9202 cp_parser_error (parser, "expected template-name");
9203 }
9204 if (cp_parser_parse_definitely (parser))
9205 return argument;
9206 /* It must be a non-type argument. There permitted cases are given
9207 in [temp.arg.nontype]:
9208
9209 -- an integral constant-expression of integral or enumeration
9210 type; or
9211
9212 -- the name of a non-type template-parameter; or
9213
9214 -- the name of an object or function with external linkage...
9215
9216 -- the address of an object or function with external linkage...
9217
9218 -- a pointer to member... */
9219 /* Look for a non-type template parameter. */
9220 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9221 {
9222 cp_parser_parse_tentatively (parser);
9223 argument = cp_parser_primary_expression (parser,
9224 /*adress_p=*/false,
9225 /*cast_p=*/false,
9226 /*template_arg_p=*/true,
9227 &idk);
9228 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9229 || !cp_parser_next_token_ends_template_argument_p (parser))
9230 cp_parser_simulate_error (parser);
9231 if (cp_parser_parse_definitely (parser))
9232 return argument;
9233 }
9234
9235 /* If the next token is "&", the argument must be the address of an
9236 object or function with external linkage. */
9237 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9238 if (address_p)
9239 cp_lexer_consume_token (parser->lexer);
9240 /* See if we might have an id-expression. */
9241 token = cp_lexer_peek_token (parser->lexer);
9242 if (token->type == CPP_NAME
9243 || token->keyword == RID_OPERATOR
9244 || token->type == CPP_SCOPE
9245 || token->type == CPP_TEMPLATE_ID
9246 || token->type == CPP_NESTED_NAME_SPECIFIER)
9247 {
9248 cp_parser_parse_tentatively (parser);
9249 argument = cp_parser_primary_expression (parser,
9250 address_p,
9251 /*cast_p=*/false,
9252 /*template_arg_p=*/true,
9253 &idk);
9254 if (cp_parser_error_occurred (parser)
9255 || !cp_parser_next_token_ends_template_argument_p (parser))
9256 cp_parser_abort_tentative_parse (parser);
9257 else
9258 {
9259 if (TREE_CODE (argument) == INDIRECT_REF)
9260 {
9261 gcc_assert (REFERENCE_REF_P (argument));
9262 argument = TREE_OPERAND (argument, 0);
9263 }
9264
9265 if (TREE_CODE (argument) == VAR_DECL)
9266 {
9267 /* A variable without external linkage might still be a
9268 valid constant-expression, so no error is issued here
9269 if the external-linkage check fails. */
9270 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9271 cp_parser_simulate_error (parser);
9272 }
9273 else if (is_overloaded_fn (argument))
9274 /* All overloaded functions are allowed; if the external
9275 linkage test does not pass, an error will be issued
9276 later. */
9277 ;
9278 else if (address_p
9279 && (TREE_CODE (argument) == OFFSET_REF
9280 || TREE_CODE (argument) == SCOPE_REF))
9281 /* A pointer-to-member. */
9282 ;
9283 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9284 ;
9285 else
9286 cp_parser_simulate_error (parser);
9287
9288 if (cp_parser_parse_definitely (parser))
9289 {
9290 if (address_p)
9291 argument = build_x_unary_op (ADDR_EXPR, argument);
9292 return argument;
9293 }
9294 }
9295 }
9296 /* If the argument started with "&", there are no other valid
9297 alternatives at this point. */
9298 if (address_p)
9299 {
9300 cp_parser_error (parser, "invalid non-type template argument");
9301 return error_mark_node;
9302 }
9303
9304 /* If the argument wasn't successfully parsed as a type-id followed
9305 by '>>', the argument can only be a constant expression now.
9306 Otherwise, we try parsing the constant-expression tentatively,
9307 because the argument could really be a type-id. */
9308 if (maybe_type_id)
9309 cp_parser_parse_tentatively (parser);
9310 argument = cp_parser_constant_expression (parser,
9311 /*allow_non_constant_p=*/false,
9312 /*non_constant_p=*/NULL);
9313 argument = fold_non_dependent_expr (argument);
9314 if (!maybe_type_id)
9315 return argument;
9316 if (!cp_parser_next_token_ends_template_argument_p (parser))
9317 cp_parser_error (parser, "expected template-argument");
9318 if (cp_parser_parse_definitely (parser))
9319 return argument;
9320 /* We did our best to parse the argument as a non type-id, but that
9321 was the only alternative that matched (albeit with a '>' after
9322 it). We can assume it's just a typo from the user, and a
9323 diagnostic will then be issued. */
9324 return cp_parser_type_id (parser);
9325 }
9326
9327 /* Parse an explicit-instantiation.
9328
9329 explicit-instantiation:
9330 template declaration
9331
9332 Although the standard says `declaration', what it really means is:
9333
9334 explicit-instantiation:
9335 template decl-specifier-seq [opt] declarator [opt] ;
9336
9337 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9338 supposed to be allowed. A defect report has been filed about this
9339 issue.
9340
9341 GNU Extension:
9342
9343 explicit-instantiation:
9344 storage-class-specifier template
9345 decl-specifier-seq [opt] declarator [opt] ;
9346 function-specifier template
9347 decl-specifier-seq [opt] declarator [opt] ; */
9348
9349 static void
9350 cp_parser_explicit_instantiation (cp_parser* parser)
9351 {
9352 int declares_class_or_enum;
9353 cp_decl_specifier_seq decl_specifiers;
9354 tree extension_specifier = NULL_TREE;
9355
9356 /* Look for an (optional) storage-class-specifier or
9357 function-specifier. */
9358 if (cp_parser_allow_gnu_extensions_p (parser))
9359 {
9360 extension_specifier
9361 = cp_parser_storage_class_specifier_opt (parser);
9362 if (!extension_specifier)
9363 extension_specifier
9364 = cp_parser_function_specifier_opt (parser,
9365 /*decl_specs=*/NULL);
9366 }
9367
9368 /* Look for the `template' keyword. */
9369 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9370 /* Let the front end know that we are processing an explicit
9371 instantiation. */
9372 begin_explicit_instantiation ();
9373 /* [temp.explicit] says that we are supposed to ignore access
9374 control while processing explicit instantiation directives. */
9375 push_deferring_access_checks (dk_no_check);
9376 /* Parse a decl-specifier-seq. */
9377 cp_parser_decl_specifier_seq (parser,
9378 CP_PARSER_FLAGS_OPTIONAL,
9379 &decl_specifiers,
9380 &declares_class_or_enum);
9381 /* If there was exactly one decl-specifier, and it declared a class,
9382 and there's no declarator, then we have an explicit type
9383 instantiation. */
9384 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9385 {
9386 tree type;
9387
9388 type = check_tag_decl (&decl_specifiers);
9389 /* Turn access control back on for names used during
9390 template instantiation. */
9391 pop_deferring_access_checks ();
9392 if (type)
9393 do_type_instantiation (type, extension_specifier,
9394 /*complain=*/tf_error);
9395 }
9396 else
9397 {
9398 cp_declarator *declarator;
9399 tree decl;
9400
9401 /* Parse the declarator. */
9402 declarator
9403 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9404 /*ctor_dtor_or_conv_p=*/NULL,
9405 /*parenthesized_p=*/NULL,
9406 /*member_p=*/false);
9407 if (declares_class_or_enum & 2)
9408 cp_parser_check_for_definition_in_return_type (declarator,
9409 decl_specifiers.type);
9410 if (declarator != cp_error_declarator)
9411 {
9412 decl = grokdeclarator (declarator, &decl_specifiers,
9413 NORMAL, 0, &decl_specifiers.attributes);
9414 /* Turn access control back on for names used during
9415 template instantiation. */
9416 pop_deferring_access_checks ();
9417 /* Do the explicit instantiation. */
9418 do_decl_instantiation (decl, extension_specifier);
9419 }
9420 else
9421 {
9422 pop_deferring_access_checks ();
9423 /* Skip the body of the explicit instantiation. */
9424 cp_parser_skip_to_end_of_statement (parser);
9425 }
9426 }
9427 /* We're done with the instantiation. */
9428 end_explicit_instantiation ();
9429
9430 cp_parser_consume_semicolon_at_end_of_statement (parser);
9431 }
9432
9433 /* Parse an explicit-specialization.
9434
9435 explicit-specialization:
9436 template < > declaration
9437
9438 Although the standard says `declaration', what it really means is:
9439
9440 explicit-specialization:
9441 template <> decl-specifier [opt] init-declarator [opt] ;
9442 template <> function-definition
9443 template <> explicit-specialization
9444 template <> template-declaration */
9445
9446 static void
9447 cp_parser_explicit_specialization (cp_parser* parser)
9448 {
9449 bool need_lang_pop;
9450 /* Look for the `template' keyword. */
9451 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9452 /* Look for the `<'. */
9453 cp_parser_require (parser, CPP_LESS, "`<'");
9454 /* Look for the `>'. */
9455 cp_parser_require (parser, CPP_GREATER, "`>'");
9456 /* We have processed another parameter list. */
9457 ++parser->num_template_parameter_lists;
9458 /* [temp]
9459
9460 A template ... explicit specialization ... shall not have C
9461 linkage. */
9462 if (current_lang_name == lang_name_c)
9463 {
9464 error ("template specialization with C linkage");
9465 /* Give it C++ linkage to avoid confusing other parts of the
9466 front end. */
9467 push_lang_context (lang_name_cplusplus);
9468 need_lang_pop = true;
9469 }
9470 else
9471 need_lang_pop = false;
9472 /* Let the front end know that we are beginning a specialization. */
9473 if (!begin_specialization ())
9474 {
9475 end_specialization ();
9476 cp_parser_skip_to_end_of_block_or_statement (parser);
9477 return;
9478 }
9479
9480 /* If the next keyword is `template', we need to figure out whether
9481 or not we're looking a template-declaration. */
9482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9483 {
9484 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9485 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9486 cp_parser_template_declaration_after_export (parser,
9487 /*member_p=*/false);
9488 else
9489 cp_parser_explicit_specialization (parser);
9490 }
9491 else
9492 /* Parse the dependent declaration. */
9493 cp_parser_single_declaration (parser,
9494 /*checks=*/NULL_TREE,
9495 /*member_p=*/false,
9496 /*friend_p=*/NULL);
9497 /* We're done with the specialization. */
9498 end_specialization ();
9499 /* For the erroneous case of a template with C linkage, we pushed an
9500 implicit C++ linkage scope; exit that scope now. */
9501 if (need_lang_pop)
9502 pop_lang_context ();
9503 /* We're done with this parameter list. */
9504 --parser->num_template_parameter_lists;
9505 }
9506
9507 /* Parse a type-specifier.
9508
9509 type-specifier:
9510 simple-type-specifier
9511 class-specifier
9512 enum-specifier
9513 elaborated-type-specifier
9514 cv-qualifier
9515
9516 GNU Extension:
9517
9518 type-specifier:
9519 __complex__
9520
9521 Returns a representation of the type-specifier. For a
9522 class-specifier, enum-specifier, or elaborated-type-specifier, a
9523 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9524
9525 The parser flags FLAGS is used to control type-specifier parsing.
9526
9527 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9528 in a decl-specifier-seq.
9529
9530 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9531 class-specifier, enum-specifier, or elaborated-type-specifier, then
9532 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9533 if a type is declared; 2 if it is defined. Otherwise, it is set to
9534 zero.
9535
9536 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9537 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9538 is set to FALSE. */
9539
9540 static tree
9541 cp_parser_type_specifier (cp_parser* parser,
9542 cp_parser_flags flags,
9543 cp_decl_specifier_seq *decl_specs,
9544 bool is_declaration,
9545 int* declares_class_or_enum,
9546 bool* is_cv_qualifier)
9547 {
9548 tree type_spec = NULL_TREE;
9549 cp_token *token;
9550 enum rid keyword;
9551 cp_decl_spec ds = ds_last;
9552
9553 /* Assume this type-specifier does not declare a new type. */
9554 if (declares_class_or_enum)
9555 *declares_class_or_enum = 0;
9556 /* And that it does not specify a cv-qualifier. */
9557 if (is_cv_qualifier)
9558 *is_cv_qualifier = false;
9559 /* Peek at the next token. */
9560 token = cp_lexer_peek_token (parser->lexer);
9561
9562 /* If we're looking at a keyword, we can use that to guide the
9563 production we choose. */
9564 keyword = token->keyword;
9565 switch (keyword)
9566 {
9567 case RID_ENUM:
9568 /* Look for the enum-specifier. */
9569 type_spec = cp_parser_enum_specifier (parser);
9570 /* If that worked, we're done. */
9571 if (type_spec)
9572 {
9573 if (declares_class_or_enum)
9574 *declares_class_or_enum = 2;
9575 if (decl_specs)
9576 cp_parser_set_decl_spec_type (decl_specs,
9577 type_spec,
9578 /*user_defined_p=*/true);
9579 return type_spec;
9580 }
9581 else
9582 goto elaborated_type_specifier;
9583
9584 /* Any of these indicate either a class-specifier, or an
9585 elaborated-type-specifier. */
9586 case RID_CLASS:
9587 case RID_STRUCT:
9588 case RID_UNION:
9589 /* Parse tentatively so that we can back up if we don't find a
9590 class-specifier. */
9591 cp_parser_parse_tentatively (parser);
9592 /* Look for the class-specifier. */
9593 type_spec = cp_parser_class_specifier (parser);
9594 /* If that worked, we're done. */
9595 if (cp_parser_parse_definitely (parser))
9596 {
9597 if (declares_class_or_enum)
9598 *declares_class_or_enum = 2;
9599 if (decl_specs)
9600 cp_parser_set_decl_spec_type (decl_specs,
9601 type_spec,
9602 /*user_defined_p=*/true);
9603 return type_spec;
9604 }
9605
9606 /* Fall through. */
9607 elaborated_type_specifier:
9608 /* We're declaring (not defining) a class or enum. */
9609 if (declares_class_or_enum)
9610 *declares_class_or_enum = 1;
9611
9612 /* Fall through. */
9613 case RID_TYPENAME:
9614 /* Look for an elaborated-type-specifier. */
9615 type_spec
9616 = (cp_parser_elaborated_type_specifier
9617 (parser,
9618 decl_specs && decl_specs->specs[(int) ds_friend],
9619 is_declaration));
9620 if (decl_specs)
9621 cp_parser_set_decl_spec_type (decl_specs,
9622 type_spec,
9623 /*user_defined_p=*/true);
9624 return type_spec;
9625
9626 case RID_CONST:
9627 ds = ds_const;
9628 if (is_cv_qualifier)
9629 *is_cv_qualifier = true;
9630 break;
9631
9632 case RID_VOLATILE:
9633 ds = ds_volatile;
9634 if (is_cv_qualifier)
9635 *is_cv_qualifier = true;
9636 break;
9637
9638 case RID_RESTRICT:
9639 ds = ds_restrict;
9640 if (is_cv_qualifier)
9641 *is_cv_qualifier = true;
9642 break;
9643
9644 case RID_COMPLEX:
9645 /* The `__complex__' keyword is a GNU extension. */
9646 ds = ds_complex;
9647 break;
9648
9649 default:
9650 break;
9651 }
9652
9653 /* Handle simple keywords. */
9654 if (ds != ds_last)
9655 {
9656 if (decl_specs)
9657 {
9658 ++decl_specs->specs[(int)ds];
9659 decl_specs->any_specifiers_p = true;
9660 }
9661 return cp_lexer_consume_token (parser->lexer)->value;
9662 }
9663
9664 /* If we do not already have a type-specifier, assume we are looking
9665 at a simple-type-specifier. */
9666 type_spec = cp_parser_simple_type_specifier (parser,
9667 decl_specs,
9668 flags);
9669
9670 /* If we didn't find a type-specifier, and a type-specifier was not
9671 optional in this context, issue an error message. */
9672 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9673 {
9674 cp_parser_error (parser, "expected type specifier");
9675 return error_mark_node;
9676 }
9677
9678 return type_spec;
9679 }
9680
9681 /* Parse a simple-type-specifier.
9682
9683 simple-type-specifier:
9684 :: [opt] nested-name-specifier [opt] type-name
9685 :: [opt] nested-name-specifier template template-id
9686 char
9687 wchar_t
9688 bool
9689 short
9690 int
9691 long
9692 signed
9693 unsigned
9694 float
9695 double
9696 void
9697
9698 GNU Extension:
9699
9700 simple-type-specifier:
9701 __typeof__ unary-expression
9702 __typeof__ ( type-id )
9703
9704 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9705 appropriately updated. */
9706
9707 static tree
9708 cp_parser_simple_type_specifier (cp_parser* parser,
9709 cp_decl_specifier_seq *decl_specs,
9710 cp_parser_flags flags)
9711 {
9712 tree type = NULL_TREE;
9713 cp_token *token;
9714
9715 /* Peek at the next token. */
9716 token = cp_lexer_peek_token (parser->lexer);
9717
9718 /* If we're looking at a keyword, things are easy. */
9719 switch (token->keyword)
9720 {
9721 case RID_CHAR:
9722 if (decl_specs)
9723 decl_specs->explicit_char_p = true;
9724 type = char_type_node;
9725 break;
9726 case RID_WCHAR:
9727 type = wchar_type_node;
9728 break;
9729 case RID_BOOL:
9730 type = boolean_type_node;
9731 break;
9732 case RID_SHORT:
9733 if (decl_specs)
9734 ++decl_specs->specs[(int) ds_short];
9735 type = short_integer_type_node;
9736 break;
9737 case RID_INT:
9738 if (decl_specs)
9739 decl_specs->explicit_int_p = true;
9740 type = integer_type_node;
9741 break;
9742 case RID_LONG:
9743 if (decl_specs)
9744 ++decl_specs->specs[(int) ds_long];
9745 type = long_integer_type_node;
9746 break;
9747 case RID_SIGNED:
9748 if (decl_specs)
9749 ++decl_specs->specs[(int) ds_signed];
9750 type = integer_type_node;
9751 break;
9752 case RID_UNSIGNED:
9753 if (decl_specs)
9754 ++decl_specs->specs[(int) ds_unsigned];
9755 type = unsigned_type_node;
9756 break;
9757 case RID_FLOAT:
9758 type = float_type_node;
9759 break;
9760 case RID_DOUBLE:
9761 type = double_type_node;
9762 break;
9763 case RID_VOID:
9764 type = void_type_node;
9765 break;
9766
9767 case RID_TYPEOF:
9768 /* Consume the `typeof' token. */
9769 cp_lexer_consume_token (parser->lexer);
9770 /* Parse the operand to `typeof'. */
9771 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9772 /* If it is not already a TYPE, take its type. */
9773 if (!TYPE_P (type))
9774 type = finish_typeof (type);
9775
9776 if (decl_specs)
9777 cp_parser_set_decl_spec_type (decl_specs, type,
9778 /*user_defined_p=*/true);
9779
9780 return type;
9781
9782 default:
9783 break;
9784 }
9785
9786 /* If the type-specifier was for a built-in type, we're done. */
9787 if (type)
9788 {
9789 tree id;
9790
9791 /* Record the type. */
9792 if (decl_specs
9793 && (token->keyword != RID_SIGNED
9794 && token->keyword != RID_UNSIGNED
9795 && token->keyword != RID_SHORT
9796 && token->keyword != RID_LONG))
9797 cp_parser_set_decl_spec_type (decl_specs,
9798 type,
9799 /*user_defined=*/false);
9800 if (decl_specs)
9801 decl_specs->any_specifiers_p = true;
9802
9803 /* Consume the token. */
9804 id = cp_lexer_consume_token (parser->lexer)->value;
9805
9806 /* There is no valid C++ program where a non-template type is
9807 followed by a "<". That usually indicates that the user thought
9808 that the type was a template. */
9809 cp_parser_check_for_invalid_template_id (parser, type);
9810
9811 return TYPE_NAME (type);
9812 }
9813
9814 /* The type-specifier must be a user-defined type. */
9815 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9816 {
9817 bool qualified_p;
9818 bool global_p;
9819
9820 /* Don't gobble tokens or issue error messages if this is an
9821 optional type-specifier. */
9822 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9823 cp_parser_parse_tentatively (parser);
9824
9825 /* Look for the optional `::' operator. */
9826 global_p
9827 = (cp_parser_global_scope_opt (parser,
9828 /*current_scope_valid_p=*/false)
9829 != NULL_TREE);
9830 /* Look for the nested-name specifier. */
9831 qualified_p
9832 = (cp_parser_nested_name_specifier_opt (parser,
9833 /*typename_keyword_p=*/false,
9834 /*check_dependency_p=*/true,
9835 /*type_p=*/false,
9836 /*is_declaration=*/false)
9837 != NULL_TREE);
9838 /* If we have seen a nested-name-specifier, and the next token
9839 is `template', then we are using the template-id production. */
9840 if (parser->scope
9841 && cp_parser_optional_template_keyword (parser))
9842 {
9843 /* Look for the template-id. */
9844 type = cp_parser_template_id (parser,
9845 /*template_keyword_p=*/true,
9846 /*check_dependency_p=*/true,
9847 /*is_declaration=*/false);
9848 /* If the template-id did not name a type, we are out of
9849 luck. */
9850 if (TREE_CODE (type) != TYPE_DECL)
9851 {
9852 cp_parser_error (parser, "expected template-id for type");
9853 type = NULL_TREE;
9854 }
9855 }
9856 /* Otherwise, look for a type-name. */
9857 else
9858 type = cp_parser_type_name (parser);
9859 /* Keep track of all name-lookups performed in class scopes. */
9860 if (type
9861 && !global_p
9862 && !qualified_p
9863 && TREE_CODE (type) == TYPE_DECL
9864 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9865 maybe_note_name_used_in_class (DECL_NAME (type), type);
9866 /* If it didn't work out, we don't have a TYPE. */
9867 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9868 && !cp_parser_parse_definitely (parser))
9869 type = NULL_TREE;
9870 if (type && decl_specs)
9871 cp_parser_set_decl_spec_type (decl_specs, type,
9872 /*user_defined=*/true);
9873 }
9874
9875 /* If we didn't get a type-name, issue an error message. */
9876 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9877 {
9878 cp_parser_error (parser, "expected type-name");
9879 return error_mark_node;
9880 }
9881
9882 /* There is no valid C++ program where a non-template type is
9883 followed by a "<". That usually indicates that the user thought
9884 that the type was a template. */
9885 if (type && type != error_mark_node)
9886 {
9887 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9888 If it is, then the '<'...'>' enclose protocol names rather than
9889 template arguments, and so everything is fine. */
9890 if (c_dialect_objc ()
9891 && (objc_is_id (type) || objc_is_class_name (type)))
9892 {
9893 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9894 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9895
9896 /* Clobber the "unqualified" type previously entered into
9897 DECL_SPECS with the new, improved protocol-qualified version. */
9898 if (decl_specs)
9899 decl_specs->type = qual_type;
9900
9901 return qual_type;
9902 }
9903
9904 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9905 }
9906
9907 return type;
9908 }
9909
9910 /* Parse a type-name.
9911
9912 type-name:
9913 class-name
9914 enum-name
9915 typedef-name
9916
9917 enum-name:
9918 identifier
9919
9920 typedef-name:
9921 identifier
9922
9923 Returns a TYPE_DECL for the type. */
9924
9925 static tree
9926 cp_parser_type_name (cp_parser* parser)
9927 {
9928 tree type_decl;
9929 tree identifier;
9930
9931 /* We can't know yet whether it is a class-name or not. */
9932 cp_parser_parse_tentatively (parser);
9933 /* Try a class-name. */
9934 type_decl = cp_parser_class_name (parser,
9935 /*typename_keyword_p=*/false,
9936 /*template_keyword_p=*/false,
9937 none_type,
9938 /*check_dependency_p=*/true,
9939 /*class_head_p=*/false,
9940 /*is_declaration=*/false);
9941 /* If it's not a class-name, keep looking. */
9942 if (!cp_parser_parse_definitely (parser))
9943 {
9944 /* It must be a typedef-name or an enum-name. */
9945 identifier = cp_parser_identifier (parser);
9946 if (identifier == error_mark_node)
9947 return error_mark_node;
9948
9949 /* Look up the type-name. */
9950 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9951
9952 if (TREE_CODE (type_decl) != TYPE_DECL
9953 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9954 {
9955 /* See if this is an Objective-C type. */
9956 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9957 tree type = objc_get_protocol_qualified_type (identifier, protos);
9958 if (type)
9959 type_decl = TYPE_NAME (type);
9960 }
9961
9962 /* Issue an error if we did not find a type-name. */
9963 if (TREE_CODE (type_decl) != TYPE_DECL)
9964 {
9965 if (!cp_parser_simulate_error (parser))
9966 cp_parser_name_lookup_error (parser, identifier, type_decl,
9967 "is not a type");
9968 type_decl = error_mark_node;
9969 }
9970 /* Remember that the name was used in the definition of the
9971 current class so that we can check later to see if the
9972 meaning would have been different after the class was
9973 entirely defined. */
9974 else if (type_decl != error_mark_node
9975 && !parser->scope)
9976 maybe_note_name_used_in_class (identifier, type_decl);
9977 }
9978
9979 return type_decl;
9980 }
9981
9982
9983 /* Parse an elaborated-type-specifier. Note that the grammar given
9984 here incorporates the resolution to DR68.
9985
9986 elaborated-type-specifier:
9987 class-key :: [opt] nested-name-specifier [opt] identifier
9988 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9989 enum :: [opt] nested-name-specifier [opt] identifier
9990 typename :: [opt] nested-name-specifier identifier
9991 typename :: [opt] nested-name-specifier template [opt]
9992 template-id
9993
9994 GNU extension:
9995
9996 elaborated-type-specifier:
9997 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9998 class-key attributes :: [opt] nested-name-specifier [opt]
9999 template [opt] template-id
10000 enum attributes :: [opt] nested-name-specifier [opt] identifier
10001
10002 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10003 declared `friend'. If IS_DECLARATION is TRUE, then this
10004 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10005 something is being declared.
10006
10007 Returns the TYPE specified. */
10008
10009 static tree
10010 cp_parser_elaborated_type_specifier (cp_parser* parser,
10011 bool is_friend,
10012 bool is_declaration)
10013 {
10014 enum tag_types tag_type;
10015 tree identifier;
10016 tree type = NULL_TREE;
10017 tree attributes = NULL_TREE;
10018
10019 /* See if we're looking at the `enum' keyword. */
10020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10021 {
10022 /* Consume the `enum' token. */
10023 cp_lexer_consume_token (parser->lexer);
10024 /* Remember that it's an enumeration type. */
10025 tag_type = enum_type;
10026 /* Parse the attributes. */
10027 attributes = cp_parser_attributes_opt (parser);
10028 }
10029 /* Or, it might be `typename'. */
10030 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10031 RID_TYPENAME))
10032 {
10033 /* Consume the `typename' token. */
10034 cp_lexer_consume_token (parser->lexer);
10035 /* Remember that it's a `typename' type. */
10036 tag_type = typename_type;
10037 /* The `typename' keyword is only allowed in templates. */
10038 if (!processing_template_decl)
10039 pedwarn ("using %<typename%> outside of template");
10040 }
10041 /* Otherwise it must be a class-key. */
10042 else
10043 {
10044 tag_type = cp_parser_class_key (parser);
10045 if (tag_type == none_type)
10046 return error_mark_node;
10047 /* Parse the attributes. */
10048 attributes = cp_parser_attributes_opt (parser);
10049 }
10050
10051 /* Look for the `::' operator. */
10052 cp_parser_global_scope_opt (parser,
10053 /*current_scope_valid_p=*/false);
10054 /* Look for the nested-name-specifier. */
10055 if (tag_type == typename_type)
10056 {
10057 if (!cp_parser_nested_name_specifier (parser,
10058 /*typename_keyword_p=*/true,
10059 /*check_dependency_p=*/true,
10060 /*type_p=*/true,
10061 is_declaration))
10062 return error_mark_node;
10063 }
10064 else
10065 /* Even though `typename' is not present, the proposed resolution
10066 to Core Issue 180 says that in `class A<T>::B', `B' should be
10067 considered a type-name, even if `A<T>' is dependent. */
10068 cp_parser_nested_name_specifier_opt (parser,
10069 /*typename_keyword_p=*/true,
10070 /*check_dependency_p=*/true,
10071 /*type_p=*/true,
10072 is_declaration);
10073 /* For everything but enumeration types, consider a template-id. */
10074 /* For an enumeration type, consider only a plain identifier. */
10075 if (tag_type != enum_type)
10076 {
10077 bool template_p = false;
10078 tree decl;
10079
10080 /* Allow the `template' keyword. */
10081 template_p = cp_parser_optional_template_keyword (parser);
10082 /* If we didn't see `template', we don't know if there's a
10083 template-id or not. */
10084 if (!template_p)
10085 cp_parser_parse_tentatively (parser);
10086 /* Parse the template-id. */
10087 decl = cp_parser_template_id (parser, template_p,
10088 /*check_dependency_p=*/true,
10089 is_declaration);
10090 /* If we didn't find a template-id, look for an ordinary
10091 identifier. */
10092 if (!template_p && !cp_parser_parse_definitely (parser))
10093 ;
10094 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10095 in effect, then we must assume that, upon instantiation, the
10096 template will correspond to a class. */
10097 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10098 && tag_type == typename_type)
10099 type = make_typename_type (parser->scope, decl,
10100 typename_type,
10101 /*complain=*/tf_error);
10102 else
10103 type = TREE_TYPE (decl);
10104 }
10105
10106 if (!type)
10107 {
10108 identifier = cp_parser_identifier (parser);
10109
10110 if (identifier == error_mark_node)
10111 {
10112 parser->scope = NULL_TREE;
10113 return error_mark_node;
10114 }
10115
10116 /* For a `typename', we needn't call xref_tag. */
10117 if (tag_type == typename_type
10118 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10119 return cp_parser_make_typename_type (parser, parser->scope,
10120 identifier);
10121 /* Look up a qualified name in the usual way. */
10122 if (parser->scope)
10123 {
10124 tree decl;
10125
10126 decl = cp_parser_lookup_name (parser, identifier,
10127 tag_type,
10128 /*is_template=*/false,
10129 /*is_namespace=*/false,
10130 /*check_dependency=*/true,
10131 /*ambiguous_decls=*/NULL);
10132
10133 /* If we are parsing friend declaration, DECL may be a
10134 TEMPLATE_DECL tree node here. However, we need to check
10135 whether this TEMPLATE_DECL results in valid code. Consider
10136 the following example:
10137
10138 namespace N {
10139 template <class T> class C {};
10140 }
10141 class X {
10142 template <class T> friend class N::C; // #1, valid code
10143 };
10144 template <class T> class Y {
10145 friend class N::C; // #2, invalid code
10146 };
10147
10148 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10149 name lookup of `N::C'. We see that friend declaration must
10150 be template for the code to be valid. Note that
10151 processing_template_decl does not work here since it is
10152 always 1 for the above two cases. */
10153
10154 decl = (cp_parser_maybe_treat_template_as_class
10155 (decl, /*tag_name_p=*/is_friend
10156 && parser->num_template_parameter_lists));
10157
10158 if (TREE_CODE (decl) != TYPE_DECL)
10159 {
10160 cp_parser_diagnose_invalid_type_name (parser,
10161 parser->scope,
10162 identifier);
10163 return error_mark_node;
10164 }
10165
10166 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10167 check_elaborated_type_specifier
10168 (tag_type, decl,
10169 (parser->num_template_parameter_lists
10170 || DECL_SELF_REFERENCE_P (decl)));
10171
10172 type = TREE_TYPE (decl);
10173 }
10174 else
10175 {
10176 /* An elaborated-type-specifier sometimes introduces a new type and
10177 sometimes names an existing type. Normally, the rule is that it
10178 introduces a new type only if there is not an existing type of
10179 the same name already in scope. For example, given:
10180
10181 struct S {};
10182 void f() { struct S s; }
10183
10184 the `struct S' in the body of `f' is the same `struct S' as in
10185 the global scope; the existing definition is used. However, if
10186 there were no global declaration, this would introduce a new
10187 local class named `S'.
10188
10189 An exception to this rule applies to the following code:
10190
10191 namespace N { struct S; }
10192
10193 Here, the elaborated-type-specifier names a new type
10194 unconditionally; even if there is already an `S' in the
10195 containing scope this declaration names a new type.
10196 This exception only applies if the elaborated-type-specifier
10197 forms the complete declaration:
10198
10199 [class.name]
10200
10201 A declaration consisting solely of `class-key identifier ;' is
10202 either a redeclaration of the name in the current scope or a
10203 forward declaration of the identifier as a class name. It
10204 introduces the name into the current scope.
10205
10206 We are in this situation precisely when the next token is a `;'.
10207
10208 An exception to the exception is that a `friend' declaration does
10209 *not* name a new type; i.e., given:
10210
10211 struct S { friend struct T; };
10212
10213 `T' is not a new type in the scope of `S'.
10214
10215 Also, `new struct S' or `sizeof (struct S)' never results in the
10216 definition of a new type; a new type can only be declared in a
10217 declaration context. */
10218
10219 tag_scope ts;
10220 bool template_p;
10221
10222 if (is_friend)
10223 /* Friends have special name lookup rules. */
10224 ts = ts_within_enclosing_non_class;
10225 else if (is_declaration
10226 && cp_lexer_next_token_is (parser->lexer,
10227 CPP_SEMICOLON))
10228 /* This is a `class-key identifier ;' */
10229 ts = ts_current;
10230 else
10231 ts = ts_global;
10232
10233 template_p =
10234 (parser->num_template_parameter_lists
10235 && (cp_parser_next_token_starts_class_definition_p (parser)
10236 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10237 /* An unqualified name was used to reference this type, so
10238 there were no qualifying templates. */
10239 if (!cp_parser_check_template_parameters (parser,
10240 /*num_templates=*/0))
10241 return error_mark_node;
10242 type = xref_tag (tag_type, identifier, ts, template_p);
10243 }
10244 }
10245
10246 if (type == error_mark_node)
10247 return error_mark_node;
10248
10249 /* Allow attributes on forward declarations of classes. */
10250 if (attributes)
10251 {
10252 if (TREE_CODE (type) == TYPENAME_TYPE)
10253 warning (OPT_Wattributes,
10254 "attributes ignored on uninstantiated type");
10255 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10256 && ! processing_explicit_instantiation)
10257 warning (OPT_Wattributes,
10258 "attributes ignored on template instantiation");
10259 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10260 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10261 else
10262 warning (OPT_Wattributes,
10263 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10264 }
10265
10266 if (tag_type != enum_type)
10267 cp_parser_check_class_key (tag_type, type);
10268
10269 /* A "<" cannot follow an elaborated type specifier. If that
10270 happens, the user was probably trying to form a template-id. */
10271 cp_parser_check_for_invalid_template_id (parser, type);
10272
10273 return type;
10274 }
10275
10276 /* Parse an enum-specifier.
10277
10278 enum-specifier:
10279 enum identifier [opt] { enumerator-list [opt] }
10280
10281 GNU Extensions:
10282 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10283 attributes[opt]
10284
10285 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10286 if the token stream isn't an enum-specifier after all. */
10287
10288 static tree
10289 cp_parser_enum_specifier (cp_parser* parser)
10290 {
10291 tree identifier;
10292 tree type;
10293 tree attributes;
10294
10295 /* Parse tentatively so that we can back up if we don't find a
10296 enum-specifier. */
10297 cp_parser_parse_tentatively (parser);
10298
10299 /* Caller guarantees that the current token is 'enum', an identifier
10300 possibly follows, and the token after that is an opening brace.
10301 If we don't have an identifier, fabricate an anonymous name for
10302 the enumeration being defined. */
10303 cp_lexer_consume_token (parser->lexer);
10304
10305 attributes = cp_parser_attributes_opt (parser);
10306
10307 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10308 identifier = cp_parser_identifier (parser);
10309 else
10310 identifier = make_anon_name ();
10311
10312 /* Look for the `{' but don't consume it yet. */
10313 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10314 cp_parser_simulate_error (parser);
10315
10316 if (!cp_parser_parse_definitely (parser))
10317 return NULL_TREE;
10318
10319 /* Issue an error message if type-definitions are forbidden here. */
10320 cp_parser_check_type_definition (parser);
10321
10322 /* Create the new type. We do this before consuming the opening brace
10323 so the enum will be recorded as being on the line of its tag (or the
10324 'enum' keyword, if there is no tag). */
10325 type = start_enum (identifier);
10326
10327 /* Consume the opening brace. */
10328 cp_lexer_consume_token (parser->lexer);
10329
10330 if (type == error_mark_node)
10331 {
10332 cp_parser_skip_to_end_of_block_or_statement (parser);
10333 return error_mark_node;
10334 }
10335
10336 /* If the next token is not '}', then there are some enumerators. */
10337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10338 cp_parser_enumerator_list (parser, type);
10339
10340 /* Consume the final '}'. */
10341 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10342
10343 /* Look for trailing attributes to apply to this enumeration, and
10344 apply them if appropriate. */
10345 if (cp_parser_allow_gnu_extensions_p (parser))
10346 {
10347 tree trailing_attr = cp_parser_attributes_opt (parser);
10348 cplus_decl_attributes (&type,
10349 trailing_attr,
10350 (int) ATTR_FLAG_TYPE_IN_PLACE);
10351 }
10352
10353 /* Finish up the enumeration. */
10354 finish_enum (type);
10355
10356 return type;
10357 }
10358
10359 /* Parse an enumerator-list. The enumerators all have the indicated
10360 TYPE.
10361
10362 enumerator-list:
10363 enumerator-definition
10364 enumerator-list , enumerator-definition */
10365
10366 static void
10367 cp_parser_enumerator_list (cp_parser* parser, tree type)
10368 {
10369 while (true)
10370 {
10371 /* Parse an enumerator-definition. */
10372 cp_parser_enumerator_definition (parser, type);
10373
10374 /* If the next token is not a ',', we've reached the end of
10375 the list. */
10376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10377 break;
10378 /* Otherwise, consume the `,' and keep going. */
10379 cp_lexer_consume_token (parser->lexer);
10380 /* If the next token is a `}', there is a trailing comma. */
10381 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10382 {
10383 if (pedantic && !in_system_header)
10384 pedwarn ("comma at end of enumerator list");
10385 break;
10386 }
10387 }
10388 }
10389
10390 /* Parse an enumerator-definition. The enumerator has the indicated
10391 TYPE.
10392
10393 enumerator-definition:
10394 enumerator
10395 enumerator = constant-expression
10396
10397 enumerator:
10398 identifier */
10399
10400 static void
10401 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10402 {
10403 tree identifier;
10404 tree value;
10405
10406 /* Look for the identifier. */
10407 identifier = cp_parser_identifier (parser);
10408 if (identifier == error_mark_node)
10409 return;
10410
10411 /* If the next token is an '=', then there is an explicit value. */
10412 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10413 {
10414 /* Consume the `=' token. */
10415 cp_lexer_consume_token (parser->lexer);
10416 /* Parse the value. */
10417 value = cp_parser_constant_expression (parser,
10418 /*allow_non_constant_p=*/false,
10419 NULL);
10420 }
10421 else
10422 value = NULL_TREE;
10423
10424 /* Create the enumerator. */
10425 build_enumerator (identifier, value, type);
10426 }
10427
10428 /* Parse a namespace-name.
10429
10430 namespace-name:
10431 original-namespace-name
10432 namespace-alias
10433
10434 Returns the NAMESPACE_DECL for the namespace. */
10435
10436 static tree
10437 cp_parser_namespace_name (cp_parser* parser)
10438 {
10439 tree identifier;
10440 tree namespace_decl;
10441
10442 /* Get the name of the namespace. */
10443 identifier = cp_parser_identifier (parser);
10444 if (identifier == error_mark_node)
10445 return error_mark_node;
10446
10447 /* Look up the identifier in the currently active scope. Look only
10448 for namespaces, due to:
10449
10450 [basic.lookup.udir]
10451
10452 When looking up a namespace-name in a using-directive or alias
10453 definition, only namespace names are considered.
10454
10455 And:
10456
10457 [basic.lookup.qual]
10458
10459 During the lookup of a name preceding the :: scope resolution
10460 operator, object, function, and enumerator names are ignored.
10461
10462 (Note that cp_parser_class_or_namespace_name only calls this
10463 function if the token after the name is the scope resolution
10464 operator.) */
10465 namespace_decl = cp_parser_lookup_name (parser, identifier,
10466 none_type,
10467 /*is_template=*/false,
10468 /*is_namespace=*/true,
10469 /*check_dependency=*/true,
10470 /*ambiguous_decls=*/NULL);
10471 /* If it's not a namespace, issue an error. */
10472 if (namespace_decl == error_mark_node
10473 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10474 {
10475 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10476 error ("%qD is not a namespace-name", identifier);
10477 cp_parser_error (parser, "expected namespace-name");
10478 namespace_decl = error_mark_node;
10479 }
10480
10481 return namespace_decl;
10482 }
10483
10484 /* Parse a namespace-definition.
10485
10486 namespace-definition:
10487 named-namespace-definition
10488 unnamed-namespace-definition
10489
10490 named-namespace-definition:
10491 original-namespace-definition
10492 extension-namespace-definition
10493
10494 original-namespace-definition:
10495 namespace identifier { namespace-body }
10496
10497 extension-namespace-definition:
10498 namespace original-namespace-name { namespace-body }
10499
10500 unnamed-namespace-definition:
10501 namespace { namespace-body } */
10502
10503 static void
10504 cp_parser_namespace_definition (cp_parser* parser)
10505 {
10506 tree identifier, attribs;
10507
10508 /* Look for the `namespace' keyword. */
10509 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10510
10511 /* Get the name of the namespace. We do not attempt to distinguish
10512 between an original-namespace-definition and an
10513 extension-namespace-definition at this point. The semantic
10514 analysis routines are responsible for that. */
10515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10516 identifier = cp_parser_identifier (parser);
10517 else
10518 identifier = NULL_TREE;
10519
10520 /* Parse any specified attributes. */
10521 attribs = cp_parser_attributes_opt (parser);
10522
10523 /* Look for the `{' to start the namespace. */
10524 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10525 /* Start the namespace. */
10526 push_namespace_with_attribs (identifier, attribs);
10527 /* Parse the body of the namespace. */
10528 cp_parser_namespace_body (parser);
10529 /* Finish the namespace. */
10530 pop_namespace ();
10531 /* Look for the final `}'. */
10532 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10533 }
10534
10535 /* Parse a namespace-body.
10536
10537 namespace-body:
10538 declaration-seq [opt] */
10539
10540 static void
10541 cp_parser_namespace_body (cp_parser* parser)
10542 {
10543 cp_parser_declaration_seq_opt (parser);
10544 }
10545
10546 /* Parse a namespace-alias-definition.
10547
10548 namespace-alias-definition:
10549 namespace identifier = qualified-namespace-specifier ; */
10550
10551 static void
10552 cp_parser_namespace_alias_definition (cp_parser* parser)
10553 {
10554 tree identifier;
10555 tree namespace_specifier;
10556
10557 /* Look for the `namespace' keyword. */
10558 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10559 /* Look for the identifier. */
10560 identifier = cp_parser_identifier (parser);
10561 if (identifier == error_mark_node)
10562 return;
10563 /* Look for the `=' token. */
10564 cp_parser_require (parser, CPP_EQ, "`='");
10565 /* Look for the qualified-namespace-specifier. */
10566 namespace_specifier
10567 = cp_parser_qualified_namespace_specifier (parser);
10568 /* Look for the `;' token. */
10569 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10570
10571 /* Register the alias in the symbol table. */
10572 do_namespace_alias (identifier, namespace_specifier);
10573 }
10574
10575 /* Parse a qualified-namespace-specifier.
10576
10577 qualified-namespace-specifier:
10578 :: [opt] nested-name-specifier [opt] namespace-name
10579
10580 Returns a NAMESPACE_DECL corresponding to the specified
10581 namespace. */
10582
10583 static tree
10584 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10585 {
10586 /* Look for the optional `::'. */
10587 cp_parser_global_scope_opt (parser,
10588 /*current_scope_valid_p=*/false);
10589
10590 /* Look for the optional nested-name-specifier. */
10591 cp_parser_nested_name_specifier_opt (parser,
10592 /*typename_keyword_p=*/false,
10593 /*check_dependency_p=*/true,
10594 /*type_p=*/false,
10595 /*is_declaration=*/true);
10596
10597 return cp_parser_namespace_name (parser);
10598 }
10599
10600 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10601 access declaration.
10602
10603 using-declaration:
10604 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10605 using :: unqualified-id ;
10606
10607 access-declaration:
10608 qualified-id ;
10609
10610 */
10611
10612 static bool
10613 cp_parser_using_declaration (cp_parser* parser,
10614 bool access_declaration_p)
10615 {
10616 cp_token *token;
10617 bool typename_p = false;
10618 bool global_scope_p;
10619 tree decl;
10620 tree identifier;
10621 tree qscope;
10622
10623 if (access_declaration_p)
10624 cp_parser_parse_tentatively (parser);
10625 else
10626 {
10627 /* Look for the `using' keyword. */
10628 cp_parser_require_keyword (parser, RID_USING, "`using'");
10629
10630 /* Peek at the next token. */
10631 token = cp_lexer_peek_token (parser->lexer);
10632 /* See if it's `typename'. */
10633 if (token->keyword == RID_TYPENAME)
10634 {
10635 /* Remember that we've seen it. */
10636 typename_p = true;
10637 /* Consume the `typename' token. */
10638 cp_lexer_consume_token (parser->lexer);
10639 }
10640 }
10641
10642 /* Look for the optional global scope qualification. */
10643 global_scope_p
10644 = (cp_parser_global_scope_opt (parser,
10645 /*current_scope_valid_p=*/false)
10646 != NULL_TREE);
10647
10648 /* If we saw `typename', or didn't see `::', then there must be a
10649 nested-name-specifier present. */
10650 if (typename_p || !global_scope_p)
10651 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10652 /*check_dependency_p=*/true,
10653 /*type_p=*/false,
10654 /*is_declaration=*/true);
10655 /* Otherwise, we could be in either of the two productions. In that
10656 case, treat the nested-name-specifier as optional. */
10657 else
10658 qscope = cp_parser_nested_name_specifier_opt (parser,
10659 /*typename_keyword_p=*/false,
10660 /*check_dependency_p=*/true,
10661 /*type_p=*/false,
10662 /*is_declaration=*/true);
10663 if (!qscope)
10664 qscope = global_namespace;
10665
10666 /* Parse the unqualified-id. */
10667 identifier = cp_parser_unqualified_id (parser,
10668 /*template_keyword_p=*/false,
10669 /*check_dependency_p=*/true,
10670 /*declarator_p=*/true,
10671 /*optional_p=*/false);
10672
10673 if (access_declaration_p)
10674 {
10675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10676 cp_parser_simulate_error (parser);
10677 if (!cp_parser_parse_definitely (parser))
10678 return false;
10679 }
10680
10681 /* The function we call to handle a using-declaration is different
10682 depending on what scope we are in. */
10683 if (qscope == error_mark_node || identifier == error_mark_node)
10684 ;
10685 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10686 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10687 /* [namespace.udecl]
10688
10689 A using declaration shall not name a template-id. */
10690 error ("a template-id may not appear in a using-declaration");
10691 else
10692 {
10693 if (at_class_scope_p ())
10694 {
10695 /* Create the USING_DECL. */
10696 decl = do_class_using_decl (parser->scope, identifier);
10697 /* Add it to the list of members in this class. */
10698 finish_member_declaration (decl);
10699 }
10700 else
10701 {
10702 decl = cp_parser_lookup_name_simple (parser, identifier);
10703 if (decl == error_mark_node)
10704 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10705 else if (!at_namespace_scope_p ())
10706 do_local_using_decl (decl, qscope, identifier);
10707 else
10708 do_toplevel_using_decl (decl, qscope, identifier);
10709 }
10710 }
10711
10712 /* Look for the final `;'. */
10713 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10714
10715 return true;
10716 }
10717
10718 /* Parse a using-directive.
10719
10720 using-directive:
10721 using namespace :: [opt] nested-name-specifier [opt]
10722 namespace-name ; */
10723
10724 static void
10725 cp_parser_using_directive (cp_parser* parser)
10726 {
10727 tree namespace_decl;
10728 tree attribs;
10729
10730 /* Look for the `using' keyword. */
10731 cp_parser_require_keyword (parser, RID_USING, "`using'");
10732 /* And the `namespace' keyword. */
10733 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10734 /* Look for the optional `::' operator. */
10735 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10736 /* And the optional nested-name-specifier. */
10737 cp_parser_nested_name_specifier_opt (parser,
10738 /*typename_keyword_p=*/false,
10739 /*check_dependency_p=*/true,
10740 /*type_p=*/false,
10741 /*is_declaration=*/true);
10742 /* Get the namespace being used. */
10743 namespace_decl = cp_parser_namespace_name (parser);
10744 /* And any specified attributes. */
10745 attribs = cp_parser_attributes_opt (parser);
10746 /* Update the symbol table. */
10747 parse_using_directive (namespace_decl, attribs);
10748 /* Look for the final `;'. */
10749 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10750 }
10751
10752 /* Parse an asm-definition.
10753
10754 asm-definition:
10755 asm ( string-literal ) ;
10756
10757 GNU Extension:
10758
10759 asm-definition:
10760 asm volatile [opt] ( string-literal ) ;
10761 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10762 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10763 : asm-operand-list [opt] ) ;
10764 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10765 : asm-operand-list [opt]
10766 : asm-operand-list [opt] ) ; */
10767
10768 static void
10769 cp_parser_asm_definition (cp_parser* parser)
10770 {
10771 tree string;
10772 tree outputs = NULL_TREE;
10773 tree inputs = NULL_TREE;
10774 tree clobbers = NULL_TREE;
10775 tree asm_stmt;
10776 bool volatile_p = false;
10777 bool extended_p = false;
10778
10779 /* Look for the `asm' keyword. */
10780 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10781 /* See if the next token is `volatile'. */
10782 if (cp_parser_allow_gnu_extensions_p (parser)
10783 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10784 {
10785 /* Remember that we saw the `volatile' keyword. */
10786 volatile_p = true;
10787 /* Consume the token. */
10788 cp_lexer_consume_token (parser->lexer);
10789 }
10790 /* Look for the opening `('. */
10791 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10792 return;
10793 /* Look for the string. */
10794 string = cp_parser_string_literal (parser, false, false);
10795 if (string == error_mark_node)
10796 {
10797 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10798 /*consume_paren=*/true);
10799 return;
10800 }
10801
10802 /* If we're allowing GNU extensions, check for the extended assembly
10803 syntax. Unfortunately, the `:' tokens need not be separated by
10804 a space in C, and so, for compatibility, we tolerate that here
10805 too. Doing that means that we have to treat the `::' operator as
10806 two `:' tokens. */
10807 if (cp_parser_allow_gnu_extensions_p (parser)
10808 && at_function_scope_p ()
10809 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10810 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10811 {
10812 bool inputs_p = false;
10813 bool clobbers_p = false;
10814
10815 /* The extended syntax was used. */
10816 extended_p = true;
10817
10818 /* Look for outputs. */
10819 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10820 {
10821 /* Consume the `:'. */
10822 cp_lexer_consume_token (parser->lexer);
10823 /* Parse the output-operands. */
10824 if (cp_lexer_next_token_is_not (parser->lexer,
10825 CPP_COLON)
10826 && cp_lexer_next_token_is_not (parser->lexer,
10827 CPP_SCOPE)
10828 && cp_lexer_next_token_is_not (parser->lexer,
10829 CPP_CLOSE_PAREN))
10830 outputs = cp_parser_asm_operand_list (parser);
10831 }
10832 /* If the next token is `::', there are no outputs, and the
10833 next token is the beginning of the inputs. */
10834 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10835 /* The inputs are coming next. */
10836 inputs_p = true;
10837
10838 /* Look for inputs. */
10839 if (inputs_p
10840 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10841 {
10842 /* Consume the `:' or `::'. */
10843 cp_lexer_consume_token (parser->lexer);
10844 /* Parse the output-operands. */
10845 if (cp_lexer_next_token_is_not (parser->lexer,
10846 CPP_COLON)
10847 && cp_lexer_next_token_is_not (parser->lexer,
10848 CPP_CLOSE_PAREN))
10849 inputs = cp_parser_asm_operand_list (parser);
10850 }
10851 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10852 /* The clobbers are coming next. */
10853 clobbers_p = true;
10854
10855 /* Look for clobbers. */
10856 if (clobbers_p
10857 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10858 {
10859 /* Consume the `:' or `::'. */
10860 cp_lexer_consume_token (parser->lexer);
10861 /* Parse the clobbers. */
10862 if (cp_lexer_next_token_is_not (parser->lexer,
10863 CPP_CLOSE_PAREN))
10864 clobbers = cp_parser_asm_clobber_list (parser);
10865 }
10866 }
10867 /* Look for the closing `)'. */
10868 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10869 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10870 /*consume_paren=*/true);
10871 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10872
10873 /* Create the ASM_EXPR. */
10874 if (at_function_scope_p ())
10875 {
10876 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10877 inputs, clobbers);
10878 /* If the extended syntax was not used, mark the ASM_EXPR. */
10879 if (!extended_p)
10880 {
10881 tree temp = asm_stmt;
10882 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10883 temp = TREE_OPERAND (temp, 0);
10884
10885 ASM_INPUT_P (temp) = 1;
10886 }
10887 }
10888 else
10889 cgraph_add_asm_node (string);
10890 }
10891
10892 /* Declarators [gram.dcl.decl] */
10893
10894 /* Parse an init-declarator.
10895
10896 init-declarator:
10897 declarator initializer [opt]
10898
10899 GNU Extension:
10900
10901 init-declarator:
10902 declarator asm-specification [opt] attributes [opt] initializer [opt]
10903
10904 function-definition:
10905 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10906 function-body
10907 decl-specifier-seq [opt] declarator function-try-block
10908
10909 GNU Extension:
10910
10911 function-definition:
10912 __extension__ function-definition
10913
10914 The DECL_SPECIFIERS apply to this declarator. Returns a
10915 representation of the entity declared. If MEMBER_P is TRUE, then
10916 this declarator appears in a class scope. The new DECL created by
10917 this declarator is returned.
10918
10919 The CHECKS are access checks that should be performed once we know
10920 what entity is being declared (and, therefore, what classes have
10921 befriended it).
10922
10923 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10924 for a function-definition here as well. If the declarator is a
10925 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10926 be TRUE upon return. By that point, the function-definition will
10927 have been completely parsed.
10928
10929 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10930 is FALSE. */
10931
10932 static tree
10933 cp_parser_init_declarator (cp_parser* parser,
10934 cp_decl_specifier_seq *decl_specifiers,
10935 tree checks,
10936 bool function_definition_allowed_p,
10937 bool member_p,
10938 int declares_class_or_enum,
10939 bool* function_definition_p)
10940 {
10941 cp_token *token;
10942 cp_declarator *declarator;
10943 tree prefix_attributes;
10944 tree attributes;
10945 tree asm_specification;
10946 tree initializer;
10947 tree decl = NULL_TREE;
10948 tree scope;
10949 bool is_initialized;
10950 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10951 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10952 "(...)". */
10953 enum cpp_ttype initialization_kind;
10954 bool is_parenthesized_init = false;
10955 bool is_non_constant_init;
10956 int ctor_dtor_or_conv_p;
10957 bool friend_p;
10958 tree pushed_scope = NULL;
10959
10960 /* Gather the attributes that were provided with the
10961 decl-specifiers. */
10962 prefix_attributes = decl_specifiers->attributes;
10963
10964 /* Assume that this is not the declarator for a function
10965 definition. */
10966 if (function_definition_p)
10967 *function_definition_p = false;
10968
10969 /* Defer access checks while parsing the declarator; we cannot know
10970 what names are accessible until we know what is being
10971 declared. */
10972 resume_deferring_access_checks ();
10973
10974 /* Parse the declarator. */
10975 declarator
10976 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10977 &ctor_dtor_or_conv_p,
10978 /*parenthesized_p=*/NULL,
10979 /*member_p=*/false);
10980 /* Gather up the deferred checks. */
10981 stop_deferring_access_checks ();
10982
10983 /* If the DECLARATOR was erroneous, there's no need to go
10984 further. */
10985 if (declarator == cp_error_declarator)
10986 return error_mark_node;
10987
10988 if (declares_class_or_enum & 2)
10989 cp_parser_check_for_definition_in_return_type (declarator,
10990 decl_specifiers->type);
10991
10992 /* Figure out what scope the entity declared by the DECLARATOR is
10993 located in. `grokdeclarator' sometimes changes the scope, so
10994 we compute it now. */
10995 scope = get_scope_of_declarator (declarator);
10996
10997 /* If we're allowing GNU extensions, look for an asm-specification
10998 and attributes. */
10999 if (cp_parser_allow_gnu_extensions_p (parser))
11000 {
11001 /* Look for an asm-specification. */
11002 asm_specification = cp_parser_asm_specification_opt (parser);
11003 /* And attributes. */
11004 attributes = cp_parser_attributes_opt (parser);
11005 }
11006 else
11007 {
11008 asm_specification = NULL_TREE;
11009 attributes = NULL_TREE;
11010 }
11011
11012 /* Peek at the next token. */
11013 token = cp_lexer_peek_token (parser->lexer);
11014 /* Check to see if the token indicates the start of a
11015 function-definition. */
11016 if (cp_parser_token_starts_function_definition_p (token))
11017 {
11018 if (!function_definition_allowed_p)
11019 {
11020 /* If a function-definition should not appear here, issue an
11021 error message. */
11022 cp_parser_error (parser,
11023 "a function-definition is not allowed here");
11024 return error_mark_node;
11025 }
11026 else
11027 {
11028 /* Neither attributes nor an asm-specification are allowed
11029 on a function-definition. */
11030 if (asm_specification)
11031 error ("an asm-specification is not allowed on a function-definition");
11032 if (attributes)
11033 error ("attributes are not allowed on a function-definition");
11034 /* This is a function-definition. */
11035 *function_definition_p = true;
11036
11037 /* Parse the function definition. */
11038 if (member_p)
11039 decl = cp_parser_save_member_function_body (parser,
11040 decl_specifiers,
11041 declarator,
11042 prefix_attributes);
11043 else
11044 decl
11045 = (cp_parser_function_definition_from_specifiers_and_declarator
11046 (parser, decl_specifiers, prefix_attributes, declarator));
11047
11048 return decl;
11049 }
11050 }
11051
11052 /* [dcl.dcl]
11053
11054 Only in function declarations for constructors, destructors, and
11055 type conversions can the decl-specifier-seq be omitted.
11056
11057 We explicitly postpone this check past the point where we handle
11058 function-definitions because we tolerate function-definitions
11059 that are missing their return types in some modes. */
11060 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11061 {
11062 cp_parser_error (parser,
11063 "expected constructor, destructor, or type conversion");
11064 return error_mark_node;
11065 }
11066
11067 /* An `=' or an `(' indicates an initializer. */
11068 if (token->type == CPP_EQ
11069 || token->type == CPP_OPEN_PAREN)
11070 {
11071 is_initialized = true;
11072 initialization_kind = token->type;
11073 }
11074 else
11075 {
11076 /* If the init-declarator isn't initialized and isn't followed by a
11077 `,' or `;', it's not a valid init-declarator. */
11078 if (token->type != CPP_COMMA
11079 && token->type != CPP_SEMICOLON)
11080 {
11081 cp_parser_error (parser, "expected initializer");
11082 return error_mark_node;
11083 }
11084 is_initialized = false;
11085 initialization_kind = CPP_EOF;
11086 }
11087
11088 /* Because start_decl has side-effects, we should only call it if we
11089 know we're going ahead. By this point, we know that we cannot
11090 possibly be looking at any other construct. */
11091 cp_parser_commit_to_tentative_parse (parser);
11092
11093 /* If the decl specifiers were bad, issue an error now that we're
11094 sure this was intended to be a declarator. Then continue
11095 declaring the variable(s), as int, to try to cut down on further
11096 errors. */
11097 if (decl_specifiers->any_specifiers_p
11098 && decl_specifiers->type == error_mark_node)
11099 {
11100 cp_parser_error (parser, "invalid type in declaration");
11101 decl_specifiers->type = integer_type_node;
11102 }
11103
11104 /* Check to see whether or not this declaration is a friend. */
11105 friend_p = cp_parser_friend_p (decl_specifiers);
11106
11107 /* Check that the number of template-parameter-lists is OK. */
11108 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11109 return error_mark_node;
11110
11111 /* Enter the newly declared entry in the symbol table. If we're
11112 processing a declaration in a class-specifier, we wait until
11113 after processing the initializer. */
11114 if (!member_p)
11115 {
11116 if (parser->in_unbraced_linkage_specification_p)
11117 decl_specifiers->storage_class = sc_extern;
11118 decl = start_decl (declarator, decl_specifiers,
11119 is_initialized, attributes, prefix_attributes,
11120 &pushed_scope);
11121 }
11122 else if (scope)
11123 /* Enter the SCOPE. That way unqualified names appearing in the
11124 initializer will be looked up in SCOPE. */
11125 pushed_scope = push_scope (scope);
11126
11127 /* Perform deferred access control checks, now that we know in which
11128 SCOPE the declared entity resides. */
11129 if (!member_p && decl)
11130 {
11131 tree saved_current_function_decl = NULL_TREE;
11132
11133 /* If the entity being declared is a function, pretend that we
11134 are in its scope. If it is a `friend', it may have access to
11135 things that would not otherwise be accessible. */
11136 if (TREE_CODE (decl) == FUNCTION_DECL)
11137 {
11138 saved_current_function_decl = current_function_decl;
11139 current_function_decl = decl;
11140 }
11141
11142 /* Perform access checks for template parameters. */
11143 cp_parser_perform_template_parameter_access_checks (checks);
11144
11145 /* Perform the access control checks for the declarator and the
11146 the decl-specifiers. */
11147 perform_deferred_access_checks ();
11148
11149 /* Restore the saved value. */
11150 if (TREE_CODE (decl) == FUNCTION_DECL)
11151 current_function_decl = saved_current_function_decl;
11152 }
11153
11154 /* Parse the initializer. */
11155 initializer = NULL_TREE;
11156 is_parenthesized_init = false;
11157 is_non_constant_init = true;
11158 if (is_initialized)
11159 {
11160 if (declarator->kind == cdk_function
11161 && declarator->declarator->kind == cdk_id
11162 && initialization_kind == CPP_EQ)
11163 initializer = cp_parser_pure_specifier (parser);
11164 else
11165 initializer = cp_parser_initializer (parser,
11166 &is_parenthesized_init,
11167 &is_non_constant_init);
11168 }
11169
11170 /* The old parser allows attributes to appear after a parenthesized
11171 initializer. Mark Mitchell proposed removing this functionality
11172 on the GCC mailing lists on 2002-08-13. This parser accepts the
11173 attributes -- but ignores them. */
11174 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11175 if (cp_parser_attributes_opt (parser))
11176 warning (OPT_Wattributes,
11177 "attributes after parenthesized initializer ignored");
11178
11179 /* For an in-class declaration, use `grokfield' to create the
11180 declaration. */
11181 if (member_p)
11182 {
11183 if (pushed_scope)
11184 {
11185 pop_scope (pushed_scope);
11186 pushed_scope = false;
11187 }
11188 decl = grokfield (declarator, decl_specifiers,
11189 initializer, !is_non_constant_init,
11190 /*asmspec=*/NULL_TREE,
11191 prefix_attributes);
11192 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11193 cp_parser_save_default_args (parser, decl);
11194 }
11195
11196 /* Finish processing the declaration. But, skip friend
11197 declarations. */
11198 if (!friend_p && decl && decl != error_mark_node)
11199 {
11200 cp_finish_decl (decl,
11201 initializer, !is_non_constant_init,
11202 asm_specification,
11203 /* If the initializer is in parentheses, then this is
11204 a direct-initialization, which means that an
11205 `explicit' constructor is OK. Otherwise, an
11206 `explicit' constructor cannot be used. */
11207 ((is_parenthesized_init || !is_initialized)
11208 ? 0 : LOOKUP_ONLYCONVERTING));
11209 }
11210 if (!friend_p && pushed_scope)
11211 pop_scope (pushed_scope);
11212
11213 return decl;
11214 }
11215
11216 /* Parse a declarator.
11217
11218 declarator:
11219 direct-declarator
11220 ptr-operator declarator
11221
11222 abstract-declarator:
11223 ptr-operator abstract-declarator [opt]
11224 direct-abstract-declarator
11225
11226 GNU Extensions:
11227
11228 declarator:
11229 attributes [opt] direct-declarator
11230 attributes [opt] ptr-operator declarator
11231
11232 abstract-declarator:
11233 attributes [opt] ptr-operator abstract-declarator [opt]
11234 attributes [opt] direct-abstract-declarator
11235
11236 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11237 detect constructor, destructor or conversion operators. It is set
11238 to -1 if the declarator is a name, and +1 if it is a
11239 function. Otherwise it is set to zero. Usually you just want to
11240 test for >0, but internally the negative value is used.
11241
11242 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11243 a decl-specifier-seq unless it declares a constructor, destructor,
11244 or conversion. It might seem that we could check this condition in
11245 semantic analysis, rather than parsing, but that makes it difficult
11246 to handle something like `f()'. We want to notice that there are
11247 no decl-specifiers, and therefore realize that this is an
11248 expression, not a declaration.)
11249
11250 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11251 the declarator is a direct-declarator of the form "(...)".
11252
11253 MEMBER_P is true iff this declarator is a member-declarator. */
11254
11255 static cp_declarator *
11256 cp_parser_declarator (cp_parser* parser,
11257 cp_parser_declarator_kind dcl_kind,
11258 int* ctor_dtor_or_conv_p,
11259 bool* parenthesized_p,
11260 bool member_p)
11261 {
11262 cp_token *token;
11263 cp_declarator *declarator;
11264 enum tree_code code;
11265 cp_cv_quals cv_quals;
11266 tree class_type;
11267 tree attributes = NULL_TREE;
11268
11269 /* Assume this is not a constructor, destructor, or type-conversion
11270 operator. */
11271 if (ctor_dtor_or_conv_p)
11272 *ctor_dtor_or_conv_p = 0;
11273
11274 if (cp_parser_allow_gnu_extensions_p (parser))
11275 attributes = cp_parser_attributes_opt (parser);
11276
11277 /* Peek at the next token. */
11278 token = cp_lexer_peek_token (parser->lexer);
11279
11280 /* Check for the ptr-operator production. */
11281 cp_parser_parse_tentatively (parser);
11282 /* Parse the ptr-operator. */
11283 code = cp_parser_ptr_operator (parser,
11284 &class_type,
11285 &cv_quals);
11286 /* If that worked, then we have a ptr-operator. */
11287 if (cp_parser_parse_definitely (parser))
11288 {
11289 /* If a ptr-operator was found, then this declarator was not
11290 parenthesized. */
11291 if (parenthesized_p)
11292 *parenthesized_p = true;
11293 /* The dependent declarator is optional if we are parsing an
11294 abstract-declarator. */
11295 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11296 cp_parser_parse_tentatively (parser);
11297
11298 /* Parse the dependent declarator. */
11299 declarator = cp_parser_declarator (parser, dcl_kind,
11300 /*ctor_dtor_or_conv_p=*/NULL,
11301 /*parenthesized_p=*/NULL,
11302 /*member_p=*/false);
11303
11304 /* If we are parsing an abstract-declarator, we must handle the
11305 case where the dependent declarator is absent. */
11306 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11307 && !cp_parser_parse_definitely (parser))
11308 declarator = NULL;
11309
11310 /* Build the representation of the ptr-operator. */
11311 if (class_type)
11312 declarator = make_ptrmem_declarator (cv_quals,
11313 class_type,
11314 declarator);
11315 else if (code == INDIRECT_REF)
11316 declarator = make_pointer_declarator (cv_quals, declarator);
11317 else
11318 declarator = make_reference_declarator (cv_quals, declarator);
11319 }
11320 /* Everything else is a direct-declarator. */
11321 else
11322 {
11323 if (parenthesized_p)
11324 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11325 CPP_OPEN_PAREN);
11326 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11327 ctor_dtor_or_conv_p,
11328 member_p);
11329 }
11330
11331 if (attributes && declarator && declarator != cp_error_declarator)
11332 declarator->attributes = attributes;
11333
11334 return declarator;
11335 }
11336
11337 /* Parse a direct-declarator or direct-abstract-declarator.
11338
11339 direct-declarator:
11340 declarator-id
11341 direct-declarator ( parameter-declaration-clause )
11342 cv-qualifier-seq [opt]
11343 exception-specification [opt]
11344 direct-declarator [ constant-expression [opt] ]
11345 ( declarator )
11346
11347 direct-abstract-declarator:
11348 direct-abstract-declarator [opt]
11349 ( parameter-declaration-clause )
11350 cv-qualifier-seq [opt]
11351 exception-specification [opt]
11352 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11353 ( abstract-declarator )
11354
11355 Returns a representation of the declarator. DCL_KIND is
11356 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11357 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11358 we are parsing a direct-declarator. It is
11359 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11360 of ambiguity we prefer an abstract declarator, as per
11361 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11362 cp_parser_declarator. */
11363
11364 static cp_declarator *
11365 cp_parser_direct_declarator (cp_parser* parser,
11366 cp_parser_declarator_kind dcl_kind,
11367 int* ctor_dtor_or_conv_p,
11368 bool member_p)
11369 {
11370 cp_token *token;
11371 cp_declarator *declarator = NULL;
11372 tree scope = NULL_TREE;
11373 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11374 bool saved_in_declarator_p = parser->in_declarator_p;
11375 bool first = true;
11376 tree pushed_scope = NULL_TREE;
11377
11378 while (true)
11379 {
11380 /* Peek at the next token. */
11381 token = cp_lexer_peek_token (parser->lexer);
11382 if (token->type == CPP_OPEN_PAREN)
11383 {
11384 /* This is either a parameter-declaration-clause, or a
11385 parenthesized declarator. When we know we are parsing a
11386 named declarator, it must be a parenthesized declarator
11387 if FIRST is true. For instance, `(int)' is a
11388 parameter-declaration-clause, with an omitted
11389 direct-abstract-declarator. But `((*))', is a
11390 parenthesized abstract declarator. Finally, when T is a
11391 template parameter `(T)' is a
11392 parameter-declaration-clause, and not a parenthesized
11393 named declarator.
11394
11395 We first try and parse a parameter-declaration-clause,
11396 and then try a nested declarator (if FIRST is true).
11397
11398 It is not an error for it not to be a
11399 parameter-declaration-clause, even when FIRST is
11400 false. Consider,
11401
11402 int i (int);
11403 int i (3);
11404
11405 The first is the declaration of a function while the
11406 second is a the definition of a variable, including its
11407 initializer.
11408
11409 Having seen only the parenthesis, we cannot know which of
11410 these two alternatives should be selected. Even more
11411 complex are examples like:
11412
11413 int i (int (a));
11414 int i (int (3));
11415
11416 The former is a function-declaration; the latter is a
11417 variable initialization.
11418
11419 Thus again, we try a parameter-declaration-clause, and if
11420 that fails, we back out and return. */
11421
11422 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11423 {
11424 cp_parameter_declarator *params;
11425 unsigned saved_num_template_parameter_lists;
11426
11427 /* In a member-declarator, the only valid interpretation
11428 of a parenthesis is the start of a
11429 parameter-declaration-clause. (It is invalid to
11430 initialize a static data member with a parenthesized
11431 initializer; only the "=" form of initialization is
11432 permitted.) */
11433 if (!member_p)
11434 cp_parser_parse_tentatively (parser);
11435
11436 /* Consume the `('. */
11437 cp_lexer_consume_token (parser->lexer);
11438 if (first)
11439 {
11440 /* If this is going to be an abstract declarator, we're
11441 in a declarator and we can't have default args. */
11442 parser->default_arg_ok_p = false;
11443 parser->in_declarator_p = true;
11444 }
11445
11446 /* Inside the function parameter list, surrounding
11447 template-parameter-lists do not apply. */
11448 saved_num_template_parameter_lists
11449 = parser->num_template_parameter_lists;
11450 parser->num_template_parameter_lists = 0;
11451
11452 /* Parse the parameter-declaration-clause. */
11453 params = cp_parser_parameter_declaration_clause (parser);
11454
11455 parser->num_template_parameter_lists
11456 = saved_num_template_parameter_lists;
11457
11458 /* If all went well, parse the cv-qualifier-seq and the
11459 exception-specification. */
11460 if (member_p || cp_parser_parse_definitely (parser))
11461 {
11462 cp_cv_quals cv_quals;
11463 tree exception_specification;
11464
11465 if (ctor_dtor_or_conv_p)
11466 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11467 first = false;
11468 /* Consume the `)'. */
11469 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11470
11471 /* Parse the cv-qualifier-seq. */
11472 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11473 /* And the exception-specification. */
11474 exception_specification
11475 = cp_parser_exception_specification_opt (parser);
11476
11477 /* Create the function-declarator. */
11478 declarator = make_call_declarator (declarator,
11479 params,
11480 cv_quals,
11481 exception_specification);
11482 /* Any subsequent parameter lists are to do with
11483 return type, so are not those of the declared
11484 function. */
11485 parser->default_arg_ok_p = false;
11486
11487 /* Repeat the main loop. */
11488 continue;
11489 }
11490 }
11491
11492 /* If this is the first, we can try a parenthesized
11493 declarator. */
11494 if (first)
11495 {
11496 bool saved_in_type_id_in_expr_p;
11497
11498 parser->default_arg_ok_p = saved_default_arg_ok_p;
11499 parser->in_declarator_p = saved_in_declarator_p;
11500
11501 /* Consume the `('. */
11502 cp_lexer_consume_token (parser->lexer);
11503 /* Parse the nested declarator. */
11504 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11505 parser->in_type_id_in_expr_p = true;
11506 declarator
11507 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11508 /*parenthesized_p=*/NULL,
11509 member_p);
11510 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11511 first = false;
11512 /* Expect a `)'. */
11513 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11514 declarator = cp_error_declarator;
11515 if (declarator == cp_error_declarator)
11516 break;
11517
11518 goto handle_declarator;
11519 }
11520 /* Otherwise, we must be done. */
11521 else
11522 break;
11523 }
11524 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11525 && token->type == CPP_OPEN_SQUARE)
11526 {
11527 /* Parse an array-declarator. */
11528 tree bounds;
11529
11530 if (ctor_dtor_or_conv_p)
11531 *ctor_dtor_or_conv_p = 0;
11532
11533 first = false;
11534 parser->default_arg_ok_p = false;
11535 parser->in_declarator_p = true;
11536 /* Consume the `['. */
11537 cp_lexer_consume_token (parser->lexer);
11538 /* Peek at the next token. */
11539 token = cp_lexer_peek_token (parser->lexer);
11540 /* If the next token is `]', then there is no
11541 constant-expression. */
11542 if (token->type != CPP_CLOSE_SQUARE)
11543 {
11544 bool non_constant_p;
11545
11546 bounds
11547 = cp_parser_constant_expression (parser,
11548 /*allow_non_constant=*/true,
11549 &non_constant_p);
11550 if (!non_constant_p)
11551 bounds = fold_non_dependent_expr (bounds);
11552 /* Normally, the array bound must be an integral constant
11553 expression. However, as an extension, we allow VLAs
11554 in function scopes. */
11555 else if (!at_function_scope_p ())
11556 {
11557 error ("array bound is not an integer constant");
11558 bounds = error_mark_node;
11559 }
11560 }
11561 else
11562 bounds = NULL_TREE;
11563 /* Look for the closing `]'. */
11564 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11565 {
11566 declarator = cp_error_declarator;
11567 break;
11568 }
11569
11570 declarator = make_array_declarator (declarator, bounds);
11571 }
11572 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11573 {
11574 tree qualifying_scope;
11575 tree unqualified_name;
11576 special_function_kind sfk;
11577 bool abstract_ok;
11578
11579 /* Parse a declarator-id */
11580 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11581 if (abstract_ok)
11582 cp_parser_parse_tentatively (parser);
11583 unqualified_name
11584 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11585 qualifying_scope = parser->scope;
11586 if (abstract_ok)
11587 {
11588 if (!cp_parser_parse_definitely (parser))
11589 unqualified_name = error_mark_node;
11590 else if (unqualified_name
11591 && (qualifying_scope
11592 || (TREE_CODE (unqualified_name)
11593 != IDENTIFIER_NODE)))
11594 {
11595 cp_parser_error (parser, "expected unqualified-id");
11596 unqualified_name = error_mark_node;
11597 }
11598 }
11599
11600 if (!unqualified_name)
11601 return NULL;
11602 if (unqualified_name == error_mark_node)
11603 {
11604 declarator = cp_error_declarator;
11605 break;
11606 }
11607
11608 if (qualifying_scope && at_namespace_scope_p ()
11609 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11610 {
11611 /* In the declaration of a member of a template class
11612 outside of the class itself, the SCOPE will sometimes
11613 be a TYPENAME_TYPE. For example, given:
11614
11615 template <typename T>
11616 int S<T>::R::i = 3;
11617
11618 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11619 this context, we must resolve S<T>::R to an ordinary
11620 type, rather than a typename type.
11621
11622 The reason we normally avoid resolving TYPENAME_TYPEs
11623 is that a specialization of `S' might render
11624 `S<T>::R' not a type. However, if `S' is
11625 specialized, then this `i' will not be used, so there
11626 is no harm in resolving the types here. */
11627 tree type;
11628
11629 /* Resolve the TYPENAME_TYPE. */
11630 type = resolve_typename_type (qualifying_scope,
11631 /*only_current_p=*/false);
11632 /* If that failed, the declarator is invalid. */
11633 if (type == error_mark_node)
11634 error ("%<%T::%D%> is not a type",
11635 TYPE_CONTEXT (qualifying_scope),
11636 TYPE_IDENTIFIER (qualifying_scope));
11637 qualifying_scope = type;
11638 }
11639
11640 sfk = sfk_none;
11641 if (unqualified_name)
11642 {
11643 tree class_type;
11644
11645 if (qualifying_scope
11646 && CLASS_TYPE_P (qualifying_scope))
11647 class_type = qualifying_scope;
11648 else
11649 class_type = current_class_type;
11650
11651 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11652 {
11653 tree name_type = TREE_TYPE (unqualified_name);
11654 if (class_type && same_type_p (name_type, class_type))
11655 {
11656 if (qualifying_scope
11657 && CLASSTYPE_USE_TEMPLATE (name_type))
11658 {
11659 error ("invalid use of constructor as a template");
11660 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11661 "name the constructor in a qualified name",
11662 class_type,
11663 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11664 class_type, name_type);
11665 declarator = cp_error_declarator;
11666 break;
11667 }
11668 else
11669 unqualified_name = constructor_name (class_type);
11670 }
11671 else
11672 {
11673 /* We do not attempt to print the declarator
11674 here because we do not have enough
11675 information about its original syntactic
11676 form. */
11677 cp_parser_error (parser, "invalid declarator");
11678 declarator = cp_error_declarator;
11679 break;
11680 }
11681 }
11682
11683 if (class_type)
11684 {
11685 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11686 sfk = sfk_destructor;
11687 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11688 sfk = sfk_conversion;
11689 else if (/* There's no way to declare a constructor
11690 for an anonymous type, even if the type
11691 got a name for linkage purposes. */
11692 !TYPE_WAS_ANONYMOUS (class_type)
11693 && constructor_name_p (unqualified_name,
11694 class_type))
11695 {
11696 unqualified_name = constructor_name (class_type);
11697 sfk = sfk_constructor;
11698 }
11699
11700 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11701 *ctor_dtor_or_conv_p = -1;
11702 }
11703 }
11704 declarator = make_id_declarator (qualifying_scope,
11705 unqualified_name,
11706 sfk);
11707 declarator->id_loc = token->location;
11708
11709 handle_declarator:;
11710 scope = get_scope_of_declarator (declarator);
11711 if (scope)
11712 /* Any names that appear after the declarator-id for a
11713 member are looked up in the containing scope. */
11714 pushed_scope = push_scope (scope);
11715 parser->in_declarator_p = true;
11716 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11717 || (declarator && declarator->kind == cdk_id))
11718 /* Default args are only allowed on function
11719 declarations. */
11720 parser->default_arg_ok_p = saved_default_arg_ok_p;
11721 else
11722 parser->default_arg_ok_p = false;
11723
11724 first = false;
11725 }
11726 /* We're done. */
11727 else
11728 break;
11729 }
11730
11731 /* For an abstract declarator, we might wind up with nothing at this
11732 point. That's an error; the declarator is not optional. */
11733 if (!declarator)
11734 cp_parser_error (parser, "expected declarator");
11735
11736 /* If we entered a scope, we must exit it now. */
11737 if (pushed_scope)
11738 pop_scope (pushed_scope);
11739
11740 parser->default_arg_ok_p = saved_default_arg_ok_p;
11741 parser->in_declarator_p = saved_in_declarator_p;
11742
11743 return declarator;
11744 }
11745
11746 /* Parse a ptr-operator.
11747
11748 ptr-operator:
11749 * cv-qualifier-seq [opt]
11750 &
11751 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11752
11753 GNU Extension:
11754
11755 ptr-operator:
11756 & cv-qualifier-seq [opt]
11757
11758 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11759 Returns ADDR_EXPR if a reference was used. In the case of a
11760 pointer-to-member, *TYPE is filled in with the TYPE containing the
11761 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11762 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11763 ERROR_MARK if an error occurred. */
11764
11765 static enum tree_code
11766 cp_parser_ptr_operator (cp_parser* parser,
11767 tree* type,
11768 cp_cv_quals *cv_quals)
11769 {
11770 enum tree_code code = ERROR_MARK;
11771 cp_token *token;
11772
11773 /* Assume that it's not a pointer-to-member. */
11774 *type = NULL_TREE;
11775 /* And that there are no cv-qualifiers. */
11776 *cv_quals = TYPE_UNQUALIFIED;
11777
11778 /* Peek at the next token. */
11779 token = cp_lexer_peek_token (parser->lexer);
11780 /* If it's a `*' or `&' we have a pointer or reference. */
11781 if (token->type == CPP_MULT || token->type == CPP_AND)
11782 {
11783 /* Remember which ptr-operator we were processing. */
11784 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11785
11786 /* Consume the `*' or `&'. */
11787 cp_lexer_consume_token (parser->lexer);
11788
11789 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11790 `&', if we are allowing GNU extensions. (The only qualifier
11791 that can legally appear after `&' is `restrict', but that is
11792 enforced during semantic analysis. */
11793 if (code == INDIRECT_REF
11794 || cp_parser_allow_gnu_extensions_p (parser))
11795 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11796 }
11797 else
11798 {
11799 /* Try the pointer-to-member case. */
11800 cp_parser_parse_tentatively (parser);
11801 /* Look for the optional `::' operator. */
11802 cp_parser_global_scope_opt (parser,
11803 /*current_scope_valid_p=*/false);
11804 /* Look for the nested-name specifier. */
11805 cp_parser_nested_name_specifier (parser,
11806 /*typename_keyword_p=*/false,
11807 /*check_dependency_p=*/true,
11808 /*type_p=*/false,
11809 /*is_declaration=*/false);
11810 /* If we found it, and the next token is a `*', then we are
11811 indeed looking at a pointer-to-member operator. */
11812 if (!cp_parser_error_occurred (parser)
11813 && cp_parser_require (parser, CPP_MULT, "`*'"))
11814 {
11815 /* Indicate that the `*' operator was used. */
11816 code = INDIRECT_REF;
11817
11818 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11819 error ("%qD is a namespace", parser->scope);
11820 else
11821 {
11822 /* The type of which the member is a member is given by the
11823 current SCOPE. */
11824 *type = parser->scope;
11825 /* The next name will not be qualified. */
11826 parser->scope = NULL_TREE;
11827 parser->qualifying_scope = NULL_TREE;
11828 parser->object_scope = NULL_TREE;
11829 /* Look for the optional cv-qualifier-seq. */
11830 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11831 }
11832 }
11833 /* If that didn't work we don't have a ptr-operator. */
11834 if (!cp_parser_parse_definitely (parser))
11835 cp_parser_error (parser, "expected ptr-operator");
11836 }
11837
11838 return code;
11839 }
11840
11841 /* Parse an (optional) cv-qualifier-seq.
11842
11843 cv-qualifier-seq:
11844 cv-qualifier cv-qualifier-seq [opt]
11845
11846 cv-qualifier:
11847 const
11848 volatile
11849
11850 GNU Extension:
11851
11852 cv-qualifier:
11853 __restrict__
11854
11855 Returns a bitmask representing the cv-qualifiers. */
11856
11857 static cp_cv_quals
11858 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11859 {
11860 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11861
11862 while (true)
11863 {
11864 cp_token *token;
11865 cp_cv_quals cv_qualifier;
11866
11867 /* Peek at the next token. */
11868 token = cp_lexer_peek_token (parser->lexer);
11869 /* See if it's a cv-qualifier. */
11870 switch (token->keyword)
11871 {
11872 case RID_CONST:
11873 cv_qualifier = TYPE_QUAL_CONST;
11874 break;
11875
11876 case RID_VOLATILE:
11877 cv_qualifier = TYPE_QUAL_VOLATILE;
11878 break;
11879
11880 case RID_RESTRICT:
11881 cv_qualifier = TYPE_QUAL_RESTRICT;
11882 break;
11883
11884 default:
11885 cv_qualifier = TYPE_UNQUALIFIED;
11886 break;
11887 }
11888
11889 if (!cv_qualifier)
11890 break;
11891
11892 if (cv_quals & cv_qualifier)
11893 {
11894 error ("duplicate cv-qualifier");
11895 cp_lexer_purge_token (parser->lexer);
11896 }
11897 else
11898 {
11899 cp_lexer_consume_token (parser->lexer);
11900 cv_quals |= cv_qualifier;
11901 }
11902 }
11903
11904 return cv_quals;
11905 }
11906
11907 /* Parse a declarator-id.
11908
11909 declarator-id:
11910 id-expression
11911 :: [opt] nested-name-specifier [opt] type-name
11912
11913 In the `id-expression' case, the value returned is as for
11914 cp_parser_id_expression if the id-expression was an unqualified-id.
11915 If the id-expression was a qualified-id, then a SCOPE_REF is
11916 returned. The first operand is the scope (either a NAMESPACE_DECL
11917 or TREE_TYPE), but the second is still just a representation of an
11918 unqualified-id. */
11919
11920 static tree
11921 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11922 {
11923 tree id;
11924 /* The expression must be an id-expression. Assume that qualified
11925 names are the names of types so that:
11926
11927 template <class T>
11928 int S<T>::R::i = 3;
11929
11930 will work; we must treat `S<T>::R' as the name of a type.
11931 Similarly, assume that qualified names are templates, where
11932 required, so that:
11933
11934 template <class T>
11935 int S<T>::R<T>::i = 3;
11936
11937 will work, too. */
11938 id = cp_parser_id_expression (parser,
11939 /*template_keyword_p=*/false,
11940 /*check_dependency_p=*/false,
11941 /*template_p=*/NULL,
11942 /*declarator_p=*/true,
11943 optional_p);
11944 if (id && BASELINK_P (id))
11945 id = BASELINK_FUNCTIONS (id);
11946 return id;
11947 }
11948
11949 /* Parse a type-id.
11950
11951 type-id:
11952 type-specifier-seq abstract-declarator [opt]
11953
11954 Returns the TYPE specified. */
11955
11956 static tree
11957 cp_parser_type_id (cp_parser* parser)
11958 {
11959 cp_decl_specifier_seq type_specifier_seq;
11960 cp_declarator *abstract_declarator;
11961
11962 /* Parse the type-specifier-seq. */
11963 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11964 &type_specifier_seq);
11965 if (type_specifier_seq.type == error_mark_node)
11966 return error_mark_node;
11967
11968 /* There might or might not be an abstract declarator. */
11969 cp_parser_parse_tentatively (parser);
11970 /* Look for the declarator. */
11971 abstract_declarator
11972 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11973 /*parenthesized_p=*/NULL,
11974 /*member_p=*/false);
11975 /* Check to see if there really was a declarator. */
11976 if (!cp_parser_parse_definitely (parser))
11977 abstract_declarator = NULL;
11978
11979 return groktypename (&type_specifier_seq, abstract_declarator);
11980 }
11981
11982 /* Parse a type-specifier-seq.
11983
11984 type-specifier-seq:
11985 type-specifier type-specifier-seq [opt]
11986
11987 GNU extension:
11988
11989 type-specifier-seq:
11990 attributes type-specifier-seq [opt]
11991
11992 If IS_CONDITION is true, we are at the start of a "condition",
11993 e.g., we've just seen "if (".
11994
11995 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11996
11997 static void
11998 cp_parser_type_specifier_seq (cp_parser* parser,
11999 bool is_condition,
12000 cp_decl_specifier_seq *type_specifier_seq)
12001 {
12002 bool seen_type_specifier = false;
12003 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12004
12005 /* Clear the TYPE_SPECIFIER_SEQ. */
12006 clear_decl_specs (type_specifier_seq);
12007
12008 /* Parse the type-specifiers and attributes. */
12009 while (true)
12010 {
12011 tree type_specifier;
12012 bool is_cv_qualifier;
12013
12014 /* Check for attributes first. */
12015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12016 {
12017 type_specifier_seq->attributes =
12018 chainon (type_specifier_seq->attributes,
12019 cp_parser_attributes_opt (parser));
12020 continue;
12021 }
12022
12023 /* Look for the type-specifier. */
12024 type_specifier = cp_parser_type_specifier (parser,
12025 flags,
12026 type_specifier_seq,
12027 /*is_declaration=*/false,
12028 NULL,
12029 &is_cv_qualifier);
12030 if (!type_specifier)
12031 {
12032 /* If the first type-specifier could not be found, this is not a
12033 type-specifier-seq at all. */
12034 if (!seen_type_specifier)
12035 {
12036 cp_parser_error (parser, "expected type-specifier");
12037 type_specifier_seq->type = error_mark_node;
12038 return;
12039 }
12040 /* If subsequent type-specifiers could not be found, the
12041 type-specifier-seq is complete. */
12042 break;
12043 }
12044
12045 seen_type_specifier = true;
12046 /* The standard says that a condition can be:
12047
12048 type-specifier-seq declarator = assignment-expression
12049
12050 However, given:
12051
12052 struct S {};
12053 if (int S = ...)
12054
12055 we should treat the "S" as a declarator, not as a
12056 type-specifier. The standard doesn't say that explicitly for
12057 type-specifier-seq, but it does say that for
12058 decl-specifier-seq in an ordinary declaration. Perhaps it
12059 would be clearer just to allow a decl-specifier-seq here, and
12060 then add a semantic restriction that if any decl-specifiers
12061 that are not type-specifiers appear, the program is invalid. */
12062 if (is_condition && !is_cv_qualifier)
12063 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12064 }
12065
12066 cp_parser_check_decl_spec (type_specifier_seq);
12067 }
12068
12069 /* Parse a parameter-declaration-clause.
12070
12071 parameter-declaration-clause:
12072 parameter-declaration-list [opt] ... [opt]
12073 parameter-declaration-list , ...
12074
12075 Returns a representation for the parameter declarations. A return
12076 value of NULL indicates a parameter-declaration-clause consisting
12077 only of an ellipsis. */
12078
12079 static cp_parameter_declarator *
12080 cp_parser_parameter_declaration_clause (cp_parser* parser)
12081 {
12082 cp_parameter_declarator *parameters;
12083 cp_token *token;
12084 bool ellipsis_p;
12085 bool is_error;
12086
12087 /* Peek at the next token. */
12088 token = cp_lexer_peek_token (parser->lexer);
12089 /* Check for trivial parameter-declaration-clauses. */
12090 if (token->type == CPP_ELLIPSIS)
12091 {
12092 /* Consume the `...' token. */
12093 cp_lexer_consume_token (parser->lexer);
12094 return NULL;
12095 }
12096 else if (token->type == CPP_CLOSE_PAREN)
12097 /* There are no parameters. */
12098 {
12099 #ifndef NO_IMPLICIT_EXTERN_C
12100 if (in_system_header && current_class_type == NULL
12101 && current_lang_name == lang_name_c)
12102 return NULL;
12103 else
12104 #endif
12105 return no_parameters;
12106 }
12107 /* Check for `(void)', too, which is a special case. */
12108 else if (token->keyword == RID_VOID
12109 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12110 == CPP_CLOSE_PAREN))
12111 {
12112 /* Consume the `void' token. */
12113 cp_lexer_consume_token (parser->lexer);
12114 /* There are no parameters. */
12115 return no_parameters;
12116 }
12117
12118 /* Parse the parameter-declaration-list. */
12119 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12120 /* If a parse error occurred while parsing the
12121 parameter-declaration-list, then the entire
12122 parameter-declaration-clause is erroneous. */
12123 if (is_error)
12124 return NULL;
12125
12126 /* Peek at the next token. */
12127 token = cp_lexer_peek_token (parser->lexer);
12128 /* If it's a `,', the clause should terminate with an ellipsis. */
12129 if (token->type == CPP_COMMA)
12130 {
12131 /* Consume the `,'. */
12132 cp_lexer_consume_token (parser->lexer);
12133 /* Expect an ellipsis. */
12134 ellipsis_p
12135 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12136 }
12137 /* It might also be `...' if the optional trailing `,' was
12138 omitted. */
12139 else if (token->type == CPP_ELLIPSIS)
12140 {
12141 /* Consume the `...' token. */
12142 cp_lexer_consume_token (parser->lexer);
12143 /* And remember that we saw it. */
12144 ellipsis_p = true;
12145 }
12146 else
12147 ellipsis_p = false;
12148
12149 /* Finish the parameter list. */
12150 if (parameters && ellipsis_p)
12151 parameters->ellipsis_p = true;
12152
12153 return parameters;
12154 }
12155
12156 /* Parse a parameter-declaration-list.
12157
12158 parameter-declaration-list:
12159 parameter-declaration
12160 parameter-declaration-list , parameter-declaration
12161
12162 Returns a representation of the parameter-declaration-list, as for
12163 cp_parser_parameter_declaration_clause. However, the
12164 `void_list_node' is never appended to the list. Upon return,
12165 *IS_ERROR will be true iff an error occurred. */
12166
12167 static cp_parameter_declarator *
12168 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12169 {
12170 cp_parameter_declarator *parameters = NULL;
12171 cp_parameter_declarator **tail = &parameters;
12172 bool saved_in_unbraced_linkage_specification_p;
12173
12174 /* Assume all will go well. */
12175 *is_error = false;
12176 /* The special considerations that apply to a function within an
12177 unbraced linkage specifications do not apply to the parameters
12178 to the function. */
12179 saved_in_unbraced_linkage_specification_p
12180 = parser->in_unbraced_linkage_specification_p;
12181 parser->in_unbraced_linkage_specification_p = false;
12182
12183 /* Look for more parameters. */
12184 while (true)
12185 {
12186 cp_parameter_declarator *parameter;
12187 bool parenthesized_p;
12188 /* Parse the parameter. */
12189 parameter
12190 = cp_parser_parameter_declaration (parser,
12191 /*template_parm_p=*/false,
12192 &parenthesized_p);
12193
12194 /* If a parse error occurred parsing the parameter declaration,
12195 then the entire parameter-declaration-list is erroneous. */
12196 if (!parameter)
12197 {
12198 *is_error = true;
12199 parameters = NULL;
12200 break;
12201 }
12202 /* Add the new parameter to the list. */
12203 *tail = parameter;
12204 tail = &parameter->next;
12205
12206 /* Peek at the next token. */
12207 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12208 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12209 /* These are for Objective-C++ */
12210 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12211 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12212 /* The parameter-declaration-list is complete. */
12213 break;
12214 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12215 {
12216 cp_token *token;
12217
12218 /* Peek at the next token. */
12219 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12220 /* If it's an ellipsis, then the list is complete. */
12221 if (token->type == CPP_ELLIPSIS)
12222 break;
12223 /* Otherwise, there must be more parameters. Consume the
12224 `,'. */
12225 cp_lexer_consume_token (parser->lexer);
12226 /* When parsing something like:
12227
12228 int i(float f, double d)
12229
12230 we can tell after seeing the declaration for "f" that we
12231 are not looking at an initialization of a variable "i",
12232 but rather at the declaration of a function "i".
12233
12234 Due to the fact that the parsing of template arguments
12235 (as specified to a template-id) requires backtracking we
12236 cannot use this technique when inside a template argument
12237 list. */
12238 if (!parser->in_template_argument_list_p
12239 && !parser->in_type_id_in_expr_p
12240 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12241 /* However, a parameter-declaration of the form
12242 "foat(f)" (which is a valid declaration of a
12243 parameter "f") can also be interpreted as an
12244 expression (the conversion of "f" to "float"). */
12245 && !parenthesized_p)
12246 cp_parser_commit_to_tentative_parse (parser);
12247 }
12248 else
12249 {
12250 cp_parser_error (parser, "expected %<,%> or %<...%>");
12251 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12252 cp_parser_skip_to_closing_parenthesis (parser,
12253 /*recovering=*/true,
12254 /*or_comma=*/false,
12255 /*consume_paren=*/false);
12256 break;
12257 }
12258 }
12259
12260 parser->in_unbraced_linkage_specification_p
12261 = saved_in_unbraced_linkage_specification_p;
12262
12263 return parameters;
12264 }
12265
12266 /* Parse a parameter declaration.
12267
12268 parameter-declaration:
12269 decl-specifier-seq declarator
12270 decl-specifier-seq declarator = assignment-expression
12271 decl-specifier-seq abstract-declarator [opt]
12272 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12273
12274 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12275 declares a template parameter. (In that case, a non-nested `>'
12276 token encountered during the parsing of the assignment-expression
12277 is not interpreted as a greater-than operator.)
12278
12279 Returns a representation of the parameter, or NULL if an error
12280 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12281 true iff the declarator is of the form "(p)". */
12282
12283 static cp_parameter_declarator *
12284 cp_parser_parameter_declaration (cp_parser *parser,
12285 bool template_parm_p,
12286 bool *parenthesized_p)
12287 {
12288 int declares_class_or_enum;
12289 bool greater_than_is_operator_p;
12290 cp_decl_specifier_seq decl_specifiers;
12291 cp_declarator *declarator;
12292 tree default_argument;
12293 cp_token *token;
12294 const char *saved_message;
12295
12296 /* In a template parameter, `>' is not an operator.
12297
12298 [temp.param]
12299
12300 When parsing a default template-argument for a non-type
12301 template-parameter, the first non-nested `>' is taken as the end
12302 of the template parameter-list rather than a greater-than
12303 operator. */
12304 greater_than_is_operator_p = !template_parm_p;
12305
12306 /* Type definitions may not appear in parameter types. */
12307 saved_message = parser->type_definition_forbidden_message;
12308 parser->type_definition_forbidden_message
12309 = "types may not be defined in parameter types";
12310
12311 /* Parse the declaration-specifiers. */
12312 cp_parser_decl_specifier_seq (parser,
12313 CP_PARSER_FLAGS_NONE,
12314 &decl_specifiers,
12315 &declares_class_or_enum);
12316 /* If an error occurred, there's no reason to attempt to parse the
12317 rest of the declaration. */
12318 if (cp_parser_error_occurred (parser))
12319 {
12320 parser->type_definition_forbidden_message = saved_message;
12321 return NULL;
12322 }
12323
12324 /* Peek at the next token. */
12325 token = cp_lexer_peek_token (parser->lexer);
12326 /* If the next token is a `)', `,', `=', `>', or `...', then there
12327 is no declarator. */
12328 if (token->type == CPP_CLOSE_PAREN
12329 || token->type == CPP_COMMA
12330 || token->type == CPP_EQ
12331 || token->type == CPP_ELLIPSIS
12332 || token->type == CPP_GREATER)
12333 {
12334 declarator = NULL;
12335 if (parenthesized_p)
12336 *parenthesized_p = false;
12337 }
12338 /* Otherwise, there should be a declarator. */
12339 else
12340 {
12341 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12342 parser->default_arg_ok_p = false;
12343
12344 /* After seeing a decl-specifier-seq, if the next token is not a
12345 "(", there is no possibility that the code is a valid
12346 expression. Therefore, if parsing tentatively, we commit at
12347 this point. */
12348 if (!parser->in_template_argument_list_p
12349 /* In an expression context, having seen:
12350
12351 (int((char ...
12352
12353 we cannot be sure whether we are looking at a
12354 function-type (taking a "char" as a parameter) or a cast
12355 of some object of type "char" to "int". */
12356 && !parser->in_type_id_in_expr_p
12357 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12358 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12359 cp_parser_commit_to_tentative_parse (parser);
12360 /* Parse the declarator. */
12361 declarator = cp_parser_declarator (parser,
12362 CP_PARSER_DECLARATOR_EITHER,
12363 /*ctor_dtor_or_conv_p=*/NULL,
12364 parenthesized_p,
12365 /*member_p=*/false);
12366 parser->default_arg_ok_p = saved_default_arg_ok_p;
12367 /* After the declarator, allow more attributes. */
12368 decl_specifiers.attributes
12369 = chainon (decl_specifiers.attributes,
12370 cp_parser_attributes_opt (parser));
12371 }
12372
12373 /* The restriction on defining new types applies only to the type
12374 of the parameter, not to the default argument. */
12375 parser->type_definition_forbidden_message = saved_message;
12376
12377 /* If the next token is `=', then process a default argument. */
12378 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12379 {
12380 bool saved_greater_than_is_operator_p;
12381 /* Consume the `='. */
12382 cp_lexer_consume_token (parser->lexer);
12383
12384 /* If we are defining a class, then the tokens that make up the
12385 default argument must be saved and processed later. */
12386 if (!template_parm_p && at_class_scope_p ()
12387 && TYPE_BEING_DEFINED (current_class_type))
12388 {
12389 unsigned depth = 0;
12390 cp_token *first_token;
12391 cp_token *token;
12392
12393 /* Add tokens until we have processed the entire default
12394 argument. We add the range [first_token, token). */
12395 first_token = cp_lexer_peek_token (parser->lexer);
12396 while (true)
12397 {
12398 bool done = false;
12399
12400 /* Peek at the next token. */
12401 token = cp_lexer_peek_token (parser->lexer);
12402 /* What we do depends on what token we have. */
12403 switch (token->type)
12404 {
12405 /* In valid code, a default argument must be
12406 immediately followed by a `,' `)', or `...'. */
12407 case CPP_COMMA:
12408 case CPP_CLOSE_PAREN:
12409 case CPP_ELLIPSIS:
12410 /* If we run into a non-nested `;', `}', or `]',
12411 then the code is invalid -- but the default
12412 argument is certainly over. */
12413 case CPP_SEMICOLON:
12414 case CPP_CLOSE_BRACE:
12415 case CPP_CLOSE_SQUARE:
12416 if (depth == 0)
12417 done = true;
12418 /* Update DEPTH, if necessary. */
12419 else if (token->type == CPP_CLOSE_PAREN
12420 || token->type == CPP_CLOSE_BRACE
12421 || token->type == CPP_CLOSE_SQUARE)
12422 --depth;
12423 break;
12424
12425 case CPP_OPEN_PAREN:
12426 case CPP_OPEN_SQUARE:
12427 case CPP_OPEN_BRACE:
12428 ++depth;
12429 break;
12430
12431 case CPP_GREATER:
12432 /* If we see a non-nested `>', and `>' is not an
12433 operator, then it marks the end of the default
12434 argument. */
12435 if (!depth && !greater_than_is_operator_p)
12436 done = true;
12437 break;
12438
12439 /* If we run out of tokens, issue an error message. */
12440 case CPP_EOF:
12441 case CPP_PRAGMA_EOL:
12442 error ("file ends in default argument");
12443 done = true;
12444 break;
12445
12446 case CPP_NAME:
12447 case CPP_SCOPE:
12448 /* In these cases, we should look for template-ids.
12449 For example, if the default argument is
12450 `X<int, double>()', we need to do name lookup to
12451 figure out whether or not `X' is a template; if
12452 so, the `,' does not end the default argument.
12453
12454 That is not yet done. */
12455 break;
12456
12457 default:
12458 break;
12459 }
12460
12461 /* If we've reached the end, stop. */
12462 if (done)
12463 break;
12464
12465 /* Add the token to the token block. */
12466 token = cp_lexer_consume_token (parser->lexer);
12467 }
12468
12469 /* Create a DEFAULT_ARG to represented the unparsed default
12470 argument. */
12471 default_argument = make_node (DEFAULT_ARG);
12472 DEFARG_TOKENS (default_argument)
12473 = cp_token_cache_new (first_token, token);
12474 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12475 }
12476 /* Outside of a class definition, we can just parse the
12477 assignment-expression. */
12478 else
12479 {
12480 bool saved_local_variables_forbidden_p;
12481
12482 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12483 set correctly. */
12484 saved_greater_than_is_operator_p
12485 = parser->greater_than_is_operator_p;
12486 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12487 /* Local variable names (and the `this' keyword) may not
12488 appear in a default argument. */
12489 saved_local_variables_forbidden_p
12490 = parser->local_variables_forbidden_p;
12491 parser->local_variables_forbidden_p = true;
12492 /* The default argument expression may cause implicitly
12493 defined member functions to be synthesized, which will
12494 result in garbage collection. We must treat this
12495 situation as if we were within the body of function so as
12496 to avoid collecting live data on the stack. */
12497 ++function_depth;
12498 /* Parse the assignment-expression. */
12499 if (template_parm_p)
12500 push_deferring_access_checks (dk_no_deferred);
12501 default_argument
12502 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12503 if (template_parm_p)
12504 pop_deferring_access_checks ();
12505 /* Restore saved state. */
12506 --function_depth;
12507 parser->greater_than_is_operator_p
12508 = saved_greater_than_is_operator_p;
12509 parser->local_variables_forbidden_p
12510 = saved_local_variables_forbidden_p;
12511 }
12512 if (!parser->default_arg_ok_p)
12513 {
12514 if (!flag_pedantic_errors)
12515 warning (0, "deprecated use of default argument for parameter of non-function");
12516 else
12517 {
12518 error ("default arguments are only permitted for function parameters");
12519 default_argument = NULL_TREE;
12520 }
12521 }
12522 }
12523 else
12524 default_argument = NULL_TREE;
12525
12526 return make_parameter_declarator (&decl_specifiers,
12527 declarator,
12528 default_argument);
12529 }
12530
12531 /* Parse a function-body.
12532
12533 function-body:
12534 compound_statement */
12535
12536 static void
12537 cp_parser_function_body (cp_parser *parser)
12538 {
12539 cp_parser_compound_statement (parser, NULL, false);
12540 }
12541
12542 /* Parse a ctor-initializer-opt followed by a function-body. Return
12543 true if a ctor-initializer was present. */
12544
12545 static bool
12546 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12547 {
12548 tree body;
12549 bool ctor_initializer_p;
12550
12551 /* Begin the function body. */
12552 body = begin_function_body ();
12553 /* Parse the optional ctor-initializer. */
12554 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12555 /* Parse the function-body. */
12556 cp_parser_function_body (parser);
12557 /* Finish the function body. */
12558 finish_function_body (body);
12559
12560 return ctor_initializer_p;
12561 }
12562
12563 /* Parse an initializer.
12564
12565 initializer:
12566 = initializer-clause
12567 ( expression-list )
12568
12569 Returns an expression representing the initializer. If no
12570 initializer is present, NULL_TREE is returned.
12571
12572 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12573 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12574 set to FALSE if there is no initializer present. If there is an
12575 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12576 is set to true; otherwise it is set to false. */
12577
12578 static tree
12579 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12580 bool* non_constant_p)
12581 {
12582 cp_token *token;
12583 tree init;
12584
12585 /* Peek at the next token. */
12586 token = cp_lexer_peek_token (parser->lexer);
12587
12588 /* Let our caller know whether or not this initializer was
12589 parenthesized. */
12590 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12591 /* Assume that the initializer is constant. */
12592 *non_constant_p = false;
12593
12594 if (token->type == CPP_EQ)
12595 {
12596 /* Consume the `='. */
12597 cp_lexer_consume_token (parser->lexer);
12598 /* Parse the initializer-clause. */
12599 init = cp_parser_initializer_clause (parser, non_constant_p);
12600 }
12601 else if (token->type == CPP_OPEN_PAREN)
12602 init = cp_parser_parenthesized_expression_list (parser, false,
12603 /*cast_p=*/false,
12604 non_constant_p);
12605 else
12606 {
12607 /* Anything else is an error. */
12608 cp_parser_error (parser, "expected initializer");
12609 init = error_mark_node;
12610 }
12611
12612 return init;
12613 }
12614
12615 /* Parse an initializer-clause.
12616
12617 initializer-clause:
12618 assignment-expression
12619 { initializer-list , [opt] }
12620 { }
12621
12622 Returns an expression representing the initializer.
12623
12624 If the `assignment-expression' production is used the value
12625 returned is simply a representation for the expression.
12626
12627 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12628 the elements of the initializer-list (or NULL, if the last
12629 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12630 NULL_TREE. There is no way to detect whether or not the optional
12631 trailing `,' was provided. NON_CONSTANT_P is as for
12632 cp_parser_initializer. */
12633
12634 static tree
12635 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12636 {
12637 tree initializer;
12638
12639 /* Assume the expression is constant. */
12640 *non_constant_p = false;
12641
12642 /* If it is not a `{', then we are looking at an
12643 assignment-expression. */
12644 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12645 {
12646 initializer
12647 = cp_parser_constant_expression (parser,
12648 /*allow_non_constant_p=*/true,
12649 non_constant_p);
12650 if (!*non_constant_p)
12651 initializer = fold_non_dependent_expr (initializer);
12652 }
12653 else
12654 {
12655 /* Consume the `{' token. */
12656 cp_lexer_consume_token (parser->lexer);
12657 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12658 initializer = make_node (CONSTRUCTOR);
12659 /* If it's not a `}', then there is a non-trivial initializer. */
12660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12661 {
12662 /* Parse the initializer list. */
12663 CONSTRUCTOR_ELTS (initializer)
12664 = cp_parser_initializer_list (parser, non_constant_p);
12665 /* A trailing `,' token is allowed. */
12666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12667 cp_lexer_consume_token (parser->lexer);
12668 }
12669 /* Now, there should be a trailing `}'. */
12670 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12671 }
12672
12673 return initializer;
12674 }
12675
12676 /* Parse an initializer-list.
12677
12678 initializer-list:
12679 initializer-clause
12680 initializer-list , initializer-clause
12681
12682 GNU Extension:
12683
12684 initializer-list:
12685 identifier : initializer-clause
12686 initializer-list, identifier : initializer-clause
12687
12688 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12689 for the initializer. If the INDEX of the elt is non-NULL, it is the
12690 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12691 as for cp_parser_initializer. */
12692
12693 static VEC(constructor_elt,gc) *
12694 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12695 {
12696 VEC(constructor_elt,gc) *v = NULL;
12697
12698 /* Assume all of the expressions are constant. */
12699 *non_constant_p = false;
12700
12701 /* Parse the rest of the list. */
12702 while (true)
12703 {
12704 cp_token *token;
12705 tree identifier;
12706 tree initializer;
12707 bool clause_non_constant_p;
12708
12709 /* If the next token is an identifier and the following one is a
12710 colon, we are looking at the GNU designated-initializer
12711 syntax. */
12712 if (cp_parser_allow_gnu_extensions_p (parser)
12713 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12714 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12715 {
12716 /* Consume the identifier. */
12717 identifier = cp_lexer_consume_token (parser->lexer)->value;
12718 /* Consume the `:'. */
12719 cp_lexer_consume_token (parser->lexer);
12720 }
12721 else
12722 identifier = NULL_TREE;
12723
12724 /* Parse the initializer. */
12725 initializer = cp_parser_initializer_clause (parser,
12726 &clause_non_constant_p);
12727 /* If any clause is non-constant, so is the entire initializer. */
12728 if (clause_non_constant_p)
12729 *non_constant_p = true;
12730
12731 /* Add it to the vector. */
12732 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12733
12734 /* If the next token is not a comma, we have reached the end of
12735 the list. */
12736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12737 break;
12738
12739 /* Peek at the next token. */
12740 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12741 /* If the next token is a `}', then we're still done. An
12742 initializer-clause can have a trailing `,' after the
12743 initializer-list and before the closing `}'. */
12744 if (token->type == CPP_CLOSE_BRACE)
12745 break;
12746
12747 /* Consume the `,' token. */
12748 cp_lexer_consume_token (parser->lexer);
12749 }
12750
12751 return v;
12752 }
12753
12754 /* Classes [gram.class] */
12755
12756 /* Parse a class-name.
12757
12758 class-name:
12759 identifier
12760 template-id
12761
12762 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12763 to indicate that names looked up in dependent types should be
12764 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12765 keyword has been used to indicate that the name that appears next
12766 is a template. TAG_TYPE indicates the explicit tag given before
12767 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12768 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12769 is the class being defined in a class-head.
12770
12771 Returns the TYPE_DECL representing the class. */
12772
12773 static tree
12774 cp_parser_class_name (cp_parser *parser,
12775 bool typename_keyword_p,
12776 bool template_keyword_p,
12777 enum tag_types tag_type,
12778 bool check_dependency_p,
12779 bool class_head_p,
12780 bool is_declaration)
12781 {
12782 tree decl;
12783 tree scope;
12784 bool typename_p;
12785 cp_token *token;
12786
12787 /* All class-names start with an identifier. */
12788 token = cp_lexer_peek_token (parser->lexer);
12789 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12790 {
12791 cp_parser_error (parser, "expected class-name");
12792 return error_mark_node;
12793 }
12794
12795 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12796 to a template-id, so we save it here. */
12797 scope = parser->scope;
12798 if (scope == error_mark_node)
12799 return error_mark_node;
12800
12801 /* Any name names a type if we're following the `typename' keyword
12802 in a qualified name where the enclosing scope is type-dependent. */
12803 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12804 && dependent_type_p (scope));
12805 /* Handle the common case (an identifier, but not a template-id)
12806 efficiently. */
12807 if (token->type == CPP_NAME
12808 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12809 {
12810 cp_token *identifier_token;
12811 tree identifier;
12812 bool ambiguous_p;
12813
12814 /* Look for the identifier. */
12815 identifier_token = cp_lexer_peek_token (parser->lexer);
12816 ambiguous_p = identifier_token->ambiguous_p;
12817 identifier = cp_parser_identifier (parser);
12818 /* If the next token isn't an identifier, we are certainly not
12819 looking at a class-name. */
12820 if (identifier == error_mark_node)
12821 decl = error_mark_node;
12822 /* If we know this is a type-name, there's no need to look it
12823 up. */
12824 else if (typename_p)
12825 decl = identifier;
12826 else
12827 {
12828 tree ambiguous_decls;
12829 /* If we already know that this lookup is ambiguous, then
12830 we've already issued an error message; there's no reason
12831 to check again. */
12832 if (ambiguous_p)
12833 {
12834 cp_parser_simulate_error (parser);
12835 return error_mark_node;
12836 }
12837 /* If the next token is a `::', then the name must be a type
12838 name.
12839
12840 [basic.lookup.qual]
12841
12842 During the lookup for a name preceding the :: scope
12843 resolution operator, object, function, and enumerator
12844 names are ignored. */
12845 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12846 tag_type = typename_type;
12847 /* Look up the name. */
12848 decl = cp_parser_lookup_name (parser, identifier,
12849 tag_type,
12850 /*is_template=*/false,
12851 /*is_namespace=*/false,
12852 check_dependency_p,
12853 &ambiguous_decls);
12854 if (ambiguous_decls)
12855 {
12856 error ("reference to %qD is ambiguous", identifier);
12857 print_candidates (ambiguous_decls);
12858 if (cp_parser_parsing_tentatively (parser))
12859 {
12860 identifier_token->ambiguous_p = true;
12861 cp_parser_simulate_error (parser);
12862 }
12863 return error_mark_node;
12864 }
12865 }
12866 }
12867 else
12868 {
12869 /* Try a template-id. */
12870 decl = cp_parser_template_id (parser, template_keyword_p,
12871 check_dependency_p,
12872 is_declaration);
12873 if (decl == error_mark_node)
12874 return error_mark_node;
12875 }
12876
12877 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12878
12879 /* If this is a typename, create a TYPENAME_TYPE. */
12880 if (typename_p && decl != error_mark_node)
12881 {
12882 decl = make_typename_type (scope, decl, typename_type,
12883 /*complain=*/tf_error);
12884 if (decl != error_mark_node)
12885 decl = TYPE_NAME (decl);
12886 }
12887
12888 /* Check to see that it is really the name of a class. */
12889 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12890 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12891 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12892 /* Situations like this:
12893
12894 template <typename T> struct A {
12895 typename T::template X<int>::I i;
12896 };
12897
12898 are problematic. Is `T::template X<int>' a class-name? The
12899 standard does not seem to be definitive, but there is no other
12900 valid interpretation of the following `::'. Therefore, those
12901 names are considered class-names. */
12902 {
12903 decl = make_typename_type (scope, decl, tag_type, tf_error);
12904 if (decl != error_mark_node)
12905 decl = TYPE_NAME (decl);
12906 }
12907 else if (TREE_CODE (decl) != TYPE_DECL
12908 || TREE_TYPE (decl) == error_mark_node
12909 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12910 decl = error_mark_node;
12911
12912 if (decl == error_mark_node)
12913 cp_parser_error (parser, "expected class-name");
12914
12915 return decl;
12916 }
12917
12918 /* Parse a class-specifier.
12919
12920 class-specifier:
12921 class-head { member-specification [opt] }
12922
12923 Returns the TREE_TYPE representing the class. */
12924
12925 static tree
12926 cp_parser_class_specifier (cp_parser* parser)
12927 {
12928 cp_token *token;
12929 tree type;
12930 tree attributes = NULL_TREE;
12931 int has_trailing_semicolon;
12932 bool nested_name_specifier_p;
12933 unsigned saved_num_template_parameter_lists;
12934 tree old_scope = NULL_TREE;
12935 tree scope = NULL_TREE;
12936
12937 push_deferring_access_checks (dk_no_deferred);
12938
12939 /* Parse the class-head. */
12940 type = cp_parser_class_head (parser,
12941 &nested_name_specifier_p,
12942 &attributes);
12943 /* If the class-head was a semantic disaster, skip the entire body
12944 of the class. */
12945 if (!type)
12946 {
12947 cp_parser_skip_to_end_of_block_or_statement (parser);
12948 pop_deferring_access_checks ();
12949 return error_mark_node;
12950 }
12951
12952 /* Look for the `{'. */
12953 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12954 {
12955 pop_deferring_access_checks ();
12956 return error_mark_node;
12957 }
12958
12959 /* Issue an error message if type-definitions are forbidden here. */
12960 cp_parser_check_type_definition (parser);
12961 /* Remember that we are defining one more class. */
12962 ++parser->num_classes_being_defined;
12963 /* Inside the class, surrounding template-parameter-lists do not
12964 apply. */
12965 saved_num_template_parameter_lists
12966 = parser->num_template_parameter_lists;
12967 parser->num_template_parameter_lists = 0;
12968
12969 /* Start the class. */
12970 if (nested_name_specifier_p)
12971 {
12972 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12973 old_scope = push_inner_scope (scope);
12974 }
12975 type = begin_class_definition (type, attributes);
12976
12977 if (type == error_mark_node)
12978 /* If the type is erroneous, skip the entire body of the class. */
12979 cp_parser_skip_to_closing_brace (parser);
12980 else
12981 /* Parse the member-specification. */
12982 cp_parser_member_specification_opt (parser);
12983
12984 /* Look for the trailing `}'. */
12985 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12986 /* We get better error messages by noticing a common problem: a
12987 missing trailing `;'. */
12988 token = cp_lexer_peek_token (parser->lexer);
12989 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12990 /* Look for trailing attributes to apply to this class. */
12991 if (cp_parser_allow_gnu_extensions_p (parser))
12992 attributes = cp_parser_attributes_opt (parser);
12993 if (type != error_mark_node)
12994 type = finish_struct (type, attributes);
12995 if (nested_name_specifier_p)
12996 pop_inner_scope (old_scope, scope);
12997 /* If this class is not itself within the scope of another class,
12998 then we need to parse the bodies of all of the queued function
12999 definitions. Note that the queued functions defined in a class
13000 are not always processed immediately following the
13001 class-specifier for that class. Consider:
13002
13003 struct A {
13004 struct B { void f() { sizeof (A); } };
13005 };
13006
13007 If `f' were processed before the processing of `A' were
13008 completed, there would be no way to compute the size of `A'.
13009 Note that the nesting we are interested in here is lexical --
13010 not the semantic nesting given by TYPE_CONTEXT. In particular,
13011 for:
13012
13013 struct A { struct B; };
13014 struct A::B { void f() { } };
13015
13016 there is no need to delay the parsing of `A::B::f'. */
13017 if (--parser->num_classes_being_defined == 0)
13018 {
13019 tree queue_entry;
13020 tree fn;
13021 tree class_type = NULL_TREE;
13022 tree pushed_scope = NULL_TREE;
13023
13024 /* In a first pass, parse default arguments to the functions.
13025 Then, in a second pass, parse the bodies of the functions.
13026 This two-phased approach handles cases like:
13027
13028 struct S {
13029 void f() { g(); }
13030 void g(int i = 3);
13031 };
13032
13033 */
13034 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13035 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13036 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13037 TREE_PURPOSE (parser->unparsed_functions_queues)
13038 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13039 {
13040 fn = TREE_VALUE (queue_entry);
13041 /* If there are default arguments that have not yet been processed,
13042 take care of them now. */
13043 if (class_type != TREE_PURPOSE (queue_entry))
13044 {
13045 if (pushed_scope)
13046 pop_scope (pushed_scope);
13047 class_type = TREE_PURPOSE (queue_entry);
13048 pushed_scope = push_scope (class_type);
13049 }
13050 /* Make sure that any template parameters are in scope. */
13051 maybe_begin_member_template_processing (fn);
13052 /* Parse the default argument expressions. */
13053 cp_parser_late_parsing_default_args (parser, fn);
13054 /* Remove any template parameters from the symbol table. */
13055 maybe_end_member_template_processing ();
13056 }
13057 if (pushed_scope)
13058 pop_scope (pushed_scope);
13059 /* Now parse the body of the functions. */
13060 for (TREE_VALUE (parser->unparsed_functions_queues)
13061 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13062 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13063 TREE_VALUE (parser->unparsed_functions_queues)
13064 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13065 {
13066 /* Figure out which function we need to process. */
13067 fn = TREE_VALUE (queue_entry);
13068 /* Parse the function. */
13069 cp_parser_late_parsing_for_member (parser, fn);
13070 }
13071 }
13072
13073 /* Put back any saved access checks. */
13074 pop_deferring_access_checks ();
13075
13076 /* Restore the count of active template-parameter-lists. */
13077 parser->num_template_parameter_lists
13078 = saved_num_template_parameter_lists;
13079
13080 return type;
13081 }
13082
13083 /* Parse a class-head.
13084
13085 class-head:
13086 class-key identifier [opt] base-clause [opt]
13087 class-key nested-name-specifier identifier base-clause [opt]
13088 class-key nested-name-specifier [opt] template-id
13089 base-clause [opt]
13090
13091 GNU Extensions:
13092 class-key attributes identifier [opt] base-clause [opt]
13093 class-key attributes nested-name-specifier identifier base-clause [opt]
13094 class-key attributes nested-name-specifier [opt] template-id
13095 base-clause [opt]
13096
13097 Returns the TYPE of the indicated class. Sets
13098 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13099 involving a nested-name-specifier was used, and FALSE otherwise.
13100
13101 Returns error_mark_node if this is not a class-head.
13102
13103 Returns NULL_TREE if the class-head is syntactically valid, but
13104 semantically invalid in a way that means we should skip the entire
13105 body of the class. */
13106
13107 static tree
13108 cp_parser_class_head (cp_parser* parser,
13109 bool* nested_name_specifier_p,
13110 tree *attributes_p)
13111 {
13112 tree nested_name_specifier;
13113 enum tag_types class_key;
13114 tree id = NULL_TREE;
13115 tree type = NULL_TREE;
13116 tree attributes;
13117 bool template_id_p = false;
13118 bool qualified_p = false;
13119 bool invalid_nested_name_p = false;
13120 bool invalid_explicit_specialization_p = false;
13121 tree pushed_scope = NULL_TREE;
13122 unsigned num_templates;
13123 tree bases;
13124
13125 /* Assume no nested-name-specifier will be present. */
13126 *nested_name_specifier_p = false;
13127 /* Assume no template parameter lists will be used in defining the
13128 type. */
13129 num_templates = 0;
13130
13131 /* Look for the class-key. */
13132 class_key = cp_parser_class_key (parser);
13133 if (class_key == none_type)
13134 return error_mark_node;
13135
13136 /* Parse the attributes. */
13137 attributes = cp_parser_attributes_opt (parser);
13138
13139 /* If the next token is `::', that is invalid -- but sometimes
13140 people do try to write:
13141
13142 struct ::S {};
13143
13144 Handle this gracefully by accepting the extra qualifier, and then
13145 issuing an error about it later if this really is a
13146 class-head. If it turns out just to be an elaborated type
13147 specifier, remain silent. */
13148 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13149 qualified_p = true;
13150
13151 push_deferring_access_checks (dk_no_check);
13152
13153 /* Determine the name of the class. Begin by looking for an
13154 optional nested-name-specifier. */
13155 nested_name_specifier
13156 = cp_parser_nested_name_specifier_opt (parser,
13157 /*typename_keyword_p=*/false,
13158 /*check_dependency_p=*/false,
13159 /*type_p=*/false,
13160 /*is_declaration=*/false);
13161 /* If there was a nested-name-specifier, then there *must* be an
13162 identifier. */
13163 if (nested_name_specifier)
13164 {
13165 /* Although the grammar says `identifier', it really means
13166 `class-name' or `template-name'. You are only allowed to
13167 define a class that has already been declared with this
13168 syntax.
13169
13170 The proposed resolution for Core Issue 180 says that wherever
13171 you see `class T::X' you should treat `X' as a type-name.
13172
13173 It is OK to define an inaccessible class; for example:
13174
13175 class A { class B; };
13176 class A::B {};
13177
13178 We do not know if we will see a class-name, or a
13179 template-name. We look for a class-name first, in case the
13180 class-name is a template-id; if we looked for the
13181 template-name first we would stop after the template-name. */
13182 cp_parser_parse_tentatively (parser);
13183 type = cp_parser_class_name (parser,
13184 /*typename_keyword_p=*/false,
13185 /*template_keyword_p=*/false,
13186 class_type,
13187 /*check_dependency_p=*/false,
13188 /*class_head_p=*/true,
13189 /*is_declaration=*/false);
13190 /* If that didn't work, ignore the nested-name-specifier. */
13191 if (!cp_parser_parse_definitely (parser))
13192 {
13193 invalid_nested_name_p = true;
13194 id = cp_parser_identifier (parser);
13195 if (id == error_mark_node)
13196 id = NULL_TREE;
13197 }
13198 /* If we could not find a corresponding TYPE, treat this
13199 declaration like an unqualified declaration. */
13200 if (type == error_mark_node)
13201 nested_name_specifier = NULL_TREE;
13202 /* Otherwise, count the number of templates used in TYPE and its
13203 containing scopes. */
13204 else
13205 {
13206 tree scope;
13207
13208 for (scope = TREE_TYPE (type);
13209 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13210 scope = (TYPE_P (scope)
13211 ? TYPE_CONTEXT (scope)
13212 : DECL_CONTEXT (scope)))
13213 if (TYPE_P (scope)
13214 && CLASS_TYPE_P (scope)
13215 && CLASSTYPE_TEMPLATE_INFO (scope)
13216 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13217 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13218 ++num_templates;
13219 }
13220 }
13221 /* Otherwise, the identifier is optional. */
13222 else
13223 {
13224 /* We don't know whether what comes next is a template-id,
13225 an identifier, or nothing at all. */
13226 cp_parser_parse_tentatively (parser);
13227 /* Check for a template-id. */
13228 id = cp_parser_template_id (parser,
13229 /*template_keyword_p=*/false,
13230 /*check_dependency_p=*/true,
13231 /*is_declaration=*/true);
13232 /* If that didn't work, it could still be an identifier. */
13233 if (!cp_parser_parse_definitely (parser))
13234 {
13235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13236 id = cp_parser_identifier (parser);
13237 else
13238 id = NULL_TREE;
13239 }
13240 else
13241 {
13242 template_id_p = true;
13243 ++num_templates;
13244 }
13245 }
13246
13247 pop_deferring_access_checks ();
13248
13249 if (id)
13250 cp_parser_check_for_invalid_template_id (parser, id);
13251
13252 /* If it's not a `:' or a `{' then we can't really be looking at a
13253 class-head, since a class-head only appears as part of a
13254 class-specifier. We have to detect this situation before calling
13255 xref_tag, since that has irreversible side-effects. */
13256 if (!cp_parser_next_token_starts_class_definition_p (parser))
13257 {
13258 cp_parser_error (parser, "expected %<{%> or %<:%>");
13259 return error_mark_node;
13260 }
13261
13262 /* At this point, we're going ahead with the class-specifier, even
13263 if some other problem occurs. */
13264 cp_parser_commit_to_tentative_parse (parser);
13265 /* Issue the error about the overly-qualified name now. */
13266 if (qualified_p)
13267 cp_parser_error (parser,
13268 "global qualification of class name is invalid");
13269 else if (invalid_nested_name_p)
13270 cp_parser_error (parser,
13271 "qualified name does not name a class");
13272 else if (nested_name_specifier)
13273 {
13274 tree scope;
13275
13276 /* Reject typedef-names in class heads. */
13277 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13278 {
13279 error ("invalid class name in declaration of %qD", type);
13280 type = NULL_TREE;
13281 goto done;
13282 }
13283
13284 /* Figure out in what scope the declaration is being placed. */
13285 scope = current_scope ();
13286 /* If that scope does not contain the scope in which the
13287 class was originally declared, the program is invalid. */
13288 if (scope && !is_ancestor (scope, nested_name_specifier))
13289 {
13290 error ("declaration of %qD in %qD which does not enclose %qD",
13291 type, scope, nested_name_specifier);
13292 type = NULL_TREE;
13293 goto done;
13294 }
13295 /* [dcl.meaning]
13296
13297 A declarator-id shall not be qualified exception of the
13298 definition of a ... nested class outside of its class
13299 ... [or] a the definition or explicit instantiation of a
13300 class member of a namespace outside of its namespace. */
13301 if (scope == nested_name_specifier)
13302 {
13303 pedwarn ("extra qualification ignored");
13304 nested_name_specifier = NULL_TREE;
13305 num_templates = 0;
13306 }
13307 }
13308 /* An explicit-specialization must be preceded by "template <>". If
13309 it is not, try to recover gracefully. */
13310 if (at_namespace_scope_p ()
13311 && parser->num_template_parameter_lists == 0
13312 && template_id_p)
13313 {
13314 error ("an explicit specialization must be preceded by %<template <>%>");
13315 invalid_explicit_specialization_p = true;
13316 /* Take the same action that would have been taken by
13317 cp_parser_explicit_specialization. */
13318 ++parser->num_template_parameter_lists;
13319 begin_specialization ();
13320 }
13321 /* There must be no "return" statements between this point and the
13322 end of this function; set "type "to the correct return value and
13323 use "goto done;" to return. */
13324 /* Make sure that the right number of template parameters were
13325 present. */
13326 if (!cp_parser_check_template_parameters (parser, num_templates))
13327 {
13328 /* If something went wrong, there is no point in even trying to
13329 process the class-definition. */
13330 type = NULL_TREE;
13331 goto done;
13332 }
13333
13334 /* Look up the type. */
13335 if (template_id_p)
13336 {
13337 type = TREE_TYPE (id);
13338 type = maybe_process_partial_specialization (type);
13339 if (nested_name_specifier)
13340 pushed_scope = push_scope (nested_name_specifier);
13341 }
13342 else if (nested_name_specifier)
13343 {
13344 tree class_type;
13345
13346 /* Given:
13347
13348 template <typename T> struct S { struct T };
13349 template <typename T> struct S<T>::T { };
13350
13351 we will get a TYPENAME_TYPE when processing the definition of
13352 `S::T'. We need to resolve it to the actual type before we
13353 try to define it. */
13354 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13355 {
13356 class_type = resolve_typename_type (TREE_TYPE (type),
13357 /*only_current_p=*/false);
13358 if (class_type != error_mark_node)
13359 type = TYPE_NAME (class_type);
13360 else
13361 {
13362 cp_parser_error (parser, "could not resolve typename type");
13363 type = error_mark_node;
13364 }
13365 }
13366
13367 maybe_process_partial_specialization (TREE_TYPE (type));
13368 class_type = current_class_type;
13369 /* Enter the scope indicated by the nested-name-specifier. */
13370 pushed_scope = push_scope (nested_name_specifier);
13371 /* Get the canonical version of this type. */
13372 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13373 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13374 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13375 {
13376 type = push_template_decl (type);
13377 if (type == error_mark_node)
13378 {
13379 type = NULL_TREE;
13380 goto done;
13381 }
13382 }
13383
13384 type = TREE_TYPE (type);
13385 *nested_name_specifier_p = true;
13386 }
13387 else /* The name is not a nested name. */
13388 {
13389 /* If the class was unnamed, create a dummy name. */
13390 if (!id)
13391 id = make_anon_name ();
13392 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13393 parser->num_template_parameter_lists);
13394 }
13395
13396 /* Indicate whether this class was declared as a `class' or as a
13397 `struct'. */
13398 if (TREE_CODE (type) == RECORD_TYPE)
13399 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13400 cp_parser_check_class_key (class_key, type);
13401
13402 /* If this type was already complete, and we see another definition,
13403 that's an error. */
13404 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13405 {
13406 error ("redefinition of %q#T", type);
13407 error ("previous definition of %q+#T", type);
13408 type = NULL_TREE;
13409 goto done;
13410 }
13411
13412 /* We will have entered the scope containing the class; the names of
13413 base classes should be looked up in that context. For example:
13414
13415 struct A { struct B {}; struct C; };
13416 struct A::C : B {};
13417
13418 is valid. */
13419 bases = NULL_TREE;
13420
13421 /* Get the list of base-classes, if there is one. */
13422 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13423 bases = cp_parser_base_clause (parser);
13424
13425 /* Process the base classes. */
13426 xref_basetypes (type, bases);
13427
13428 done:
13429 /* Leave the scope given by the nested-name-specifier. We will
13430 enter the class scope itself while processing the members. */
13431 if (pushed_scope)
13432 pop_scope (pushed_scope);
13433
13434 if (invalid_explicit_specialization_p)
13435 {
13436 end_specialization ();
13437 --parser->num_template_parameter_lists;
13438 }
13439 *attributes_p = attributes;
13440 return type;
13441 }
13442
13443 /* Parse a class-key.
13444
13445 class-key:
13446 class
13447 struct
13448 union
13449
13450 Returns the kind of class-key specified, or none_type to indicate
13451 error. */
13452
13453 static enum tag_types
13454 cp_parser_class_key (cp_parser* parser)
13455 {
13456 cp_token *token;
13457 enum tag_types tag_type;
13458
13459 /* Look for the class-key. */
13460 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13461 if (!token)
13462 return none_type;
13463
13464 /* Check to see if the TOKEN is a class-key. */
13465 tag_type = cp_parser_token_is_class_key (token);
13466 if (!tag_type)
13467 cp_parser_error (parser, "expected class-key");
13468 return tag_type;
13469 }
13470
13471 /* Parse an (optional) member-specification.
13472
13473 member-specification:
13474 member-declaration member-specification [opt]
13475 access-specifier : member-specification [opt] */
13476
13477 static void
13478 cp_parser_member_specification_opt (cp_parser* parser)
13479 {
13480 while (true)
13481 {
13482 cp_token *token;
13483 enum rid keyword;
13484
13485 /* Peek at the next token. */
13486 token = cp_lexer_peek_token (parser->lexer);
13487 /* If it's a `}', or EOF then we've seen all the members. */
13488 if (token->type == CPP_CLOSE_BRACE
13489 || token->type == CPP_EOF
13490 || token->type == CPP_PRAGMA_EOL)
13491 break;
13492
13493 /* See if this token is a keyword. */
13494 keyword = token->keyword;
13495 switch (keyword)
13496 {
13497 case RID_PUBLIC:
13498 case RID_PROTECTED:
13499 case RID_PRIVATE:
13500 /* Consume the access-specifier. */
13501 cp_lexer_consume_token (parser->lexer);
13502 /* Remember which access-specifier is active. */
13503 current_access_specifier = token->value;
13504 /* Look for the `:'. */
13505 cp_parser_require (parser, CPP_COLON, "`:'");
13506 break;
13507
13508 default:
13509 /* Accept #pragmas at class scope. */
13510 if (token->type == CPP_PRAGMA)
13511 {
13512 cp_parser_pragma (parser, pragma_external);
13513 break;
13514 }
13515
13516 /* Otherwise, the next construction must be a
13517 member-declaration. */
13518 cp_parser_member_declaration (parser);
13519 }
13520 }
13521 }
13522
13523 /* Parse a member-declaration.
13524
13525 member-declaration:
13526 decl-specifier-seq [opt] member-declarator-list [opt] ;
13527 function-definition ; [opt]
13528 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13529 using-declaration
13530 template-declaration
13531
13532 member-declarator-list:
13533 member-declarator
13534 member-declarator-list , member-declarator
13535
13536 member-declarator:
13537 declarator pure-specifier [opt]
13538 declarator constant-initializer [opt]
13539 identifier [opt] : constant-expression
13540
13541 GNU Extensions:
13542
13543 member-declaration:
13544 __extension__ member-declaration
13545
13546 member-declarator:
13547 declarator attributes [opt] pure-specifier [opt]
13548 declarator attributes [opt] constant-initializer [opt]
13549 identifier [opt] attributes [opt] : constant-expression */
13550
13551 static void
13552 cp_parser_member_declaration (cp_parser* parser)
13553 {
13554 cp_decl_specifier_seq decl_specifiers;
13555 tree prefix_attributes;
13556 tree decl;
13557 int declares_class_or_enum;
13558 bool friend_p;
13559 cp_token *token;
13560 int saved_pedantic;
13561
13562 /* Check for the `__extension__' keyword. */
13563 if (cp_parser_extension_opt (parser, &saved_pedantic))
13564 {
13565 /* Recurse. */
13566 cp_parser_member_declaration (parser);
13567 /* Restore the old value of the PEDANTIC flag. */
13568 pedantic = saved_pedantic;
13569
13570 return;
13571 }
13572
13573 /* Check for a template-declaration. */
13574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13575 {
13576 /* An explicit specialization here is an error condition, and we
13577 expect the specialization handler to detect and report this. */
13578 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13579 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13580 cp_parser_explicit_specialization (parser);
13581 else
13582 cp_parser_template_declaration (parser, /*member_p=*/true);
13583
13584 return;
13585 }
13586
13587 /* Check for a using-declaration. */
13588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13589 {
13590 /* Parse the using-declaration. */
13591 cp_parser_using_declaration (parser,
13592 /*access_declaration_p=*/false);
13593 return;
13594 }
13595
13596 /* Check for @defs. */
13597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13598 {
13599 tree ivar, member;
13600 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13601 ivar = ivar_chains;
13602 while (ivar)
13603 {
13604 member = ivar;
13605 ivar = TREE_CHAIN (member);
13606 TREE_CHAIN (member) = NULL_TREE;
13607 finish_member_declaration (member);
13608 }
13609 return;
13610 }
13611
13612 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13613 return;
13614
13615 /* Parse the decl-specifier-seq. */
13616 cp_parser_decl_specifier_seq (parser,
13617 CP_PARSER_FLAGS_OPTIONAL,
13618 &decl_specifiers,
13619 &declares_class_or_enum);
13620 prefix_attributes = decl_specifiers.attributes;
13621 decl_specifiers.attributes = NULL_TREE;
13622 /* Check for an invalid type-name. */
13623 if (!decl_specifiers.type
13624 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13625 return;
13626 /* If there is no declarator, then the decl-specifier-seq should
13627 specify a type. */
13628 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13629 {
13630 /* If there was no decl-specifier-seq, and the next token is a
13631 `;', then we have something like:
13632
13633 struct S { ; };
13634
13635 [class.mem]
13636
13637 Each member-declaration shall declare at least one member
13638 name of the class. */
13639 if (!decl_specifiers.any_specifiers_p)
13640 {
13641 cp_token *token = cp_lexer_peek_token (parser->lexer);
13642 if (pedantic && !token->in_system_header)
13643 pedwarn ("%Hextra %<;%>", &token->location);
13644 }
13645 else
13646 {
13647 tree type;
13648
13649 /* See if this declaration is a friend. */
13650 friend_p = cp_parser_friend_p (&decl_specifiers);
13651 /* If there were decl-specifiers, check to see if there was
13652 a class-declaration. */
13653 type = check_tag_decl (&decl_specifiers);
13654 /* Nested classes have already been added to the class, but
13655 a `friend' needs to be explicitly registered. */
13656 if (friend_p)
13657 {
13658 /* If the `friend' keyword was present, the friend must
13659 be introduced with a class-key. */
13660 if (!declares_class_or_enum)
13661 error ("a class-key must be used when declaring a friend");
13662 /* In this case:
13663
13664 template <typename T> struct A {
13665 friend struct A<T>::B;
13666 };
13667
13668 A<T>::B will be represented by a TYPENAME_TYPE, and
13669 therefore not recognized by check_tag_decl. */
13670 if (!type
13671 && decl_specifiers.type
13672 && TYPE_P (decl_specifiers.type))
13673 type = decl_specifiers.type;
13674 if (!type || !TYPE_P (type))
13675 error ("friend declaration does not name a class or "
13676 "function");
13677 else
13678 make_friend_class (current_class_type, type,
13679 /*complain=*/true);
13680 }
13681 /* If there is no TYPE, an error message will already have
13682 been issued. */
13683 else if (!type || type == error_mark_node)
13684 ;
13685 /* An anonymous aggregate has to be handled specially; such
13686 a declaration really declares a data member (with a
13687 particular type), as opposed to a nested class. */
13688 else if (ANON_AGGR_TYPE_P (type))
13689 {
13690 /* Remove constructors and such from TYPE, now that we
13691 know it is an anonymous aggregate. */
13692 fixup_anonymous_aggr (type);
13693 /* And make the corresponding data member. */
13694 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13695 /* Add it to the class. */
13696 finish_member_declaration (decl);
13697 }
13698 else
13699 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13700 }
13701 }
13702 else
13703 {
13704 /* See if these declarations will be friends. */
13705 friend_p = cp_parser_friend_p (&decl_specifiers);
13706
13707 /* Keep going until we hit the `;' at the end of the
13708 declaration. */
13709 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13710 {
13711 tree attributes = NULL_TREE;
13712 tree first_attribute;
13713
13714 /* Peek at the next token. */
13715 token = cp_lexer_peek_token (parser->lexer);
13716
13717 /* Check for a bitfield declaration. */
13718 if (token->type == CPP_COLON
13719 || (token->type == CPP_NAME
13720 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13721 == CPP_COLON))
13722 {
13723 tree identifier;
13724 tree width;
13725
13726 /* Get the name of the bitfield. Note that we cannot just
13727 check TOKEN here because it may have been invalidated by
13728 the call to cp_lexer_peek_nth_token above. */
13729 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13730 identifier = cp_parser_identifier (parser);
13731 else
13732 identifier = NULL_TREE;
13733
13734 /* Consume the `:' token. */
13735 cp_lexer_consume_token (parser->lexer);
13736 /* Get the width of the bitfield. */
13737 width
13738 = cp_parser_constant_expression (parser,
13739 /*allow_non_constant=*/false,
13740 NULL);
13741
13742 /* Look for attributes that apply to the bitfield. */
13743 attributes = cp_parser_attributes_opt (parser);
13744 /* Remember which attributes are prefix attributes and
13745 which are not. */
13746 first_attribute = attributes;
13747 /* Combine the attributes. */
13748 attributes = chainon (prefix_attributes, attributes);
13749
13750 /* Create the bitfield declaration. */
13751 decl = grokbitfield (identifier
13752 ? make_id_declarator (NULL_TREE,
13753 identifier,
13754 sfk_none)
13755 : NULL,
13756 &decl_specifiers,
13757 width);
13758 /* Apply the attributes. */
13759 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13760 }
13761 else
13762 {
13763 cp_declarator *declarator;
13764 tree initializer;
13765 tree asm_specification;
13766 int ctor_dtor_or_conv_p;
13767
13768 /* Parse the declarator. */
13769 declarator
13770 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13771 &ctor_dtor_or_conv_p,
13772 /*parenthesized_p=*/NULL,
13773 /*member_p=*/true);
13774
13775 /* If something went wrong parsing the declarator, make sure
13776 that we at least consume some tokens. */
13777 if (declarator == cp_error_declarator)
13778 {
13779 /* Skip to the end of the statement. */
13780 cp_parser_skip_to_end_of_statement (parser);
13781 /* If the next token is not a semicolon, that is
13782 probably because we just skipped over the body of
13783 a function. So, we consume a semicolon if
13784 present, but do not issue an error message if it
13785 is not present. */
13786 if (cp_lexer_next_token_is (parser->lexer,
13787 CPP_SEMICOLON))
13788 cp_lexer_consume_token (parser->lexer);
13789 return;
13790 }
13791
13792 if (declares_class_or_enum & 2)
13793 cp_parser_check_for_definition_in_return_type
13794 (declarator, decl_specifiers.type);
13795
13796 /* Look for an asm-specification. */
13797 asm_specification = cp_parser_asm_specification_opt (parser);
13798 /* Look for attributes that apply to the declaration. */
13799 attributes = cp_parser_attributes_opt (parser);
13800 /* Remember which attributes are prefix attributes and
13801 which are not. */
13802 first_attribute = attributes;
13803 /* Combine the attributes. */
13804 attributes = chainon (prefix_attributes, attributes);
13805
13806 /* If it's an `=', then we have a constant-initializer or a
13807 pure-specifier. It is not correct to parse the
13808 initializer before registering the member declaration
13809 since the member declaration should be in scope while
13810 its initializer is processed. However, the rest of the
13811 front end does not yet provide an interface that allows
13812 us to handle this correctly. */
13813 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13814 {
13815 /* In [class.mem]:
13816
13817 A pure-specifier shall be used only in the declaration of
13818 a virtual function.
13819
13820 A member-declarator can contain a constant-initializer
13821 only if it declares a static member of integral or
13822 enumeration type.
13823
13824 Therefore, if the DECLARATOR is for a function, we look
13825 for a pure-specifier; otherwise, we look for a
13826 constant-initializer. When we call `grokfield', it will
13827 perform more stringent semantics checks. */
13828 if (declarator->kind == cdk_function
13829 && declarator->declarator->kind == cdk_id)
13830 initializer = cp_parser_pure_specifier (parser);
13831 else
13832 /* Parse the initializer. */
13833 initializer = cp_parser_constant_initializer (parser);
13834 }
13835 /* Otherwise, there is no initializer. */
13836 else
13837 initializer = NULL_TREE;
13838
13839 /* See if we are probably looking at a function
13840 definition. We are certainly not looking at a
13841 member-declarator. Calling `grokfield' has
13842 side-effects, so we must not do it unless we are sure
13843 that we are looking at a member-declarator. */
13844 if (cp_parser_token_starts_function_definition_p
13845 (cp_lexer_peek_token (parser->lexer)))
13846 {
13847 /* The grammar does not allow a pure-specifier to be
13848 used when a member function is defined. (It is
13849 possible that this fact is an oversight in the
13850 standard, since a pure function may be defined
13851 outside of the class-specifier. */
13852 if (initializer)
13853 error ("pure-specifier on function-definition");
13854 decl = cp_parser_save_member_function_body (parser,
13855 &decl_specifiers,
13856 declarator,
13857 attributes);
13858 /* If the member was not a friend, declare it here. */
13859 if (!friend_p)
13860 finish_member_declaration (decl);
13861 /* Peek at the next token. */
13862 token = cp_lexer_peek_token (parser->lexer);
13863 /* If the next token is a semicolon, consume it. */
13864 if (token->type == CPP_SEMICOLON)
13865 cp_lexer_consume_token (parser->lexer);
13866 return;
13867 }
13868 else
13869 /* Create the declaration. */
13870 decl = grokfield (declarator, &decl_specifiers,
13871 initializer, /*init_const_expr_p=*/true,
13872 asm_specification,
13873 attributes);
13874 }
13875
13876 /* Reset PREFIX_ATTRIBUTES. */
13877 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13878 attributes = TREE_CHAIN (attributes);
13879 if (attributes)
13880 TREE_CHAIN (attributes) = NULL_TREE;
13881
13882 /* If there is any qualification still in effect, clear it
13883 now; we will be starting fresh with the next declarator. */
13884 parser->scope = NULL_TREE;
13885 parser->qualifying_scope = NULL_TREE;
13886 parser->object_scope = NULL_TREE;
13887 /* If it's a `,', then there are more declarators. */
13888 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13889 cp_lexer_consume_token (parser->lexer);
13890 /* If the next token isn't a `;', then we have a parse error. */
13891 else if (cp_lexer_next_token_is_not (parser->lexer,
13892 CPP_SEMICOLON))
13893 {
13894 cp_parser_error (parser, "expected %<;%>");
13895 /* Skip tokens until we find a `;'. */
13896 cp_parser_skip_to_end_of_statement (parser);
13897
13898 break;
13899 }
13900
13901 if (decl)
13902 {
13903 /* Add DECL to the list of members. */
13904 if (!friend_p)
13905 finish_member_declaration (decl);
13906
13907 if (TREE_CODE (decl) == FUNCTION_DECL)
13908 cp_parser_save_default_args (parser, decl);
13909 }
13910 }
13911 }
13912
13913 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13914 }
13915
13916 /* Parse a pure-specifier.
13917
13918 pure-specifier:
13919 = 0
13920
13921 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13922 Otherwise, ERROR_MARK_NODE is returned. */
13923
13924 static tree
13925 cp_parser_pure_specifier (cp_parser* parser)
13926 {
13927 cp_token *token;
13928
13929 /* Look for the `=' token. */
13930 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13931 return error_mark_node;
13932 /* Look for the `0' token. */
13933 token = cp_lexer_consume_token (parser->lexer);
13934 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13935 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13936 {
13937 cp_parser_error (parser,
13938 "invalid pure specifier (only `= 0' is allowed)");
13939 cp_parser_skip_to_end_of_statement (parser);
13940 return error_mark_node;
13941 }
13942 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13943 {
13944 error ("templates may not be %<virtual%>");
13945 return error_mark_node;
13946 }
13947
13948 return integer_zero_node;
13949 }
13950
13951 /* Parse a constant-initializer.
13952
13953 constant-initializer:
13954 = constant-expression
13955
13956 Returns a representation of the constant-expression. */
13957
13958 static tree
13959 cp_parser_constant_initializer (cp_parser* parser)
13960 {
13961 /* Look for the `=' token. */
13962 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13963 return error_mark_node;
13964
13965 /* It is invalid to write:
13966
13967 struct S { static const int i = { 7 }; };
13968
13969 */
13970 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13971 {
13972 cp_parser_error (parser,
13973 "a brace-enclosed initializer is not allowed here");
13974 /* Consume the opening brace. */
13975 cp_lexer_consume_token (parser->lexer);
13976 /* Skip the initializer. */
13977 cp_parser_skip_to_closing_brace (parser);
13978 /* Look for the trailing `}'. */
13979 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13980
13981 return error_mark_node;
13982 }
13983
13984 return cp_parser_constant_expression (parser,
13985 /*allow_non_constant=*/false,
13986 NULL);
13987 }
13988
13989 /* Derived classes [gram.class.derived] */
13990
13991 /* Parse a base-clause.
13992
13993 base-clause:
13994 : base-specifier-list
13995
13996 base-specifier-list:
13997 base-specifier
13998 base-specifier-list , base-specifier
13999
14000 Returns a TREE_LIST representing the base-classes, in the order in
14001 which they were declared. The representation of each node is as
14002 described by cp_parser_base_specifier.
14003
14004 In the case that no bases are specified, this function will return
14005 NULL_TREE, not ERROR_MARK_NODE. */
14006
14007 static tree
14008 cp_parser_base_clause (cp_parser* parser)
14009 {
14010 tree bases = NULL_TREE;
14011
14012 /* Look for the `:' that begins the list. */
14013 cp_parser_require (parser, CPP_COLON, "`:'");
14014
14015 /* Scan the base-specifier-list. */
14016 while (true)
14017 {
14018 cp_token *token;
14019 tree base;
14020
14021 /* Look for the base-specifier. */
14022 base = cp_parser_base_specifier (parser);
14023 /* Add BASE to the front of the list. */
14024 if (base != error_mark_node)
14025 {
14026 TREE_CHAIN (base) = bases;
14027 bases = base;
14028 }
14029 /* Peek at the next token. */
14030 token = cp_lexer_peek_token (parser->lexer);
14031 /* If it's not a comma, then the list is complete. */
14032 if (token->type != CPP_COMMA)
14033 break;
14034 /* Consume the `,'. */
14035 cp_lexer_consume_token (parser->lexer);
14036 }
14037
14038 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14039 base class had a qualified name. However, the next name that
14040 appears is certainly not qualified. */
14041 parser->scope = NULL_TREE;
14042 parser->qualifying_scope = NULL_TREE;
14043 parser->object_scope = NULL_TREE;
14044
14045 return nreverse (bases);
14046 }
14047
14048 /* Parse a base-specifier.
14049
14050 base-specifier:
14051 :: [opt] nested-name-specifier [opt] class-name
14052 virtual access-specifier [opt] :: [opt] nested-name-specifier
14053 [opt] class-name
14054 access-specifier virtual [opt] :: [opt] nested-name-specifier
14055 [opt] class-name
14056
14057 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14058 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14059 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14060 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14061
14062 static tree
14063 cp_parser_base_specifier (cp_parser* parser)
14064 {
14065 cp_token *token;
14066 bool done = false;
14067 bool virtual_p = false;
14068 bool duplicate_virtual_error_issued_p = false;
14069 bool duplicate_access_error_issued_p = false;
14070 bool class_scope_p, template_p;
14071 tree access = access_default_node;
14072 tree type;
14073
14074 /* Process the optional `virtual' and `access-specifier'. */
14075 while (!done)
14076 {
14077 /* Peek at the next token. */
14078 token = cp_lexer_peek_token (parser->lexer);
14079 /* Process `virtual'. */
14080 switch (token->keyword)
14081 {
14082 case RID_VIRTUAL:
14083 /* If `virtual' appears more than once, issue an error. */
14084 if (virtual_p && !duplicate_virtual_error_issued_p)
14085 {
14086 cp_parser_error (parser,
14087 "%<virtual%> specified more than once in base-specified");
14088 duplicate_virtual_error_issued_p = true;
14089 }
14090
14091 virtual_p = true;
14092
14093 /* Consume the `virtual' token. */
14094 cp_lexer_consume_token (parser->lexer);
14095
14096 break;
14097
14098 case RID_PUBLIC:
14099 case RID_PROTECTED:
14100 case RID_PRIVATE:
14101 /* If more than one access specifier appears, issue an
14102 error. */
14103 if (access != access_default_node
14104 && !duplicate_access_error_issued_p)
14105 {
14106 cp_parser_error (parser,
14107 "more than one access specifier in base-specified");
14108 duplicate_access_error_issued_p = true;
14109 }
14110
14111 access = ridpointers[(int) token->keyword];
14112
14113 /* Consume the access-specifier. */
14114 cp_lexer_consume_token (parser->lexer);
14115
14116 break;
14117
14118 default:
14119 done = true;
14120 break;
14121 }
14122 }
14123 /* It is not uncommon to see programs mechanically, erroneously, use
14124 the 'typename' keyword to denote (dependent) qualified types
14125 as base classes. */
14126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14127 {
14128 if (!processing_template_decl)
14129 error ("keyword %<typename%> not allowed outside of templates");
14130 else
14131 error ("keyword %<typename%> not allowed in this context "
14132 "(the base class is implicitly a type)");
14133 cp_lexer_consume_token (parser->lexer);
14134 }
14135
14136 /* Look for the optional `::' operator. */
14137 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14138 /* Look for the nested-name-specifier. The simplest way to
14139 implement:
14140
14141 [temp.res]
14142
14143 The keyword `typename' is not permitted in a base-specifier or
14144 mem-initializer; in these contexts a qualified name that
14145 depends on a template-parameter is implicitly assumed to be a
14146 type name.
14147
14148 is to pretend that we have seen the `typename' keyword at this
14149 point. */
14150 cp_parser_nested_name_specifier_opt (parser,
14151 /*typename_keyword_p=*/true,
14152 /*check_dependency_p=*/true,
14153 typename_type,
14154 /*is_declaration=*/true);
14155 /* If the base class is given by a qualified name, assume that names
14156 we see are type names or templates, as appropriate. */
14157 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14158 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14159
14160 /* Finally, look for the class-name. */
14161 type = cp_parser_class_name (parser,
14162 class_scope_p,
14163 template_p,
14164 typename_type,
14165 /*check_dependency_p=*/true,
14166 /*class_head_p=*/false,
14167 /*is_declaration=*/true);
14168
14169 if (type == error_mark_node)
14170 return error_mark_node;
14171
14172 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14173 }
14174
14175 /* Exception handling [gram.exception] */
14176
14177 /* Parse an (optional) exception-specification.
14178
14179 exception-specification:
14180 throw ( type-id-list [opt] )
14181
14182 Returns a TREE_LIST representing the exception-specification. The
14183 TREE_VALUE of each node is a type. */
14184
14185 static tree
14186 cp_parser_exception_specification_opt (cp_parser* parser)
14187 {
14188 cp_token *token;
14189 tree type_id_list;
14190
14191 /* Peek at the next token. */
14192 token = cp_lexer_peek_token (parser->lexer);
14193 /* If it's not `throw', then there's no exception-specification. */
14194 if (!cp_parser_is_keyword (token, RID_THROW))
14195 return NULL_TREE;
14196
14197 /* Consume the `throw'. */
14198 cp_lexer_consume_token (parser->lexer);
14199
14200 /* Look for the `('. */
14201 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14202
14203 /* Peek at the next token. */
14204 token = cp_lexer_peek_token (parser->lexer);
14205 /* If it's not a `)', then there is a type-id-list. */
14206 if (token->type != CPP_CLOSE_PAREN)
14207 {
14208 const char *saved_message;
14209
14210 /* Types may not be defined in an exception-specification. */
14211 saved_message = parser->type_definition_forbidden_message;
14212 parser->type_definition_forbidden_message
14213 = "types may not be defined in an exception-specification";
14214 /* Parse the type-id-list. */
14215 type_id_list = cp_parser_type_id_list (parser);
14216 /* Restore the saved message. */
14217 parser->type_definition_forbidden_message = saved_message;
14218 }
14219 else
14220 type_id_list = empty_except_spec;
14221
14222 /* Look for the `)'. */
14223 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14224
14225 return type_id_list;
14226 }
14227
14228 /* Parse an (optional) type-id-list.
14229
14230 type-id-list:
14231 type-id
14232 type-id-list , type-id
14233
14234 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14235 in the order that the types were presented. */
14236
14237 static tree
14238 cp_parser_type_id_list (cp_parser* parser)
14239 {
14240 tree types = NULL_TREE;
14241
14242 while (true)
14243 {
14244 cp_token *token;
14245 tree type;
14246
14247 /* Get the next type-id. */
14248 type = cp_parser_type_id (parser);
14249 /* Add it to the list. */
14250 types = add_exception_specifier (types, type, /*complain=*/1);
14251 /* Peek at the next token. */
14252 token = cp_lexer_peek_token (parser->lexer);
14253 /* If it is not a `,', we are done. */
14254 if (token->type != CPP_COMMA)
14255 break;
14256 /* Consume the `,'. */
14257 cp_lexer_consume_token (parser->lexer);
14258 }
14259
14260 return nreverse (types);
14261 }
14262
14263 /* Parse a try-block.
14264
14265 try-block:
14266 try compound-statement handler-seq */
14267
14268 static tree
14269 cp_parser_try_block (cp_parser* parser)
14270 {
14271 tree try_block;
14272
14273 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14274 try_block = begin_try_block ();
14275 cp_parser_compound_statement (parser, NULL, true);
14276 finish_try_block (try_block);
14277 cp_parser_handler_seq (parser);
14278 finish_handler_sequence (try_block);
14279
14280 return try_block;
14281 }
14282
14283 /* Parse a function-try-block.
14284
14285 function-try-block:
14286 try ctor-initializer [opt] function-body handler-seq */
14287
14288 static bool
14289 cp_parser_function_try_block (cp_parser* parser)
14290 {
14291 tree compound_stmt;
14292 tree try_block;
14293 bool ctor_initializer_p;
14294
14295 /* Look for the `try' keyword. */
14296 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14297 return false;
14298 /* Let the rest of the front-end know where we are. */
14299 try_block = begin_function_try_block (&compound_stmt);
14300 /* Parse the function-body. */
14301 ctor_initializer_p
14302 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14303 /* We're done with the `try' part. */
14304 finish_function_try_block (try_block);
14305 /* Parse the handlers. */
14306 cp_parser_handler_seq (parser);
14307 /* We're done with the handlers. */
14308 finish_function_handler_sequence (try_block, compound_stmt);
14309
14310 return ctor_initializer_p;
14311 }
14312
14313 /* Parse a handler-seq.
14314
14315 handler-seq:
14316 handler handler-seq [opt] */
14317
14318 static void
14319 cp_parser_handler_seq (cp_parser* parser)
14320 {
14321 while (true)
14322 {
14323 cp_token *token;
14324
14325 /* Parse the handler. */
14326 cp_parser_handler (parser);
14327 /* Peek at the next token. */
14328 token = cp_lexer_peek_token (parser->lexer);
14329 /* If it's not `catch' then there are no more handlers. */
14330 if (!cp_parser_is_keyword (token, RID_CATCH))
14331 break;
14332 }
14333 }
14334
14335 /* Parse a handler.
14336
14337 handler:
14338 catch ( exception-declaration ) compound-statement */
14339
14340 static void
14341 cp_parser_handler (cp_parser* parser)
14342 {
14343 tree handler;
14344 tree declaration;
14345
14346 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14347 handler = begin_handler ();
14348 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14349 declaration = cp_parser_exception_declaration (parser);
14350 finish_handler_parms (declaration, handler);
14351 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14352 cp_parser_compound_statement (parser, NULL, false);
14353 finish_handler (handler);
14354 }
14355
14356 /* Parse an exception-declaration.
14357
14358 exception-declaration:
14359 type-specifier-seq declarator
14360 type-specifier-seq abstract-declarator
14361 type-specifier-seq
14362 ...
14363
14364 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14365 ellipsis variant is used. */
14366
14367 static tree
14368 cp_parser_exception_declaration (cp_parser* parser)
14369 {
14370 cp_decl_specifier_seq type_specifiers;
14371 cp_declarator *declarator;
14372 const char *saved_message;
14373
14374 /* If it's an ellipsis, it's easy to handle. */
14375 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14376 {
14377 /* Consume the `...' token. */
14378 cp_lexer_consume_token (parser->lexer);
14379 return NULL_TREE;
14380 }
14381
14382 /* Types may not be defined in exception-declarations. */
14383 saved_message = parser->type_definition_forbidden_message;
14384 parser->type_definition_forbidden_message
14385 = "types may not be defined in exception-declarations";
14386
14387 /* Parse the type-specifier-seq. */
14388 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14389 &type_specifiers);
14390 /* If it's a `)', then there is no declarator. */
14391 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14392 declarator = NULL;
14393 else
14394 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14395 /*ctor_dtor_or_conv_p=*/NULL,
14396 /*parenthesized_p=*/NULL,
14397 /*member_p=*/false);
14398
14399 /* Restore the saved message. */
14400 parser->type_definition_forbidden_message = saved_message;
14401
14402 if (!type_specifiers.any_specifiers_p)
14403 return error_mark_node;
14404
14405 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14406 }
14407
14408 /* Parse a throw-expression.
14409
14410 throw-expression:
14411 throw assignment-expression [opt]
14412
14413 Returns a THROW_EXPR representing the throw-expression. */
14414
14415 static tree
14416 cp_parser_throw_expression (cp_parser* parser)
14417 {
14418 tree expression;
14419 cp_token* token;
14420
14421 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14422 token = cp_lexer_peek_token (parser->lexer);
14423 /* Figure out whether or not there is an assignment-expression
14424 following the "throw" keyword. */
14425 if (token->type == CPP_COMMA
14426 || token->type == CPP_SEMICOLON
14427 || token->type == CPP_CLOSE_PAREN
14428 || token->type == CPP_CLOSE_SQUARE
14429 || token->type == CPP_CLOSE_BRACE
14430 || token->type == CPP_COLON)
14431 expression = NULL_TREE;
14432 else
14433 expression = cp_parser_assignment_expression (parser,
14434 /*cast_p=*/false);
14435
14436 return build_throw (expression);
14437 }
14438
14439 /* GNU Extensions */
14440
14441 /* Parse an (optional) asm-specification.
14442
14443 asm-specification:
14444 asm ( string-literal )
14445
14446 If the asm-specification is present, returns a STRING_CST
14447 corresponding to the string-literal. Otherwise, returns
14448 NULL_TREE. */
14449
14450 static tree
14451 cp_parser_asm_specification_opt (cp_parser* parser)
14452 {
14453 cp_token *token;
14454 tree asm_specification;
14455
14456 /* Peek at the next token. */
14457 token = cp_lexer_peek_token (parser->lexer);
14458 /* If the next token isn't the `asm' keyword, then there's no
14459 asm-specification. */
14460 if (!cp_parser_is_keyword (token, RID_ASM))
14461 return NULL_TREE;
14462
14463 /* Consume the `asm' token. */
14464 cp_lexer_consume_token (parser->lexer);
14465 /* Look for the `('. */
14466 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14467
14468 /* Look for the string-literal. */
14469 asm_specification = cp_parser_string_literal (parser, false, false);
14470
14471 /* Look for the `)'. */
14472 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14473
14474 return asm_specification;
14475 }
14476
14477 /* Parse an asm-operand-list.
14478
14479 asm-operand-list:
14480 asm-operand
14481 asm-operand-list , asm-operand
14482
14483 asm-operand:
14484 string-literal ( expression )
14485 [ string-literal ] string-literal ( expression )
14486
14487 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14488 each node is the expression. The TREE_PURPOSE is itself a
14489 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14490 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14491 is a STRING_CST for the string literal before the parenthesis. */
14492
14493 static tree
14494 cp_parser_asm_operand_list (cp_parser* parser)
14495 {
14496 tree asm_operands = NULL_TREE;
14497
14498 while (true)
14499 {
14500 tree string_literal;
14501 tree expression;
14502 tree name;
14503
14504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14505 {
14506 /* Consume the `[' token. */
14507 cp_lexer_consume_token (parser->lexer);
14508 /* Read the operand name. */
14509 name = cp_parser_identifier (parser);
14510 if (name != error_mark_node)
14511 name = build_string (IDENTIFIER_LENGTH (name),
14512 IDENTIFIER_POINTER (name));
14513 /* Look for the closing `]'. */
14514 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14515 }
14516 else
14517 name = NULL_TREE;
14518 /* Look for the string-literal. */
14519 string_literal = cp_parser_string_literal (parser, false, false);
14520
14521 /* Look for the `('. */
14522 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14523 /* Parse the expression. */
14524 expression = cp_parser_expression (parser, /*cast_p=*/false);
14525 /* Look for the `)'. */
14526 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14527
14528 /* Add this operand to the list. */
14529 asm_operands = tree_cons (build_tree_list (name, string_literal),
14530 expression,
14531 asm_operands);
14532 /* If the next token is not a `,', there are no more
14533 operands. */
14534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14535 break;
14536 /* Consume the `,'. */
14537 cp_lexer_consume_token (parser->lexer);
14538 }
14539
14540 return nreverse (asm_operands);
14541 }
14542
14543 /* Parse an asm-clobber-list.
14544
14545 asm-clobber-list:
14546 string-literal
14547 asm-clobber-list , string-literal
14548
14549 Returns a TREE_LIST, indicating the clobbers in the order that they
14550 appeared. The TREE_VALUE of each node is a STRING_CST. */
14551
14552 static tree
14553 cp_parser_asm_clobber_list (cp_parser* parser)
14554 {
14555 tree clobbers = NULL_TREE;
14556
14557 while (true)
14558 {
14559 tree string_literal;
14560
14561 /* Look for the string literal. */
14562 string_literal = cp_parser_string_literal (parser, false, false);
14563 /* Add it to the list. */
14564 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14565 /* If the next token is not a `,', then the list is
14566 complete. */
14567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14568 break;
14569 /* Consume the `,' token. */
14570 cp_lexer_consume_token (parser->lexer);
14571 }
14572
14573 return clobbers;
14574 }
14575
14576 /* Parse an (optional) series of attributes.
14577
14578 attributes:
14579 attributes attribute
14580
14581 attribute:
14582 __attribute__ (( attribute-list [opt] ))
14583
14584 The return value is as for cp_parser_attribute_list. */
14585
14586 static tree
14587 cp_parser_attributes_opt (cp_parser* parser)
14588 {
14589 tree attributes = NULL_TREE;
14590
14591 while (true)
14592 {
14593 cp_token *token;
14594 tree attribute_list;
14595
14596 /* Peek at the next token. */
14597 token = cp_lexer_peek_token (parser->lexer);
14598 /* If it's not `__attribute__', then we're done. */
14599 if (token->keyword != RID_ATTRIBUTE)
14600 break;
14601
14602 /* Consume the `__attribute__' keyword. */
14603 cp_lexer_consume_token (parser->lexer);
14604 /* Look for the two `(' tokens. */
14605 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14606 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14607
14608 /* Peek at the next token. */
14609 token = cp_lexer_peek_token (parser->lexer);
14610 if (token->type != CPP_CLOSE_PAREN)
14611 /* Parse the attribute-list. */
14612 attribute_list = cp_parser_attribute_list (parser);
14613 else
14614 /* If the next token is a `)', then there is no attribute
14615 list. */
14616 attribute_list = NULL;
14617
14618 /* Look for the two `)' tokens. */
14619 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14620 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14621
14622 /* Add these new attributes to the list. */
14623 attributes = chainon (attributes, attribute_list);
14624 }
14625
14626 return attributes;
14627 }
14628
14629 /* Parse an attribute-list.
14630
14631 attribute-list:
14632 attribute
14633 attribute-list , attribute
14634
14635 attribute:
14636 identifier
14637 identifier ( identifier )
14638 identifier ( identifier , expression-list )
14639 identifier ( expression-list )
14640
14641 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14642 to an attribute. The TREE_PURPOSE of each node is the identifier
14643 indicating which attribute is in use. The TREE_VALUE represents
14644 the arguments, if any. */
14645
14646 static tree
14647 cp_parser_attribute_list (cp_parser* parser)
14648 {
14649 tree attribute_list = NULL_TREE;
14650 bool save_translate_strings_p = parser->translate_strings_p;
14651
14652 parser->translate_strings_p = false;
14653 while (true)
14654 {
14655 cp_token *token;
14656 tree identifier;
14657 tree attribute;
14658
14659 /* Look for the identifier. We also allow keywords here; for
14660 example `__attribute__ ((const))' is legal. */
14661 token = cp_lexer_peek_token (parser->lexer);
14662 if (token->type == CPP_NAME
14663 || token->type == CPP_KEYWORD)
14664 {
14665 tree arguments = NULL_TREE;
14666
14667 /* Consume the token. */
14668 token = cp_lexer_consume_token (parser->lexer);
14669
14670 /* Save away the identifier that indicates which attribute
14671 this is. */
14672 identifier = token->value;
14673 attribute = build_tree_list (identifier, NULL_TREE);
14674
14675 /* Peek at the next token. */
14676 token = cp_lexer_peek_token (parser->lexer);
14677 /* If it's an `(', then parse the attribute arguments. */
14678 if (token->type == CPP_OPEN_PAREN)
14679 {
14680 arguments = cp_parser_parenthesized_expression_list
14681 (parser, true, /*cast_p=*/false,
14682 /*non_constant_p=*/NULL);
14683 /* Save the arguments away. */
14684 TREE_VALUE (attribute) = arguments;
14685 }
14686
14687 if (arguments != error_mark_node)
14688 {
14689 /* Add this attribute to the list. */
14690 TREE_CHAIN (attribute) = attribute_list;
14691 attribute_list = attribute;
14692 }
14693
14694 token = cp_lexer_peek_token (parser->lexer);
14695 }
14696 /* Now, look for more attributes. If the next token isn't a
14697 `,', we're done. */
14698 if (token->type != CPP_COMMA)
14699 break;
14700
14701 /* Consume the comma and keep going. */
14702 cp_lexer_consume_token (parser->lexer);
14703 }
14704 parser->translate_strings_p = save_translate_strings_p;
14705
14706 /* We built up the list in reverse order. */
14707 return nreverse (attribute_list);
14708 }
14709
14710 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14711 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14712 current value of the PEDANTIC flag, regardless of whether or not
14713 the `__extension__' keyword is present. The caller is responsible
14714 for restoring the value of the PEDANTIC flag. */
14715
14716 static bool
14717 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14718 {
14719 /* Save the old value of the PEDANTIC flag. */
14720 *saved_pedantic = pedantic;
14721
14722 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14723 {
14724 /* Consume the `__extension__' token. */
14725 cp_lexer_consume_token (parser->lexer);
14726 /* We're not being pedantic while the `__extension__' keyword is
14727 in effect. */
14728 pedantic = 0;
14729
14730 return true;
14731 }
14732
14733 return false;
14734 }
14735
14736 /* Parse a label declaration.
14737
14738 label-declaration:
14739 __label__ label-declarator-seq ;
14740
14741 label-declarator-seq:
14742 identifier , label-declarator-seq
14743 identifier */
14744
14745 static void
14746 cp_parser_label_declaration (cp_parser* parser)
14747 {
14748 /* Look for the `__label__' keyword. */
14749 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14750
14751 while (true)
14752 {
14753 tree identifier;
14754
14755 /* Look for an identifier. */
14756 identifier = cp_parser_identifier (parser);
14757 /* If we failed, stop. */
14758 if (identifier == error_mark_node)
14759 break;
14760 /* Declare it as a label. */
14761 finish_label_decl (identifier);
14762 /* If the next token is a `;', stop. */
14763 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14764 break;
14765 /* Look for the `,' separating the label declarations. */
14766 cp_parser_require (parser, CPP_COMMA, "`,'");
14767 }
14768
14769 /* Look for the final `;'. */
14770 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14771 }
14772
14773 /* Support Functions */
14774
14775 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14776 NAME should have one of the representations used for an
14777 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14778 is returned. If PARSER->SCOPE is a dependent type, then a
14779 SCOPE_REF is returned.
14780
14781 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14782 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14783 was formed. Abstractly, such entities should not be passed to this
14784 function, because they do not need to be looked up, but it is
14785 simpler to check for this special case here, rather than at the
14786 call-sites.
14787
14788 In cases not explicitly covered above, this function returns a
14789 DECL, OVERLOAD, or baselink representing the result of the lookup.
14790 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14791 is returned.
14792
14793 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14794 (e.g., "struct") that was used. In that case bindings that do not
14795 refer to types are ignored.
14796
14797 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14798 ignored.
14799
14800 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14801 are ignored.
14802
14803 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14804 types.
14805
14806 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14807 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14808 NULL_TREE otherwise. */
14809
14810 static tree
14811 cp_parser_lookup_name (cp_parser *parser, tree name,
14812 enum tag_types tag_type,
14813 bool is_template,
14814 bool is_namespace,
14815 bool check_dependency,
14816 tree *ambiguous_decls)
14817 {
14818 int flags = 0;
14819 tree decl;
14820 tree object_type = parser->context->object_type;
14821
14822 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14823 flags |= LOOKUP_COMPLAIN;
14824
14825 /* Assume that the lookup will be unambiguous. */
14826 if (ambiguous_decls)
14827 *ambiguous_decls = NULL_TREE;
14828
14829 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14830 no longer valid. Note that if we are parsing tentatively, and
14831 the parse fails, OBJECT_TYPE will be automatically restored. */
14832 parser->context->object_type = NULL_TREE;
14833
14834 if (name == error_mark_node)
14835 return error_mark_node;
14836
14837 /* A template-id has already been resolved; there is no lookup to
14838 do. */
14839 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14840 return name;
14841 if (BASELINK_P (name))
14842 {
14843 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14844 == TEMPLATE_ID_EXPR);
14845 return name;
14846 }
14847
14848 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14849 it should already have been checked to make sure that the name
14850 used matches the type being destroyed. */
14851 if (TREE_CODE (name) == BIT_NOT_EXPR)
14852 {
14853 tree type;
14854
14855 /* Figure out to which type this destructor applies. */
14856 if (parser->scope)
14857 type = parser->scope;
14858 else if (object_type)
14859 type = object_type;
14860 else
14861 type = current_class_type;
14862 /* If that's not a class type, there is no destructor. */
14863 if (!type || !CLASS_TYPE_P (type))
14864 return error_mark_node;
14865 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14866 lazily_declare_fn (sfk_destructor, type);
14867 if (!CLASSTYPE_DESTRUCTORS (type))
14868 return error_mark_node;
14869 /* If it was a class type, return the destructor. */
14870 return CLASSTYPE_DESTRUCTORS (type);
14871 }
14872
14873 /* By this point, the NAME should be an ordinary identifier. If
14874 the id-expression was a qualified name, the qualifying scope is
14875 stored in PARSER->SCOPE at this point. */
14876 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14877
14878 /* Perform the lookup. */
14879 if (parser->scope)
14880 {
14881 bool dependent_p;
14882
14883 if (parser->scope == error_mark_node)
14884 return error_mark_node;
14885
14886 /* If the SCOPE is dependent, the lookup must be deferred until
14887 the template is instantiated -- unless we are explicitly
14888 looking up names in uninstantiated templates. Even then, we
14889 cannot look up the name if the scope is not a class type; it
14890 might, for example, be a template type parameter. */
14891 dependent_p = (TYPE_P (parser->scope)
14892 && !(parser->in_declarator_p
14893 && currently_open_class (parser->scope))
14894 && dependent_type_p (parser->scope));
14895 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14896 && dependent_p)
14897 {
14898 if (tag_type)
14899 {
14900 tree type;
14901
14902 /* The resolution to Core Issue 180 says that `struct
14903 A::B' should be considered a type-name, even if `A'
14904 is dependent. */
14905 type = make_typename_type (parser->scope, name, tag_type,
14906 /*complain=*/tf_error);
14907 decl = TYPE_NAME (type);
14908 }
14909 else if (is_template
14910 && (cp_parser_next_token_ends_template_argument_p (parser)
14911 || cp_lexer_next_token_is (parser->lexer,
14912 CPP_CLOSE_PAREN)))
14913 decl = make_unbound_class_template (parser->scope,
14914 name, NULL_TREE,
14915 /*complain=*/tf_error);
14916 else
14917 decl = build_qualified_name (/*type=*/NULL_TREE,
14918 parser->scope, name,
14919 is_template);
14920 }
14921 else
14922 {
14923 tree pushed_scope = NULL_TREE;
14924
14925 /* If PARSER->SCOPE is a dependent type, then it must be a
14926 class type, and we must not be checking dependencies;
14927 otherwise, we would have processed this lookup above. So
14928 that PARSER->SCOPE is not considered a dependent base by
14929 lookup_member, we must enter the scope here. */
14930 if (dependent_p)
14931 pushed_scope = push_scope (parser->scope);
14932 /* If the PARSER->SCOPE is a template specialization, it
14933 may be instantiated during name lookup. In that case,
14934 errors may be issued. Even if we rollback the current
14935 tentative parse, those errors are valid. */
14936 decl = lookup_qualified_name (parser->scope, name,
14937 tag_type != none_type,
14938 /*complain=*/true);
14939 if (pushed_scope)
14940 pop_scope (pushed_scope);
14941 }
14942 parser->qualifying_scope = parser->scope;
14943 parser->object_scope = NULL_TREE;
14944 }
14945 else if (object_type)
14946 {
14947 tree object_decl = NULL_TREE;
14948 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14949 OBJECT_TYPE is not a class. */
14950 if (CLASS_TYPE_P (object_type))
14951 /* If the OBJECT_TYPE is a template specialization, it may
14952 be instantiated during name lookup. In that case, errors
14953 may be issued. Even if we rollback the current tentative
14954 parse, those errors are valid. */
14955 object_decl = lookup_member (object_type,
14956 name,
14957 /*protect=*/0,
14958 tag_type != none_type);
14959 /* Look it up in the enclosing context, too. */
14960 decl = lookup_name_real (name, tag_type != none_type,
14961 /*nonclass=*/0,
14962 /*block_p=*/true, is_namespace, flags);
14963 parser->object_scope = object_type;
14964 parser->qualifying_scope = NULL_TREE;
14965 if (object_decl)
14966 decl = object_decl;
14967 }
14968 else
14969 {
14970 decl = lookup_name_real (name, tag_type != none_type,
14971 /*nonclass=*/0,
14972 /*block_p=*/true, is_namespace, flags);
14973 parser->qualifying_scope = NULL_TREE;
14974 parser->object_scope = NULL_TREE;
14975 }
14976
14977 /* If the lookup failed, let our caller know. */
14978 if (!decl || decl == error_mark_node)
14979 return error_mark_node;
14980
14981 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14982 if (TREE_CODE (decl) == TREE_LIST)
14983 {
14984 if (ambiguous_decls)
14985 *ambiguous_decls = decl;
14986 /* The error message we have to print is too complicated for
14987 cp_parser_error, so we incorporate its actions directly. */
14988 if (!cp_parser_simulate_error (parser))
14989 {
14990 error ("reference to %qD is ambiguous", name);
14991 print_candidates (decl);
14992 }
14993 return error_mark_node;
14994 }
14995
14996 gcc_assert (DECL_P (decl)
14997 || TREE_CODE (decl) == OVERLOAD
14998 || TREE_CODE (decl) == SCOPE_REF
14999 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15000 || BASELINK_P (decl));
15001
15002 /* If we have resolved the name of a member declaration, check to
15003 see if the declaration is accessible. When the name resolves to
15004 set of overloaded functions, accessibility is checked when
15005 overload resolution is done.
15006
15007 During an explicit instantiation, access is not checked at all,
15008 as per [temp.explicit]. */
15009 if (DECL_P (decl))
15010 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15011
15012 return decl;
15013 }
15014
15015 /* Like cp_parser_lookup_name, but for use in the typical case where
15016 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15017 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15018
15019 static tree
15020 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15021 {
15022 return cp_parser_lookup_name (parser, name,
15023 none_type,
15024 /*is_template=*/false,
15025 /*is_namespace=*/false,
15026 /*check_dependency=*/true,
15027 /*ambiguous_decls=*/NULL);
15028 }
15029
15030 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15031 the current context, return the TYPE_DECL. If TAG_NAME_P is
15032 true, the DECL indicates the class being defined in a class-head,
15033 or declared in an elaborated-type-specifier.
15034
15035 Otherwise, return DECL. */
15036
15037 static tree
15038 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15039 {
15040 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15041 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15042
15043 struct A {
15044 template <typename T> struct B;
15045 };
15046
15047 template <typename T> struct A::B {};
15048
15049 Similarly, in an elaborated-type-specifier:
15050
15051 namespace N { struct X{}; }
15052
15053 struct A {
15054 template <typename T> friend struct N::X;
15055 };
15056
15057 However, if the DECL refers to a class type, and we are in
15058 the scope of the class, then the name lookup automatically
15059 finds the TYPE_DECL created by build_self_reference rather
15060 than a TEMPLATE_DECL. For example, in:
15061
15062 template <class T> struct S {
15063 S s;
15064 };
15065
15066 there is no need to handle such case. */
15067
15068 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15069 return DECL_TEMPLATE_RESULT (decl);
15070
15071 return decl;
15072 }
15073
15074 /* If too many, or too few, template-parameter lists apply to the
15075 declarator, issue an error message. Returns TRUE if all went well,
15076 and FALSE otherwise. */
15077
15078 static bool
15079 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15080 cp_declarator *declarator)
15081 {
15082 unsigned num_templates;
15083
15084 /* We haven't seen any classes that involve template parameters yet. */
15085 num_templates = 0;
15086
15087 switch (declarator->kind)
15088 {
15089 case cdk_id:
15090 if (declarator->u.id.qualifying_scope)
15091 {
15092 tree scope;
15093 tree member;
15094
15095 scope = declarator->u.id.qualifying_scope;
15096 member = declarator->u.id.unqualified_name;
15097
15098 while (scope && CLASS_TYPE_P (scope))
15099 {
15100 /* You're supposed to have one `template <...>'
15101 for every template class, but you don't need one
15102 for a full specialization. For example:
15103
15104 template <class T> struct S{};
15105 template <> struct S<int> { void f(); };
15106 void S<int>::f () {}
15107
15108 is correct; there shouldn't be a `template <>' for
15109 the definition of `S<int>::f'. */
15110 if (CLASSTYPE_TEMPLATE_INFO (scope)
15111 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15112 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15113 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15114 ++num_templates;
15115
15116 scope = TYPE_CONTEXT (scope);
15117 }
15118 }
15119 else if (TREE_CODE (declarator->u.id.unqualified_name)
15120 == TEMPLATE_ID_EXPR)
15121 /* If the DECLARATOR has the form `X<y>' then it uses one
15122 additional level of template parameters. */
15123 ++num_templates;
15124
15125 return cp_parser_check_template_parameters (parser,
15126 num_templates);
15127
15128 case cdk_function:
15129 case cdk_array:
15130 case cdk_pointer:
15131 case cdk_reference:
15132 case cdk_ptrmem:
15133 return (cp_parser_check_declarator_template_parameters
15134 (parser, declarator->declarator));
15135
15136 case cdk_error:
15137 return true;
15138
15139 default:
15140 gcc_unreachable ();
15141 }
15142 return false;
15143 }
15144
15145 /* NUM_TEMPLATES were used in the current declaration. If that is
15146 invalid, return FALSE and issue an error messages. Otherwise,
15147 return TRUE. */
15148
15149 static bool
15150 cp_parser_check_template_parameters (cp_parser* parser,
15151 unsigned num_templates)
15152 {
15153 /* If there are more template classes than parameter lists, we have
15154 something like:
15155
15156 template <class T> void S<T>::R<T>::f (); */
15157 if (parser->num_template_parameter_lists < num_templates)
15158 {
15159 error ("too few template-parameter-lists");
15160 return false;
15161 }
15162 /* If there are the same number of template classes and parameter
15163 lists, that's OK. */
15164 if (parser->num_template_parameter_lists == num_templates)
15165 return true;
15166 /* If there are more, but only one more, then we are referring to a
15167 member template. That's OK too. */
15168 if (parser->num_template_parameter_lists == num_templates + 1)
15169 return true;
15170 /* Otherwise, there are too many template parameter lists. We have
15171 something like:
15172
15173 template <class T> template <class U> void S::f(); */
15174 error ("too many template-parameter-lists");
15175 return false;
15176 }
15177
15178 /* Parse an optional `::' token indicating that the following name is
15179 from the global namespace. If so, PARSER->SCOPE is set to the
15180 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15181 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15182 Returns the new value of PARSER->SCOPE, if the `::' token is
15183 present, and NULL_TREE otherwise. */
15184
15185 static tree
15186 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15187 {
15188 cp_token *token;
15189
15190 /* Peek at the next token. */
15191 token = cp_lexer_peek_token (parser->lexer);
15192 /* If we're looking at a `::' token then we're starting from the
15193 global namespace, not our current location. */
15194 if (token->type == CPP_SCOPE)
15195 {
15196 /* Consume the `::' token. */
15197 cp_lexer_consume_token (parser->lexer);
15198 /* Set the SCOPE so that we know where to start the lookup. */
15199 parser->scope = global_namespace;
15200 parser->qualifying_scope = global_namespace;
15201 parser->object_scope = NULL_TREE;
15202
15203 return parser->scope;
15204 }
15205 else if (!current_scope_valid_p)
15206 {
15207 parser->scope = NULL_TREE;
15208 parser->qualifying_scope = NULL_TREE;
15209 parser->object_scope = NULL_TREE;
15210 }
15211
15212 return NULL_TREE;
15213 }
15214
15215 /* Returns TRUE if the upcoming token sequence is the start of a
15216 constructor declarator. If FRIEND_P is true, the declarator is
15217 preceded by the `friend' specifier. */
15218
15219 static bool
15220 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15221 {
15222 bool constructor_p;
15223 tree type_decl = NULL_TREE;
15224 bool nested_name_p;
15225 cp_token *next_token;
15226
15227 /* The common case is that this is not a constructor declarator, so
15228 try to avoid doing lots of work if at all possible. It's not
15229 valid declare a constructor at function scope. */
15230 if (at_function_scope_p ())
15231 return false;
15232 /* And only certain tokens can begin a constructor declarator. */
15233 next_token = cp_lexer_peek_token (parser->lexer);
15234 if (next_token->type != CPP_NAME
15235 && next_token->type != CPP_SCOPE
15236 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15237 && next_token->type != CPP_TEMPLATE_ID)
15238 return false;
15239
15240 /* Parse tentatively; we are going to roll back all of the tokens
15241 consumed here. */
15242 cp_parser_parse_tentatively (parser);
15243 /* Assume that we are looking at a constructor declarator. */
15244 constructor_p = true;
15245
15246 /* Look for the optional `::' operator. */
15247 cp_parser_global_scope_opt (parser,
15248 /*current_scope_valid_p=*/false);
15249 /* Look for the nested-name-specifier. */
15250 nested_name_p
15251 = (cp_parser_nested_name_specifier_opt (parser,
15252 /*typename_keyword_p=*/false,
15253 /*check_dependency_p=*/false,
15254 /*type_p=*/false,
15255 /*is_declaration=*/false)
15256 != NULL_TREE);
15257 /* Outside of a class-specifier, there must be a
15258 nested-name-specifier. */
15259 if (!nested_name_p &&
15260 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15261 || friend_p))
15262 constructor_p = false;
15263 /* If we still think that this might be a constructor-declarator,
15264 look for a class-name. */
15265 if (constructor_p)
15266 {
15267 /* If we have:
15268
15269 template <typename T> struct S { S(); };
15270 template <typename T> S<T>::S ();
15271
15272 we must recognize that the nested `S' names a class.
15273 Similarly, for:
15274
15275 template <typename T> S<T>::S<T> ();
15276
15277 we must recognize that the nested `S' names a template. */
15278 type_decl = cp_parser_class_name (parser,
15279 /*typename_keyword_p=*/false,
15280 /*template_keyword_p=*/false,
15281 none_type,
15282 /*check_dependency_p=*/false,
15283 /*class_head_p=*/false,
15284 /*is_declaration=*/false);
15285 /* If there was no class-name, then this is not a constructor. */
15286 constructor_p = !cp_parser_error_occurred (parser);
15287 }
15288
15289 /* If we're still considering a constructor, we have to see a `(',
15290 to begin the parameter-declaration-clause, followed by either a
15291 `)', an `...', or a decl-specifier. We need to check for a
15292 type-specifier to avoid being fooled into thinking that:
15293
15294 S::S (f) (int);
15295
15296 is a constructor. (It is actually a function named `f' that
15297 takes one parameter (of type `int') and returns a value of type
15298 `S::S'. */
15299 if (constructor_p
15300 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15301 {
15302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15303 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15304 /* A parameter declaration begins with a decl-specifier,
15305 which is either the "attribute" keyword, a storage class
15306 specifier, or (usually) a type-specifier. */
15307 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15308 && !cp_parser_storage_class_specifier_opt (parser))
15309 {
15310 tree type;
15311 tree pushed_scope = NULL_TREE;
15312 unsigned saved_num_template_parameter_lists;
15313
15314 /* Names appearing in the type-specifier should be looked up
15315 in the scope of the class. */
15316 if (current_class_type)
15317 type = NULL_TREE;
15318 else
15319 {
15320 type = TREE_TYPE (type_decl);
15321 if (TREE_CODE (type) == TYPENAME_TYPE)
15322 {
15323 type = resolve_typename_type (type,
15324 /*only_current_p=*/false);
15325 if (type == error_mark_node)
15326 {
15327 cp_parser_abort_tentative_parse (parser);
15328 return false;
15329 }
15330 }
15331 pushed_scope = push_scope (type);
15332 }
15333
15334 /* Inside the constructor parameter list, surrounding
15335 template-parameter-lists do not apply. */
15336 saved_num_template_parameter_lists
15337 = parser->num_template_parameter_lists;
15338 parser->num_template_parameter_lists = 0;
15339
15340 /* Look for the type-specifier. */
15341 cp_parser_type_specifier (parser,
15342 CP_PARSER_FLAGS_NONE,
15343 /*decl_specs=*/NULL,
15344 /*is_declarator=*/true,
15345 /*declares_class_or_enum=*/NULL,
15346 /*is_cv_qualifier=*/NULL);
15347
15348 parser->num_template_parameter_lists
15349 = saved_num_template_parameter_lists;
15350
15351 /* Leave the scope of the class. */
15352 if (pushed_scope)
15353 pop_scope (pushed_scope);
15354
15355 constructor_p = !cp_parser_error_occurred (parser);
15356 }
15357 }
15358 else
15359 constructor_p = false;
15360 /* We did not really want to consume any tokens. */
15361 cp_parser_abort_tentative_parse (parser);
15362
15363 return constructor_p;
15364 }
15365
15366 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15367 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15368 they must be performed once we are in the scope of the function.
15369
15370 Returns the function defined. */
15371
15372 static tree
15373 cp_parser_function_definition_from_specifiers_and_declarator
15374 (cp_parser* parser,
15375 cp_decl_specifier_seq *decl_specifiers,
15376 tree attributes,
15377 const cp_declarator *declarator)
15378 {
15379 tree fn;
15380 bool success_p;
15381
15382 /* Begin the function-definition. */
15383 success_p = start_function (decl_specifiers, declarator, attributes);
15384
15385 /* The things we're about to see are not directly qualified by any
15386 template headers we've seen thus far. */
15387 reset_specialization ();
15388
15389 /* If there were names looked up in the decl-specifier-seq that we
15390 did not check, check them now. We must wait until we are in the
15391 scope of the function to perform the checks, since the function
15392 might be a friend. */
15393 perform_deferred_access_checks ();
15394
15395 if (!success_p)
15396 {
15397 /* Skip the entire function. */
15398 cp_parser_skip_to_end_of_block_or_statement (parser);
15399 fn = error_mark_node;
15400 }
15401 else
15402 fn = cp_parser_function_definition_after_declarator (parser,
15403 /*inline_p=*/false);
15404
15405 return fn;
15406 }
15407
15408 /* Parse the part of a function-definition that follows the
15409 declarator. INLINE_P is TRUE iff this function is an inline
15410 function defined with a class-specifier.
15411
15412 Returns the function defined. */
15413
15414 static tree
15415 cp_parser_function_definition_after_declarator (cp_parser* parser,
15416 bool inline_p)
15417 {
15418 tree fn;
15419 bool ctor_initializer_p = false;
15420 bool saved_in_unbraced_linkage_specification_p;
15421 unsigned saved_num_template_parameter_lists;
15422
15423 /* If the next token is `return', then the code may be trying to
15424 make use of the "named return value" extension that G++ used to
15425 support. */
15426 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15427 {
15428 /* Consume the `return' keyword. */
15429 cp_lexer_consume_token (parser->lexer);
15430 /* Look for the identifier that indicates what value is to be
15431 returned. */
15432 cp_parser_identifier (parser);
15433 /* Issue an error message. */
15434 error ("named return values are no longer supported");
15435 /* Skip tokens until we reach the start of the function body. */
15436 while (true)
15437 {
15438 cp_token *token = cp_lexer_peek_token (parser->lexer);
15439 if (token->type == CPP_OPEN_BRACE
15440 || token->type == CPP_EOF
15441 || token->type == CPP_PRAGMA_EOL)
15442 break;
15443 cp_lexer_consume_token (parser->lexer);
15444 }
15445 }
15446 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15447 anything declared inside `f'. */
15448 saved_in_unbraced_linkage_specification_p
15449 = parser->in_unbraced_linkage_specification_p;
15450 parser->in_unbraced_linkage_specification_p = false;
15451 /* Inside the function, surrounding template-parameter-lists do not
15452 apply. */
15453 saved_num_template_parameter_lists
15454 = parser->num_template_parameter_lists;
15455 parser->num_template_parameter_lists = 0;
15456 /* If the next token is `try', then we are looking at a
15457 function-try-block. */
15458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15459 ctor_initializer_p = cp_parser_function_try_block (parser);
15460 /* A function-try-block includes the function-body, so we only do
15461 this next part if we're not processing a function-try-block. */
15462 else
15463 ctor_initializer_p
15464 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15465
15466 /* Finish the function. */
15467 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15468 (inline_p ? 2 : 0));
15469 /* Generate code for it, if necessary. */
15470 expand_or_defer_fn (fn);
15471 /* Restore the saved values. */
15472 parser->in_unbraced_linkage_specification_p
15473 = saved_in_unbraced_linkage_specification_p;
15474 parser->num_template_parameter_lists
15475 = saved_num_template_parameter_lists;
15476
15477 return fn;
15478 }
15479
15480 /* Parse a template-declaration, assuming that the `export' (and
15481 `extern') keywords, if present, has already been scanned. MEMBER_P
15482 is as for cp_parser_template_declaration. */
15483
15484 static void
15485 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15486 {
15487 tree decl = NULL_TREE;
15488 tree checks;
15489 tree parameter_list;
15490 bool friend_p = false;
15491 bool need_lang_pop;
15492
15493 /* Look for the `template' keyword. */
15494 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15495 return;
15496
15497 /* And the `<'. */
15498 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15499 return;
15500 /* [temp]
15501
15502 A template ... shall not have C linkage. */
15503 if (current_lang_name == lang_name_c)
15504 {
15505 error ("template with C linkage");
15506 /* Give it C++ linkage to avoid confusing other parts of the
15507 front end. */
15508 push_lang_context (lang_name_cplusplus);
15509 need_lang_pop = true;
15510 }
15511 else
15512 need_lang_pop = false;
15513
15514 /* We cannot perform access checks on the template parameter
15515 declarations until we know what is being declared, just as we
15516 cannot check the decl-specifier list. */
15517 push_deferring_access_checks (dk_deferred);
15518
15519 /* If the next token is `>', then we have an invalid
15520 specialization. Rather than complain about an invalid template
15521 parameter, issue an error message here. */
15522 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15523 {
15524 cp_parser_error (parser, "invalid explicit specialization");
15525 begin_specialization ();
15526 parameter_list = NULL_TREE;
15527 }
15528 else
15529 /* Parse the template parameters. */
15530 parameter_list = cp_parser_template_parameter_list (parser);
15531
15532 /* Get the deferred access checks from the parameter list. These
15533 will be checked once we know what is being declared, as for a
15534 member template the checks must be performed in the scope of the
15535 class containing the member. */
15536 checks = get_deferred_access_checks ();
15537
15538 /* Look for the `>'. */
15539 cp_parser_skip_to_end_of_template_parameter_list (parser);
15540 /* We just processed one more parameter list. */
15541 ++parser->num_template_parameter_lists;
15542 /* If the next token is `template', there are more template
15543 parameters. */
15544 if (cp_lexer_next_token_is_keyword (parser->lexer,
15545 RID_TEMPLATE))
15546 cp_parser_template_declaration_after_export (parser, member_p);
15547 else
15548 {
15549 /* There are no access checks when parsing a template, as we do not
15550 know if a specialization will be a friend. */
15551 push_deferring_access_checks (dk_no_check);
15552 decl = cp_parser_single_declaration (parser,
15553 checks,
15554 member_p,
15555 &friend_p);
15556 pop_deferring_access_checks ();
15557
15558 /* If this is a member template declaration, let the front
15559 end know. */
15560 if (member_p && !friend_p && decl)
15561 {
15562 if (TREE_CODE (decl) == TYPE_DECL)
15563 cp_parser_check_access_in_redeclaration (decl);
15564
15565 decl = finish_member_template_decl (decl);
15566 }
15567 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15568 make_friend_class (current_class_type, TREE_TYPE (decl),
15569 /*complain=*/true);
15570 }
15571 /* We are done with the current parameter list. */
15572 --parser->num_template_parameter_lists;
15573
15574 pop_deferring_access_checks ();
15575
15576 /* Finish up. */
15577 finish_template_decl (parameter_list);
15578
15579 /* Register member declarations. */
15580 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15581 finish_member_declaration (decl);
15582 /* For the erroneous case of a template with C linkage, we pushed an
15583 implicit C++ linkage scope; exit that scope now. */
15584 if (need_lang_pop)
15585 pop_lang_context ();
15586 /* If DECL is a function template, we must return to parse it later.
15587 (Even though there is no definition, there might be default
15588 arguments that need handling.) */
15589 if (member_p && decl
15590 && (TREE_CODE (decl) == FUNCTION_DECL
15591 || DECL_FUNCTION_TEMPLATE_P (decl)))
15592 TREE_VALUE (parser->unparsed_functions_queues)
15593 = tree_cons (NULL_TREE, decl,
15594 TREE_VALUE (parser->unparsed_functions_queues));
15595 }
15596
15597 /* Perform the deferred access checks from a template-parameter-list.
15598 CHECKS is a TREE_LIST of access checks, as returned by
15599 get_deferred_access_checks. */
15600
15601 static void
15602 cp_parser_perform_template_parameter_access_checks (tree checks)
15603 {
15604 ++processing_template_parmlist;
15605 perform_access_checks (checks);
15606 --processing_template_parmlist;
15607 }
15608
15609 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15610 `function-definition' sequence. MEMBER_P is true, this declaration
15611 appears in a class scope.
15612
15613 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15614 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15615
15616 static tree
15617 cp_parser_single_declaration (cp_parser* parser,
15618 tree checks,
15619 bool member_p,
15620 bool* friend_p)
15621 {
15622 int declares_class_or_enum;
15623 tree decl = NULL_TREE;
15624 cp_decl_specifier_seq decl_specifiers;
15625 bool function_definition_p = false;
15626
15627 /* This function is only used when processing a template
15628 declaration. */
15629 gcc_assert (innermost_scope_kind () == sk_template_parms
15630 || innermost_scope_kind () == sk_template_spec);
15631
15632 /* Defer access checks until we know what is being declared. */
15633 push_deferring_access_checks (dk_deferred);
15634
15635 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15636 alternative. */
15637 cp_parser_decl_specifier_seq (parser,
15638 CP_PARSER_FLAGS_OPTIONAL,
15639 &decl_specifiers,
15640 &declares_class_or_enum);
15641 if (friend_p)
15642 *friend_p = cp_parser_friend_p (&decl_specifiers);
15643
15644 /* There are no template typedefs. */
15645 if (decl_specifiers.specs[(int) ds_typedef])
15646 {
15647 error ("template declaration of %qs", "typedef");
15648 decl = error_mark_node;
15649 }
15650
15651 /* Gather up the access checks that occurred the
15652 decl-specifier-seq. */
15653 stop_deferring_access_checks ();
15654
15655 /* Check for the declaration of a template class. */
15656 if (declares_class_or_enum)
15657 {
15658 if (cp_parser_declares_only_class_p (parser))
15659 {
15660 decl = shadow_tag (&decl_specifiers);
15661
15662 /* In this case:
15663
15664 struct C {
15665 friend template <typename T> struct A<T>::B;
15666 };
15667
15668 A<T>::B will be represented by a TYPENAME_TYPE, and
15669 therefore not recognized by shadow_tag. */
15670 if (friend_p && *friend_p
15671 && !decl
15672 && decl_specifiers.type
15673 && TYPE_P (decl_specifiers.type))
15674 decl = decl_specifiers.type;
15675
15676 if (decl && decl != error_mark_node)
15677 decl = TYPE_NAME (decl);
15678 else
15679 decl = error_mark_node;
15680
15681 /* Perform access checks for template parameters. */
15682 cp_parser_perform_template_parameter_access_checks (checks);
15683 }
15684 }
15685 /* If it's not a template class, try for a template function. If
15686 the next token is a `;', then this declaration does not declare
15687 anything. But, if there were errors in the decl-specifiers, then
15688 the error might well have come from an attempted class-specifier.
15689 In that case, there's no need to warn about a missing declarator. */
15690 if (!decl
15691 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15692 || decl_specifiers.type != error_mark_node))
15693 decl = cp_parser_init_declarator (parser,
15694 &decl_specifiers,
15695 checks,
15696 /*function_definition_allowed_p=*/true,
15697 member_p,
15698 declares_class_or_enum,
15699 &function_definition_p);
15700
15701 pop_deferring_access_checks ();
15702
15703 /* Clear any current qualification; whatever comes next is the start
15704 of something new. */
15705 parser->scope = NULL_TREE;
15706 parser->qualifying_scope = NULL_TREE;
15707 parser->object_scope = NULL_TREE;
15708 /* Look for a trailing `;' after the declaration. */
15709 if (!function_definition_p
15710 && (decl == error_mark_node
15711 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15712 cp_parser_skip_to_end_of_block_or_statement (parser);
15713
15714 return decl;
15715 }
15716
15717 /* Parse a cast-expression that is not the operand of a unary "&". */
15718
15719 static tree
15720 cp_parser_simple_cast_expression (cp_parser *parser)
15721 {
15722 return cp_parser_cast_expression (parser, /*address_p=*/false,
15723 /*cast_p=*/false);
15724 }
15725
15726 /* Parse a functional cast to TYPE. Returns an expression
15727 representing the cast. */
15728
15729 static tree
15730 cp_parser_functional_cast (cp_parser* parser, tree type)
15731 {
15732 tree expression_list;
15733 tree cast;
15734
15735 expression_list
15736 = cp_parser_parenthesized_expression_list (parser, false,
15737 /*cast_p=*/true,
15738 /*non_constant_p=*/NULL);
15739
15740 cast = build_functional_cast (type, expression_list);
15741 /* [expr.const]/1: In an integral constant expression "only type
15742 conversions to integral or enumeration type can be used". */
15743 if (TREE_CODE (type) == TYPE_DECL)
15744 type = TREE_TYPE (type);
15745 if (cast != error_mark_node
15746 && !cast_valid_in_integral_constant_expression_p (type)
15747 && (cp_parser_non_integral_constant_expression
15748 (parser, "a call to a constructor")))
15749 return error_mark_node;
15750 return cast;
15751 }
15752
15753 /* Save the tokens that make up the body of a member function defined
15754 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15755 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15756 specifiers applied to the declaration. Returns the FUNCTION_DECL
15757 for the member function. */
15758
15759 static tree
15760 cp_parser_save_member_function_body (cp_parser* parser,
15761 cp_decl_specifier_seq *decl_specifiers,
15762 cp_declarator *declarator,
15763 tree attributes)
15764 {
15765 cp_token *first;
15766 cp_token *last;
15767 tree fn;
15768
15769 /* Create the function-declaration. */
15770 fn = start_method (decl_specifiers, declarator, attributes);
15771 /* If something went badly wrong, bail out now. */
15772 if (fn == error_mark_node)
15773 {
15774 /* If there's a function-body, skip it. */
15775 if (cp_parser_token_starts_function_definition_p
15776 (cp_lexer_peek_token (parser->lexer)))
15777 cp_parser_skip_to_end_of_block_or_statement (parser);
15778 return error_mark_node;
15779 }
15780
15781 /* Remember it, if there default args to post process. */
15782 cp_parser_save_default_args (parser, fn);
15783
15784 /* Save away the tokens that make up the body of the
15785 function. */
15786 first = parser->lexer->next_token;
15787 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15788 /* Handle function try blocks. */
15789 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15790 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15791 last = parser->lexer->next_token;
15792
15793 /* Save away the inline definition; we will process it when the
15794 class is complete. */
15795 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15796 DECL_PENDING_INLINE_P (fn) = 1;
15797
15798 /* We need to know that this was defined in the class, so that
15799 friend templates are handled correctly. */
15800 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15801
15802 /* We're done with the inline definition. */
15803 finish_method (fn);
15804
15805 /* Add FN to the queue of functions to be parsed later. */
15806 TREE_VALUE (parser->unparsed_functions_queues)
15807 = tree_cons (NULL_TREE, fn,
15808 TREE_VALUE (parser->unparsed_functions_queues));
15809
15810 return fn;
15811 }
15812
15813 /* Parse a template-argument-list, as well as the trailing ">" (but
15814 not the opening ">"). See cp_parser_template_argument_list for the
15815 return value. */
15816
15817 static tree
15818 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15819 {
15820 tree arguments;
15821 tree saved_scope;
15822 tree saved_qualifying_scope;
15823 tree saved_object_scope;
15824 bool saved_greater_than_is_operator_p;
15825 bool saved_skip_evaluation;
15826
15827 /* [temp.names]
15828
15829 When parsing a template-id, the first non-nested `>' is taken as
15830 the end of the template-argument-list rather than a greater-than
15831 operator. */
15832 saved_greater_than_is_operator_p
15833 = parser->greater_than_is_operator_p;
15834 parser->greater_than_is_operator_p = false;
15835 /* Parsing the argument list may modify SCOPE, so we save it
15836 here. */
15837 saved_scope = parser->scope;
15838 saved_qualifying_scope = parser->qualifying_scope;
15839 saved_object_scope = parser->object_scope;
15840 /* We need to evaluate the template arguments, even though this
15841 template-id may be nested within a "sizeof". */
15842 saved_skip_evaluation = skip_evaluation;
15843 skip_evaluation = false;
15844 /* Parse the template-argument-list itself. */
15845 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15846 arguments = NULL_TREE;
15847 else
15848 arguments = cp_parser_template_argument_list (parser);
15849 /* Look for the `>' that ends the template-argument-list. If we find
15850 a '>>' instead, it's probably just a typo. */
15851 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15852 {
15853 if (!saved_greater_than_is_operator_p)
15854 {
15855 /* If we're in a nested template argument list, the '>>' has
15856 to be a typo for '> >'. We emit the error message, but we
15857 continue parsing and we push a '>' as next token, so that
15858 the argument list will be parsed correctly. Note that the
15859 global source location is still on the token before the
15860 '>>', so we need to say explicitly where we want it. */
15861 cp_token *token = cp_lexer_peek_token (parser->lexer);
15862 error ("%H%<>>%> should be %<> >%> "
15863 "within a nested template argument list",
15864 &token->location);
15865
15866 /* ??? Proper recovery should terminate two levels of
15867 template argument list here. */
15868 token->type = CPP_GREATER;
15869 }
15870 else
15871 {
15872 /* If this is not a nested template argument list, the '>>'
15873 is a typo for '>'. Emit an error message and continue.
15874 Same deal about the token location, but here we can get it
15875 right by consuming the '>>' before issuing the diagnostic. */
15876 cp_lexer_consume_token (parser->lexer);
15877 error ("spurious %<>>%>, use %<>%> to terminate "
15878 "a template argument list");
15879 }
15880 }
15881 else
15882 cp_parser_skip_to_end_of_template_parameter_list (parser);
15883 /* The `>' token might be a greater-than operator again now. */
15884 parser->greater_than_is_operator_p
15885 = saved_greater_than_is_operator_p;
15886 /* Restore the SAVED_SCOPE. */
15887 parser->scope = saved_scope;
15888 parser->qualifying_scope = saved_qualifying_scope;
15889 parser->object_scope = saved_object_scope;
15890 skip_evaluation = saved_skip_evaluation;
15891
15892 return arguments;
15893 }
15894
15895 /* MEMBER_FUNCTION is a member function, or a friend. If default
15896 arguments, or the body of the function have not yet been parsed,
15897 parse them now. */
15898
15899 static void
15900 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15901 {
15902 /* If this member is a template, get the underlying
15903 FUNCTION_DECL. */
15904 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15905 member_function = DECL_TEMPLATE_RESULT (member_function);
15906
15907 /* There should not be any class definitions in progress at this
15908 point; the bodies of members are only parsed outside of all class
15909 definitions. */
15910 gcc_assert (parser->num_classes_being_defined == 0);
15911 /* While we're parsing the member functions we might encounter more
15912 classes. We want to handle them right away, but we don't want
15913 them getting mixed up with functions that are currently in the
15914 queue. */
15915 parser->unparsed_functions_queues
15916 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15917
15918 /* Make sure that any template parameters are in scope. */
15919 maybe_begin_member_template_processing (member_function);
15920
15921 /* If the body of the function has not yet been parsed, parse it
15922 now. */
15923 if (DECL_PENDING_INLINE_P (member_function))
15924 {
15925 tree function_scope;
15926 cp_token_cache *tokens;
15927
15928 /* The function is no longer pending; we are processing it. */
15929 tokens = DECL_PENDING_INLINE_INFO (member_function);
15930 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15931 DECL_PENDING_INLINE_P (member_function) = 0;
15932
15933 /* If this is a local class, enter the scope of the containing
15934 function. */
15935 function_scope = current_function_decl;
15936 if (function_scope)
15937 push_function_context_to (function_scope);
15938
15939
15940 /* Push the body of the function onto the lexer stack. */
15941 cp_parser_push_lexer_for_tokens (parser, tokens);
15942
15943 /* Let the front end know that we going to be defining this
15944 function. */
15945 start_preparsed_function (member_function, NULL_TREE,
15946 SF_PRE_PARSED | SF_INCLASS_INLINE);
15947
15948 /* Don't do access checking if it is a templated function. */
15949 if (processing_template_decl)
15950 push_deferring_access_checks (dk_no_check);
15951
15952 /* Now, parse the body of the function. */
15953 cp_parser_function_definition_after_declarator (parser,
15954 /*inline_p=*/true);
15955
15956 if (processing_template_decl)
15957 pop_deferring_access_checks ();
15958
15959 /* Leave the scope of the containing function. */
15960 if (function_scope)
15961 pop_function_context_from (function_scope);
15962 cp_parser_pop_lexer (parser);
15963 }
15964
15965 /* Remove any template parameters from the symbol table. */
15966 maybe_end_member_template_processing ();
15967
15968 /* Restore the queue. */
15969 parser->unparsed_functions_queues
15970 = TREE_CHAIN (parser->unparsed_functions_queues);
15971 }
15972
15973 /* If DECL contains any default args, remember it on the unparsed
15974 functions queue. */
15975
15976 static void
15977 cp_parser_save_default_args (cp_parser* parser, tree decl)
15978 {
15979 tree probe;
15980
15981 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15982 probe;
15983 probe = TREE_CHAIN (probe))
15984 if (TREE_PURPOSE (probe))
15985 {
15986 TREE_PURPOSE (parser->unparsed_functions_queues)
15987 = tree_cons (current_class_type, decl,
15988 TREE_PURPOSE (parser->unparsed_functions_queues));
15989 break;
15990 }
15991 }
15992
15993 /* FN is a FUNCTION_DECL which may contains a parameter with an
15994 unparsed DEFAULT_ARG. Parse the default args now. This function
15995 assumes that the current scope is the scope in which the default
15996 argument should be processed. */
15997
15998 static void
15999 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16000 {
16001 bool saved_local_variables_forbidden_p;
16002 tree parm;
16003
16004 /* While we're parsing the default args, we might (due to the
16005 statement expression extension) encounter more classes. We want
16006 to handle them right away, but we don't want them getting mixed
16007 up with default args that are currently in the queue. */
16008 parser->unparsed_functions_queues
16009 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16010
16011 /* Local variable names (and the `this' keyword) may not appear
16012 in a default argument. */
16013 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16014 parser->local_variables_forbidden_p = true;
16015
16016 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16017 parm;
16018 parm = TREE_CHAIN (parm))
16019 {
16020 cp_token_cache *tokens;
16021 tree default_arg = TREE_PURPOSE (parm);
16022 tree parsed_arg;
16023 VEC(tree,gc) *insts;
16024 tree copy;
16025 unsigned ix;
16026
16027 if (!default_arg)
16028 continue;
16029
16030 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16031 /* This can happen for a friend declaration for a function
16032 already declared with default arguments. */
16033 continue;
16034
16035 /* Push the saved tokens for the default argument onto the parser's
16036 lexer stack. */
16037 tokens = DEFARG_TOKENS (default_arg);
16038 cp_parser_push_lexer_for_tokens (parser, tokens);
16039
16040 /* Parse the assignment-expression. */
16041 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16042
16043 if (!processing_template_decl)
16044 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16045
16046 TREE_PURPOSE (parm) = parsed_arg;
16047
16048 /* Update any instantiations we've already created. */
16049 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16050 VEC_iterate (tree, insts, ix, copy); ix++)
16051 TREE_PURPOSE (copy) = parsed_arg;
16052
16053 /* If the token stream has not been completely used up, then
16054 there was extra junk after the end of the default
16055 argument. */
16056 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16057 cp_parser_error (parser, "expected %<,%>");
16058
16059 /* Revert to the main lexer. */
16060 cp_parser_pop_lexer (parser);
16061 }
16062
16063 /* Make sure no default arg is missing. */
16064 check_default_args (fn);
16065
16066 /* Restore the state of local_variables_forbidden_p. */
16067 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16068
16069 /* Restore the queue. */
16070 parser->unparsed_functions_queues
16071 = TREE_CHAIN (parser->unparsed_functions_queues);
16072 }
16073
16074 /* Parse the operand of `sizeof' (or a similar operator). Returns
16075 either a TYPE or an expression, depending on the form of the
16076 input. The KEYWORD indicates which kind of expression we have
16077 encountered. */
16078
16079 static tree
16080 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16081 {
16082 static const char *format;
16083 tree expr = NULL_TREE;
16084 const char *saved_message;
16085 bool saved_integral_constant_expression_p;
16086 bool saved_non_integral_constant_expression_p;
16087
16088 /* Initialize FORMAT the first time we get here. */
16089 if (!format)
16090 format = "types may not be defined in '%s' expressions";
16091
16092 /* Types cannot be defined in a `sizeof' expression. Save away the
16093 old message. */
16094 saved_message = parser->type_definition_forbidden_message;
16095 /* And create the new one. */
16096 parser->type_definition_forbidden_message
16097 = XNEWVEC (const char, strlen (format)
16098 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16099 + 1 /* `\0' */);
16100 sprintf ((char *) parser->type_definition_forbidden_message,
16101 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16102
16103 /* The restrictions on constant-expressions do not apply inside
16104 sizeof expressions. */
16105 saved_integral_constant_expression_p
16106 = parser->integral_constant_expression_p;
16107 saved_non_integral_constant_expression_p
16108 = parser->non_integral_constant_expression_p;
16109 parser->integral_constant_expression_p = false;
16110
16111 /* Do not actually evaluate the expression. */
16112 ++skip_evaluation;
16113 /* If it's a `(', then we might be looking at the type-id
16114 construction. */
16115 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16116 {
16117 tree type;
16118 bool saved_in_type_id_in_expr_p;
16119
16120 /* We can't be sure yet whether we're looking at a type-id or an
16121 expression. */
16122 cp_parser_parse_tentatively (parser);
16123 /* Consume the `('. */
16124 cp_lexer_consume_token (parser->lexer);
16125 /* Parse the type-id. */
16126 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16127 parser->in_type_id_in_expr_p = true;
16128 type = cp_parser_type_id (parser);
16129 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16130 /* Now, look for the trailing `)'. */
16131 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16132 /* If all went well, then we're done. */
16133 if (cp_parser_parse_definitely (parser))
16134 {
16135 cp_decl_specifier_seq decl_specs;
16136
16137 /* Build a trivial decl-specifier-seq. */
16138 clear_decl_specs (&decl_specs);
16139 decl_specs.type = type;
16140
16141 /* Call grokdeclarator to figure out what type this is. */
16142 expr = grokdeclarator (NULL,
16143 &decl_specs,
16144 TYPENAME,
16145 /*initialized=*/0,
16146 /*attrlist=*/NULL);
16147 }
16148 }
16149
16150 /* If the type-id production did not work out, then we must be
16151 looking at the unary-expression production. */
16152 if (!expr)
16153 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16154 /*cast_p=*/false);
16155 /* Go back to evaluating expressions. */
16156 --skip_evaluation;
16157
16158 /* Free the message we created. */
16159 free ((char *) parser->type_definition_forbidden_message);
16160 /* And restore the old one. */
16161 parser->type_definition_forbidden_message = saved_message;
16162 parser->integral_constant_expression_p
16163 = saved_integral_constant_expression_p;
16164 parser->non_integral_constant_expression_p
16165 = saved_non_integral_constant_expression_p;
16166
16167 return expr;
16168 }
16169
16170 /* If the current declaration has no declarator, return true. */
16171
16172 static bool
16173 cp_parser_declares_only_class_p (cp_parser *parser)
16174 {
16175 /* If the next token is a `;' or a `,' then there is no
16176 declarator. */
16177 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16178 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16179 }
16180
16181 /* Update the DECL_SPECS to reflect the storage class indicated by
16182 KEYWORD. */
16183
16184 static void
16185 cp_parser_set_storage_class (cp_parser *parser,
16186 cp_decl_specifier_seq *decl_specs,
16187 enum rid keyword)
16188 {
16189 cp_storage_class storage_class;
16190
16191 if (parser->in_unbraced_linkage_specification_p)
16192 {
16193 error ("invalid use of %qD in linkage specification",
16194 ridpointers[keyword]);
16195 return;
16196 }
16197 else if (decl_specs->storage_class != sc_none)
16198 {
16199 decl_specs->conflicting_specifiers_p = true;
16200 return;
16201 }
16202
16203 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16204 && decl_specs->specs[(int) ds_thread])
16205 {
16206 error ("%<__thread%> before %qD", ridpointers[keyword]);
16207 decl_specs->specs[(int) ds_thread] = 0;
16208 }
16209
16210 switch (keyword)
16211 {
16212 case RID_AUTO:
16213 storage_class = sc_auto;
16214 break;
16215 case RID_REGISTER:
16216 storage_class = sc_register;
16217 break;
16218 case RID_STATIC:
16219 storage_class = sc_static;
16220 break;
16221 case RID_EXTERN:
16222 storage_class = sc_extern;
16223 break;
16224 case RID_MUTABLE:
16225 storage_class = sc_mutable;
16226 break;
16227 default:
16228 gcc_unreachable ();
16229 }
16230 decl_specs->storage_class = storage_class;
16231
16232 /* A storage class specifier cannot be applied alongside a typedef
16233 specifier. If there is a typedef specifier present then set
16234 conflicting_specifiers_p which will trigger an error later
16235 on in grokdeclarator. */
16236 if (decl_specs->specs[(int)ds_typedef])
16237 decl_specs->conflicting_specifiers_p = true;
16238 }
16239
16240 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16241 is true, the type is a user-defined type; otherwise it is a
16242 built-in type specified by a keyword. */
16243
16244 static void
16245 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16246 tree type_spec,
16247 bool user_defined_p)
16248 {
16249 decl_specs->any_specifiers_p = true;
16250
16251 /* If the user tries to redeclare bool or wchar_t (with, for
16252 example, in "typedef int wchar_t;") we remember that this is what
16253 happened. In system headers, we ignore these declarations so
16254 that G++ can work with system headers that are not C++-safe. */
16255 if (decl_specs->specs[(int) ds_typedef]
16256 && !user_defined_p
16257 && (type_spec == boolean_type_node
16258 || type_spec == wchar_type_node)
16259 && (decl_specs->type
16260 || decl_specs->specs[(int) ds_long]
16261 || decl_specs->specs[(int) ds_short]
16262 || decl_specs->specs[(int) ds_unsigned]
16263 || decl_specs->specs[(int) ds_signed]))
16264 {
16265 decl_specs->redefined_builtin_type = type_spec;
16266 if (!decl_specs->type)
16267 {
16268 decl_specs->type = type_spec;
16269 decl_specs->user_defined_type_p = false;
16270 }
16271 }
16272 else if (decl_specs->type)
16273 decl_specs->multiple_types_p = true;
16274 else
16275 {
16276 decl_specs->type = type_spec;
16277 decl_specs->user_defined_type_p = user_defined_p;
16278 decl_specs->redefined_builtin_type = NULL_TREE;
16279 }
16280 }
16281
16282 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16283 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16284
16285 static bool
16286 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16287 {
16288 return decl_specifiers->specs[(int) ds_friend] != 0;
16289 }
16290
16291 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16292 issue an error message indicating that TOKEN_DESC was expected.
16293
16294 Returns the token consumed, if the token had the appropriate type.
16295 Otherwise, returns NULL. */
16296
16297 static cp_token *
16298 cp_parser_require (cp_parser* parser,
16299 enum cpp_ttype type,
16300 const char* token_desc)
16301 {
16302 if (cp_lexer_next_token_is (parser->lexer, type))
16303 return cp_lexer_consume_token (parser->lexer);
16304 else
16305 {
16306 /* Output the MESSAGE -- unless we're parsing tentatively. */
16307 if (!cp_parser_simulate_error (parser))
16308 {
16309 char *message = concat ("expected ", token_desc, NULL);
16310 cp_parser_error (parser, message);
16311 free (message);
16312 }
16313 return NULL;
16314 }
16315 }
16316
16317 /* An error message is produced if the next token is not '>'.
16318 All further tokens are skipped until the desired token is
16319 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16320
16321 static void
16322 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16323 {
16324 /* Current level of '< ... >'. */
16325 unsigned level = 0;
16326 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16327 unsigned nesting_depth = 0;
16328
16329 /* Are we ready, yet? If not, issue error message. */
16330 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16331 return;
16332
16333 /* Skip tokens until the desired token is found. */
16334 while (true)
16335 {
16336 /* Peek at the next token. */
16337 switch (cp_lexer_peek_token (parser->lexer)->type)
16338 {
16339 case CPP_LESS:
16340 if (!nesting_depth)
16341 ++level;
16342 break;
16343
16344 case CPP_GREATER:
16345 if (!nesting_depth && level-- == 0)
16346 {
16347 /* We've reached the token we want, consume it and stop. */
16348 cp_lexer_consume_token (parser->lexer);
16349 return;
16350 }
16351 break;
16352
16353 case CPP_OPEN_PAREN:
16354 case CPP_OPEN_SQUARE:
16355 ++nesting_depth;
16356 break;
16357
16358 case CPP_CLOSE_PAREN:
16359 case CPP_CLOSE_SQUARE:
16360 if (nesting_depth-- == 0)
16361 return;
16362 break;
16363
16364 case CPP_EOF:
16365 case CPP_PRAGMA_EOL:
16366 case CPP_SEMICOLON:
16367 case CPP_OPEN_BRACE:
16368 case CPP_CLOSE_BRACE:
16369 /* The '>' was probably forgotten, don't look further. */
16370 return;
16371
16372 default:
16373 break;
16374 }
16375
16376 /* Consume this token. */
16377 cp_lexer_consume_token (parser->lexer);
16378 }
16379 }
16380
16381 /* If the next token is the indicated keyword, consume it. Otherwise,
16382 issue an error message indicating that TOKEN_DESC was expected.
16383
16384 Returns the token consumed, if the token had the appropriate type.
16385 Otherwise, returns NULL. */
16386
16387 static cp_token *
16388 cp_parser_require_keyword (cp_parser* parser,
16389 enum rid keyword,
16390 const char* token_desc)
16391 {
16392 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16393
16394 if (token && token->keyword != keyword)
16395 {
16396 dyn_string_t error_msg;
16397
16398 /* Format the error message. */
16399 error_msg = dyn_string_new (0);
16400 dyn_string_append_cstr (error_msg, "expected ");
16401 dyn_string_append_cstr (error_msg, token_desc);
16402 cp_parser_error (parser, error_msg->s);
16403 dyn_string_delete (error_msg);
16404 return NULL;
16405 }
16406
16407 return token;
16408 }
16409
16410 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16411 function-definition. */
16412
16413 static bool
16414 cp_parser_token_starts_function_definition_p (cp_token* token)
16415 {
16416 return (/* An ordinary function-body begins with an `{'. */
16417 token->type == CPP_OPEN_BRACE
16418 /* A ctor-initializer begins with a `:'. */
16419 || token->type == CPP_COLON
16420 /* A function-try-block begins with `try'. */
16421 || token->keyword == RID_TRY
16422 /* The named return value extension begins with `return'. */
16423 || token->keyword == RID_RETURN);
16424 }
16425
16426 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16427 definition. */
16428
16429 static bool
16430 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16431 {
16432 cp_token *token;
16433
16434 token = cp_lexer_peek_token (parser->lexer);
16435 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16436 }
16437
16438 /* Returns TRUE iff the next token is the "," or ">" ending a
16439 template-argument. */
16440
16441 static bool
16442 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16443 {
16444 cp_token *token;
16445
16446 token = cp_lexer_peek_token (parser->lexer);
16447 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16448 }
16449
16450 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16451 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16452
16453 static bool
16454 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16455 size_t n)
16456 {
16457 cp_token *token;
16458
16459 token = cp_lexer_peek_nth_token (parser->lexer, n);
16460 if (token->type == CPP_LESS)
16461 return true;
16462 /* Check for the sequence `<::' in the original code. It would be lexed as
16463 `[:', where `[' is a digraph, and there is no whitespace before
16464 `:'. */
16465 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16466 {
16467 cp_token *token2;
16468 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16469 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16470 return true;
16471 }
16472 return false;
16473 }
16474
16475 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16476 or none_type otherwise. */
16477
16478 static enum tag_types
16479 cp_parser_token_is_class_key (cp_token* token)
16480 {
16481 switch (token->keyword)
16482 {
16483 case RID_CLASS:
16484 return class_type;
16485 case RID_STRUCT:
16486 return record_type;
16487 case RID_UNION:
16488 return union_type;
16489
16490 default:
16491 return none_type;
16492 }
16493 }
16494
16495 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16496
16497 static void
16498 cp_parser_check_class_key (enum tag_types class_key, tree type)
16499 {
16500 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16501 pedwarn ("%qs tag used in naming %q#T",
16502 class_key == union_type ? "union"
16503 : class_key == record_type ? "struct" : "class",
16504 type);
16505 }
16506
16507 /* Issue an error message if DECL is redeclared with different
16508 access than its original declaration [class.access.spec/3].
16509 This applies to nested classes and nested class templates.
16510 [class.mem/1]. */
16511
16512 static void
16513 cp_parser_check_access_in_redeclaration (tree decl)
16514 {
16515 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16516 return;
16517
16518 if ((TREE_PRIVATE (decl)
16519 != (current_access_specifier == access_private_node))
16520 || (TREE_PROTECTED (decl)
16521 != (current_access_specifier == access_protected_node)))
16522 error ("%qD redeclared with different access", decl);
16523 }
16524
16525 /* Look for the `template' keyword, as a syntactic disambiguator.
16526 Return TRUE iff it is present, in which case it will be
16527 consumed. */
16528
16529 static bool
16530 cp_parser_optional_template_keyword (cp_parser *parser)
16531 {
16532 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16533 {
16534 /* The `template' keyword can only be used within templates;
16535 outside templates the parser can always figure out what is a
16536 template and what is not. */
16537 if (!processing_template_decl)
16538 {
16539 error ("%<template%> (as a disambiguator) is only allowed "
16540 "within templates");
16541 /* If this part of the token stream is rescanned, the same
16542 error message would be generated. So, we purge the token
16543 from the stream. */
16544 cp_lexer_purge_token (parser->lexer);
16545 return false;
16546 }
16547 else
16548 {
16549 /* Consume the `template' keyword. */
16550 cp_lexer_consume_token (parser->lexer);
16551 return true;
16552 }
16553 }
16554
16555 return false;
16556 }
16557
16558 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16559 set PARSER->SCOPE, and perform other related actions. */
16560
16561 static void
16562 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16563 {
16564 tree value;
16565 tree check;
16566
16567 /* Get the stored value. */
16568 value = cp_lexer_consume_token (parser->lexer)->value;
16569 /* Perform any access checks that were deferred. */
16570 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16571 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16572 /* Set the scope from the stored value. */
16573 parser->scope = TREE_VALUE (value);
16574 parser->qualifying_scope = TREE_TYPE (value);
16575 parser->object_scope = NULL_TREE;
16576 }
16577
16578 /* Consume tokens up through a non-nested END token. */
16579
16580 static void
16581 cp_parser_cache_group (cp_parser *parser,
16582 enum cpp_ttype end,
16583 unsigned depth)
16584 {
16585 while (true)
16586 {
16587 cp_token *token;
16588
16589 /* Abort a parenthesized expression if we encounter a brace. */
16590 if ((end == CPP_CLOSE_PAREN || depth == 0)
16591 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16592 return;
16593 /* If we've reached the end of the file, stop. */
16594 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16595 || (end != CPP_PRAGMA_EOL
16596 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16597 return;
16598 /* Consume the next token. */
16599 token = cp_lexer_consume_token (parser->lexer);
16600 /* See if it starts a new group. */
16601 if (token->type == CPP_OPEN_BRACE)
16602 {
16603 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16604 if (depth == 0)
16605 return;
16606 }
16607 else if (token->type == CPP_OPEN_PAREN)
16608 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16609 else if (token->type == CPP_PRAGMA)
16610 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16611 else if (token->type == end)
16612 return;
16613 }
16614 }
16615
16616 /* Begin parsing tentatively. We always save tokens while parsing
16617 tentatively so that if the tentative parsing fails we can restore the
16618 tokens. */
16619
16620 static void
16621 cp_parser_parse_tentatively (cp_parser* parser)
16622 {
16623 /* Enter a new parsing context. */
16624 parser->context = cp_parser_context_new (parser->context);
16625 /* Begin saving tokens. */
16626 cp_lexer_save_tokens (parser->lexer);
16627 /* In order to avoid repetitive access control error messages,
16628 access checks are queued up until we are no longer parsing
16629 tentatively. */
16630 push_deferring_access_checks (dk_deferred);
16631 }
16632
16633 /* Commit to the currently active tentative parse. */
16634
16635 static void
16636 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16637 {
16638 cp_parser_context *context;
16639 cp_lexer *lexer;
16640
16641 /* Mark all of the levels as committed. */
16642 lexer = parser->lexer;
16643 for (context = parser->context; context->next; context = context->next)
16644 {
16645 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16646 break;
16647 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16648 while (!cp_lexer_saving_tokens (lexer))
16649 lexer = lexer->next;
16650 cp_lexer_commit_tokens (lexer);
16651 }
16652 }
16653
16654 /* Abort the currently active tentative parse. All consumed tokens
16655 will be rolled back, and no diagnostics will be issued. */
16656
16657 static void
16658 cp_parser_abort_tentative_parse (cp_parser* parser)
16659 {
16660 cp_parser_simulate_error (parser);
16661 /* Now, pretend that we want to see if the construct was
16662 successfully parsed. */
16663 cp_parser_parse_definitely (parser);
16664 }
16665
16666 /* Stop parsing tentatively. If a parse error has occurred, restore the
16667 token stream. Otherwise, commit to the tokens we have consumed.
16668 Returns true if no error occurred; false otherwise. */
16669
16670 static bool
16671 cp_parser_parse_definitely (cp_parser* parser)
16672 {
16673 bool error_occurred;
16674 cp_parser_context *context;
16675
16676 /* Remember whether or not an error occurred, since we are about to
16677 destroy that information. */
16678 error_occurred = cp_parser_error_occurred (parser);
16679 /* Remove the topmost context from the stack. */
16680 context = parser->context;
16681 parser->context = context->next;
16682 /* If no parse errors occurred, commit to the tentative parse. */
16683 if (!error_occurred)
16684 {
16685 /* Commit to the tokens read tentatively, unless that was
16686 already done. */
16687 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16688 cp_lexer_commit_tokens (parser->lexer);
16689
16690 pop_to_parent_deferring_access_checks ();
16691 }
16692 /* Otherwise, if errors occurred, roll back our state so that things
16693 are just as they were before we began the tentative parse. */
16694 else
16695 {
16696 cp_lexer_rollback_tokens (parser->lexer);
16697 pop_deferring_access_checks ();
16698 }
16699 /* Add the context to the front of the free list. */
16700 context->next = cp_parser_context_free_list;
16701 cp_parser_context_free_list = context;
16702
16703 return !error_occurred;
16704 }
16705
16706 /* Returns true if we are parsing tentatively and are not committed to
16707 this tentative parse. */
16708
16709 static bool
16710 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16711 {
16712 return (cp_parser_parsing_tentatively (parser)
16713 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16714 }
16715
16716 /* Returns nonzero iff an error has occurred during the most recent
16717 tentative parse. */
16718
16719 static bool
16720 cp_parser_error_occurred (cp_parser* parser)
16721 {
16722 return (cp_parser_parsing_tentatively (parser)
16723 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16724 }
16725
16726 /* Returns nonzero if GNU extensions are allowed. */
16727
16728 static bool
16729 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16730 {
16731 return parser->allow_gnu_extensions_p;
16732 }
16733 \f
16734 /* Objective-C++ Productions */
16735
16736
16737 /* Parse an Objective-C expression, which feeds into a primary-expression
16738 above.
16739
16740 objc-expression:
16741 objc-message-expression
16742 objc-string-literal
16743 objc-encode-expression
16744 objc-protocol-expression
16745 objc-selector-expression
16746
16747 Returns a tree representation of the expression. */
16748
16749 static tree
16750 cp_parser_objc_expression (cp_parser* parser)
16751 {
16752 /* Try to figure out what kind of declaration is present. */
16753 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16754
16755 switch (kwd->type)
16756 {
16757 case CPP_OPEN_SQUARE:
16758 return cp_parser_objc_message_expression (parser);
16759
16760 case CPP_OBJC_STRING:
16761 kwd = cp_lexer_consume_token (parser->lexer);
16762 return objc_build_string_object (kwd->value);
16763
16764 case CPP_KEYWORD:
16765 switch (kwd->keyword)
16766 {
16767 case RID_AT_ENCODE:
16768 return cp_parser_objc_encode_expression (parser);
16769
16770 case RID_AT_PROTOCOL:
16771 return cp_parser_objc_protocol_expression (parser);
16772
16773 case RID_AT_SELECTOR:
16774 return cp_parser_objc_selector_expression (parser);
16775
16776 default:
16777 break;
16778 }
16779 default:
16780 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16781 cp_parser_skip_to_end_of_block_or_statement (parser);
16782 }
16783
16784 return error_mark_node;
16785 }
16786
16787 /* Parse an Objective-C message expression.
16788
16789 objc-message-expression:
16790 [ objc-message-receiver objc-message-args ]
16791
16792 Returns a representation of an Objective-C message. */
16793
16794 static tree
16795 cp_parser_objc_message_expression (cp_parser* parser)
16796 {
16797 tree receiver, messageargs;
16798
16799 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16800 receiver = cp_parser_objc_message_receiver (parser);
16801 messageargs = cp_parser_objc_message_args (parser);
16802 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16803
16804 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16805 }
16806
16807 /* Parse an objc-message-receiver.
16808
16809 objc-message-receiver:
16810 expression
16811 simple-type-specifier
16812
16813 Returns a representation of the type or expression. */
16814
16815 static tree
16816 cp_parser_objc_message_receiver (cp_parser* parser)
16817 {
16818 tree rcv;
16819
16820 /* An Objective-C message receiver may be either (1) a type
16821 or (2) an expression. */
16822 cp_parser_parse_tentatively (parser);
16823 rcv = cp_parser_expression (parser, false);
16824
16825 if (cp_parser_parse_definitely (parser))
16826 return rcv;
16827
16828 rcv = cp_parser_simple_type_specifier (parser,
16829 /*decl_specs=*/NULL,
16830 CP_PARSER_FLAGS_NONE);
16831
16832 return objc_get_class_reference (rcv);
16833 }
16834
16835 /* Parse the arguments and selectors comprising an Objective-C message.
16836
16837 objc-message-args:
16838 objc-selector
16839 objc-selector-args
16840 objc-selector-args , objc-comma-args
16841
16842 objc-selector-args:
16843 objc-selector [opt] : assignment-expression
16844 objc-selector-args objc-selector [opt] : assignment-expression
16845
16846 objc-comma-args:
16847 assignment-expression
16848 objc-comma-args , assignment-expression
16849
16850 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16851 selector arguments and TREE_VALUE containing a list of comma
16852 arguments. */
16853
16854 static tree
16855 cp_parser_objc_message_args (cp_parser* parser)
16856 {
16857 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16858 bool maybe_unary_selector_p = true;
16859 cp_token *token = cp_lexer_peek_token (parser->lexer);
16860
16861 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16862 {
16863 tree selector = NULL_TREE, arg;
16864
16865 if (token->type != CPP_COLON)
16866 selector = cp_parser_objc_selector (parser);
16867
16868 /* Detect if we have a unary selector. */
16869 if (maybe_unary_selector_p
16870 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16871 return build_tree_list (selector, NULL_TREE);
16872
16873 maybe_unary_selector_p = false;
16874 cp_parser_require (parser, CPP_COLON, "`:'");
16875 arg = cp_parser_assignment_expression (parser, false);
16876
16877 sel_args
16878 = chainon (sel_args,
16879 build_tree_list (selector, arg));
16880
16881 token = cp_lexer_peek_token (parser->lexer);
16882 }
16883
16884 /* Handle non-selector arguments, if any. */
16885 while (token->type == CPP_COMMA)
16886 {
16887 tree arg;
16888
16889 cp_lexer_consume_token (parser->lexer);
16890 arg = cp_parser_assignment_expression (parser, false);
16891
16892 addl_args
16893 = chainon (addl_args,
16894 build_tree_list (NULL_TREE, arg));
16895
16896 token = cp_lexer_peek_token (parser->lexer);
16897 }
16898
16899 return build_tree_list (sel_args, addl_args);
16900 }
16901
16902 /* Parse an Objective-C encode expression.
16903
16904 objc-encode-expression:
16905 @encode objc-typename
16906
16907 Returns an encoded representation of the type argument. */
16908
16909 static tree
16910 cp_parser_objc_encode_expression (cp_parser* parser)
16911 {
16912 tree type;
16913
16914 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16915 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16916 type = complete_type (cp_parser_type_id (parser));
16917 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16918
16919 if (!type)
16920 {
16921 error ("%<@encode%> must specify a type as an argument");
16922 return error_mark_node;
16923 }
16924
16925 return objc_build_encode_expr (type);
16926 }
16927
16928 /* Parse an Objective-C @defs expression. */
16929
16930 static tree
16931 cp_parser_objc_defs_expression (cp_parser *parser)
16932 {
16933 tree name;
16934
16935 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16936 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16937 name = cp_parser_identifier (parser);
16938 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16939
16940 return objc_get_class_ivars (name);
16941 }
16942
16943 /* Parse an Objective-C protocol expression.
16944
16945 objc-protocol-expression:
16946 @protocol ( identifier )
16947
16948 Returns a representation of the protocol expression. */
16949
16950 static tree
16951 cp_parser_objc_protocol_expression (cp_parser* parser)
16952 {
16953 tree proto;
16954
16955 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16956 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16957 proto = cp_parser_identifier (parser);
16958 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16959
16960 return objc_build_protocol_expr (proto);
16961 }
16962
16963 /* Parse an Objective-C selector expression.
16964
16965 objc-selector-expression:
16966 @selector ( objc-method-signature )
16967
16968 objc-method-signature:
16969 objc-selector
16970 objc-selector-seq
16971
16972 objc-selector-seq:
16973 objc-selector :
16974 objc-selector-seq objc-selector :
16975
16976 Returns a representation of the method selector. */
16977
16978 static tree
16979 cp_parser_objc_selector_expression (cp_parser* parser)
16980 {
16981 tree sel_seq = NULL_TREE;
16982 bool maybe_unary_selector_p = true;
16983 cp_token *token;
16984
16985 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16986 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16987 token = cp_lexer_peek_token (parser->lexer);
16988
16989 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16990 || token->type == CPP_SCOPE)
16991 {
16992 tree selector = NULL_TREE;
16993
16994 if (token->type != CPP_COLON
16995 || token->type == CPP_SCOPE)
16996 selector = cp_parser_objc_selector (parser);
16997
16998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16999 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17000 {
17001 /* Detect if we have a unary selector. */
17002 if (maybe_unary_selector_p)
17003 {
17004 sel_seq = selector;
17005 goto finish_selector;
17006 }
17007 else
17008 {
17009 cp_parser_error (parser, "expected %<:%>");
17010 }
17011 }
17012 maybe_unary_selector_p = false;
17013 token = cp_lexer_consume_token (parser->lexer);
17014
17015 if (token->type == CPP_SCOPE)
17016 {
17017 sel_seq
17018 = chainon (sel_seq,
17019 build_tree_list (selector, NULL_TREE));
17020 sel_seq
17021 = chainon (sel_seq,
17022 build_tree_list (NULL_TREE, NULL_TREE));
17023 }
17024 else
17025 sel_seq
17026 = chainon (sel_seq,
17027 build_tree_list (selector, NULL_TREE));
17028
17029 token = cp_lexer_peek_token (parser->lexer);
17030 }
17031
17032 finish_selector:
17033 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17034
17035 return objc_build_selector_expr (sel_seq);
17036 }
17037
17038 /* Parse a list of identifiers.
17039
17040 objc-identifier-list:
17041 identifier
17042 objc-identifier-list , identifier
17043
17044 Returns a TREE_LIST of identifier nodes. */
17045
17046 static tree
17047 cp_parser_objc_identifier_list (cp_parser* parser)
17048 {
17049 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17050 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17051
17052 while (sep->type == CPP_COMMA)
17053 {
17054 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17055 list = chainon (list,
17056 build_tree_list (NULL_TREE,
17057 cp_parser_identifier (parser)));
17058 sep = cp_lexer_peek_token (parser->lexer);
17059 }
17060
17061 return list;
17062 }
17063
17064 /* Parse an Objective-C alias declaration.
17065
17066 objc-alias-declaration:
17067 @compatibility_alias identifier identifier ;
17068
17069 This function registers the alias mapping with the Objective-C front-end.
17070 It returns nothing. */
17071
17072 static void
17073 cp_parser_objc_alias_declaration (cp_parser* parser)
17074 {
17075 tree alias, orig;
17076
17077 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17078 alias = cp_parser_identifier (parser);
17079 orig = cp_parser_identifier (parser);
17080 objc_declare_alias (alias, orig);
17081 cp_parser_consume_semicolon_at_end_of_statement (parser);
17082 }
17083
17084 /* Parse an Objective-C class forward-declaration.
17085
17086 objc-class-declaration:
17087 @class objc-identifier-list ;
17088
17089 The function registers the forward declarations with the Objective-C
17090 front-end. It returns nothing. */
17091
17092 static void
17093 cp_parser_objc_class_declaration (cp_parser* parser)
17094 {
17095 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17096 objc_declare_class (cp_parser_objc_identifier_list (parser));
17097 cp_parser_consume_semicolon_at_end_of_statement (parser);
17098 }
17099
17100 /* Parse a list of Objective-C protocol references.
17101
17102 objc-protocol-refs-opt:
17103 objc-protocol-refs [opt]
17104
17105 objc-protocol-refs:
17106 < objc-identifier-list >
17107
17108 Returns a TREE_LIST of identifiers, if any. */
17109
17110 static tree
17111 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17112 {
17113 tree protorefs = NULL_TREE;
17114
17115 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17116 {
17117 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17118 protorefs = cp_parser_objc_identifier_list (parser);
17119 cp_parser_require (parser, CPP_GREATER, "`>'");
17120 }
17121
17122 return protorefs;
17123 }
17124
17125 /* Parse a Objective-C visibility specification. */
17126
17127 static void
17128 cp_parser_objc_visibility_spec (cp_parser* parser)
17129 {
17130 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17131
17132 switch (vis->keyword)
17133 {
17134 case RID_AT_PRIVATE:
17135 objc_set_visibility (2);
17136 break;
17137 case RID_AT_PROTECTED:
17138 objc_set_visibility (0);
17139 break;
17140 case RID_AT_PUBLIC:
17141 objc_set_visibility (1);
17142 break;
17143 default:
17144 return;
17145 }
17146
17147 /* Eat '@private'/'@protected'/'@public'. */
17148 cp_lexer_consume_token (parser->lexer);
17149 }
17150
17151 /* Parse an Objective-C method type. */
17152
17153 static void
17154 cp_parser_objc_method_type (cp_parser* parser)
17155 {
17156 objc_set_method_type
17157 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17158 ? PLUS_EXPR
17159 : MINUS_EXPR);
17160 }
17161
17162 /* Parse an Objective-C protocol qualifier. */
17163
17164 static tree
17165 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17166 {
17167 tree quals = NULL_TREE, node;
17168 cp_token *token = cp_lexer_peek_token (parser->lexer);
17169
17170 node = token->value;
17171
17172 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17173 && (node == ridpointers [(int) RID_IN]
17174 || node == ridpointers [(int) RID_OUT]
17175 || node == ridpointers [(int) RID_INOUT]
17176 || node == ridpointers [(int) RID_BYCOPY]
17177 || node == ridpointers [(int) RID_BYREF]
17178 || node == ridpointers [(int) RID_ONEWAY]))
17179 {
17180 quals = tree_cons (NULL_TREE, node, quals);
17181 cp_lexer_consume_token (parser->lexer);
17182 token = cp_lexer_peek_token (parser->lexer);
17183 node = token->value;
17184 }
17185
17186 return quals;
17187 }
17188
17189 /* Parse an Objective-C typename. */
17190
17191 static tree
17192 cp_parser_objc_typename (cp_parser* parser)
17193 {
17194 tree typename = NULL_TREE;
17195
17196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17197 {
17198 tree proto_quals, cp_type = NULL_TREE;
17199
17200 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17201 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17202
17203 /* An ObjC type name may consist of just protocol qualifiers, in which
17204 case the type shall default to 'id'. */
17205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17206 cp_type = cp_parser_type_id (parser);
17207
17208 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17209 typename = build_tree_list (proto_quals, cp_type);
17210 }
17211
17212 return typename;
17213 }
17214
17215 /* Check to see if TYPE refers to an Objective-C selector name. */
17216
17217 static bool
17218 cp_parser_objc_selector_p (enum cpp_ttype type)
17219 {
17220 return (type == CPP_NAME || type == CPP_KEYWORD
17221 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17222 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17223 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17224 || type == CPP_XOR || type == CPP_XOR_EQ);
17225 }
17226
17227 /* Parse an Objective-C selector. */
17228
17229 static tree
17230 cp_parser_objc_selector (cp_parser* parser)
17231 {
17232 cp_token *token = cp_lexer_consume_token (parser->lexer);
17233
17234 if (!cp_parser_objc_selector_p (token->type))
17235 {
17236 error ("invalid Objective-C++ selector name");
17237 return error_mark_node;
17238 }
17239
17240 /* C++ operator names are allowed to appear in ObjC selectors. */
17241 switch (token->type)
17242 {
17243 case CPP_AND_AND: return get_identifier ("and");
17244 case CPP_AND_EQ: return get_identifier ("and_eq");
17245 case CPP_AND: return get_identifier ("bitand");
17246 case CPP_OR: return get_identifier ("bitor");
17247 case CPP_COMPL: return get_identifier ("compl");
17248 case CPP_NOT: return get_identifier ("not");
17249 case CPP_NOT_EQ: return get_identifier ("not_eq");
17250 case CPP_OR_OR: return get_identifier ("or");
17251 case CPP_OR_EQ: return get_identifier ("or_eq");
17252 case CPP_XOR: return get_identifier ("xor");
17253 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17254 default: return token->value;
17255 }
17256 }
17257
17258 /* Parse an Objective-C params list. */
17259
17260 static tree
17261 cp_parser_objc_method_keyword_params (cp_parser* parser)
17262 {
17263 tree params = NULL_TREE;
17264 bool maybe_unary_selector_p = true;
17265 cp_token *token = cp_lexer_peek_token (parser->lexer);
17266
17267 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17268 {
17269 tree selector = NULL_TREE, typename, identifier;
17270
17271 if (token->type != CPP_COLON)
17272 selector = cp_parser_objc_selector (parser);
17273
17274 /* Detect if we have a unary selector. */
17275 if (maybe_unary_selector_p
17276 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17277 return selector;
17278
17279 maybe_unary_selector_p = false;
17280 cp_parser_require (parser, CPP_COLON, "`:'");
17281 typename = cp_parser_objc_typename (parser);
17282 identifier = cp_parser_identifier (parser);
17283
17284 params
17285 = chainon (params,
17286 objc_build_keyword_decl (selector,
17287 typename,
17288 identifier));
17289
17290 token = cp_lexer_peek_token (parser->lexer);
17291 }
17292
17293 return params;
17294 }
17295
17296 /* Parse the non-keyword Objective-C params. */
17297
17298 static tree
17299 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17300 {
17301 tree params = make_node (TREE_LIST);
17302 cp_token *token = cp_lexer_peek_token (parser->lexer);
17303 *ellipsisp = false; /* Initially, assume no ellipsis. */
17304
17305 while (token->type == CPP_COMMA)
17306 {
17307 cp_parameter_declarator *parmdecl;
17308 tree parm;
17309
17310 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17311 token = cp_lexer_peek_token (parser->lexer);
17312
17313 if (token->type == CPP_ELLIPSIS)
17314 {
17315 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17316 *ellipsisp = true;
17317 break;
17318 }
17319
17320 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17321 parm = grokdeclarator (parmdecl->declarator,
17322 &parmdecl->decl_specifiers,
17323 PARM, /*initialized=*/0,
17324 /*attrlist=*/NULL);
17325
17326 chainon (params, build_tree_list (NULL_TREE, parm));
17327 token = cp_lexer_peek_token (parser->lexer);
17328 }
17329
17330 return params;
17331 }
17332
17333 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17334
17335 static void
17336 cp_parser_objc_interstitial_code (cp_parser* parser)
17337 {
17338 cp_token *token = cp_lexer_peek_token (parser->lexer);
17339
17340 /* If the next token is `extern' and the following token is a string
17341 literal, then we have a linkage specification. */
17342 if (token->keyword == RID_EXTERN
17343 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17344 cp_parser_linkage_specification (parser);
17345 /* Handle #pragma, if any. */
17346 else if (token->type == CPP_PRAGMA)
17347 cp_parser_pragma (parser, pragma_external);
17348 /* Allow stray semicolons. */
17349 else if (token->type == CPP_SEMICOLON)
17350 cp_lexer_consume_token (parser->lexer);
17351 /* Finally, try to parse a block-declaration, or a function-definition. */
17352 else
17353 cp_parser_block_declaration (parser, /*statement_p=*/false);
17354 }
17355
17356 /* Parse a method signature. */
17357
17358 static tree
17359 cp_parser_objc_method_signature (cp_parser* parser)
17360 {
17361 tree rettype, kwdparms, optparms;
17362 bool ellipsis = false;
17363
17364 cp_parser_objc_method_type (parser);
17365 rettype = cp_parser_objc_typename (parser);
17366 kwdparms = cp_parser_objc_method_keyword_params (parser);
17367 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17368
17369 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17370 }
17371
17372 /* Pars an Objective-C method prototype list. */
17373
17374 static void
17375 cp_parser_objc_method_prototype_list (cp_parser* parser)
17376 {
17377 cp_token *token = cp_lexer_peek_token (parser->lexer);
17378
17379 while (token->keyword != RID_AT_END)
17380 {
17381 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17382 {
17383 objc_add_method_declaration
17384 (cp_parser_objc_method_signature (parser));
17385 cp_parser_consume_semicolon_at_end_of_statement (parser);
17386 }
17387 else
17388 /* Allow for interspersed non-ObjC++ code. */
17389 cp_parser_objc_interstitial_code (parser);
17390
17391 token = cp_lexer_peek_token (parser->lexer);
17392 }
17393
17394 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17395 objc_finish_interface ();
17396 }
17397
17398 /* Parse an Objective-C method definition list. */
17399
17400 static void
17401 cp_parser_objc_method_definition_list (cp_parser* parser)
17402 {
17403 cp_token *token = cp_lexer_peek_token (parser->lexer);
17404
17405 while (token->keyword != RID_AT_END)
17406 {
17407 tree meth;
17408
17409 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17410 {
17411 push_deferring_access_checks (dk_deferred);
17412 objc_start_method_definition
17413 (cp_parser_objc_method_signature (parser));
17414
17415 /* For historical reasons, we accept an optional semicolon. */
17416 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17417 cp_lexer_consume_token (parser->lexer);
17418
17419 perform_deferred_access_checks ();
17420 stop_deferring_access_checks ();
17421 meth = cp_parser_function_definition_after_declarator (parser,
17422 false);
17423 pop_deferring_access_checks ();
17424 objc_finish_method_definition (meth);
17425 }
17426 else
17427 /* Allow for interspersed non-ObjC++ code. */
17428 cp_parser_objc_interstitial_code (parser);
17429
17430 token = cp_lexer_peek_token (parser->lexer);
17431 }
17432
17433 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17434 objc_finish_implementation ();
17435 }
17436
17437 /* Parse Objective-C ivars. */
17438
17439 static void
17440 cp_parser_objc_class_ivars (cp_parser* parser)
17441 {
17442 cp_token *token = cp_lexer_peek_token (parser->lexer);
17443
17444 if (token->type != CPP_OPEN_BRACE)
17445 return; /* No ivars specified. */
17446
17447 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17448 token = cp_lexer_peek_token (parser->lexer);
17449
17450 while (token->type != CPP_CLOSE_BRACE)
17451 {
17452 cp_decl_specifier_seq declspecs;
17453 int decl_class_or_enum_p;
17454 tree prefix_attributes;
17455
17456 cp_parser_objc_visibility_spec (parser);
17457
17458 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17459 break;
17460
17461 cp_parser_decl_specifier_seq (parser,
17462 CP_PARSER_FLAGS_OPTIONAL,
17463 &declspecs,
17464 &decl_class_or_enum_p);
17465 prefix_attributes = declspecs.attributes;
17466 declspecs.attributes = NULL_TREE;
17467
17468 /* Keep going until we hit the `;' at the end of the
17469 declaration. */
17470 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17471 {
17472 tree width = NULL_TREE, attributes, first_attribute, decl;
17473 cp_declarator *declarator = NULL;
17474 int ctor_dtor_or_conv_p;
17475
17476 /* Check for a (possibly unnamed) bitfield declaration. */
17477 token = cp_lexer_peek_token (parser->lexer);
17478 if (token->type == CPP_COLON)
17479 goto eat_colon;
17480
17481 if (token->type == CPP_NAME
17482 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17483 == CPP_COLON))
17484 {
17485 /* Get the name of the bitfield. */
17486 declarator = make_id_declarator (NULL_TREE,
17487 cp_parser_identifier (parser),
17488 sfk_none);
17489
17490 eat_colon:
17491 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17492 /* Get the width of the bitfield. */
17493 width
17494 = cp_parser_constant_expression (parser,
17495 /*allow_non_constant=*/false,
17496 NULL);
17497 }
17498 else
17499 {
17500 /* Parse the declarator. */
17501 declarator
17502 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17503 &ctor_dtor_or_conv_p,
17504 /*parenthesized_p=*/NULL,
17505 /*member_p=*/false);
17506 }
17507
17508 /* Look for attributes that apply to the ivar. */
17509 attributes = cp_parser_attributes_opt (parser);
17510 /* Remember which attributes are prefix attributes and
17511 which are not. */
17512 first_attribute = attributes;
17513 /* Combine the attributes. */
17514 attributes = chainon (prefix_attributes, attributes);
17515
17516 if (width)
17517 {
17518 /* Create the bitfield declaration. */
17519 decl = grokbitfield (declarator, &declspecs, width);
17520 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17521 }
17522 else
17523 decl = grokfield (declarator, &declspecs,
17524 NULL_TREE, /*init_const_expr_p=*/false,
17525 NULL_TREE, attributes);
17526
17527 /* Add the instance variable. */
17528 objc_add_instance_variable (decl);
17529
17530 /* Reset PREFIX_ATTRIBUTES. */
17531 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17532 attributes = TREE_CHAIN (attributes);
17533 if (attributes)
17534 TREE_CHAIN (attributes) = NULL_TREE;
17535
17536 token = cp_lexer_peek_token (parser->lexer);
17537
17538 if (token->type == CPP_COMMA)
17539 {
17540 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17541 continue;
17542 }
17543 break;
17544 }
17545
17546 cp_parser_consume_semicolon_at_end_of_statement (parser);
17547 token = cp_lexer_peek_token (parser->lexer);
17548 }
17549
17550 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17551 /* For historical reasons, we accept an optional semicolon. */
17552 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17553 cp_lexer_consume_token (parser->lexer);
17554 }
17555
17556 /* Parse an Objective-C protocol declaration. */
17557
17558 static void
17559 cp_parser_objc_protocol_declaration (cp_parser* parser)
17560 {
17561 tree proto, protorefs;
17562 cp_token *tok;
17563
17564 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17566 {
17567 error ("identifier expected after %<@protocol%>");
17568 goto finish;
17569 }
17570
17571 /* See if we have a forward declaration or a definition. */
17572 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17573
17574 /* Try a forward declaration first. */
17575 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17576 {
17577 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17578 finish:
17579 cp_parser_consume_semicolon_at_end_of_statement (parser);
17580 }
17581
17582 /* Ok, we got a full-fledged definition (or at least should). */
17583 else
17584 {
17585 proto = cp_parser_identifier (parser);
17586 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17587 objc_start_protocol (proto, protorefs);
17588 cp_parser_objc_method_prototype_list (parser);
17589 }
17590 }
17591
17592 /* Parse an Objective-C superclass or category. */
17593
17594 static void
17595 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17596 tree *categ)
17597 {
17598 cp_token *next = cp_lexer_peek_token (parser->lexer);
17599
17600 *super = *categ = NULL_TREE;
17601 if (next->type == CPP_COLON)
17602 {
17603 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17604 *super = cp_parser_identifier (parser);
17605 }
17606 else if (next->type == CPP_OPEN_PAREN)
17607 {
17608 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17609 *categ = cp_parser_identifier (parser);
17610 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17611 }
17612 }
17613
17614 /* Parse an Objective-C class interface. */
17615
17616 static void
17617 cp_parser_objc_class_interface (cp_parser* parser)
17618 {
17619 tree name, super, categ, protos;
17620
17621 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17622 name = cp_parser_identifier (parser);
17623 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17624 protos = cp_parser_objc_protocol_refs_opt (parser);
17625
17626 /* We have either a class or a category on our hands. */
17627 if (categ)
17628 objc_start_category_interface (name, categ, protos);
17629 else
17630 {
17631 objc_start_class_interface (name, super, protos);
17632 /* Handle instance variable declarations, if any. */
17633 cp_parser_objc_class_ivars (parser);
17634 objc_continue_interface ();
17635 }
17636
17637 cp_parser_objc_method_prototype_list (parser);
17638 }
17639
17640 /* Parse an Objective-C class implementation. */
17641
17642 static void
17643 cp_parser_objc_class_implementation (cp_parser* parser)
17644 {
17645 tree name, super, categ;
17646
17647 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17648 name = cp_parser_identifier (parser);
17649 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17650
17651 /* We have either a class or a category on our hands. */
17652 if (categ)
17653 objc_start_category_implementation (name, categ);
17654 else
17655 {
17656 objc_start_class_implementation (name, super);
17657 /* Handle instance variable declarations, if any. */
17658 cp_parser_objc_class_ivars (parser);
17659 objc_continue_implementation ();
17660 }
17661
17662 cp_parser_objc_method_definition_list (parser);
17663 }
17664
17665 /* Consume the @end token and finish off the implementation. */
17666
17667 static void
17668 cp_parser_objc_end_implementation (cp_parser* parser)
17669 {
17670 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17671 objc_finish_implementation ();
17672 }
17673
17674 /* Parse an Objective-C declaration. */
17675
17676 static void
17677 cp_parser_objc_declaration (cp_parser* parser)
17678 {
17679 /* Try to figure out what kind of declaration is present. */
17680 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17681
17682 switch (kwd->keyword)
17683 {
17684 case RID_AT_ALIAS:
17685 cp_parser_objc_alias_declaration (parser);
17686 break;
17687 case RID_AT_CLASS:
17688 cp_parser_objc_class_declaration (parser);
17689 break;
17690 case RID_AT_PROTOCOL:
17691 cp_parser_objc_protocol_declaration (parser);
17692 break;
17693 case RID_AT_INTERFACE:
17694 cp_parser_objc_class_interface (parser);
17695 break;
17696 case RID_AT_IMPLEMENTATION:
17697 cp_parser_objc_class_implementation (parser);
17698 break;
17699 case RID_AT_END:
17700 cp_parser_objc_end_implementation (parser);
17701 break;
17702 default:
17703 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17704 cp_parser_skip_to_end_of_block_or_statement (parser);
17705 }
17706 }
17707
17708 /* Parse an Objective-C try-catch-finally statement.
17709
17710 objc-try-catch-finally-stmt:
17711 @try compound-statement objc-catch-clause-seq [opt]
17712 objc-finally-clause [opt]
17713
17714 objc-catch-clause-seq:
17715 objc-catch-clause objc-catch-clause-seq [opt]
17716
17717 objc-catch-clause:
17718 @catch ( exception-declaration ) compound-statement
17719
17720 objc-finally-clause
17721 @finally compound-statement
17722
17723 Returns NULL_TREE. */
17724
17725 static tree
17726 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17727 location_t location;
17728 tree stmt;
17729
17730 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17731 location = cp_lexer_peek_token (parser->lexer)->location;
17732 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17733 node, lest it get absorbed into the surrounding block. */
17734 stmt = push_stmt_list ();
17735 cp_parser_compound_statement (parser, NULL, false);
17736 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17737
17738 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17739 {
17740 cp_parameter_declarator *parmdecl;
17741 tree parm;
17742
17743 cp_lexer_consume_token (parser->lexer);
17744 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17745 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17746 parm = grokdeclarator (parmdecl->declarator,
17747 &parmdecl->decl_specifiers,
17748 PARM, /*initialized=*/0,
17749 /*attrlist=*/NULL);
17750 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17751 objc_begin_catch_clause (parm);
17752 cp_parser_compound_statement (parser, NULL, false);
17753 objc_finish_catch_clause ();
17754 }
17755
17756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17757 {
17758 cp_lexer_consume_token (parser->lexer);
17759 location = cp_lexer_peek_token (parser->lexer)->location;
17760 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17761 node, lest it get absorbed into the surrounding block. */
17762 stmt = push_stmt_list ();
17763 cp_parser_compound_statement (parser, NULL, false);
17764 objc_build_finally_clause (location, pop_stmt_list (stmt));
17765 }
17766
17767 return objc_finish_try_stmt ();
17768 }
17769
17770 /* Parse an Objective-C synchronized statement.
17771
17772 objc-synchronized-stmt:
17773 @synchronized ( expression ) compound-statement
17774
17775 Returns NULL_TREE. */
17776
17777 static tree
17778 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17779 location_t location;
17780 tree lock, stmt;
17781
17782 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17783
17784 location = cp_lexer_peek_token (parser->lexer)->location;
17785 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17786 lock = cp_parser_expression (parser, false);
17787 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17788
17789 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17790 node, lest it get absorbed into the surrounding block. */
17791 stmt = push_stmt_list ();
17792 cp_parser_compound_statement (parser, NULL, false);
17793
17794 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17795 }
17796
17797 /* Parse an Objective-C throw statement.
17798
17799 objc-throw-stmt:
17800 @throw assignment-expression [opt] ;
17801
17802 Returns a constructed '@throw' statement. */
17803
17804 static tree
17805 cp_parser_objc_throw_statement (cp_parser *parser) {
17806 tree expr = NULL_TREE;
17807
17808 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17809
17810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17811 expr = cp_parser_assignment_expression (parser, false);
17812
17813 cp_parser_consume_semicolon_at_end_of_statement (parser);
17814
17815 return objc_build_throw_stmt (expr);
17816 }
17817
17818 /* Parse an Objective-C statement. */
17819
17820 static tree
17821 cp_parser_objc_statement (cp_parser * parser) {
17822 /* Try to figure out what kind of declaration is present. */
17823 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17824
17825 switch (kwd->keyword)
17826 {
17827 case RID_AT_TRY:
17828 return cp_parser_objc_try_catch_finally_statement (parser);
17829 case RID_AT_SYNCHRONIZED:
17830 return cp_parser_objc_synchronized_statement (parser);
17831 case RID_AT_THROW:
17832 return cp_parser_objc_throw_statement (parser);
17833 default:
17834 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17835 cp_parser_skip_to_end_of_block_or_statement (parser);
17836 }
17837
17838 return error_mark_node;
17839 }
17840 \f
17841 /* OpenMP 2.5 parsing routines. */
17842
17843 /* All OpenMP clauses. OpenMP 2.5. */
17844 typedef enum pragma_omp_clause {
17845 PRAGMA_OMP_CLAUSE_NONE = 0,
17846
17847 PRAGMA_OMP_CLAUSE_COPYIN,
17848 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17849 PRAGMA_OMP_CLAUSE_DEFAULT,
17850 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17851 PRAGMA_OMP_CLAUSE_IF,
17852 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17853 PRAGMA_OMP_CLAUSE_NOWAIT,
17854 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17855 PRAGMA_OMP_CLAUSE_ORDERED,
17856 PRAGMA_OMP_CLAUSE_PRIVATE,
17857 PRAGMA_OMP_CLAUSE_REDUCTION,
17858 PRAGMA_OMP_CLAUSE_SCHEDULE,
17859 PRAGMA_OMP_CLAUSE_SHARED
17860 } pragma_omp_clause;
17861
17862 /* Returns name of the next clause.
17863 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17864 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17865 returned and the token is consumed. */
17866
17867 static pragma_omp_clause
17868 cp_parser_omp_clause_name (cp_parser *parser)
17869 {
17870 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17871
17872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17873 result = PRAGMA_OMP_CLAUSE_IF;
17874 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17875 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17876 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17877 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17878 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17879 {
17880 tree id = cp_lexer_peek_token (parser->lexer)->value;
17881 const char *p = IDENTIFIER_POINTER (id);
17882
17883 switch (p[0])
17884 {
17885 case 'c':
17886 if (!strcmp ("copyin", p))
17887 result = PRAGMA_OMP_CLAUSE_COPYIN;
17888 else if (!strcmp ("copyprivate", p))
17889 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17890 break;
17891 case 'f':
17892 if (!strcmp ("firstprivate", p))
17893 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17894 break;
17895 case 'l':
17896 if (!strcmp ("lastprivate", p))
17897 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17898 break;
17899 case 'n':
17900 if (!strcmp ("nowait", p))
17901 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17902 else if (!strcmp ("num_threads", p))
17903 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17904 break;
17905 case 'o':
17906 if (!strcmp ("ordered", p))
17907 result = PRAGMA_OMP_CLAUSE_ORDERED;
17908 break;
17909 case 'r':
17910 if (!strcmp ("reduction", p))
17911 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17912 break;
17913 case 's':
17914 if (!strcmp ("schedule", p))
17915 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17916 else if (!strcmp ("shared", p))
17917 result = PRAGMA_OMP_CLAUSE_SHARED;
17918 break;
17919 }
17920 }
17921
17922 if (result != PRAGMA_OMP_CLAUSE_NONE)
17923 cp_lexer_consume_token (parser->lexer);
17924
17925 return result;
17926 }
17927
17928 /* Validate that a clause of the given type does not already exist. */
17929
17930 static void
17931 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17932 {
17933 tree c;
17934
17935 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17936 if (OMP_CLAUSE_CODE (c) == code)
17937 {
17938 error ("too many %qs clauses", name);
17939 break;
17940 }
17941 }
17942
17943 /* OpenMP 2.5:
17944 variable-list:
17945 identifier
17946 variable-list , identifier
17947
17948 In addition, we match a closing parenthesis. An opening parenthesis
17949 will have been consumed by the caller.
17950
17951 If KIND is nonzero, create the appropriate node and install the decl
17952 in OMP_CLAUSE_DECL and add the node to the head of the list.
17953
17954 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17955 return the list created. */
17956
17957 static tree
17958 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17959 tree list)
17960 {
17961 while (1)
17962 {
17963 tree name, decl;
17964
17965 name = cp_parser_id_expression (parser, /*template_p=*/false,
17966 /*check_dependency_p=*/true,
17967 /*template_p=*/NULL,
17968 /*declarator_p=*/false,
17969 /*optional_p=*/false);
17970 if (name == error_mark_node)
17971 goto skip_comma;
17972
17973 decl = cp_parser_lookup_name_simple (parser, name);
17974 if (decl == error_mark_node)
17975 cp_parser_name_lookup_error (parser, name, decl, NULL);
17976 else if (kind != 0)
17977 {
17978 tree u = build_omp_clause (kind);
17979 OMP_CLAUSE_DECL (u) = decl;
17980 OMP_CLAUSE_CHAIN (u) = list;
17981 list = u;
17982 }
17983 else
17984 list = tree_cons (decl, NULL_TREE, list);
17985
17986 get_comma:
17987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17988 break;
17989 cp_lexer_consume_token (parser->lexer);
17990 }
17991
17992 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17993 {
17994 int ending;
17995
17996 /* Try to resync to an unnested comma. Copied from
17997 cp_parser_parenthesized_expression_list. */
17998 skip_comma:
17999 ending = cp_parser_skip_to_closing_parenthesis (parser,
18000 /*recovering=*/true,
18001 /*or_comma=*/true,
18002 /*consume_paren=*/true);
18003 if (ending < 0)
18004 goto get_comma;
18005 }
18006
18007 return list;
18008 }
18009
18010 /* Similarly, but expect leading and trailing parenthesis. This is a very
18011 common case for omp clauses. */
18012
18013 static tree
18014 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18015 {
18016 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18017 return cp_parser_omp_var_list_no_open (parser, kind, list);
18018 return list;
18019 }
18020
18021 /* OpenMP 2.5:
18022 default ( shared | none ) */
18023
18024 static tree
18025 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18026 {
18027 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18028 tree c;
18029
18030 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18031 return list;
18032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18033 {
18034 tree id = cp_lexer_peek_token (parser->lexer)->value;
18035 const char *p = IDENTIFIER_POINTER (id);
18036
18037 switch (p[0])
18038 {
18039 case 'n':
18040 if (strcmp ("none", p) != 0)
18041 goto invalid_kind;
18042 kind = OMP_CLAUSE_DEFAULT_NONE;
18043 break;
18044
18045 case 's':
18046 if (strcmp ("shared", p) != 0)
18047 goto invalid_kind;
18048 kind = OMP_CLAUSE_DEFAULT_SHARED;
18049 break;
18050
18051 default:
18052 goto invalid_kind;
18053 }
18054
18055 cp_lexer_consume_token (parser->lexer);
18056 }
18057 else
18058 {
18059 invalid_kind:
18060 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18061 }
18062
18063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18064 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18065 /*or_comma=*/false,
18066 /*consume_paren=*/true);
18067
18068 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18069 return list;
18070
18071 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18072 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18073 OMP_CLAUSE_CHAIN (c) = list;
18074 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18075
18076 return c;
18077 }
18078
18079 /* OpenMP 2.5:
18080 if ( expression ) */
18081
18082 static tree
18083 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18084 {
18085 tree t, c;
18086
18087 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18088 return list;
18089
18090 t = cp_parser_condition (parser);
18091
18092 if (t == error_mark_node
18093 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18094 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18095 /*or_comma=*/false,
18096 /*consume_paren=*/true);
18097
18098 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18099
18100 c = build_omp_clause (OMP_CLAUSE_IF);
18101 OMP_CLAUSE_IF_EXPR (c) = t;
18102 OMP_CLAUSE_CHAIN (c) = list;
18103
18104 return c;
18105 }
18106
18107 /* OpenMP 2.5:
18108 nowait */
18109
18110 static tree
18111 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18112 {
18113 tree c;
18114
18115 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18116
18117 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18118 OMP_CLAUSE_CHAIN (c) = list;
18119 return c;
18120 }
18121
18122 /* OpenMP 2.5:
18123 num_threads ( expression ) */
18124
18125 static tree
18126 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18127 {
18128 tree t, c;
18129
18130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18131 return list;
18132
18133 t = cp_parser_expression (parser, false);
18134
18135 if (t == error_mark_node
18136 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18137 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18138 /*or_comma=*/false,
18139 /*consume_paren=*/true);
18140
18141 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18142
18143 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18144 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18145 OMP_CLAUSE_CHAIN (c) = list;
18146
18147 return c;
18148 }
18149
18150 /* OpenMP 2.5:
18151 ordered */
18152
18153 static tree
18154 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18155 {
18156 tree c;
18157
18158 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18159
18160 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18161 OMP_CLAUSE_CHAIN (c) = list;
18162 return c;
18163 }
18164
18165 /* OpenMP 2.5:
18166 reduction ( reduction-operator : variable-list )
18167
18168 reduction-operator:
18169 One of: + * - & ^ | && || */
18170
18171 static tree
18172 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18173 {
18174 enum tree_code code;
18175 tree nlist, c;
18176
18177 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18178 return list;
18179
18180 switch (cp_lexer_peek_token (parser->lexer)->type)
18181 {
18182 case CPP_PLUS:
18183 code = PLUS_EXPR;
18184 break;
18185 case CPP_MULT:
18186 code = MULT_EXPR;
18187 break;
18188 case CPP_MINUS:
18189 code = MINUS_EXPR;
18190 break;
18191 case CPP_AND:
18192 code = BIT_AND_EXPR;
18193 break;
18194 case CPP_XOR:
18195 code = BIT_XOR_EXPR;
18196 break;
18197 case CPP_OR:
18198 code = BIT_IOR_EXPR;
18199 break;
18200 case CPP_AND_AND:
18201 code = TRUTH_ANDIF_EXPR;
18202 break;
18203 case CPP_OR_OR:
18204 code = TRUTH_ORIF_EXPR;
18205 break;
18206 default:
18207 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18208 resync_fail:
18209 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18210 /*or_comma=*/false,
18211 /*consume_paren=*/true);
18212 return list;
18213 }
18214 cp_lexer_consume_token (parser->lexer);
18215
18216 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18217 goto resync_fail;
18218
18219 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18220 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18221 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18222
18223 return nlist;
18224 }
18225
18226 /* OpenMP 2.5:
18227 schedule ( schedule-kind )
18228 schedule ( schedule-kind , expression )
18229
18230 schedule-kind:
18231 static | dynamic | guided | runtime */
18232
18233 static tree
18234 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18235 {
18236 tree c, t;
18237
18238 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18239 return list;
18240
18241 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18242
18243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18244 {
18245 tree id = cp_lexer_peek_token (parser->lexer)->value;
18246 const char *p = IDENTIFIER_POINTER (id);
18247
18248 switch (p[0])
18249 {
18250 case 'd':
18251 if (strcmp ("dynamic", p) != 0)
18252 goto invalid_kind;
18253 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18254 break;
18255
18256 case 'g':
18257 if (strcmp ("guided", p) != 0)
18258 goto invalid_kind;
18259 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18260 break;
18261
18262 case 'r':
18263 if (strcmp ("runtime", p) != 0)
18264 goto invalid_kind;
18265 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18266 break;
18267
18268 default:
18269 goto invalid_kind;
18270 }
18271 }
18272 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18273 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18274 else
18275 goto invalid_kind;
18276 cp_lexer_consume_token (parser->lexer);
18277
18278 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18279 {
18280 cp_lexer_consume_token (parser->lexer);
18281
18282 t = cp_parser_assignment_expression (parser, false);
18283
18284 if (t == error_mark_node)
18285 goto resync_fail;
18286 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18287 error ("schedule %<runtime%> does not take "
18288 "a %<chunk_size%> parameter");
18289 else
18290 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18291
18292 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18293 goto resync_fail;
18294 }
18295 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18296 goto resync_fail;
18297
18298 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18299 OMP_CLAUSE_CHAIN (c) = list;
18300 return c;
18301
18302 invalid_kind:
18303 cp_parser_error (parser, "invalid schedule kind");
18304 resync_fail:
18305 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18306 /*or_comma=*/false,
18307 /*consume_paren=*/true);
18308 return list;
18309 }
18310
18311 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18312 is a bitmask in MASK. Return the list of clauses found; the result
18313 of clause default goes in *pdefault. */
18314
18315 static tree
18316 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18317 const char *where, cp_token *pragma_tok)
18318 {
18319 tree clauses = NULL;
18320
18321 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18322 {
18323 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18324 const char *c_name;
18325 tree prev = clauses;
18326
18327 switch (c_kind)
18328 {
18329 case PRAGMA_OMP_CLAUSE_COPYIN:
18330 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18331 c_name = "copyin";
18332 break;
18333 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18334 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18335 clauses);
18336 c_name = "copyprivate";
18337 break;
18338 case PRAGMA_OMP_CLAUSE_DEFAULT:
18339 clauses = cp_parser_omp_clause_default (parser, clauses);
18340 c_name = "default";
18341 break;
18342 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18343 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18344 clauses);
18345 c_name = "firstprivate";
18346 break;
18347 case PRAGMA_OMP_CLAUSE_IF:
18348 clauses = cp_parser_omp_clause_if (parser, clauses);
18349 c_name = "if";
18350 break;
18351 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18352 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18353 clauses);
18354 c_name = "lastprivate";
18355 break;
18356 case PRAGMA_OMP_CLAUSE_NOWAIT:
18357 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18358 c_name = "nowait";
18359 break;
18360 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18361 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18362 c_name = "num_threads";
18363 break;
18364 case PRAGMA_OMP_CLAUSE_ORDERED:
18365 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18366 c_name = "ordered";
18367 break;
18368 case PRAGMA_OMP_CLAUSE_PRIVATE:
18369 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18370 clauses);
18371 c_name = "private";
18372 break;
18373 case PRAGMA_OMP_CLAUSE_REDUCTION:
18374 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18375 c_name = "reduction";
18376 break;
18377 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18378 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18379 c_name = "schedule";
18380 break;
18381 case PRAGMA_OMP_CLAUSE_SHARED:
18382 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18383 clauses);
18384 c_name = "shared";
18385 break;
18386 default:
18387 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18388 goto saw_error;
18389 }
18390
18391 if (((mask >> c_kind) & 1) == 0)
18392 {
18393 /* Remove the invalid clause(s) from the list to avoid
18394 confusing the rest of the compiler. */
18395 clauses = prev;
18396 error ("%qs is not valid for %qs", c_name, where);
18397 }
18398 }
18399 saw_error:
18400 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18401 return finish_omp_clauses (clauses);
18402 }
18403
18404 /* OpenMP 2.5:
18405 structured-block:
18406 statement
18407
18408 In practice, we're also interested in adding the statement to an
18409 outer node. So it is convenient if we work around the fact that
18410 cp_parser_statement calls add_stmt. */
18411
18412 static unsigned
18413 cp_parser_begin_omp_structured_block (cp_parser *parser)
18414 {
18415 unsigned save = parser->in_statement;
18416
18417 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18418 This preserves the "not within loop or switch" style error messages
18419 for nonsense cases like
18420 void foo() {
18421 #pragma omp single
18422 break;
18423 }
18424 */
18425 if (parser->in_statement)
18426 parser->in_statement = IN_OMP_BLOCK;
18427
18428 return save;
18429 }
18430
18431 static void
18432 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18433 {
18434 parser->in_statement = save;
18435 }
18436
18437 static tree
18438 cp_parser_omp_structured_block (cp_parser *parser)
18439 {
18440 tree stmt = begin_omp_structured_block ();
18441 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18442
18443 cp_parser_statement (parser, NULL_TREE, false);
18444
18445 cp_parser_end_omp_structured_block (parser, save);
18446 return finish_omp_structured_block (stmt);
18447 }
18448
18449 /* OpenMP 2.5:
18450 # pragma omp atomic new-line
18451 expression-stmt
18452
18453 expression-stmt:
18454 x binop= expr | x++ | ++x | x-- | --x
18455 binop:
18456 +, *, -, /, &, ^, |, <<, >>
18457
18458 where x is an lvalue expression with scalar type. */
18459
18460 static void
18461 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18462 {
18463 tree lhs, rhs;
18464 enum tree_code code;
18465
18466 cp_parser_require_pragma_eol (parser, pragma_tok);
18467
18468 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18469 /*cast_p=*/false);
18470 switch (TREE_CODE (lhs))
18471 {
18472 case ERROR_MARK:
18473 goto saw_error;
18474
18475 case PREINCREMENT_EXPR:
18476 case POSTINCREMENT_EXPR:
18477 lhs = TREE_OPERAND (lhs, 0);
18478 code = PLUS_EXPR;
18479 rhs = integer_one_node;
18480 break;
18481
18482 case PREDECREMENT_EXPR:
18483 case POSTDECREMENT_EXPR:
18484 lhs = TREE_OPERAND (lhs, 0);
18485 code = MINUS_EXPR;
18486 rhs = integer_one_node;
18487 break;
18488
18489 default:
18490 switch (cp_lexer_peek_token (parser->lexer)->type)
18491 {
18492 case CPP_MULT_EQ:
18493 code = MULT_EXPR;
18494 break;
18495 case CPP_DIV_EQ:
18496 code = TRUNC_DIV_EXPR;
18497 break;
18498 case CPP_PLUS_EQ:
18499 code = PLUS_EXPR;
18500 break;
18501 case CPP_MINUS_EQ:
18502 code = MINUS_EXPR;
18503 break;
18504 case CPP_LSHIFT_EQ:
18505 code = LSHIFT_EXPR;
18506 break;
18507 case CPP_RSHIFT_EQ:
18508 code = RSHIFT_EXPR;
18509 break;
18510 case CPP_AND_EQ:
18511 code = BIT_AND_EXPR;
18512 break;
18513 case CPP_OR_EQ:
18514 code = BIT_IOR_EXPR;
18515 break;
18516 case CPP_XOR_EQ:
18517 code = BIT_XOR_EXPR;
18518 break;
18519 default:
18520 cp_parser_error (parser,
18521 "invalid operator for %<#pragma omp atomic%>");
18522 goto saw_error;
18523 }
18524 cp_lexer_consume_token (parser->lexer);
18525
18526 rhs = cp_parser_expression (parser, false);
18527 if (rhs == error_mark_node)
18528 goto saw_error;
18529 break;
18530 }
18531 finish_omp_atomic (code, lhs, rhs);
18532 cp_parser_consume_semicolon_at_end_of_statement (parser);
18533 return;
18534
18535 saw_error:
18536 cp_parser_skip_to_end_of_block_or_statement (parser);
18537 }
18538
18539
18540 /* OpenMP 2.5:
18541 # pragma omp barrier new-line */
18542
18543 static void
18544 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18545 {
18546 cp_parser_require_pragma_eol (parser, pragma_tok);
18547 finish_omp_barrier ();
18548 }
18549
18550 /* OpenMP 2.5:
18551 # pragma omp critical [(name)] new-line
18552 structured-block */
18553
18554 static tree
18555 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18556 {
18557 tree stmt, name = NULL;
18558
18559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18560 {
18561 cp_lexer_consume_token (parser->lexer);
18562
18563 name = cp_parser_identifier (parser);
18564
18565 if (name == error_mark_node
18566 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18568 /*or_comma=*/false,
18569 /*consume_paren=*/true);
18570 if (name == error_mark_node)
18571 name = NULL;
18572 }
18573 cp_parser_require_pragma_eol (parser, pragma_tok);
18574
18575 stmt = cp_parser_omp_structured_block (parser);
18576 return c_finish_omp_critical (stmt, name);
18577 }
18578
18579 /* OpenMP 2.5:
18580 # pragma omp flush flush-vars[opt] new-line
18581
18582 flush-vars:
18583 ( variable-list ) */
18584
18585 static void
18586 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18587 {
18588 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18589 (void) cp_parser_omp_var_list (parser, 0, NULL);
18590 cp_parser_require_pragma_eol (parser, pragma_tok);
18591
18592 finish_omp_flush ();
18593 }
18594
18595 /* Parse the restricted form of the for statment allowed by OpenMP. */
18596
18597 static tree
18598 cp_parser_omp_for_loop (cp_parser *parser)
18599 {
18600 tree init, cond, incr, body, decl, pre_body;
18601 location_t loc;
18602
18603 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18604 {
18605 cp_parser_error (parser, "for statement expected");
18606 return NULL;
18607 }
18608 loc = cp_lexer_consume_token (parser->lexer)->location;
18609 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18610 return NULL;
18611
18612 init = decl = NULL;
18613 pre_body = push_stmt_list ();
18614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18615 {
18616 cp_decl_specifier_seq type_specifiers;
18617
18618 /* First, try to parse as an initialized declaration. See
18619 cp_parser_condition, from whence the bulk of this is copied. */
18620
18621 cp_parser_parse_tentatively (parser);
18622 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18623 &type_specifiers);
18624 if (!cp_parser_error_occurred (parser))
18625 {
18626 tree asm_specification, attributes;
18627 cp_declarator *declarator;
18628
18629 declarator = cp_parser_declarator (parser,
18630 CP_PARSER_DECLARATOR_NAMED,
18631 /*ctor_dtor_or_conv_p=*/NULL,
18632 /*parenthesized_p=*/NULL,
18633 /*member_p=*/false);
18634 attributes = cp_parser_attributes_opt (parser);
18635 asm_specification = cp_parser_asm_specification_opt (parser);
18636
18637 cp_parser_require (parser, CPP_EQ, "`='");
18638 if (cp_parser_parse_definitely (parser))
18639 {
18640 tree pushed_scope;
18641
18642 decl = start_decl (declarator, &type_specifiers,
18643 /*initialized_p=*/false, attributes,
18644 /*prefix_attributes=*/NULL_TREE,
18645 &pushed_scope);
18646
18647 init = cp_parser_assignment_expression (parser, false);
18648
18649 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18650 asm_specification, LOOKUP_ONLYCONVERTING);
18651
18652 if (pushed_scope)
18653 pop_scope (pushed_scope);
18654 }
18655 }
18656 else
18657 cp_parser_abort_tentative_parse (parser);
18658
18659 /* If parsing as an initialized declaration failed, try again as
18660 a simple expression. */
18661 if (decl == NULL)
18662 init = cp_parser_expression (parser, false);
18663 }
18664 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18665 pre_body = pop_stmt_list (pre_body);
18666
18667 cond = NULL;
18668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18669 cond = cp_parser_condition (parser);
18670 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18671
18672 incr = NULL;
18673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18674 incr = cp_parser_expression (parser, false);
18675
18676 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18678 /*or_comma=*/false,
18679 /*consume_paren=*/true);
18680
18681 /* Note that we saved the original contents of this flag when we entered
18682 the structured block, and so we don't need to re-save it here. */
18683 parser->in_statement = IN_OMP_FOR;
18684
18685 /* Note that the grammar doesn't call for a structured block here,
18686 though the loop as a whole is a structured block. */
18687 body = push_stmt_list ();
18688 cp_parser_statement (parser, NULL_TREE, false);
18689 body = pop_stmt_list (body);
18690
18691 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18692 }
18693
18694 /* OpenMP 2.5:
18695 #pragma omp for for-clause[optseq] new-line
18696 for-loop */
18697
18698 #define OMP_FOR_CLAUSE_MASK \
18699 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18700 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18701 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18702 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18703 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18704 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18705 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18706
18707 static tree
18708 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18709 {
18710 tree clauses, sb, ret;
18711 unsigned int save;
18712
18713 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18714 "#pragma omp for", pragma_tok);
18715
18716 sb = begin_omp_structured_block ();
18717 save = cp_parser_begin_omp_structured_block (parser);
18718
18719 ret = cp_parser_omp_for_loop (parser);
18720 if (ret)
18721 OMP_FOR_CLAUSES (ret) = clauses;
18722
18723 cp_parser_end_omp_structured_block (parser, save);
18724 add_stmt (finish_omp_structured_block (sb));
18725
18726 return ret;
18727 }
18728
18729 /* OpenMP 2.5:
18730 # pragma omp master new-line
18731 structured-block */
18732
18733 static tree
18734 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18735 {
18736 cp_parser_require_pragma_eol (parser, pragma_tok);
18737 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18738 }
18739
18740 /* OpenMP 2.5:
18741 # pragma omp ordered new-line
18742 structured-block */
18743
18744 static tree
18745 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18746 {
18747 cp_parser_require_pragma_eol (parser, pragma_tok);
18748 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18749 }
18750
18751 /* OpenMP 2.5:
18752
18753 section-scope:
18754 { section-sequence }
18755
18756 section-sequence:
18757 section-directive[opt] structured-block
18758 section-sequence section-directive structured-block */
18759
18760 static tree
18761 cp_parser_omp_sections_scope (cp_parser *parser)
18762 {
18763 tree stmt, substmt;
18764 bool error_suppress = false;
18765 cp_token *tok;
18766
18767 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18768 return NULL_TREE;
18769
18770 stmt = push_stmt_list ();
18771
18772 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18773 {
18774 unsigned save;
18775
18776 substmt = begin_omp_structured_block ();
18777 save = cp_parser_begin_omp_structured_block (parser);
18778
18779 while (1)
18780 {
18781 cp_parser_statement (parser, NULL_TREE, false);
18782
18783 tok = cp_lexer_peek_token (parser->lexer);
18784 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18785 break;
18786 if (tok->type == CPP_CLOSE_BRACE)
18787 break;
18788 if (tok->type == CPP_EOF)
18789 break;
18790 }
18791
18792 cp_parser_end_omp_structured_block (parser, save);
18793 substmt = finish_omp_structured_block (substmt);
18794 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18795 add_stmt (substmt);
18796 }
18797
18798 while (1)
18799 {
18800 tok = cp_lexer_peek_token (parser->lexer);
18801 if (tok->type == CPP_CLOSE_BRACE)
18802 break;
18803 if (tok->type == CPP_EOF)
18804 break;
18805
18806 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18807 {
18808 cp_lexer_consume_token (parser->lexer);
18809 cp_parser_require_pragma_eol (parser, tok);
18810 error_suppress = false;
18811 }
18812 else if (!error_suppress)
18813 {
18814 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18815 error_suppress = true;
18816 }
18817
18818 substmt = cp_parser_omp_structured_block (parser);
18819 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18820 add_stmt (substmt);
18821 }
18822 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18823
18824 substmt = pop_stmt_list (stmt);
18825
18826 stmt = make_node (OMP_SECTIONS);
18827 TREE_TYPE (stmt) = void_type_node;
18828 OMP_SECTIONS_BODY (stmt) = substmt;
18829
18830 add_stmt (stmt);
18831 return stmt;
18832 }
18833
18834 /* OpenMP 2.5:
18835 # pragma omp sections sections-clause[optseq] newline
18836 sections-scope */
18837
18838 #define OMP_SECTIONS_CLAUSE_MASK \
18839 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18840 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18841 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18842 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18843 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18844
18845 static tree
18846 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18847 {
18848 tree clauses, ret;
18849
18850 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18851 "#pragma omp sections", pragma_tok);
18852
18853 ret = cp_parser_omp_sections_scope (parser);
18854 if (ret)
18855 OMP_SECTIONS_CLAUSES (ret) = clauses;
18856
18857 return ret;
18858 }
18859
18860 /* OpenMP 2.5:
18861 # pragma parallel parallel-clause new-line
18862 # pragma parallel for parallel-for-clause new-line
18863 # pragma parallel sections parallel-sections-clause new-line */
18864
18865 #define OMP_PARALLEL_CLAUSE_MASK \
18866 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18867 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18868 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18869 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18870 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18871 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18872 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18873 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18874
18875 static tree
18876 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18877 {
18878 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18879 const char *p_name = "#pragma omp parallel";
18880 tree stmt, clauses, par_clause, ws_clause, block;
18881 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18882 unsigned int save;
18883
18884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18885 {
18886 cp_lexer_consume_token (parser->lexer);
18887 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18888 p_name = "#pragma omp parallel for";
18889 mask |= OMP_FOR_CLAUSE_MASK;
18890 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18891 }
18892 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18893 {
18894 tree id = cp_lexer_peek_token (parser->lexer)->value;
18895 const char *p = IDENTIFIER_POINTER (id);
18896 if (strcmp (p, "sections") == 0)
18897 {
18898 cp_lexer_consume_token (parser->lexer);
18899 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18900 p_name = "#pragma omp parallel sections";
18901 mask |= OMP_SECTIONS_CLAUSE_MASK;
18902 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18903 }
18904 }
18905
18906 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18907 block = begin_omp_parallel ();
18908 save = cp_parser_begin_omp_structured_block (parser);
18909
18910 switch (p_kind)
18911 {
18912 case PRAGMA_OMP_PARALLEL:
18913 cp_parser_already_scoped_statement (parser);
18914 par_clause = clauses;
18915 break;
18916
18917 case PRAGMA_OMP_PARALLEL_FOR:
18918 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18919 stmt = cp_parser_omp_for_loop (parser);
18920 if (stmt)
18921 OMP_FOR_CLAUSES (stmt) = ws_clause;
18922 break;
18923
18924 case PRAGMA_OMP_PARALLEL_SECTIONS:
18925 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18926 stmt = cp_parser_omp_sections_scope (parser);
18927 if (stmt)
18928 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18929 break;
18930
18931 default:
18932 gcc_unreachable ();
18933 }
18934
18935 cp_parser_end_omp_structured_block (parser, save);
18936 stmt = finish_omp_parallel (par_clause, block);
18937 if (p_kind != PRAGMA_OMP_PARALLEL)
18938 OMP_PARALLEL_COMBINED (stmt) = 1;
18939 return stmt;
18940 }
18941
18942 /* OpenMP 2.5:
18943 # pragma omp single single-clause[optseq] new-line
18944 structured-block */
18945
18946 #define OMP_SINGLE_CLAUSE_MASK \
18947 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18948 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18949 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18950 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18951
18952 static tree
18953 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18954 {
18955 tree stmt = make_node (OMP_SINGLE);
18956 TREE_TYPE (stmt) = void_type_node;
18957
18958 OMP_SINGLE_CLAUSES (stmt)
18959 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18960 "#pragma omp single", pragma_tok);
18961 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18962
18963 return add_stmt (stmt);
18964 }
18965
18966 /* OpenMP 2.5:
18967 # pragma omp threadprivate (variable-list) */
18968
18969 static void
18970 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18971 {
18972 tree vars;
18973
18974 vars = cp_parser_omp_var_list (parser, 0, NULL);
18975 cp_parser_require_pragma_eol (parser, pragma_tok);
18976
18977 if (!targetm.have_tls)
18978 sorry ("threadprivate variables not supported in this target");
18979
18980 finish_omp_threadprivate (vars);
18981 }
18982
18983 /* Main entry point to OpenMP statement pragmas. */
18984
18985 static void
18986 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18987 {
18988 tree stmt;
18989
18990 switch (pragma_tok->pragma_kind)
18991 {
18992 case PRAGMA_OMP_ATOMIC:
18993 cp_parser_omp_atomic (parser, pragma_tok);
18994 return;
18995 case PRAGMA_OMP_CRITICAL:
18996 stmt = cp_parser_omp_critical (parser, pragma_tok);
18997 break;
18998 case PRAGMA_OMP_FOR:
18999 stmt = cp_parser_omp_for (parser, pragma_tok);
19000 break;
19001 case PRAGMA_OMP_MASTER:
19002 stmt = cp_parser_omp_master (parser, pragma_tok);
19003 break;
19004 case PRAGMA_OMP_ORDERED:
19005 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19006 break;
19007 case PRAGMA_OMP_PARALLEL:
19008 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19009 break;
19010 case PRAGMA_OMP_SECTIONS:
19011 stmt = cp_parser_omp_sections (parser, pragma_tok);
19012 break;
19013 case PRAGMA_OMP_SINGLE:
19014 stmt = cp_parser_omp_single (parser, pragma_tok);
19015 break;
19016 default:
19017 gcc_unreachable ();
19018 }
19019
19020 if (stmt)
19021 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19022 }
19023 \f
19024 /* The parser. */
19025
19026 static GTY (()) cp_parser *the_parser;
19027
19028 \f
19029 /* Special handling for the first token or line in the file. The first
19030 thing in the file might be #pragma GCC pch_preprocess, which loads a
19031 PCH file, which is a GC collection point. So we need to handle this
19032 first pragma without benefit of an existing lexer structure.
19033
19034 Always returns one token to the caller in *FIRST_TOKEN. This is
19035 either the true first token of the file, or the first token after
19036 the initial pragma. */
19037
19038 static void
19039 cp_parser_initial_pragma (cp_token *first_token)
19040 {
19041 tree name = NULL;
19042
19043 cp_lexer_get_preprocessor_token (NULL, first_token);
19044 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19045 return;
19046
19047 cp_lexer_get_preprocessor_token (NULL, first_token);
19048 if (first_token->type == CPP_STRING)
19049 {
19050 name = first_token->value;
19051
19052 cp_lexer_get_preprocessor_token (NULL, first_token);
19053 if (first_token->type != CPP_PRAGMA_EOL)
19054 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19055 }
19056 else
19057 error ("expected string literal");
19058
19059 /* Skip to the end of the pragma. */
19060 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19061 cp_lexer_get_preprocessor_token (NULL, first_token);
19062
19063 /* Now actually load the PCH file. */
19064 if (name)
19065 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19066
19067 /* Read one more token to return to our caller. We have to do this
19068 after reading the PCH file in, since its pointers have to be
19069 live. */
19070 cp_lexer_get_preprocessor_token (NULL, first_token);
19071 }
19072
19073 /* Normal parsing of a pragma token. Here we can (and must) use the
19074 regular lexer. */
19075
19076 static bool
19077 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19078 {
19079 cp_token *pragma_tok;
19080 unsigned int id;
19081
19082 pragma_tok = cp_lexer_consume_token (parser->lexer);
19083 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19084 parser->lexer->in_pragma = true;
19085
19086 id = pragma_tok->pragma_kind;
19087 switch (id)
19088 {
19089 case PRAGMA_GCC_PCH_PREPROCESS:
19090 error ("%<#pragma GCC pch_preprocess%> must be first");
19091 break;
19092
19093 case PRAGMA_OMP_BARRIER:
19094 switch (context)
19095 {
19096 case pragma_compound:
19097 cp_parser_omp_barrier (parser, pragma_tok);
19098 return false;
19099 case pragma_stmt:
19100 error ("%<#pragma omp barrier%> may only be "
19101 "used in compound statements");
19102 break;
19103 default:
19104 goto bad_stmt;
19105 }
19106 break;
19107
19108 case PRAGMA_OMP_FLUSH:
19109 switch (context)
19110 {
19111 case pragma_compound:
19112 cp_parser_omp_flush (parser, pragma_tok);
19113 return false;
19114 case pragma_stmt:
19115 error ("%<#pragma omp flush%> may only be "
19116 "used in compound statements");
19117 break;
19118 default:
19119 goto bad_stmt;
19120 }
19121 break;
19122
19123 case PRAGMA_OMP_THREADPRIVATE:
19124 cp_parser_omp_threadprivate (parser, pragma_tok);
19125 return false;
19126
19127 case PRAGMA_OMP_ATOMIC:
19128 case PRAGMA_OMP_CRITICAL:
19129 case PRAGMA_OMP_FOR:
19130 case PRAGMA_OMP_MASTER:
19131 case PRAGMA_OMP_ORDERED:
19132 case PRAGMA_OMP_PARALLEL:
19133 case PRAGMA_OMP_SECTIONS:
19134 case PRAGMA_OMP_SINGLE:
19135 if (context == pragma_external)
19136 goto bad_stmt;
19137 cp_parser_omp_construct (parser, pragma_tok);
19138 return true;
19139
19140 case PRAGMA_OMP_SECTION:
19141 error ("%<#pragma omp section%> may only be used in "
19142 "%<#pragma omp sections%> construct");
19143 break;
19144
19145 default:
19146 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19147 c_invoke_pragma_handler (id);
19148 break;
19149
19150 bad_stmt:
19151 cp_parser_error (parser, "expected declaration specifiers");
19152 break;
19153 }
19154
19155 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19156 return false;
19157 }
19158
19159 /* The interface the pragma parsers have to the lexer. */
19160
19161 enum cpp_ttype
19162 pragma_lex (tree *value)
19163 {
19164 cp_token *tok;
19165 enum cpp_ttype ret;
19166
19167 tok = cp_lexer_peek_token (the_parser->lexer);
19168
19169 ret = tok->type;
19170 *value = tok->value;
19171
19172 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19173 ret = CPP_EOF;
19174 else if (ret == CPP_STRING)
19175 *value = cp_parser_string_literal (the_parser, false, false);
19176 else
19177 {
19178 cp_lexer_consume_token (the_parser->lexer);
19179 if (ret == CPP_KEYWORD)
19180 ret = CPP_NAME;
19181 }
19182
19183 return ret;
19184 }
19185
19186 \f
19187 /* External interface. */
19188
19189 /* Parse one entire translation unit. */
19190
19191 void
19192 c_parse_file (void)
19193 {
19194 bool error_occurred;
19195 static bool already_called = false;
19196
19197 if (already_called)
19198 {
19199 sorry ("inter-module optimizations not implemented for C++");
19200 return;
19201 }
19202 already_called = true;
19203
19204 the_parser = cp_parser_new ();
19205 push_deferring_access_checks (flag_access_control
19206 ? dk_no_deferred : dk_no_check);
19207 error_occurred = cp_parser_translation_unit (the_parser);
19208 the_parser = NULL;
19209 }
19210
19211 /* This variable must be provided by every front end. */
19212
19213 int yydebug;
19214
19215 #include "gt-cp-parser.h"