re PR c++/28211 (wrong linkage of template argument, diagnostic could be improved)
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 /* A C++ token. */
49
50 typedef struct cp_token GTY (())
51 {
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
57 /* Token flags. */
58 unsigned char flags;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The input file stack index at which this token was found. */
70 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71 /* The value associated with this token, if any. */
72 tree value;
73 /* The location at which this token was found. */
74 location_t location;
75 } cp_token;
76
77 /* We use a stack of token pointer for saving token sets. */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
81
82 static const cp_token eof_token =
83 {
84 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
86 0
87 #else
88 {0, 0}
89 #endif
90 };
91
92 /* The cp_lexer structure represents the C++ lexer. It is responsible
93 for managing the token stream from the preprocessor and supplying
94 it to the parser. Tokens are never added to the cp_lexer after
95 it is created. */
96
97 typedef struct cp_lexer GTY (())
98 {
99 /* The memory allocated for the buffer. NULL if this lexer does not
100 own the token buffer. */
101 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102 /* If the lexer owns the buffer, this is the number of tokens in the
103 buffer. */
104 size_t buffer_length;
105
106 /* A pointer just past the last available token. The tokens
107 in this lexer are [buffer, last_token). */
108 cp_token_position GTY ((skip)) last_token;
109
110 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
111 no more available tokens. */
112 cp_token_position GTY ((skip)) next_token;
113
114 /* A stack indicating positions at which cp_lexer_save_tokens was
115 called. The top entry is the most recent position at which we
116 began saving tokens. If the stack is non-empty, we are saving
117 tokens. */
118 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
119
120 /* The next lexer in a linked list of lexers. */
121 struct cp_lexer *next;
122
123 /* True if we should output debugging information. */
124 bool debugging_p;
125
126 /* True if we're in the context of parsing a pragma, and should not
127 increment past the end-of-line marker. */
128 bool in_pragma;
129 } cp_lexer;
130
131 /* cp_token_cache is a range of tokens. There is no need to represent
132 allocate heap memory for it, since tokens are never removed from the
133 lexer's array. There is also no need for the GC to walk through
134 a cp_token_cache, since everything in here is referenced through
135 a lexer. */
136
137 typedef struct cp_token_cache GTY(())
138 {
139 /* The beginning of the token range. */
140 cp_token * GTY((skip)) first;
141
142 /* Points immediately after the last token in the range. */
143 cp_token * GTY ((skip)) last;
144 } cp_token_cache;
145
146 /* Prototypes. */
147
148 static cp_lexer *cp_lexer_new_main
149 (void);
150 static cp_lexer *cp_lexer_new_from_tokens
151 (cp_token_cache *tokens);
152 static void cp_lexer_destroy
153 (cp_lexer *);
154 static int cp_lexer_saving_tokens
155 (const cp_lexer *);
156 static cp_token_position cp_lexer_token_position
157 (cp_lexer *, bool);
158 static cp_token *cp_lexer_token_at
159 (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161 (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
163 (cp_lexer *);
164 static cp_token *cp_lexer_peek_nth_token
165 (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169 (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171 (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
173 (cp_lexer *);
174 static void cp_lexer_purge_token
175 (cp_lexer *);
176 static void cp_lexer_purge_tokens_after
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
179 (cp_lexer *);
180 static void cp_lexer_commit_tokens
181 (cp_lexer *);
182 static void cp_lexer_rollback_tokens
183 (cp_lexer *);
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186 (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
188 (cp_lexer *);
189 static void cp_lexer_start_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192 (cp_lexer *) ATTRIBUTE_UNUSED;
193 #else
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195 about passing NULL to functions that require non-NULL arguments
196 (fputs, fprintf). It will never be used, so all we need is a value
197 of the right type that's guaranteed not to be NULL. */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
202
203 static cp_token_cache *cp_token_cache_new
204 (cp_token *, cp_token *);
205
206 static void cp_parser_initial_pragma
207 (cp_token *);
208
209 /* Manifest constants. */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
212
213 /* A token type for keywords, as opposed to ordinary identifiers. */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
215
216 /* A token type for template-ids. If a template-id is processed while
217 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218 the value of the CPP_TEMPLATE_ID is whatever was returned by
219 cp_parser_template_id. */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
221
222 /* A token type for nested-name-specifiers. If a
223 nested-name-specifier is processed while parsing tentatively, it is
224 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226 cp_parser_nested_name_specifier_opt. */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
228
229 /* A token type for tokens that are not tokens at all; these are used
230 to represent slots in the array where there used to be a token
231 that has now been deleted. */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
233
234 /* The number of token types, including C++-specific ones. */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
236
237 /* Variables. */
238
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
243
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
245 preprocessor. */
246
247 static cp_lexer *
248 cp_lexer_new_main (void)
249 {
250 cp_token first_token;
251 cp_lexer *lexer;
252 cp_token *pos;
253 size_t alloc;
254 size_t space;
255 cp_token *buffer;
256
257 /* It's possible that parsing the first pragma will load a PCH file,
258 which is a GC collection point. So we have to do that before
259 allocating any memory. */
260 cp_parser_initial_pragma (&first_token);
261
262 /* Tell c_lex_with_flags not to merge string constants. */
263 c_lex_return_raw_strings = true;
264
265 c_common_no_more_pch ();
266
267 /* Allocate the memory. */
268 lexer = GGC_CNEW (cp_lexer);
269
270 #ifdef ENABLE_CHECKING
271 /* Initially we are not debugging. */
272 lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275 CP_SAVED_TOKEN_STACK);
276
277 /* Create the buffer. */
278 alloc = CP_LEXER_BUFFER_SIZE;
279 buffer = GGC_NEWVEC (cp_token, alloc);
280
281 /* Put the first token in the buffer. */
282 space = alloc;
283 pos = buffer;
284 *pos = first_token;
285
286 /* Get the remaining tokens from the preprocessor. */
287 while (pos->type != CPP_EOF)
288 {
289 pos++;
290 if (!--space)
291 {
292 space = alloc;
293 alloc *= 2;
294 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295 pos = buffer + space;
296 }
297 cp_lexer_get_preprocessor_token (lexer, pos);
298 }
299 lexer->buffer = buffer;
300 lexer->buffer_length = alloc - space;
301 lexer->last_token = pos;
302 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
303
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
310 return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
328
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
335 #endif
336
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
338 return lexer;
339 }
340
341 /* Frees all resources associated with LEXER. */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346 if (lexer->buffer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
349 ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output. */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359 return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369 return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375 return pos;
376 }
377
378 /* nonzero if we are presently saving tokens. */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN. Return true
387 if we reach EOF. */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 cp_token *token)
392 {
393 static int is_extern_c = 0;
394
395 /* Get a new token from the preprocessor. */
396 token->type
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->input_file_stack_index = input_file_stack_tick;
399 token->keyword = RID_MAX;
400 token->pragma_kind = PRAGMA_NONE;
401 token->in_system_header = in_system_header;
402
403 /* On some systems, some header files are surrounded by an
404 implicit extern "C" block. Set a flag in the token if it
405 comes from such a header. */
406 is_extern_c += pending_lang_change;
407 pending_lang_change = 0;
408 token->implicit_extern_c = is_extern_c > 0;
409
410 /* Check to see if this token is a keyword. */
411 if (token->type == CPP_NAME)
412 {
413 if (C_IS_RESERVED_WORD (token->value))
414 {
415 /* Mark this token as a keyword. */
416 token->type = CPP_KEYWORD;
417 /* Record which keyword. */
418 token->keyword = C_RID_CODE (token->value);
419 /* Update the value. Some keywords are mapped to particular
420 entities, rather than simply having the value of the
421 corresponding IDENTIFIER_NODE. For example, `__const' is
422 mapped to `const'. */
423 token->value = ridpointers[token->keyword];
424 }
425 else
426 {
427 token->ambiguous_p = false;
428 token->keyword = RID_MAX;
429 }
430 }
431 /* Handle Objective-C++ keywords. */
432 else if (token->type == CPP_AT_NAME)
433 {
434 token->type = CPP_KEYWORD;
435 switch (C_RID_CODE (token->value))
436 {
437 /* Map 'class' to '@class', 'private' to '@private', etc. */
438 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442 case RID_THROW: token->keyword = RID_AT_THROW; break;
443 case RID_TRY: token->keyword = RID_AT_TRY; break;
444 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445 default: token->keyword = C_RID_CODE (token->value);
446 }
447 }
448 else if (token->type == CPP_PRAGMA)
449 {
450 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
451 token->pragma_kind = TREE_INT_CST_LOW (token->value);
452 token->value = NULL;
453 }
454 }
455
456 /* Update the globals input_location and in_system_header and the
457 input file stack from TOKEN. */
458 static inline void
459 cp_lexer_set_source_position_from_token (cp_token *token)
460 {
461 if (token->type != CPP_EOF)
462 {
463 input_location = token->location;
464 in_system_header = token->in_system_header;
465 restore_input_file_stack (token->input_file_stack_index);
466 }
467 }
468
469 /* Return a pointer to the next token in the token stream, but do not
470 consume it. */
471
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
474 {
475 if (cp_lexer_debugging_p (lexer))
476 {
477 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479 putc ('\n', cp_lexer_debug_stream);
480 }
481 return lexer->next_token;
482 }
483
484 /* Return true if the next token has the indicated TYPE. */
485
486 static inline bool
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
488 {
489 return cp_lexer_peek_token (lexer)->type == type;
490 }
491
492 /* Return true if the next token does not have the indicated TYPE. */
493
494 static inline bool
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
496 {
497 return !cp_lexer_next_token_is (lexer, type);
498 }
499
500 /* Return true if the next token is the indicated KEYWORD. */
501
502 static inline bool
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
504 {
505 return cp_lexer_peek_token (lexer)->keyword == keyword;
506 }
507
508 /* Return a pointer to the Nth token in the token stream. If N is 1,
509 then this is precisely equivalent to cp_lexer_peek_token (except
510 that it is not inline). One would like to disallow that case, but
511 there is one case (cp_parser_nth_token_starts_template_id) where
512 the caller passes a variable for N and it might be 1. */
513
514 static cp_token *
515 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
516 {
517 cp_token *token;
518
519 /* N is 1-based, not zero-based. */
520 gcc_assert (n > 0);
521
522 if (cp_lexer_debugging_p (lexer))
523 fprintf (cp_lexer_debug_stream,
524 "cp_lexer: peeking ahead %ld at token: ", (long)n);
525
526 --n;
527 token = lexer->next_token;
528 gcc_assert (!n || token != &eof_token);
529 while (n != 0)
530 {
531 ++token;
532 if (token == lexer->last_token)
533 {
534 token = (cp_token *)&eof_token;
535 break;
536 }
537
538 if (token->type != CPP_PURGED)
539 --n;
540 }
541
542 if (cp_lexer_debugging_p (lexer))
543 {
544 cp_lexer_print_token (cp_lexer_debug_stream, token);
545 putc ('\n', cp_lexer_debug_stream);
546 }
547
548 return token;
549 }
550
551 /* Return the next token, and advance the lexer's next_token pointer
552 to point to the next non-purged token. */
553
554 static cp_token *
555 cp_lexer_consume_token (cp_lexer* lexer)
556 {
557 cp_token *token = lexer->next_token;
558
559 gcc_assert (token != &eof_token);
560 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
561
562 do
563 {
564 lexer->next_token++;
565 if (lexer->next_token == lexer->last_token)
566 {
567 lexer->next_token = (cp_token *)&eof_token;
568 break;
569 }
570
571 }
572 while (lexer->next_token->type == CPP_PURGED);
573
574 cp_lexer_set_source_position_from_token (token);
575
576 /* Provide debugging output. */
577 if (cp_lexer_debugging_p (lexer))
578 {
579 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
580 cp_lexer_print_token (cp_lexer_debug_stream, token);
581 putc ('\n', cp_lexer_debug_stream);
582 }
583
584 return token;
585 }
586
587 /* Permanently remove the next token from the token stream, and
588 advance the next_token pointer to refer to the next non-purged
589 token. */
590
591 static void
592 cp_lexer_purge_token (cp_lexer *lexer)
593 {
594 cp_token *tok = lexer->next_token;
595
596 gcc_assert (tok != &eof_token);
597 tok->type = CPP_PURGED;
598 tok->location = UNKNOWN_LOCATION;
599 tok->value = NULL_TREE;
600 tok->keyword = RID_MAX;
601
602 do
603 {
604 tok++;
605 if (tok == lexer->last_token)
606 {
607 tok = (cp_token *)&eof_token;
608 break;
609 }
610 }
611 while (tok->type == CPP_PURGED);
612 lexer->next_token = tok;
613 }
614
615 /* Permanently remove all tokens after TOK, up to, but not
616 including, the token that will be returned next by
617 cp_lexer_peek_token. */
618
619 static void
620 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
621 {
622 cp_token *peek = lexer->next_token;
623
624 if (peek == &eof_token)
625 peek = lexer->last_token;
626
627 gcc_assert (tok < peek);
628
629 for ( tok += 1; tok != peek; tok += 1)
630 {
631 tok->type = CPP_PURGED;
632 tok->location = UNKNOWN_LOCATION;
633 tok->value = NULL_TREE;
634 tok->keyword = RID_MAX;
635 }
636 }
637
638 /* Begin saving tokens. All tokens consumed after this point will be
639 preserved. */
640
641 static void
642 cp_lexer_save_tokens (cp_lexer* lexer)
643 {
644 /* Provide debugging output. */
645 if (cp_lexer_debugging_p (lexer))
646 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
647
648 VEC_safe_push (cp_token_position, heap,
649 lexer->saved_tokens, lexer->next_token);
650 }
651
652 /* Commit to the portion of the token stream most recently saved. */
653
654 static void
655 cp_lexer_commit_tokens (cp_lexer* lexer)
656 {
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
659 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
660
661 VEC_pop (cp_token_position, lexer->saved_tokens);
662 }
663
664 /* Return all tokens saved since the last call to cp_lexer_save_tokens
665 to the token stream. Stop saving tokens. */
666
667 static void
668 cp_lexer_rollback_tokens (cp_lexer* lexer)
669 {
670 /* Provide debugging output. */
671 if (cp_lexer_debugging_p (lexer))
672 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
673
674 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
675 }
676
677 /* Print a representation of the TOKEN on the STREAM. */
678
679 #ifdef ENABLE_CHECKING
680
681 static void
682 cp_lexer_print_token (FILE * stream, cp_token *token)
683 {
684 /* We don't use cpp_type2name here because the parser defines
685 a few tokens of its own. */
686 static const char *const token_names[] = {
687 /* cpplib-defined token types */
688 #define OP(e, s) #e,
689 #define TK(e, s) #e,
690 TTYPE_TABLE
691 #undef OP
692 #undef TK
693 /* C++ parser token types - see "Manifest constants", above. */
694 "KEYWORD",
695 "TEMPLATE_ID",
696 "NESTED_NAME_SPECIFIER",
697 "PURGED"
698 };
699
700 /* If we have a name for the token, print it out. Otherwise, we
701 simply give the numeric code. */
702 gcc_assert (token->type < ARRAY_SIZE(token_names));
703 fputs (token_names[token->type], stream);
704
705 /* For some tokens, print the associated data. */
706 switch (token->type)
707 {
708 case CPP_KEYWORD:
709 /* Some keywords have a value that is not an IDENTIFIER_NODE.
710 For example, `struct' is mapped to an INTEGER_CST. */
711 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
712 break;
713 /* else fall through */
714 case CPP_NAME:
715 fputs (IDENTIFIER_POINTER (token->value), stream);
716 break;
717
718 case CPP_STRING:
719 case CPP_WSTRING:
720 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
721 break;
722
723 default:
724 break;
725 }
726 }
727
728 /* Start emitting debugging information. */
729
730 static void
731 cp_lexer_start_debugging (cp_lexer* lexer)
732 {
733 lexer->debugging_p = true;
734 }
735
736 /* Stop emitting debugging information. */
737
738 static void
739 cp_lexer_stop_debugging (cp_lexer* lexer)
740 {
741 lexer->debugging_p = false;
742 }
743
744 #endif /* ENABLE_CHECKING */
745
746 /* Create a new cp_token_cache, representing a range of tokens. */
747
748 static cp_token_cache *
749 cp_token_cache_new (cp_token *first, cp_token *last)
750 {
751 cp_token_cache *cache = GGC_NEW (cp_token_cache);
752 cache->first = first;
753 cache->last = last;
754 return cache;
755 }
756
757 \f
758 /* Decl-specifiers. */
759
760 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
761
762 static void
763 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
764 {
765 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
766 }
767
768 /* Declarators. */
769
770 /* Nothing other than the parser should be creating declarators;
771 declarators are a semi-syntactic representation of C++ entities.
772 Other parts of the front end that need to create entities (like
773 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
774
775 static cp_declarator *make_call_declarator
776 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
777 static cp_declarator *make_array_declarator
778 (cp_declarator *, tree);
779 static cp_declarator *make_pointer_declarator
780 (cp_cv_quals, cp_declarator *);
781 static cp_declarator *make_reference_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_parameter_declarator *make_parameter_declarator
784 (cp_decl_specifier_seq *, cp_declarator *, tree);
785 static cp_declarator *make_ptrmem_declarator
786 (cp_cv_quals, tree, cp_declarator *);
787
788 /* An erroneous declarator. */
789 static cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792 allocated. */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool. */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800 return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND. Clear fields that are
804 common to all declarators. */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809 cp_declarator *declarator;
810
811 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812 declarator->kind = kind;
813 declarator->attributes = NULL_TREE;
814 declarator->declarator = NULL;
815
816 return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier. If
820 QUALIFYING_SCOPE is non-NULL, the identifier is
821 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
822 UNQUALIFIED_NAME. SFK indicates the kind of special function this
823 is, if any. */
824
825 static cp_declarator *
826 make_id_declarator (tree qualifying_scope, tree unqualified_name,
827 special_function_kind sfk)
828 {
829 cp_declarator *declarator;
830
831 /* It is valid to write:
832
833 class C { void f(); };
834 typedef C D;
835 void D::f();
836
837 The standard is not clear about whether `typedef const C D' is
838 legal; as of 2002-09-15 the committee is considering that
839 question. EDG 3.0 allows that syntax. Therefore, we do as
840 well. */
841 if (qualifying_scope && TYPE_P (qualifying_scope))
842 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
843
844 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
845 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
846 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
847
848 declarator = make_declarator (cdk_id);
849 declarator->u.id.qualifying_scope = qualifying_scope;
850 declarator->u.id.unqualified_name = unqualified_name;
851 declarator->u.id.sfk = sfk;
852
853 return declarator;
854 }
855
856 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
857 of modifiers such as const or volatile to apply to the pointer
858 type, represented as identifiers. */
859
860 cp_declarator *
861 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
862 {
863 cp_declarator *declarator;
864
865 declarator = make_declarator (cdk_pointer);
866 declarator->declarator = target;
867 declarator->u.pointer.qualifiers = cv_qualifiers;
868 declarator->u.pointer.class_type = NULL_TREE;
869
870 return declarator;
871 }
872
873 /* Like make_pointer_declarator -- but for references. */
874
875 cp_declarator *
876 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
877 {
878 cp_declarator *declarator;
879
880 declarator = make_declarator (cdk_reference);
881 declarator->declarator = target;
882 declarator->u.pointer.qualifiers = cv_qualifiers;
883 declarator->u.pointer.class_type = NULL_TREE;
884
885 return declarator;
886 }
887
888 /* Like make_pointer_declarator -- but for a pointer to a non-static
889 member of CLASS_TYPE. */
890
891 cp_declarator *
892 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
893 cp_declarator *pointee)
894 {
895 cp_declarator *declarator;
896
897 declarator = make_declarator (cdk_ptrmem);
898 declarator->declarator = pointee;
899 declarator->u.pointer.qualifiers = cv_qualifiers;
900 declarator->u.pointer.class_type = class_type;
901
902 return declarator;
903 }
904
905 /* Make a declarator for the function given by TARGET, with the
906 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
907 "const"-qualified member function. The EXCEPTION_SPECIFICATION
908 indicates what exceptions can be thrown. */
909
910 cp_declarator *
911 make_call_declarator (cp_declarator *target,
912 cp_parameter_declarator *parms,
913 cp_cv_quals cv_qualifiers,
914 tree exception_specification)
915 {
916 cp_declarator *declarator;
917
918 declarator = make_declarator (cdk_function);
919 declarator->declarator = target;
920 declarator->u.function.parameters = parms;
921 declarator->u.function.qualifiers = cv_qualifiers;
922 declarator->u.function.exception_specification = exception_specification;
923
924 return declarator;
925 }
926
927 /* Make a declarator for an array of BOUNDS elements, each of which is
928 defined by ELEMENT. */
929
930 cp_declarator *
931 make_array_declarator (cp_declarator *element, tree bounds)
932 {
933 cp_declarator *declarator;
934
935 declarator = make_declarator (cdk_array);
936 declarator->declarator = element;
937 declarator->u.array.bounds = bounds;
938
939 return declarator;
940 }
941
942 cp_parameter_declarator *no_parameters;
943
944 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
945 DECLARATOR and DEFAULT_ARGUMENT. */
946
947 cp_parameter_declarator *
948 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
949 cp_declarator *declarator,
950 tree default_argument)
951 {
952 cp_parameter_declarator *parameter;
953
954 parameter = ((cp_parameter_declarator *)
955 alloc_declarator (sizeof (cp_parameter_declarator)));
956 parameter->next = NULL;
957 if (decl_specifiers)
958 parameter->decl_specifiers = *decl_specifiers;
959 else
960 clear_decl_specs (&parameter->decl_specifiers);
961 parameter->declarator = declarator;
962 parameter->default_argument = default_argument;
963 parameter->ellipsis_p = false;
964
965 return parameter;
966 }
967
968 /* Returns true iff DECLARATOR is a declaration for a function. */
969
970 static bool
971 function_declarator_p (const cp_declarator *declarator)
972 {
973 while (declarator)
974 {
975 if (declarator->kind == cdk_function
976 && declarator->declarator->kind == cdk_id)
977 return true;
978 if (declarator->kind == cdk_id
979 || declarator->kind == cdk_error)
980 return false;
981 declarator = declarator->declarator;
982 }
983 return false;
984 }
985
986 /* The parser. */
987
988 /* Overview
989 --------
990
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
998
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1001 code processed.
1002
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1011
1012 Methodology
1013 -----------
1014
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1031
1032 Future Improvements
1033 -------------------
1034
1035 The performance of the parser could probably be improved substantially.
1036 We could often eliminate the need to parse tentatively by looking ahead
1037 a little bit. In some places, this approach might not entirely eliminate
1038 the need to parse tentatively, but it might still speed up the average
1039 case. */
1040
1041 /* Flags that are passed to some parsing functions. These values can
1042 be bitwise-ored together. */
1043
1044 typedef enum cp_parser_flags
1045 {
1046 /* No flags. */
1047 CP_PARSER_FLAGS_NONE = 0x0,
1048 /* The construct is optional. If it is not present, then no error
1049 should be issued. */
1050 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1051 /* When parsing a type-specifier, do not allow user-defined types. */
1052 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1053 } cp_parser_flags;
1054
1055 /* The different kinds of declarators we want to parse. */
1056
1057 typedef enum cp_parser_declarator_kind
1058 {
1059 /* We want an abstract declarator. */
1060 CP_PARSER_DECLARATOR_ABSTRACT,
1061 /* We want a named declarator. */
1062 CP_PARSER_DECLARATOR_NAMED,
1063 /* We don't mind, but the name must be an unqualified-id. */
1064 CP_PARSER_DECLARATOR_EITHER
1065 } cp_parser_declarator_kind;
1066
1067 /* The precedence values used to parse binary expressions. The minimum value
1068 of PREC must be 1, because zero is reserved to quickly discriminate
1069 binary operators from other tokens. */
1070
1071 enum cp_parser_prec
1072 {
1073 PREC_NOT_OPERATOR,
1074 PREC_LOGICAL_OR_EXPRESSION,
1075 PREC_LOGICAL_AND_EXPRESSION,
1076 PREC_INCLUSIVE_OR_EXPRESSION,
1077 PREC_EXCLUSIVE_OR_EXPRESSION,
1078 PREC_AND_EXPRESSION,
1079 PREC_EQUALITY_EXPRESSION,
1080 PREC_RELATIONAL_EXPRESSION,
1081 PREC_SHIFT_EXPRESSION,
1082 PREC_ADDITIVE_EXPRESSION,
1083 PREC_MULTIPLICATIVE_EXPRESSION,
1084 PREC_PM_EXPRESSION,
1085 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1086 };
1087
1088 /* A mapping from a token type to a corresponding tree node type, with a
1089 precedence value. */
1090
1091 typedef struct cp_parser_binary_operations_map_node
1092 {
1093 /* The token type. */
1094 enum cpp_ttype token_type;
1095 /* The corresponding tree code. */
1096 enum tree_code tree_type;
1097 /* The precedence of this operator. */
1098 enum cp_parser_prec prec;
1099 } cp_parser_binary_operations_map_node;
1100
1101 /* The status of a tentative parse. */
1102
1103 typedef enum cp_parser_status_kind
1104 {
1105 /* No errors have occurred. */
1106 CP_PARSER_STATUS_KIND_NO_ERROR,
1107 /* An error has occurred. */
1108 CP_PARSER_STATUS_KIND_ERROR,
1109 /* We are committed to this tentative parse, whether or not an error
1110 has occurred. */
1111 CP_PARSER_STATUS_KIND_COMMITTED
1112 } cp_parser_status_kind;
1113
1114 typedef struct cp_parser_expression_stack_entry
1115 {
1116 tree lhs;
1117 enum tree_code tree_type;
1118 int prec;
1119 } cp_parser_expression_stack_entry;
1120
1121 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1122 entries because precedence levels on the stack are monotonically
1123 increasing. */
1124 typedef struct cp_parser_expression_stack_entry
1125 cp_parser_expression_stack[NUM_PREC_VALUES];
1126
1127 /* Context that is saved and restored when parsing tentatively. */
1128 typedef struct cp_parser_context GTY (())
1129 {
1130 /* If this is a tentative parsing context, the status of the
1131 tentative parse. */
1132 enum cp_parser_status_kind status;
1133 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1134 that are looked up in this context must be looked up both in the
1135 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1136 the context of the containing expression. */
1137 tree object_type;
1138
1139 /* The next parsing context in the stack. */
1140 struct cp_parser_context *next;
1141 } cp_parser_context;
1142
1143 /* Prototypes. */
1144
1145 /* Constructors and destructors. */
1146
1147 static cp_parser_context *cp_parser_context_new
1148 (cp_parser_context *);
1149
1150 /* Class variables. */
1151
1152 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1153
1154 /* The operator-precedence table used by cp_parser_binary_expression.
1155 Transformed into an associative array (binops_by_token) by
1156 cp_parser_new. */
1157
1158 static const cp_parser_binary_operations_map_node binops[] = {
1159 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1160 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1161
1162 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1163 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1164 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1165
1166 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1167 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1168
1169 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1170 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1171
1172 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1173 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1174 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1175 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1176
1177 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1178 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1179
1180 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1181
1182 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1183
1184 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1185
1186 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1187
1188 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1189 };
1190
1191 /* The same as binops, but initialized by cp_parser_new so that
1192 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1193 for speed. */
1194 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1195
1196 /* Constructors and destructors. */
1197
1198 /* Construct a new context. The context below this one on the stack
1199 is given by NEXT. */
1200
1201 static cp_parser_context *
1202 cp_parser_context_new (cp_parser_context* next)
1203 {
1204 cp_parser_context *context;
1205
1206 /* Allocate the storage. */
1207 if (cp_parser_context_free_list != NULL)
1208 {
1209 /* Pull the first entry from the free list. */
1210 context = cp_parser_context_free_list;
1211 cp_parser_context_free_list = context->next;
1212 memset (context, 0, sizeof (*context));
1213 }
1214 else
1215 context = GGC_CNEW (cp_parser_context);
1216
1217 /* No errors have occurred yet in this context. */
1218 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1219 /* If this is not the bottomost context, copy information that we
1220 need from the previous context. */
1221 if (next)
1222 {
1223 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1224 expression, then we are parsing one in this context, too. */
1225 context->object_type = next->object_type;
1226 /* Thread the stack. */
1227 context->next = next;
1228 }
1229
1230 return context;
1231 }
1232
1233 /* The cp_parser structure represents the C++ parser. */
1234
1235 typedef struct cp_parser GTY(())
1236 {
1237 /* The lexer from which we are obtaining tokens. */
1238 cp_lexer *lexer;
1239
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look. It can
1244 also be ERROR_MARK, when we've parsed a bogus scope.
1245
1246 This value is not cleared automatically after a name is looked
1247 up, so we must be careful to clear it before starting a new look
1248 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1249 will look up `Z' in the scope of `X', rather than the current
1250 scope.) Unfortunately, it is difficult to tell when name lookup
1251 is complete, because we sometimes peek at a token, look it up,
1252 and then decide not to consume it. */
1253 tree scope;
1254
1255 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1256 last lookup took place. OBJECT_SCOPE is used if an expression
1257 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1258 respectively. QUALIFYING_SCOPE is used for an expression of the
1259 form "X::Y"; it refers to X. */
1260 tree object_scope;
1261 tree qualifying_scope;
1262
1263 /* A stack of parsing contexts. All but the bottom entry on the
1264 stack will be tentative contexts.
1265
1266 We parse tentatively in order to determine which construct is in
1267 use in some situations. For example, in order to determine
1268 whether a statement is an expression-statement or a
1269 declaration-statement we parse it tentatively as a
1270 declaration-statement. If that fails, we then reparse the same
1271 token stream as an expression-statement. */
1272 cp_parser_context *context;
1273
1274 /* True if we are parsing GNU C++. If this flag is not set, then
1275 GNU extensions are not recognized. */
1276 bool allow_gnu_extensions_p;
1277
1278 /* TRUE if the `>' token should be interpreted as the greater-than
1279 operator. FALSE if it is the end of a template-id or
1280 template-parameter-list. */
1281 bool greater_than_is_operator_p;
1282
1283 /* TRUE if default arguments are allowed within a parameter list
1284 that starts at this point. FALSE if only a gnu extension makes
1285 them permissible. */
1286 bool default_arg_ok_p;
1287
1288 /* TRUE if we are parsing an integral constant-expression. See
1289 [expr.const] for a precise definition. */
1290 bool integral_constant_expression_p;
1291
1292 /* TRUE if we are parsing an integral constant-expression -- but a
1293 non-constant expression should be permitted as well. This flag
1294 is used when parsing an array bound so that GNU variable-length
1295 arrays are tolerated. */
1296 bool allow_non_integral_constant_expression_p;
1297
1298 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1299 been seen that makes the expression non-constant. */
1300 bool non_integral_constant_expression_p;
1301
1302 /* TRUE if local variable names and `this' are forbidden in the
1303 current context. */
1304 bool local_variables_forbidden_p;
1305
1306 /* TRUE if the declaration we are parsing is part of a
1307 linkage-specification of the form `extern string-literal
1308 declaration'. */
1309 bool in_unbraced_linkage_specification_p;
1310
1311 /* TRUE if we are presently parsing a declarator, after the
1312 direct-declarator. */
1313 bool in_declarator_p;
1314
1315 /* TRUE if we are presently parsing a template-argument-list. */
1316 bool in_template_argument_list_p;
1317
1318 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1319 to IN_OMP_BLOCK if parsing OpenMP structured block and
1320 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1321 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1322 iteration-statement, OpenMP block or loop within that switch. */
1323 #define IN_SWITCH_STMT 1
1324 #define IN_ITERATION_STMT 2
1325 #define IN_OMP_BLOCK 4
1326 #define IN_OMP_FOR 8
1327 unsigned char in_statement;
1328
1329 /* TRUE if we are presently parsing the body of a switch statement.
1330 Note that this doesn't quite overlap with in_statement above.
1331 The difference relates to giving the right sets of error messages:
1332 "case not in switch" vs "break statement used with OpenMP...". */
1333 bool in_switch_statement_p;
1334
1335 /* TRUE if we are parsing a type-id in an expression context. In
1336 such a situation, both "type (expr)" and "type (type)" are valid
1337 alternatives. */
1338 bool in_type_id_in_expr_p;
1339
1340 /* TRUE if we are currently in a header file where declarations are
1341 implicitly extern "C". */
1342 bool implicit_extern_c;
1343
1344 /* TRUE if strings in expressions should be translated to the execution
1345 character set. */
1346 bool translate_strings_p;
1347
1348 /* If non-NULL, then we are parsing a construct where new type
1349 definitions are not permitted. The string stored here will be
1350 issued as an error message if a type is defined. */
1351 const char *type_definition_forbidden_message;
1352
1353 /* A list of lists. The outer list is a stack, used for member
1354 functions of local classes. At each level there are two sub-list,
1355 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1356 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1357 TREE_VALUE's. The functions are chained in reverse declaration
1358 order.
1359
1360 The TREE_PURPOSE sublist contains those functions with default
1361 arguments that need post processing, and the TREE_VALUE sublist
1362 contains those functions with definitions that need post
1363 processing.
1364
1365 These lists can only be processed once the outermost class being
1366 defined is complete. */
1367 tree unparsed_functions_queues;
1368
1369 /* The number of classes whose definitions are currently in
1370 progress. */
1371 unsigned num_classes_being_defined;
1372
1373 /* The number of template parameter lists that apply directly to the
1374 current declaration. */
1375 unsigned num_template_parameter_lists;
1376 } cp_parser;
1377
1378 /* Prototypes. */
1379
1380 /* Constructors and destructors. */
1381
1382 static cp_parser *cp_parser_new
1383 (void);
1384
1385 /* Routines to parse various constructs.
1386
1387 Those that return `tree' will return the error_mark_node (rather
1388 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1389 Sometimes, they will return an ordinary node if error-recovery was
1390 attempted, even though a parse error occurred. So, to check
1391 whether or not a parse error occurred, you should always use
1392 cp_parser_error_occurred. If the construct is optional (indicated
1393 either by an `_opt' in the name of the function that does the
1394 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1395 the construct is not present. */
1396
1397 /* Lexical conventions [gram.lex] */
1398
1399 static tree cp_parser_identifier
1400 (cp_parser *);
1401 static tree cp_parser_string_literal
1402 (cp_parser *, bool, bool);
1403
1404 /* Basic concepts [gram.basic] */
1405
1406 static bool cp_parser_translation_unit
1407 (cp_parser *);
1408
1409 /* Expressions [gram.expr] */
1410
1411 static tree cp_parser_primary_expression
1412 (cp_parser *, bool, bool, bool, cp_id_kind *);
1413 static tree cp_parser_id_expression
1414 (cp_parser *, bool, bool, bool *, bool, bool);
1415 static tree cp_parser_unqualified_id
1416 (cp_parser *, bool, bool, bool, bool);
1417 static tree cp_parser_nested_name_specifier_opt
1418 (cp_parser *, bool, bool, bool, bool);
1419 static tree cp_parser_nested_name_specifier
1420 (cp_parser *, bool, bool, bool, bool);
1421 static tree cp_parser_class_or_namespace_name
1422 (cp_parser *, bool, bool, bool, bool, bool);
1423 static tree cp_parser_postfix_expression
1424 (cp_parser *, bool, bool);
1425 static tree cp_parser_postfix_open_square_expression
1426 (cp_parser *, tree, bool);
1427 static tree cp_parser_postfix_dot_deref_expression
1428 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1429 static tree cp_parser_parenthesized_expression_list
1430 (cp_parser *, bool, bool, bool *);
1431 static void cp_parser_pseudo_destructor_name
1432 (cp_parser *, tree *, tree *);
1433 static tree cp_parser_unary_expression
1434 (cp_parser *, bool, bool);
1435 static enum tree_code cp_parser_unary_operator
1436 (cp_token *);
1437 static tree cp_parser_new_expression
1438 (cp_parser *);
1439 static tree cp_parser_new_placement
1440 (cp_parser *);
1441 static tree cp_parser_new_type_id
1442 (cp_parser *, tree *);
1443 static cp_declarator *cp_parser_new_declarator_opt
1444 (cp_parser *);
1445 static cp_declarator *cp_parser_direct_new_declarator
1446 (cp_parser *);
1447 static tree cp_parser_new_initializer
1448 (cp_parser *);
1449 static tree cp_parser_delete_expression
1450 (cp_parser *);
1451 static tree cp_parser_cast_expression
1452 (cp_parser *, bool, bool);
1453 static tree cp_parser_binary_expression
1454 (cp_parser *, bool);
1455 static tree cp_parser_question_colon_clause
1456 (cp_parser *, tree);
1457 static tree cp_parser_assignment_expression
1458 (cp_parser *, bool);
1459 static enum tree_code cp_parser_assignment_operator_opt
1460 (cp_parser *);
1461 static tree cp_parser_expression
1462 (cp_parser *, bool);
1463 static tree cp_parser_constant_expression
1464 (cp_parser *, bool, bool *);
1465 static tree cp_parser_builtin_offsetof
1466 (cp_parser *);
1467
1468 /* Statements [gram.stmt.stmt] */
1469
1470 static void cp_parser_statement
1471 (cp_parser *, tree, bool);
1472 static void cp_parser_label_for_labeled_statement
1473 (cp_parser *);
1474 static tree cp_parser_expression_statement
1475 (cp_parser *, tree);
1476 static tree cp_parser_compound_statement
1477 (cp_parser *, tree, bool);
1478 static void cp_parser_statement_seq_opt
1479 (cp_parser *, tree);
1480 static tree cp_parser_selection_statement
1481 (cp_parser *);
1482 static tree cp_parser_condition
1483 (cp_parser *);
1484 static tree cp_parser_iteration_statement
1485 (cp_parser *);
1486 static void cp_parser_for_init_statement
1487 (cp_parser *);
1488 static tree cp_parser_jump_statement
1489 (cp_parser *);
1490 static void cp_parser_declaration_statement
1491 (cp_parser *);
1492
1493 static tree cp_parser_implicitly_scoped_statement
1494 (cp_parser *);
1495 static void cp_parser_already_scoped_statement
1496 (cp_parser *);
1497
1498 /* Declarations [gram.dcl.dcl] */
1499
1500 static void cp_parser_declaration_seq_opt
1501 (cp_parser *);
1502 static void cp_parser_declaration
1503 (cp_parser *);
1504 static void cp_parser_block_declaration
1505 (cp_parser *, bool);
1506 static void cp_parser_simple_declaration
1507 (cp_parser *, bool);
1508 static void cp_parser_decl_specifier_seq
1509 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1510 static tree cp_parser_storage_class_specifier_opt
1511 (cp_parser *);
1512 static tree cp_parser_function_specifier_opt
1513 (cp_parser *, cp_decl_specifier_seq *);
1514 static tree cp_parser_type_specifier
1515 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1516 int *, bool *);
1517 static tree cp_parser_simple_type_specifier
1518 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1519 static tree cp_parser_type_name
1520 (cp_parser *);
1521 static tree cp_parser_elaborated_type_specifier
1522 (cp_parser *, bool, bool);
1523 static tree cp_parser_enum_specifier
1524 (cp_parser *);
1525 static void cp_parser_enumerator_list
1526 (cp_parser *, tree);
1527 static void cp_parser_enumerator_definition
1528 (cp_parser *, tree);
1529 static tree cp_parser_namespace_name
1530 (cp_parser *);
1531 static void cp_parser_namespace_definition
1532 (cp_parser *);
1533 static void cp_parser_namespace_body
1534 (cp_parser *);
1535 static tree cp_parser_qualified_namespace_specifier
1536 (cp_parser *);
1537 static void cp_parser_namespace_alias_definition
1538 (cp_parser *);
1539 static bool cp_parser_using_declaration
1540 (cp_parser *, bool);
1541 static void cp_parser_using_directive
1542 (cp_parser *);
1543 static void cp_parser_asm_definition
1544 (cp_parser *);
1545 static void cp_parser_linkage_specification
1546 (cp_parser *);
1547
1548 /* Declarators [gram.dcl.decl] */
1549
1550 static tree cp_parser_init_declarator
1551 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1552 static cp_declarator *cp_parser_declarator
1553 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1554 static cp_declarator *cp_parser_direct_declarator
1555 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1556 static enum tree_code cp_parser_ptr_operator
1557 (cp_parser *, tree *, cp_cv_quals *);
1558 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1559 (cp_parser *);
1560 static tree cp_parser_declarator_id
1561 (cp_parser *, bool);
1562 static tree cp_parser_type_id
1563 (cp_parser *);
1564 static void cp_parser_type_specifier_seq
1565 (cp_parser *, bool, cp_decl_specifier_seq *);
1566 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1567 (cp_parser *);
1568 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1569 (cp_parser *, bool *);
1570 static cp_parameter_declarator *cp_parser_parameter_declaration
1571 (cp_parser *, bool, bool *);
1572 static void cp_parser_function_body
1573 (cp_parser *);
1574 static tree cp_parser_initializer
1575 (cp_parser *, bool *, bool *);
1576 static tree cp_parser_initializer_clause
1577 (cp_parser *, bool *);
1578 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1579 (cp_parser *, bool *);
1580
1581 static bool cp_parser_ctor_initializer_opt_and_function_body
1582 (cp_parser *);
1583
1584 /* Classes [gram.class] */
1585
1586 static tree cp_parser_class_name
1587 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1588 static tree cp_parser_class_specifier
1589 (cp_parser *);
1590 static tree cp_parser_class_head
1591 (cp_parser *, bool *, tree *);
1592 static enum tag_types cp_parser_class_key
1593 (cp_parser *);
1594 static void cp_parser_member_specification_opt
1595 (cp_parser *);
1596 static void cp_parser_member_declaration
1597 (cp_parser *);
1598 static tree cp_parser_pure_specifier
1599 (cp_parser *);
1600 static tree cp_parser_constant_initializer
1601 (cp_parser *);
1602
1603 /* Derived classes [gram.class.derived] */
1604
1605 static tree cp_parser_base_clause
1606 (cp_parser *);
1607 static tree cp_parser_base_specifier
1608 (cp_parser *);
1609
1610 /* Special member functions [gram.special] */
1611
1612 static tree cp_parser_conversion_function_id
1613 (cp_parser *);
1614 static tree cp_parser_conversion_type_id
1615 (cp_parser *);
1616 static cp_declarator *cp_parser_conversion_declarator_opt
1617 (cp_parser *);
1618 static bool cp_parser_ctor_initializer_opt
1619 (cp_parser *);
1620 static void cp_parser_mem_initializer_list
1621 (cp_parser *);
1622 static tree cp_parser_mem_initializer
1623 (cp_parser *);
1624 static tree cp_parser_mem_initializer_id
1625 (cp_parser *);
1626
1627 /* Overloading [gram.over] */
1628
1629 static tree cp_parser_operator_function_id
1630 (cp_parser *);
1631 static tree cp_parser_operator
1632 (cp_parser *);
1633
1634 /* Templates [gram.temp] */
1635
1636 static void cp_parser_template_declaration
1637 (cp_parser *, bool);
1638 static tree cp_parser_template_parameter_list
1639 (cp_parser *);
1640 static tree cp_parser_template_parameter
1641 (cp_parser *, bool *);
1642 static tree cp_parser_type_parameter
1643 (cp_parser *);
1644 static tree cp_parser_template_id
1645 (cp_parser *, bool, bool, bool);
1646 static tree cp_parser_template_name
1647 (cp_parser *, bool, bool, bool, bool *);
1648 static tree cp_parser_template_argument_list
1649 (cp_parser *);
1650 static tree cp_parser_template_argument
1651 (cp_parser *);
1652 static void cp_parser_explicit_instantiation
1653 (cp_parser *);
1654 static void cp_parser_explicit_specialization
1655 (cp_parser *);
1656
1657 /* Exception handling [gram.exception] */
1658
1659 static tree cp_parser_try_block
1660 (cp_parser *);
1661 static bool cp_parser_function_try_block
1662 (cp_parser *);
1663 static void cp_parser_handler_seq
1664 (cp_parser *);
1665 static void cp_parser_handler
1666 (cp_parser *);
1667 static tree cp_parser_exception_declaration
1668 (cp_parser *);
1669 static tree cp_parser_throw_expression
1670 (cp_parser *);
1671 static tree cp_parser_exception_specification_opt
1672 (cp_parser *);
1673 static tree cp_parser_type_id_list
1674 (cp_parser *);
1675
1676 /* GNU Extensions */
1677
1678 static tree cp_parser_asm_specification_opt
1679 (cp_parser *);
1680 static tree cp_parser_asm_operand_list
1681 (cp_parser *);
1682 static tree cp_parser_asm_clobber_list
1683 (cp_parser *);
1684 static tree cp_parser_attributes_opt
1685 (cp_parser *);
1686 static tree cp_parser_attribute_list
1687 (cp_parser *);
1688 static bool cp_parser_extension_opt
1689 (cp_parser *, int *);
1690 static void cp_parser_label_declaration
1691 (cp_parser *);
1692
1693 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1694 static bool cp_parser_pragma
1695 (cp_parser *, enum pragma_context);
1696
1697 /* Objective-C++ Productions */
1698
1699 static tree cp_parser_objc_message_receiver
1700 (cp_parser *);
1701 static tree cp_parser_objc_message_args
1702 (cp_parser *);
1703 static tree cp_parser_objc_message_expression
1704 (cp_parser *);
1705 static tree cp_parser_objc_encode_expression
1706 (cp_parser *);
1707 static tree cp_parser_objc_defs_expression
1708 (cp_parser *);
1709 static tree cp_parser_objc_protocol_expression
1710 (cp_parser *);
1711 static tree cp_parser_objc_selector_expression
1712 (cp_parser *);
1713 static tree cp_parser_objc_expression
1714 (cp_parser *);
1715 static bool cp_parser_objc_selector_p
1716 (enum cpp_ttype);
1717 static tree cp_parser_objc_selector
1718 (cp_parser *);
1719 static tree cp_parser_objc_protocol_refs_opt
1720 (cp_parser *);
1721 static void cp_parser_objc_declaration
1722 (cp_parser *);
1723 static tree cp_parser_objc_statement
1724 (cp_parser *);
1725
1726 /* Utility Routines */
1727
1728 static tree cp_parser_lookup_name
1729 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1730 static tree cp_parser_lookup_name_simple
1731 (cp_parser *, tree);
1732 static tree cp_parser_maybe_treat_template_as_class
1733 (tree, bool);
1734 static bool cp_parser_check_declarator_template_parameters
1735 (cp_parser *, cp_declarator *);
1736 static bool cp_parser_check_template_parameters
1737 (cp_parser *, unsigned);
1738 static tree cp_parser_simple_cast_expression
1739 (cp_parser *);
1740 static tree cp_parser_global_scope_opt
1741 (cp_parser *, bool);
1742 static bool cp_parser_constructor_declarator_p
1743 (cp_parser *, bool);
1744 static tree cp_parser_function_definition_from_specifiers_and_declarator
1745 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1746 static tree cp_parser_function_definition_after_declarator
1747 (cp_parser *, bool);
1748 static void cp_parser_template_declaration_after_export
1749 (cp_parser *, bool);
1750 static void cp_parser_perform_template_parameter_access_checks
1751 (tree);
1752 static tree cp_parser_single_declaration
1753 (cp_parser *, tree, bool, bool *);
1754 static tree cp_parser_functional_cast
1755 (cp_parser *, tree);
1756 static tree cp_parser_save_member_function_body
1757 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1758 static tree cp_parser_enclosed_template_argument_list
1759 (cp_parser *);
1760 static void cp_parser_save_default_args
1761 (cp_parser *, tree);
1762 static void cp_parser_late_parsing_for_member
1763 (cp_parser *, tree);
1764 static void cp_parser_late_parsing_default_args
1765 (cp_parser *, tree);
1766 static tree cp_parser_sizeof_operand
1767 (cp_parser *, enum rid);
1768 static bool cp_parser_declares_only_class_p
1769 (cp_parser *);
1770 static void cp_parser_set_storage_class
1771 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1772 static void cp_parser_set_decl_spec_type
1773 (cp_decl_specifier_seq *, tree, bool);
1774 static bool cp_parser_friend_p
1775 (const cp_decl_specifier_seq *);
1776 static cp_token *cp_parser_require
1777 (cp_parser *, enum cpp_ttype, const char *);
1778 static cp_token *cp_parser_require_keyword
1779 (cp_parser *, enum rid, const char *);
1780 static bool cp_parser_token_starts_function_definition_p
1781 (cp_token *);
1782 static bool cp_parser_next_token_starts_class_definition_p
1783 (cp_parser *);
1784 static bool cp_parser_next_token_ends_template_argument_p
1785 (cp_parser *);
1786 static bool cp_parser_nth_token_starts_template_argument_list_p
1787 (cp_parser *, size_t);
1788 static enum tag_types cp_parser_token_is_class_key
1789 (cp_token *);
1790 static void cp_parser_check_class_key
1791 (enum tag_types, tree type);
1792 static void cp_parser_check_access_in_redeclaration
1793 (tree type);
1794 static bool cp_parser_optional_template_keyword
1795 (cp_parser *);
1796 static void cp_parser_pre_parsed_nested_name_specifier
1797 (cp_parser *);
1798 static void cp_parser_cache_group
1799 (cp_parser *, enum cpp_ttype, unsigned);
1800 static void cp_parser_parse_tentatively
1801 (cp_parser *);
1802 static void cp_parser_commit_to_tentative_parse
1803 (cp_parser *);
1804 static void cp_parser_abort_tentative_parse
1805 (cp_parser *);
1806 static bool cp_parser_parse_definitely
1807 (cp_parser *);
1808 static inline bool cp_parser_parsing_tentatively
1809 (cp_parser *);
1810 static bool cp_parser_uncommitted_to_tentative_parse_p
1811 (cp_parser *);
1812 static void cp_parser_error
1813 (cp_parser *, const char *);
1814 static void cp_parser_name_lookup_error
1815 (cp_parser *, tree, tree, const char *);
1816 static bool cp_parser_simulate_error
1817 (cp_parser *);
1818 static void cp_parser_check_type_definition
1819 (cp_parser *);
1820 static void cp_parser_check_for_definition_in_return_type
1821 (cp_declarator *, tree);
1822 static void cp_parser_check_for_invalid_template_id
1823 (cp_parser *, tree);
1824 static bool cp_parser_non_integral_constant_expression
1825 (cp_parser *, const char *);
1826 static void cp_parser_diagnose_invalid_type_name
1827 (cp_parser *, tree, tree);
1828 static bool cp_parser_parse_and_diagnose_invalid_type_name
1829 (cp_parser *);
1830 static int cp_parser_skip_to_closing_parenthesis
1831 (cp_parser *, bool, bool, bool);
1832 static void cp_parser_skip_to_end_of_statement
1833 (cp_parser *);
1834 static void cp_parser_consume_semicolon_at_end_of_statement
1835 (cp_parser *);
1836 static void cp_parser_skip_to_end_of_block_or_statement
1837 (cp_parser *);
1838 static void cp_parser_skip_to_closing_brace
1839 (cp_parser *);
1840 static void cp_parser_skip_to_end_of_template_parameter_list
1841 (cp_parser *);
1842 static void cp_parser_skip_to_pragma_eol
1843 (cp_parser*, cp_token *);
1844 static bool cp_parser_error_occurred
1845 (cp_parser *);
1846 static bool cp_parser_allow_gnu_extensions_p
1847 (cp_parser *);
1848 static bool cp_parser_is_string_literal
1849 (cp_token *);
1850 static bool cp_parser_is_keyword
1851 (cp_token *, enum rid);
1852 static tree cp_parser_make_typename_type
1853 (cp_parser *, tree, tree);
1854
1855 /* Returns nonzero if we are parsing tentatively. */
1856
1857 static inline bool
1858 cp_parser_parsing_tentatively (cp_parser* parser)
1859 {
1860 return parser->context->next != NULL;
1861 }
1862
1863 /* Returns nonzero if TOKEN is a string literal. */
1864
1865 static bool
1866 cp_parser_is_string_literal (cp_token* token)
1867 {
1868 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1869 }
1870
1871 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1872
1873 static bool
1874 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1875 {
1876 return token->keyword == keyword;
1877 }
1878
1879 /* If not parsing tentatively, issue a diagnostic of the form
1880 FILE:LINE: MESSAGE before TOKEN
1881 where TOKEN is the next token in the input stream. MESSAGE
1882 (specified by the caller) is usually of the form "expected
1883 OTHER-TOKEN". */
1884
1885 static void
1886 cp_parser_error (cp_parser* parser, const char* message)
1887 {
1888 if (!cp_parser_simulate_error (parser))
1889 {
1890 cp_token *token = cp_lexer_peek_token (parser->lexer);
1891 /* This diagnostic makes more sense if it is tagged to the line
1892 of the token we just peeked at. */
1893 cp_lexer_set_source_position_from_token (token);
1894
1895 if (token->type == CPP_PRAGMA)
1896 {
1897 error ("%<#pragma%> is not allowed here");
1898 cp_parser_skip_to_pragma_eol (parser, token);
1899 return;
1900 }
1901
1902 c_parse_error (message,
1903 /* Because c_parser_error does not understand
1904 CPP_KEYWORD, keywords are treated like
1905 identifiers. */
1906 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1907 token->value);
1908 }
1909 }
1910
1911 /* Issue an error about name-lookup failing. NAME is the
1912 IDENTIFIER_NODE DECL is the result of
1913 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1914 the thing that we hoped to find. */
1915
1916 static void
1917 cp_parser_name_lookup_error (cp_parser* parser,
1918 tree name,
1919 tree decl,
1920 const char* desired)
1921 {
1922 /* If name lookup completely failed, tell the user that NAME was not
1923 declared. */
1924 if (decl == error_mark_node)
1925 {
1926 if (parser->scope && parser->scope != global_namespace)
1927 error ("%<%D::%D%> has not been declared",
1928 parser->scope, name);
1929 else if (parser->scope == global_namespace)
1930 error ("%<::%D%> has not been declared", name);
1931 else if (parser->object_scope
1932 && !CLASS_TYPE_P (parser->object_scope))
1933 error ("request for member %qD in non-class type %qT",
1934 name, parser->object_scope);
1935 else if (parser->object_scope)
1936 error ("%<%T::%D%> has not been declared",
1937 parser->object_scope, name);
1938 else
1939 error ("%qD has not been declared", name);
1940 }
1941 else if (parser->scope && parser->scope != global_namespace)
1942 error ("%<%D::%D%> %s", parser->scope, name, desired);
1943 else if (parser->scope == global_namespace)
1944 error ("%<::%D%> %s", name, desired);
1945 else
1946 error ("%qD %s", name, desired);
1947 }
1948
1949 /* If we are parsing tentatively, remember that an error has occurred
1950 during this tentative parse. Returns true if the error was
1951 simulated; false if a message should be issued by the caller. */
1952
1953 static bool
1954 cp_parser_simulate_error (cp_parser* parser)
1955 {
1956 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1957 {
1958 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1959 return true;
1960 }
1961 return false;
1962 }
1963
1964 /* Check for repeated decl-specifiers. */
1965
1966 static void
1967 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1968 {
1969 cp_decl_spec ds;
1970
1971 for (ds = ds_first; ds != ds_last; ++ds)
1972 {
1973 unsigned count = decl_specs->specs[(int)ds];
1974 if (count < 2)
1975 continue;
1976 /* The "long" specifier is a special case because of "long long". */
1977 if (ds == ds_long)
1978 {
1979 if (count > 2)
1980 error ("%<long long long%> is too long for GCC");
1981 else if (pedantic && !in_system_header && warn_long_long)
1982 pedwarn ("ISO C++ does not support %<long long%>");
1983 }
1984 else if (count > 1)
1985 {
1986 static const char *const decl_spec_names[] = {
1987 "signed",
1988 "unsigned",
1989 "short",
1990 "long",
1991 "const",
1992 "volatile",
1993 "restrict",
1994 "inline",
1995 "virtual",
1996 "explicit",
1997 "friend",
1998 "typedef",
1999 "__complex",
2000 "__thread"
2001 };
2002 error ("duplicate %qs", decl_spec_names[(int)ds]);
2003 }
2004 }
2005 }
2006
2007 /* This function is called when a type is defined. If type
2008 definitions are forbidden at this point, an error message is
2009 issued. */
2010
2011 static void
2012 cp_parser_check_type_definition (cp_parser* parser)
2013 {
2014 /* If types are forbidden here, issue a message. */
2015 if (parser->type_definition_forbidden_message)
2016 /* Use `%s' to print the string in case there are any escape
2017 characters in the message. */
2018 error ("%s", parser->type_definition_forbidden_message);
2019 }
2020
2021 /* This function is called when the DECLARATOR is processed. The TYPE
2022 was a type defined in the decl-specifiers. If it is invalid to
2023 define a type in the decl-specifiers for DECLARATOR, an error is
2024 issued. */
2025
2026 static void
2027 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2028 tree type)
2029 {
2030 /* [dcl.fct] forbids type definitions in return types.
2031 Unfortunately, it's not easy to know whether or not we are
2032 processing a return type until after the fact. */
2033 while (declarator
2034 && (declarator->kind == cdk_pointer
2035 || declarator->kind == cdk_reference
2036 || declarator->kind == cdk_ptrmem))
2037 declarator = declarator->declarator;
2038 if (declarator
2039 && declarator->kind == cdk_function)
2040 {
2041 error ("new types may not be defined in a return type");
2042 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2043 type);
2044 }
2045 }
2046
2047 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2048 "<" in any valid C++ program. If the next token is indeed "<",
2049 issue a message warning the user about what appears to be an
2050 invalid attempt to form a template-id. */
2051
2052 static void
2053 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2054 tree type)
2055 {
2056 cp_token_position start = 0;
2057
2058 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2059 {
2060 if (TYPE_P (type))
2061 error ("%qT is not a template", type);
2062 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2063 error ("%qE is not a template", type);
2064 else
2065 error ("invalid template-id");
2066 /* Remember the location of the invalid "<". */
2067 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2068 start = cp_lexer_token_position (parser->lexer, true);
2069 /* Consume the "<". */
2070 cp_lexer_consume_token (parser->lexer);
2071 /* Parse the template arguments. */
2072 cp_parser_enclosed_template_argument_list (parser);
2073 /* Permanently remove the invalid template arguments so that
2074 this error message is not issued again. */
2075 if (start)
2076 cp_lexer_purge_tokens_after (parser->lexer, start);
2077 }
2078 }
2079
2080 /* If parsing an integral constant-expression, issue an error message
2081 about the fact that THING appeared and return true. Otherwise,
2082 return false. In either case, set
2083 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2084
2085 static bool
2086 cp_parser_non_integral_constant_expression (cp_parser *parser,
2087 const char *thing)
2088 {
2089 parser->non_integral_constant_expression_p = true;
2090 if (parser->integral_constant_expression_p)
2091 {
2092 if (!parser->allow_non_integral_constant_expression_p)
2093 {
2094 error ("%s cannot appear in a constant-expression", thing);
2095 return true;
2096 }
2097 }
2098 return false;
2099 }
2100
2101 /* Emit a diagnostic for an invalid type name. SCOPE is the
2102 qualifying scope (or NULL, if none) for ID. This function commits
2103 to the current active tentative parse, if any. (Otherwise, the
2104 problematic construct might be encountered again later, resulting
2105 in duplicate error messages.) */
2106
2107 static void
2108 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2109 {
2110 tree decl, old_scope;
2111 /* Try to lookup the identifier. */
2112 old_scope = parser->scope;
2113 parser->scope = scope;
2114 decl = cp_parser_lookup_name_simple (parser, id);
2115 parser->scope = old_scope;
2116 /* If the lookup found a template-name, it means that the user forgot
2117 to specify an argument list. Emit a useful error message. */
2118 if (TREE_CODE (decl) == TEMPLATE_DECL)
2119 error ("invalid use of template-name %qE without an argument list", decl);
2120 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2121 error ("invalid use of destructor %qD as a type", id);
2122 else if (TREE_CODE (decl) == TYPE_DECL)
2123 /* Something like 'unsigned A a;' */
2124 error ("invalid combination of multiple type-specifiers");
2125 else if (!parser->scope)
2126 {
2127 /* Issue an error message. */
2128 error ("%qE does not name a type", id);
2129 /* If we're in a template class, it's possible that the user was
2130 referring to a type from a base class. For example:
2131
2132 template <typename T> struct A { typedef T X; };
2133 template <typename T> struct B : public A<T> { X x; };
2134
2135 The user should have said "typename A<T>::X". */
2136 if (processing_template_decl && current_class_type
2137 && TYPE_BINFO (current_class_type))
2138 {
2139 tree b;
2140
2141 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2142 b;
2143 b = TREE_CHAIN (b))
2144 {
2145 tree base_type = BINFO_TYPE (b);
2146 if (CLASS_TYPE_P (base_type)
2147 && dependent_type_p (base_type))
2148 {
2149 tree field;
2150 /* Go from a particular instantiation of the
2151 template (which will have an empty TYPE_FIELDs),
2152 to the main version. */
2153 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2154 for (field = TYPE_FIELDS (base_type);
2155 field;
2156 field = TREE_CHAIN (field))
2157 if (TREE_CODE (field) == TYPE_DECL
2158 && DECL_NAME (field) == id)
2159 {
2160 inform ("(perhaps %<typename %T::%E%> was intended)",
2161 BINFO_TYPE (b), id);
2162 break;
2163 }
2164 if (field)
2165 break;
2166 }
2167 }
2168 }
2169 }
2170 /* Here we diagnose qualified-ids where the scope is actually correct,
2171 but the identifier does not resolve to a valid type name. */
2172 else if (parser->scope != error_mark_node)
2173 {
2174 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2175 error ("%qE in namespace %qE does not name a type",
2176 id, parser->scope);
2177 else if (TYPE_P (parser->scope))
2178 error ("%qE in class %qT does not name a type", id, parser->scope);
2179 else
2180 gcc_unreachable ();
2181 }
2182 cp_parser_commit_to_tentative_parse (parser);
2183 }
2184
2185 /* Check for a common situation where a type-name should be present,
2186 but is not, and issue a sensible error message. Returns true if an
2187 invalid type-name was detected.
2188
2189 The situation handled by this function are variable declarations of the
2190 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2191 Usually, `ID' should name a type, but if we got here it means that it
2192 does not. We try to emit the best possible error message depending on
2193 how exactly the id-expression looks like. */
2194
2195 static bool
2196 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2197 {
2198 tree id;
2199
2200 cp_parser_parse_tentatively (parser);
2201 id = cp_parser_id_expression (parser,
2202 /*template_keyword_p=*/false,
2203 /*check_dependency_p=*/true,
2204 /*template_p=*/NULL,
2205 /*declarator_p=*/true,
2206 /*optional_p=*/false);
2207 /* After the id-expression, there should be a plain identifier,
2208 otherwise this is not a simple variable declaration. Also, if
2209 the scope is dependent, we cannot do much. */
2210 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2211 || (parser->scope && TYPE_P (parser->scope)
2212 && dependent_type_p (parser->scope)))
2213 {
2214 cp_parser_abort_tentative_parse (parser);
2215 return false;
2216 }
2217 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2218 return false;
2219
2220 /* Emit a diagnostic for the invalid type. */
2221 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2222 /* Skip to the end of the declaration; there's no point in
2223 trying to process it. */
2224 cp_parser_skip_to_end_of_block_or_statement (parser);
2225 return true;
2226 }
2227
2228 /* Consume tokens up to, and including, the next non-nested closing `)'.
2229 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2230 are doing error recovery. Returns -1 if OR_COMMA is true and we
2231 found an unnested comma. */
2232
2233 static int
2234 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2235 bool recovering,
2236 bool or_comma,
2237 bool consume_paren)
2238 {
2239 unsigned paren_depth = 0;
2240 unsigned brace_depth = 0;
2241
2242 if (recovering && !or_comma
2243 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2244 return 0;
2245
2246 while (true)
2247 {
2248 cp_token * token = cp_lexer_peek_token (parser->lexer);
2249
2250 switch (token->type)
2251 {
2252 case CPP_EOF:
2253 case CPP_PRAGMA_EOL:
2254 /* If we've run out of tokens, then there is no closing `)'. */
2255 return 0;
2256
2257 case CPP_SEMICOLON:
2258 /* This matches the processing in skip_to_end_of_statement. */
2259 if (!brace_depth)
2260 return 0;
2261 break;
2262
2263 case CPP_OPEN_BRACE:
2264 ++brace_depth;
2265 break;
2266 case CPP_CLOSE_BRACE:
2267 if (!brace_depth--)
2268 return 0;
2269 break;
2270
2271 case CPP_COMMA:
2272 if (recovering && or_comma && !brace_depth && !paren_depth)
2273 return -1;
2274 break;
2275
2276 case CPP_OPEN_PAREN:
2277 if (!brace_depth)
2278 ++paren_depth;
2279 break;
2280
2281 case CPP_CLOSE_PAREN:
2282 if (!brace_depth && !paren_depth--)
2283 {
2284 if (consume_paren)
2285 cp_lexer_consume_token (parser->lexer);
2286 return 1;
2287 }
2288 break;
2289
2290 default:
2291 break;
2292 }
2293
2294 /* Consume the token. */
2295 cp_lexer_consume_token (parser->lexer);
2296 }
2297 }
2298
2299 /* Consume tokens until we reach the end of the current statement.
2300 Normally, that will be just before consuming a `;'. However, if a
2301 non-nested `}' comes first, then we stop before consuming that. */
2302
2303 static void
2304 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2305 {
2306 unsigned nesting_depth = 0;
2307
2308 while (true)
2309 {
2310 cp_token *token = cp_lexer_peek_token (parser->lexer);
2311
2312 switch (token->type)
2313 {
2314 case CPP_EOF:
2315 case CPP_PRAGMA_EOL:
2316 /* If we've run out of tokens, stop. */
2317 return;
2318
2319 case CPP_SEMICOLON:
2320 /* If the next token is a `;', we have reached the end of the
2321 statement. */
2322 if (!nesting_depth)
2323 return;
2324 break;
2325
2326 case CPP_CLOSE_BRACE:
2327 /* If this is a non-nested '}', stop before consuming it.
2328 That way, when confronted with something like:
2329
2330 { 3 + }
2331
2332 we stop before consuming the closing '}', even though we
2333 have not yet reached a `;'. */
2334 if (nesting_depth == 0)
2335 return;
2336
2337 /* If it is the closing '}' for a block that we have
2338 scanned, stop -- but only after consuming the token.
2339 That way given:
2340
2341 void f g () { ... }
2342 typedef int I;
2343
2344 we will stop after the body of the erroneously declared
2345 function, but before consuming the following `typedef'
2346 declaration. */
2347 if (--nesting_depth == 0)
2348 {
2349 cp_lexer_consume_token (parser->lexer);
2350 return;
2351 }
2352
2353 case CPP_OPEN_BRACE:
2354 ++nesting_depth;
2355 break;
2356
2357 default:
2358 break;
2359 }
2360
2361 /* Consume the token. */
2362 cp_lexer_consume_token (parser->lexer);
2363 }
2364 }
2365
2366 /* This function is called at the end of a statement or declaration.
2367 If the next token is a semicolon, it is consumed; otherwise, error
2368 recovery is attempted. */
2369
2370 static void
2371 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2372 {
2373 /* Look for the trailing `;'. */
2374 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2375 {
2376 /* If there is additional (erroneous) input, skip to the end of
2377 the statement. */
2378 cp_parser_skip_to_end_of_statement (parser);
2379 /* If the next token is now a `;', consume it. */
2380 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2381 cp_lexer_consume_token (parser->lexer);
2382 }
2383 }
2384
2385 /* Skip tokens until we have consumed an entire block, or until we
2386 have consumed a non-nested `;'. */
2387
2388 static void
2389 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2390 {
2391 int nesting_depth = 0;
2392
2393 while (nesting_depth >= 0)
2394 {
2395 cp_token *token = cp_lexer_peek_token (parser->lexer);
2396
2397 switch (token->type)
2398 {
2399 case CPP_EOF:
2400 case CPP_PRAGMA_EOL:
2401 /* If we've run out of tokens, stop. */
2402 return;
2403
2404 case CPP_SEMICOLON:
2405 /* Stop if this is an unnested ';'. */
2406 if (!nesting_depth)
2407 nesting_depth = -1;
2408 break;
2409
2410 case CPP_CLOSE_BRACE:
2411 /* Stop if this is an unnested '}', or closes the outermost
2412 nesting level. */
2413 nesting_depth--;
2414 if (!nesting_depth)
2415 nesting_depth = -1;
2416 break;
2417
2418 case CPP_OPEN_BRACE:
2419 /* Nest. */
2420 nesting_depth++;
2421 break;
2422
2423 default:
2424 break;
2425 }
2426
2427 /* Consume the token. */
2428 cp_lexer_consume_token (parser->lexer);
2429 }
2430 }
2431
2432 /* Skip tokens until a non-nested closing curly brace is the next
2433 token. */
2434
2435 static void
2436 cp_parser_skip_to_closing_brace (cp_parser *parser)
2437 {
2438 unsigned nesting_depth = 0;
2439
2440 while (true)
2441 {
2442 cp_token *token = cp_lexer_peek_token (parser->lexer);
2443
2444 switch (token->type)
2445 {
2446 case CPP_EOF:
2447 case CPP_PRAGMA_EOL:
2448 /* If we've run out of tokens, stop. */
2449 return;
2450
2451 case CPP_CLOSE_BRACE:
2452 /* If the next token is a non-nested `}', then we have reached
2453 the end of the current block. */
2454 if (nesting_depth-- == 0)
2455 return;
2456 break;
2457
2458 case CPP_OPEN_BRACE:
2459 /* If it the next token is a `{', then we are entering a new
2460 block. Consume the entire block. */
2461 ++nesting_depth;
2462 break;
2463
2464 default:
2465 break;
2466 }
2467
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser->lexer);
2470 }
2471 }
2472
2473 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2474 parameter is the PRAGMA token, allowing us to purge the entire pragma
2475 sequence. */
2476
2477 static void
2478 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2479 {
2480 cp_token *token;
2481
2482 parser->lexer->in_pragma = false;
2483
2484 do
2485 token = cp_lexer_consume_token (parser->lexer);
2486 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2487
2488 /* Ensure that the pragma is not parsed again. */
2489 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2490 }
2491
2492 /* Require pragma end of line, resyncing with it as necessary. The
2493 arguments are as for cp_parser_skip_to_pragma_eol. */
2494
2495 static void
2496 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2497 {
2498 parser->lexer->in_pragma = false;
2499 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2501 }
2502
2503 /* This is a simple wrapper around make_typename_type. When the id is
2504 an unresolved identifier node, we can provide a superior diagnostic
2505 using cp_parser_diagnose_invalid_type_name. */
2506
2507 static tree
2508 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2509 {
2510 tree result;
2511 if (TREE_CODE (id) == IDENTIFIER_NODE)
2512 {
2513 result = make_typename_type (scope, id, typename_type,
2514 /*complain=*/tf_none);
2515 if (result == error_mark_node)
2516 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2517 return result;
2518 }
2519 return make_typename_type (scope, id, typename_type, tf_error);
2520 }
2521
2522
2523 /* Create a new C++ parser. */
2524
2525 static cp_parser *
2526 cp_parser_new (void)
2527 {
2528 cp_parser *parser;
2529 cp_lexer *lexer;
2530 unsigned i;
2531
2532 /* cp_lexer_new_main is called before calling ggc_alloc because
2533 cp_lexer_new_main might load a PCH file. */
2534 lexer = cp_lexer_new_main ();
2535
2536 /* Initialize the binops_by_token so that we can get the tree
2537 directly from the token. */
2538 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2539 binops_by_token[binops[i].token_type] = binops[i];
2540
2541 parser = GGC_CNEW (cp_parser);
2542 parser->lexer = lexer;
2543 parser->context = cp_parser_context_new (NULL);
2544
2545 /* For now, we always accept GNU extensions. */
2546 parser->allow_gnu_extensions_p = 1;
2547
2548 /* The `>' token is a greater-than operator, not the end of a
2549 template-id. */
2550 parser->greater_than_is_operator_p = true;
2551
2552 parser->default_arg_ok_p = true;
2553
2554 /* We are not parsing a constant-expression. */
2555 parser->integral_constant_expression_p = false;
2556 parser->allow_non_integral_constant_expression_p = false;
2557 parser->non_integral_constant_expression_p = false;
2558
2559 /* Local variable names are not forbidden. */
2560 parser->local_variables_forbidden_p = false;
2561
2562 /* We are not processing an `extern "C"' declaration. */
2563 parser->in_unbraced_linkage_specification_p = false;
2564
2565 /* We are not processing a declarator. */
2566 parser->in_declarator_p = false;
2567
2568 /* We are not processing a template-argument-list. */
2569 parser->in_template_argument_list_p = false;
2570
2571 /* We are not in an iteration statement. */
2572 parser->in_statement = 0;
2573
2574 /* We are not in a switch statement. */
2575 parser->in_switch_statement_p = false;
2576
2577 /* We are not parsing a type-id inside an expression. */
2578 parser->in_type_id_in_expr_p = false;
2579
2580 /* Declarations aren't implicitly extern "C". */
2581 parser->implicit_extern_c = false;
2582
2583 /* String literals should be translated to the execution character set. */
2584 parser->translate_strings_p = true;
2585
2586 /* The unparsed function queue is empty. */
2587 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2588
2589 /* There are no classes being defined. */
2590 parser->num_classes_being_defined = 0;
2591
2592 /* No template parameters apply. */
2593 parser->num_template_parameter_lists = 0;
2594
2595 return parser;
2596 }
2597
2598 /* Create a cp_lexer structure which will emit the tokens in CACHE
2599 and push it onto the parser's lexer stack. This is used for delayed
2600 parsing of in-class method bodies and default arguments, and should
2601 not be confused with tentative parsing. */
2602 static void
2603 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2604 {
2605 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2606 lexer->next = parser->lexer;
2607 parser->lexer = lexer;
2608
2609 /* Move the current source position to that of the first token in the
2610 new lexer. */
2611 cp_lexer_set_source_position_from_token (lexer->next_token);
2612 }
2613
2614 /* Pop the top lexer off the parser stack. This is never used for the
2615 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2616 static void
2617 cp_parser_pop_lexer (cp_parser *parser)
2618 {
2619 cp_lexer *lexer = parser->lexer;
2620 parser->lexer = lexer->next;
2621 cp_lexer_destroy (lexer);
2622
2623 /* Put the current source position back where it was before this
2624 lexer was pushed. */
2625 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2626 }
2627
2628 /* Lexical conventions [gram.lex] */
2629
2630 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2631 identifier. */
2632
2633 static tree
2634 cp_parser_identifier (cp_parser* parser)
2635 {
2636 cp_token *token;
2637
2638 /* Look for the identifier. */
2639 token = cp_parser_require (parser, CPP_NAME, "identifier");
2640 /* Return the value. */
2641 return token ? token->value : error_mark_node;
2642 }
2643
2644 /* Parse a sequence of adjacent string constants. Returns a
2645 TREE_STRING representing the combined, nul-terminated string
2646 constant. If TRANSLATE is true, translate the string to the
2647 execution character set. If WIDE_OK is true, a wide string is
2648 invalid here.
2649
2650 C++98 [lex.string] says that if a narrow string literal token is
2651 adjacent to a wide string literal token, the behavior is undefined.
2652 However, C99 6.4.5p4 says that this results in a wide string literal.
2653 We follow C99 here, for consistency with the C front end.
2654
2655 This code is largely lifted from lex_string() in c-lex.c.
2656
2657 FUTURE: ObjC++ will need to handle @-strings here. */
2658 static tree
2659 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2660 {
2661 tree value;
2662 bool wide = false;
2663 size_t count;
2664 struct obstack str_ob;
2665 cpp_string str, istr, *strs;
2666 cp_token *tok;
2667
2668 tok = cp_lexer_peek_token (parser->lexer);
2669 if (!cp_parser_is_string_literal (tok))
2670 {
2671 cp_parser_error (parser, "expected string-literal");
2672 return error_mark_node;
2673 }
2674
2675 /* Try to avoid the overhead of creating and destroying an obstack
2676 for the common case of just one string. */
2677 if (!cp_parser_is_string_literal
2678 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2679 {
2680 cp_lexer_consume_token (parser->lexer);
2681
2682 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2683 str.len = TREE_STRING_LENGTH (tok->value);
2684 count = 1;
2685 if (tok->type == CPP_WSTRING)
2686 wide = true;
2687
2688 strs = &str;
2689 }
2690 else
2691 {
2692 gcc_obstack_init (&str_ob);
2693 count = 0;
2694
2695 do
2696 {
2697 cp_lexer_consume_token (parser->lexer);
2698 count++;
2699 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2700 str.len = TREE_STRING_LENGTH (tok->value);
2701 if (tok->type == CPP_WSTRING)
2702 wide = true;
2703
2704 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2705
2706 tok = cp_lexer_peek_token (parser->lexer);
2707 }
2708 while (cp_parser_is_string_literal (tok));
2709
2710 strs = (cpp_string *) obstack_finish (&str_ob);
2711 }
2712
2713 if (wide && !wide_ok)
2714 {
2715 cp_parser_error (parser, "a wide string is invalid in this context");
2716 wide = false;
2717 }
2718
2719 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2720 (parse_in, strs, count, &istr, wide))
2721 {
2722 value = build_string (istr.len, (char *)istr.text);
2723 free ((void *)istr.text);
2724
2725 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2726 value = fix_string_type (value);
2727 }
2728 else
2729 /* cpp_interpret_string has issued an error. */
2730 value = error_mark_node;
2731
2732 if (count > 1)
2733 obstack_free (&str_ob, 0);
2734
2735 return value;
2736 }
2737
2738
2739 /* Basic concepts [gram.basic] */
2740
2741 /* Parse a translation-unit.
2742
2743 translation-unit:
2744 declaration-seq [opt]
2745
2746 Returns TRUE if all went well. */
2747
2748 static bool
2749 cp_parser_translation_unit (cp_parser* parser)
2750 {
2751 /* The address of the first non-permanent object on the declarator
2752 obstack. */
2753 static void *declarator_obstack_base;
2754
2755 bool success;
2756
2757 /* Create the declarator obstack, if necessary. */
2758 if (!cp_error_declarator)
2759 {
2760 gcc_obstack_init (&declarator_obstack);
2761 /* Create the error declarator. */
2762 cp_error_declarator = make_declarator (cdk_error);
2763 /* Create the empty parameter list. */
2764 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2765 /* Remember where the base of the declarator obstack lies. */
2766 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2767 }
2768
2769 cp_parser_declaration_seq_opt (parser);
2770
2771 /* If there are no tokens left then all went well. */
2772 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2773 {
2774 /* Get rid of the token array; we don't need it any more. */
2775 cp_lexer_destroy (parser->lexer);
2776 parser->lexer = NULL;
2777
2778 /* This file might have been a context that's implicitly extern
2779 "C". If so, pop the lang context. (Only relevant for PCH.) */
2780 if (parser->implicit_extern_c)
2781 {
2782 pop_lang_context ();
2783 parser->implicit_extern_c = false;
2784 }
2785
2786 /* Finish up. */
2787 finish_translation_unit ();
2788
2789 success = true;
2790 }
2791 else
2792 {
2793 cp_parser_error (parser, "expected declaration");
2794 success = false;
2795 }
2796
2797 /* Make sure the declarator obstack was fully cleaned up. */
2798 gcc_assert (obstack_next_free (&declarator_obstack)
2799 == declarator_obstack_base);
2800
2801 /* All went well. */
2802 return success;
2803 }
2804
2805 /* Expressions [gram.expr] */
2806
2807 /* Parse a primary-expression.
2808
2809 primary-expression:
2810 literal
2811 this
2812 ( expression )
2813 id-expression
2814
2815 GNU Extensions:
2816
2817 primary-expression:
2818 ( compound-statement )
2819 __builtin_va_arg ( assignment-expression , type-id )
2820 __builtin_offsetof ( type-id , offsetof-expression )
2821
2822 Objective-C++ Extension:
2823
2824 primary-expression:
2825 objc-expression
2826
2827 literal:
2828 __null
2829
2830 ADDRESS_P is true iff this expression was immediately preceded by
2831 "&" and therefore might denote a pointer-to-member. CAST_P is true
2832 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2833 true iff this expression is a template argument.
2834
2835 Returns a representation of the expression. Upon return, *IDK
2836 indicates what kind of id-expression (if any) was present. */
2837
2838 static tree
2839 cp_parser_primary_expression (cp_parser *parser,
2840 bool address_p,
2841 bool cast_p,
2842 bool template_arg_p,
2843 cp_id_kind *idk)
2844 {
2845 cp_token *token;
2846
2847 /* Assume the primary expression is not an id-expression. */
2848 *idk = CP_ID_KIND_NONE;
2849
2850 /* Peek at the next token. */
2851 token = cp_lexer_peek_token (parser->lexer);
2852 switch (token->type)
2853 {
2854 /* literal:
2855 integer-literal
2856 character-literal
2857 floating-literal
2858 string-literal
2859 boolean-literal */
2860 case CPP_CHAR:
2861 case CPP_WCHAR:
2862 case CPP_NUMBER:
2863 token = cp_lexer_consume_token (parser->lexer);
2864 /* Floating-point literals are only allowed in an integral
2865 constant expression if they are cast to an integral or
2866 enumeration type. */
2867 if (TREE_CODE (token->value) == REAL_CST
2868 && parser->integral_constant_expression_p
2869 && pedantic)
2870 {
2871 /* CAST_P will be set even in invalid code like "int(2.7 +
2872 ...)". Therefore, we have to check that the next token
2873 is sure to end the cast. */
2874 if (cast_p)
2875 {
2876 cp_token *next_token;
2877
2878 next_token = cp_lexer_peek_token (parser->lexer);
2879 if (/* The comma at the end of an
2880 enumerator-definition. */
2881 next_token->type != CPP_COMMA
2882 /* The curly brace at the end of an enum-specifier. */
2883 && next_token->type != CPP_CLOSE_BRACE
2884 /* The end of a statement. */
2885 && next_token->type != CPP_SEMICOLON
2886 /* The end of the cast-expression. */
2887 && next_token->type != CPP_CLOSE_PAREN
2888 /* The end of an array bound. */
2889 && next_token->type != CPP_CLOSE_SQUARE
2890 /* The closing ">" in a template-argument-list. */
2891 && (next_token->type != CPP_GREATER
2892 || parser->greater_than_is_operator_p))
2893 cast_p = false;
2894 }
2895
2896 /* If we are within a cast, then the constraint that the
2897 cast is to an integral or enumeration type will be
2898 checked at that point. If we are not within a cast, then
2899 this code is invalid. */
2900 if (!cast_p)
2901 cp_parser_non_integral_constant_expression
2902 (parser, "floating-point literal");
2903 }
2904 return token->value;
2905
2906 case CPP_STRING:
2907 case CPP_WSTRING:
2908 /* ??? Should wide strings be allowed when parser->translate_strings_p
2909 is false (i.e. in attributes)? If not, we can kill the third
2910 argument to cp_parser_string_literal. */
2911 return cp_parser_string_literal (parser,
2912 parser->translate_strings_p,
2913 true);
2914
2915 case CPP_OPEN_PAREN:
2916 {
2917 tree expr;
2918 bool saved_greater_than_is_operator_p;
2919
2920 /* Consume the `('. */
2921 cp_lexer_consume_token (parser->lexer);
2922 /* Within a parenthesized expression, a `>' token is always
2923 the greater-than operator. */
2924 saved_greater_than_is_operator_p
2925 = parser->greater_than_is_operator_p;
2926 parser->greater_than_is_operator_p = true;
2927 /* If we see `( { ' then we are looking at the beginning of
2928 a GNU statement-expression. */
2929 if (cp_parser_allow_gnu_extensions_p (parser)
2930 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2931 {
2932 /* Statement-expressions are not allowed by the standard. */
2933 if (pedantic)
2934 pedwarn ("ISO C++ forbids braced-groups within expressions");
2935
2936 /* And they're not allowed outside of a function-body; you
2937 cannot, for example, write:
2938
2939 int i = ({ int j = 3; j + 1; });
2940
2941 at class or namespace scope. */
2942 if (!at_function_scope_p ())
2943 error ("statement-expressions are allowed only inside functions");
2944 /* Start the statement-expression. */
2945 expr = begin_stmt_expr ();
2946 /* Parse the compound-statement. */
2947 cp_parser_compound_statement (parser, expr, false);
2948 /* Finish up. */
2949 expr = finish_stmt_expr (expr, false);
2950 }
2951 else
2952 {
2953 /* Parse the parenthesized expression. */
2954 expr = cp_parser_expression (parser, cast_p);
2955 /* Let the front end know that this expression was
2956 enclosed in parentheses. This matters in case, for
2957 example, the expression is of the form `A::B', since
2958 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2959 not. */
2960 finish_parenthesized_expr (expr);
2961 }
2962 /* The `>' token might be the end of a template-id or
2963 template-parameter-list now. */
2964 parser->greater_than_is_operator_p
2965 = saved_greater_than_is_operator_p;
2966 /* Consume the `)'. */
2967 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2968 cp_parser_skip_to_end_of_statement (parser);
2969
2970 return expr;
2971 }
2972
2973 case CPP_KEYWORD:
2974 switch (token->keyword)
2975 {
2976 /* These two are the boolean literals. */
2977 case RID_TRUE:
2978 cp_lexer_consume_token (parser->lexer);
2979 return boolean_true_node;
2980 case RID_FALSE:
2981 cp_lexer_consume_token (parser->lexer);
2982 return boolean_false_node;
2983
2984 /* The `__null' literal. */
2985 case RID_NULL:
2986 cp_lexer_consume_token (parser->lexer);
2987 return null_node;
2988
2989 /* Recognize the `this' keyword. */
2990 case RID_THIS:
2991 cp_lexer_consume_token (parser->lexer);
2992 if (parser->local_variables_forbidden_p)
2993 {
2994 error ("%<this%> may not be used in this context");
2995 return error_mark_node;
2996 }
2997 /* Pointers cannot appear in constant-expressions. */
2998 if (cp_parser_non_integral_constant_expression (parser,
2999 "`this'"))
3000 return error_mark_node;
3001 return finish_this_expr ();
3002
3003 /* The `operator' keyword can be the beginning of an
3004 id-expression. */
3005 case RID_OPERATOR:
3006 goto id_expression;
3007
3008 case RID_FUNCTION_NAME:
3009 case RID_PRETTY_FUNCTION_NAME:
3010 case RID_C99_FUNCTION_NAME:
3011 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3012 __func__ are the names of variables -- but they are
3013 treated specially. Therefore, they are handled here,
3014 rather than relying on the generic id-expression logic
3015 below. Grammatically, these names are id-expressions.
3016
3017 Consume the token. */
3018 token = cp_lexer_consume_token (parser->lexer);
3019 /* Look up the name. */
3020 return finish_fname (token->value);
3021
3022 case RID_VA_ARG:
3023 {
3024 tree expression;
3025 tree type;
3026
3027 /* The `__builtin_va_arg' construct is used to handle
3028 `va_arg'. Consume the `__builtin_va_arg' token. */
3029 cp_lexer_consume_token (parser->lexer);
3030 /* Look for the opening `('. */
3031 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3032 /* Now, parse the assignment-expression. */
3033 expression = cp_parser_assignment_expression (parser,
3034 /*cast_p=*/false);
3035 /* Look for the `,'. */
3036 cp_parser_require (parser, CPP_COMMA, "`,'");
3037 /* Parse the type-id. */
3038 type = cp_parser_type_id (parser);
3039 /* Look for the closing `)'. */
3040 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3041 /* Using `va_arg' in a constant-expression is not
3042 allowed. */
3043 if (cp_parser_non_integral_constant_expression (parser,
3044 "`va_arg'"))
3045 return error_mark_node;
3046 return build_x_va_arg (expression, type);
3047 }
3048
3049 case RID_OFFSETOF:
3050 return cp_parser_builtin_offsetof (parser);
3051
3052 /* Objective-C++ expressions. */
3053 case RID_AT_ENCODE:
3054 case RID_AT_PROTOCOL:
3055 case RID_AT_SELECTOR:
3056 return cp_parser_objc_expression (parser);
3057
3058 default:
3059 cp_parser_error (parser, "expected primary-expression");
3060 return error_mark_node;
3061 }
3062
3063 /* An id-expression can start with either an identifier, a
3064 `::' as the beginning of a qualified-id, or the "operator"
3065 keyword. */
3066 case CPP_NAME:
3067 case CPP_SCOPE:
3068 case CPP_TEMPLATE_ID:
3069 case CPP_NESTED_NAME_SPECIFIER:
3070 {
3071 tree id_expression;
3072 tree decl;
3073 const char *error_msg;
3074 bool template_p;
3075 bool done;
3076
3077 id_expression:
3078 /* Parse the id-expression. */
3079 id_expression
3080 = cp_parser_id_expression (parser,
3081 /*template_keyword_p=*/false,
3082 /*check_dependency_p=*/true,
3083 &template_p,
3084 /*declarator_p=*/false,
3085 /*optional_p=*/false);
3086 if (id_expression == error_mark_node)
3087 return error_mark_node;
3088 token = cp_lexer_peek_token (parser->lexer);
3089 done = (token->type != CPP_OPEN_SQUARE
3090 && token->type != CPP_OPEN_PAREN
3091 && token->type != CPP_DOT
3092 && token->type != CPP_DEREF
3093 && token->type != CPP_PLUS_PLUS
3094 && token->type != CPP_MINUS_MINUS);
3095 /* If we have a template-id, then no further lookup is
3096 required. If the template-id was for a template-class, we
3097 will sometimes have a TYPE_DECL at this point. */
3098 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3099 || TREE_CODE (id_expression) == TYPE_DECL)
3100 decl = id_expression;
3101 /* Look up the name. */
3102 else
3103 {
3104 tree ambiguous_decls;
3105
3106 decl = cp_parser_lookup_name (parser, id_expression,
3107 none_type,
3108 template_p,
3109 /*is_namespace=*/false,
3110 /*check_dependency=*/true,
3111 &ambiguous_decls);
3112 /* If the lookup was ambiguous, an error will already have
3113 been issued. */
3114 if (ambiguous_decls)
3115 return error_mark_node;
3116
3117 /* In Objective-C++, an instance variable (ivar) may be preferred
3118 to whatever cp_parser_lookup_name() found. */
3119 decl = objc_lookup_ivar (decl, id_expression);
3120
3121 /* If name lookup gives us a SCOPE_REF, then the
3122 qualifying scope was dependent. */
3123 if (TREE_CODE (decl) == SCOPE_REF)
3124 return decl;
3125 /* Check to see if DECL is a local variable in a context
3126 where that is forbidden. */
3127 if (parser->local_variables_forbidden_p
3128 && local_variable_p (decl))
3129 {
3130 /* It might be that we only found DECL because we are
3131 trying to be generous with pre-ISO scoping rules.
3132 For example, consider:
3133
3134 int i;
3135 void g() {
3136 for (int i = 0; i < 10; ++i) {}
3137 extern void f(int j = i);
3138 }
3139
3140 Here, name look up will originally find the out
3141 of scope `i'. We need to issue a warning message,
3142 but then use the global `i'. */
3143 decl = check_for_out_of_scope_variable (decl);
3144 if (local_variable_p (decl))
3145 {
3146 error ("local variable %qD may not appear in this context",
3147 decl);
3148 return error_mark_node;
3149 }
3150 }
3151 }
3152
3153 decl = (finish_id_expression
3154 (id_expression, decl, parser->scope,
3155 idk,
3156 parser->integral_constant_expression_p,
3157 parser->allow_non_integral_constant_expression_p,
3158 &parser->non_integral_constant_expression_p,
3159 template_p, done, address_p,
3160 template_arg_p,
3161 &error_msg));
3162 if (error_msg)
3163 cp_parser_error (parser, error_msg);
3164 return decl;
3165 }
3166
3167 /* Anything else is an error. */
3168 default:
3169 /* ...unless we have an Objective-C++ message or string literal, that is. */
3170 if (c_dialect_objc ()
3171 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3172 return cp_parser_objc_expression (parser);
3173
3174 cp_parser_error (parser, "expected primary-expression");
3175 return error_mark_node;
3176 }
3177 }
3178
3179 /* Parse an id-expression.
3180
3181 id-expression:
3182 unqualified-id
3183 qualified-id
3184
3185 qualified-id:
3186 :: [opt] nested-name-specifier template [opt] unqualified-id
3187 :: identifier
3188 :: operator-function-id
3189 :: template-id
3190
3191 Return a representation of the unqualified portion of the
3192 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3193 a `::' or nested-name-specifier.
3194
3195 Often, if the id-expression was a qualified-id, the caller will
3196 want to make a SCOPE_REF to represent the qualified-id. This
3197 function does not do this in order to avoid wastefully creating
3198 SCOPE_REFs when they are not required.
3199
3200 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3201 `template' keyword.
3202
3203 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3204 uninstantiated templates.
3205
3206 If *TEMPLATE_P is non-NULL, it is set to true iff the
3207 `template' keyword is used to explicitly indicate that the entity
3208 named is a template.
3209
3210 If DECLARATOR_P is true, the id-expression is appearing as part of
3211 a declarator, rather than as part of an expression. */
3212
3213 static tree
3214 cp_parser_id_expression (cp_parser *parser,
3215 bool template_keyword_p,
3216 bool check_dependency_p,
3217 bool *template_p,
3218 bool declarator_p,
3219 bool optional_p)
3220 {
3221 bool global_scope_p;
3222 bool nested_name_specifier_p;
3223
3224 /* Assume the `template' keyword was not used. */
3225 if (template_p)
3226 *template_p = template_keyword_p;
3227
3228 /* Look for the optional `::' operator. */
3229 global_scope_p
3230 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3231 != NULL_TREE);
3232 /* Look for the optional nested-name-specifier. */
3233 nested_name_specifier_p
3234 = (cp_parser_nested_name_specifier_opt (parser,
3235 /*typename_keyword_p=*/false,
3236 check_dependency_p,
3237 /*type_p=*/false,
3238 declarator_p)
3239 != NULL_TREE);
3240 /* If there is a nested-name-specifier, then we are looking at
3241 the first qualified-id production. */
3242 if (nested_name_specifier_p)
3243 {
3244 tree saved_scope;
3245 tree saved_object_scope;
3246 tree saved_qualifying_scope;
3247 tree unqualified_id;
3248 bool is_template;
3249
3250 /* See if the next token is the `template' keyword. */
3251 if (!template_p)
3252 template_p = &is_template;
3253 *template_p = cp_parser_optional_template_keyword (parser);
3254 /* Name lookup we do during the processing of the
3255 unqualified-id might obliterate SCOPE. */
3256 saved_scope = parser->scope;
3257 saved_object_scope = parser->object_scope;
3258 saved_qualifying_scope = parser->qualifying_scope;
3259 /* Process the final unqualified-id. */
3260 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3261 check_dependency_p,
3262 declarator_p,
3263 /*optional_p=*/false);
3264 /* Restore the SAVED_SCOPE for our caller. */
3265 parser->scope = saved_scope;
3266 parser->object_scope = saved_object_scope;
3267 parser->qualifying_scope = saved_qualifying_scope;
3268
3269 return unqualified_id;
3270 }
3271 /* Otherwise, if we are in global scope, then we are looking at one
3272 of the other qualified-id productions. */
3273 else if (global_scope_p)
3274 {
3275 cp_token *token;
3276 tree id;
3277
3278 /* Peek at the next token. */
3279 token = cp_lexer_peek_token (parser->lexer);
3280
3281 /* If it's an identifier, and the next token is not a "<", then
3282 we can avoid the template-id case. This is an optimization
3283 for this common case. */
3284 if (token->type == CPP_NAME
3285 && !cp_parser_nth_token_starts_template_argument_list_p
3286 (parser, 2))
3287 return cp_parser_identifier (parser);
3288
3289 cp_parser_parse_tentatively (parser);
3290 /* Try a template-id. */
3291 id = cp_parser_template_id (parser,
3292 /*template_keyword_p=*/false,
3293 /*check_dependency_p=*/true,
3294 declarator_p);
3295 /* If that worked, we're done. */
3296 if (cp_parser_parse_definitely (parser))
3297 return id;
3298
3299 /* Peek at the next token. (Changes in the token buffer may
3300 have invalidated the pointer obtained above.) */
3301 token = cp_lexer_peek_token (parser->lexer);
3302
3303 switch (token->type)
3304 {
3305 case CPP_NAME:
3306 return cp_parser_identifier (parser);
3307
3308 case CPP_KEYWORD:
3309 if (token->keyword == RID_OPERATOR)
3310 return cp_parser_operator_function_id (parser);
3311 /* Fall through. */
3312
3313 default:
3314 cp_parser_error (parser, "expected id-expression");
3315 return error_mark_node;
3316 }
3317 }
3318 else
3319 return cp_parser_unqualified_id (parser, template_keyword_p,
3320 /*check_dependency_p=*/true,
3321 declarator_p,
3322 optional_p);
3323 }
3324
3325 /* Parse an unqualified-id.
3326
3327 unqualified-id:
3328 identifier
3329 operator-function-id
3330 conversion-function-id
3331 ~ class-name
3332 template-id
3333
3334 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3335 keyword, in a construct like `A::template ...'.
3336
3337 Returns a representation of unqualified-id. For the `identifier'
3338 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3339 production a BIT_NOT_EXPR is returned; the operand of the
3340 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3341 other productions, see the documentation accompanying the
3342 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3343 names are looked up in uninstantiated templates. If DECLARATOR_P
3344 is true, the unqualified-id is appearing as part of a declarator,
3345 rather than as part of an expression. */
3346
3347 static tree
3348 cp_parser_unqualified_id (cp_parser* parser,
3349 bool template_keyword_p,
3350 bool check_dependency_p,
3351 bool declarator_p,
3352 bool optional_p)
3353 {
3354 cp_token *token;
3355
3356 /* Peek at the next token. */
3357 token = cp_lexer_peek_token (parser->lexer);
3358
3359 switch (token->type)
3360 {
3361 case CPP_NAME:
3362 {
3363 tree id;
3364
3365 /* We don't know yet whether or not this will be a
3366 template-id. */
3367 cp_parser_parse_tentatively (parser);
3368 /* Try a template-id. */
3369 id = cp_parser_template_id (parser, template_keyword_p,
3370 check_dependency_p,
3371 declarator_p);
3372 /* If it worked, we're done. */
3373 if (cp_parser_parse_definitely (parser))
3374 return id;
3375 /* Otherwise, it's an ordinary identifier. */
3376 return cp_parser_identifier (parser);
3377 }
3378
3379 case CPP_TEMPLATE_ID:
3380 return cp_parser_template_id (parser, template_keyword_p,
3381 check_dependency_p,
3382 declarator_p);
3383
3384 case CPP_COMPL:
3385 {
3386 tree type_decl;
3387 tree qualifying_scope;
3388 tree object_scope;
3389 tree scope;
3390 bool done;
3391
3392 /* Consume the `~' token. */
3393 cp_lexer_consume_token (parser->lexer);
3394 /* Parse the class-name. The standard, as written, seems to
3395 say that:
3396
3397 template <typename T> struct S { ~S (); };
3398 template <typename T> S<T>::~S() {}
3399
3400 is invalid, since `~' must be followed by a class-name, but
3401 `S<T>' is dependent, and so not known to be a class.
3402 That's not right; we need to look in uninstantiated
3403 templates. A further complication arises from:
3404
3405 template <typename T> void f(T t) {
3406 t.T::~T();
3407 }
3408
3409 Here, it is not possible to look up `T' in the scope of `T'
3410 itself. We must look in both the current scope, and the
3411 scope of the containing complete expression.
3412
3413 Yet another issue is:
3414
3415 struct S {
3416 int S;
3417 ~S();
3418 };
3419
3420 S::~S() {}
3421
3422 The standard does not seem to say that the `S' in `~S'
3423 should refer to the type `S' and not the data member
3424 `S::S'. */
3425
3426 /* DR 244 says that we look up the name after the "~" in the
3427 same scope as we looked up the qualifying name. That idea
3428 isn't fully worked out; it's more complicated than that. */
3429 scope = parser->scope;
3430 object_scope = parser->object_scope;
3431 qualifying_scope = parser->qualifying_scope;
3432
3433 /* Check for invalid scopes. */
3434 if (scope == error_mark_node)
3435 {
3436 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3437 cp_lexer_consume_token (parser->lexer);
3438 return error_mark_node;
3439 }
3440 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3441 {
3442 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3443 error ("scope %qT before %<~%> is not a class-name", scope);
3444 cp_parser_simulate_error (parser);
3445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3446 cp_lexer_consume_token (parser->lexer);
3447 return error_mark_node;
3448 }
3449 gcc_assert (!scope || TYPE_P (scope));
3450
3451 /* If the name is of the form "X::~X" it's OK. */
3452 token = cp_lexer_peek_token (parser->lexer);
3453 if (scope
3454 && token->type == CPP_NAME
3455 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3456 == CPP_OPEN_PAREN)
3457 && constructor_name_p (token->value, scope))
3458 {
3459 cp_lexer_consume_token (parser->lexer);
3460 return build_nt (BIT_NOT_EXPR, scope);
3461 }
3462
3463 /* If there was an explicit qualification (S::~T), first look
3464 in the scope given by the qualification (i.e., S). */
3465 done = false;
3466 type_decl = NULL_TREE;
3467 if (scope)
3468 {
3469 cp_parser_parse_tentatively (parser);
3470 type_decl = cp_parser_class_name (parser,
3471 /*typename_keyword_p=*/false,
3472 /*template_keyword_p=*/false,
3473 none_type,
3474 /*check_dependency=*/false,
3475 /*class_head_p=*/false,
3476 declarator_p);
3477 if (cp_parser_parse_definitely (parser))
3478 done = true;
3479 }
3480 /* In "N::S::~S", look in "N" as well. */
3481 if (!done && scope && qualifying_scope)
3482 {
3483 cp_parser_parse_tentatively (parser);
3484 parser->scope = qualifying_scope;
3485 parser->object_scope = NULL_TREE;
3486 parser->qualifying_scope = NULL_TREE;
3487 type_decl
3488 = cp_parser_class_name (parser,
3489 /*typename_keyword_p=*/false,
3490 /*template_keyword_p=*/false,
3491 none_type,
3492 /*check_dependency=*/false,
3493 /*class_head_p=*/false,
3494 declarator_p);
3495 if (cp_parser_parse_definitely (parser))
3496 done = true;
3497 }
3498 /* In "p->S::~T", look in the scope given by "*p" as well. */
3499 else if (!done && object_scope)
3500 {
3501 cp_parser_parse_tentatively (parser);
3502 parser->scope = object_scope;
3503 parser->object_scope = NULL_TREE;
3504 parser->qualifying_scope = NULL_TREE;
3505 type_decl
3506 = cp_parser_class_name (parser,
3507 /*typename_keyword_p=*/false,
3508 /*template_keyword_p=*/false,
3509 none_type,
3510 /*check_dependency=*/false,
3511 /*class_head_p=*/false,
3512 declarator_p);
3513 if (cp_parser_parse_definitely (parser))
3514 done = true;
3515 }
3516 /* Look in the surrounding context. */
3517 if (!done)
3518 {
3519 parser->scope = NULL_TREE;
3520 parser->object_scope = NULL_TREE;
3521 parser->qualifying_scope = NULL_TREE;
3522 type_decl
3523 = cp_parser_class_name (parser,
3524 /*typename_keyword_p=*/false,
3525 /*template_keyword_p=*/false,
3526 none_type,
3527 /*check_dependency=*/false,
3528 /*class_head_p=*/false,
3529 declarator_p);
3530 }
3531 /* If an error occurred, assume that the name of the
3532 destructor is the same as the name of the qualifying
3533 class. That allows us to keep parsing after running
3534 into ill-formed destructor names. */
3535 if (type_decl == error_mark_node && scope)
3536 return build_nt (BIT_NOT_EXPR, scope);
3537 else if (type_decl == error_mark_node)
3538 return error_mark_node;
3539
3540 /* Check that destructor name and scope match. */
3541 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3542 {
3543 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3544 error ("declaration of %<~%T%> as member of %qT",
3545 type_decl, scope);
3546 cp_parser_simulate_error (parser);
3547 return error_mark_node;
3548 }
3549
3550 /* [class.dtor]
3551
3552 A typedef-name that names a class shall not be used as the
3553 identifier in the declarator for a destructor declaration. */
3554 if (declarator_p
3555 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3556 && !DECL_SELF_REFERENCE_P (type_decl)
3557 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3558 error ("typedef-name %qD used as destructor declarator",
3559 type_decl);
3560
3561 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3562 }
3563
3564 case CPP_KEYWORD:
3565 if (token->keyword == RID_OPERATOR)
3566 {
3567 tree id;
3568
3569 /* This could be a template-id, so we try that first. */
3570 cp_parser_parse_tentatively (parser);
3571 /* Try a template-id. */
3572 id = cp_parser_template_id (parser, template_keyword_p,
3573 /*check_dependency_p=*/true,
3574 declarator_p);
3575 /* If that worked, we're done. */
3576 if (cp_parser_parse_definitely (parser))
3577 return id;
3578 /* We still don't know whether we're looking at an
3579 operator-function-id or a conversion-function-id. */
3580 cp_parser_parse_tentatively (parser);
3581 /* Try an operator-function-id. */
3582 id = cp_parser_operator_function_id (parser);
3583 /* If that didn't work, try a conversion-function-id. */
3584 if (!cp_parser_parse_definitely (parser))
3585 id = cp_parser_conversion_function_id (parser);
3586
3587 return id;
3588 }
3589 /* Fall through. */
3590
3591 default:
3592 if (optional_p)
3593 return NULL_TREE;
3594 cp_parser_error (parser, "expected unqualified-id");
3595 return error_mark_node;
3596 }
3597 }
3598
3599 /* Parse an (optional) nested-name-specifier.
3600
3601 nested-name-specifier:
3602 class-or-namespace-name :: nested-name-specifier [opt]
3603 class-or-namespace-name :: template nested-name-specifier [opt]
3604
3605 PARSER->SCOPE should be set appropriately before this function is
3606 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3607 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3608 in name lookups.
3609
3610 Sets PARSER->SCOPE to the class (TYPE) or namespace
3611 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3612 it unchanged if there is no nested-name-specifier. Returns the new
3613 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3614
3615 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3616 part of a declaration and/or decl-specifier. */
3617
3618 static tree
3619 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3620 bool typename_keyword_p,
3621 bool check_dependency_p,
3622 bool type_p,
3623 bool is_declaration)
3624 {
3625 bool success = false;
3626 cp_token_position start = 0;
3627 cp_token *token;
3628
3629 /* Remember where the nested-name-specifier starts. */
3630 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3631 {
3632 start = cp_lexer_token_position (parser->lexer, false);
3633 push_deferring_access_checks (dk_deferred);
3634 }
3635
3636 while (true)
3637 {
3638 tree new_scope;
3639 tree old_scope;
3640 tree saved_qualifying_scope;
3641 bool template_keyword_p;
3642
3643 /* Spot cases that cannot be the beginning of a
3644 nested-name-specifier. */
3645 token = cp_lexer_peek_token (parser->lexer);
3646
3647 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3648 the already parsed nested-name-specifier. */
3649 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3650 {
3651 /* Grab the nested-name-specifier and continue the loop. */
3652 cp_parser_pre_parsed_nested_name_specifier (parser);
3653 /* If we originally encountered this nested-name-specifier
3654 with IS_DECLARATION set to false, we will not have
3655 resolved TYPENAME_TYPEs, so we must do so here. */
3656 if (is_declaration
3657 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3658 {
3659 new_scope = resolve_typename_type (parser->scope,
3660 /*only_current_p=*/false);
3661 if (new_scope != error_mark_node)
3662 parser->scope = new_scope;
3663 }
3664 success = true;
3665 continue;
3666 }
3667
3668 /* Spot cases that cannot be the beginning of a
3669 nested-name-specifier. On the second and subsequent times
3670 through the loop, we look for the `template' keyword. */
3671 if (success && token->keyword == RID_TEMPLATE)
3672 ;
3673 /* A template-id can start a nested-name-specifier. */
3674 else if (token->type == CPP_TEMPLATE_ID)
3675 ;
3676 else
3677 {
3678 /* If the next token is not an identifier, then it is
3679 definitely not a class-or-namespace-name. */
3680 if (token->type != CPP_NAME)
3681 break;
3682 /* If the following token is neither a `<' (to begin a
3683 template-id), nor a `::', then we are not looking at a
3684 nested-name-specifier. */
3685 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3686 if (token->type != CPP_SCOPE
3687 && !cp_parser_nth_token_starts_template_argument_list_p
3688 (parser, 2))
3689 break;
3690 }
3691
3692 /* The nested-name-specifier is optional, so we parse
3693 tentatively. */
3694 cp_parser_parse_tentatively (parser);
3695
3696 /* Look for the optional `template' keyword, if this isn't the
3697 first time through the loop. */
3698 if (success)
3699 template_keyword_p = cp_parser_optional_template_keyword (parser);
3700 else
3701 template_keyword_p = false;
3702
3703 /* Save the old scope since the name lookup we are about to do
3704 might destroy it. */
3705 old_scope = parser->scope;
3706 saved_qualifying_scope = parser->qualifying_scope;
3707 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3708 look up names in "X<T>::I" in order to determine that "Y" is
3709 a template. So, if we have a typename at this point, we make
3710 an effort to look through it. */
3711 if (is_declaration
3712 && !typename_keyword_p
3713 && parser->scope
3714 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3715 parser->scope = resolve_typename_type (parser->scope,
3716 /*only_current_p=*/false);
3717 /* Parse the qualifying entity. */
3718 new_scope
3719 = cp_parser_class_or_namespace_name (parser,
3720 typename_keyword_p,
3721 template_keyword_p,
3722 check_dependency_p,
3723 type_p,
3724 is_declaration);
3725 /* Look for the `::' token. */
3726 cp_parser_require (parser, CPP_SCOPE, "`::'");
3727
3728 /* If we found what we wanted, we keep going; otherwise, we're
3729 done. */
3730 if (!cp_parser_parse_definitely (parser))
3731 {
3732 bool error_p = false;
3733
3734 /* Restore the OLD_SCOPE since it was valid before the
3735 failed attempt at finding the last
3736 class-or-namespace-name. */
3737 parser->scope = old_scope;
3738 parser->qualifying_scope = saved_qualifying_scope;
3739 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3740 break;
3741 /* If the next token is an identifier, and the one after
3742 that is a `::', then any valid interpretation would have
3743 found a class-or-namespace-name. */
3744 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3745 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3746 == CPP_SCOPE)
3747 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3748 != CPP_COMPL))
3749 {
3750 token = cp_lexer_consume_token (parser->lexer);
3751 if (!error_p)
3752 {
3753 if (!token->ambiguous_p)
3754 {
3755 tree decl;
3756 tree ambiguous_decls;
3757
3758 decl = cp_parser_lookup_name (parser, token->value,
3759 none_type,
3760 /*is_template=*/false,
3761 /*is_namespace=*/false,
3762 /*check_dependency=*/true,
3763 &ambiguous_decls);
3764 if (TREE_CODE (decl) == TEMPLATE_DECL)
3765 error ("%qD used without template parameters", decl);
3766 else if (ambiguous_decls)
3767 {
3768 error ("reference to %qD is ambiguous",
3769 token->value);
3770 print_candidates (ambiguous_decls);
3771 decl = error_mark_node;
3772 }
3773 else
3774 cp_parser_name_lookup_error
3775 (parser, token->value, decl,
3776 "is not a class or namespace");
3777 }
3778 parser->scope = error_mark_node;
3779 error_p = true;
3780 /* Treat this as a successful nested-name-specifier
3781 due to:
3782
3783 [basic.lookup.qual]
3784
3785 If the name found is not a class-name (clause
3786 _class_) or namespace-name (_namespace.def_), the
3787 program is ill-formed. */
3788 success = true;
3789 }
3790 cp_lexer_consume_token (parser->lexer);
3791 }
3792 break;
3793 }
3794 /* We've found one valid nested-name-specifier. */
3795 success = true;
3796 /* Name lookup always gives us a DECL. */
3797 if (TREE_CODE (new_scope) == TYPE_DECL)
3798 new_scope = TREE_TYPE (new_scope);
3799 /* Uses of "template" must be followed by actual templates. */
3800 if (template_keyword_p
3801 && !(CLASS_TYPE_P (new_scope)
3802 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3803 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3804 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3805 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3806 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3807 == TEMPLATE_ID_EXPR)))
3808 pedwarn (TYPE_P (new_scope)
3809 ? "%qT is not a template"
3810 : "%qD is not a template",
3811 new_scope);
3812 /* If it is a class scope, try to complete it; we are about to
3813 be looking up names inside the class. */
3814 if (TYPE_P (new_scope)
3815 /* Since checking types for dependency can be expensive,
3816 avoid doing it if the type is already complete. */
3817 && !COMPLETE_TYPE_P (new_scope)
3818 /* Do not try to complete dependent types. */
3819 && !dependent_type_p (new_scope))
3820 new_scope = complete_type (new_scope);
3821 /* Make sure we look in the right scope the next time through
3822 the loop. */
3823 parser->scope = new_scope;
3824 }
3825
3826 /* If parsing tentatively, replace the sequence of tokens that makes
3827 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3828 token. That way, should we re-parse the token stream, we will
3829 not have to repeat the effort required to do the parse, nor will
3830 we issue duplicate error messages. */
3831 if (success && start)
3832 {
3833 cp_token *token;
3834 tree access_checks;
3835
3836 token = cp_lexer_token_at (parser->lexer, start);
3837 /* Reset the contents of the START token. */
3838 token->type = CPP_NESTED_NAME_SPECIFIER;
3839 /* Retrieve any deferred checks. Do not pop this access checks yet
3840 so the memory will not be reclaimed during token replacing below. */
3841 access_checks = get_deferred_access_checks ();
3842 token->value = build_tree_list (copy_list (access_checks),
3843 parser->scope);
3844 TREE_TYPE (token->value) = parser->qualifying_scope;
3845 token->keyword = RID_MAX;
3846
3847 /* Purge all subsequent tokens. */
3848 cp_lexer_purge_tokens_after (parser->lexer, start);
3849 }
3850
3851 if (start)
3852 pop_to_parent_deferring_access_checks ();
3853
3854 return success ? parser->scope : NULL_TREE;
3855 }
3856
3857 /* Parse a nested-name-specifier. See
3858 cp_parser_nested_name_specifier_opt for details. This function
3859 behaves identically, except that it will an issue an error if no
3860 nested-name-specifier is present. */
3861
3862 static tree
3863 cp_parser_nested_name_specifier (cp_parser *parser,
3864 bool typename_keyword_p,
3865 bool check_dependency_p,
3866 bool type_p,
3867 bool is_declaration)
3868 {
3869 tree scope;
3870
3871 /* Look for the nested-name-specifier. */
3872 scope = cp_parser_nested_name_specifier_opt (parser,
3873 typename_keyword_p,
3874 check_dependency_p,
3875 type_p,
3876 is_declaration);
3877 /* If it was not present, issue an error message. */
3878 if (!scope)
3879 {
3880 cp_parser_error (parser, "expected nested-name-specifier");
3881 parser->scope = NULL_TREE;
3882 }
3883
3884 return scope;
3885 }
3886
3887 /* Parse a class-or-namespace-name.
3888
3889 class-or-namespace-name:
3890 class-name
3891 namespace-name
3892
3893 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3894 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3895 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3896 TYPE_P is TRUE iff the next name should be taken as a class-name,
3897 even the same name is declared to be another entity in the same
3898 scope.
3899
3900 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3901 specified by the class-or-namespace-name. If neither is found the
3902 ERROR_MARK_NODE is returned. */
3903
3904 static tree
3905 cp_parser_class_or_namespace_name (cp_parser *parser,
3906 bool typename_keyword_p,
3907 bool template_keyword_p,
3908 bool check_dependency_p,
3909 bool type_p,
3910 bool is_declaration)
3911 {
3912 tree saved_scope;
3913 tree saved_qualifying_scope;
3914 tree saved_object_scope;
3915 tree scope;
3916 bool only_class_p;
3917
3918 /* Before we try to parse the class-name, we must save away the
3919 current PARSER->SCOPE since cp_parser_class_name will destroy
3920 it. */
3921 saved_scope = parser->scope;
3922 saved_qualifying_scope = parser->qualifying_scope;
3923 saved_object_scope = parser->object_scope;
3924 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3925 there is no need to look for a namespace-name. */
3926 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3927 if (!only_class_p)
3928 cp_parser_parse_tentatively (parser);
3929 scope = cp_parser_class_name (parser,
3930 typename_keyword_p,
3931 template_keyword_p,
3932 type_p ? class_type : none_type,
3933 check_dependency_p,
3934 /*class_head_p=*/false,
3935 is_declaration);
3936 /* If that didn't work, try for a namespace-name. */
3937 if (!only_class_p && !cp_parser_parse_definitely (parser))
3938 {
3939 /* Restore the saved scope. */
3940 parser->scope = saved_scope;
3941 parser->qualifying_scope = saved_qualifying_scope;
3942 parser->object_scope = saved_object_scope;
3943 /* If we are not looking at an identifier followed by the scope
3944 resolution operator, then this is not part of a
3945 nested-name-specifier. (Note that this function is only used
3946 to parse the components of a nested-name-specifier.) */
3947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3948 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3949 return error_mark_node;
3950 scope = cp_parser_namespace_name (parser);
3951 }
3952
3953 return scope;
3954 }
3955
3956 /* Parse a postfix-expression.
3957
3958 postfix-expression:
3959 primary-expression
3960 postfix-expression [ expression ]
3961 postfix-expression ( expression-list [opt] )
3962 simple-type-specifier ( expression-list [opt] )
3963 typename :: [opt] nested-name-specifier identifier
3964 ( expression-list [opt] )
3965 typename :: [opt] nested-name-specifier template [opt] template-id
3966 ( expression-list [opt] )
3967 postfix-expression . template [opt] id-expression
3968 postfix-expression -> template [opt] id-expression
3969 postfix-expression . pseudo-destructor-name
3970 postfix-expression -> pseudo-destructor-name
3971 postfix-expression ++
3972 postfix-expression --
3973 dynamic_cast < type-id > ( expression )
3974 static_cast < type-id > ( expression )
3975 reinterpret_cast < type-id > ( expression )
3976 const_cast < type-id > ( expression )
3977 typeid ( expression )
3978 typeid ( type-id )
3979
3980 GNU Extension:
3981
3982 postfix-expression:
3983 ( type-id ) { initializer-list , [opt] }
3984
3985 This extension is a GNU version of the C99 compound-literal
3986 construct. (The C99 grammar uses `type-name' instead of `type-id',
3987 but they are essentially the same concept.)
3988
3989 If ADDRESS_P is true, the postfix expression is the operand of the
3990 `&' operator. CAST_P is true if this expression is the target of a
3991 cast.
3992
3993 Returns a representation of the expression. */
3994
3995 static tree
3996 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3997 {
3998 cp_token *token;
3999 enum rid keyword;
4000 cp_id_kind idk = CP_ID_KIND_NONE;
4001 tree postfix_expression = NULL_TREE;
4002
4003 /* Peek at the next token. */
4004 token = cp_lexer_peek_token (parser->lexer);
4005 /* Some of the productions are determined by keywords. */
4006 keyword = token->keyword;
4007 switch (keyword)
4008 {
4009 case RID_DYNCAST:
4010 case RID_STATCAST:
4011 case RID_REINTCAST:
4012 case RID_CONSTCAST:
4013 {
4014 tree type;
4015 tree expression;
4016 const char *saved_message;
4017
4018 /* All of these can be handled in the same way from the point
4019 of view of parsing. Begin by consuming the token
4020 identifying the cast. */
4021 cp_lexer_consume_token (parser->lexer);
4022
4023 /* New types cannot be defined in the cast. */
4024 saved_message = parser->type_definition_forbidden_message;
4025 parser->type_definition_forbidden_message
4026 = "types may not be defined in casts";
4027
4028 /* Look for the opening `<'. */
4029 cp_parser_require (parser, CPP_LESS, "`<'");
4030 /* Parse the type to which we are casting. */
4031 type = cp_parser_type_id (parser);
4032 /* Look for the closing `>'. */
4033 cp_parser_require (parser, CPP_GREATER, "`>'");
4034 /* Restore the old message. */
4035 parser->type_definition_forbidden_message = saved_message;
4036
4037 /* And the expression which is being cast. */
4038 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4039 expression = cp_parser_expression (parser, /*cast_p=*/true);
4040 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4041
4042 /* Only type conversions to integral or enumeration types
4043 can be used in constant-expressions. */
4044 if (!cast_valid_in_integral_constant_expression_p (type)
4045 && (cp_parser_non_integral_constant_expression
4046 (parser,
4047 "a cast to a type other than an integral or "
4048 "enumeration type")))
4049 return error_mark_node;
4050
4051 switch (keyword)
4052 {
4053 case RID_DYNCAST:
4054 postfix_expression
4055 = build_dynamic_cast (type, expression);
4056 break;
4057 case RID_STATCAST:
4058 postfix_expression
4059 = build_static_cast (type, expression);
4060 break;
4061 case RID_REINTCAST:
4062 postfix_expression
4063 = build_reinterpret_cast (type, expression);
4064 break;
4065 case RID_CONSTCAST:
4066 postfix_expression
4067 = build_const_cast (type, expression);
4068 break;
4069 default:
4070 gcc_unreachable ();
4071 }
4072 }
4073 break;
4074
4075 case RID_TYPEID:
4076 {
4077 tree type;
4078 const char *saved_message;
4079 bool saved_in_type_id_in_expr_p;
4080
4081 /* Consume the `typeid' token. */
4082 cp_lexer_consume_token (parser->lexer);
4083 /* Look for the `(' token. */
4084 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4085 /* Types cannot be defined in a `typeid' expression. */
4086 saved_message = parser->type_definition_forbidden_message;
4087 parser->type_definition_forbidden_message
4088 = "types may not be defined in a `typeid\' expression";
4089 /* We can't be sure yet whether we're looking at a type-id or an
4090 expression. */
4091 cp_parser_parse_tentatively (parser);
4092 /* Try a type-id first. */
4093 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4094 parser->in_type_id_in_expr_p = true;
4095 type = cp_parser_type_id (parser);
4096 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4097 /* Look for the `)' token. Otherwise, we can't be sure that
4098 we're not looking at an expression: consider `typeid (int
4099 (3))', for example. */
4100 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4101 /* If all went well, simply lookup the type-id. */
4102 if (cp_parser_parse_definitely (parser))
4103 postfix_expression = get_typeid (type);
4104 /* Otherwise, fall back to the expression variant. */
4105 else
4106 {
4107 tree expression;
4108
4109 /* Look for an expression. */
4110 expression = cp_parser_expression (parser, /*cast_p=*/false);
4111 /* Compute its typeid. */
4112 postfix_expression = build_typeid (expression);
4113 /* Look for the `)' token. */
4114 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4115 }
4116 /* Restore the saved message. */
4117 parser->type_definition_forbidden_message = saved_message;
4118 /* `typeid' may not appear in an integral constant expression. */
4119 if (cp_parser_non_integral_constant_expression(parser,
4120 "`typeid' operator"))
4121 return error_mark_node;
4122 }
4123 break;
4124
4125 case RID_TYPENAME:
4126 {
4127 tree type;
4128 /* The syntax permitted here is the same permitted for an
4129 elaborated-type-specifier. */
4130 type = cp_parser_elaborated_type_specifier (parser,
4131 /*is_friend=*/false,
4132 /*is_declaration=*/false);
4133 postfix_expression = cp_parser_functional_cast (parser, type);
4134 }
4135 break;
4136
4137 default:
4138 {
4139 tree type;
4140
4141 /* If the next thing is a simple-type-specifier, we may be
4142 looking at a functional cast. We could also be looking at
4143 an id-expression. So, we try the functional cast, and if
4144 that doesn't work we fall back to the primary-expression. */
4145 cp_parser_parse_tentatively (parser);
4146 /* Look for the simple-type-specifier. */
4147 type = cp_parser_simple_type_specifier (parser,
4148 /*decl_specs=*/NULL,
4149 CP_PARSER_FLAGS_NONE);
4150 /* Parse the cast itself. */
4151 if (!cp_parser_error_occurred (parser))
4152 postfix_expression
4153 = cp_parser_functional_cast (parser, type);
4154 /* If that worked, we're done. */
4155 if (cp_parser_parse_definitely (parser))
4156 break;
4157
4158 /* If the functional-cast didn't work out, try a
4159 compound-literal. */
4160 if (cp_parser_allow_gnu_extensions_p (parser)
4161 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4162 {
4163 VEC(constructor_elt,gc) *initializer_list = NULL;
4164 bool saved_in_type_id_in_expr_p;
4165
4166 cp_parser_parse_tentatively (parser);
4167 /* Consume the `('. */
4168 cp_lexer_consume_token (parser->lexer);
4169 /* Parse the type. */
4170 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4171 parser->in_type_id_in_expr_p = true;
4172 type = cp_parser_type_id (parser);
4173 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4174 /* Look for the `)'. */
4175 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4176 /* Look for the `{'. */
4177 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4178 /* If things aren't going well, there's no need to
4179 keep going. */
4180 if (!cp_parser_error_occurred (parser))
4181 {
4182 bool non_constant_p;
4183 /* Parse the initializer-list. */
4184 initializer_list
4185 = cp_parser_initializer_list (parser, &non_constant_p);
4186 /* Allow a trailing `,'. */
4187 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4188 cp_lexer_consume_token (parser->lexer);
4189 /* Look for the final `}'. */
4190 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4191 }
4192 /* If that worked, we're definitely looking at a
4193 compound-literal expression. */
4194 if (cp_parser_parse_definitely (parser))
4195 {
4196 /* Warn the user that a compound literal is not
4197 allowed in standard C++. */
4198 if (pedantic)
4199 pedwarn ("ISO C++ forbids compound-literals");
4200 /* Form the representation of the compound-literal. */
4201 postfix_expression
4202 = finish_compound_literal (type, initializer_list);
4203 break;
4204 }
4205 }
4206
4207 /* It must be a primary-expression. */
4208 postfix_expression
4209 = cp_parser_primary_expression (parser, address_p, cast_p,
4210 /*template_arg_p=*/false,
4211 &idk);
4212 }
4213 break;
4214 }
4215
4216 /* Keep looping until the postfix-expression is complete. */
4217 while (true)
4218 {
4219 if (idk == CP_ID_KIND_UNQUALIFIED
4220 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4221 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4222 /* It is not a Koenig lookup function call. */
4223 postfix_expression
4224 = unqualified_name_lookup_error (postfix_expression);
4225
4226 /* Peek at the next token. */
4227 token = cp_lexer_peek_token (parser->lexer);
4228
4229 switch (token->type)
4230 {
4231 case CPP_OPEN_SQUARE:
4232 postfix_expression
4233 = cp_parser_postfix_open_square_expression (parser,
4234 postfix_expression,
4235 false);
4236 idk = CP_ID_KIND_NONE;
4237 break;
4238
4239 case CPP_OPEN_PAREN:
4240 /* postfix-expression ( expression-list [opt] ) */
4241 {
4242 bool koenig_p;
4243 bool is_builtin_constant_p;
4244 bool saved_integral_constant_expression_p = false;
4245 bool saved_non_integral_constant_expression_p = false;
4246 tree args;
4247
4248 is_builtin_constant_p
4249 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4250 if (is_builtin_constant_p)
4251 {
4252 /* The whole point of __builtin_constant_p is to allow
4253 non-constant expressions to appear as arguments. */
4254 saved_integral_constant_expression_p
4255 = parser->integral_constant_expression_p;
4256 saved_non_integral_constant_expression_p
4257 = parser->non_integral_constant_expression_p;
4258 parser->integral_constant_expression_p = false;
4259 }
4260 args = (cp_parser_parenthesized_expression_list
4261 (parser, /*is_attribute_list=*/false,
4262 /*cast_p=*/false,
4263 /*non_constant_p=*/NULL));
4264 if (is_builtin_constant_p)
4265 {
4266 parser->integral_constant_expression_p
4267 = saved_integral_constant_expression_p;
4268 parser->non_integral_constant_expression_p
4269 = saved_non_integral_constant_expression_p;
4270 }
4271
4272 if (args == error_mark_node)
4273 {
4274 postfix_expression = error_mark_node;
4275 break;
4276 }
4277
4278 /* Function calls are not permitted in
4279 constant-expressions. */
4280 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4281 && cp_parser_non_integral_constant_expression (parser,
4282 "a function call"))
4283 {
4284 postfix_expression = error_mark_node;
4285 break;
4286 }
4287
4288 koenig_p = false;
4289 if (idk == CP_ID_KIND_UNQUALIFIED)
4290 {
4291 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4292 {
4293 if (args)
4294 {
4295 koenig_p = true;
4296 postfix_expression
4297 = perform_koenig_lookup (postfix_expression, args);
4298 }
4299 else
4300 postfix_expression
4301 = unqualified_fn_lookup_error (postfix_expression);
4302 }
4303 /* We do not perform argument-dependent lookup if
4304 normal lookup finds a non-function, in accordance
4305 with the expected resolution of DR 218. */
4306 else if (args && is_overloaded_fn (postfix_expression))
4307 {
4308 tree fn = get_first_fn (postfix_expression);
4309
4310 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4311 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4312
4313 /* Only do argument dependent lookup if regular
4314 lookup does not find a set of member functions.
4315 [basic.lookup.koenig]/2a */
4316 if (!DECL_FUNCTION_MEMBER_P (fn))
4317 {
4318 koenig_p = true;
4319 postfix_expression
4320 = perform_koenig_lookup (postfix_expression, args);
4321 }
4322 }
4323 }
4324
4325 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4326 {
4327 tree instance = TREE_OPERAND (postfix_expression, 0);
4328 tree fn = TREE_OPERAND (postfix_expression, 1);
4329
4330 if (processing_template_decl
4331 && (type_dependent_expression_p (instance)
4332 || (!BASELINK_P (fn)
4333 && TREE_CODE (fn) != FIELD_DECL)
4334 || type_dependent_expression_p (fn)
4335 || any_type_dependent_arguments_p (args)))
4336 {
4337 postfix_expression
4338 = build_min_nt (CALL_EXPR, postfix_expression,
4339 args, NULL_TREE);
4340 break;
4341 }
4342
4343 if (BASELINK_P (fn))
4344 postfix_expression
4345 = (build_new_method_call
4346 (instance, fn, args, NULL_TREE,
4347 (idk == CP_ID_KIND_QUALIFIED
4348 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4349 /*fn_p=*/NULL));
4350 else
4351 postfix_expression
4352 = finish_call_expr (postfix_expression, args,
4353 /*disallow_virtual=*/false,
4354 /*koenig_p=*/false);
4355 }
4356 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4357 || TREE_CODE (postfix_expression) == MEMBER_REF
4358 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4359 postfix_expression = (build_offset_ref_call_from_tree
4360 (postfix_expression, args));
4361 else if (idk == CP_ID_KIND_QUALIFIED)
4362 /* A call to a static class member, or a namespace-scope
4363 function. */
4364 postfix_expression
4365 = finish_call_expr (postfix_expression, args,
4366 /*disallow_virtual=*/true,
4367 koenig_p);
4368 else
4369 /* All other function calls. */
4370 postfix_expression
4371 = finish_call_expr (postfix_expression, args,
4372 /*disallow_virtual=*/false,
4373 koenig_p);
4374
4375 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4376 idk = CP_ID_KIND_NONE;
4377 }
4378 break;
4379
4380 case CPP_DOT:
4381 case CPP_DEREF:
4382 /* postfix-expression . template [opt] id-expression
4383 postfix-expression . pseudo-destructor-name
4384 postfix-expression -> template [opt] id-expression
4385 postfix-expression -> pseudo-destructor-name */
4386
4387 /* Consume the `.' or `->' operator. */
4388 cp_lexer_consume_token (parser->lexer);
4389
4390 postfix_expression
4391 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4392 postfix_expression,
4393 false, &idk);
4394 break;
4395
4396 case CPP_PLUS_PLUS:
4397 /* postfix-expression ++ */
4398 /* Consume the `++' token. */
4399 cp_lexer_consume_token (parser->lexer);
4400 /* Generate a representation for the complete expression. */
4401 postfix_expression
4402 = finish_increment_expr (postfix_expression,
4403 POSTINCREMENT_EXPR);
4404 /* Increments may not appear in constant-expressions. */
4405 if (cp_parser_non_integral_constant_expression (parser,
4406 "an increment"))
4407 postfix_expression = error_mark_node;
4408 idk = CP_ID_KIND_NONE;
4409 break;
4410
4411 case CPP_MINUS_MINUS:
4412 /* postfix-expression -- */
4413 /* Consume the `--' token. */
4414 cp_lexer_consume_token (parser->lexer);
4415 /* Generate a representation for the complete expression. */
4416 postfix_expression
4417 = finish_increment_expr (postfix_expression,
4418 POSTDECREMENT_EXPR);
4419 /* Decrements may not appear in constant-expressions. */
4420 if (cp_parser_non_integral_constant_expression (parser,
4421 "a decrement"))
4422 postfix_expression = error_mark_node;
4423 idk = CP_ID_KIND_NONE;
4424 break;
4425
4426 default:
4427 return postfix_expression;
4428 }
4429 }
4430
4431 /* We should never get here. */
4432 gcc_unreachable ();
4433 return error_mark_node;
4434 }
4435
4436 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4437 by cp_parser_builtin_offsetof. We're looking for
4438
4439 postfix-expression [ expression ]
4440
4441 FOR_OFFSETOF is set if we're being called in that context, which
4442 changes how we deal with integer constant expressions. */
4443
4444 static tree
4445 cp_parser_postfix_open_square_expression (cp_parser *parser,
4446 tree postfix_expression,
4447 bool for_offsetof)
4448 {
4449 tree index;
4450
4451 /* Consume the `[' token. */
4452 cp_lexer_consume_token (parser->lexer);
4453
4454 /* Parse the index expression. */
4455 /* ??? For offsetof, there is a question of what to allow here. If
4456 offsetof is not being used in an integral constant expression context,
4457 then we *could* get the right answer by computing the value at runtime.
4458 If we are in an integral constant expression context, then we might
4459 could accept any constant expression; hard to say without analysis.
4460 Rather than open the barn door too wide right away, allow only integer
4461 constant expressions here. */
4462 if (for_offsetof)
4463 index = cp_parser_constant_expression (parser, false, NULL);
4464 else
4465 index = cp_parser_expression (parser, /*cast_p=*/false);
4466
4467 /* Look for the closing `]'. */
4468 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4469
4470 /* Build the ARRAY_REF. */
4471 postfix_expression = grok_array_decl (postfix_expression, index);
4472
4473 /* When not doing offsetof, array references are not permitted in
4474 constant-expressions. */
4475 if (!for_offsetof
4476 && (cp_parser_non_integral_constant_expression
4477 (parser, "an array reference")))
4478 postfix_expression = error_mark_node;
4479
4480 return postfix_expression;
4481 }
4482
4483 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4484 by cp_parser_builtin_offsetof. We're looking for
4485
4486 postfix-expression . template [opt] id-expression
4487 postfix-expression . pseudo-destructor-name
4488 postfix-expression -> template [opt] id-expression
4489 postfix-expression -> pseudo-destructor-name
4490
4491 FOR_OFFSETOF is set if we're being called in that context. That sorta
4492 limits what of the above we'll actually accept, but nevermind.
4493 TOKEN_TYPE is the "." or "->" token, which will already have been
4494 removed from the stream. */
4495
4496 static tree
4497 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4498 enum cpp_ttype token_type,
4499 tree postfix_expression,
4500 bool for_offsetof, cp_id_kind *idk)
4501 {
4502 tree name;
4503 bool dependent_p;
4504 bool pseudo_destructor_p;
4505 tree scope = NULL_TREE;
4506
4507 /* If this is a `->' operator, dereference the pointer. */
4508 if (token_type == CPP_DEREF)
4509 postfix_expression = build_x_arrow (postfix_expression);
4510 /* Check to see whether or not the expression is type-dependent. */
4511 dependent_p = type_dependent_expression_p (postfix_expression);
4512 /* The identifier following the `->' or `.' is not qualified. */
4513 parser->scope = NULL_TREE;
4514 parser->qualifying_scope = NULL_TREE;
4515 parser->object_scope = NULL_TREE;
4516 *idk = CP_ID_KIND_NONE;
4517 /* Enter the scope corresponding to the type of the object
4518 given by the POSTFIX_EXPRESSION. */
4519 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4520 {
4521 scope = TREE_TYPE (postfix_expression);
4522 /* According to the standard, no expression should ever have
4523 reference type. Unfortunately, we do not currently match
4524 the standard in this respect in that our internal representation
4525 of an expression may have reference type even when the standard
4526 says it does not. Therefore, we have to manually obtain the
4527 underlying type here. */
4528 scope = non_reference (scope);
4529 /* The type of the POSTFIX_EXPRESSION must be complete. */
4530 if (scope == unknown_type_node)
4531 {
4532 error ("%qE does not have class type", postfix_expression);
4533 scope = NULL_TREE;
4534 }
4535 else
4536 scope = complete_type_or_else (scope, NULL_TREE);
4537 /* Let the name lookup machinery know that we are processing a
4538 class member access expression. */
4539 parser->context->object_type = scope;
4540 /* If something went wrong, we want to be able to discern that case,
4541 as opposed to the case where there was no SCOPE due to the type
4542 of expression being dependent. */
4543 if (!scope)
4544 scope = error_mark_node;
4545 /* If the SCOPE was erroneous, make the various semantic analysis
4546 functions exit quickly -- and without issuing additional error
4547 messages. */
4548 if (scope == error_mark_node)
4549 postfix_expression = error_mark_node;
4550 }
4551
4552 /* Assume this expression is not a pseudo-destructor access. */
4553 pseudo_destructor_p = false;
4554
4555 /* If the SCOPE is a scalar type, then, if this is a valid program,
4556 we must be looking at a pseudo-destructor-name. */
4557 if (scope && SCALAR_TYPE_P (scope))
4558 {
4559 tree s;
4560 tree type;
4561
4562 cp_parser_parse_tentatively (parser);
4563 /* Parse the pseudo-destructor-name. */
4564 s = NULL_TREE;
4565 cp_parser_pseudo_destructor_name (parser, &s, &type);
4566 if (cp_parser_parse_definitely (parser))
4567 {
4568 pseudo_destructor_p = true;
4569 postfix_expression
4570 = finish_pseudo_destructor_expr (postfix_expression,
4571 s, TREE_TYPE (type));
4572 }
4573 }
4574
4575 if (!pseudo_destructor_p)
4576 {
4577 /* If the SCOPE is not a scalar type, we are looking at an
4578 ordinary class member access expression, rather than a
4579 pseudo-destructor-name. */
4580 bool template_p;
4581 /* Parse the id-expression. */
4582 name = (cp_parser_id_expression
4583 (parser,
4584 cp_parser_optional_template_keyword (parser),
4585 /*check_dependency_p=*/true,
4586 &template_p,
4587 /*declarator_p=*/false,
4588 /*optional_p=*/false));
4589 /* In general, build a SCOPE_REF if the member name is qualified.
4590 However, if the name was not dependent and has already been
4591 resolved; there is no need to build the SCOPE_REF. For example;
4592
4593 struct X { void f(); };
4594 template <typename T> void f(T* t) { t->X::f(); }
4595
4596 Even though "t" is dependent, "X::f" is not and has been resolved
4597 to a BASELINK; there is no need to include scope information. */
4598
4599 /* But we do need to remember that there was an explicit scope for
4600 virtual function calls. */
4601 if (parser->scope)
4602 *idk = CP_ID_KIND_QUALIFIED;
4603
4604 /* If the name is a template-id that names a type, we will get a
4605 TYPE_DECL here. That is invalid code. */
4606 if (TREE_CODE (name) == TYPE_DECL)
4607 {
4608 error ("invalid use of %qD", name);
4609 postfix_expression = error_mark_node;
4610 }
4611 else
4612 {
4613 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4614 {
4615 name = build_qualified_name (/*type=*/NULL_TREE,
4616 parser->scope,
4617 name,
4618 template_p);
4619 parser->scope = NULL_TREE;
4620 parser->qualifying_scope = NULL_TREE;
4621 parser->object_scope = NULL_TREE;
4622 }
4623 if (scope && name && BASELINK_P (name))
4624 adjust_result_of_qualified_name_lookup
4625 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4626 postfix_expression
4627 = finish_class_member_access_expr (postfix_expression, name,
4628 template_p);
4629 }
4630 }
4631
4632 /* We no longer need to look up names in the scope of the object on
4633 the left-hand side of the `.' or `->' operator. */
4634 parser->context->object_type = NULL_TREE;
4635
4636 /* Outside of offsetof, these operators may not appear in
4637 constant-expressions. */
4638 if (!for_offsetof
4639 && (cp_parser_non_integral_constant_expression
4640 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4641 postfix_expression = error_mark_node;
4642
4643 return postfix_expression;
4644 }
4645
4646 /* Parse a parenthesized expression-list.
4647
4648 expression-list:
4649 assignment-expression
4650 expression-list, assignment-expression
4651
4652 attribute-list:
4653 expression-list
4654 identifier
4655 identifier, expression-list
4656
4657 CAST_P is true if this expression is the target of a cast.
4658
4659 Returns a TREE_LIST. The TREE_VALUE of each node is a
4660 representation of an assignment-expression. Note that a TREE_LIST
4661 is returned even if there is only a single expression in the list.
4662 error_mark_node is returned if the ( and or ) are
4663 missing. NULL_TREE is returned on no expressions. The parentheses
4664 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4665 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4666 indicates whether or not all of the expressions in the list were
4667 constant. */
4668
4669 static tree
4670 cp_parser_parenthesized_expression_list (cp_parser* parser,
4671 bool is_attribute_list,
4672 bool cast_p,
4673 bool *non_constant_p)
4674 {
4675 tree expression_list = NULL_TREE;
4676 bool fold_expr_p = is_attribute_list;
4677 tree identifier = NULL_TREE;
4678
4679 /* Assume all the expressions will be constant. */
4680 if (non_constant_p)
4681 *non_constant_p = false;
4682
4683 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4684 return error_mark_node;
4685
4686 /* Consume expressions until there are no more. */
4687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4688 while (true)
4689 {
4690 tree expr;
4691
4692 /* At the beginning of attribute lists, check to see if the
4693 next token is an identifier. */
4694 if (is_attribute_list
4695 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4696 {
4697 cp_token *token;
4698
4699 /* Consume the identifier. */
4700 token = cp_lexer_consume_token (parser->lexer);
4701 /* Save the identifier. */
4702 identifier = token->value;
4703 }
4704 else
4705 {
4706 /* Parse the next assignment-expression. */
4707 if (non_constant_p)
4708 {
4709 bool expr_non_constant_p;
4710 expr = (cp_parser_constant_expression
4711 (parser, /*allow_non_constant_p=*/true,
4712 &expr_non_constant_p));
4713 if (expr_non_constant_p)
4714 *non_constant_p = true;
4715 }
4716 else
4717 expr = cp_parser_assignment_expression (parser, cast_p);
4718
4719 if (fold_expr_p)
4720 expr = fold_non_dependent_expr (expr);
4721
4722 /* Add it to the list. We add error_mark_node
4723 expressions to the list, so that we can still tell if
4724 the correct form for a parenthesized expression-list
4725 is found. That gives better errors. */
4726 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4727
4728 if (expr == error_mark_node)
4729 goto skip_comma;
4730 }
4731
4732 /* After the first item, attribute lists look the same as
4733 expression lists. */
4734 is_attribute_list = false;
4735
4736 get_comma:;
4737 /* If the next token isn't a `,', then we are done. */
4738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4739 break;
4740
4741 /* Otherwise, consume the `,' and keep going. */
4742 cp_lexer_consume_token (parser->lexer);
4743 }
4744
4745 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4746 {
4747 int ending;
4748
4749 skip_comma:;
4750 /* We try and resync to an unnested comma, as that will give the
4751 user better diagnostics. */
4752 ending = cp_parser_skip_to_closing_parenthesis (parser,
4753 /*recovering=*/true,
4754 /*or_comma=*/true,
4755 /*consume_paren=*/true);
4756 if (ending < 0)
4757 goto get_comma;
4758 if (!ending)
4759 return error_mark_node;
4760 }
4761
4762 /* We built up the list in reverse order so we must reverse it now. */
4763 expression_list = nreverse (expression_list);
4764 if (identifier)
4765 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4766
4767 return expression_list;
4768 }
4769
4770 /* Parse a pseudo-destructor-name.
4771
4772 pseudo-destructor-name:
4773 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4774 :: [opt] nested-name-specifier template template-id :: ~ type-name
4775 :: [opt] nested-name-specifier [opt] ~ type-name
4776
4777 If either of the first two productions is used, sets *SCOPE to the
4778 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4779 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4780 or ERROR_MARK_NODE if the parse fails. */
4781
4782 static void
4783 cp_parser_pseudo_destructor_name (cp_parser* parser,
4784 tree* scope,
4785 tree* type)
4786 {
4787 bool nested_name_specifier_p;
4788
4789 /* Assume that things will not work out. */
4790 *type = error_mark_node;
4791
4792 /* Look for the optional `::' operator. */
4793 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4794 /* Look for the optional nested-name-specifier. */
4795 nested_name_specifier_p
4796 = (cp_parser_nested_name_specifier_opt (parser,
4797 /*typename_keyword_p=*/false,
4798 /*check_dependency_p=*/true,
4799 /*type_p=*/false,
4800 /*is_declaration=*/true)
4801 != NULL_TREE);
4802 /* Now, if we saw a nested-name-specifier, we might be doing the
4803 second production. */
4804 if (nested_name_specifier_p
4805 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4806 {
4807 /* Consume the `template' keyword. */
4808 cp_lexer_consume_token (parser->lexer);
4809 /* Parse the template-id. */
4810 cp_parser_template_id (parser,
4811 /*template_keyword_p=*/true,
4812 /*check_dependency_p=*/false,
4813 /*is_declaration=*/true);
4814 /* Look for the `::' token. */
4815 cp_parser_require (parser, CPP_SCOPE, "`::'");
4816 }
4817 /* If the next token is not a `~', then there might be some
4818 additional qualification. */
4819 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4820 {
4821 /* Look for the type-name. */
4822 *scope = TREE_TYPE (cp_parser_type_name (parser));
4823
4824 if (*scope == error_mark_node)
4825 return;
4826
4827 /* If we don't have ::~, then something has gone wrong. Since
4828 the only caller of this function is looking for something
4829 after `.' or `->' after a scalar type, most likely the
4830 program is trying to get a member of a non-aggregate
4831 type. */
4832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4833 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4834 {
4835 cp_parser_error (parser, "request for member of non-aggregate type");
4836 return;
4837 }
4838
4839 /* Look for the `::' token. */
4840 cp_parser_require (parser, CPP_SCOPE, "`::'");
4841 }
4842 else
4843 *scope = NULL_TREE;
4844
4845 /* Look for the `~'. */
4846 cp_parser_require (parser, CPP_COMPL, "`~'");
4847 /* Look for the type-name again. We are not responsible for
4848 checking that it matches the first type-name. */
4849 *type = cp_parser_type_name (parser);
4850 }
4851
4852 /* Parse a unary-expression.
4853
4854 unary-expression:
4855 postfix-expression
4856 ++ cast-expression
4857 -- cast-expression
4858 unary-operator cast-expression
4859 sizeof unary-expression
4860 sizeof ( type-id )
4861 new-expression
4862 delete-expression
4863
4864 GNU Extensions:
4865
4866 unary-expression:
4867 __extension__ cast-expression
4868 __alignof__ unary-expression
4869 __alignof__ ( type-id )
4870 __real__ cast-expression
4871 __imag__ cast-expression
4872 && identifier
4873
4874 ADDRESS_P is true iff the unary-expression is appearing as the
4875 operand of the `&' operator. CAST_P is true if this expression is
4876 the target of a cast.
4877
4878 Returns a representation of the expression. */
4879
4880 static tree
4881 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4882 {
4883 cp_token *token;
4884 enum tree_code unary_operator;
4885
4886 /* Peek at the next token. */
4887 token = cp_lexer_peek_token (parser->lexer);
4888 /* Some keywords give away the kind of expression. */
4889 if (token->type == CPP_KEYWORD)
4890 {
4891 enum rid keyword = token->keyword;
4892
4893 switch (keyword)
4894 {
4895 case RID_ALIGNOF:
4896 case RID_SIZEOF:
4897 {
4898 tree operand;
4899 enum tree_code op;
4900
4901 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4902 /* Consume the token. */
4903 cp_lexer_consume_token (parser->lexer);
4904 /* Parse the operand. */
4905 operand = cp_parser_sizeof_operand (parser, keyword);
4906
4907 if (TYPE_P (operand))
4908 return cxx_sizeof_or_alignof_type (operand, op, true);
4909 else
4910 return cxx_sizeof_or_alignof_expr (operand, op);
4911 }
4912
4913 case RID_NEW:
4914 return cp_parser_new_expression (parser);
4915
4916 case RID_DELETE:
4917 return cp_parser_delete_expression (parser);
4918
4919 case RID_EXTENSION:
4920 {
4921 /* The saved value of the PEDANTIC flag. */
4922 int saved_pedantic;
4923 tree expr;
4924
4925 /* Save away the PEDANTIC flag. */
4926 cp_parser_extension_opt (parser, &saved_pedantic);
4927 /* Parse the cast-expression. */
4928 expr = cp_parser_simple_cast_expression (parser);
4929 /* Restore the PEDANTIC flag. */
4930 pedantic = saved_pedantic;
4931
4932 return expr;
4933 }
4934
4935 case RID_REALPART:
4936 case RID_IMAGPART:
4937 {
4938 tree expression;
4939
4940 /* Consume the `__real__' or `__imag__' token. */
4941 cp_lexer_consume_token (parser->lexer);
4942 /* Parse the cast-expression. */
4943 expression = cp_parser_simple_cast_expression (parser);
4944 /* Create the complete representation. */
4945 return build_x_unary_op ((keyword == RID_REALPART
4946 ? REALPART_EXPR : IMAGPART_EXPR),
4947 expression);
4948 }
4949 break;
4950
4951 default:
4952 break;
4953 }
4954 }
4955
4956 /* Look for the `:: new' and `:: delete', which also signal the
4957 beginning of a new-expression, or delete-expression,
4958 respectively. If the next token is `::', then it might be one of
4959 these. */
4960 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4961 {
4962 enum rid keyword;
4963
4964 /* See if the token after the `::' is one of the keywords in
4965 which we're interested. */
4966 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4967 /* If it's `new', we have a new-expression. */
4968 if (keyword == RID_NEW)
4969 return cp_parser_new_expression (parser);
4970 /* Similarly, for `delete'. */
4971 else if (keyword == RID_DELETE)
4972 return cp_parser_delete_expression (parser);
4973 }
4974
4975 /* Look for a unary operator. */
4976 unary_operator = cp_parser_unary_operator (token);
4977 /* The `++' and `--' operators can be handled similarly, even though
4978 they are not technically unary-operators in the grammar. */
4979 if (unary_operator == ERROR_MARK)
4980 {
4981 if (token->type == CPP_PLUS_PLUS)
4982 unary_operator = PREINCREMENT_EXPR;
4983 else if (token->type == CPP_MINUS_MINUS)
4984 unary_operator = PREDECREMENT_EXPR;
4985 /* Handle the GNU address-of-label extension. */
4986 else if (cp_parser_allow_gnu_extensions_p (parser)
4987 && token->type == CPP_AND_AND)
4988 {
4989 tree identifier;
4990
4991 /* Consume the '&&' token. */
4992 cp_lexer_consume_token (parser->lexer);
4993 /* Look for the identifier. */
4994 identifier = cp_parser_identifier (parser);
4995 /* Create an expression representing the address. */
4996 return finish_label_address_expr (identifier);
4997 }
4998 }
4999 if (unary_operator != ERROR_MARK)
5000 {
5001 tree cast_expression;
5002 tree expression = error_mark_node;
5003 const char *non_constant_p = NULL;
5004
5005 /* Consume the operator token. */
5006 token = cp_lexer_consume_token (parser->lexer);
5007 /* Parse the cast-expression. */
5008 cast_expression
5009 = cp_parser_cast_expression (parser,
5010 unary_operator == ADDR_EXPR,
5011 /*cast_p=*/false);
5012 /* Now, build an appropriate representation. */
5013 switch (unary_operator)
5014 {
5015 case INDIRECT_REF:
5016 non_constant_p = "`*'";
5017 expression = build_x_indirect_ref (cast_expression, "unary *");
5018 break;
5019
5020 case ADDR_EXPR:
5021 non_constant_p = "`&'";
5022 /* Fall through. */
5023 case BIT_NOT_EXPR:
5024 expression = build_x_unary_op (unary_operator, cast_expression);
5025 break;
5026
5027 case PREINCREMENT_EXPR:
5028 case PREDECREMENT_EXPR:
5029 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5030 ? "`++'" : "`--'");
5031 /* Fall through. */
5032 case UNARY_PLUS_EXPR:
5033 case NEGATE_EXPR:
5034 case TRUTH_NOT_EXPR:
5035 expression = finish_unary_op_expr (unary_operator, cast_expression);
5036 break;
5037
5038 default:
5039 gcc_unreachable ();
5040 }
5041
5042 if (non_constant_p
5043 && cp_parser_non_integral_constant_expression (parser,
5044 non_constant_p))
5045 expression = error_mark_node;
5046
5047 return expression;
5048 }
5049
5050 return cp_parser_postfix_expression (parser, address_p, cast_p);
5051 }
5052
5053 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5054 unary-operator, the corresponding tree code is returned. */
5055
5056 static enum tree_code
5057 cp_parser_unary_operator (cp_token* token)
5058 {
5059 switch (token->type)
5060 {
5061 case CPP_MULT:
5062 return INDIRECT_REF;
5063
5064 case CPP_AND:
5065 return ADDR_EXPR;
5066
5067 case CPP_PLUS:
5068 return UNARY_PLUS_EXPR;
5069
5070 case CPP_MINUS:
5071 return NEGATE_EXPR;
5072
5073 case CPP_NOT:
5074 return TRUTH_NOT_EXPR;
5075
5076 case CPP_COMPL:
5077 return BIT_NOT_EXPR;
5078
5079 default:
5080 return ERROR_MARK;
5081 }
5082 }
5083
5084 /* Parse a new-expression.
5085
5086 new-expression:
5087 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5088 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5089
5090 Returns a representation of the expression. */
5091
5092 static tree
5093 cp_parser_new_expression (cp_parser* parser)
5094 {
5095 bool global_scope_p;
5096 tree placement;
5097 tree type;
5098 tree initializer;
5099 tree nelts;
5100
5101 /* Look for the optional `::' operator. */
5102 global_scope_p
5103 = (cp_parser_global_scope_opt (parser,
5104 /*current_scope_valid_p=*/false)
5105 != NULL_TREE);
5106 /* Look for the `new' operator. */
5107 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5108 /* There's no easy way to tell a new-placement from the
5109 `( type-id )' construct. */
5110 cp_parser_parse_tentatively (parser);
5111 /* Look for a new-placement. */
5112 placement = cp_parser_new_placement (parser);
5113 /* If that didn't work out, there's no new-placement. */
5114 if (!cp_parser_parse_definitely (parser))
5115 placement = NULL_TREE;
5116
5117 /* If the next token is a `(', then we have a parenthesized
5118 type-id. */
5119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5120 {
5121 /* Consume the `('. */
5122 cp_lexer_consume_token (parser->lexer);
5123 /* Parse the type-id. */
5124 type = cp_parser_type_id (parser);
5125 /* Look for the closing `)'. */
5126 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5127 /* There should not be a direct-new-declarator in this production,
5128 but GCC used to allowed this, so we check and emit a sensible error
5129 message for this case. */
5130 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5131 {
5132 error ("array bound forbidden after parenthesized type-id");
5133 inform ("try removing the parentheses around the type-id");
5134 cp_parser_direct_new_declarator (parser);
5135 }
5136 nelts = NULL_TREE;
5137 }
5138 /* Otherwise, there must be a new-type-id. */
5139 else
5140 type = cp_parser_new_type_id (parser, &nelts);
5141
5142 /* If the next token is a `(', then we have a new-initializer. */
5143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5144 initializer = cp_parser_new_initializer (parser);
5145 else
5146 initializer = NULL_TREE;
5147
5148 /* A new-expression may not appear in an integral constant
5149 expression. */
5150 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5151 return error_mark_node;
5152
5153 /* Create a representation of the new-expression. */
5154 return build_new (placement, type, nelts, initializer, global_scope_p);
5155 }
5156
5157 /* Parse a new-placement.
5158
5159 new-placement:
5160 ( expression-list )
5161
5162 Returns the same representation as for an expression-list. */
5163
5164 static tree
5165 cp_parser_new_placement (cp_parser* parser)
5166 {
5167 tree expression_list;
5168
5169 /* Parse the expression-list. */
5170 expression_list = (cp_parser_parenthesized_expression_list
5171 (parser, false, /*cast_p=*/false,
5172 /*non_constant_p=*/NULL));
5173
5174 return expression_list;
5175 }
5176
5177 /* Parse a new-type-id.
5178
5179 new-type-id:
5180 type-specifier-seq new-declarator [opt]
5181
5182 Returns the TYPE allocated. If the new-type-id indicates an array
5183 type, *NELTS is set to the number of elements in the last array
5184 bound; the TYPE will not include the last array bound. */
5185
5186 static tree
5187 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5188 {
5189 cp_decl_specifier_seq type_specifier_seq;
5190 cp_declarator *new_declarator;
5191 cp_declarator *declarator;
5192 cp_declarator *outer_declarator;
5193 const char *saved_message;
5194 tree type;
5195
5196 /* The type-specifier sequence must not contain type definitions.
5197 (It cannot contain declarations of new types either, but if they
5198 are not definitions we will catch that because they are not
5199 complete.) */
5200 saved_message = parser->type_definition_forbidden_message;
5201 parser->type_definition_forbidden_message
5202 = "types may not be defined in a new-type-id";
5203 /* Parse the type-specifier-seq. */
5204 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5205 &type_specifier_seq);
5206 /* Restore the old message. */
5207 parser->type_definition_forbidden_message = saved_message;
5208 /* Parse the new-declarator. */
5209 new_declarator = cp_parser_new_declarator_opt (parser);
5210
5211 /* Determine the number of elements in the last array dimension, if
5212 any. */
5213 *nelts = NULL_TREE;
5214 /* Skip down to the last array dimension. */
5215 declarator = new_declarator;
5216 outer_declarator = NULL;
5217 while (declarator && (declarator->kind == cdk_pointer
5218 || declarator->kind == cdk_ptrmem))
5219 {
5220 outer_declarator = declarator;
5221 declarator = declarator->declarator;
5222 }
5223 while (declarator
5224 && declarator->kind == cdk_array
5225 && declarator->declarator
5226 && declarator->declarator->kind == cdk_array)
5227 {
5228 outer_declarator = declarator;
5229 declarator = declarator->declarator;
5230 }
5231
5232 if (declarator && declarator->kind == cdk_array)
5233 {
5234 *nelts = declarator->u.array.bounds;
5235 if (*nelts == error_mark_node)
5236 *nelts = integer_one_node;
5237
5238 if (outer_declarator)
5239 outer_declarator->declarator = declarator->declarator;
5240 else
5241 new_declarator = NULL;
5242 }
5243
5244 type = groktypename (&type_specifier_seq, new_declarator);
5245 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5246 {
5247 *nelts = array_type_nelts_top (type);
5248 type = TREE_TYPE (type);
5249 }
5250 return type;
5251 }
5252
5253 /* Parse an (optional) new-declarator.
5254
5255 new-declarator:
5256 ptr-operator new-declarator [opt]
5257 direct-new-declarator
5258
5259 Returns the declarator. */
5260
5261 static cp_declarator *
5262 cp_parser_new_declarator_opt (cp_parser* parser)
5263 {
5264 enum tree_code code;
5265 tree type;
5266 cp_cv_quals cv_quals;
5267
5268 /* We don't know if there's a ptr-operator next, or not. */
5269 cp_parser_parse_tentatively (parser);
5270 /* Look for a ptr-operator. */
5271 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5272 /* If that worked, look for more new-declarators. */
5273 if (cp_parser_parse_definitely (parser))
5274 {
5275 cp_declarator *declarator;
5276
5277 /* Parse another optional declarator. */
5278 declarator = cp_parser_new_declarator_opt (parser);
5279
5280 /* Create the representation of the declarator. */
5281 if (type)
5282 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5283 else if (code == INDIRECT_REF)
5284 declarator = make_pointer_declarator (cv_quals, declarator);
5285 else
5286 declarator = make_reference_declarator (cv_quals, declarator);
5287
5288 return declarator;
5289 }
5290
5291 /* If the next token is a `[', there is a direct-new-declarator. */
5292 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5293 return cp_parser_direct_new_declarator (parser);
5294
5295 return NULL;
5296 }
5297
5298 /* Parse a direct-new-declarator.
5299
5300 direct-new-declarator:
5301 [ expression ]
5302 direct-new-declarator [constant-expression]
5303
5304 */
5305
5306 static cp_declarator *
5307 cp_parser_direct_new_declarator (cp_parser* parser)
5308 {
5309 cp_declarator *declarator = NULL;
5310
5311 while (true)
5312 {
5313 tree expression;
5314
5315 /* Look for the opening `['. */
5316 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5317 /* The first expression is not required to be constant. */
5318 if (!declarator)
5319 {
5320 expression = cp_parser_expression (parser, /*cast_p=*/false);
5321 /* The standard requires that the expression have integral
5322 type. DR 74 adds enumeration types. We believe that the
5323 real intent is that these expressions be handled like the
5324 expression in a `switch' condition, which also allows
5325 classes with a single conversion to integral or
5326 enumeration type. */
5327 if (!processing_template_decl)
5328 {
5329 expression
5330 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5331 expression,
5332 /*complain=*/true);
5333 if (!expression)
5334 {
5335 error ("expression in new-declarator must have integral "
5336 "or enumeration type");
5337 expression = error_mark_node;
5338 }
5339 }
5340 }
5341 /* But all the other expressions must be. */
5342 else
5343 expression
5344 = cp_parser_constant_expression (parser,
5345 /*allow_non_constant=*/false,
5346 NULL);
5347 /* Look for the closing `]'. */
5348 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5349
5350 /* Add this bound to the declarator. */
5351 declarator = make_array_declarator (declarator, expression);
5352
5353 /* If the next token is not a `[', then there are no more
5354 bounds. */
5355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5356 break;
5357 }
5358
5359 return declarator;
5360 }
5361
5362 /* Parse a new-initializer.
5363
5364 new-initializer:
5365 ( expression-list [opt] )
5366
5367 Returns a representation of the expression-list. If there is no
5368 expression-list, VOID_ZERO_NODE is returned. */
5369
5370 static tree
5371 cp_parser_new_initializer (cp_parser* parser)
5372 {
5373 tree expression_list;
5374
5375 expression_list = (cp_parser_parenthesized_expression_list
5376 (parser, false, /*cast_p=*/false,
5377 /*non_constant_p=*/NULL));
5378 if (!expression_list)
5379 expression_list = void_zero_node;
5380
5381 return expression_list;
5382 }
5383
5384 /* Parse a delete-expression.
5385
5386 delete-expression:
5387 :: [opt] delete cast-expression
5388 :: [opt] delete [ ] cast-expression
5389
5390 Returns a representation of the expression. */
5391
5392 static tree
5393 cp_parser_delete_expression (cp_parser* parser)
5394 {
5395 bool global_scope_p;
5396 bool array_p;
5397 tree expression;
5398
5399 /* Look for the optional `::' operator. */
5400 global_scope_p
5401 = (cp_parser_global_scope_opt (parser,
5402 /*current_scope_valid_p=*/false)
5403 != NULL_TREE);
5404 /* Look for the `delete' keyword. */
5405 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5406 /* See if the array syntax is in use. */
5407 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5408 {
5409 /* Consume the `[' token. */
5410 cp_lexer_consume_token (parser->lexer);
5411 /* Look for the `]' token. */
5412 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5413 /* Remember that this is the `[]' construct. */
5414 array_p = true;
5415 }
5416 else
5417 array_p = false;
5418
5419 /* Parse the cast-expression. */
5420 expression = cp_parser_simple_cast_expression (parser);
5421
5422 /* A delete-expression may not appear in an integral constant
5423 expression. */
5424 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5425 return error_mark_node;
5426
5427 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5428 }
5429
5430 /* Parse a cast-expression.
5431
5432 cast-expression:
5433 unary-expression
5434 ( type-id ) cast-expression
5435
5436 ADDRESS_P is true iff the unary-expression is appearing as the
5437 operand of the `&' operator. CAST_P is true if this expression is
5438 the target of a cast.
5439
5440 Returns a representation of the expression. */
5441
5442 static tree
5443 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5444 {
5445 /* If it's a `(', then we might be looking at a cast. */
5446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5447 {
5448 tree type = NULL_TREE;
5449 tree expr = NULL_TREE;
5450 bool compound_literal_p;
5451 const char *saved_message;
5452
5453 /* There's no way to know yet whether or not this is a cast.
5454 For example, `(int (3))' is a unary-expression, while `(int)
5455 3' is a cast. So, we resort to parsing tentatively. */
5456 cp_parser_parse_tentatively (parser);
5457 /* Types may not be defined in a cast. */
5458 saved_message = parser->type_definition_forbidden_message;
5459 parser->type_definition_forbidden_message
5460 = "types may not be defined in casts";
5461 /* Consume the `('. */
5462 cp_lexer_consume_token (parser->lexer);
5463 /* A very tricky bit is that `(struct S) { 3 }' is a
5464 compound-literal (which we permit in C++ as an extension).
5465 But, that construct is not a cast-expression -- it is a
5466 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5467 is legal; if the compound-literal were a cast-expression,
5468 you'd need an extra set of parentheses.) But, if we parse
5469 the type-id, and it happens to be a class-specifier, then we
5470 will commit to the parse at that point, because we cannot
5471 undo the action that is done when creating a new class. So,
5472 then we cannot back up and do a postfix-expression.
5473
5474 Therefore, we scan ahead to the closing `)', and check to see
5475 if the token after the `)' is a `{'. If so, we are not
5476 looking at a cast-expression.
5477
5478 Save tokens so that we can put them back. */
5479 cp_lexer_save_tokens (parser->lexer);
5480 /* Skip tokens until the next token is a closing parenthesis.
5481 If we find the closing `)', and the next token is a `{', then
5482 we are looking at a compound-literal. */
5483 compound_literal_p
5484 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5485 /*consume_paren=*/true)
5486 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5487 /* Roll back the tokens we skipped. */
5488 cp_lexer_rollback_tokens (parser->lexer);
5489 /* If we were looking at a compound-literal, simulate an error
5490 so that the call to cp_parser_parse_definitely below will
5491 fail. */
5492 if (compound_literal_p)
5493 cp_parser_simulate_error (parser);
5494 else
5495 {
5496 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5497 parser->in_type_id_in_expr_p = true;
5498 /* Look for the type-id. */
5499 type = cp_parser_type_id (parser);
5500 /* Look for the closing `)'. */
5501 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5502 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5503 }
5504
5505 /* Restore the saved message. */
5506 parser->type_definition_forbidden_message = saved_message;
5507
5508 /* If ok so far, parse the dependent expression. We cannot be
5509 sure it is a cast. Consider `(T ())'. It is a parenthesized
5510 ctor of T, but looks like a cast to function returning T
5511 without a dependent expression. */
5512 if (!cp_parser_error_occurred (parser))
5513 expr = cp_parser_cast_expression (parser,
5514 /*address_p=*/false,
5515 /*cast_p=*/true);
5516
5517 if (cp_parser_parse_definitely (parser))
5518 {
5519 /* Warn about old-style casts, if so requested. */
5520 if (warn_old_style_cast
5521 && !in_system_header
5522 && !VOID_TYPE_P (type)
5523 && current_lang_name != lang_name_c)
5524 warning (OPT_Wold_style_cast, "use of old-style cast");
5525
5526 /* Only type conversions to integral or enumeration types
5527 can be used in constant-expressions. */
5528 if (!cast_valid_in_integral_constant_expression_p (type)
5529 && (cp_parser_non_integral_constant_expression
5530 (parser,
5531 "a cast to a type other than an integral or "
5532 "enumeration type")))
5533 return error_mark_node;
5534
5535 /* Perform the cast. */
5536 expr = build_c_cast (type, expr);
5537 return expr;
5538 }
5539 }
5540
5541 /* If we get here, then it's not a cast, so it must be a
5542 unary-expression. */
5543 return cp_parser_unary_expression (parser, address_p, cast_p);
5544 }
5545
5546 /* Parse a binary expression of the general form:
5547
5548 pm-expression:
5549 cast-expression
5550 pm-expression .* cast-expression
5551 pm-expression ->* cast-expression
5552
5553 multiplicative-expression:
5554 pm-expression
5555 multiplicative-expression * pm-expression
5556 multiplicative-expression / pm-expression
5557 multiplicative-expression % pm-expression
5558
5559 additive-expression:
5560 multiplicative-expression
5561 additive-expression + multiplicative-expression
5562 additive-expression - multiplicative-expression
5563
5564 shift-expression:
5565 additive-expression
5566 shift-expression << additive-expression
5567 shift-expression >> additive-expression
5568
5569 relational-expression:
5570 shift-expression
5571 relational-expression < shift-expression
5572 relational-expression > shift-expression
5573 relational-expression <= shift-expression
5574 relational-expression >= shift-expression
5575
5576 GNU Extension:
5577
5578 relational-expression:
5579 relational-expression <? shift-expression
5580 relational-expression >? shift-expression
5581
5582 equality-expression:
5583 relational-expression
5584 equality-expression == relational-expression
5585 equality-expression != relational-expression
5586
5587 and-expression:
5588 equality-expression
5589 and-expression & equality-expression
5590
5591 exclusive-or-expression:
5592 and-expression
5593 exclusive-or-expression ^ and-expression
5594
5595 inclusive-or-expression:
5596 exclusive-or-expression
5597 inclusive-or-expression | exclusive-or-expression
5598
5599 logical-and-expression:
5600 inclusive-or-expression
5601 logical-and-expression && inclusive-or-expression
5602
5603 logical-or-expression:
5604 logical-and-expression
5605 logical-or-expression || logical-and-expression
5606
5607 All these are implemented with a single function like:
5608
5609 binary-expression:
5610 simple-cast-expression
5611 binary-expression <token> binary-expression
5612
5613 CAST_P is true if this expression is the target of a cast.
5614
5615 The binops_by_token map is used to get the tree codes for each <token> type.
5616 binary-expressions are associated according to a precedence table. */
5617
5618 #define TOKEN_PRECEDENCE(token) \
5619 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5620 ? PREC_NOT_OPERATOR \
5621 : binops_by_token[token->type].prec)
5622
5623 static tree
5624 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5625 {
5626 cp_parser_expression_stack stack;
5627 cp_parser_expression_stack_entry *sp = &stack[0];
5628 tree lhs, rhs;
5629 cp_token *token;
5630 enum tree_code tree_type;
5631 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5632 bool overloaded_p;
5633
5634 /* Parse the first expression. */
5635 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5636
5637 for (;;)
5638 {
5639 /* Get an operator token. */
5640 token = cp_lexer_peek_token (parser->lexer);
5641
5642 new_prec = TOKEN_PRECEDENCE (token);
5643
5644 /* Popping an entry off the stack means we completed a subexpression:
5645 - either we found a token which is not an operator (`>' where it is not
5646 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5647 will happen repeatedly;
5648 - or, we found an operator which has lower priority. This is the case
5649 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5650 parsing `3 * 4'. */
5651 if (new_prec <= prec)
5652 {
5653 if (sp == stack)
5654 break;
5655 else
5656 goto pop;
5657 }
5658
5659 get_rhs:
5660 tree_type = binops_by_token[token->type].tree_type;
5661
5662 /* We used the operator token. */
5663 cp_lexer_consume_token (parser->lexer);
5664
5665 /* Extract another operand. It may be the RHS of this expression
5666 or the LHS of a new, higher priority expression. */
5667 rhs = cp_parser_simple_cast_expression (parser);
5668
5669 /* Get another operator token. Look up its precedence to avoid
5670 building a useless (immediately popped) stack entry for common
5671 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5672 token = cp_lexer_peek_token (parser->lexer);
5673 lookahead_prec = TOKEN_PRECEDENCE (token);
5674 if (lookahead_prec > new_prec)
5675 {
5676 /* ... and prepare to parse the RHS of the new, higher priority
5677 expression. Since precedence levels on the stack are
5678 monotonically increasing, we do not have to care about
5679 stack overflows. */
5680 sp->prec = prec;
5681 sp->tree_type = tree_type;
5682 sp->lhs = lhs;
5683 sp++;
5684 lhs = rhs;
5685 prec = new_prec;
5686 new_prec = lookahead_prec;
5687 goto get_rhs;
5688
5689 pop:
5690 /* If the stack is not empty, we have parsed into LHS the right side
5691 (`4' in the example above) of an expression we had suspended.
5692 We can use the information on the stack to recover the LHS (`3')
5693 from the stack together with the tree code (`MULT_EXPR'), and
5694 the precedence of the higher level subexpression
5695 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5696 which will be used to actually build the additive expression. */
5697 --sp;
5698 prec = sp->prec;
5699 tree_type = sp->tree_type;
5700 rhs = lhs;
5701 lhs = sp->lhs;
5702 }
5703
5704 overloaded_p = false;
5705 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5706
5707 /* If the binary operator required the use of an overloaded operator,
5708 then this expression cannot be an integral constant-expression.
5709 An overloaded operator can be used even if both operands are
5710 otherwise permissible in an integral constant-expression if at
5711 least one of the operands is of enumeration type. */
5712
5713 if (overloaded_p
5714 && (cp_parser_non_integral_constant_expression
5715 (parser, "calls to overloaded operators")))
5716 return error_mark_node;
5717 }
5718
5719 return lhs;
5720 }
5721
5722
5723 /* Parse the `? expression : assignment-expression' part of a
5724 conditional-expression. The LOGICAL_OR_EXPR is the
5725 logical-or-expression that started the conditional-expression.
5726 Returns a representation of the entire conditional-expression.
5727
5728 This routine is used by cp_parser_assignment_expression.
5729
5730 ? expression : assignment-expression
5731
5732 GNU Extensions:
5733
5734 ? : assignment-expression */
5735
5736 static tree
5737 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5738 {
5739 tree expr;
5740 tree assignment_expr;
5741
5742 /* Consume the `?' token. */
5743 cp_lexer_consume_token (parser->lexer);
5744 if (cp_parser_allow_gnu_extensions_p (parser)
5745 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5746 /* Implicit true clause. */
5747 expr = NULL_TREE;
5748 else
5749 /* Parse the expression. */
5750 expr = cp_parser_expression (parser, /*cast_p=*/false);
5751
5752 /* The next token should be a `:'. */
5753 cp_parser_require (parser, CPP_COLON, "`:'");
5754 /* Parse the assignment-expression. */
5755 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5756
5757 /* Build the conditional-expression. */
5758 return build_x_conditional_expr (logical_or_expr,
5759 expr,
5760 assignment_expr);
5761 }
5762
5763 /* Parse an assignment-expression.
5764
5765 assignment-expression:
5766 conditional-expression
5767 logical-or-expression assignment-operator assignment_expression
5768 throw-expression
5769
5770 CAST_P is true if this expression is the target of a cast.
5771
5772 Returns a representation for the expression. */
5773
5774 static tree
5775 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5776 {
5777 tree expr;
5778
5779 /* If the next token is the `throw' keyword, then we're looking at
5780 a throw-expression. */
5781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5782 expr = cp_parser_throw_expression (parser);
5783 /* Otherwise, it must be that we are looking at a
5784 logical-or-expression. */
5785 else
5786 {
5787 /* Parse the binary expressions (logical-or-expression). */
5788 expr = cp_parser_binary_expression (parser, cast_p);
5789 /* If the next token is a `?' then we're actually looking at a
5790 conditional-expression. */
5791 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5792 return cp_parser_question_colon_clause (parser, expr);
5793 else
5794 {
5795 enum tree_code assignment_operator;
5796
5797 /* If it's an assignment-operator, we're using the second
5798 production. */
5799 assignment_operator
5800 = cp_parser_assignment_operator_opt (parser);
5801 if (assignment_operator != ERROR_MARK)
5802 {
5803 tree rhs;
5804
5805 /* Parse the right-hand side of the assignment. */
5806 rhs = cp_parser_assignment_expression (parser, cast_p);
5807 /* An assignment may not appear in a
5808 constant-expression. */
5809 if (cp_parser_non_integral_constant_expression (parser,
5810 "an assignment"))
5811 return error_mark_node;
5812 /* Build the assignment expression. */
5813 expr = build_x_modify_expr (expr,
5814 assignment_operator,
5815 rhs);
5816 }
5817 }
5818 }
5819
5820 return expr;
5821 }
5822
5823 /* Parse an (optional) assignment-operator.
5824
5825 assignment-operator: one of
5826 = *= /= %= += -= >>= <<= &= ^= |=
5827
5828 GNU Extension:
5829
5830 assignment-operator: one of
5831 <?= >?=
5832
5833 If the next token is an assignment operator, the corresponding tree
5834 code is returned, and the token is consumed. For example, for
5835 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5836 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5837 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5838 operator, ERROR_MARK is returned. */
5839
5840 static enum tree_code
5841 cp_parser_assignment_operator_opt (cp_parser* parser)
5842 {
5843 enum tree_code op;
5844 cp_token *token;
5845
5846 /* Peek at the next toen. */
5847 token = cp_lexer_peek_token (parser->lexer);
5848
5849 switch (token->type)
5850 {
5851 case CPP_EQ:
5852 op = NOP_EXPR;
5853 break;
5854
5855 case CPP_MULT_EQ:
5856 op = MULT_EXPR;
5857 break;
5858
5859 case CPP_DIV_EQ:
5860 op = TRUNC_DIV_EXPR;
5861 break;
5862
5863 case CPP_MOD_EQ:
5864 op = TRUNC_MOD_EXPR;
5865 break;
5866
5867 case CPP_PLUS_EQ:
5868 op = PLUS_EXPR;
5869 break;
5870
5871 case CPP_MINUS_EQ:
5872 op = MINUS_EXPR;
5873 break;
5874
5875 case CPP_RSHIFT_EQ:
5876 op = RSHIFT_EXPR;
5877 break;
5878
5879 case CPP_LSHIFT_EQ:
5880 op = LSHIFT_EXPR;
5881 break;
5882
5883 case CPP_AND_EQ:
5884 op = BIT_AND_EXPR;
5885 break;
5886
5887 case CPP_XOR_EQ:
5888 op = BIT_XOR_EXPR;
5889 break;
5890
5891 case CPP_OR_EQ:
5892 op = BIT_IOR_EXPR;
5893 break;
5894
5895 default:
5896 /* Nothing else is an assignment operator. */
5897 op = ERROR_MARK;
5898 }
5899
5900 /* If it was an assignment operator, consume it. */
5901 if (op != ERROR_MARK)
5902 cp_lexer_consume_token (parser->lexer);
5903
5904 return op;
5905 }
5906
5907 /* Parse an expression.
5908
5909 expression:
5910 assignment-expression
5911 expression , assignment-expression
5912
5913 CAST_P is true if this expression is the target of a cast.
5914
5915 Returns a representation of the expression. */
5916
5917 static tree
5918 cp_parser_expression (cp_parser* parser, bool cast_p)
5919 {
5920 tree expression = NULL_TREE;
5921
5922 while (true)
5923 {
5924 tree assignment_expression;
5925
5926 /* Parse the next assignment-expression. */
5927 assignment_expression
5928 = cp_parser_assignment_expression (parser, cast_p);
5929 /* If this is the first assignment-expression, we can just
5930 save it away. */
5931 if (!expression)
5932 expression = assignment_expression;
5933 else
5934 expression = build_x_compound_expr (expression,
5935 assignment_expression);
5936 /* If the next token is not a comma, then we are done with the
5937 expression. */
5938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5939 break;
5940 /* Consume the `,'. */
5941 cp_lexer_consume_token (parser->lexer);
5942 /* A comma operator cannot appear in a constant-expression. */
5943 if (cp_parser_non_integral_constant_expression (parser,
5944 "a comma operator"))
5945 expression = error_mark_node;
5946 }
5947
5948 return expression;
5949 }
5950
5951 /* Parse a constant-expression.
5952
5953 constant-expression:
5954 conditional-expression
5955
5956 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5957 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5958 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5959 is false, NON_CONSTANT_P should be NULL. */
5960
5961 static tree
5962 cp_parser_constant_expression (cp_parser* parser,
5963 bool allow_non_constant_p,
5964 bool *non_constant_p)
5965 {
5966 bool saved_integral_constant_expression_p;
5967 bool saved_allow_non_integral_constant_expression_p;
5968 bool saved_non_integral_constant_expression_p;
5969 tree expression;
5970
5971 /* It might seem that we could simply parse the
5972 conditional-expression, and then check to see if it were
5973 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5974 one that the compiler can figure out is constant, possibly after
5975 doing some simplifications or optimizations. The standard has a
5976 precise definition of constant-expression, and we must honor
5977 that, even though it is somewhat more restrictive.
5978
5979 For example:
5980
5981 int i[(2, 3)];
5982
5983 is not a legal declaration, because `(2, 3)' is not a
5984 constant-expression. The `,' operator is forbidden in a
5985 constant-expression. However, GCC's constant-folding machinery
5986 will fold this operation to an INTEGER_CST for `3'. */
5987
5988 /* Save the old settings. */
5989 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5990 saved_allow_non_integral_constant_expression_p
5991 = parser->allow_non_integral_constant_expression_p;
5992 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5993 /* We are now parsing a constant-expression. */
5994 parser->integral_constant_expression_p = true;
5995 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5996 parser->non_integral_constant_expression_p = false;
5997 /* Although the grammar says "conditional-expression", we parse an
5998 "assignment-expression", which also permits "throw-expression"
5999 and the use of assignment operators. In the case that
6000 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6001 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6002 actually essential that we look for an assignment-expression.
6003 For example, cp_parser_initializer_clauses uses this function to
6004 determine whether a particular assignment-expression is in fact
6005 constant. */
6006 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6007 /* Restore the old settings. */
6008 parser->integral_constant_expression_p
6009 = saved_integral_constant_expression_p;
6010 parser->allow_non_integral_constant_expression_p
6011 = saved_allow_non_integral_constant_expression_p;
6012 if (allow_non_constant_p)
6013 *non_constant_p = parser->non_integral_constant_expression_p;
6014 else if (parser->non_integral_constant_expression_p)
6015 expression = error_mark_node;
6016 parser->non_integral_constant_expression_p
6017 = saved_non_integral_constant_expression_p;
6018
6019 return expression;
6020 }
6021
6022 /* Parse __builtin_offsetof.
6023
6024 offsetof-expression:
6025 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6026
6027 offsetof-member-designator:
6028 id-expression
6029 | offsetof-member-designator "." id-expression
6030 | offsetof-member-designator "[" expression "]" */
6031
6032 static tree
6033 cp_parser_builtin_offsetof (cp_parser *parser)
6034 {
6035 int save_ice_p, save_non_ice_p;
6036 tree type, expr;
6037 cp_id_kind dummy;
6038
6039 /* We're about to accept non-integral-constant things, but will
6040 definitely yield an integral constant expression. Save and
6041 restore these values around our local parsing. */
6042 save_ice_p = parser->integral_constant_expression_p;
6043 save_non_ice_p = parser->non_integral_constant_expression_p;
6044
6045 /* Consume the "__builtin_offsetof" token. */
6046 cp_lexer_consume_token (parser->lexer);
6047 /* Consume the opening `('. */
6048 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6049 /* Parse the type-id. */
6050 type = cp_parser_type_id (parser);
6051 /* Look for the `,'. */
6052 cp_parser_require (parser, CPP_COMMA, "`,'");
6053
6054 /* Build the (type *)null that begins the traditional offsetof macro. */
6055 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6056
6057 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6058 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6059 true, &dummy);
6060 while (true)
6061 {
6062 cp_token *token = cp_lexer_peek_token (parser->lexer);
6063 switch (token->type)
6064 {
6065 case CPP_OPEN_SQUARE:
6066 /* offsetof-member-designator "[" expression "]" */
6067 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6068 break;
6069
6070 case CPP_DOT:
6071 /* offsetof-member-designator "." identifier */
6072 cp_lexer_consume_token (parser->lexer);
6073 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6074 true, &dummy);
6075 break;
6076
6077 case CPP_CLOSE_PAREN:
6078 /* Consume the ")" token. */
6079 cp_lexer_consume_token (parser->lexer);
6080 goto success;
6081
6082 default:
6083 /* Error. We know the following require will fail, but
6084 that gives the proper error message. */
6085 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6086 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6087 expr = error_mark_node;
6088 goto failure;
6089 }
6090 }
6091
6092 success:
6093 /* If we're processing a template, we can't finish the semantics yet.
6094 Otherwise we can fold the entire expression now. */
6095 if (processing_template_decl)
6096 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6097 else
6098 expr = finish_offsetof (expr);
6099
6100 failure:
6101 parser->integral_constant_expression_p = save_ice_p;
6102 parser->non_integral_constant_expression_p = save_non_ice_p;
6103
6104 return expr;
6105 }
6106
6107 /* Statements [gram.stmt.stmt] */
6108
6109 /* Parse a statement.
6110
6111 statement:
6112 labeled-statement
6113 expression-statement
6114 compound-statement
6115 selection-statement
6116 iteration-statement
6117 jump-statement
6118 declaration-statement
6119 try-block
6120
6121 IN_COMPOUND is true when the statement is nested inside a
6122 cp_parser_compound_statement; this matters for certain pragmas. */
6123
6124 static void
6125 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6126 bool in_compound)
6127 {
6128 tree statement;
6129 cp_token *token;
6130 location_t statement_location;
6131
6132 restart:
6133 /* There is no statement yet. */
6134 statement = NULL_TREE;
6135 /* Peek at the next token. */
6136 token = cp_lexer_peek_token (parser->lexer);
6137 /* Remember the location of the first token in the statement. */
6138 statement_location = token->location;
6139 /* If this is a keyword, then that will often determine what kind of
6140 statement we have. */
6141 if (token->type == CPP_KEYWORD)
6142 {
6143 enum rid keyword = token->keyword;
6144
6145 switch (keyword)
6146 {
6147 case RID_CASE:
6148 case RID_DEFAULT:
6149 /* Looks like a labeled-statement with a case label.
6150 Parse the label, and then use tail recursion to parse
6151 the statement. */
6152 cp_parser_label_for_labeled_statement (parser);
6153 goto restart;
6154
6155 case RID_IF:
6156 case RID_SWITCH:
6157 statement = cp_parser_selection_statement (parser);
6158 break;
6159
6160 case RID_WHILE:
6161 case RID_DO:
6162 case RID_FOR:
6163 statement = cp_parser_iteration_statement (parser);
6164 break;
6165
6166 case RID_BREAK:
6167 case RID_CONTINUE:
6168 case RID_RETURN:
6169 case RID_GOTO:
6170 statement = cp_parser_jump_statement (parser);
6171 break;
6172
6173 /* Objective-C++ exception-handling constructs. */
6174 case RID_AT_TRY:
6175 case RID_AT_CATCH:
6176 case RID_AT_FINALLY:
6177 case RID_AT_SYNCHRONIZED:
6178 case RID_AT_THROW:
6179 statement = cp_parser_objc_statement (parser);
6180 break;
6181
6182 case RID_TRY:
6183 statement = cp_parser_try_block (parser);
6184 break;
6185
6186 default:
6187 /* It might be a keyword like `int' that can start a
6188 declaration-statement. */
6189 break;
6190 }
6191 }
6192 else if (token->type == CPP_NAME)
6193 {
6194 /* If the next token is a `:', then we are looking at a
6195 labeled-statement. */
6196 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6197 if (token->type == CPP_COLON)
6198 {
6199 /* Looks like a labeled-statement with an ordinary label.
6200 Parse the label, and then use tail recursion to parse
6201 the statement. */
6202 cp_parser_label_for_labeled_statement (parser);
6203 goto restart;
6204 }
6205 }
6206 /* Anything that starts with a `{' must be a compound-statement. */
6207 else if (token->type == CPP_OPEN_BRACE)
6208 statement = cp_parser_compound_statement (parser, NULL, false);
6209 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6210 a statement all its own. */
6211 else if (token->type == CPP_PRAGMA)
6212 {
6213 /* Only certain OpenMP pragmas are attached to statements, and thus
6214 are considered statements themselves. All others are not. In
6215 the context of a compound, accept the pragma as a "statement" and
6216 return so that we can check for a close brace. Otherwise we
6217 require a real statement and must go back and read one. */
6218 if (in_compound)
6219 cp_parser_pragma (parser, pragma_compound);
6220 else if (!cp_parser_pragma (parser, pragma_stmt))
6221 goto restart;
6222 return;
6223 }
6224 else if (token->type == CPP_EOF)
6225 {
6226 cp_parser_error (parser, "expected statement");
6227 return;
6228 }
6229
6230 /* Everything else must be a declaration-statement or an
6231 expression-statement. Try for the declaration-statement
6232 first, unless we are looking at a `;', in which case we know that
6233 we have an expression-statement. */
6234 if (!statement)
6235 {
6236 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6237 {
6238 cp_parser_parse_tentatively (parser);
6239 /* Try to parse the declaration-statement. */
6240 cp_parser_declaration_statement (parser);
6241 /* If that worked, we're done. */
6242 if (cp_parser_parse_definitely (parser))
6243 return;
6244 }
6245 /* Look for an expression-statement instead. */
6246 statement = cp_parser_expression_statement (parser, in_statement_expr);
6247 }
6248
6249 /* Set the line number for the statement. */
6250 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6251 SET_EXPR_LOCATION (statement, statement_location);
6252 }
6253
6254 /* Parse the label for a labeled-statement, i.e.
6255
6256 identifier :
6257 case constant-expression :
6258 default :
6259
6260 GNU Extension:
6261 case constant-expression ... constant-expression : statement
6262
6263 When a label is parsed without errors, the label is added to the
6264 parse tree by the finish_* functions, so this function doesn't
6265 have to return the label. */
6266
6267 static void
6268 cp_parser_label_for_labeled_statement (cp_parser* parser)
6269 {
6270 cp_token *token;
6271
6272 /* The next token should be an identifier. */
6273 token = cp_lexer_peek_token (parser->lexer);
6274 if (token->type != CPP_NAME
6275 && token->type != CPP_KEYWORD)
6276 {
6277 cp_parser_error (parser, "expected labeled-statement");
6278 return;
6279 }
6280
6281 switch (token->keyword)
6282 {
6283 case RID_CASE:
6284 {
6285 tree expr, expr_hi;
6286 cp_token *ellipsis;
6287
6288 /* Consume the `case' token. */
6289 cp_lexer_consume_token (parser->lexer);
6290 /* Parse the constant-expression. */
6291 expr = cp_parser_constant_expression (parser,
6292 /*allow_non_constant_p=*/false,
6293 NULL);
6294
6295 ellipsis = cp_lexer_peek_token (parser->lexer);
6296 if (ellipsis->type == CPP_ELLIPSIS)
6297 {
6298 /* Consume the `...' token. */
6299 cp_lexer_consume_token (parser->lexer);
6300 expr_hi =
6301 cp_parser_constant_expression (parser,
6302 /*allow_non_constant_p=*/false,
6303 NULL);
6304 /* We don't need to emit warnings here, as the common code
6305 will do this for us. */
6306 }
6307 else
6308 expr_hi = NULL_TREE;
6309
6310 if (parser->in_switch_statement_p)
6311 finish_case_label (expr, expr_hi);
6312 else
6313 error ("case label %qE not within a switch statement", expr);
6314 }
6315 break;
6316
6317 case RID_DEFAULT:
6318 /* Consume the `default' token. */
6319 cp_lexer_consume_token (parser->lexer);
6320
6321 if (parser->in_switch_statement_p)
6322 finish_case_label (NULL_TREE, NULL_TREE);
6323 else
6324 error ("case label not within a switch statement");
6325 break;
6326
6327 default:
6328 /* Anything else must be an ordinary label. */
6329 finish_label_stmt (cp_parser_identifier (parser));
6330 break;
6331 }
6332
6333 /* Require the `:' token. */
6334 cp_parser_require (parser, CPP_COLON, "`:'");
6335 }
6336
6337 /* Parse an expression-statement.
6338
6339 expression-statement:
6340 expression [opt] ;
6341
6342 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6343 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6344 indicates whether this expression-statement is part of an
6345 expression statement. */
6346
6347 static tree
6348 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6349 {
6350 tree statement = NULL_TREE;
6351
6352 /* If the next token is a ';', then there is no expression
6353 statement. */
6354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6355 statement = cp_parser_expression (parser, /*cast_p=*/false);
6356
6357 /* Consume the final `;'. */
6358 cp_parser_consume_semicolon_at_end_of_statement (parser);
6359
6360 if (in_statement_expr
6361 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6362 /* This is the final expression statement of a statement
6363 expression. */
6364 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6365 else if (statement)
6366 statement = finish_expr_stmt (statement);
6367 else
6368 finish_stmt ();
6369
6370 return statement;
6371 }
6372
6373 /* Parse a compound-statement.
6374
6375 compound-statement:
6376 { statement-seq [opt] }
6377
6378 Returns a tree representing the statement. */
6379
6380 static tree
6381 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6382 bool in_try)
6383 {
6384 tree compound_stmt;
6385
6386 /* Consume the `{'. */
6387 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6388 return error_mark_node;
6389 /* Begin the compound-statement. */
6390 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6391 /* Parse an (optional) statement-seq. */
6392 cp_parser_statement_seq_opt (parser, in_statement_expr);
6393 /* Finish the compound-statement. */
6394 finish_compound_stmt (compound_stmt);
6395 /* Consume the `}'. */
6396 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6397
6398 return compound_stmt;
6399 }
6400
6401 /* Parse an (optional) statement-seq.
6402
6403 statement-seq:
6404 statement
6405 statement-seq [opt] statement */
6406
6407 static void
6408 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6409 {
6410 /* Scan statements until there aren't any more. */
6411 while (true)
6412 {
6413 cp_token *token = cp_lexer_peek_token (parser->lexer);
6414
6415 /* If we're looking at a `}', then we've run out of statements. */
6416 if (token->type == CPP_CLOSE_BRACE
6417 || token->type == CPP_EOF
6418 || token->type == CPP_PRAGMA_EOL)
6419 break;
6420
6421 /* Parse the statement. */
6422 cp_parser_statement (parser, in_statement_expr, true);
6423 }
6424 }
6425
6426 /* Parse a selection-statement.
6427
6428 selection-statement:
6429 if ( condition ) statement
6430 if ( condition ) statement else statement
6431 switch ( condition ) statement
6432
6433 Returns the new IF_STMT or SWITCH_STMT. */
6434
6435 static tree
6436 cp_parser_selection_statement (cp_parser* parser)
6437 {
6438 cp_token *token;
6439 enum rid keyword;
6440
6441 /* Peek at the next token. */
6442 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6443
6444 /* See what kind of keyword it is. */
6445 keyword = token->keyword;
6446 switch (keyword)
6447 {
6448 case RID_IF:
6449 case RID_SWITCH:
6450 {
6451 tree statement;
6452 tree condition;
6453
6454 /* Look for the `('. */
6455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6456 {
6457 cp_parser_skip_to_end_of_statement (parser);
6458 return error_mark_node;
6459 }
6460
6461 /* Begin the selection-statement. */
6462 if (keyword == RID_IF)
6463 statement = begin_if_stmt ();
6464 else
6465 statement = begin_switch_stmt ();
6466
6467 /* Parse the condition. */
6468 condition = cp_parser_condition (parser);
6469 /* Look for the `)'. */
6470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6471 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6472 /*consume_paren=*/true);
6473
6474 if (keyword == RID_IF)
6475 {
6476 /* Add the condition. */
6477 finish_if_stmt_cond (condition, statement);
6478
6479 /* Parse the then-clause. */
6480 cp_parser_implicitly_scoped_statement (parser);
6481 finish_then_clause (statement);
6482
6483 /* If the next token is `else', parse the else-clause. */
6484 if (cp_lexer_next_token_is_keyword (parser->lexer,
6485 RID_ELSE))
6486 {
6487 /* Consume the `else' keyword. */
6488 cp_lexer_consume_token (parser->lexer);
6489 begin_else_clause (statement);
6490 /* Parse the else-clause. */
6491 cp_parser_implicitly_scoped_statement (parser);
6492 finish_else_clause (statement);
6493 }
6494
6495 /* Now we're all done with the if-statement. */
6496 finish_if_stmt (statement);
6497 }
6498 else
6499 {
6500 bool in_switch_statement_p;
6501 unsigned char in_statement;
6502
6503 /* Add the condition. */
6504 finish_switch_cond (condition, statement);
6505
6506 /* Parse the body of the switch-statement. */
6507 in_switch_statement_p = parser->in_switch_statement_p;
6508 in_statement = parser->in_statement;
6509 parser->in_switch_statement_p = true;
6510 parser->in_statement |= IN_SWITCH_STMT;
6511 cp_parser_implicitly_scoped_statement (parser);
6512 parser->in_switch_statement_p = in_switch_statement_p;
6513 parser->in_statement = in_statement;
6514
6515 /* Now we're all done with the switch-statement. */
6516 finish_switch_stmt (statement);
6517 }
6518
6519 return statement;
6520 }
6521 break;
6522
6523 default:
6524 cp_parser_error (parser, "expected selection-statement");
6525 return error_mark_node;
6526 }
6527 }
6528
6529 /* Parse a condition.
6530
6531 condition:
6532 expression
6533 type-specifier-seq declarator = assignment-expression
6534
6535 GNU Extension:
6536
6537 condition:
6538 type-specifier-seq declarator asm-specification [opt]
6539 attributes [opt] = assignment-expression
6540
6541 Returns the expression that should be tested. */
6542
6543 static tree
6544 cp_parser_condition (cp_parser* parser)
6545 {
6546 cp_decl_specifier_seq type_specifiers;
6547 const char *saved_message;
6548
6549 /* Try the declaration first. */
6550 cp_parser_parse_tentatively (parser);
6551 /* New types are not allowed in the type-specifier-seq for a
6552 condition. */
6553 saved_message = parser->type_definition_forbidden_message;
6554 parser->type_definition_forbidden_message
6555 = "types may not be defined in conditions";
6556 /* Parse the type-specifier-seq. */
6557 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6558 &type_specifiers);
6559 /* Restore the saved message. */
6560 parser->type_definition_forbidden_message = saved_message;
6561 /* If all is well, we might be looking at a declaration. */
6562 if (!cp_parser_error_occurred (parser))
6563 {
6564 tree decl;
6565 tree asm_specification;
6566 tree attributes;
6567 cp_declarator *declarator;
6568 tree initializer = NULL_TREE;
6569
6570 /* Parse the declarator. */
6571 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6572 /*ctor_dtor_or_conv_p=*/NULL,
6573 /*parenthesized_p=*/NULL,
6574 /*member_p=*/false);
6575 /* Parse the attributes. */
6576 attributes = cp_parser_attributes_opt (parser);
6577 /* Parse the asm-specification. */
6578 asm_specification = cp_parser_asm_specification_opt (parser);
6579 /* If the next token is not an `=', then we might still be
6580 looking at an expression. For example:
6581
6582 if (A(a).x)
6583
6584 looks like a decl-specifier-seq and a declarator -- but then
6585 there is no `=', so this is an expression. */
6586 cp_parser_require (parser, CPP_EQ, "`='");
6587 /* If we did see an `=', then we are looking at a declaration
6588 for sure. */
6589 if (cp_parser_parse_definitely (parser))
6590 {
6591 tree pushed_scope;
6592 bool non_constant_p;
6593
6594 /* Create the declaration. */
6595 decl = start_decl (declarator, &type_specifiers,
6596 /*initialized_p=*/true,
6597 attributes, /*prefix_attributes=*/NULL_TREE,
6598 &pushed_scope);
6599 /* Parse the assignment-expression. */
6600 initializer
6601 = cp_parser_constant_expression (parser,
6602 /*allow_non_constant_p=*/true,
6603 &non_constant_p);
6604 if (!non_constant_p)
6605 initializer = fold_non_dependent_expr (initializer);
6606
6607 /* Process the initializer. */
6608 cp_finish_decl (decl,
6609 initializer, !non_constant_p,
6610 asm_specification,
6611 LOOKUP_ONLYCONVERTING);
6612
6613 if (pushed_scope)
6614 pop_scope (pushed_scope);
6615
6616 return convert_from_reference (decl);
6617 }
6618 }
6619 /* If we didn't even get past the declarator successfully, we are
6620 definitely not looking at a declaration. */
6621 else
6622 cp_parser_abort_tentative_parse (parser);
6623
6624 /* Otherwise, we are looking at an expression. */
6625 return cp_parser_expression (parser, /*cast_p=*/false);
6626 }
6627
6628 /* Parse an iteration-statement.
6629
6630 iteration-statement:
6631 while ( condition ) statement
6632 do statement while ( expression ) ;
6633 for ( for-init-statement condition [opt] ; expression [opt] )
6634 statement
6635
6636 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6637
6638 static tree
6639 cp_parser_iteration_statement (cp_parser* parser)
6640 {
6641 cp_token *token;
6642 enum rid keyword;
6643 tree statement;
6644 unsigned char in_statement;
6645
6646 /* Peek at the next token. */
6647 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6648 if (!token)
6649 return error_mark_node;
6650
6651 /* Remember whether or not we are already within an iteration
6652 statement. */
6653 in_statement = parser->in_statement;
6654
6655 /* See what kind of keyword it is. */
6656 keyword = token->keyword;
6657 switch (keyword)
6658 {
6659 case RID_WHILE:
6660 {
6661 tree condition;
6662
6663 /* Begin the while-statement. */
6664 statement = begin_while_stmt ();
6665 /* Look for the `('. */
6666 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6667 /* Parse the condition. */
6668 condition = cp_parser_condition (parser);
6669 finish_while_stmt_cond (condition, statement);
6670 /* Look for the `)'. */
6671 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6672 /* Parse the dependent statement. */
6673 parser->in_statement = IN_ITERATION_STMT;
6674 cp_parser_already_scoped_statement (parser);
6675 parser->in_statement = in_statement;
6676 /* We're done with the while-statement. */
6677 finish_while_stmt (statement);
6678 }
6679 break;
6680
6681 case RID_DO:
6682 {
6683 tree expression;
6684
6685 /* Begin the do-statement. */
6686 statement = begin_do_stmt ();
6687 /* Parse the body of the do-statement. */
6688 parser->in_statement = IN_ITERATION_STMT;
6689 cp_parser_implicitly_scoped_statement (parser);
6690 parser->in_statement = in_statement;
6691 finish_do_body (statement);
6692 /* Look for the `while' keyword. */
6693 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6694 /* Look for the `('. */
6695 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6696 /* Parse the expression. */
6697 expression = cp_parser_expression (parser, /*cast_p=*/false);
6698 /* We're done with the do-statement. */
6699 finish_do_stmt (expression, statement);
6700 /* Look for the `)'. */
6701 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6702 /* Look for the `;'. */
6703 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6704 }
6705 break;
6706
6707 case RID_FOR:
6708 {
6709 tree condition = NULL_TREE;
6710 tree expression = NULL_TREE;
6711
6712 /* Begin the for-statement. */
6713 statement = begin_for_stmt ();
6714 /* Look for the `('. */
6715 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6716 /* Parse the initialization. */
6717 cp_parser_for_init_statement (parser);
6718 finish_for_init_stmt (statement);
6719
6720 /* If there's a condition, process it. */
6721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6722 condition = cp_parser_condition (parser);
6723 finish_for_cond (condition, statement);
6724 /* Look for the `;'. */
6725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6726
6727 /* If there's an expression, process it. */
6728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6729 expression = cp_parser_expression (parser, /*cast_p=*/false);
6730 finish_for_expr (expression, statement);
6731 /* Look for the `)'. */
6732 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6733
6734 /* Parse the body of the for-statement. */
6735 parser->in_statement = IN_ITERATION_STMT;
6736 cp_parser_already_scoped_statement (parser);
6737 parser->in_statement = in_statement;
6738
6739 /* We're done with the for-statement. */
6740 finish_for_stmt (statement);
6741 }
6742 break;
6743
6744 default:
6745 cp_parser_error (parser, "expected iteration-statement");
6746 statement = error_mark_node;
6747 break;
6748 }
6749
6750 return statement;
6751 }
6752
6753 /* Parse a for-init-statement.
6754
6755 for-init-statement:
6756 expression-statement
6757 simple-declaration */
6758
6759 static void
6760 cp_parser_for_init_statement (cp_parser* parser)
6761 {
6762 /* If the next token is a `;', then we have an empty
6763 expression-statement. Grammatically, this is also a
6764 simple-declaration, but an invalid one, because it does not
6765 declare anything. Therefore, if we did not handle this case
6766 specially, we would issue an error message about an invalid
6767 declaration. */
6768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6769 {
6770 /* We're going to speculatively look for a declaration, falling back
6771 to an expression, if necessary. */
6772 cp_parser_parse_tentatively (parser);
6773 /* Parse the declaration. */
6774 cp_parser_simple_declaration (parser,
6775 /*function_definition_allowed_p=*/false);
6776 /* If the tentative parse failed, then we shall need to look for an
6777 expression-statement. */
6778 if (cp_parser_parse_definitely (parser))
6779 return;
6780 }
6781
6782 cp_parser_expression_statement (parser, false);
6783 }
6784
6785 /* Parse a jump-statement.
6786
6787 jump-statement:
6788 break ;
6789 continue ;
6790 return expression [opt] ;
6791 goto identifier ;
6792
6793 GNU extension:
6794
6795 jump-statement:
6796 goto * expression ;
6797
6798 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6799
6800 static tree
6801 cp_parser_jump_statement (cp_parser* parser)
6802 {
6803 tree statement = error_mark_node;
6804 cp_token *token;
6805 enum rid keyword;
6806
6807 /* Peek at the next token. */
6808 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6809 if (!token)
6810 return error_mark_node;
6811
6812 /* See what kind of keyword it is. */
6813 keyword = token->keyword;
6814 switch (keyword)
6815 {
6816 case RID_BREAK:
6817 switch (parser->in_statement)
6818 {
6819 case 0:
6820 error ("break statement not within loop or switch");
6821 break;
6822 default:
6823 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6824 || parser->in_statement == IN_ITERATION_STMT);
6825 statement = finish_break_stmt ();
6826 break;
6827 case IN_OMP_BLOCK:
6828 error ("invalid exit from OpenMP structured block");
6829 break;
6830 case IN_OMP_FOR:
6831 error ("break statement used with OpenMP for loop");
6832 break;
6833 }
6834 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6835 break;
6836
6837 case RID_CONTINUE:
6838 switch (parser->in_statement & ~IN_SWITCH_STMT)
6839 {
6840 case 0:
6841 error ("continue statement not within a loop");
6842 break;
6843 case IN_ITERATION_STMT:
6844 case IN_OMP_FOR:
6845 statement = finish_continue_stmt ();
6846 break;
6847 case IN_OMP_BLOCK:
6848 error ("invalid exit from OpenMP structured block");
6849 break;
6850 default:
6851 gcc_unreachable ();
6852 }
6853 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6854 break;
6855
6856 case RID_RETURN:
6857 {
6858 tree expr;
6859
6860 /* If the next token is a `;', then there is no
6861 expression. */
6862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6863 expr = cp_parser_expression (parser, /*cast_p=*/false);
6864 else
6865 expr = NULL_TREE;
6866 /* Build the return-statement. */
6867 statement = finish_return_stmt (expr);
6868 /* Look for the final `;'. */
6869 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6870 }
6871 break;
6872
6873 case RID_GOTO:
6874 /* Create the goto-statement. */
6875 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6876 {
6877 /* Issue a warning about this use of a GNU extension. */
6878 if (pedantic)
6879 pedwarn ("ISO C++ forbids computed gotos");
6880 /* Consume the '*' token. */
6881 cp_lexer_consume_token (parser->lexer);
6882 /* Parse the dependent expression. */
6883 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6884 }
6885 else
6886 finish_goto_stmt (cp_parser_identifier (parser));
6887 /* Look for the final `;'. */
6888 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6889 break;
6890
6891 default:
6892 cp_parser_error (parser, "expected jump-statement");
6893 break;
6894 }
6895
6896 return statement;
6897 }
6898
6899 /* Parse a declaration-statement.
6900
6901 declaration-statement:
6902 block-declaration */
6903
6904 static void
6905 cp_parser_declaration_statement (cp_parser* parser)
6906 {
6907 void *p;
6908
6909 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6910 p = obstack_alloc (&declarator_obstack, 0);
6911
6912 /* Parse the block-declaration. */
6913 cp_parser_block_declaration (parser, /*statement_p=*/true);
6914
6915 /* Free any declarators allocated. */
6916 obstack_free (&declarator_obstack, p);
6917
6918 /* Finish off the statement. */
6919 finish_stmt ();
6920 }
6921
6922 /* Some dependent statements (like `if (cond) statement'), are
6923 implicitly in their own scope. In other words, if the statement is
6924 a single statement (as opposed to a compound-statement), it is
6925 none-the-less treated as if it were enclosed in braces. Any
6926 declarations appearing in the dependent statement are out of scope
6927 after control passes that point. This function parses a statement,
6928 but ensures that is in its own scope, even if it is not a
6929 compound-statement.
6930
6931 Returns the new statement. */
6932
6933 static tree
6934 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6935 {
6936 tree statement;
6937
6938 /* Mark if () ; with a special NOP_EXPR. */
6939 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6940 {
6941 cp_lexer_consume_token (parser->lexer);
6942 statement = add_stmt (build_empty_stmt ());
6943 }
6944 /* if a compound is opened, we simply parse the statement directly. */
6945 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6946 statement = cp_parser_compound_statement (parser, NULL, false);
6947 /* If the token is not a `{', then we must take special action. */
6948 else
6949 {
6950 /* Create a compound-statement. */
6951 statement = begin_compound_stmt (0);
6952 /* Parse the dependent-statement. */
6953 cp_parser_statement (parser, NULL_TREE, false);
6954 /* Finish the dummy compound-statement. */
6955 finish_compound_stmt (statement);
6956 }
6957
6958 /* Return the statement. */
6959 return statement;
6960 }
6961
6962 /* For some dependent statements (like `while (cond) statement'), we
6963 have already created a scope. Therefore, even if the dependent
6964 statement is a compound-statement, we do not want to create another
6965 scope. */
6966
6967 static void
6968 cp_parser_already_scoped_statement (cp_parser* parser)
6969 {
6970 /* If the token is a `{', then we must take special action. */
6971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6972 cp_parser_statement (parser, NULL_TREE, false);
6973 else
6974 {
6975 /* Avoid calling cp_parser_compound_statement, so that we
6976 don't create a new scope. Do everything else by hand. */
6977 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6978 cp_parser_statement_seq_opt (parser, NULL_TREE);
6979 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6980 }
6981 }
6982
6983 /* Declarations [gram.dcl.dcl] */
6984
6985 /* Parse an optional declaration-sequence.
6986
6987 declaration-seq:
6988 declaration
6989 declaration-seq declaration */
6990
6991 static void
6992 cp_parser_declaration_seq_opt (cp_parser* parser)
6993 {
6994 while (true)
6995 {
6996 cp_token *token;
6997
6998 token = cp_lexer_peek_token (parser->lexer);
6999
7000 if (token->type == CPP_CLOSE_BRACE
7001 || token->type == CPP_EOF
7002 || token->type == CPP_PRAGMA_EOL)
7003 break;
7004
7005 if (token->type == CPP_SEMICOLON)
7006 {
7007 /* A declaration consisting of a single semicolon is
7008 invalid. Allow it unless we're being pedantic. */
7009 cp_lexer_consume_token (parser->lexer);
7010 if (pedantic && !in_system_header)
7011 pedwarn ("extra %<;%>");
7012 continue;
7013 }
7014
7015 /* If we're entering or exiting a region that's implicitly
7016 extern "C", modify the lang context appropriately. */
7017 if (!parser->implicit_extern_c && token->implicit_extern_c)
7018 {
7019 push_lang_context (lang_name_c);
7020 parser->implicit_extern_c = true;
7021 }
7022 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7023 {
7024 pop_lang_context ();
7025 parser->implicit_extern_c = false;
7026 }
7027
7028 if (token->type == CPP_PRAGMA)
7029 {
7030 /* A top-level declaration can consist solely of a #pragma.
7031 A nested declaration cannot, so this is done here and not
7032 in cp_parser_declaration. (A #pragma at block scope is
7033 handled in cp_parser_statement.) */
7034 cp_parser_pragma (parser, pragma_external);
7035 continue;
7036 }
7037
7038 /* Parse the declaration itself. */
7039 cp_parser_declaration (parser);
7040 }
7041 }
7042
7043 /* Parse a declaration.
7044
7045 declaration:
7046 block-declaration
7047 function-definition
7048 template-declaration
7049 explicit-instantiation
7050 explicit-specialization
7051 linkage-specification
7052 namespace-definition
7053
7054 GNU extension:
7055
7056 declaration:
7057 __extension__ declaration */
7058
7059 static void
7060 cp_parser_declaration (cp_parser* parser)
7061 {
7062 cp_token token1;
7063 cp_token token2;
7064 int saved_pedantic;
7065 void *p;
7066
7067 /* Check for the `__extension__' keyword. */
7068 if (cp_parser_extension_opt (parser, &saved_pedantic))
7069 {
7070 /* Parse the qualified declaration. */
7071 cp_parser_declaration (parser);
7072 /* Restore the PEDANTIC flag. */
7073 pedantic = saved_pedantic;
7074
7075 return;
7076 }
7077
7078 /* Try to figure out what kind of declaration is present. */
7079 token1 = *cp_lexer_peek_token (parser->lexer);
7080
7081 if (token1.type != CPP_EOF)
7082 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7083 else
7084 {
7085 token2.type = CPP_EOF;
7086 token2.keyword = RID_MAX;
7087 }
7088
7089 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7090 p = obstack_alloc (&declarator_obstack, 0);
7091
7092 /* If the next token is `extern' and the following token is a string
7093 literal, then we have a linkage specification. */
7094 if (token1.keyword == RID_EXTERN
7095 && cp_parser_is_string_literal (&token2))
7096 cp_parser_linkage_specification (parser);
7097 /* If the next token is `template', then we have either a template
7098 declaration, an explicit instantiation, or an explicit
7099 specialization. */
7100 else if (token1.keyword == RID_TEMPLATE)
7101 {
7102 /* `template <>' indicates a template specialization. */
7103 if (token2.type == CPP_LESS
7104 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7105 cp_parser_explicit_specialization (parser);
7106 /* `template <' indicates a template declaration. */
7107 else if (token2.type == CPP_LESS)
7108 cp_parser_template_declaration (parser, /*member_p=*/false);
7109 /* Anything else must be an explicit instantiation. */
7110 else
7111 cp_parser_explicit_instantiation (parser);
7112 }
7113 /* If the next token is `export', then we have a template
7114 declaration. */
7115 else if (token1.keyword == RID_EXPORT)
7116 cp_parser_template_declaration (parser, /*member_p=*/false);
7117 /* If the next token is `extern', 'static' or 'inline' and the one
7118 after that is `template', we have a GNU extended explicit
7119 instantiation directive. */
7120 else if (cp_parser_allow_gnu_extensions_p (parser)
7121 && (token1.keyword == RID_EXTERN
7122 || token1.keyword == RID_STATIC
7123 || token1.keyword == RID_INLINE)
7124 && token2.keyword == RID_TEMPLATE)
7125 cp_parser_explicit_instantiation (parser);
7126 /* If the next token is `namespace', check for a named or unnamed
7127 namespace definition. */
7128 else if (token1.keyword == RID_NAMESPACE
7129 && (/* A named namespace definition. */
7130 (token2.type == CPP_NAME
7131 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7132 != CPP_EQ))
7133 /* An unnamed namespace definition. */
7134 || token2.type == CPP_OPEN_BRACE
7135 || token2.keyword == RID_ATTRIBUTE))
7136 cp_parser_namespace_definition (parser);
7137 /* Objective-C++ declaration/definition. */
7138 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7139 cp_parser_objc_declaration (parser);
7140 /* We must have either a block declaration or a function
7141 definition. */
7142 else
7143 /* Try to parse a block-declaration, or a function-definition. */
7144 cp_parser_block_declaration (parser, /*statement_p=*/false);
7145
7146 /* Free any declarators allocated. */
7147 obstack_free (&declarator_obstack, p);
7148 }
7149
7150 /* Parse a block-declaration.
7151
7152 block-declaration:
7153 simple-declaration
7154 asm-definition
7155 namespace-alias-definition
7156 using-declaration
7157 using-directive
7158
7159 GNU Extension:
7160
7161 block-declaration:
7162 __extension__ block-declaration
7163 label-declaration
7164
7165 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7166 part of a declaration-statement. */
7167
7168 static void
7169 cp_parser_block_declaration (cp_parser *parser,
7170 bool statement_p)
7171 {
7172 cp_token *token1;
7173 int saved_pedantic;
7174
7175 /* Check for the `__extension__' keyword. */
7176 if (cp_parser_extension_opt (parser, &saved_pedantic))
7177 {
7178 /* Parse the qualified declaration. */
7179 cp_parser_block_declaration (parser, statement_p);
7180 /* Restore the PEDANTIC flag. */
7181 pedantic = saved_pedantic;
7182
7183 return;
7184 }
7185
7186 /* Peek at the next token to figure out which kind of declaration is
7187 present. */
7188 token1 = cp_lexer_peek_token (parser->lexer);
7189
7190 /* If the next keyword is `asm', we have an asm-definition. */
7191 if (token1->keyword == RID_ASM)
7192 {
7193 if (statement_p)
7194 cp_parser_commit_to_tentative_parse (parser);
7195 cp_parser_asm_definition (parser);
7196 }
7197 /* If the next keyword is `namespace', we have a
7198 namespace-alias-definition. */
7199 else if (token1->keyword == RID_NAMESPACE)
7200 cp_parser_namespace_alias_definition (parser);
7201 /* If the next keyword is `using', we have either a
7202 using-declaration or a using-directive. */
7203 else if (token1->keyword == RID_USING)
7204 {
7205 cp_token *token2;
7206
7207 if (statement_p)
7208 cp_parser_commit_to_tentative_parse (parser);
7209 /* If the token after `using' is `namespace', then we have a
7210 using-directive. */
7211 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7212 if (token2->keyword == RID_NAMESPACE)
7213 cp_parser_using_directive (parser);
7214 /* Otherwise, it's a using-declaration. */
7215 else
7216 cp_parser_using_declaration (parser,
7217 /*access_declaration_p=*/false);
7218 }
7219 /* If the next keyword is `__label__' we have a label declaration. */
7220 else if (token1->keyword == RID_LABEL)
7221 {
7222 if (statement_p)
7223 cp_parser_commit_to_tentative_parse (parser);
7224 cp_parser_label_declaration (parser);
7225 }
7226 /* Anything else must be a simple-declaration. */
7227 else
7228 cp_parser_simple_declaration (parser, !statement_p);
7229 }
7230
7231 /* Parse a simple-declaration.
7232
7233 simple-declaration:
7234 decl-specifier-seq [opt] init-declarator-list [opt] ;
7235
7236 init-declarator-list:
7237 init-declarator
7238 init-declarator-list , init-declarator
7239
7240 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7241 function-definition as a simple-declaration. */
7242
7243 static void
7244 cp_parser_simple_declaration (cp_parser* parser,
7245 bool function_definition_allowed_p)
7246 {
7247 cp_decl_specifier_seq decl_specifiers;
7248 int declares_class_or_enum;
7249 bool saw_declarator;
7250
7251 /* Defer access checks until we know what is being declared; the
7252 checks for names appearing in the decl-specifier-seq should be
7253 done as if we were in the scope of the thing being declared. */
7254 push_deferring_access_checks (dk_deferred);
7255
7256 /* Parse the decl-specifier-seq. We have to keep track of whether
7257 or not the decl-specifier-seq declares a named class or
7258 enumeration type, since that is the only case in which the
7259 init-declarator-list is allowed to be empty.
7260
7261 [dcl.dcl]
7262
7263 In a simple-declaration, the optional init-declarator-list can be
7264 omitted only when declaring a class or enumeration, that is when
7265 the decl-specifier-seq contains either a class-specifier, an
7266 elaborated-type-specifier, or an enum-specifier. */
7267 cp_parser_decl_specifier_seq (parser,
7268 CP_PARSER_FLAGS_OPTIONAL,
7269 &decl_specifiers,
7270 &declares_class_or_enum);
7271 /* We no longer need to defer access checks. */
7272 stop_deferring_access_checks ();
7273
7274 /* In a block scope, a valid declaration must always have a
7275 decl-specifier-seq. By not trying to parse declarators, we can
7276 resolve the declaration/expression ambiguity more quickly. */
7277 if (!function_definition_allowed_p
7278 && !decl_specifiers.any_specifiers_p)
7279 {
7280 cp_parser_error (parser, "expected declaration");
7281 goto done;
7282 }
7283
7284 /* If the next two tokens are both identifiers, the code is
7285 erroneous. The usual cause of this situation is code like:
7286
7287 T t;
7288
7289 where "T" should name a type -- but does not. */
7290 if (!decl_specifiers.type
7291 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7292 {
7293 /* If parsing tentatively, we should commit; we really are
7294 looking at a declaration. */
7295 cp_parser_commit_to_tentative_parse (parser);
7296 /* Give up. */
7297 goto done;
7298 }
7299
7300 /* If we have seen at least one decl-specifier, and the next token
7301 is not a parenthesis, then we must be looking at a declaration.
7302 (After "int (" we might be looking at a functional cast.) */
7303 if (decl_specifiers.any_specifiers_p
7304 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7305 cp_parser_commit_to_tentative_parse (parser);
7306
7307 /* Keep going until we hit the `;' at the end of the simple
7308 declaration. */
7309 saw_declarator = false;
7310 while (cp_lexer_next_token_is_not (parser->lexer,
7311 CPP_SEMICOLON))
7312 {
7313 cp_token *token;
7314 bool function_definition_p;
7315 tree decl;
7316
7317 if (saw_declarator)
7318 {
7319 /* If we are processing next declarator, coma is expected */
7320 token = cp_lexer_peek_token (parser->lexer);
7321 gcc_assert (token->type == CPP_COMMA);
7322 cp_lexer_consume_token (parser->lexer);
7323 }
7324 else
7325 saw_declarator = true;
7326
7327 /* Parse the init-declarator. */
7328 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7329 /*checks=*/NULL_TREE,
7330 function_definition_allowed_p,
7331 /*member_p=*/false,
7332 declares_class_or_enum,
7333 &function_definition_p);
7334 /* If an error occurred while parsing tentatively, exit quickly.
7335 (That usually happens when in the body of a function; each
7336 statement is treated as a declaration-statement until proven
7337 otherwise.) */
7338 if (cp_parser_error_occurred (parser))
7339 goto done;
7340 /* Handle function definitions specially. */
7341 if (function_definition_p)
7342 {
7343 /* If the next token is a `,', then we are probably
7344 processing something like:
7345
7346 void f() {}, *p;
7347
7348 which is erroneous. */
7349 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7350 error ("mixing declarations and function-definitions is forbidden");
7351 /* Otherwise, we're done with the list of declarators. */
7352 else
7353 {
7354 pop_deferring_access_checks ();
7355 return;
7356 }
7357 }
7358 /* The next token should be either a `,' or a `;'. */
7359 token = cp_lexer_peek_token (parser->lexer);
7360 /* If it's a `,', there are more declarators to come. */
7361 if (token->type == CPP_COMMA)
7362 /* will be consumed next time around */;
7363 /* If it's a `;', we are done. */
7364 else if (token->type == CPP_SEMICOLON)
7365 break;
7366 /* Anything else is an error. */
7367 else
7368 {
7369 /* If we have already issued an error message we don't need
7370 to issue another one. */
7371 if (decl != error_mark_node
7372 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7373 cp_parser_error (parser, "expected %<,%> or %<;%>");
7374 /* Skip tokens until we reach the end of the statement. */
7375 cp_parser_skip_to_end_of_statement (parser);
7376 /* If the next token is now a `;', consume it. */
7377 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7378 cp_lexer_consume_token (parser->lexer);
7379 goto done;
7380 }
7381 /* After the first time around, a function-definition is not
7382 allowed -- even if it was OK at first. For example:
7383
7384 int i, f() {}
7385
7386 is not valid. */
7387 function_definition_allowed_p = false;
7388 }
7389
7390 /* Issue an error message if no declarators are present, and the
7391 decl-specifier-seq does not itself declare a class or
7392 enumeration. */
7393 if (!saw_declarator)
7394 {
7395 if (cp_parser_declares_only_class_p (parser))
7396 shadow_tag (&decl_specifiers);
7397 /* Perform any deferred access checks. */
7398 perform_deferred_access_checks ();
7399 }
7400
7401 /* Consume the `;'. */
7402 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7403
7404 done:
7405 pop_deferring_access_checks ();
7406 }
7407
7408 /* Parse a decl-specifier-seq.
7409
7410 decl-specifier-seq:
7411 decl-specifier-seq [opt] decl-specifier
7412
7413 decl-specifier:
7414 storage-class-specifier
7415 type-specifier
7416 function-specifier
7417 friend
7418 typedef
7419
7420 GNU Extension:
7421
7422 decl-specifier:
7423 attributes
7424
7425 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7426
7427 The parser flags FLAGS is used to control type-specifier parsing.
7428
7429 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7430 flags:
7431
7432 1: one of the decl-specifiers is an elaborated-type-specifier
7433 (i.e., a type declaration)
7434 2: one of the decl-specifiers is an enum-specifier or a
7435 class-specifier (i.e., a type definition)
7436
7437 */
7438
7439 static void
7440 cp_parser_decl_specifier_seq (cp_parser* parser,
7441 cp_parser_flags flags,
7442 cp_decl_specifier_seq *decl_specs,
7443 int* declares_class_or_enum)
7444 {
7445 bool constructor_possible_p = !parser->in_declarator_p;
7446
7447 /* Clear DECL_SPECS. */
7448 clear_decl_specs (decl_specs);
7449
7450 /* Assume no class or enumeration type is declared. */
7451 *declares_class_or_enum = 0;
7452
7453 /* Keep reading specifiers until there are no more to read. */
7454 while (true)
7455 {
7456 bool constructor_p;
7457 bool found_decl_spec;
7458 cp_token *token;
7459
7460 /* Peek at the next token. */
7461 token = cp_lexer_peek_token (parser->lexer);
7462 /* Handle attributes. */
7463 if (token->keyword == RID_ATTRIBUTE)
7464 {
7465 /* Parse the attributes. */
7466 decl_specs->attributes
7467 = chainon (decl_specs->attributes,
7468 cp_parser_attributes_opt (parser));
7469 continue;
7470 }
7471 /* Assume we will find a decl-specifier keyword. */
7472 found_decl_spec = true;
7473 /* If the next token is an appropriate keyword, we can simply
7474 add it to the list. */
7475 switch (token->keyword)
7476 {
7477 /* decl-specifier:
7478 friend */
7479 case RID_FRIEND:
7480 if (!at_class_scope_p ())
7481 {
7482 error ("%<friend%> used outside of class");
7483 cp_lexer_purge_token (parser->lexer);
7484 }
7485 else
7486 {
7487 ++decl_specs->specs[(int) ds_friend];
7488 /* Consume the token. */
7489 cp_lexer_consume_token (parser->lexer);
7490 }
7491 break;
7492
7493 /* function-specifier:
7494 inline
7495 virtual
7496 explicit */
7497 case RID_INLINE:
7498 case RID_VIRTUAL:
7499 case RID_EXPLICIT:
7500 cp_parser_function_specifier_opt (parser, decl_specs);
7501 break;
7502
7503 /* decl-specifier:
7504 typedef */
7505 case RID_TYPEDEF:
7506 ++decl_specs->specs[(int) ds_typedef];
7507 /* Consume the token. */
7508 cp_lexer_consume_token (parser->lexer);
7509 /* A constructor declarator cannot appear in a typedef. */
7510 constructor_possible_p = false;
7511 /* The "typedef" keyword can only occur in a declaration; we
7512 may as well commit at this point. */
7513 cp_parser_commit_to_tentative_parse (parser);
7514
7515 if (decl_specs->storage_class != sc_none)
7516 decl_specs->conflicting_specifiers_p = true;
7517 break;
7518
7519 /* storage-class-specifier:
7520 auto
7521 register
7522 static
7523 extern
7524 mutable
7525
7526 GNU Extension:
7527 thread */
7528 case RID_AUTO:
7529 case RID_REGISTER:
7530 case RID_STATIC:
7531 case RID_EXTERN:
7532 case RID_MUTABLE:
7533 /* Consume the token. */
7534 cp_lexer_consume_token (parser->lexer);
7535 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7536 break;
7537 case RID_THREAD:
7538 /* Consume the token. */
7539 cp_lexer_consume_token (parser->lexer);
7540 ++decl_specs->specs[(int) ds_thread];
7541 break;
7542
7543 default:
7544 /* We did not yet find a decl-specifier yet. */
7545 found_decl_spec = false;
7546 break;
7547 }
7548
7549 /* Constructors are a special case. The `S' in `S()' is not a
7550 decl-specifier; it is the beginning of the declarator. */
7551 constructor_p
7552 = (!found_decl_spec
7553 && constructor_possible_p
7554 && (cp_parser_constructor_declarator_p
7555 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7556
7557 /* If we don't have a DECL_SPEC yet, then we must be looking at
7558 a type-specifier. */
7559 if (!found_decl_spec && !constructor_p)
7560 {
7561 int decl_spec_declares_class_or_enum;
7562 bool is_cv_qualifier;
7563 tree type_spec;
7564
7565 type_spec
7566 = cp_parser_type_specifier (parser, flags,
7567 decl_specs,
7568 /*is_declaration=*/true,
7569 &decl_spec_declares_class_or_enum,
7570 &is_cv_qualifier);
7571
7572 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7573
7574 /* If this type-specifier referenced a user-defined type
7575 (a typedef, class-name, etc.), then we can't allow any
7576 more such type-specifiers henceforth.
7577
7578 [dcl.spec]
7579
7580 The longest sequence of decl-specifiers that could
7581 possibly be a type name is taken as the
7582 decl-specifier-seq of a declaration. The sequence shall
7583 be self-consistent as described below.
7584
7585 [dcl.type]
7586
7587 As a general rule, at most one type-specifier is allowed
7588 in the complete decl-specifier-seq of a declaration. The
7589 only exceptions are the following:
7590
7591 -- const or volatile can be combined with any other
7592 type-specifier.
7593
7594 -- signed or unsigned can be combined with char, long,
7595 short, or int.
7596
7597 -- ..
7598
7599 Example:
7600
7601 typedef char* Pc;
7602 void g (const int Pc);
7603
7604 Here, Pc is *not* part of the decl-specifier seq; it's
7605 the declarator. Therefore, once we see a type-specifier
7606 (other than a cv-qualifier), we forbid any additional
7607 user-defined types. We *do* still allow things like `int
7608 int' to be considered a decl-specifier-seq, and issue the
7609 error message later. */
7610 if (type_spec && !is_cv_qualifier)
7611 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7612 /* A constructor declarator cannot follow a type-specifier. */
7613 if (type_spec)
7614 {
7615 constructor_possible_p = false;
7616 found_decl_spec = true;
7617 }
7618 }
7619
7620 /* If we still do not have a DECL_SPEC, then there are no more
7621 decl-specifiers. */
7622 if (!found_decl_spec)
7623 break;
7624
7625 decl_specs->any_specifiers_p = true;
7626 /* After we see one decl-specifier, further decl-specifiers are
7627 always optional. */
7628 flags |= CP_PARSER_FLAGS_OPTIONAL;
7629 }
7630
7631 cp_parser_check_decl_spec (decl_specs);
7632
7633 /* Don't allow a friend specifier with a class definition. */
7634 if (decl_specs->specs[(int) ds_friend] != 0
7635 && (*declares_class_or_enum & 2))
7636 error ("class definition may not be declared a friend");
7637 }
7638
7639 /* Parse an (optional) storage-class-specifier.
7640
7641 storage-class-specifier:
7642 auto
7643 register
7644 static
7645 extern
7646 mutable
7647
7648 GNU Extension:
7649
7650 storage-class-specifier:
7651 thread
7652
7653 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7654
7655 static tree
7656 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7657 {
7658 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7659 {
7660 case RID_AUTO:
7661 case RID_REGISTER:
7662 case RID_STATIC:
7663 case RID_EXTERN:
7664 case RID_MUTABLE:
7665 case RID_THREAD:
7666 /* Consume the token. */
7667 return cp_lexer_consume_token (parser->lexer)->value;
7668
7669 default:
7670 return NULL_TREE;
7671 }
7672 }
7673
7674 /* Parse an (optional) function-specifier.
7675
7676 function-specifier:
7677 inline
7678 virtual
7679 explicit
7680
7681 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7682 Updates DECL_SPECS, if it is non-NULL. */
7683
7684 static tree
7685 cp_parser_function_specifier_opt (cp_parser* parser,
7686 cp_decl_specifier_seq *decl_specs)
7687 {
7688 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7689 {
7690 case RID_INLINE:
7691 if (decl_specs)
7692 ++decl_specs->specs[(int) ds_inline];
7693 break;
7694
7695 case RID_VIRTUAL:
7696 /* 14.5.2.3 [temp.mem]
7697
7698 A member function template shall not be virtual. */
7699 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7700 error ("templates may not be %<virtual%>");
7701 else if (decl_specs)
7702 ++decl_specs->specs[(int) ds_virtual];
7703 break;
7704
7705 case RID_EXPLICIT:
7706 if (decl_specs)
7707 ++decl_specs->specs[(int) ds_explicit];
7708 break;
7709
7710 default:
7711 return NULL_TREE;
7712 }
7713
7714 /* Consume the token. */
7715 return cp_lexer_consume_token (parser->lexer)->value;
7716 }
7717
7718 /* Parse a linkage-specification.
7719
7720 linkage-specification:
7721 extern string-literal { declaration-seq [opt] }
7722 extern string-literal declaration */
7723
7724 static void
7725 cp_parser_linkage_specification (cp_parser* parser)
7726 {
7727 tree linkage;
7728
7729 /* Look for the `extern' keyword. */
7730 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7731
7732 /* Look for the string-literal. */
7733 linkage = cp_parser_string_literal (parser, false, false);
7734
7735 /* Transform the literal into an identifier. If the literal is a
7736 wide-character string, or contains embedded NULs, then we can't
7737 handle it as the user wants. */
7738 if (strlen (TREE_STRING_POINTER (linkage))
7739 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7740 {
7741 cp_parser_error (parser, "invalid linkage-specification");
7742 /* Assume C++ linkage. */
7743 linkage = lang_name_cplusplus;
7744 }
7745 else
7746 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7747
7748 /* We're now using the new linkage. */
7749 push_lang_context (linkage);
7750
7751 /* If the next token is a `{', then we're using the first
7752 production. */
7753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7754 {
7755 /* Consume the `{' token. */
7756 cp_lexer_consume_token (parser->lexer);
7757 /* Parse the declarations. */
7758 cp_parser_declaration_seq_opt (parser);
7759 /* Look for the closing `}'. */
7760 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7761 }
7762 /* Otherwise, there's just one declaration. */
7763 else
7764 {
7765 bool saved_in_unbraced_linkage_specification_p;
7766
7767 saved_in_unbraced_linkage_specification_p
7768 = parser->in_unbraced_linkage_specification_p;
7769 parser->in_unbraced_linkage_specification_p = true;
7770 cp_parser_declaration (parser);
7771 parser->in_unbraced_linkage_specification_p
7772 = saved_in_unbraced_linkage_specification_p;
7773 }
7774
7775 /* We're done with the linkage-specification. */
7776 pop_lang_context ();
7777 }
7778
7779 /* Special member functions [gram.special] */
7780
7781 /* Parse a conversion-function-id.
7782
7783 conversion-function-id:
7784 operator conversion-type-id
7785
7786 Returns an IDENTIFIER_NODE representing the operator. */
7787
7788 static tree
7789 cp_parser_conversion_function_id (cp_parser* parser)
7790 {
7791 tree type;
7792 tree saved_scope;
7793 tree saved_qualifying_scope;
7794 tree saved_object_scope;
7795 tree pushed_scope = NULL_TREE;
7796
7797 /* Look for the `operator' token. */
7798 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7799 return error_mark_node;
7800 /* When we parse the conversion-type-id, the current scope will be
7801 reset. However, we need that information in able to look up the
7802 conversion function later, so we save it here. */
7803 saved_scope = parser->scope;
7804 saved_qualifying_scope = parser->qualifying_scope;
7805 saved_object_scope = parser->object_scope;
7806 /* We must enter the scope of the class so that the names of
7807 entities declared within the class are available in the
7808 conversion-type-id. For example, consider:
7809
7810 struct S {
7811 typedef int I;
7812 operator I();
7813 };
7814
7815 S::operator I() { ... }
7816
7817 In order to see that `I' is a type-name in the definition, we
7818 must be in the scope of `S'. */
7819 if (saved_scope)
7820 pushed_scope = push_scope (saved_scope);
7821 /* Parse the conversion-type-id. */
7822 type = cp_parser_conversion_type_id (parser);
7823 /* Leave the scope of the class, if any. */
7824 if (pushed_scope)
7825 pop_scope (pushed_scope);
7826 /* Restore the saved scope. */
7827 parser->scope = saved_scope;
7828 parser->qualifying_scope = saved_qualifying_scope;
7829 parser->object_scope = saved_object_scope;
7830 /* If the TYPE is invalid, indicate failure. */
7831 if (type == error_mark_node)
7832 return error_mark_node;
7833 return mangle_conv_op_name_for_type (type);
7834 }
7835
7836 /* Parse a conversion-type-id:
7837
7838 conversion-type-id:
7839 type-specifier-seq conversion-declarator [opt]
7840
7841 Returns the TYPE specified. */
7842
7843 static tree
7844 cp_parser_conversion_type_id (cp_parser* parser)
7845 {
7846 tree attributes;
7847 cp_decl_specifier_seq type_specifiers;
7848 cp_declarator *declarator;
7849 tree type_specified;
7850
7851 /* Parse the attributes. */
7852 attributes = cp_parser_attributes_opt (parser);
7853 /* Parse the type-specifiers. */
7854 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7855 &type_specifiers);
7856 /* If that didn't work, stop. */
7857 if (type_specifiers.type == error_mark_node)
7858 return error_mark_node;
7859 /* Parse the conversion-declarator. */
7860 declarator = cp_parser_conversion_declarator_opt (parser);
7861
7862 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7863 /*initialized=*/0, &attributes);
7864 if (attributes)
7865 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7866 return type_specified;
7867 }
7868
7869 /* Parse an (optional) conversion-declarator.
7870
7871 conversion-declarator:
7872 ptr-operator conversion-declarator [opt]
7873
7874 */
7875
7876 static cp_declarator *
7877 cp_parser_conversion_declarator_opt (cp_parser* parser)
7878 {
7879 enum tree_code code;
7880 tree class_type;
7881 cp_cv_quals cv_quals;
7882
7883 /* We don't know if there's a ptr-operator next, or not. */
7884 cp_parser_parse_tentatively (parser);
7885 /* Try the ptr-operator. */
7886 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7887 /* If it worked, look for more conversion-declarators. */
7888 if (cp_parser_parse_definitely (parser))
7889 {
7890 cp_declarator *declarator;
7891
7892 /* Parse another optional declarator. */
7893 declarator = cp_parser_conversion_declarator_opt (parser);
7894
7895 /* Create the representation of the declarator. */
7896 if (class_type)
7897 declarator = make_ptrmem_declarator (cv_quals, class_type,
7898 declarator);
7899 else if (code == INDIRECT_REF)
7900 declarator = make_pointer_declarator (cv_quals, declarator);
7901 else
7902 declarator = make_reference_declarator (cv_quals, declarator);
7903
7904 return declarator;
7905 }
7906
7907 return NULL;
7908 }
7909
7910 /* Parse an (optional) ctor-initializer.
7911
7912 ctor-initializer:
7913 : mem-initializer-list
7914
7915 Returns TRUE iff the ctor-initializer was actually present. */
7916
7917 static bool
7918 cp_parser_ctor_initializer_opt (cp_parser* parser)
7919 {
7920 /* If the next token is not a `:', then there is no
7921 ctor-initializer. */
7922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7923 {
7924 /* Do default initialization of any bases and members. */
7925 if (DECL_CONSTRUCTOR_P (current_function_decl))
7926 finish_mem_initializers (NULL_TREE);
7927
7928 return false;
7929 }
7930
7931 /* Consume the `:' token. */
7932 cp_lexer_consume_token (parser->lexer);
7933 /* And the mem-initializer-list. */
7934 cp_parser_mem_initializer_list (parser);
7935
7936 return true;
7937 }
7938
7939 /* Parse a mem-initializer-list.
7940
7941 mem-initializer-list:
7942 mem-initializer
7943 mem-initializer , mem-initializer-list */
7944
7945 static void
7946 cp_parser_mem_initializer_list (cp_parser* parser)
7947 {
7948 tree mem_initializer_list = NULL_TREE;
7949
7950 /* Let the semantic analysis code know that we are starting the
7951 mem-initializer-list. */
7952 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7953 error ("only constructors take base initializers");
7954
7955 /* Loop through the list. */
7956 while (true)
7957 {
7958 tree mem_initializer;
7959
7960 /* Parse the mem-initializer. */
7961 mem_initializer = cp_parser_mem_initializer (parser);
7962 /* Add it to the list, unless it was erroneous. */
7963 if (mem_initializer != error_mark_node)
7964 {
7965 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7966 mem_initializer_list = mem_initializer;
7967 }
7968 /* If the next token is not a `,', we're done. */
7969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7970 break;
7971 /* Consume the `,' token. */
7972 cp_lexer_consume_token (parser->lexer);
7973 }
7974
7975 /* Perform semantic analysis. */
7976 if (DECL_CONSTRUCTOR_P (current_function_decl))
7977 finish_mem_initializers (mem_initializer_list);
7978 }
7979
7980 /* Parse a mem-initializer.
7981
7982 mem-initializer:
7983 mem-initializer-id ( expression-list [opt] )
7984
7985 GNU extension:
7986
7987 mem-initializer:
7988 ( expression-list [opt] )
7989
7990 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7991 class) or FIELD_DECL (for a non-static data member) to initialize;
7992 the TREE_VALUE is the expression-list. An empty initialization
7993 list is represented by void_list_node. */
7994
7995 static tree
7996 cp_parser_mem_initializer (cp_parser* parser)
7997 {
7998 tree mem_initializer_id;
7999 tree expression_list;
8000 tree member;
8001
8002 /* Find out what is being initialized. */
8003 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8004 {
8005 pedwarn ("anachronistic old-style base class initializer");
8006 mem_initializer_id = NULL_TREE;
8007 }
8008 else
8009 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8010 member = expand_member_init (mem_initializer_id);
8011 if (member && !DECL_P (member))
8012 in_base_initializer = 1;
8013
8014 expression_list
8015 = cp_parser_parenthesized_expression_list (parser, false,
8016 /*cast_p=*/false,
8017 /*non_constant_p=*/NULL);
8018 if (expression_list == error_mark_node)
8019 return error_mark_node;
8020 if (!expression_list)
8021 expression_list = void_type_node;
8022
8023 in_base_initializer = 0;
8024
8025 return member ? build_tree_list (member, expression_list) : error_mark_node;
8026 }
8027
8028 /* Parse a mem-initializer-id.
8029
8030 mem-initializer-id:
8031 :: [opt] nested-name-specifier [opt] class-name
8032 identifier
8033
8034 Returns a TYPE indicating the class to be initializer for the first
8035 production. Returns an IDENTIFIER_NODE indicating the data member
8036 to be initialized for the second production. */
8037
8038 static tree
8039 cp_parser_mem_initializer_id (cp_parser* parser)
8040 {
8041 bool global_scope_p;
8042 bool nested_name_specifier_p;
8043 bool template_p = false;
8044 tree id;
8045
8046 /* `typename' is not allowed in this context ([temp.res]). */
8047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8048 {
8049 error ("keyword %<typename%> not allowed in this context (a qualified "
8050 "member initializer is implicitly a type)");
8051 cp_lexer_consume_token (parser->lexer);
8052 }
8053 /* Look for the optional `::' operator. */
8054 global_scope_p
8055 = (cp_parser_global_scope_opt (parser,
8056 /*current_scope_valid_p=*/false)
8057 != NULL_TREE);
8058 /* Look for the optional nested-name-specifier. The simplest way to
8059 implement:
8060
8061 [temp.res]
8062
8063 The keyword `typename' is not permitted in a base-specifier or
8064 mem-initializer; in these contexts a qualified name that
8065 depends on a template-parameter is implicitly assumed to be a
8066 type name.
8067
8068 is to assume that we have seen the `typename' keyword at this
8069 point. */
8070 nested_name_specifier_p
8071 = (cp_parser_nested_name_specifier_opt (parser,
8072 /*typename_keyword_p=*/true,
8073 /*check_dependency_p=*/true,
8074 /*type_p=*/true,
8075 /*is_declaration=*/true)
8076 != NULL_TREE);
8077 if (nested_name_specifier_p)
8078 template_p = cp_parser_optional_template_keyword (parser);
8079 /* If there is a `::' operator or a nested-name-specifier, then we
8080 are definitely looking for a class-name. */
8081 if (global_scope_p || nested_name_specifier_p)
8082 return cp_parser_class_name (parser,
8083 /*typename_keyword_p=*/true,
8084 /*template_keyword_p=*/template_p,
8085 none_type,
8086 /*check_dependency_p=*/true,
8087 /*class_head_p=*/false,
8088 /*is_declaration=*/true);
8089 /* Otherwise, we could also be looking for an ordinary identifier. */
8090 cp_parser_parse_tentatively (parser);
8091 /* Try a class-name. */
8092 id = cp_parser_class_name (parser,
8093 /*typename_keyword_p=*/true,
8094 /*template_keyword_p=*/false,
8095 none_type,
8096 /*check_dependency_p=*/true,
8097 /*class_head_p=*/false,
8098 /*is_declaration=*/true);
8099 /* If we found one, we're done. */
8100 if (cp_parser_parse_definitely (parser))
8101 return id;
8102 /* Otherwise, look for an ordinary identifier. */
8103 return cp_parser_identifier (parser);
8104 }
8105
8106 /* Overloading [gram.over] */
8107
8108 /* Parse an operator-function-id.
8109
8110 operator-function-id:
8111 operator operator
8112
8113 Returns an IDENTIFIER_NODE for the operator which is a
8114 human-readable spelling of the identifier, e.g., `operator +'. */
8115
8116 static tree
8117 cp_parser_operator_function_id (cp_parser* parser)
8118 {
8119 /* Look for the `operator' keyword. */
8120 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8121 return error_mark_node;
8122 /* And then the name of the operator itself. */
8123 return cp_parser_operator (parser);
8124 }
8125
8126 /* Parse an operator.
8127
8128 operator:
8129 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8130 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8131 || ++ -- , ->* -> () []
8132
8133 GNU Extensions:
8134
8135 operator:
8136 <? >? <?= >?=
8137
8138 Returns an IDENTIFIER_NODE for the operator which is a
8139 human-readable spelling of the identifier, e.g., `operator +'. */
8140
8141 static tree
8142 cp_parser_operator (cp_parser* parser)
8143 {
8144 tree id = NULL_TREE;
8145 cp_token *token;
8146
8147 /* Peek at the next token. */
8148 token = cp_lexer_peek_token (parser->lexer);
8149 /* Figure out which operator we have. */
8150 switch (token->type)
8151 {
8152 case CPP_KEYWORD:
8153 {
8154 enum tree_code op;
8155
8156 /* The keyword should be either `new' or `delete'. */
8157 if (token->keyword == RID_NEW)
8158 op = NEW_EXPR;
8159 else if (token->keyword == RID_DELETE)
8160 op = DELETE_EXPR;
8161 else
8162 break;
8163
8164 /* Consume the `new' or `delete' token. */
8165 cp_lexer_consume_token (parser->lexer);
8166
8167 /* Peek at the next token. */
8168 token = cp_lexer_peek_token (parser->lexer);
8169 /* If it's a `[' token then this is the array variant of the
8170 operator. */
8171 if (token->type == CPP_OPEN_SQUARE)
8172 {
8173 /* Consume the `[' token. */
8174 cp_lexer_consume_token (parser->lexer);
8175 /* Look for the `]' token. */
8176 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8177 id = ansi_opname (op == NEW_EXPR
8178 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8179 }
8180 /* Otherwise, we have the non-array variant. */
8181 else
8182 id = ansi_opname (op);
8183
8184 return id;
8185 }
8186
8187 case CPP_PLUS:
8188 id = ansi_opname (PLUS_EXPR);
8189 break;
8190
8191 case CPP_MINUS:
8192 id = ansi_opname (MINUS_EXPR);
8193 break;
8194
8195 case CPP_MULT:
8196 id = ansi_opname (MULT_EXPR);
8197 break;
8198
8199 case CPP_DIV:
8200 id = ansi_opname (TRUNC_DIV_EXPR);
8201 break;
8202
8203 case CPP_MOD:
8204 id = ansi_opname (TRUNC_MOD_EXPR);
8205 break;
8206
8207 case CPP_XOR:
8208 id = ansi_opname (BIT_XOR_EXPR);
8209 break;
8210
8211 case CPP_AND:
8212 id = ansi_opname (BIT_AND_EXPR);
8213 break;
8214
8215 case CPP_OR:
8216 id = ansi_opname (BIT_IOR_EXPR);
8217 break;
8218
8219 case CPP_COMPL:
8220 id = ansi_opname (BIT_NOT_EXPR);
8221 break;
8222
8223 case CPP_NOT:
8224 id = ansi_opname (TRUTH_NOT_EXPR);
8225 break;
8226
8227 case CPP_EQ:
8228 id = ansi_assopname (NOP_EXPR);
8229 break;
8230
8231 case CPP_LESS:
8232 id = ansi_opname (LT_EXPR);
8233 break;
8234
8235 case CPP_GREATER:
8236 id = ansi_opname (GT_EXPR);
8237 break;
8238
8239 case CPP_PLUS_EQ:
8240 id = ansi_assopname (PLUS_EXPR);
8241 break;
8242
8243 case CPP_MINUS_EQ:
8244 id = ansi_assopname (MINUS_EXPR);
8245 break;
8246
8247 case CPP_MULT_EQ:
8248 id = ansi_assopname (MULT_EXPR);
8249 break;
8250
8251 case CPP_DIV_EQ:
8252 id = ansi_assopname (TRUNC_DIV_EXPR);
8253 break;
8254
8255 case CPP_MOD_EQ:
8256 id = ansi_assopname (TRUNC_MOD_EXPR);
8257 break;
8258
8259 case CPP_XOR_EQ:
8260 id = ansi_assopname (BIT_XOR_EXPR);
8261 break;
8262
8263 case CPP_AND_EQ:
8264 id = ansi_assopname (BIT_AND_EXPR);
8265 break;
8266
8267 case CPP_OR_EQ:
8268 id = ansi_assopname (BIT_IOR_EXPR);
8269 break;
8270
8271 case CPP_LSHIFT:
8272 id = ansi_opname (LSHIFT_EXPR);
8273 break;
8274
8275 case CPP_RSHIFT:
8276 id = ansi_opname (RSHIFT_EXPR);
8277 break;
8278
8279 case CPP_LSHIFT_EQ:
8280 id = ansi_assopname (LSHIFT_EXPR);
8281 break;
8282
8283 case CPP_RSHIFT_EQ:
8284 id = ansi_assopname (RSHIFT_EXPR);
8285 break;
8286
8287 case CPP_EQ_EQ:
8288 id = ansi_opname (EQ_EXPR);
8289 break;
8290
8291 case CPP_NOT_EQ:
8292 id = ansi_opname (NE_EXPR);
8293 break;
8294
8295 case CPP_LESS_EQ:
8296 id = ansi_opname (LE_EXPR);
8297 break;
8298
8299 case CPP_GREATER_EQ:
8300 id = ansi_opname (GE_EXPR);
8301 break;
8302
8303 case CPP_AND_AND:
8304 id = ansi_opname (TRUTH_ANDIF_EXPR);
8305 break;
8306
8307 case CPP_OR_OR:
8308 id = ansi_opname (TRUTH_ORIF_EXPR);
8309 break;
8310
8311 case CPP_PLUS_PLUS:
8312 id = ansi_opname (POSTINCREMENT_EXPR);
8313 break;
8314
8315 case CPP_MINUS_MINUS:
8316 id = ansi_opname (PREDECREMENT_EXPR);
8317 break;
8318
8319 case CPP_COMMA:
8320 id = ansi_opname (COMPOUND_EXPR);
8321 break;
8322
8323 case CPP_DEREF_STAR:
8324 id = ansi_opname (MEMBER_REF);
8325 break;
8326
8327 case CPP_DEREF:
8328 id = ansi_opname (COMPONENT_REF);
8329 break;
8330
8331 case CPP_OPEN_PAREN:
8332 /* Consume the `('. */
8333 cp_lexer_consume_token (parser->lexer);
8334 /* Look for the matching `)'. */
8335 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8336 return ansi_opname (CALL_EXPR);
8337
8338 case CPP_OPEN_SQUARE:
8339 /* Consume the `['. */
8340 cp_lexer_consume_token (parser->lexer);
8341 /* Look for the matching `]'. */
8342 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8343 return ansi_opname (ARRAY_REF);
8344
8345 default:
8346 /* Anything else is an error. */
8347 break;
8348 }
8349
8350 /* If we have selected an identifier, we need to consume the
8351 operator token. */
8352 if (id)
8353 cp_lexer_consume_token (parser->lexer);
8354 /* Otherwise, no valid operator name was present. */
8355 else
8356 {
8357 cp_parser_error (parser, "expected operator");
8358 id = error_mark_node;
8359 }
8360
8361 return id;
8362 }
8363
8364 /* Parse a template-declaration.
8365
8366 template-declaration:
8367 export [opt] template < template-parameter-list > declaration
8368
8369 If MEMBER_P is TRUE, this template-declaration occurs within a
8370 class-specifier.
8371
8372 The grammar rule given by the standard isn't correct. What
8373 is really meant is:
8374
8375 template-declaration:
8376 export [opt] template-parameter-list-seq
8377 decl-specifier-seq [opt] init-declarator [opt] ;
8378 export [opt] template-parameter-list-seq
8379 function-definition
8380
8381 template-parameter-list-seq:
8382 template-parameter-list-seq [opt]
8383 template < template-parameter-list > */
8384
8385 static void
8386 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8387 {
8388 /* Check for `export'. */
8389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8390 {
8391 /* Consume the `export' token. */
8392 cp_lexer_consume_token (parser->lexer);
8393 /* Warn that we do not support `export'. */
8394 warning (0, "keyword %<export%> not implemented, and will be ignored");
8395 }
8396
8397 cp_parser_template_declaration_after_export (parser, member_p);
8398 }
8399
8400 /* Parse a template-parameter-list.
8401
8402 template-parameter-list:
8403 template-parameter
8404 template-parameter-list , template-parameter
8405
8406 Returns a TREE_LIST. Each node represents a template parameter.
8407 The nodes are connected via their TREE_CHAINs. */
8408
8409 static tree
8410 cp_parser_template_parameter_list (cp_parser* parser)
8411 {
8412 tree parameter_list = NULL_TREE;
8413
8414 begin_template_parm_list ();
8415 while (true)
8416 {
8417 tree parameter;
8418 cp_token *token;
8419 bool is_non_type;
8420
8421 /* Parse the template-parameter. */
8422 parameter = cp_parser_template_parameter (parser, &is_non_type);
8423 /* Add it to the list. */
8424 if (parameter != error_mark_node)
8425 parameter_list = process_template_parm (parameter_list,
8426 parameter,
8427 is_non_type);
8428 else
8429 {
8430 tree err_parm = build_tree_list (parameter, parameter);
8431 TREE_VALUE (err_parm) = error_mark_node;
8432 parameter_list = chainon (parameter_list, err_parm);
8433 }
8434
8435 /* Peek at the next token. */
8436 token = cp_lexer_peek_token (parser->lexer);
8437 /* If it's not a `,', we're done. */
8438 if (token->type != CPP_COMMA)
8439 break;
8440 /* Otherwise, consume the `,' token. */
8441 cp_lexer_consume_token (parser->lexer);
8442 }
8443
8444 return end_template_parm_list (parameter_list);
8445 }
8446
8447 /* Parse a template-parameter.
8448
8449 template-parameter:
8450 type-parameter
8451 parameter-declaration
8452
8453 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8454 the parameter. The TREE_PURPOSE is the default value, if any.
8455 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8456 iff this parameter is a non-type parameter. */
8457
8458 static tree
8459 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8460 {
8461 cp_token *token;
8462 cp_parameter_declarator *parameter_declarator;
8463 tree parm;
8464
8465 /* Assume it is a type parameter or a template parameter. */
8466 *is_non_type = false;
8467 /* Peek at the next token. */
8468 token = cp_lexer_peek_token (parser->lexer);
8469 /* If it is `class' or `template', we have a type-parameter. */
8470 if (token->keyword == RID_TEMPLATE)
8471 return cp_parser_type_parameter (parser);
8472 /* If it is `class' or `typename' we do not know yet whether it is a
8473 type parameter or a non-type parameter. Consider:
8474
8475 template <typename T, typename T::X X> ...
8476
8477 or:
8478
8479 template <class C, class D*> ...
8480
8481 Here, the first parameter is a type parameter, and the second is
8482 a non-type parameter. We can tell by looking at the token after
8483 the identifier -- if it is a `,', `=', or `>' then we have a type
8484 parameter. */
8485 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8486 {
8487 /* Peek at the token after `class' or `typename'. */
8488 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8489 /* If it's an identifier, skip it. */
8490 if (token->type == CPP_NAME)
8491 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8492 /* Now, see if the token looks like the end of a template
8493 parameter. */
8494 if (token->type == CPP_COMMA
8495 || token->type == CPP_EQ
8496 || token->type == CPP_GREATER)
8497 return cp_parser_type_parameter (parser);
8498 }
8499
8500 /* Otherwise, it is a non-type parameter.
8501
8502 [temp.param]
8503
8504 When parsing a default template-argument for a non-type
8505 template-parameter, the first non-nested `>' is taken as the end
8506 of the template parameter-list rather than a greater-than
8507 operator. */
8508 *is_non_type = true;
8509 parameter_declarator
8510 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8511 /*parenthesized_p=*/NULL);
8512 parm = grokdeclarator (parameter_declarator->declarator,
8513 &parameter_declarator->decl_specifiers,
8514 PARM, /*initialized=*/0,
8515 /*attrlist=*/NULL);
8516 if (parm == error_mark_node)
8517 return error_mark_node;
8518 return build_tree_list (parameter_declarator->default_argument, parm);
8519 }
8520
8521 /* Parse a type-parameter.
8522
8523 type-parameter:
8524 class identifier [opt]
8525 class identifier [opt] = type-id
8526 typename identifier [opt]
8527 typename identifier [opt] = type-id
8528 template < template-parameter-list > class identifier [opt]
8529 template < template-parameter-list > class identifier [opt]
8530 = id-expression
8531
8532 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8533 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8534 the declaration of the parameter. */
8535
8536 static tree
8537 cp_parser_type_parameter (cp_parser* parser)
8538 {
8539 cp_token *token;
8540 tree parameter;
8541
8542 /* Look for a keyword to tell us what kind of parameter this is. */
8543 token = cp_parser_require (parser, CPP_KEYWORD,
8544 "`class', `typename', or `template'");
8545 if (!token)
8546 return error_mark_node;
8547
8548 switch (token->keyword)
8549 {
8550 case RID_CLASS:
8551 case RID_TYPENAME:
8552 {
8553 tree identifier;
8554 tree default_argument;
8555
8556 /* If the next token is an identifier, then it names the
8557 parameter. */
8558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8559 identifier = cp_parser_identifier (parser);
8560 else
8561 identifier = NULL_TREE;
8562
8563 /* Create the parameter. */
8564 parameter = finish_template_type_parm (class_type_node, identifier);
8565
8566 /* If the next token is an `=', we have a default argument. */
8567 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8568 {
8569 /* Consume the `=' token. */
8570 cp_lexer_consume_token (parser->lexer);
8571 /* Parse the default-argument. */
8572 push_deferring_access_checks (dk_no_deferred);
8573 default_argument = cp_parser_type_id (parser);
8574 pop_deferring_access_checks ();
8575 }
8576 else
8577 default_argument = NULL_TREE;
8578
8579 /* Create the combined representation of the parameter and the
8580 default argument. */
8581 parameter = build_tree_list (default_argument, parameter);
8582 }
8583 break;
8584
8585 case RID_TEMPLATE:
8586 {
8587 tree parameter_list;
8588 tree identifier;
8589 tree default_argument;
8590
8591 /* Look for the `<'. */
8592 cp_parser_require (parser, CPP_LESS, "`<'");
8593 /* Parse the template-parameter-list. */
8594 parameter_list = cp_parser_template_parameter_list (parser);
8595 /* Look for the `>'. */
8596 cp_parser_require (parser, CPP_GREATER, "`>'");
8597 /* Look for the `class' keyword. */
8598 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8599 /* If the next token is an `=', then there is a
8600 default-argument. If the next token is a `>', we are at
8601 the end of the parameter-list. If the next token is a `,',
8602 then we are at the end of this parameter. */
8603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8604 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8605 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8606 {
8607 identifier = cp_parser_identifier (parser);
8608 /* Treat invalid names as if the parameter were nameless. */
8609 if (identifier == error_mark_node)
8610 identifier = NULL_TREE;
8611 }
8612 else
8613 identifier = NULL_TREE;
8614
8615 /* Create the template parameter. */
8616 parameter = finish_template_template_parm (class_type_node,
8617 identifier);
8618
8619 /* If the next token is an `=', then there is a
8620 default-argument. */
8621 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8622 {
8623 bool is_template;
8624
8625 /* Consume the `='. */
8626 cp_lexer_consume_token (parser->lexer);
8627 /* Parse the id-expression. */
8628 push_deferring_access_checks (dk_no_deferred);
8629 default_argument
8630 = cp_parser_id_expression (parser,
8631 /*template_keyword_p=*/false,
8632 /*check_dependency_p=*/true,
8633 /*template_p=*/&is_template,
8634 /*declarator_p=*/false,
8635 /*optional_p=*/false);
8636 if (TREE_CODE (default_argument) == TYPE_DECL)
8637 /* If the id-expression was a template-id that refers to
8638 a template-class, we already have the declaration here,
8639 so no further lookup is needed. */
8640 ;
8641 else
8642 /* Look up the name. */
8643 default_argument
8644 = cp_parser_lookup_name (parser, default_argument,
8645 none_type,
8646 /*is_template=*/is_template,
8647 /*is_namespace=*/false,
8648 /*check_dependency=*/true,
8649 /*ambiguous_decls=*/NULL);
8650 /* See if the default argument is valid. */
8651 default_argument
8652 = check_template_template_default_arg (default_argument);
8653 pop_deferring_access_checks ();
8654 }
8655 else
8656 default_argument = NULL_TREE;
8657
8658 /* Create the combined representation of the parameter and the
8659 default argument. */
8660 parameter = build_tree_list (default_argument, parameter);
8661 }
8662 break;
8663
8664 default:
8665 gcc_unreachable ();
8666 break;
8667 }
8668
8669 return parameter;
8670 }
8671
8672 /* Parse a template-id.
8673
8674 template-id:
8675 template-name < template-argument-list [opt] >
8676
8677 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8678 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8679 returned. Otherwise, if the template-name names a function, or set
8680 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8681 names a class, returns a TYPE_DECL for the specialization.
8682
8683 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8684 uninstantiated templates. */
8685
8686 static tree
8687 cp_parser_template_id (cp_parser *parser,
8688 bool template_keyword_p,
8689 bool check_dependency_p,
8690 bool is_declaration)
8691 {
8692 tree template;
8693 tree arguments;
8694 tree template_id;
8695 cp_token_position start_of_id = 0;
8696 tree access_check = NULL_TREE;
8697 cp_token *next_token, *next_token_2;
8698 bool is_identifier;
8699
8700 /* If the next token corresponds to a template-id, there is no need
8701 to reparse it. */
8702 next_token = cp_lexer_peek_token (parser->lexer);
8703 if (next_token->type == CPP_TEMPLATE_ID)
8704 {
8705 tree value;
8706 tree check;
8707
8708 /* Get the stored value. */
8709 value = cp_lexer_consume_token (parser->lexer)->value;
8710 /* Perform any access checks that were deferred. */
8711 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8712 perform_or_defer_access_check (TREE_PURPOSE (check),
8713 TREE_VALUE (check));
8714 /* Return the stored value. */
8715 return TREE_VALUE (value);
8716 }
8717
8718 /* Avoid performing name lookup if there is no possibility of
8719 finding a template-id. */
8720 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8721 || (next_token->type == CPP_NAME
8722 && !cp_parser_nth_token_starts_template_argument_list_p
8723 (parser, 2)))
8724 {
8725 cp_parser_error (parser, "expected template-id");
8726 return error_mark_node;
8727 }
8728
8729 /* Remember where the template-id starts. */
8730 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8731 start_of_id = cp_lexer_token_position (parser->lexer, false);
8732
8733 push_deferring_access_checks (dk_deferred);
8734
8735 /* Parse the template-name. */
8736 is_identifier = false;
8737 template = cp_parser_template_name (parser, template_keyword_p,
8738 check_dependency_p,
8739 is_declaration,
8740 &is_identifier);
8741 if (template == error_mark_node || is_identifier)
8742 {
8743 pop_deferring_access_checks ();
8744 return template;
8745 }
8746
8747 /* If we find the sequence `[:' after a template-name, it's probably
8748 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8749 parse correctly the argument list. */
8750 next_token = cp_lexer_peek_token (parser->lexer);
8751 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8752 if (next_token->type == CPP_OPEN_SQUARE
8753 && next_token->flags & DIGRAPH
8754 && next_token_2->type == CPP_COLON
8755 && !(next_token_2->flags & PREV_WHITE))
8756 {
8757 cp_parser_parse_tentatively (parser);
8758 /* Change `:' into `::'. */
8759 next_token_2->type = CPP_SCOPE;
8760 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8761 CPP_LESS. */
8762 cp_lexer_consume_token (parser->lexer);
8763 /* Parse the arguments. */
8764 arguments = cp_parser_enclosed_template_argument_list (parser);
8765 if (!cp_parser_parse_definitely (parser))
8766 {
8767 /* If we couldn't parse an argument list, then we revert our changes
8768 and return simply an error. Maybe this is not a template-id
8769 after all. */
8770 next_token_2->type = CPP_COLON;
8771 cp_parser_error (parser, "expected %<<%>");
8772 pop_deferring_access_checks ();
8773 return error_mark_node;
8774 }
8775 /* Otherwise, emit an error about the invalid digraph, but continue
8776 parsing because we got our argument list. */
8777 pedwarn ("%<<::%> cannot begin a template-argument list");
8778 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8779 "between %<<%> and %<::%>");
8780 if (!flag_permissive)
8781 {
8782 static bool hint;
8783 if (!hint)
8784 {
8785 inform ("(if you use -fpermissive G++ will accept your code)");
8786 hint = true;
8787 }
8788 }
8789 }
8790 else
8791 {
8792 /* Look for the `<' that starts the template-argument-list. */
8793 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8794 {
8795 pop_deferring_access_checks ();
8796 return error_mark_node;
8797 }
8798 /* Parse the arguments. */
8799 arguments = cp_parser_enclosed_template_argument_list (parser);
8800 }
8801
8802 /* Build a representation of the specialization. */
8803 if (TREE_CODE (template) == IDENTIFIER_NODE)
8804 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8805 else if (DECL_CLASS_TEMPLATE_P (template)
8806 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8807 {
8808 bool entering_scope;
8809 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8810 template (rather than some instantiation thereof) only if
8811 is not nested within some other construct. For example, in
8812 "template <typename T> void f(T) { A<T>::", A<T> is just an
8813 instantiation of A. */
8814 entering_scope = (template_parm_scope_p ()
8815 && cp_lexer_next_token_is (parser->lexer,
8816 CPP_SCOPE));
8817 template_id
8818 = finish_template_type (template, arguments, entering_scope);
8819 }
8820 else
8821 {
8822 /* If it's not a class-template or a template-template, it should be
8823 a function-template. */
8824 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8825 || TREE_CODE (template) == OVERLOAD
8826 || BASELINK_P (template)));
8827
8828 template_id = lookup_template_function (template, arguments);
8829 }
8830
8831 /* Retrieve any deferred checks. Do not pop this access checks yet
8832 so the memory will not be reclaimed during token replacing below. */
8833 access_check = get_deferred_access_checks ();
8834
8835 /* If parsing tentatively, replace the sequence of tokens that makes
8836 up the template-id with a CPP_TEMPLATE_ID token. That way,
8837 should we re-parse the token stream, we will not have to repeat
8838 the effort required to do the parse, nor will we issue duplicate
8839 error messages about problems during instantiation of the
8840 template. */
8841 if (start_of_id)
8842 {
8843 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8844
8845 /* Reset the contents of the START_OF_ID token. */
8846 token->type = CPP_TEMPLATE_ID;
8847 token->value = build_tree_list (access_check, template_id);
8848 token->keyword = RID_MAX;
8849
8850 /* Purge all subsequent tokens. */
8851 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8852
8853 /* ??? Can we actually assume that, if template_id ==
8854 error_mark_node, we will have issued a diagnostic to the
8855 user, as opposed to simply marking the tentative parse as
8856 failed? */
8857 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8858 error ("parse error in template argument list");
8859 }
8860
8861 pop_deferring_access_checks ();
8862 return template_id;
8863 }
8864
8865 /* Parse a template-name.
8866
8867 template-name:
8868 identifier
8869
8870 The standard should actually say:
8871
8872 template-name:
8873 identifier
8874 operator-function-id
8875
8876 A defect report has been filed about this issue.
8877
8878 A conversion-function-id cannot be a template name because they cannot
8879 be part of a template-id. In fact, looking at this code:
8880
8881 a.operator K<int>()
8882
8883 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8884 It is impossible to call a templated conversion-function-id with an
8885 explicit argument list, since the only allowed template parameter is
8886 the type to which it is converting.
8887
8888 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8889 `template' keyword, in a construction like:
8890
8891 T::template f<3>()
8892
8893 In that case `f' is taken to be a template-name, even though there
8894 is no way of knowing for sure.
8895
8896 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8897 name refers to a set of overloaded functions, at least one of which
8898 is a template, or an IDENTIFIER_NODE with the name of the template,
8899 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8900 names are looked up inside uninstantiated templates. */
8901
8902 static tree
8903 cp_parser_template_name (cp_parser* parser,
8904 bool template_keyword_p,
8905 bool check_dependency_p,
8906 bool is_declaration,
8907 bool *is_identifier)
8908 {
8909 tree identifier;
8910 tree decl;
8911 tree fns;
8912
8913 /* If the next token is `operator', then we have either an
8914 operator-function-id or a conversion-function-id. */
8915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8916 {
8917 /* We don't know whether we're looking at an
8918 operator-function-id or a conversion-function-id. */
8919 cp_parser_parse_tentatively (parser);
8920 /* Try an operator-function-id. */
8921 identifier = cp_parser_operator_function_id (parser);
8922 /* If that didn't work, try a conversion-function-id. */
8923 if (!cp_parser_parse_definitely (parser))
8924 {
8925 cp_parser_error (parser, "expected template-name");
8926 return error_mark_node;
8927 }
8928 }
8929 /* Look for the identifier. */
8930 else
8931 identifier = cp_parser_identifier (parser);
8932
8933 /* If we didn't find an identifier, we don't have a template-id. */
8934 if (identifier == error_mark_node)
8935 return error_mark_node;
8936
8937 /* If the name immediately followed the `template' keyword, then it
8938 is a template-name. However, if the next token is not `<', then
8939 we do not treat it as a template-name, since it is not being used
8940 as part of a template-id. This enables us to handle constructs
8941 like:
8942
8943 template <typename T> struct S { S(); };
8944 template <typename T> S<T>::S();
8945
8946 correctly. We would treat `S' as a template -- if it were `S<T>'
8947 -- but we do not if there is no `<'. */
8948
8949 if (processing_template_decl
8950 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8951 {
8952 /* In a declaration, in a dependent context, we pretend that the
8953 "template" keyword was present in order to improve error
8954 recovery. For example, given:
8955
8956 template <typename T> void f(T::X<int>);
8957
8958 we want to treat "X<int>" as a template-id. */
8959 if (is_declaration
8960 && !template_keyword_p
8961 && parser->scope && TYPE_P (parser->scope)
8962 && check_dependency_p
8963 && dependent_type_p (parser->scope)
8964 /* Do not do this for dtors (or ctors), since they never
8965 need the template keyword before their name. */
8966 && !constructor_name_p (identifier, parser->scope))
8967 {
8968 cp_token_position start = 0;
8969
8970 /* Explain what went wrong. */
8971 error ("non-template %qD used as template", identifier);
8972 inform ("use %<%T::template %D%> to indicate that it is a template",
8973 parser->scope, identifier);
8974 /* If parsing tentatively, find the location of the "<" token. */
8975 if (cp_parser_simulate_error (parser))
8976 start = cp_lexer_token_position (parser->lexer, true);
8977 /* Parse the template arguments so that we can issue error
8978 messages about them. */
8979 cp_lexer_consume_token (parser->lexer);
8980 cp_parser_enclosed_template_argument_list (parser);
8981 /* Skip tokens until we find a good place from which to
8982 continue parsing. */
8983 cp_parser_skip_to_closing_parenthesis (parser,
8984 /*recovering=*/true,
8985 /*or_comma=*/true,
8986 /*consume_paren=*/false);
8987 /* If parsing tentatively, permanently remove the
8988 template argument list. That will prevent duplicate
8989 error messages from being issued about the missing
8990 "template" keyword. */
8991 if (start)
8992 cp_lexer_purge_tokens_after (parser->lexer, start);
8993 if (is_identifier)
8994 *is_identifier = true;
8995 return identifier;
8996 }
8997
8998 /* If the "template" keyword is present, then there is generally
8999 no point in doing name-lookup, so we just return IDENTIFIER.
9000 But, if the qualifying scope is non-dependent then we can
9001 (and must) do name-lookup normally. */
9002 if (template_keyword_p
9003 && (!parser->scope
9004 || (TYPE_P (parser->scope)
9005 && dependent_type_p (parser->scope))))
9006 return identifier;
9007 }
9008
9009 /* Look up the name. */
9010 decl = cp_parser_lookup_name (parser, identifier,
9011 none_type,
9012 /*is_template=*/false,
9013 /*is_namespace=*/false,
9014 check_dependency_p,
9015 /*ambiguous_decls=*/NULL);
9016 decl = maybe_get_template_decl_from_type_decl (decl);
9017
9018 /* If DECL is a template, then the name was a template-name. */
9019 if (TREE_CODE (decl) == TEMPLATE_DECL)
9020 ;
9021 else
9022 {
9023 tree fn = NULL_TREE;
9024
9025 /* The standard does not explicitly indicate whether a name that
9026 names a set of overloaded declarations, some of which are
9027 templates, is a template-name. However, such a name should
9028 be a template-name; otherwise, there is no way to form a
9029 template-id for the overloaded templates. */
9030 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9031 if (TREE_CODE (fns) == OVERLOAD)
9032 for (fn = fns; fn; fn = OVL_NEXT (fn))
9033 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9034 break;
9035
9036 if (!fn)
9037 {
9038 /* The name does not name a template. */
9039 cp_parser_error (parser, "expected template-name");
9040 return error_mark_node;
9041 }
9042 }
9043
9044 /* If DECL is dependent, and refers to a function, then just return
9045 its name; we will look it up again during template instantiation. */
9046 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9047 {
9048 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9049 if (TYPE_P (scope) && dependent_type_p (scope))
9050 return identifier;
9051 }
9052
9053 return decl;
9054 }
9055
9056 /* Parse a template-argument-list.
9057
9058 template-argument-list:
9059 template-argument
9060 template-argument-list , template-argument
9061
9062 Returns a TREE_VEC containing the arguments. */
9063
9064 static tree
9065 cp_parser_template_argument_list (cp_parser* parser)
9066 {
9067 tree fixed_args[10];
9068 unsigned n_args = 0;
9069 unsigned alloced = 10;
9070 tree *arg_ary = fixed_args;
9071 tree vec;
9072 bool saved_in_template_argument_list_p;
9073 bool saved_ice_p;
9074 bool saved_non_ice_p;
9075
9076 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9077 parser->in_template_argument_list_p = true;
9078 /* Even if the template-id appears in an integral
9079 constant-expression, the contents of the argument list do
9080 not. */
9081 saved_ice_p = parser->integral_constant_expression_p;
9082 parser->integral_constant_expression_p = false;
9083 saved_non_ice_p = parser->non_integral_constant_expression_p;
9084 parser->non_integral_constant_expression_p = false;
9085 /* Parse the arguments. */
9086 do
9087 {
9088 tree argument;
9089
9090 if (n_args)
9091 /* Consume the comma. */
9092 cp_lexer_consume_token (parser->lexer);
9093
9094 /* Parse the template-argument. */
9095 argument = cp_parser_template_argument (parser);
9096 if (n_args == alloced)
9097 {
9098 alloced *= 2;
9099
9100 if (arg_ary == fixed_args)
9101 {
9102 arg_ary = XNEWVEC (tree, alloced);
9103 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9104 }
9105 else
9106 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9107 }
9108 arg_ary[n_args++] = argument;
9109 }
9110 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9111
9112 vec = make_tree_vec (n_args);
9113
9114 while (n_args--)
9115 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9116
9117 if (arg_ary != fixed_args)
9118 free (arg_ary);
9119 parser->non_integral_constant_expression_p = saved_non_ice_p;
9120 parser->integral_constant_expression_p = saved_ice_p;
9121 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9122 return vec;
9123 }
9124
9125 /* Parse a template-argument.
9126
9127 template-argument:
9128 assignment-expression
9129 type-id
9130 id-expression
9131
9132 The representation is that of an assignment-expression, type-id, or
9133 id-expression -- except that the qualified id-expression is
9134 evaluated, so that the value returned is either a DECL or an
9135 OVERLOAD.
9136
9137 Although the standard says "assignment-expression", it forbids
9138 throw-expressions or assignments in the template argument.
9139 Therefore, we use "conditional-expression" instead. */
9140
9141 static tree
9142 cp_parser_template_argument (cp_parser* parser)
9143 {
9144 tree argument;
9145 bool template_p;
9146 bool address_p;
9147 bool maybe_type_id = false;
9148 cp_token *token;
9149 cp_id_kind idk;
9150
9151 /* There's really no way to know what we're looking at, so we just
9152 try each alternative in order.
9153
9154 [temp.arg]
9155
9156 In a template-argument, an ambiguity between a type-id and an
9157 expression is resolved to a type-id, regardless of the form of
9158 the corresponding template-parameter.
9159
9160 Therefore, we try a type-id first. */
9161 cp_parser_parse_tentatively (parser);
9162 argument = cp_parser_type_id (parser);
9163 /* If there was no error parsing the type-id but the next token is a '>>',
9164 we probably found a typo for '> >'. But there are type-id which are
9165 also valid expressions. For instance:
9166
9167 struct X { int operator >> (int); };
9168 template <int V> struct Foo {};
9169 Foo<X () >> 5> r;
9170
9171 Here 'X()' is a valid type-id of a function type, but the user just
9172 wanted to write the expression "X() >> 5". Thus, we remember that we
9173 found a valid type-id, but we still try to parse the argument as an
9174 expression to see what happens. */
9175 if (!cp_parser_error_occurred (parser)
9176 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9177 {
9178 maybe_type_id = true;
9179 cp_parser_abort_tentative_parse (parser);
9180 }
9181 else
9182 {
9183 /* If the next token isn't a `,' or a `>', then this argument wasn't
9184 really finished. This means that the argument is not a valid
9185 type-id. */
9186 if (!cp_parser_next_token_ends_template_argument_p (parser))
9187 cp_parser_error (parser, "expected template-argument");
9188 /* If that worked, we're done. */
9189 if (cp_parser_parse_definitely (parser))
9190 return argument;
9191 }
9192 /* We're still not sure what the argument will be. */
9193 cp_parser_parse_tentatively (parser);
9194 /* Try a template. */
9195 argument = cp_parser_id_expression (parser,
9196 /*template_keyword_p=*/false,
9197 /*check_dependency_p=*/true,
9198 &template_p,
9199 /*declarator_p=*/false,
9200 /*optional_p=*/false);
9201 /* If the next token isn't a `,' or a `>', then this argument wasn't
9202 really finished. */
9203 if (!cp_parser_next_token_ends_template_argument_p (parser))
9204 cp_parser_error (parser, "expected template-argument");
9205 if (!cp_parser_error_occurred (parser))
9206 {
9207 /* Figure out what is being referred to. If the id-expression
9208 was for a class template specialization, then we will have a
9209 TYPE_DECL at this point. There is no need to do name lookup
9210 at this point in that case. */
9211 if (TREE_CODE (argument) != TYPE_DECL)
9212 argument = cp_parser_lookup_name (parser, argument,
9213 none_type,
9214 /*is_template=*/template_p,
9215 /*is_namespace=*/false,
9216 /*check_dependency=*/true,
9217 /*ambiguous_decls=*/NULL);
9218 if (TREE_CODE (argument) != TEMPLATE_DECL
9219 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9220 cp_parser_error (parser, "expected template-name");
9221 }
9222 if (cp_parser_parse_definitely (parser))
9223 return argument;
9224 /* It must be a non-type argument. There permitted cases are given
9225 in [temp.arg.nontype]:
9226
9227 -- an integral constant-expression of integral or enumeration
9228 type; or
9229
9230 -- the name of a non-type template-parameter; or
9231
9232 -- the name of an object or function with external linkage...
9233
9234 -- the address of an object or function with external linkage...
9235
9236 -- a pointer to member... */
9237 /* Look for a non-type template parameter. */
9238 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9239 {
9240 cp_parser_parse_tentatively (parser);
9241 argument = cp_parser_primary_expression (parser,
9242 /*adress_p=*/false,
9243 /*cast_p=*/false,
9244 /*template_arg_p=*/true,
9245 &idk);
9246 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9247 || !cp_parser_next_token_ends_template_argument_p (parser))
9248 cp_parser_simulate_error (parser);
9249 if (cp_parser_parse_definitely (parser))
9250 return argument;
9251 }
9252
9253 /* If the next token is "&", the argument must be the address of an
9254 object or function with external linkage. */
9255 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9256 if (address_p)
9257 cp_lexer_consume_token (parser->lexer);
9258 /* See if we might have an id-expression. */
9259 token = cp_lexer_peek_token (parser->lexer);
9260 if (token->type == CPP_NAME
9261 || token->keyword == RID_OPERATOR
9262 || token->type == CPP_SCOPE
9263 || token->type == CPP_TEMPLATE_ID
9264 || token->type == CPP_NESTED_NAME_SPECIFIER)
9265 {
9266 cp_parser_parse_tentatively (parser);
9267 argument = cp_parser_primary_expression (parser,
9268 address_p,
9269 /*cast_p=*/false,
9270 /*template_arg_p=*/true,
9271 &idk);
9272 if (cp_parser_error_occurred (parser)
9273 || !cp_parser_next_token_ends_template_argument_p (parser))
9274 cp_parser_abort_tentative_parse (parser);
9275 else
9276 {
9277 if (TREE_CODE (argument) == INDIRECT_REF)
9278 {
9279 gcc_assert (REFERENCE_REF_P (argument));
9280 argument = TREE_OPERAND (argument, 0);
9281 }
9282
9283 if (TREE_CODE (argument) == VAR_DECL)
9284 {
9285 /* A variable without external linkage might still be a
9286 valid constant-expression, so no error is issued here
9287 if the external-linkage check fails. */
9288 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9289 cp_parser_simulate_error (parser);
9290 }
9291 else if (is_overloaded_fn (argument))
9292 /* All overloaded functions are allowed; if the external
9293 linkage test does not pass, an error will be issued
9294 later. */
9295 ;
9296 else if (address_p
9297 && (TREE_CODE (argument) == OFFSET_REF
9298 || TREE_CODE (argument) == SCOPE_REF))
9299 /* A pointer-to-member. */
9300 ;
9301 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9302 ;
9303 else
9304 cp_parser_simulate_error (parser);
9305
9306 if (cp_parser_parse_definitely (parser))
9307 {
9308 if (address_p)
9309 argument = build_x_unary_op (ADDR_EXPR, argument);
9310 return argument;
9311 }
9312 }
9313 }
9314 /* If the argument started with "&", there are no other valid
9315 alternatives at this point. */
9316 if (address_p)
9317 {
9318 cp_parser_error (parser, "invalid non-type template argument");
9319 return error_mark_node;
9320 }
9321
9322 /* If the argument wasn't successfully parsed as a type-id followed
9323 by '>>', the argument can only be a constant expression now.
9324 Otherwise, we try parsing the constant-expression tentatively,
9325 because the argument could really be a type-id. */
9326 if (maybe_type_id)
9327 cp_parser_parse_tentatively (parser);
9328 argument = cp_parser_constant_expression (parser,
9329 /*allow_non_constant_p=*/false,
9330 /*non_constant_p=*/NULL);
9331 argument = fold_non_dependent_expr (argument);
9332 if (!maybe_type_id)
9333 return argument;
9334 if (!cp_parser_next_token_ends_template_argument_p (parser))
9335 cp_parser_error (parser, "expected template-argument");
9336 if (cp_parser_parse_definitely (parser))
9337 return argument;
9338 /* We did our best to parse the argument as a non type-id, but that
9339 was the only alternative that matched (albeit with a '>' after
9340 it). We can assume it's just a typo from the user, and a
9341 diagnostic will then be issued. */
9342 return cp_parser_type_id (parser);
9343 }
9344
9345 /* Parse an explicit-instantiation.
9346
9347 explicit-instantiation:
9348 template declaration
9349
9350 Although the standard says `declaration', what it really means is:
9351
9352 explicit-instantiation:
9353 template decl-specifier-seq [opt] declarator [opt] ;
9354
9355 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9356 supposed to be allowed. A defect report has been filed about this
9357 issue.
9358
9359 GNU Extension:
9360
9361 explicit-instantiation:
9362 storage-class-specifier template
9363 decl-specifier-seq [opt] declarator [opt] ;
9364 function-specifier template
9365 decl-specifier-seq [opt] declarator [opt] ; */
9366
9367 static void
9368 cp_parser_explicit_instantiation (cp_parser* parser)
9369 {
9370 int declares_class_or_enum;
9371 cp_decl_specifier_seq decl_specifiers;
9372 tree extension_specifier = NULL_TREE;
9373
9374 /* Look for an (optional) storage-class-specifier or
9375 function-specifier. */
9376 if (cp_parser_allow_gnu_extensions_p (parser))
9377 {
9378 extension_specifier
9379 = cp_parser_storage_class_specifier_opt (parser);
9380 if (!extension_specifier)
9381 extension_specifier
9382 = cp_parser_function_specifier_opt (parser,
9383 /*decl_specs=*/NULL);
9384 }
9385
9386 /* Look for the `template' keyword. */
9387 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9388 /* Let the front end know that we are processing an explicit
9389 instantiation. */
9390 begin_explicit_instantiation ();
9391 /* [temp.explicit] says that we are supposed to ignore access
9392 control while processing explicit instantiation directives. */
9393 push_deferring_access_checks (dk_no_check);
9394 /* Parse a decl-specifier-seq. */
9395 cp_parser_decl_specifier_seq (parser,
9396 CP_PARSER_FLAGS_OPTIONAL,
9397 &decl_specifiers,
9398 &declares_class_or_enum);
9399 /* If there was exactly one decl-specifier, and it declared a class,
9400 and there's no declarator, then we have an explicit type
9401 instantiation. */
9402 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9403 {
9404 tree type;
9405
9406 type = check_tag_decl (&decl_specifiers);
9407 /* Turn access control back on for names used during
9408 template instantiation. */
9409 pop_deferring_access_checks ();
9410 if (type)
9411 do_type_instantiation (type, extension_specifier,
9412 /*complain=*/tf_error);
9413 }
9414 else
9415 {
9416 cp_declarator *declarator;
9417 tree decl;
9418
9419 /* Parse the declarator. */
9420 declarator
9421 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9422 /*ctor_dtor_or_conv_p=*/NULL,
9423 /*parenthesized_p=*/NULL,
9424 /*member_p=*/false);
9425 if (declares_class_or_enum & 2)
9426 cp_parser_check_for_definition_in_return_type (declarator,
9427 decl_specifiers.type);
9428 if (declarator != cp_error_declarator)
9429 {
9430 decl = grokdeclarator (declarator, &decl_specifiers,
9431 NORMAL, 0, &decl_specifiers.attributes);
9432 /* Turn access control back on for names used during
9433 template instantiation. */
9434 pop_deferring_access_checks ();
9435 /* Do the explicit instantiation. */
9436 do_decl_instantiation (decl, extension_specifier);
9437 }
9438 else
9439 {
9440 pop_deferring_access_checks ();
9441 /* Skip the body of the explicit instantiation. */
9442 cp_parser_skip_to_end_of_statement (parser);
9443 }
9444 }
9445 /* We're done with the instantiation. */
9446 end_explicit_instantiation ();
9447
9448 cp_parser_consume_semicolon_at_end_of_statement (parser);
9449 }
9450
9451 /* Parse an explicit-specialization.
9452
9453 explicit-specialization:
9454 template < > declaration
9455
9456 Although the standard says `declaration', what it really means is:
9457
9458 explicit-specialization:
9459 template <> decl-specifier [opt] init-declarator [opt] ;
9460 template <> function-definition
9461 template <> explicit-specialization
9462 template <> template-declaration */
9463
9464 static void
9465 cp_parser_explicit_specialization (cp_parser* parser)
9466 {
9467 bool need_lang_pop;
9468 /* Look for the `template' keyword. */
9469 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9470 /* Look for the `<'. */
9471 cp_parser_require (parser, CPP_LESS, "`<'");
9472 /* Look for the `>'. */
9473 cp_parser_require (parser, CPP_GREATER, "`>'");
9474 /* We have processed another parameter list. */
9475 ++parser->num_template_parameter_lists;
9476 /* [temp]
9477
9478 A template ... explicit specialization ... shall not have C
9479 linkage. */
9480 if (current_lang_name == lang_name_c)
9481 {
9482 error ("template specialization with C linkage");
9483 /* Give it C++ linkage to avoid confusing other parts of the
9484 front end. */
9485 push_lang_context (lang_name_cplusplus);
9486 need_lang_pop = true;
9487 }
9488 else
9489 need_lang_pop = false;
9490 /* Let the front end know that we are beginning a specialization. */
9491 if (!begin_specialization ())
9492 {
9493 end_specialization ();
9494 cp_parser_skip_to_end_of_block_or_statement (parser);
9495 return;
9496 }
9497
9498 /* If the next keyword is `template', we need to figure out whether
9499 or not we're looking a template-declaration. */
9500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9501 {
9502 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9503 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9504 cp_parser_template_declaration_after_export (parser,
9505 /*member_p=*/false);
9506 else
9507 cp_parser_explicit_specialization (parser);
9508 }
9509 else
9510 /* Parse the dependent declaration. */
9511 cp_parser_single_declaration (parser,
9512 /*checks=*/NULL_TREE,
9513 /*member_p=*/false,
9514 /*friend_p=*/NULL);
9515 /* We're done with the specialization. */
9516 end_specialization ();
9517 /* For the erroneous case of a template with C linkage, we pushed an
9518 implicit C++ linkage scope; exit that scope now. */
9519 if (need_lang_pop)
9520 pop_lang_context ();
9521 /* We're done with this parameter list. */
9522 --parser->num_template_parameter_lists;
9523 }
9524
9525 /* Parse a type-specifier.
9526
9527 type-specifier:
9528 simple-type-specifier
9529 class-specifier
9530 enum-specifier
9531 elaborated-type-specifier
9532 cv-qualifier
9533
9534 GNU Extension:
9535
9536 type-specifier:
9537 __complex__
9538
9539 Returns a representation of the type-specifier. For a
9540 class-specifier, enum-specifier, or elaborated-type-specifier, a
9541 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9542
9543 The parser flags FLAGS is used to control type-specifier parsing.
9544
9545 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9546 in a decl-specifier-seq.
9547
9548 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9549 class-specifier, enum-specifier, or elaborated-type-specifier, then
9550 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9551 if a type is declared; 2 if it is defined. Otherwise, it is set to
9552 zero.
9553
9554 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9555 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9556 is set to FALSE. */
9557
9558 static tree
9559 cp_parser_type_specifier (cp_parser* parser,
9560 cp_parser_flags flags,
9561 cp_decl_specifier_seq *decl_specs,
9562 bool is_declaration,
9563 int* declares_class_or_enum,
9564 bool* is_cv_qualifier)
9565 {
9566 tree type_spec = NULL_TREE;
9567 cp_token *token;
9568 enum rid keyword;
9569 cp_decl_spec ds = ds_last;
9570
9571 /* Assume this type-specifier does not declare a new type. */
9572 if (declares_class_or_enum)
9573 *declares_class_or_enum = 0;
9574 /* And that it does not specify a cv-qualifier. */
9575 if (is_cv_qualifier)
9576 *is_cv_qualifier = false;
9577 /* Peek at the next token. */
9578 token = cp_lexer_peek_token (parser->lexer);
9579
9580 /* If we're looking at a keyword, we can use that to guide the
9581 production we choose. */
9582 keyword = token->keyword;
9583 switch (keyword)
9584 {
9585 case RID_ENUM:
9586 /* Look for the enum-specifier. */
9587 type_spec = cp_parser_enum_specifier (parser);
9588 /* If that worked, we're done. */
9589 if (type_spec)
9590 {
9591 if (declares_class_or_enum)
9592 *declares_class_or_enum = 2;
9593 if (decl_specs)
9594 cp_parser_set_decl_spec_type (decl_specs,
9595 type_spec,
9596 /*user_defined_p=*/true);
9597 return type_spec;
9598 }
9599 else
9600 goto elaborated_type_specifier;
9601
9602 /* Any of these indicate either a class-specifier, or an
9603 elaborated-type-specifier. */
9604 case RID_CLASS:
9605 case RID_STRUCT:
9606 case RID_UNION:
9607 /* Parse tentatively so that we can back up if we don't find a
9608 class-specifier. */
9609 cp_parser_parse_tentatively (parser);
9610 /* Look for the class-specifier. */
9611 type_spec = cp_parser_class_specifier (parser);
9612 /* If that worked, we're done. */
9613 if (cp_parser_parse_definitely (parser))
9614 {
9615 if (declares_class_or_enum)
9616 *declares_class_or_enum = 2;
9617 if (decl_specs)
9618 cp_parser_set_decl_spec_type (decl_specs,
9619 type_spec,
9620 /*user_defined_p=*/true);
9621 return type_spec;
9622 }
9623
9624 /* Fall through. */
9625 elaborated_type_specifier:
9626 /* We're declaring (not defining) a class or enum. */
9627 if (declares_class_or_enum)
9628 *declares_class_or_enum = 1;
9629
9630 /* Fall through. */
9631 case RID_TYPENAME:
9632 /* Look for an elaborated-type-specifier. */
9633 type_spec
9634 = (cp_parser_elaborated_type_specifier
9635 (parser,
9636 decl_specs && decl_specs->specs[(int) ds_friend],
9637 is_declaration));
9638 if (decl_specs)
9639 cp_parser_set_decl_spec_type (decl_specs,
9640 type_spec,
9641 /*user_defined_p=*/true);
9642 return type_spec;
9643
9644 case RID_CONST:
9645 ds = ds_const;
9646 if (is_cv_qualifier)
9647 *is_cv_qualifier = true;
9648 break;
9649
9650 case RID_VOLATILE:
9651 ds = ds_volatile;
9652 if (is_cv_qualifier)
9653 *is_cv_qualifier = true;
9654 break;
9655
9656 case RID_RESTRICT:
9657 ds = ds_restrict;
9658 if (is_cv_qualifier)
9659 *is_cv_qualifier = true;
9660 break;
9661
9662 case RID_COMPLEX:
9663 /* The `__complex__' keyword is a GNU extension. */
9664 ds = ds_complex;
9665 break;
9666
9667 default:
9668 break;
9669 }
9670
9671 /* Handle simple keywords. */
9672 if (ds != ds_last)
9673 {
9674 if (decl_specs)
9675 {
9676 ++decl_specs->specs[(int)ds];
9677 decl_specs->any_specifiers_p = true;
9678 }
9679 return cp_lexer_consume_token (parser->lexer)->value;
9680 }
9681
9682 /* If we do not already have a type-specifier, assume we are looking
9683 at a simple-type-specifier. */
9684 type_spec = cp_parser_simple_type_specifier (parser,
9685 decl_specs,
9686 flags);
9687
9688 /* If we didn't find a type-specifier, and a type-specifier was not
9689 optional in this context, issue an error message. */
9690 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9691 {
9692 cp_parser_error (parser, "expected type specifier");
9693 return error_mark_node;
9694 }
9695
9696 return type_spec;
9697 }
9698
9699 /* Parse a simple-type-specifier.
9700
9701 simple-type-specifier:
9702 :: [opt] nested-name-specifier [opt] type-name
9703 :: [opt] nested-name-specifier template template-id
9704 char
9705 wchar_t
9706 bool
9707 short
9708 int
9709 long
9710 signed
9711 unsigned
9712 float
9713 double
9714 void
9715
9716 GNU Extension:
9717
9718 simple-type-specifier:
9719 __typeof__ unary-expression
9720 __typeof__ ( type-id )
9721
9722 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9723 appropriately updated. */
9724
9725 static tree
9726 cp_parser_simple_type_specifier (cp_parser* parser,
9727 cp_decl_specifier_seq *decl_specs,
9728 cp_parser_flags flags)
9729 {
9730 tree type = NULL_TREE;
9731 cp_token *token;
9732
9733 /* Peek at the next token. */
9734 token = cp_lexer_peek_token (parser->lexer);
9735
9736 /* If we're looking at a keyword, things are easy. */
9737 switch (token->keyword)
9738 {
9739 case RID_CHAR:
9740 if (decl_specs)
9741 decl_specs->explicit_char_p = true;
9742 type = char_type_node;
9743 break;
9744 case RID_WCHAR:
9745 type = wchar_type_node;
9746 break;
9747 case RID_BOOL:
9748 type = boolean_type_node;
9749 break;
9750 case RID_SHORT:
9751 if (decl_specs)
9752 ++decl_specs->specs[(int) ds_short];
9753 type = short_integer_type_node;
9754 break;
9755 case RID_INT:
9756 if (decl_specs)
9757 decl_specs->explicit_int_p = true;
9758 type = integer_type_node;
9759 break;
9760 case RID_LONG:
9761 if (decl_specs)
9762 ++decl_specs->specs[(int) ds_long];
9763 type = long_integer_type_node;
9764 break;
9765 case RID_SIGNED:
9766 if (decl_specs)
9767 ++decl_specs->specs[(int) ds_signed];
9768 type = integer_type_node;
9769 break;
9770 case RID_UNSIGNED:
9771 if (decl_specs)
9772 ++decl_specs->specs[(int) ds_unsigned];
9773 type = unsigned_type_node;
9774 break;
9775 case RID_FLOAT:
9776 type = float_type_node;
9777 break;
9778 case RID_DOUBLE:
9779 type = double_type_node;
9780 break;
9781 case RID_VOID:
9782 type = void_type_node;
9783 break;
9784
9785 case RID_TYPEOF:
9786 /* Consume the `typeof' token. */
9787 cp_lexer_consume_token (parser->lexer);
9788 /* Parse the operand to `typeof'. */
9789 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9790 /* If it is not already a TYPE, take its type. */
9791 if (!TYPE_P (type))
9792 type = finish_typeof (type);
9793
9794 if (decl_specs)
9795 cp_parser_set_decl_spec_type (decl_specs, type,
9796 /*user_defined_p=*/true);
9797
9798 return type;
9799
9800 default:
9801 break;
9802 }
9803
9804 /* If the type-specifier was for a built-in type, we're done. */
9805 if (type)
9806 {
9807 tree id;
9808
9809 /* Record the type. */
9810 if (decl_specs
9811 && (token->keyword != RID_SIGNED
9812 && token->keyword != RID_UNSIGNED
9813 && token->keyword != RID_SHORT
9814 && token->keyword != RID_LONG))
9815 cp_parser_set_decl_spec_type (decl_specs,
9816 type,
9817 /*user_defined=*/false);
9818 if (decl_specs)
9819 decl_specs->any_specifiers_p = true;
9820
9821 /* Consume the token. */
9822 id = cp_lexer_consume_token (parser->lexer)->value;
9823
9824 /* There is no valid C++ program where a non-template type is
9825 followed by a "<". That usually indicates that the user thought
9826 that the type was a template. */
9827 cp_parser_check_for_invalid_template_id (parser, type);
9828
9829 return TYPE_NAME (type);
9830 }
9831
9832 /* The type-specifier must be a user-defined type. */
9833 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9834 {
9835 bool qualified_p;
9836 bool global_p;
9837
9838 /* Don't gobble tokens or issue error messages if this is an
9839 optional type-specifier. */
9840 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9841 cp_parser_parse_tentatively (parser);
9842
9843 /* Look for the optional `::' operator. */
9844 global_p
9845 = (cp_parser_global_scope_opt (parser,
9846 /*current_scope_valid_p=*/false)
9847 != NULL_TREE);
9848 /* Look for the nested-name specifier. */
9849 qualified_p
9850 = (cp_parser_nested_name_specifier_opt (parser,
9851 /*typename_keyword_p=*/false,
9852 /*check_dependency_p=*/true,
9853 /*type_p=*/false,
9854 /*is_declaration=*/false)
9855 != NULL_TREE);
9856 /* If we have seen a nested-name-specifier, and the next token
9857 is `template', then we are using the template-id production. */
9858 if (parser->scope
9859 && cp_parser_optional_template_keyword (parser))
9860 {
9861 /* Look for the template-id. */
9862 type = cp_parser_template_id (parser,
9863 /*template_keyword_p=*/true,
9864 /*check_dependency_p=*/true,
9865 /*is_declaration=*/false);
9866 /* If the template-id did not name a type, we are out of
9867 luck. */
9868 if (TREE_CODE (type) != TYPE_DECL)
9869 {
9870 cp_parser_error (parser, "expected template-id for type");
9871 type = NULL_TREE;
9872 }
9873 }
9874 /* Otherwise, look for a type-name. */
9875 else
9876 type = cp_parser_type_name (parser);
9877 /* Keep track of all name-lookups performed in class scopes. */
9878 if (type
9879 && !global_p
9880 && !qualified_p
9881 && TREE_CODE (type) == TYPE_DECL
9882 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9883 maybe_note_name_used_in_class (DECL_NAME (type), type);
9884 /* If it didn't work out, we don't have a TYPE. */
9885 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9886 && !cp_parser_parse_definitely (parser))
9887 type = NULL_TREE;
9888 if (type && decl_specs)
9889 cp_parser_set_decl_spec_type (decl_specs, type,
9890 /*user_defined=*/true);
9891 }
9892
9893 /* If we didn't get a type-name, issue an error message. */
9894 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9895 {
9896 cp_parser_error (parser, "expected type-name");
9897 return error_mark_node;
9898 }
9899
9900 /* There is no valid C++ program where a non-template type is
9901 followed by a "<". That usually indicates that the user thought
9902 that the type was a template. */
9903 if (type && type != error_mark_node)
9904 {
9905 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9906 If it is, then the '<'...'>' enclose protocol names rather than
9907 template arguments, and so everything is fine. */
9908 if (c_dialect_objc ()
9909 && (objc_is_id (type) || objc_is_class_name (type)))
9910 {
9911 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9912 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9913
9914 /* Clobber the "unqualified" type previously entered into
9915 DECL_SPECS with the new, improved protocol-qualified version. */
9916 if (decl_specs)
9917 decl_specs->type = qual_type;
9918
9919 return qual_type;
9920 }
9921
9922 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9923 }
9924
9925 return type;
9926 }
9927
9928 /* Parse a type-name.
9929
9930 type-name:
9931 class-name
9932 enum-name
9933 typedef-name
9934
9935 enum-name:
9936 identifier
9937
9938 typedef-name:
9939 identifier
9940
9941 Returns a TYPE_DECL for the type. */
9942
9943 static tree
9944 cp_parser_type_name (cp_parser* parser)
9945 {
9946 tree type_decl;
9947 tree identifier;
9948
9949 /* We can't know yet whether it is a class-name or not. */
9950 cp_parser_parse_tentatively (parser);
9951 /* Try a class-name. */
9952 type_decl = cp_parser_class_name (parser,
9953 /*typename_keyword_p=*/false,
9954 /*template_keyword_p=*/false,
9955 none_type,
9956 /*check_dependency_p=*/true,
9957 /*class_head_p=*/false,
9958 /*is_declaration=*/false);
9959 /* If it's not a class-name, keep looking. */
9960 if (!cp_parser_parse_definitely (parser))
9961 {
9962 /* It must be a typedef-name or an enum-name. */
9963 identifier = cp_parser_identifier (parser);
9964 if (identifier == error_mark_node)
9965 return error_mark_node;
9966
9967 /* Look up the type-name. */
9968 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9969
9970 if (TREE_CODE (type_decl) != TYPE_DECL
9971 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9972 {
9973 /* See if this is an Objective-C type. */
9974 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9975 tree type = objc_get_protocol_qualified_type (identifier, protos);
9976 if (type)
9977 type_decl = TYPE_NAME (type);
9978 }
9979
9980 /* Issue an error if we did not find a type-name. */
9981 if (TREE_CODE (type_decl) != TYPE_DECL)
9982 {
9983 if (!cp_parser_simulate_error (parser))
9984 cp_parser_name_lookup_error (parser, identifier, type_decl,
9985 "is not a type");
9986 type_decl = error_mark_node;
9987 }
9988 /* Remember that the name was used in the definition of the
9989 current class so that we can check later to see if the
9990 meaning would have been different after the class was
9991 entirely defined. */
9992 else if (type_decl != error_mark_node
9993 && !parser->scope)
9994 maybe_note_name_used_in_class (identifier, type_decl);
9995 }
9996
9997 return type_decl;
9998 }
9999
10000
10001 /* Parse an elaborated-type-specifier. Note that the grammar given
10002 here incorporates the resolution to DR68.
10003
10004 elaborated-type-specifier:
10005 class-key :: [opt] nested-name-specifier [opt] identifier
10006 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10007 enum :: [opt] nested-name-specifier [opt] identifier
10008 typename :: [opt] nested-name-specifier identifier
10009 typename :: [opt] nested-name-specifier template [opt]
10010 template-id
10011
10012 GNU extension:
10013
10014 elaborated-type-specifier:
10015 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10016 class-key attributes :: [opt] nested-name-specifier [opt]
10017 template [opt] template-id
10018 enum attributes :: [opt] nested-name-specifier [opt] identifier
10019
10020 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10021 declared `friend'. If IS_DECLARATION is TRUE, then this
10022 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10023 something is being declared.
10024
10025 Returns the TYPE specified. */
10026
10027 static tree
10028 cp_parser_elaborated_type_specifier (cp_parser* parser,
10029 bool is_friend,
10030 bool is_declaration)
10031 {
10032 enum tag_types tag_type;
10033 tree identifier;
10034 tree type = NULL_TREE;
10035 tree attributes = NULL_TREE;
10036
10037 /* See if we're looking at the `enum' keyword. */
10038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10039 {
10040 /* Consume the `enum' token. */
10041 cp_lexer_consume_token (parser->lexer);
10042 /* Remember that it's an enumeration type. */
10043 tag_type = enum_type;
10044 /* Parse the attributes. */
10045 attributes = cp_parser_attributes_opt (parser);
10046 }
10047 /* Or, it might be `typename'. */
10048 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10049 RID_TYPENAME))
10050 {
10051 /* Consume the `typename' token. */
10052 cp_lexer_consume_token (parser->lexer);
10053 /* Remember that it's a `typename' type. */
10054 tag_type = typename_type;
10055 /* The `typename' keyword is only allowed in templates. */
10056 if (!processing_template_decl)
10057 pedwarn ("using %<typename%> outside of template");
10058 }
10059 /* Otherwise it must be a class-key. */
10060 else
10061 {
10062 tag_type = cp_parser_class_key (parser);
10063 if (tag_type == none_type)
10064 return error_mark_node;
10065 /* Parse the attributes. */
10066 attributes = cp_parser_attributes_opt (parser);
10067 }
10068
10069 /* Look for the `::' operator. */
10070 cp_parser_global_scope_opt (parser,
10071 /*current_scope_valid_p=*/false);
10072 /* Look for the nested-name-specifier. */
10073 if (tag_type == typename_type)
10074 {
10075 if (!cp_parser_nested_name_specifier (parser,
10076 /*typename_keyword_p=*/true,
10077 /*check_dependency_p=*/true,
10078 /*type_p=*/true,
10079 is_declaration))
10080 return error_mark_node;
10081 }
10082 else
10083 /* Even though `typename' is not present, the proposed resolution
10084 to Core Issue 180 says that in `class A<T>::B', `B' should be
10085 considered a type-name, even if `A<T>' is dependent. */
10086 cp_parser_nested_name_specifier_opt (parser,
10087 /*typename_keyword_p=*/true,
10088 /*check_dependency_p=*/true,
10089 /*type_p=*/true,
10090 is_declaration);
10091 /* For everything but enumeration types, consider a template-id. */
10092 /* For an enumeration type, consider only a plain identifier. */
10093 if (tag_type != enum_type)
10094 {
10095 bool template_p = false;
10096 tree decl;
10097
10098 /* Allow the `template' keyword. */
10099 template_p = cp_parser_optional_template_keyword (parser);
10100 /* If we didn't see `template', we don't know if there's a
10101 template-id or not. */
10102 if (!template_p)
10103 cp_parser_parse_tentatively (parser);
10104 /* Parse the template-id. */
10105 decl = cp_parser_template_id (parser, template_p,
10106 /*check_dependency_p=*/true,
10107 is_declaration);
10108 /* If we didn't find a template-id, look for an ordinary
10109 identifier. */
10110 if (!template_p && !cp_parser_parse_definitely (parser))
10111 ;
10112 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10113 in effect, then we must assume that, upon instantiation, the
10114 template will correspond to a class. */
10115 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10116 && tag_type == typename_type)
10117 type = make_typename_type (parser->scope, decl,
10118 typename_type,
10119 /*complain=*/tf_error);
10120 else
10121 type = TREE_TYPE (decl);
10122 }
10123
10124 if (!type)
10125 {
10126 identifier = cp_parser_identifier (parser);
10127
10128 if (identifier == error_mark_node)
10129 {
10130 parser->scope = NULL_TREE;
10131 return error_mark_node;
10132 }
10133
10134 /* For a `typename', we needn't call xref_tag. */
10135 if (tag_type == typename_type
10136 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10137 return cp_parser_make_typename_type (parser, parser->scope,
10138 identifier);
10139 /* Look up a qualified name in the usual way. */
10140 if (parser->scope)
10141 {
10142 tree decl;
10143
10144 decl = cp_parser_lookup_name (parser, identifier,
10145 tag_type,
10146 /*is_template=*/false,
10147 /*is_namespace=*/false,
10148 /*check_dependency=*/true,
10149 /*ambiguous_decls=*/NULL);
10150
10151 /* If we are parsing friend declaration, DECL may be a
10152 TEMPLATE_DECL tree node here. However, we need to check
10153 whether this TEMPLATE_DECL results in valid code. Consider
10154 the following example:
10155
10156 namespace N {
10157 template <class T> class C {};
10158 }
10159 class X {
10160 template <class T> friend class N::C; // #1, valid code
10161 };
10162 template <class T> class Y {
10163 friend class N::C; // #2, invalid code
10164 };
10165
10166 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10167 name lookup of `N::C'. We see that friend declaration must
10168 be template for the code to be valid. Note that
10169 processing_template_decl does not work here since it is
10170 always 1 for the above two cases. */
10171
10172 decl = (cp_parser_maybe_treat_template_as_class
10173 (decl, /*tag_name_p=*/is_friend
10174 && parser->num_template_parameter_lists));
10175
10176 if (TREE_CODE (decl) != TYPE_DECL)
10177 {
10178 cp_parser_diagnose_invalid_type_name (parser,
10179 parser->scope,
10180 identifier);
10181 return error_mark_node;
10182 }
10183
10184 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10185 check_elaborated_type_specifier
10186 (tag_type, decl,
10187 (parser->num_template_parameter_lists
10188 || DECL_SELF_REFERENCE_P (decl)));
10189
10190 type = TREE_TYPE (decl);
10191 }
10192 else
10193 {
10194 /* An elaborated-type-specifier sometimes introduces a new type and
10195 sometimes names an existing type. Normally, the rule is that it
10196 introduces a new type only if there is not an existing type of
10197 the same name already in scope. For example, given:
10198
10199 struct S {};
10200 void f() { struct S s; }
10201
10202 the `struct S' in the body of `f' is the same `struct S' as in
10203 the global scope; the existing definition is used. However, if
10204 there were no global declaration, this would introduce a new
10205 local class named `S'.
10206
10207 An exception to this rule applies to the following code:
10208
10209 namespace N { struct S; }
10210
10211 Here, the elaborated-type-specifier names a new type
10212 unconditionally; even if there is already an `S' in the
10213 containing scope this declaration names a new type.
10214 This exception only applies if the elaborated-type-specifier
10215 forms the complete declaration:
10216
10217 [class.name]
10218
10219 A declaration consisting solely of `class-key identifier ;' is
10220 either a redeclaration of the name in the current scope or a
10221 forward declaration of the identifier as a class name. It
10222 introduces the name into the current scope.
10223
10224 We are in this situation precisely when the next token is a `;'.
10225
10226 An exception to the exception is that a `friend' declaration does
10227 *not* name a new type; i.e., given:
10228
10229 struct S { friend struct T; };
10230
10231 `T' is not a new type in the scope of `S'.
10232
10233 Also, `new struct S' or `sizeof (struct S)' never results in the
10234 definition of a new type; a new type can only be declared in a
10235 declaration context. */
10236
10237 tag_scope ts;
10238 bool template_p;
10239
10240 if (is_friend)
10241 /* Friends have special name lookup rules. */
10242 ts = ts_within_enclosing_non_class;
10243 else if (is_declaration
10244 && cp_lexer_next_token_is (parser->lexer,
10245 CPP_SEMICOLON))
10246 /* This is a `class-key identifier ;' */
10247 ts = ts_current;
10248 else
10249 ts = ts_global;
10250
10251 template_p =
10252 (parser->num_template_parameter_lists
10253 && (cp_parser_next_token_starts_class_definition_p (parser)
10254 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10255 /* An unqualified name was used to reference this type, so
10256 there were no qualifying templates. */
10257 if (!cp_parser_check_template_parameters (parser,
10258 /*num_templates=*/0))
10259 return error_mark_node;
10260 type = xref_tag (tag_type, identifier, ts, template_p);
10261 }
10262 }
10263
10264 if (type == error_mark_node)
10265 return error_mark_node;
10266
10267 /* Allow attributes on forward declarations of classes. */
10268 if (attributes)
10269 {
10270 if (TREE_CODE (type) == TYPENAME_TYPE)
10271 warning (OPT_Wattributes,
10272 "attributes ignored on uninstantiated type");
10273 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10274 && ! processing_explicit_instantiation)
10275 warning (OPT_Wattributes,
10276 "attributes ignored on template instantiation");
10277 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10278 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10279 else
10280 warning (OPT_Wattributes,
10281 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10282 }
10283
10284 if (tag_type != enum_type)
10285 cp_parser_check_class_key (tag_type, type);
10286
10287 /* A "<" cannot follow an elaborated type specifier. If that
10288 happens, the user was probably trying to form a template-id. */
10289 cp_parser_check_for_invalid_template_id (parser, type);
10290
10291 return type;
10292 }
10293
10294 /* Parse an enum-specifier.
10295
10296 enum-specifier:
10297 enum identifier [opt] { enumerator-list [opt] }
10298
10299 GNU Extensions:
10300 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10301 attributes[opt]
10302
10303 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10304 if the token stream isn't an enum-specifier after all. */
10305
10306 static tree
10307 cp_parser_enum_specifier (cp_parser* parser)
10308 {
10309 tree identifier;
10310 tree type;
10311 tree attributes;
10312
10313 /* Parse tentatively so that we can back up if we don't find a
10314 enum-specifier. */
10315 cp_parser_parse_tentatively (parser);
10316
10317 /* Caller guarantees that the current token is 'enum', an identifier
10318 possibly follows, and the token after that is an opening brace.
10319 If we don't have an identifier, fabricate an anonymous name for
10320 the enumeration being defined. */
10321 cp_lexer_consume_token (parser->lexer);
10322
10323 attributes = cp_parser_attributes_opt (parser);
10324
10325 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10326 identifier = cp_parser_identifier (parser);
10327 else
10328 identifier = make_anon_name ();
10329
10330 /* Look for the `{' but don't consume it yet. */
10331 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10332 cp_parser_simulate_error (parser);
10333
10334 if (!cp_parser_parse_definitely (parser))
10335 return NULL_TREE;
10336
10337 /* Issue an error message if type-definitions are forbidden here. */
10338 cp_parser_check_type_definition (parser);
10339
10340 /* Create the new type. We do this before consuming the opening brace
10341 so the enum will be recorded as being on the line of its tag (or the
10342 'enum' keyword, if there is no tag). */
10343 type = start_enum (identifier);
10344
10345 /* Consume the opening brace. */
10346 cp_lexer_consume_token (parser->lexer);
10347
10348 if (type == error_mark_node)
10349 {
10350 cp_parser_skip_to_end_of_block_or_statement (parser);
10351 return error_mark_node;
10352 }
10353
10354 /* If the next token is not '}', then there are some enumerators. */
10355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10356 cp_parser_enumerator_list (parser, type);
10357
10358 /* Consume the final '}'. */
10359 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10360
10361 /* Look for trailing attributes to apply to this enumeration, and
10362 apply them if appropriate. */
10363 if (cp_parser_allow_gnu_extensions_p (parser))
10364 {
10365 tree trailing_attr = cp_parser_attributes_opt (parser);
10366 cplus_decl_attributes (&type,
10367 trailing_attr,
10368 (int) ATTR_FLAG_TYPE_IN_PLACE);
10369 }
10370
10371 /* Finish up the enumeration. */
10372 finish_enum (type);
10373
10374 return type;
10375 }
10376
10377 /* Parse an enumerator-list. The enumerators all have the indicated
10378 TYPE.
10379
10380 enumerator-list:
10381 enumerator-definition
10382 enumerator-list , enumerator-definition */
10383
10384 static void
10385 cp_parser_enumerator_list (cp_parser* parser, tree type)
10386 {
10387 while (true)
10388 {
10389 /* Parse an enumerator-definition. */
10390 cp_parser_enumerator_definition (parser, type);
10391
10392 /* If the next token is not a ',', we've reached the end of
10393 the list. */
10394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10395 break;
10396 /* Otherwise, consume the `,' and keep going. */
10397 cp_lexer_consume_token (parser->lexer);
10398 /* If the next token is a `}', there is a trailing comma. */
10399 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10400 {
10401 if (pedantic && !in_system_header)
10402 pedwarn ("comma at end of enumerator list");
10403 break;
10404 }
10405 }
10406 }
10407
10408 /* Parse an enumerator-definition. The enumerator has the indicated
10409 TYPE.
10410
10411 enumerator-definition:
10412 enumerator
10413 enumerator = constant-expression
10414
10415 enumerator:
10416 identifier */
10417
10418 static void
10419 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10420 {
10421 tree identifier;
10422 tree value;
10423
10424 /* Look for the identifier. */
10425 identifier = cp_parser_identifier (parser);
10426 if (identifier == error_mark_node)
10427 return;
10428
10429 /* If the next token is an '=', then there is an explicit value. */
10430 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10431 {
10432 /* Consume the `=' token. */
10433 cp_lexer_consume_token (parser->lexer);
10434 /* Parse the value. */
10435 value = cp_parser_constant_expression (parser,
10436 /*allow_non_constant_p=*/false,
10437 NULL);
10438 }
10439 else
10440 value = NULL_TREE;
10441
10442 /* Create the enumerator. */
10443 build_enumerator (identifier, value, type);
10444 }
10445
10446 /* Parse a namespace-name.
10447
10448 namespace-name:
10449 original-namespace-name
10450 namespace-alias
10451
10452 Returns the NAMESPACE_DECL for the namespace. */
10453
10454 static tree
10455 cp_parser_namespace_name (cp_parser* parser)
10456 {
10457 tree identifier;
10458 tree namespace_decl;
10459
10460 /* Get the name of the namespace. */
10461 identifier = cp_parser_identifier (parser);
10462 if (identifier == error_mark_node)
10463 return error_mark_node;
10464
10465 /* Look up the identifier in the currently active scope. Look only
10466 for namespaces, due to:
10467
10468 [basic.lookup.udir]
10469
10470 When looking up a namespace-name in a using-directive or alias
10471 definition, only namespace names are considered.
10472
10473 And:
10474
10475 [basic.lookup.qual]
10476
10477 During the lookup of a name preceding the :: scope resolution
10478 operator, object, function, and enumerator names are ignored.
10479
10480 (Note that cp_parser_class_or_namespace_name only calls this
10481 function if the token after the name is the scope resolution
10482 operator.) */
10483 namespace_decl = cp_parser_lookup_name (parser, identifier,
10484 none_type,
10485 /*is_template=*/false,
10486 /*is_namespace=*/true,
10487 /*check_dependency=*/true,
10488 /*ambiguous_decls=*/NULL);
10489 /* If it's not a namespace, issue an error. */
10490 if (namespace_decl == error_mark_node
10491 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10492 {
10493 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10494 error ("%qD is not a namespace-name", identifier);
10495 cp_parser_error (parser, "expected namespace-name");
10496 namespace_decl = error_mark_node;
10497 }
10498
10499 return namespace_decl;
10500 }
10501
10502 /* Parse a namespace-definition.
10503
10504 namespace-definition:
10505 named-namespace-definition
10506 unnamed-namespace-definition
10507
10508 named-namespace-definition:
10509 original-namespace-definition
10510 extension-namespace-definition
10511
10512 original-namespace-definition:
10513 namespace identifier { namespace-body }
10514
10515 extension-namespace-definition:
10516 namespace original-namespace-name { namespace-body }
10517
10518 unnamed-namespace-definition:
10519 namespace { namespace-body } */
10520
10521 static void
10522 cp_parser_namespace_definition (cp_parser* parser)
10523 {
10524 tree identifier, attribs;
10525
10526 /* Look for the `namespace' keyword. */
10527 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10528
10529 /* Get the name of the namespace. We do not attempt to distinguish
10530 between an original-namespace-definition and an
10531 extension-namespace-definition at this point. The semantic
10532 analysis routines are responsible for that. */
10533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10534 identifier = cp_parser_identifier (parser);
10535 else
10536 identifier = NULL_TREE;
10537
10538 /* Parse any specified attributes. */
10539 attribs = cp_parser_attributes_opt (parser);
10540
10541 /* Look for the `{' to start the namespace. */
10542 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10543 /* Start the namespace. */
10544 push_namespace_with_attribs (identifier, attribs);
10545 /* Parse the body of the namespace. */
10546 cp_parser_namespace_body (parser);
10547 /* Finish the namespace. */
10548 pop_namespace ();
10549 /* Look for the final `}'. */
10550 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10551 }
10552
10553 /* Parse a namespace-body.
10554
10555 namespace-body:
10556 declaration-seq [opt] */
10557
10558 static void
10559 cp_parser_namespace_body (cp_parser* parser)
10560 {
10561 cp_parser_declaration_seq_opt (parser);
10562 }
10563
10564 /* Parse a namespace-alias-definition.
10565
10566 namespace-alias-definition:
10567 namespace identifier = qualified-namespace-specifier ; */
10568
10569 static void
10570 cp_parser_namespace_alias_definition (cp_parser* parser)
10571 {
10572 tree identifier;
10573 tree namespace_specifier;
10574
10575 /* Look for the `namespace' keyword. */
10576 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10577 /* Look for the identifier. */
10578 identifier = cp_parser_identifier (parser);
10579 if (identifier == error_mark_node)
10580 return;
10581 /* Look for the `=' token. */
10582 cp_parser_require (parser, CPP_EQ, "`='");
10583 /* Look for the qualified-namespace-specifier. */
10584 namespace_specifier
10585 = cp_parser_qualified_namespace_specifier (parser);
10586 /* Look for the `;' token. */
10587 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10588
10589 /* Register the alias in the symbol table. */
10590 do_namespace_alias (identifier, namespace_specifier);
10591 }
10592
10593 /* Parse a qualified-namespace-specifier.
10594
10595 qualified-namespace-specifier:
10596 :: [opt] nested-name-specifier [opt] namespace-name
10597
10598 Returns a NAMESPACE_DECL corresponding to the specified
10599 namespace. */
10600
10601 static tree
10602 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10603 {
10604 /* Look for the optional `::'. */
10605 cp_parser_global_scope_opt (parser,
10606 /*current_scope_valid_p=*/false);
10607
10608 /* Look for the optional nested-name-specifier. */
10609 cp_parser_nested_name_specifier_opt (parser,
10610 /*typename_keyword_p=*/false,
10611 /*check_dependency_p=*/true,
10612 /*type_p=*/false,
10613 /*is_declaration=*/true);
10614
10615 return cp_parser_namespace_name (parser);
10616 }
10617
10618 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10619 access declaration.
10620
10621 using-declaration:
10622 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10623 using :: unqualified-id ;
10624
10625 access-declaration:
10626 qualified-id ;
10627
10628 */
10629
10630 static bool
10631 cp_parser_using_declaration (cp_parser* parser,
10632 bool access_declaration_p)
10633 {
10634 cp_token *token;
10635 bool typename_p = false;
10636 bool global_scope_p;
10637 tree decl;
10638 tree identifier;
10639 tree qscope;
10640
10641 if (access_declaration_p)
10642 cp_parser_parse_tentatively (parser);
10643 else
10644 {
10645 /* Look for the `using' keyword. */
10646 cp_parser_require_keyword (parser, RID_USING, "`using'");
10647
10648 /* Peek at the next token. */
10649 token = cp_lexer_peek_token (parser->lexer);
10650 /* See if it's `typename'. */
10651 if (token->keyword == RID_TYPENAME)
10652 {
10653 /* Remember that we've seen it. */
10654 typename_p = true;
10655 /* Consume the `typename' token. */
10656 cp_lexer_consume_token (parser->lexer);
10657 }
10658 }
10659
10660 /* Look for the optional global scope qualification. */
10661 global_scope_p
10662 = (cp_parser_global_scope_opt (parser,
10663 /*current_scope_valid_p=*/false)
10664 != NULL_TREE);
10665
10666 /* If we saw `typename', or didn't see `::', then there must be a
10667 nested-name-specifier present. */
10668 if (typename_p || !global_scope_p)
10669 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10670 /*check_dependency_p=*/true,
10671 /*type_p=*/false,
10672 /*is_declaration=*/true);
10673 /* Otherwise, we could be in either of the two productions. In that
10674 case, treat the nested-name-specifier as optional. */
10675 else
10676 qscope = cp_parser_nested_name_specifier_opt (parser,
10677 /*typename_keyword_p=*/false,
10678 /*check_dependency_p=*/true,
10679 /*type_p=*/false,
10680 /*is_declaration=*/true);
10681 if (!qscope)
10682 qscope = global_namespace;
10683
10684 /* Parse the unqualified-id. */
10685 identifier = cp_parser_unqualified_id (parser,
10686 /*template_keyword_p=*/false,
10687 /*check_dependency_p=*/true,
10688 /*declarator_p=*/true,
10689 /*optional_p=*/false);
10690
10691 if (access_declaration_p)
10692 {
10693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10694 cp_parser_simulate_error (parser);
10695 if (!cp_parser_parse_definitely (parser))
10696 return false;
10697 }
10698
10699 /* The function we call to handle a using-declaration is different
10700 depending on what scope we are in. */
10701 if (qscope == error_mark_node || identifier == error_mark_node)
10702 ;
10703 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10704 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10705 /* [namespace.udecl]
10706
10707 A using declaration shall not name a template-id. */
10708 error ("a template-id may not appear in a using-declaration");
10709 else
10710 {
10711 if (at_class_scope_p ())
10712 {
10713 /* Create the USING_DECL. */
10714 decl = do_class_using_decl (parser->scope, identifier);
10715 /* Add it to the list of members in this class. */
10716 finish_member_declaration (decl);
10717 }
10718 else
10719 {
10720 decl = cp_parser_lookup_name_simple (parser, identifier);
10721 if (decl == error_mark_node)
10722 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10723 else if (!at_namespace_scope_p ())
10724 do_local_using_decl (decl, qscope, identifier);
10725 else
10726 do_toplevel_using_decl (decl, qscope, identifier);
10727 }
10728 }
10729
10730 /* Look for the final `;'. */
10731 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10732
10733 return true;
10734 }
10735
10736 /* Parse a using-directive.
10737
10738 using-directive:
10739 using namespace :: [opt] nested-name-specifier [opt]
10740 namespace-name ; */
10741
10742 static void
10743 cp_parser_using_directive (cp_parser* parser)
10744 {
10745 tree namespace_decl;
10746 tree attribs;
10747
10748 /* Look for the `using' keyword. */
10749 cp_parser_require_keyword (parser, RID_USING, "`using'");
10750 /* And the `namespace' keyword. */
10751 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10752 /* Look for the optional `::' operator. */
10753 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10754 /* And the optional nested-name-specifier. */
10755 cp_parser_nested_name_specifier_opt (parser,
10756 /*typename_keyword_p=*/false,
10757 /*check_dependency_p=*/true,
10758 /*type_p=*/false,
10759 /*is_declaration=*/true);
10760 /* Get the namespace being used. */
10761 namespace_decl = cp_parser_namespace_name (parser);
10762 /* And any specified attributes. */
10763 attribs = cp_parser_attributes_opt (parser);
10764 /* Update the symbol table. */
10765 parse_using_directive (namespace_decl, attribs);
10766 /* Look for the final `;'. */
10767 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10768 }
10769
10770 /* Parse an asm-definition.
10771
10772 asm-definition:
10773 asm ( string-literal ) ;
10774
10775 GNU Extension:
10776
10777 asm-definition:
10778 asm volatile [opt] ( string-literal ) ;
10779 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10780 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10781 : asm-operand-list [opt] ) ;
10782 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10783 : asm-operand-list [opt]
10784 : asm-operand-list [opt] ) ; */
10785
10786 static void
10787 cp_parser_asm_definition (cp_parser* parser)
10788 {
10789 tree string;
10790 tree outputs = NULL_TREE;
10791 tree inputs = NULL_TREE;
10792 tree clobbers = NULL_TREE;
10793 tree asm_stmt;
10794 bool volatile_p = false;
10795 bool extended_p = false;
10796
10797 /* Look for the `asm' keyword. */
10798 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10799 /* See if the next token is `volatile'. */
10800 if (cp_parser_allow_gnu_extensions_p (parser)
10801 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10802 {
10803 /* Remember that we saw the `volatile' keyword. */
10804 volatile_p = true;
10805 /* Consume the token. */
10806 cp_lexer_consume_token (parser->lexer);
10807 }
10808 /* Look for the opening `('. */
10809 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10810 return;
10811 /* Look for the string. */
10812 string = cp_parser_string_literal (parser, false, false);
10813 if (string == error_mark_node)
10814 {
10815 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10816 /*consume_paren=*/true);
10817 return;
10818 }
10819
10820 /* If we're allowing GNU extensions, check for the extended assembly
10821 syntax. Unfortunately, the `:' tokens need not be separated by
10822 a space in C, and so, for compatibility, we tolerate that here
10823 too. Doing that means that we have to treat the `::' operator as
10824 two `:' tokens. */
10825 if (cp_parser_allow_gnu_extensions_p (parser)
10826 && at_function_scope_p ()
10827 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10828 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10829 {
10830 bool inputs_p = false;
10831 bool clobbers_p = false;
10832
10833 /* The extended syntax was used. */
10834 extended_p = true;
10835
10836 /* Look for outputs. */
10837 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10838 {
10839 /* Consume the `:'. */
10840 cp_lexer_consume_token (parser->lexer);
10841 /* Parse the output-operands. */
10842 if (cp_lexer_next_token_is_not (parser->lexer,
10843 CPP_COLON)
10844 && cp_lexer_next_token_is_not (parser->lexer,
10845 CPP_SCOPE)
10846 && cp_lexer_next_token_is_not (parser->lexer,
10847 CPP_CLOSE_PAREN))
10848 outputs = cp_parser_asm_operand_list (parser);
10849 }
10850 /* If the next token is `::', there are no outputs, and the
10851 next token is the beginning of the inputs. */
10852 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10853 /* The inputs are coming next. */
10854 inputs_p = true;
10855
10856 /* Look for inputs. */
10857 if (inputs_p
10858 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10859 {
10860 /* Consume the `:' or `::'. */
10861 cp_lexer_consume_token (parser->lexer);
10862 /* Parse the output-operands. */
10863 if (cp_lexer_next_token_is_not (parser->lexer,
10864 CPP_COLON)
10865 && cp_lexer_next_token_is_not (parser->lexer,
10866 CPP_CLOSE_PAREN))
10867 inputs = cp_parser_asm_operand_list (parser);
10868 }
10869 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10870 /* The clobbers are coming next. */
10871 clobbers_p = true;
10872
10873 /* Look for clobbers. */
10874 if (clobbers_p
10875 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10876 {
10877 /* Consume the `:' or `::'. */
10878 cp_lexer_consume_token (parser->lexer);
10879 /* Parse the clobbers. */
10880 if (cp_lexer_next_token_is_not (parser->lexer,
10881 CPP_CLOSE_PAREN))
10882 clobbers = cp_parser_asm_clobber_list (parser);
10883 }
10884 }
10885 /* Look for the closing `)'. */
10886 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10887 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10888 /*consume_paren=*/true);
10889 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10890
10891 /* Create the ASM_EXPR. */
10892 if (at_function_scope_p ())
10893 {
10894 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10895 inputs, clobbers);
10896 /* If the extended syntax was not used, mark the ASM_EXPR. */
10897 if (!extended_p)
10898 {
10899 tree temp = asm_stmt;
10900 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10901 temp = TREE_OPERAND (temp, 0);
10902
10903 ASM_INPUT_P (temp) = 1;
10904 }
10905 }
10906 else
10907 cgraph_add_asm_node (string);
10908 }
10909
10910 /* Declarators [gram.dcl.decl] */
10911
10912 /* Parse an init-declarator.
10913
10914 init-declarator:
10915 declarator initializer [opt]
10916
10917 GNU Extension:
10918
10919 init-declarator:
10920 declarator asm-specification [opt] attributes [opt] initializer [opt]
10921
10922 function-definition:
10923 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10924 function-body
10925 decl-specifier-seq [opt] declarator function-try-block
10926
10927 GNU Extension:
10928
10929 function-definition:
10930 __extension__ function-definition
10931
10932 The DECL_SPECIFIERS apply to this declarator. Returns a
10933 representation of the entity declared. If MEMBER_P is TRUE, then
10934 this declarator appears in a class scope. The new DECL created by
10935 this declarator is returned.
10936
10937 The CHECKS are access checks that should be performed once we know
10938 what entity is being declared (and, therefore, what classes have
10939 befriended it).
10940
10941 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10942 for a function-definition here as well. If the declarator is a
10943 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10944 be TRUE upon return. By that point, the function-definition will
10945 have been completely parsed.
10946
10947 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10948 is FALSE. */
10949
10950 static tree
10951 cp_parser_init_declarator (cp_parser* parser,
10952 cp_decl_specifier_seq *decl_specifiers,
10953 tree checks,
10954 bool function_definition_allowed_p,
10955 bool member_p,
10956 int declares_class_or_enum,
10957 bool* function_definition_p)
10958 {
10959 cp_token *token;
10960 cp_declarator *declarator;
10961 tree prefix_attributes;
10962 tree attributes;
10963 tree asm_specification;
10964 tree initializer;
10965 tree decl = NULL_TREE;
10966 tree scope;
10967 bool is_initialized;
10968 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10969 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10970 "(...)". */
10971 enum cpp_ttype initialization_kind;
10972 bool is_parenthesized_init = false;
10973 bool is_non_constant_init;
10974 int ctor_dtor_or_conv_p;
10975 bool friend_p;
10976 tree pushed_scope = NULL;
10977
10978 /* Gather the attributes that were provided with the
10979 decl-specifiers. */
10980 prefix_attributes = decl_specifiers->attributes;
10981
10982 /* Assume that this is not the declarator for a function
10983 definition. */
10984 if (function_definition_p)
10985 *function_definition_p = false;
10986
10987 /* Defer access checks while parsing the declarator; we cannot know
10988 what names are accessible until we know what is being
10989 declared. */
10990 resume_deferring_access_checks ();
10991
10992 /* Parse the declarator. */
10993 declarator
10994 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10995 &ctor_dtor_or_conv_p,
10996 /*parenthesized_p=*/NULL,
10997 /*member_p=*/false);
10998 /* Gather up the deferred checks. */
10999 stop_deferring_access_checks ();
11000
11001 /* If the DECLARATOR was erroneous, there's no need to go
11002 further. */
11003 if (declarator == cp_error_declarator)
11004 return error_mark_node;
11005
11006 if (declares_class_or_enum & 2)
11007 cp_parser_check_for_definition_in_return_type (declarator,
11008 decl_specifiers->type);
11009
11010 /* Figure out what scope the entity declared by the DECLARATOR is
11011 located in. `grokdeclarator' sometimes changes the scope, so
11012 we compute it now. */
11013 scope = get_scope_of_declarator (declarator);
11014
11015 /* If we're allowing GNU extensions, look for an asm-specification
11016 and attributes. */
11017 if (cp_parser_allow_gnu_extensions_p (parser))
11018 {
11019 /* Look for an asm-specification. */
11020 asm_specification = cp_parser_asm_specification_opt (parser);
11021 /* And attributes. */
11022 attributes = cp_parser_attributes_opt (parser);
11023 }
11024 else
11025 {
11026 asm_specification = NULL_TREE;
11027 attributes = NULL_TREE;
11028 }
11029
11030 /* Peek at the next token. */
11031 token = cp_lexer_peek_token (parser->lexer);
11032 /* Check to see if the token indicates the start of a
11033 function-definition. */
11034 if (cp_parser_token_starts_function_definition_p (token))
11035 {
11036 if (!function_definition_allowed_p)
11037 {
11038 /* If a function-definition should not appear here, issue an
11039 error message. */
11040 cp_parser_error (parser,
11041 "a function-definition is not allowed here");
11042 return error_mark_node;
11043 }
11044 else
11045 {
11046 /* Neither attributes nor an asm-specification are allowed
11047 on a function-definition. */
11048 if (asm_specification)
11049 error ("an asm-specification is not allowed on a function-definition");
11050 if (attributes)
11051 error ("attributes are not allowed on a function-definition");
11052 /* This is a function-definition. */
11053 *function_definition_p = true;
11054
11055 /* Parse the function definition. */
11056 if (member_p)
11057 decl = cp_parser_save_member_function_body (parser,
11058 decl_specifiers,
11059 declarator,
11060 prefix_attributes);
11061 else
11062 decl
11063 = (cp_parser_function_definition_from_specifiers_and_declarator
11064 (parser, decl_specifiers, prefix_attributes, declarator));
11065
11066 return decl;
11067 }
11068 }
11069
11070 /* [dcl.dcl]
11071
11072 Only in function declarations for constructors, destructors, and
11073 type conversions can the decl-specifier-seq be omitted.
11074
11075 We explicitly postpone this check past the point where we handle
11076 function-definitions because we tolerate function-definitions
11077 that are missing their return types in some modes. */
11078 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11079 {
11080 cp_parser_error (parser,
11081 "expected constructor, destructor, or type conversion");
11082 return error_mark_node;
11083 }
11084
11085 /* An `=' or an `(' indicates an initializer. */
11086 if (token->type == CPP_EQ
11087 || token->type == CPP_OPEN_PAREN)
11088 {
11089 is_initialized = true;
11090 initialization_kind = token->type;
11091 }
11092 else
11093 {
11094 /* If the init-declarator isn't initialized and isn't followed by a
11095 `,' or `;', it's not a valid init-declarator. */
11096 if (token->type != CPP_COMMA
11097 && token->type != CPP_SEMICOLON)
11098 {
11099 cp_parser_error (parser, "expected initializer");
11100 return error_mark_node;
11101 }
11102 is_initialized = false;
11103 initialization_kind = CPP_EOF;
11104 }
11105
11106 /* Because start_decl has side-effects, we should only call it if we
11107 know we're going ahead. By this point, we know that we cannot
11108 possibly be looking at any other construct. */
11109 cp_parser_commit_to_tentative_parse (parser);
11110
11111 /* If the decl specifiers were bad, issue an error now that we're
11112 sure this was intended to be a declarator. Then continue
11113 declaring the variable(s), as int, to try to cut down on further
11114 errors. */
11115 if (decl_specifiers->any_specifiers_p
11116 && decl_specifiers->type == error_mark_node)
11117 {
11118 cp_parser_error (parser, "invalid type in declaration");
11119 decl_specifiers->type = integer_type_node;
11120 }
11121
11122 /* Check to see whether or not this declaration is a friend. */
11123 friend_p = cp_parser_friend_p (decl_specifiers);
11124
11125 /* Check that the number of template-parameter-lists is OK. */
11126 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11127 return error_mark_node;
11128
11129 /* Enter the newly declared entry in the symbol table. If we're
11130 processing a declaration in a class-specifier, we wait until
11131 after processing the initializer. */
11132 if (!member_p)
11133 {
11134 if (parser->in_unbraced_linkage_specification_p)
11135 decl_specifiers->storage_class = sc_extern;
11136 decl = start_decl (declarator, decl_specifiers,
11137 is_initialized, attributes, prefix_attributes,
11138 &pushed_scope);
11139 }
11140 else if (scope)
11141 /* Enter the SCOPE. That way unqualified names appearing in the
11142 initializer will be looked up in SCOPE. */
11143 pushed_scope = push_scope (scope);
11144
11145 /* Perform deferred access control checks, now that we know in which
11146 SCOPE the declared entity resides. */
11147 if (!member_p && decl)
11148 {
11149 tree saved_current_function_decl = NULL_TREE;
11150
11151 /* If the entity being declared is a function, pretend that we
11152 are in its scope. If it is a `friend', it may have access to
11153 things that would not otherwise be accessible. */
11154 if (TREE_CODE (decl) == FUNCTION_DECL)
11155 {
11156 saved_current_function_decl = current_function_decl;
11157 current_function_decl = decl;
11158 }
11159
11160 /* Perform access checks for template parameters. */
11161 cp_parser_perform_template_parameter_access_checks (checks);
11162
11163 /* Perform the access control checks for the declarator and the
11164 the decl-specifiers. */
11165 perform_deferred_access_checks ();
11166
11167 /* Restore the saved value. */
11168 if (TREE_CODE (decl) == FUNCTION_DECL)
11169 current_function_decl = saved_current_function_decl;
11170 }
11171
11172 /* Parse the initializer. */
11173 initializer = NULL_TREE;
11174 is_parenthesized_init = false;
11175 is_non_constant_init = true;
11176 if (is_initialized)
11177 {
11178 if (function_declarator_p (declarator)
11179 && initialization_kind == CPP_EQ)
11180 initializer = cp_parser_pure_specifier (parser);
11181 else
11182 initializer = cp_parser_initializer (parser,
11183 &is_parenthesized_init,
11184 &is_non_constant_init);
11185 }
11186
11187 /* The old parser allows attributes to appear after a parenthesized
11188 initializer. Mark Mitchell proposed removing this functionality
11189 on the GCC mailing lists on 2002-08-13. This parser accepts the
11190 attributes -- but ignores them. */
11191 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11192 if (cp_parser_attributes_opt (parser))
11193 warning (OPT_Wattributes,
11194 "attributes after parenthesized initializer ignored");
11195
11196 /* For an in-class declaration, use `grokfield' to create the
11197 declaration. */
11198 if (member_p)
11199 {
11200 if (pushed_scope)
11201 {
11202 pop_scope (pushed_scope);
11203 pushed_scope = false;
11204 }
11205 decl = grokfield (declarator, decl_specifiers,
11206 initializer, !is_non_constant_init,
11207 /*asmspec=*/NULL_TREE,
11208 prefix_attributes);
11209 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11210 cp_parser_save_default_args (parser, decl);
11211 }
11212
11213 /* Finish processing the declaration. But, skip friend
11214 declarations. */
11215 if (!friend_p && decl && decl != error_mark_node)
11216 {
11217 cp_finish_decl (decl,
11218 initializer, !is_non_constant_init,
11219 asm_specification,
11220 /* If the initializer is in parentheses, then this is
11221 a direct-initialization, which means that an
11222 `explicit' constructor is OK. Otherwise, an
11223 `explicit' constructor cannot be used. */
11224 ((is_parenthesized_init || !is_initialized)
11225 ? 0 : LOOKUP_ONLYCONVERTING));
11226 }
11227 if (!friend_p && pushed_scope)
11228 pop_scope (pushed_scope);
11229
11230 return decl;
11231 }
11232
11233 /* Parse a declarator.
11234
11235 declarator:
11236 direct-declarator
11237 ptr-operator declarator
11238
11239 abstract-declarator:
11240 ptr-operator abstract-declarator [opt]
11241 direct-abstract-declarator
11242
11243 GNU Extensions:
11244
11245 declarator:
11246 attributes [opt] direct-declarator
11247 attributes [opt] ptr-operator declarator
11248
11249 abstract-declarator:
11250 attributes [opt] ptr-operator abstract-declarator [opt]
11251 attributes [opt] direct-abstract-declarator
11252
11253 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11254 detect constructor, destructor or conversion operators. It is set
11255 to -1 if the declarator is a name, and +1 if it is a
11256 function. Otherwise it is set to zero. Usually you just want to
11257 test for >0, but internally the negative value is used.
11258
11259 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11260 a decl-specifier-seq unless it declares a constructor, destructor,
11261 or conversion. It might seem that we could check this condition in
11262 semantic analysis, rather than parsing, but that makes it difficult
11263 to handle something like `f()'. We want to notice that there are
11264 no decl-specifiers, and therefore realize that this is an
11265 expression, not a declaration.)
11266
11267 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11268 the declarator is a direct-declarator of the form "(...)".
11269
11270 MEMBER_P is true iff this declarator is a member-declarator. */
11271
11272 static cp_declarator *
11273 cp_parser_declarator (cp_parser* parser,
11274 cp_parser_declarator_kind dcl_kind,
11275 int* ctor_dtor_or_conv_p,
11276 bool* parenthesized_p,
11277 bool member_p)
11278 {
11279 cp_token *token;
11280 cp_declarator *declarator;
11281 enum tree_code code;
11282 cp_cv_quals cv_quals;
11283 tree class_type;
11284 tree attributes = NULL_TREE;
11285
11286 /* Assume this is not a constructor, destructor, or type-conversion
11287 operator. */
11288 if (ctor_dtor_or_conv_p)
11289 *ctor_dtor_or_conv_p = 0;
11290
11291 if (cp_parser_allow_gnu_extensions_p (parser))
11292 attributes = cp_parser_attributes_opt (parser);
11293
11294 /* Peek at the next token. */
11295 token = cp_lexer_peek_token (parser->lexer);
11296
11297 /* Check for the ptr-operator production. */
11298 cp_parser_parse_tentatively (parser);
11299 /* Parse the ptr-operator. */
11300 code = cp_parser_ptr_operator (parser,
11301 &class_type,
11302 &cv_quals);
11303 /* If that worked, then we have a ptr-operator. */
11304 if (cp_parser_parse_definitely (parser))
11305 {
11306 /* If a ptr-operator was found, then this declarator was not
11307 parenthesized. */
11308 if (parenthesized_p)
11309 *parenthesized_p = true;
11310 /* The dependent declarator is optional if we are parsing an
11311 abstract-declarator. */
11312 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11313 cp_parser_parse_tentatively (parser);
11314
11315 /* Parse the dependent declarator. */
11316 declarator = cp_parser_declarator (parser, dcl_kind,
11317 /*ctor_dtor_or_conv_p=*/NULL,
11318 /*parenthesized_p=*/NULL,
11319 /*member_p=*/false);
11320
11321 /* If we are parsing an abstract-declarator, we must handle the
11322 case where the dependent declarator is absent. */
11323 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11324 && !cp_parser_parse_definitely (parser))
11325 declarator = NULL;
11326
11327 /* Build the representation of the ptr-operator. */
11328 if (class_type)
11329 declarator = make_ptrmem_declarator (cv_quals,
11330 class_type,
11331 declarator);
11332 else if (code == INDIRECT_REF)
11333 declarator = make_pointer_declarator (cv_quals, declarator);
11334 else
11335 declarator = make_reference_declarator (cv_quals, declarator);
11336 }
11337 /* Everything else is a direct-declarator. */
11338 else
11339 {
11340 if (parenthesized_p)
11341 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11342 CPP_OPEN_PAREN);
11343 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11344 ctor_dtor_or_conv_p,
11345 member_p);
11346 }
11347
11348 if (attributes && declarator && declarator != cp_error_declarator)
11349 declarator->attributes = attributes;
11350
11351 return declarator;
11352 }
11353
11354 /* Parse a direct-declarator or direct-abstract-declarator.
11355
11356 direct-declarator:
11357 declarator-id
11358 direct-declarator ( parameter-declaration-clause )
11359 cv-qualifier-seq [opt]
11360 exception-specification [opt]
11361 direct-declarator [ constant-expression [opt] ]
11362 ( declarator )
11363
11364 direct-abstract-declarator:
11365 direct-abstract-declarator [opt]
11366 ( parameter-declaration-clause )
11367 cv-qualifier-seq [opt]
11368 exception-specification [opt]
11369 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11370 ( abstract-declarator )
11371
11372 Returns a representation of the declarator. DCL_KIND is
11373 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11374 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11375 we are parsing a direct-declarator. It is
11376 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11377 of ambiguity we prefer an abstract declarator, as per
11378 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11379 cp_parser_declarator. */
11380
11381 static cp_declarator *
11382 cp_parser_direct_declarator (cp_parser* parser,
11383 cp_parser_declarator_kind dcl_kind,
11384 int* ctor_dtor_or_conv_p,
11385 bool member_p)
11386 {
11387 cp_token *token;
11388 cp_declarator *declarator = NULL;
11389 tree scope = NULL_TREE;
11390 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11391 bool saved_in_declarator_p = parser->in_declarator_p;
11392 bool first = true;
11393 tree pushed_scope = NULL_TREE;
11394
11395 while (true)
11396 {
11397 /* Peek at the next token. */
11398 token = cp_lexer_peek_token (parser->lexer);
11399 if (token->type == CPP_OPEN_PAREN)
11400 {
11401 /* This is either a parameter-declaration-clause, or a
11402 parenthesized declarator. When we know we are parsing a
11403 named declarator, it must be a parenthesized declarator
11404 if FIRST is true. For instance, `(int)' is a
11405 parameter-declaration-clause, with an omitted
11406 direct-abstract-declarator. But `((*))', is a
11407 parenthesized abstract declarator. Finally, when T is a
11408 template parameter `(T)' is a
11409 parameter-declaration-clause, and not a parenthesized
11410 named declarator.
11411
11412 We first try and parse a parameter-declaration-clause,
11413 and then try a nested declarator (if FIRST is true).
11414
11415 It is not an error for it not to be a
11416 parameter-declaration-clause, even when FIRST is
11417 false. Consider,
11418
11419 int i (int);
11420 int i (3);
11421
11422 The first is the declaration of a function while the
11423 second is a the definition of a variable, including its
11424 initializer.
11425
11426 Having seen only the parenthesis, we cannot know which of
11427 these two alternatives should be selected. Even more
11428 complex are examples like:
11429
11430 int i (int (a));
11431 int i (int (3));
11432
11433 The former is a function-declaration; the latter is a
11434 variable initialization.
11435
11436 Thus again, we try a parameter-declaration-clause, and if
11437 that fails, we back out and return. */
11438
11439 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11440 {
11441 cp_parameter_declarator *params;
11442 unsigned saved_num_template_parameter_lists;
11443
11444 /* In a member-declarator, the only valid interpretation
11445 of a parenthesis is the start of a
11446 parameter-declaration-clause. (It is invalid to
11447 initialize a static data member with a parenthesized
11448 initializer; only the "=" form of initialization is
11449 permitted.) */
11450 if (!member_p)
11451 cp_parser_parse_tentatively (parser);
11452
11453 /* Consume the `('. */
11454 cp_lexer_consume_token (parser->lexer);
11455 if (first)
11456 {
11457 /* If this is going to be an abstract declarator, we're
11458 in a declarator and we can't have default args. */
11459 parser->default_arg_ok_p = false;
11460 parser->in_declarator_p = true;
11461 }
11462
11463 /* Inside the function parameter list, surrounding
11464 template-parameter-lists do not apply. */
11465 saved_num_template_parameter_lists
11466 = parser->num_template_parameter_lists;
11467 parser->num_template_parameter_lists = 0;
11468
11469 /* Parse the parameter-declaration-clause. */
11470 params = cp_parser_parameter_declaration_clause (parser);
11471
11472 parser->num_template_parameter_lists
11473 = saved_num_template_parameter_lists;
11474
11475 /* If all went well, parse the cv-qualifier-seq and the
11476 exception-specification. */
11477 if (member_p || cp_parser_parse_definitely (parser))
11478 {
11479 cp_cv_quals cv_quals;
11480 tree exception_specification;
11481
11482 if (ctor_dtor_or_conv_p)
11483 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11484 first = false;
11485 /* Consume the `)'. */
11486 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11487
11488 /* Parse the cv-qualifier-seq. */
11489 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11490 /* And the exception-specification. */
11491 exception_specification
11492 = cp_parser_exception_specification_opt (parser);
11493
11494 /* Create the function-declarator. */
11495 declarator = make_call_declarator (declarator,
11496 params,
11497 cv_quals,
11498 exception_specification);
11499 /* Any subsequent parameter lists are to do with
11500 return type, so are not those of the declared
11501 function. */
11502 parser->default_arg_ok_p = false;
11503
11504 /* Repeat the main loop. */
11505 continue;
11506 }
11507 }
11508
11509 /* If this is the first, we can try a parenthesized
11510 declarator. */
11511 if (first)
11512 {
11513 bool saved_in_type_id_in_expr_p;
11514
11515 parser->default_arg_ok_p = saved_default_arg_ok_p;
11516 parser->in_declarator_p = saved_in_declarator_p;
11517
11518 /* Consume the `('. */
11519 cp_lexer_consume_token (parser->lexer);
11520 /* Parse the nested declarator. */
11521 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11522 parser->in_type_id_in_expr_p = true;
11523 declarator
11524 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11525 /*parenthesized_p=*/NULL,
11526 member_p);
11527 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11528 first = false;
11529 /* Expect a `)'. */
11530 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11531 declarator = cp_error_declarator;
11532 if (declarator == cp_error_declarator)
11533 break;
11534
11535 goto handle_declarator;
11536 }
11537 /* Otherwise, we must be done. */
11538 else
11539 break;
11540 }
11541 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11542 && token->type == CPP_OPEN_SQUARE)
11543 {
11544 /* Parse an array-declarator. */
11545 tree bounds;
11546
11547 if (ctor_dtor_or_conv_p)
11548 *ctor_dtor_or_conv_p = 0;
11549
11550 first = false;
11551 parser->default_arg_ok_p = false;
11552 parser->in_declarator_p = true;
11553 /* Consume the `['. */
11554 cp_lexer_consume_token (parser->lexer);
11555 /* Peek at the next token. */
11556 token = cp_lexer_peek_token (parser->lexer);
11557 /* If the next token is `]', then there is no
11558 constant-expression. */
11559 if (token->type != CPP_CLOSE_SQUARE)
11560 {
11561 bool non_constant_p;
11562
11563 bounds
11564 = cp_parser_constant_expression (parser,
11565 /*allow_non_constant=*/true,
11566 &non_constant_p);
11567 if (!non_constant_p)
11568 bounds = fold_non_dependent_expr (bounds);
11569 /* Normally, the array bound must be an integral constant
11570 expression. However, as an extension, we allow VLAs
11571 in function scopes. */
11572 else if (!at_function_scope_p ())
11573 {
11574 error ("array bound is not an integer constant");
11575 bounds = error_mark_node;
11576 }
11577 }
11578 else
11579 bounds = NULL_TREE;
11580 /* Look for the closing `]'. */
11581 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11582 {
11583 declarator = cp_error_declarator;
11584 break;
11585 }
11586
11587 declarator = make_array_declarator (declarator, bounds);
11588 }
11589 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11590 {
11591 tree qualifying_scope;
11592 tree unqualified_name;
11593 special_function_kind sfk;
11594 bool abstract_ok;
11595
11596 /* Parse a declarator-id */
11597 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11598 if (abstract_ok)
11599 cp_parser_parse_tentatively (parser);
11600 unqualified_name
11601 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11602 qualifying_scope = parser->scope;
11603 if (abstract_ok)
11604 {
11605 if (!cp_parser_parse_definitely (parser))
11606 unqualified_name = error_mark_node;
11607 else if (unqualified_name
11608 && (qualifying_scope
11609 || (TREE_CODE (unqualified_name)
11610 != IDENTIFIER_NODE)))
11611 {
11612 cp_parser_error (parser, "expected unqualified-id");
11613 unqualified_name = error_mark_node;
11614 }
11615 }
11616
11617 if (!unqualified_name)
11618 return NULL;
11619 if (unqualified_name == error_mark_node)
11620 {
11621 declarator = cp_error_declarator;
11622 break;
11623 }
11624
11625 if (qualifying_scope && at_namespace_scope_p ()
11626 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11627 {
11628 /* In the declaration of a member of a template class
11629 outside of the class itself, the SCOPE will sometimes
11630 be a TYPENAME_TYPE. For example, given:
11631
11632 template <typename T>
11633 int S<T>::R::i = 3;
11634
11635 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11636 this context, we must resolve S<T>::R to an ordinary
11637 type, rather than a typename type.
11638
11639 The reason we normally avoid resolving TYPENAME_TYPEs
11640 is that a specialization of `S' might render
11641 `S<T>::R' not a type. However, if `S' is
11642 specialized, then this `i' will not be used, so there
11643 is no harm in resolving the types here. */
11644 tree type;
11645
11646 /* Resolve the TYPENAME_TYPE. */
11647 type = resolve_typename_type (qualifying_scope,
11648 /*only_current_p=*/false);
11649 /* If that failed, the declarator is invalid. */
11650 if (type == error_mark_node)
11651 error ("%<%T::%D%> is not a type",
11652 TYPE_CONTEXT (qualifying_scope),
11653 TYPE_IDENTIFIER (qualifying_scope));
11654 qualifying_scope = type;
11655 }
11656
11657 sfk = sfk_none;
11658 if (unqualified_name)
11659 {
11660 tree class_type;
11661
11662 if (qualifying_scope
11663 && CLASS_TYPE_P (qualifying_scope))
11664 class_type = qualifying_scope;
11665 else
11666 class_type = current_class_type;
11667
11668 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11669 {
11670 tree name_type = TREE_TYPE (unqualified_name);
11671 if (class_type && same_type_p (name_type, class_type))
11672 {
11673 if (qualifying_scope
11674 && CLASSTYPE_USE_TEMPLATE (name_type))
11675 {
11676 error ("invalid use of constructor as a template");
11677 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11678 "name the constructor in a qualified name",
11679 class_type,
11680 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11681 class_type, name_type);
11682 declarator = cp_error_declarator;
11683 break;
11684 }
11685 else
11686 unqualified_name = constructor_name (class_type);
11687 }
11688 else
11689 {
11690 /* We do not attempt to print the declarator
11691 here because we do not have enough
11692 information about its original syntactic
11693 form. */
11694 cp_parser_error (parser, "invalid declarator");
11695 declarator = cp_error_declarator;
11696 break;
11697 }
11698 }
11699
11700 if (class_type)
11701 {
11702 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11703 sfk = sfk_destructor;
11704 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11705 sfk = sfk_conversion;
11706 else if (/* There's no way to declare a constructor
11707 for an anonymous type, even if the type
11708 got a name for linkage purposes. */
11709 !TYPE_WAS_ANONYMOUS (class_type)
11710 && constructor_name_p (unqualified_name,
11711 class_type))
11712 {
11713 unqualified_name = constructor_name (class_type);
11714 sfk = sfk_constructor;
11715 }
11716
11717 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11718 *ctor_dtor_or_conv_p = -1;
11719 }
11720 }
11721 declarator = make_id_declarator (qualifying_scope,
11722 unqualified_name,
11723 sfk);
11724 declarator->id_loc = token->location;
11725
11726 handle_declarator:;
11727 scope = get_scope_of_declarator (declarator);
11728 if (scope)
11729 /* Any names that appear after the declarator-id for a
11730 member are looked up in the containing scope. */
11731 pushed_scope = push_scope (scope);
11732 parser->in_declarator_p = true;
11733 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11734 || (declarator && declarator->kind == cdk_id))
11735 /* Default args are only allowed on function
11736 declarations. */
11737 parser->default_arg_ok_p = saved_default_arg_ok_p;
11738 else
11739 parser->default_arg_ok_p = false;
11740
11741 first = false;
11742 }
11743 /* We're done. */
11744 else
11745 break;
11746 }
11747
11748 /* For an abstract declarator, we might wind up with nothing at this
11749 point. That's an error; the declarator is not optional. */
11750 if (!declarator)
11751 cp_parser_error (parser, "expected declarator");
11752
11753 /* If we entered a scope, we must exit it now. */
11754 if (pushed_scope)
11755 pop_scope (pushed_scope);
11756
11757 parser->default_arg_ok_p = saved_default_arg_ok_p;
11758 parser->in_declarator_p = saved_in_declarator_p;
11759
11760 return declarator;
11761 }
11762
11763 /* Parse a ptr-operator.
11764
11765 ptr-operator:
11766 * cv-qualifier-seq [opt]
11767 &
11768 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11769
11770 GNU Extension:
11771
11772 ptr-operator:
11773 & cv-qualifier-seq [opt]
11774
11775 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11776 Returns ADDR_EXPR if a reference was used. In the case of a
11777 pointer-to-member, *TYPE is filled in with the TYPE containing the
11778 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11779 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11780 ERROR_MARK if an error occurred. */
11781
11782 static enum tree_code
11783 cp_parser_ptr_operator (cp_parser* parser,
11784 tree* type,
11785 cp_cv_quals *cv_quals)
11786 {
11787 enum tree_code code = ERROR_MARK;
11788 cp_token *token;
11789
11790 /* Assume that it's not a pointer-to-member. */
11791 *type = NULL_TREE;
11792 /* And that there are no cv-qualifiers. */
11793 *cv_quals = TYPE_UNQUALIFIED;
11794
11795 /* Peek at the next token. */
11796 token = cp_lexer_peek_token (parser->lexer);
11797 /* If it's a `*' or `&' we have a pointer or reference. */
11798 if (token->type == CPP_MULT || token->type == CPP_AND)
11799 {
11800 /* Remember which ptr-operator we were processing. */
11801 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11802
11803 /* Consume the `*' or `&'. */
11804 cp_lexer_consume_token (parser->lexer);
11805
11806 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11807 `&', if we are allowing GNU extensions. (The only qualifier
11808 that can legally appear after `&' is `restrict', but that is
11809 enforced during semantic analysis. */
11810 if (code == INDIRECT_REF
11811 || cp_parser_allow_gnu_extensions_p (parser))
11812 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11813 }
11814 else
11815 {
11816 /* Try the pointer-to-member case. */
11817 cp_parser_parse_tentatively (parser);
11818 /* Look for the optional `::' operator. */
11819 cp_parser_global_scope_opt (parser,
11820 /*current_scope_valid_p=*/false);
11821 /* Look for the nested-name specifier. */
11822 cp_parser_nested_name_specifier (parser,
11823 /*typename_keyword_p=*/false,
11824 /*check_dependency_p=*/true,
11825 /*type_p=*/false,
11826 /*is_declaration=*/false);
11827 /* If we found it, and the next token is a `*', then we are
11828 indeed looking at a pointer-to-member operator. */
11829 if (!cp_parser_error_occurred (parser)
11830 && cp_parser_require (parser, CPP_MULT, "`*'"))
11831 {
11832 /* Indicate that the `*' operator was used. */
11833 code = INDIRECT_REF;
11834
11835 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11836 error ("%qD is a namespace", parser->scope);
11837 else
11838 {
11839 /* The type of which the member is a member is given by the
11840 current SCOPE. */
11841 *type = parser->scope;
11842 /* The next name will not be qualified. */
11843 parser->scope = NULL_TREE;
11844 parser->qualifying_scope = NULL_TREE;
11845 parser->object_scope = NULL_TREE;
11846 /* Look for the optional cv-qualifier-seq. */
11847 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11848 }
11849 }
11850 /* If that didn't work we don't have a ptr-operator. */
11851 if (!cp_parser_parse_definitely (parser))
11852 cp_parser_error (parser, "expected ptr-operator");
11853 }
11854
11855 return code;
11856 }
11857
11858 /* Parse an (optional) cv-qualifier-seq.
11859
11860 cv-qualifier-seq:
11861 cv-qualifier cv-qualifier-seq [opt]
11862
11863 cv-qualifier:
11864 const
11865 volatile
11866
11867 GNU Extension:
11868
11869 cv-qualifier:
11870 __restrict__
11871
11872 Returns a bitmask representing the cv-qualifiers. */
11873
11874 static cp_cv_quals
11875 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11876 {
11877 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11878
11879 while (true)
11880 {
11881 cp_token *token;
11882 cp_cv_quals cv_qualifier;
11883
11884 /* Peek at the next token. */
11885 token = cp_lexer_peek_token (parser->lexer);
11886 /* See if it's a cv-qualifier. */
11887 switch (token->keyword)
11888 {
11889 case RID_CONST:
11890 cv_qualifier = TYPE_QUAL_CONST;
11891 break;
11892
11893 case RID_VOLATILE:
11894 cv_qualifier = TYPE_QUAL_VOLATILE;
11895 break;
11896
11897 case RID_RESTRICT:
11898 cv_qualifier = TYPE_QUAL_RESTRICT;
11899 break;
11900
11901 default:
11902 cv_qualifier = TYPE_UNQUALIFIED;
11903 break;
11904 }
11905
11906 if (!cv_qualifier)
11907 break;
11908
11909 if (cv_quals & cv_qualifier)
11910 {
11911 error ("duplicate cv-qualifier");
11912 cp_lexer_purge_token (parser->lexer);
11913 }
11914 else
11915 {
11916 cp_lexer_consume_token (parser->lexer);
11917 cv_quals |= cv_qualifier;
11918 }
11919 }
11920
11921 return cv_quals;
11922 }
11923
11924 /* Parse a declarator-id.
11925
11926 declarator-id:
11927 id-expression
11928 :: [opt] nested-name-specifier [opt] type-name
11929
11930 In the `id-expression' case, the value returned is as for
11931 cp_parser_id_expression if the id-expression was an unqualified-id.
11932 If the id-expression was a qualified-id, then a SCOPE_REF is
11933 returned. The first operand is the scope (either a NAMESPACE_DECL
11934 or TREE_TYPE), but the second is still just a representation of an
11935 unqualified-id. */
11936
11937 static tree
11938 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11939 {
11940 tree id;
11941 /* The expression must be an id-expression. Assume that qualified
11942 names are the names of types so that:
11943
11944 template <class T>
11945 int S<T>::R::i = 3;
11946
11947 will work; we must treat `S<T>::R' as the name of a type.
11948 Similarly, assume that qualified names are templates, where
11949 required, so that:
11950
11951 template <class T>
11952 int S<T>::R<T>::i = 3;
11953
11954 will work, too. */
11955 id = cp_parser_id_expression (parser,
11956 /*template_keyword_p=*/false,
11957 /*check_dependency_p=*/false,
11958 /*template_p=*/NULL,
11959 /*declarator_p=*/true,
11960 optional_p);
11961 if (id && BASELINK_P (id))
11962 id = BASELINK_FUNCTIONS (id);
11963 return id;
11964 }
11965
11966 /* Parse a type-id.
11967
11968 type-id:
11969 type-specifier-seq abstract-declarator [opt]
11970
11971 Returns the TYPE specified. */
11972
11973 static tree
11974 cp_parser_type_id (cp_parser* parser)
11975 {
11976 cp_decl_specifier_seq type_specifier_seq;
11977 cp_declarator *abstract_declarator;
11978
11979 /* Parse the type-specifier-seq. */
11980 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11981 &type_specifier_seq);
11982 if (type_specifier_seq.type == error_mark_node)
11983 return error_mark_node;
11984
11985 /* There might or might not be an abstract declarator. */
11986 cp_parser_parse_tentatively (parser);
11987 /* Look for the declarator. */
11988 abstract_declarator
11989 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11990 /*parenthesized_p=*/NULL,
11991 /*member_p=*/false);
11992 /* Check to see if there really was a declarator. */
11993 if (!cp_parser_parse_definitely (parser))
11994 abstract_declarator = NULL;
11995
11996 return groktypename (&type_specifier_seq, abstract_declarator);
11997 }
11998
11999 /* Parse a type-specifier-seq.
12000
12001 type-specifier-seq:
12002 type-specifier type-specifier-seq [opt]
12003
12004 GNU extension:
12005
12006 type-specifier-seq:
12007 attributes type-specifier-seq [opt]
12008
12009 If IS_CONDITION is true, we are at the start of a "condition",
12010 e.g., we've just seen "if (".
12011
12012 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12013
12014 static void
12015 cp_parser_type_specifier_seq (cp_parser* parser,
12016 bool is_condition,
12017 cp_decl_specifier_seq *type_specifier_seq)
12018 {
12019 bool seen_type_specifier = false;
12020 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12021
12022 /* Clear the TYPE_SPECIFIER_SEQ. */
12023 clear_decl_specs (type_specifier_seq);
12024
12025 /* Parse the type-specifiers and attributes. */
12026 while (true)
12027 {
12028 tree type_specifier;
12029 bool is_cv_qualifier;
12030
12031 /* Check for attributes first. */
12032 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12033 {
12034 type_specifier_seq->attributes =
12035 chainon (type_specifier_seq->attributes,
12036 cp_parser_attributes_opt (parser));
12037 continue;
12038 }
12039
12040 /* Look for the type-specifier. */
12041 type_specifier = cp_parser_type_specifier (parser,
12042 flags,
12043 type_specifier_seq,
12044 /*is_declaration=*/false,
12045 NULL,
12046 &is_cv_qualifier);
12047 if (!type_specifier)
12048 {
12049 /* If the first type-specifier could not be found, this is not a
12050 type-specifier-seq at all. */
12051 if (!seen_type_specifier)
12052 {
12053 cp_parser_error (parser, "expected type-specifier");
12054 type_specifier_seq->type = error_mark_node;
12055 return;
12056 }
12057 /* If subsequent type-specifiers could not be found, the
12058 type-specifier-seq is complete. */
12059 break;
12060 }
12061
12062 seen_type_specifier = true;
12063 /* The standard says that a condition can be:
12064
12065 type-specifier-seq declarator = assignment-expression
12066
12067 However, given:
12068
12069 struct S {};
12070 if (int S = ...)
12071
12072 we should treat the "S" as a declarator, not as a
12073 type-specifier. The standard doesn't say that explicitly for
12074 type-specifier-seq, but it does say that for
12075 decl-specifier-seq in an ordinary declaration. Perhaps it
12076 would be clearer just to allow a decl-specifier-seq here, and
12077 then add a semantic restriction that if any decl-specifiers
12078 that are not type-specifiers appear, the program is invalid. */
12079 if (is_condition && !is_cv_qualifier)
12080 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12081 }
12082
12083 cp_parser_check_decl_spec (type_specifier_seq);
12084 }
12085
12086 /* Parse a parameter-declaration-clause.
12087
12088 parameter-declaration-clause:
12089 parameter-declaration-list [opt] ... [opt]
12090 parameter-declaration-list , ...
12091
12092 Returns a representation for the parameter declarations. A return
12093 value of NULL indicates a parameter-declaration-clause consisting
12094 only of an ellipsis. */
12095
12096 static cp_parameter_declarator *
12097 cp_parser_parameter_declaration_clause (cp_parser* parser)
12098 {
12099 cp_parameter_declarator *parameters;
12100 cp_token *token;
12101 bool ellipsis_p;
12102 bool is_error;
12103
12104 /* Peek at the next token. */
12105 token = cp_lexer_peek_token (parser->lexer);
12106 /* Check for trivial parameter-declaration-clauses. */
12107 if (token->type == CPP_ELLIPSIS)
12108 {
12109 /* Consume the `...' token. */
12110 cp_lexer_consume_token (parser->lexer);
12111 return NULL;
12112 }
12113 else if (token->type == CPP_CLOSE_PAREN)
12114 /* There are no parameters. */
12115 {
12116 #ifndef NO_IMPLICIT_EXTERN_C
12117 if (in_system_header && current_class_type == NULL
12118 && current_lang_name == lang_name_c)
12119 return NULL;
12120 else
12121 #endif
12122 return no_parameters;
12123 }
12124 /* Check for `(void)', too, which is a special case. */
12125 else if (token->keyword == RID_VOID
12126 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12127 == CPP_CLOSE_PAREN))
12128 {
12129 /* Consume the `void' token. */
12130 cp_lexer_consume_token (parser->lexer);
12131 /* There are no parameters. */
12132 return no_parameters;
12133 }
12134
12135 /* Parse the parameter-declaration-list. */
12136 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12137 /* If a parse error occurred while parsing the
12138 parameter-declaration-list, then the entire
12139 parameter-declaration-clause is erroneous. */
12140 if (is_error)
12141 return NULL;
12142
12143 /* Peek at the next token. */
12144 token = cp_lexer_peek_token (parser->lexer);
12145 /* If it's a `,', the clause should terminate with an ellipsis. */
12146 if (token->type == CPP_COMMA)
12147 {
12148 /* Consume the `,'. */
12149 cp_lexer_consume_token (parser->lexer);
12150 /* Expect an ellipsis. */
12151 ellipsis_p
12152 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12153 }
12154 /* It might also be `...' if the optional trailing `,' was
12155 omitted. */
12156 else if (token->type == CPP_ELLIPSIS)
12157 {
12158 /* Consume the `...' token. */
12159 cp_lexer_consume_token (parser->lexer);
12160 /* And remember that we saw it. */
12161 ellipsis_p = true;
12162 }
12163 else
12164 ellipsis_p = false;
12165
12166 /* Finish the parameter list. */
12167 if (parameters && ellipsis_p)
12168 parameters->ellipsis_p = true;
12169
12170 return parameters;
12171 }
12172
12173 /* Parse a parameter-declaration-list.
12174
12175 parameter-declaration-list:
12176 parameter-declaration
12177 parameter-declaration-list , parameter-declaration
12178
12179 Returns a representation of the parameter-declaration-list, as for
12180 cp_parser_parameter_declaration_clause. However, the
12181 `void_list_node' is never appended to the list. Upon return,
12182 *IS_ERROR will be true iff an error occurred. */
12183
12184 static cp_parameter_declarator *
12185 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12186 {
12187 cp_parameter_declarator *parameters = NULL;
12188 cp_parameter_declarator **tail = &parameters;
12189 bool saved_in_unbraced_linkage_specification_p;
12190
12191 /* Assume all will go well. */
12192 *is_error = false;
12193 /* The special considerations that apply to a function within an
12194 unbraced linkage specifications do not apply to the parameters
12195 to the function. */
12196 saved_in_unbraced_linkage_specification_p
12197 = parser->in_unbraced_linkage_specification_p;
12198 parser->in_unbraced_linkage_specification_p = false;
12199
12200 /* Look for more parameters. */
12201 while (true)
12202 {
12203 cp_parameter_declarator *parameter;
12204 bool parenthesized_p;
12205 /* Parse the parameter. */
12206 parameter
12207 = cp_parser_parameter_declaration (parser,
12208 /*template_parm_p=*/false,
12209 &parenthesized_p);
12210
12211 /* If a parse error occurred parsing the parameter declaration,
12212 then the entire parameter-declaration-list is erroneous. */
12213 if (!parameter)
12214 {
12215 *is_error = true;
12216 parameters = NULL;
12217 break;
12218 }
12219 /* Add the new parameter to the list. */
12220 *tail = parameter;
12221 tail = &parameter->next;
12222
12223 /* Peek at the next token. */
12224 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12225 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12226 /* These are for Objective-C++ */
12227 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12228 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12229 /* The parameter-declaration-list is complete. */
12230 break;
12231 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12232 {
12233 cp_token *token;
12234
12235 /* Peek at the next token. */
12236 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12237 /* If it's an ellipsis, then the list is complete. */
12238 if (token->type == CPP_ELLIPSIS)
12239 break;
12240 /* Otherwise, there must be more parameters. Consume the
12241 `,'. */
12242 cp_lexer_consume_token (parser->lexer);
12243 /* When parsing something like:
12244
12245 int i(float f, double d)
12246
12247 we can tell after seeing the declaration for "f" that we
12248 are not looking at an initialization of a variable "i",
12249 but rather at the declaration of a function "i".
12250
12251 Due to the fact that the parsing of template arguments
12252 (as specified to a template-id) requires backtracking we
12253 cannot use this technique when inside a template argument
12254 list. */
12255 if (!parser->in_template_argument_list_p
12256 && !parser->in_type_id_in_expr_p
12257 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12258 /* However, a parameter-declaration of the form
12259 "foat(f)" (which is a valid declaration of a
12260 parameter "f") can also be interpreted as an
12261 expression (the conversion of "f" to "float"). */
12262 && !parenthesized_p)
12263 cp_parser_commit_to_tentative_parse (parser);
12264 }
12265 else
12266 {
12267 cp_parser_error (parser, "expected %<,%> or %<...%>");
12268 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12269 cp_parser_skip_to_closing_parenthesis (parser,
12270 /*recovering=*/true,
12271 /*or_comma=*/false,
12272 /*consume_paren=*/false);
12273 break;
12274 }
12275 }
12276
12277 parser->in_unbraced_linkage_specification_p
12278 = saved_in_unbraced_linkage_specification_p;
12279
12280 return parameters;
12281 }
12282
12283 /* Parse a parameter declaration.
12284
12285 parameter-declaration:
12286 decl-specifier-seq declarator
12287 decl-specifier-seq declarator = assignment-expression
12288 decl-specifier-seq abstract-declarator [opt]
12289 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12290
12291 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12292 declares a template parameter. (In that case, a non-nested `>'
12293 token encountered during the parsing of the assignment-expression
12294 is not interpreted as a greater-than operator.)
12295
12296 Returns a representation of the parameter, or NULL if an error
12297 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12298 true iff the declarator is of the form "(p)". */
12299
12300 static cp_parameter_declarator *
12301 cp_parser_parameter_declaration (cp_parser *parser,
12302 bool template_parm_p,
12303 bool *parenthesized_p)
12304 {
12305 int declares_class_or_enum;
12306 bool greater_than_is_operator_p;
12307 cp_decl_specifier_seq decl_specifiers;
12308 cp_declarator *declarator;
12309 tree default_argument;
12310 cp_token *token;
12311 const char *saved_message;
12312
12313 /* In a template parameter, `>' is not an operator.
12314
12315 [temp.param]
12316
12317 When parsing a default template-argument for a non-type
12318 template-parameter, the first non-nested `>' is taken as the end
12319 of the template parameter-list rather than a greater-than
12320 operator. */
12321 greater_than_is_operator_p = !template_parm_p;
12322
12323 /* Type definitions may not appear in parameter types. */
12324 saved_message = parser->type_definition_forbidden_message;
12325 parser->type_definition_forbidden_message
12326 = "types may not be defined in parameter types";
12327
12328 /* Parse the declaration-specifiers. */
12329 cp_parser_decl_specifier_seq (parser,
12330 CP_PARSER_FLAGS_NONE,
12331 &decl_specifiers,
12332 &declares_class_or_enum);
12333 /* If an error occurred, there's no reason to attempt to parse the
12334 rest of the declaration. */
12335 if (cp_parser_error_occurred (parser))
12336 {
12337 parser->type_definition_forbidden_message = saved_message;
12338 return NULL;
12339 }
12340
12341 /* Peek at the next token. */
12342 token = cp_lexer_peek_token (parser->lexer);
12343 /* If the next token is a `)', `,', `=', `>', or `...', then there
12344 is no declarator. */
12345 if (token->type == CPP_CLOSE_PAREN
12346 || token->type == CPP_COMMA
12347 || token->type == CPP_EQ
12348 || token->type == CPP_ELLIPSIS
12349 || token->type == CPP_GREATER)
12350 {
12351 declarator = NULL;
12352 if (parenthesized_p)
12353 *parenthesized_p = false;
12354 }
12355 /* Otherwise, there should be a declarator. */
12356 else
12357 {
12358 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12359 parser->default_arg_ok_p = false;
12360
12361 /* After seeing a decl-specifier-seq, if the next token is not a
12362 "(", there is no possibility that the code is a valid
12363 expression. Therefore, if parsing tentatively, we commit at
12364 this point. */
12365 if (!parser->in_template_argument_list_p
12366 /* In an expression context, having seen:
12367
12368 (int((char ...
12369
12370 we cannot be sure whether we are looking at a
12371 function-type (taking a "char" as a parameter) or a cast
12372 of some object of type "char" to "int". */
12373 && !parser->in_type_id_in_expr_p
12374 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12375 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12376 cp_parser_commit_to_tentative_parse (parser);
12377 /* Parse the declarator. */
12378 declarator = cp_parser_declarator (parser,
12379 CP_PARSER_DECLARATOR_EITHER,
12380 /*ctor_dtor_or_conv_p=*/NULL,
12381 parenthesized_p,
12382 /*member_p=*/false);
12383 parser->default_arg_ok_p = saved_default_arg_ok_p;
12384 /* After the declarator, allow more attributes. */
12385 decl_specifiers.attributes
12386 = chainon (decl_specifiers.attributes,
12387 cp_parser_attributes_opt (parser));
12388 }
12389
12390 /* The restriction on defining new types applies only to the type
12391 of the parameter, not to the default argument. */
12392 parser->type_definition_forbidden_message = saved_message;
12393
12394 /* If the next token is `=', then process a default argument. */
12395 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12396 {
12397 bool saved_greater_than_is_operator_p;
12398 /* Consume the `='. */
12399 cp_lexer_consume_token (parser->lexer);
12400
12401 /* If we are defining a class, then the tokens that make up the
12402 default argument must be saved and processed later. */
12403 if (!template_parm_p && at_class_scope_p ()
12404 && TYPE_BEING_DEFINED (current_class_type))
12405 {
12406 unsigned depth = 0;
12407 cp_token *first_token;
12408 cp_token *token;
12409
12410 /* Add tokens until we have processed the entire default
12411 argument. We add the range [first_token, token). */
12412 first_token = cp_lexer_peek_token (parser->lexer);
12413 while (true)
12414 {
12415 bool done = false;
12416
12417 /* Peek at the next token. */
12418 token = cp_lexer_peek_token (parser->lexer);
12419 /* What we do depends on what token we have. */
12420 switch (token->type)
12421 {
12422 /* In valid code, a default argument must be
12423 immediately followed by a `,' `)', or `...'. */
12424 case CPP_COMMA:
12425 case CPP_CLOSE_PAREN:
12426 case CPP_ELLIPSIS:
12427 /* If we run into a non-nested `;', `}', or `]',
12428 then the code is invalid -- but the default
12429 argument is certainly over. */
12430 case CPP_SEMICOLON:
12431 case CPP_CLOSE_BRACE:
12432 case CPP_CLOSE_SQUARE:
12433 if (depth == 0)
12434 done = true;
12435 /* Update DEPTH, if necessary. */
12436 else if (token->type == CPP_CLOSE_PAREN
12437 || token->type == CPP_CLOSE_BRACE
12438 || token->type == CPP_CLOSE_SQUARE)
12439 --depth;
12440 break;
12441
12442 case CPP_OPEN_PAREN:
12443 case CPP_OPEN_SQUARE:
12444 case CPP_OPEN_BRACE:
12445 ++depth;
12446 break;
12447
12448 case CPP_GREATER:
12449 /* If we see a non-nested `>', and `>' is not an
12450 operator, then it marks the end of the default
12451 argument. */
12452 if (!depth && !greater_than_is_operator_p)
12453 done = true;
12454 break;
12455
12456 /* If we run out of tokens, issue an error message. */
12457 case CPP_EOF:
12458 case CPP_PRAGMA_EOL:
12459 error ("file ends in default argument");
12460 done = true;
12461 break;
12462
12463 case CPP_NAME:
12464 case CPP_SCOPE:
12465 /* In these cases, we should look for template-ids.
12466 For example, if the default argument is
12467 `X<int, double>()', we need to do name lookup to
12468 figure out whether or not `X' is a template; if
12469 so, the `,' does not end the default argument.
12470
12471 That is not yet done. */
12472 break;
12473
12474 default:
12475 break;
12476 }
12477
12478 /* If we've reached the end, stop. */
12479 if (done)
12480 break;
12481
12482 /* Add the token to the token block. */
12483 token = cp_lexer_consume_token (parser->lexer);
12484 }
12485
12486 /* Create a DEFAULT_ARG to represented the unparsed default
12487 argument. */
12488 default_argument = make_node (DEFAULT_ARG);
12489 DEFARG_TOKENS (default_argument)
12490 = cp_token_cache_new (first_token, token);
12491 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12492 }
12493 /* Outside of a class definition, we can just parse the
12494 assignment-expression. */
12495 else
12496 {
12497 bool saved_local_variables_forbidden_p;
12498
12499 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12500 set correctly. */
12501 saved_greater_than_is_operator_p
12502 = parser->greater_than_is_operator_p;
12503 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12504 /* Local variable names (and the `this' keyword) may not
12505 appear in a default argument. */
12506 saved_local_variables_forbidden_p
12507 = parser->local_variables_forbidden_p;
12508 parser->local_variables_forbidden_p = true;
12509 /* The default argument expression may cause implicitly
12510 defined member functions to be synthesized, which will
12511 result in garbage collection. We must treat this
12512 situation as if we were within the body of function so as
12513 to avoid collecting live data on the stack. */
12514 ++function_depth;
12515 /* Parse the assignment-expression. */
12516 if (template_parm_p)
12517 push_deferring_access_checks (dk_no_deferred);
12518 default_argument
12519 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12520 if (template_parm_p)
12521 pop_deferring_access_checks ();
12522 /* Restore saved state. */
12523 --function_depth;
12524 parser->greater_than_is_operator_p
12525 = saved_greater_than_is_operator_p;
12526 parser->local_variables_forbidden_p
12527 = saved_local_variables_forbidden_p;
12528 }
12529 if (!parser->default_arg_ok_p)
12530 {
12531 if (!flag_pedantic_errors)
12532 warning (0, "deprecated use of default argument for parameter of non-function");
12533 else
12534 {
12535 error ("default arguments are only permitted for function parameters");
12536 default_argument = NULL_TREE;
12537 }
12538 }
12539 }
12540 else
12541 default_argument = NULL_TREE;
12542
12543 return make_parameter_declarator (&decl_specifiers,
12544 declarator,
12545 default_argument);
12546 }
12547
12548 /* Parse a function-body.
12549
12550 function-body:
12551 compound_statement */
12552
12553 static void
12554 cp_parser_function_body (cp_parser *parser)
12555 {
12556 cp_parser_compound_statement (parser, NULL, false);
12557 }
12558
12559 /* Parse a ctor-initializer-opt followed by a function-body. Return
12560 true if a ctor-initializer was present. */
12561
12562 static bool
12563 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12564 {
12565 tree body;
12566 bool ctor_initializer_p;
12567
12568 /* Begin the function body. */
12569 body = begin_function_body ();
12570 /* Parse the optional ctor-initializer. */
12571 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12572 /* Parse the function-body. */
12573 cp_parser_function_body (parser);
12574 /* Finish the function body. */
12575 finish_function_body (body);
12576
12577 return ctor_initializer_p;
12578 }
12579
12580 /* Parse an initializer.
12581
12582 initializer:
12583 = initializer-clause
12584 ( expression-list )
12585
12586 Returns an expression representing the initializer. If no
12587 initializer is present, NULL_TREE is returned.
12588
12589 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12590 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12591 set to FALSE if there is no initializer present. If there is an
12592 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12593 is set to true; otherwise it is set to false. */
12594
12595 static tree
12596 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12597 bool* non_constant_p)
12598 {
12599 cp_token *token;
12600 tree init;
12601
12602 /* Peek at the next token. */
12603 token = cp_lexer_peek_token (parser->lexer);
12604
12605 /* Let our caller know whether or not this initializer was
12606 parenthesized. */
12607 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12608 /* Assume that the initializer is constant. */
12609 *non_constant_p = false;
12610
12611 if (token->type == CPP_EQ)
12612 {
12613 /* Consume the `='. */
12614 cp_lexer_consume_token (parser->lexer);
12615 /* Parse the initializer-clause. */
12616 init = cp_parser_initializer_clause (parser, non_constant_p);
12617 }
12618 else if (token->type == CPP_OPEN_PAREN)
12619 init = cp_parser_parenthesized_expression_list (parser, false,
12620 /*cast_p=*/false,
12621 non_constant_p);
12622 else
12623 {
12624 /* Anything else is an error. */
12625 cp_parser_error (parser, "expected initializer");
12626 init = error_mark_node;
12627 }
12628
12629 return init;
12630 }
12631
12632 /* Parse an initializer-clause.
12633
12634 initializer-clause:
12635 assignment-expression
12636 { initializer-list , [opt] }
12637 { }
12638
12639 Returns an expression representing the initializer.
12640
12641 If the `assignment-expression' production is used the value
12642 returned is simply a representation for the expression.
12643
12644 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12645 the elements of the initializer-list (or NULL, if the last
12646 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12647 NULL_TREE. There is no way to detect whether or not the optional
12648 trailing `,' was provided. NON_CONSTANT_P is as for
12649 cp_parser_initializer. */
12650
12651 static tree
12652 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12653 {
12654 tree initializer;
12655
12656 /* Assume the expression is constant. */
12657 *non_constant_p = false;
12658
12659 /* If it is not a `{', then we are looking at an
12660 assignment-expression. */
12661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12662 {
12663 initializer
12664 = cp_parser_constant_expression (parser,
12665 /*allow_non_constant_p=*/true,
12666 non_constant_p);
12667 if (!*non_constant_p)
12668 initializer = fold_non_dependent_expr (initializer);
12669 }
12670 else
12671 {
12672 /* Consume the `{' token. */
12673 cp_lexer_consume_token (parser->lexer);
12674 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12675 initializer = make_node (CONSTRUCTOR);
12676 /* If it's not a `}', then there is a non-trivial initializer. */
12677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12678 {
12679 /* Parse the initializer list. */
12680 CONSTRUCTOR_ELTS (initializer)
12681 = cp_parser_initializer_list (parser, non_constant_p);
12682 /* A trailing `,' token is allowed. */
12683 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12684 cp_lexer_consume_token (parser->lexer);
12685 }
12686 /* Now, there should be a trailing `}'. */
12687 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12688 }
12689
12690 return initializer;
12691 }
12692
12693 /* Parse an initializer-list.
12694
12695 initializer-list:
12696 initializer-clause
12697 initializer-list , initializer-clause
12698
12699 GNU Extension:
12700
12701 initializer-list:
12702 identifier : initializer-clause
12703 initializer-list, identifier : initializer-clause
12704
12705 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12706 for the initializer. If the INDEX of the elt is non-NULL, it is the
12707 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12708 as for cp_parser_initializer. */
12709
12710 static VEC(constructor_elt,gc) *
12711 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12712 {
12713 VEC(constructor_elt,gc) *v = NULL;
12714
12715 /* Assume all of the expressions are constant. */
12716 *non_constant_p = false;
12717
12718 /* Parse the rest of the list. */
12719 while (true)
12720 {
12721 cp_token *token;
12722 tree identifier;
12723 tree initializer;
12724 bool clause_non_constant_p;
12725
12726 /* If the next token is an identifier and the following one is a
12727 colon, we are looking at the GNU designated-initializer
12728 syntax. */
12729 if (cp_parser_allow_gnu_extensions_p (parser)
12730 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12731 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12732 {
12733 /* Consume the identifier. */
12734 identifier = cp_lexer_consume_token (parser->lexer)->value;
12735 /* Consume the `:'. */
12736 cp_lexer_consume_token (parser->lexer);
12737 }
12738 else
12739 identifier = NULL_TREE;
12740
12741 /* Parse the initializer. */
12742 initializer = cp_parser_initializer_clause (parser,
12743 &clause_non_constant_p);
12744 /* If any clause is non-constant, so is the entire initializer. */
12745 if (clause_non_constant_p)
12746 *non_constant_p = true;
12747
12748 /* Add it to the vector. */
12749 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12750
12751 /* If the next token is not a comma, we have reached the end of
12752 the list. */
12753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12754 break;
12755
12756 /* Peek at the next token. */
12757 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12758 /* If the next token is a `}', then we're still done. An
12759 initializer-clause can have a trailing `,' after the
12760 initializer-list and before the closing `}'. */
12761 if (token->type == CPP_CLOSE_BRACE)
12762 break;
12763
12764 /* Consume the `,' token. */
12765 cp_lexer_consume_token (parser->lexer);
12766 }
12767
12768 return v;
12769 }
12770
12771 /* Classes [gram.class] */
12772
12773 /* Parse a class-name.
12774
12775 class-name:
12776 identifier
12777 template-id
12778
12779 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12780 to indicate that names looked up in dependent types should be
12781 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12782 keyword has been used to indicate that the name that appears next
12783 is a template. TAG_TYPE indicates the explicit tag given before
12784 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12785 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12786 is the class being defined in a class-head.
12787
12788 Returns the TYPE_DECL representing the class. */
12789
12790 static tree
12791 cp_parser_class_name (cp_parser *parser,
12792 bool typename_keyword_p,
12793 bool template_keyword_p,
12794 enum tag_types tag_type,
12795 bool check_dependency_p,
12796 bool class_head_p,
12797 bool is_declaration)
12798 {
12799 tree decl;
12800 tree scope;
12801 bool typename_p;
12802 cp_token *token;
12803
12804 /* All class-names start with an identifier. */
12805 token = cp_lexer_peek_token (parser->lexer);
12806 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12807 {
12808 cp_parser_error (parser, "expected class-name");
12809 return error_mark_node;
12810 }
12811
12812 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12813 to a template-id, so we save it here. */
12814 scope = parser->scope;
12815 if (scope == error_mark_node)
12816 return error_mark_node;
12817
12818 /* Any name names a type if we're following the `typename' keyword
12819 in a qualified name where the enclosing scope is type-dependent. */
12820 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12821 && dependent_type_p (scope));
12822 /* Handle the common case (an identifier, but not a template-id)
12823 efficiently. */
12824 if (token->type == CPP_NAME
12825 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12826 {
12827 cp_token *identifier_token;
12828 tree identifier;
12829 bool ambiguous_p;
12830
12831 /* Look for the identifier. */
12832 identifier_token = cp_lexer_peek_token (parser->lexer);
12833 ambiguous_p = identifier_token->ambiguous_p;
12834 identifier = cp_parser_identifier (parser);
12835 /* If the next token isn't an identifier, we are certainly not
12836 looking at a class-name. */
12837 if (identifier == error_mark_node)
12838 decl = error_mark_node;
12839 /* If we know this is a type-name, there's no need to look it
12840 up. */
12841 else if (typename_p)
12842 decl = identifier;
12843 else
12844 {
12845 tree ambiguous_decls;
12846 /* If we already know that this lookup is ambiguous, then
12847 we've already issued an error message; there's no reason
12848 to check again. */
12849 if (ambiguous_p)
12850 {
12851 cp_parser_simulate_error (parser);
12852 return error_mark_node;
12853 }
12854 /* If the next token is a `::', then the name must be a type
12855 name.
12856
12857 [basic.lookup.qual]
12858
12859 During the lookup for a name preceding the :: scope
12860 resolution operator, object, function, and enumerator
12861 names are ignored. */
12862 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12863 tag_type = typename_type;
12864 /* Look up the name. */
12865 decl = cp_parser_lookup_name (parser, identifier,
12866 tag_type,
12867 /*is_template=*/false,
12868 /*is_namespace=*/false,
12869 check_dependency_p,
12870 &ambiguous_decls);
12871 if (ambiguous_decls)
12872 {
12873 error ("reference to %qD is ambiguous", identifier);
12874 print_candidates (ambiguous_decls);
12875 if (cp_parser_parsing_tentatively (parser))
12876 {
12877 identifier_token->ambiguous_p = true;
12878 cp_parser_simulate_error (parser);
12879 }
12880 return error_mark_node;
12881 }
12882 }
12883 }
12884 else
12885 {
12886 /* Try a template-id. */
12887 decl = cp_parser_template_id (parser, template_keyword_p,
12888 check_dependency_p,
12889 is_declaration);
12890 if (decl == error_mark_node)
12891 return error_mark_node;
12892 }
12893
12894 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12895
12896 /* If this is a typename, create a TYPENAME_TYPE. */
12897 if (typename_p && decl != error_mark_node)
12898 {
12899 decl = make_typename_type (scope, decl, typename_type,
12900 /*complain=*/tf_error);
12901 if (decl != error_mark_node)
12902 decl = TYPE_NAME (decl);
12903 }
12904
12905 /* Check to see that it is really the name of a class. */
12906 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12907 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12908 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12909 /* Situations like this:
12910
12911 template <typename T> struct A {
12912 typename T::template X<int>::I i;
12913 };
12914
12915 are problematic. Is `T::template X<int>' a class-name? The
12916 standard does not seem to be definitive, but there is no other
12917 valid interpretation of the following `::'. Therefore, those
12918 names are considered class-names. */
12919 {
12920 decl = make_typename_type (scope, decl, tag_type, tf_error);
12921 if (decl != error_mark_node)
12922 decl = TYPE_NAME (decl);
12923 }
12924 else if (TREE_CODE (decl) != TYPE_DECL
12925 || TREE_TYPE (decl) == error_mark_node
12926 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12927 decl = error_mark_node;
12928
12929 if (decl == error_mark_node)
12930 cp_parser_error (parser, "expected class-name");
12931
12932 return decl;
12933 }
12934
12935 /* Parse a class-specifier.
12936
12937 class-specifier:
12938 class-head { member-specification [opt] }
12939
12940 Returns the TREE_TYPE representing the class. */
12941
12942 static tree
12943 cp_parser_class_specifier (cp_parser* parser)
12944 {
12945 cp_token *token;
12946 tree type;
12947 tree attributes = NULL_TREE;
12948 int has_trailing_semicolon;
12949 bool nested_name_specifier_p;
12950 unsigned saved_num_template_parameter_lists;
12951 tree old_scope = NULL_TREE;
12952 tree scope = NULL_TREE;
12953
12954 push_deferring_access_checks (dk_no_deferred);
12955
12956 /* Parse the class-head. */
12957 type = cp_parser_class_head (parser,
12958 &nested_name_specifier_p,
12959 &attributes);
12960 /* If the class-head was a semantic disaster, skip the entire body
12961 of the class. */
12962 if (!type)
12963 {
12964 cp_parser_skip_to_end_of_block_or_statement (parser);
12965 pop_deferring_access_checks ();
12966 return error_mark_node;
12967 }
12968
12969 /* Look for the `{'. */
12970 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12971 {
12972 pop_deferring_access_checks ();
12973 return error_mark_node;
12974 }
12975
12976 /* Issue an error message if type-definitions are forbidden here. */
12977 cp_parser_check_type_definition (parser);
12978 /* Remember that we are defining one more class. */
12979 ++parser->num_classes_being_defined;
12980 /* Inside the class, surrounding template-parameter-lists do not
12981 apply. */
12982 saved_num_template_parameter_lists
12983 = parser->num_template_parameter_lists;
12984 parser->num_template_parameter_lists = 0;
12985
12986 /* Start the class. */
12987 if (nested_name_specifier_p)
12988 {
12989 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12990 old_scope = push_inner_scope (scope);
12991 }
12992 type = begin_class_definition (type, attributes);
12993
12994 if (type == error_mark_node)
12995 /* If the type is erroneous, skip the entire body of the class. */
12996 cp_parser_skip_to_closing_brace (parser);
12997 else
12998 /* Parse the member-specification. */
12999 cp_parser_member_specification_opt (parser);
13000
13001 /* Look for the trailing `}'. */
13002 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13003 /* We get better error messages by noticing a common problem: a
13004 missing trailing `;'. */
13005 token = cp_lexer_peek_token (parser->lexer);
13006 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13007 /* Look for trailing attributes to apply to this class. */
13008 if (cp_parser_allow_gnu_extensions_p (parser))
13009 attributes = cp_parser_attributes_opt (parser);
13010 if (type != error_mark_node)
13011 type = finish_struct (type, attributes);
13012 if (nested_name_specifier_p)
13013 pop_inner_scope (old_scope, scope);
13014 /* If this class is not itself within the scope of another class,
13015 then we need to parse the bodies of all of the queued function
13016 definitions. Note that the queued functions defined in a class
13017 are not always processed immediately following the
13018 class-specifier for that class. Consider:
13019
13020 struct A {
13021 struct B { void f() { sizeof (A); } };
13022 };
13023
13024 If `f' were processed before the processing of `A' were
13025 completed, there would be no way to compute the size of `A'.
13026 Note that the nesting we are interested in here is lexical --
13027 not the semantic nesting given by TYPE_CONTEXT. In particular,
13028 for:
13029
13030 struct A { struct B; };
13031 struct A::B { void f() { } };
13032
13033 there is no need to delay the parsing of `A::B::f'. */
13034 if (--parser->num_classes_being_defined == 0)
13035 {
13036 tree queue_entry;
13037 tree fn;
13038 tree class_type = NULL_TREE;
13039 tree pushed_scope = NULL_TREE;
13040
13041 /* In a first pass, parse default arguments to the functions.
13042 Then, in a second pass, parse the bodies of the functions.
13043 This two-phased approach handles cases like:
13044
13045 struct S {
13046 void f() { g(); }
13047 void g(int i = 3);
13048 };
13049
13050 */
13051 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13052 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13053 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13054 TREE_PURPOSE (parser->unparsed_functions_queues)
13055 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13056 {
13057 fn = TREE_VALUE (queue_entry);
13058 /* If there are default arguments that have not yet been processed,
13059 take care of them now. */
13060 if (class_type != TREE_PURPOSE (queue_entry))
13061 {
13062 if (pushed_scope)
13063 pop_scope (pushed_scope);
13064 class_type = TREE_PURPOSE (queue_entry);
13065 pushed_scope = push_scope (class_type);
13066 }
13067 /* Make sure that any template parameters are in scope. */
13068 maybe_begin_member_template_processing (fn);
13069 /* Parse the default argument expressions. */
13070 cp_parser_late_parsing_default_args (parser, fn);
13071 /* Remove any template parameters from the symbol table. */
13072 maybe_end_member_template_processing ();
13073 }
13074 if (pushed_scope)
13075 pop_scope (pushed_scope);
13076 /* Now parse the body of the functions. */
13077 for (TREE_VALUE (parser->unparsed_functions_queues)
13078 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13079 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13080 TREE_VALUE (parser->unparsed_functions_queues)
13081 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13082 {
13083 /* Figure out which function we need to process. */
13084 fn = TREE_VALUE (queue_entry);
13085 /* Parse the function. */
13086 cp_parser_late_parsing_for_member (parser, fn);
13087 }
13088 }
13089
13090 /* Put back any saved access checks. */
13091 pop_deferring_access_checks ();
13092
13093 /* Restore the count of active template-parameter-lists. */
13094 parser->num_template_parameter_lists
13095 = saved_num_template_parameter_lists;
13096
13097 return type;
13098 }
13099
13100 /* Parse a class-head.
13101
13102 class-head:
13103 class-key identifier [opt] base-clause [opt]
13104 class-key nested-name-specifier identifier base-clause [opt]
13105 class-key nested-name-specifier [opt] template-id
13106 base-clause [opt]
13107
13108 GNU Extensions:
13109 class-key attributes identifier [opt] base-clause [opt]
13110 class-key attributes nested-name-specifier identifier base-clause [opt]
13111 class-key attributes nested-name-specifier [opt] template-id
13112 base-clause [opt]
13113
13114 Returns the TYPE of the indicated class. Sets
13115 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13116 involving a nested-name-specifier was used, and FALSE otherwise.
13117
13118 Returns error_mark_node if this is not a class-head.
13119
13120 Returns NULL_TREE if the class-head is syntactically valid, but
13121 semantically invalid in a way that means we should skip the entire
13122 body of the class. */
13123
13124 static tree
13125 cp_parser_class_head (cp_parser* parser,
13126 bool* nested_name_specifier_p,
13127 tree *attributes_p)
13128 {
13129 tree nested_name_specifier;
13130 enum tag_types class_key;
13131 tree id = NULL_TREE;
13132 tree type = NULL_TREE;
13133 tree attributes;
13134 bool template_id_p = false;
13135 bool qualified_p = false;
13136 bool invalid_nested_name_p = false;
13137 bool invalid_explicit_specialization_p = false;
13138 tree pushed_scope = NULL_TREE;
13139 unsigned num_templates;
13140 tree bases;
13141
13142 /* Assume no nested-name-specifier will be present. */
13143 *nested_name_specifier_p = false;
13144 /* Assume no template parameter lists will be used in defining the
13145 type. */
13146 num_templates = 0;
13147
13148 /* Look for the class-key. */
13149 class_key = cp_parser_class_key (parser);
13150 if (class_key == none_type)
13151 return error_mark_node;
13152
13153 /* Parse the attributes. */
13154 attributes = cp_parser_attributes_opt (parser);
13155
13156 /* If the next token is `::', that is invalid -- but sometimes
13157 people do try to write:
13158
13159 struct ::S {};
13160
13161 Handle this gracefully by accepting the extra qualifier, and then
13162 issuing an error about it later if this really is a
13163 class-head. If it turns out just to be an elaborated type
13164 specifier, remain silent. */
13165 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13166 qualified_p = true;
13167
13168 push_deferring_access_checks (dk_no_check);
13169
13170 /* Determine the name of the class. Begin by looking for an
13171 optional nested-name-specifier. */
13172 nested_name_specifier
13173 = cp_parser_nested_name_specifier_opt (parser,
13174 /*typename_keyword_p=*/false,
13175 /*check_dependency_p=*/false,
13176 /*type_p=*/false,
13177 /*is_declaration=*/false);
13178 /* If there was a nested-name-specifier, then there *must* be an
13179 identifier. */
13180 if (nested_name_specifier)
13181 {
13182 /* Although the grammar says `identifier', it really means
13183 `class-name' or `template-name'. You are only allowed to
13184 define a class that has already been declared with this
13185 syntax.
13186
13187 The proposed resolution for Core Issue 180 says that wherever
13188 you see `class T::X' you should treat `X' as a type-name.
13189
13190 It is OK to define an inaccessible class; for example:
13191
13192 class A { class B; };
13193 class A::B {};
13194
13195 We do not know if we will see a class-name, or a
13196 template-name. We look for a class-name first, in case the
13197 class-name is a template-id; if we looked for the
13198 template-name first we would stop after the template-name. */
13199 cp_parser_parse_tentatively (parser);
13200 type = cp_parser_class_name (parser,
13201 /*typename_keyword_p=*/false,
13202 /*template_keyword_p=*/false,
13203 class_type,
13204 /*check_dependency_p=*/false,
13205 /*class_head_p=*/true,
13206 /*is_declaration=*/false);
13207 /* If that didn't work, ignore the nested-name-specifier. */
13208 if (!cp_parser_parse_definitely (parser))
13209 {
13210 invalid_nested_name_p = true;
13211 id = cp_parser_identifier (parser);
13212 if (id == error_mark_node)
13213 id = NULL_TREE;
13214 }
13215 /* If we could not find a corresponding TYPE, treat this
13216 declaration like an unqualified declaration. */
13217 if (type == error_mark_node)
13218 nested_name_specifier = NULL_TREE;
13219 /* Otherwise, count the number of templates used in TYPE and its
13220 containing scopes. */
13221 else
13222 {
13223 tree scope;
13224
13225 for (scope = TREE_TYPE (type);
13226 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13227 scope = (TYPE_P (scope)
13228 ? TYPE_CONTEXT (scope)
13229 : DECL_CONTEXT (scope)))
13230 if (TYPE_P (scope)
13231 && CLASS_TYPE_P (scope)
13232 && CLASSTYPE_TEMPLATE_INFO (scope)
13233 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13234 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13235 ++num_templates;
13236 }
13237 }
13238 /* Otherwise, the identifier is optional. */
13239 else
13240 {
13241 /* We don't know whether what comes next is a template-id,
13242 an identifier, or nothing at all. */
13243 cp_parser_parse_tentatively (parser);
13244 /* Check for a template-id. */
13245 id = cp_parser_template_id (parser,
13246 /*template_keyword_p=*/false,
13247 /*check_dependency_p=*/true,
13248 /*is_declaration=*/true);
13249 /* If that didn't work, it could still be an identifier. */
13250 if (!cp_parser_parse_definitely (parser))
13251 {
13252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13253 id = cp_parser_identifier (parser);
13254 else
13255 id = NULL_TREE;
13256 }
13257 else
13258 {
13259 template_id_p = true;
13260 ++num_templates;
13261 }
13262 }
13263
13264 pop_deferring_access_checks ();
13265
13266 if (id)
13267 cp_parser_check_for_invalid_template_id (parser, id);
13268
13269 /* If it's not a `:' or a `{' then we can't really be looking at a
13270 class-head, since a class-head only appears as part of a
13271 class-specifier. We have to detect this situation before calling
13272 xref_tag, since that has irreversible side-effects. */
13273 if (!cp_parser_next_token_starts_class_definition_p (parser))
13274 {
13275 cp_parser_error (parser, "expected %<{%> or %<:%>");
13276 return error_mark_node;
13277 }
13278
13279 /* At this point, we're going ahead with the class-specifier, even
13280 if some other problem occurs. */
13281 cp_parser_commit_to_tentative_parse (parser);
13282 /* Issue the error about the overly-qualified name now. */
13283 if (qualified_p)
13284 cp_parser_error (parser,
13285 "global qualification of class name is invalid");
13286 else if (invalid_nested_name_p)
13287 cp_parser_error (parser,
13288 "qualified name does not name a class");
13289 else if (nested_name_specifier)
13290 {
13291 tree scope;
13292
13293 /* Reject typedef-names in class heads. */
13294 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13295 {
13296 error ("invalid class name in declaration of %qD", type);
13297 type = NULL_TREE;
13298 goto done;
13299 }
13300
13301 /* Figure out in what scope the declaration is being placed. */
13302 scope = current_scope ();
13303 /* If that scope does not contain the scope in which the
13304 class was originally declared, the program is invalid. */
13305 if (scope && !is_ancestor (scope, nested_name_specifier))
13306 {
13307 error ("declaration of %qD in %qD which does not enclose %qD",
13308 type, scope, nested_name_specifier);
13309 type = NULL_TREE;
13310 goto done;
13311 }
13312 /* [dcl.meaning]
13313
13314 A declarator-id shall not be qualified exception of the
13315 definition of a ... nested class outside of its class
13316 ... [or] a the definition or explicit instantiation of a
13317 class member of a namespace outside of its namespace. */
13318 if (scope == nested_name_specifier)
13319 {
13320 pedwarn ("extra qualification ignored");
13321 nested_name_specifier = NULL_TREE;
13322 num_templates = 0;
13323 }
13324 }
13325 /* An explicit-specialization must be preceded by "template <>". If
13326 it is not, try to recover gracefully. */
13327 if (at_namespace_scope_p ()
13328 && parser->num_template_parameter_lists == 0
13329 && template_id_p)
13330 {
13331 error ("an explicit specialization must be preceded by %<template <>%>");
13332 invalid_explicit_specialization_p = true;
13333 /* Take the same action that would have been taken by
13334 cp_parser_explicit_specialization. */
13335 ++parser->num_template_parameter_lists;
13336 begin_specialization ();
13337 }
13338 /* There must be no "return" statements between this point and the
13339 end of this function; set "type "to the correct return value and
13340 use "goto done;" to return. */
13341 /* Make sure that the right number of template parameters were
13342 present. */
13343 if (!cp_parser_check_template_parameters (parser, num_templates))
13344 {
13345 /* If something went wrong, there is no point in even trying to
13346 process the class-definition. */
13347 type = NULL_TREE;
13348 goto done;
13349 }
13350
13351 /* Look up the type. */
13352 if (template_id_p)
13353 {
13354 type = TREE_TYPE (id);
13355 type = maybe_process_partial_specialization (type);
13356 if (nested_name_specifier)
13357 pushed_scope = push_scope (nested_name_specifier);
13358 }
13359 else if (nested_name_specifier)
13360 {
13361 tree class_type;
13362
13363 /* Given:
13364
13365 template <typename T> struct S { struct T };
13366 template <typename T> struct S<T>::T { };
13367
13368 we will get a TYPENAME_TYPE when processing the definition of
13369 `S::T'. We need to resolve it to the actual type before we
13370 try to define it. */
13371 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13372 {
13373 class_type = resolve_typename_type (TREE_TYPE (type),
13374 /*only_current_p=*/false);
13375 if (class_type != error_mark_node)
13376 type = TYPE_NAME (class_type);
13377 else
13378 {
13379 cp_parser_error (parser, "could not resolve typename type");
13380 type = error_mark_node;
13381 }
13382 }
13383
13384 maybe_process_partial_specialization (TREE_TYPE (type));
13385 class_type = current_class_type;
13386 /* Enter the scope indicated by the nested-name-specifier. */
13387 pushed_scope = push_scope (nested_name_specifier);
13388 /* Get the canonical version of this type. */
13389 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13390 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13391 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13392 {
13393 type = push_template_decl (type);
13394 if (type == error_mark_node)
13395 {
13396 type = NULL_TREE;
13397 goto done;
13398 }
13399 }
13400
13401 type = TREE_TYPE (type);
13402 *nested_name_specifier_p = true;
13403 }
13404 else /* The name is not a nested name. */
13405 {
13406 /* If the class was unnamed, create a dummy name. */
13407 if (!id)
13408 id = make_anon_name ();
13409 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13410 parser->num_template_parameter_lists);
13411 }
13412
13413 /* Indicate whether this class was declared as a `class' or as a
13414 `struct'. */
13415 if (TREE_CODE (type) == RECORD_TYPE)
13416 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13417 cp_parser_check_class_key (class_key, type);
13418
13419 /* If this type was already complete, and we see another definition,
13420 that's an error. */
13421 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13422 {
13423 error ("redefinition of %q#T", type);
13424 error ("previous definition of %q+#T", type);
13425 type = NULL_TREE;
13426 goto done;
13427 }
13428
13429 /* We will have entered the scope containing the class; the names of
13430 base classes should be looked up in that context. For example:
13431
13432 struct A { struct B {}; struct C; };
13433 struct A::C : B {};
13434
13435 is valid. */
13436 bases = NULL_TREE;
13437
13438 /* Get the list of base-classes, if there is one. */
13439 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13440 bases = cp_parser_base_clause (parser);
13441
13442 /* Process the base classes. */
13443 xref_basetypes (type, bases);
13444
13445 done:
13446 /* Leave the scope given by the nested-name-specifier. We will
13447 enter the class scope itself while processing the members. */
13448 if (pushed_scope)
13449 pop_scope (pushed_scope);
13450
13451 if (invalid_explicit_specialization_p)
13452 {
13453 end_specialization ();
13454 --parser->num_template_parameter_lists;
13455 }
13456 *attributes_p = attributes;
13457 return type;
13458 }
13459
13460 /* Parse a class-key.
13461
13462 class-key:
13463 class
13464 struct
13465 union
13466
13467 Returns the kind of class-key specified, or none_type to indicate
13468 error. */
13469
13470 static enum tag_types
13471 cp_parser_class_key (cp_parser* parser)
13472 {
13473 cp_token *token;
13474 enum tag_types tag_type;
13475
13476 /* Look for the class-key. */
13477 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13478 if (!token)
13479 return none_type;
13480
13481 /* Check to see if the TOKEN is a class-key. */
13482 tag_type = cp_parser_token_is_class_key (token);
13483 if (!tag_type)
13484 cp_parser_error (parser, "expected class-key");
13485 return tag_type;
13486 }
13487
13488 /* Parse an (optional) member-specification.
13489
13490 member-specification:
13491 member-declaration member-specification [opt]
13492 access-specifier : member-specification [opt] */
13493
13494 static void
13495 cp_parser_member_specification_opt (cp_parser* parser)
13496 {
13497 while (true)
13498 {
13499 cp_token *token;
13500 enum rid keyword;
13501
13502 /* Peek at the next token. */
13503 token = cp_lexer_peek_token (parser->lexer);
13504 /* If it's a `}', or EOF then we've seen all the members. */
13505 if (token->type == CPP_CLOSE_BRACE
13506 || token->type == CPP_EOF
13507 || token->type == CPP_PRAGMA_EOL)
13508 break;
13509
13510 /* See if this token is a keyword. */
13511 keyword = token->keyword;
13512 switch (keyword)
13513 {
13514 case RID_PUBLIC:
13515 case RID_PROTECTED:
13516 case RID_PRIVATE:
13517 /* Consume the access-specifier. */
13518 cp_lexer_consume_token (parser->lexer);
13519 /* Remember which access-specifier is active. */
13520 current_access_specifier = token->value;
13521 /* Look for the `:'. */
13522 cp_parser_require (parser, CPP_COLON, "`:'");
13523 break;
13524
13525 default:
13526 /* Accept #pragmas at class scope. */
13527 if (token->type == CPP_PRAGMA)
13528 {
13529 cp_parser_pragma (parser, pragma_external);
13530 break;
13531 }
13532
13533 /* Otherwise, the next construction must be a
13534 member-declaration. */
13535 cp_parser_member_declaration (parser);
13536 }
13537 }
13538 }
13539
13540 /* Parse a member-declaration.
13541
13542 member-declaration:
13543 decl-specifier-seq [opt] member-declarator-list [opt] ;
13544 function-definition ; [opt]
13545 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13546 using-declaration
13547 template-declaration
13548
13549 member-declarator-list:
13550 member-declarator
13551 member-declarator-list , member-declarator
13552
13553 member-declarator:
13554 declarator pure-specifier [opt]
13555 declarator constant-initializer [opt]
13556 identifier [opt] : constant-expression
13557
13558 GNU Extensions:
13559
13560 member-declaration:
13561 __extension__ member-declaration
13562
13563 member-declarator:
13564 declarator attributes [opt] pure-specifier [opt]
13565 declarator attributes [opt] constant-initializer [opt]
13566 identifier [opt] attributes [opt] : constant-expression */
13567
13568 static void
13569 cp_parser_member_declaration (cp_parser* parser)
13570 {
13571 cp_decl_specifier_seq decl_specifiers;
13572 tree prefix_attributes;
13573 tree decl;
13574 int declares_class_or_enum;
13575 bool friend_p;
13576 cp_token *token;
13577 int saved_pedantic;
13578
13579 /* Check for the `__extension__' keyword. */
13580 if (cp_parser_extension_opt (parser, &saved_pedantic))
13581 {
13582 /* Recurse. */
13583 cp_parser_member_declaration (parser);
13584 /* Restore the old value of the PEDANTIC flag. */
13585 pedantic = saved_pedantic;
13586
13587 return;
13588 }
13589
13590 /* Check for a template-declaration. */
13591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13592 {
13593 /* An explicit specialization here is an error condition, and we
13594 expect the specialization handler to detect and report this. */
13595 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13596 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13597 cp_parser_explicit_specialization (parser);
13598 else
13599 cp_parser_template_declaration (parser, /*member_p=*/true);
13600
13601 return;
13602 }
13603
13604 /* Check for a using-declaration. */
13605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13606 {
13607 /* Parse the using-declaration. */
13608 cp_parser_using_declaration (parser,
13609 /*access_declaration_p=*/false);
13610 return;
13611 }
13612
13613 /* Check for @defs. */
13614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13615 {
13616 tree ivar, member;
13617 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13618 ivar = ivar_chains;
13619 while (ivar)
13620 {
13621 member = ivar;
13622 ivar = TREE_CHAIN (member);
13623 TREE_CHAIN (member) = NULL_TREE;
13624 finish_member_declaration (member);
13625 }
13626 return;
13627 }
13628
13629 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13630 return;
13631
13632 /* Parse the decl-specifier-seq. */
13633 cp_parser_decl_specifier_seq (parser,
13634 CP_PARSER_FLAGS_OPTIONAL,
13635 &decl_specifiers,
13636 &declares_class_or_enum);
13637 prefix_attributes = decl_specifiers.attributes;
13638 decl_specifiers.attributes = NULL_TREE;
13639 /* Check for an invalid type-name. */
13640 if (!decl_specifiers.type
13641 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13642 return;
13643 /* If there is no declarator, then the decl-specifier-seq should
13644 specify a type. */
13645 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13646 {
13647 /* If there was no decl-specifier-seq, and the next token is a
13648 `;', then we have something like:
13649
13650 struct S { ; };
13651
13652 [class.mem]
13653
13654 Each member-declaration shall declare at least one member
13655 name of the class. */
13656 if (!decl_specifiers.any_specifiers_p)
13657 {
13658 cp_token *token = cp_lexer_peek_token (parser->lexer);
13659 if (pedantic && !token->in_system_header)
13660 pedwarn ("%Hextra %<;%>", &token->location);
13661 }
13662 else
13663 {
13664 tree type;
13665
13666 /* See if this declaration is a friend. */
13667 friend_p = cp_parser_friend_p (&decl_specifiers);
13668 /* If there were decl-specifiers, check to see if there was
13669 a class-declaration. */
13670 type = check_tag_decl (&decl_specifiers);
13671 /* Nested classes have already been added to the class, but
13672 a `friend' needs to be explicitly registered. */
13673 if (friend_p)
13674 {
13675 /* If the `friend' keyword was present, the friend must
13676 be introduced with a class-key. */
13677 if (!declares_class_or_enum)
13678 error ("a class-key must be used when declaring a friend");
13679 /* In this case:
13680
13681 template <typename T> struct A {
13682 friend struct A<T>::B;
13683 };
13684
13685 A<T>::B will be represented by a TYPENAME_TYPE, and
13686 therefore not recognized by check_tag_decl. */
13687 if (!type
13688 && decl_specifiers.type
13689 && TYPE_P (decl_specifiers.type))
13690 type = decl_specifiers.type;
13691 if (!type || !TYPE_P (type))
13692 error ("friend declaration does not name a class or "
13693 "function");
13694 else
13695 make_friend_class (current_class_type, type,
13696 /*complain=*/true);
13697 }
13698 /* If there is no TYPE, an error message will already have
13699 been issued. */
13700 else if (!type || type == error_mark_node)
13701 ;
13702 /* An anonymous aggregate has to be handled specially; such
13703 a declaration really declares a data member (with a
13704 particular type), as opposed to a nested class. */
13705 else if (ANON_AGGR_TYPE_P (type))
13706 {
13707 /* Remove constructors and such from TYPE, now that we
13708 know it is an anonymous aggregate. */
13709 fixup_anonymous_aggr (type);
13710 /* And make the corresponding data member. */
13711 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13712 /* Add it to the class. */
13713 finish_member_declaration (decl);
13714 }
13715 else
13716 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13717 }
13718 }
13719 else
13720 {
13721 /* See if these declarations will be friends. */
13722 friend_p = cp_parser_friend_p (&decl_specifiers);
13723
13724 /* Keep going until we hit the `;' at the end of the
13725 declaration. */
13726 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13727 {
13728 tree attributes = NULL_TREE;
13729 tree first_attribute;
13730
13731 /* Peek at the next token. */
13732 token = cp_lexer_peek_token (parser->lexer);
13733
13734 /* Check for a bitfield declaration. */
13735 if (token->type == CPP_COLON
13736 || (token->type == CPP_NAME
13737 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13738 == CPP_COLON))
13739 {
13740 tree identifier;
13741 tree width;
13742
13743 /* Get the name of the bitfield. Note that we cannot just
13744 check TOKEN here because it may have been invalidated by
13745 the call to cp_lexer_peek_nth_token above. */
13746 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13747 identifier = cp_parser_identifier (parser);
13748 else
13749 identifier = NULL_TREE;
13750
13751 /* Consume the `:' token. */
13752 cp_lexer_consume_token (parser->lexer);
13753 /* Get the width of the bitfield. */
13754 width
13755 = cp_parser_constant_expression (parser,
13756 /*allow_non_constant=*/false,
13757 NULL);
13758
13759 /* Look for attributes that apply to the bitfield. */
13760 attributes = cp_parser_attributes_opt (parser);
13761 /* Remember which attributes are prefix attributes and
13762 which are not. */
13763 first_attribute = attributes;
13764 /* Combine the attributes. */
13765 attributes = chainon (prefix_attributes, attributes);
13766
13767 /* Create the bitfield declaration. */
13768 decl = grokbitfield (identifier
13769 ? make_id_declarator (NULL_TREE,
13770 identifier,
13771 sfk_none)
13772 : NULL,
13773 &decl_specifiers,
13774 width);
13775 /* Apply the attributes. */
13776 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13777 }
13778 else
13779 {
13780 cp_declarator *declarator;
13781 tree initializer;
13782 tree asm_specification;
13783 int ctor_dtor_or_conv_p;
13784
13785 /* Parse the declarator. */
13786 declarator
13787 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13788 &ctor_dtor_or_conv_p,
13789 /*parenthesized_p=*/NULL,
13790 /*member_p=*/true);
13791
13792 /* If something went wrong parsing the declarator, make sure
13793 that we at least consume some tokens. */
13794 if (declarator == cp_error_declarator)
13795 {
13796 /* Skip to the end of the statement. */
13797 cp_parser_skip_to_end_of_statement (parser);
13798 /* If the next token is not a semicolon, that is
13799 probably because we just skipped over the body of
13800 a function. So, we consume a semicolon if
13801 present, but do not issue an error message if it
13802 is not present. */
13803 if (cp_lexer_next_token_is (parser->lexer,
13804 CPP_SEMICOLON))
13805 cp_lexer_consume_token (parser->lexer);
13806 return;
13807 }
13808
13809 if (declares_class_or_enum & 2)
13810 cp_parser_check_for_definition_in_return_type
13811 (declarator, decl_specifiers.type);
13812
13813 /* Look for an asm-specification. */
13814 asm_specification = cp_parser_asm_specification_opt (parser);
13815 /* Look for attributes that apply to the declaration. */
13816 attributes = cp_parser_attributes_opt (parser);
13817 /* Remember which attributes are prefix attributes and
13818 which are not. */
13819 first_attribute = attributes;
13820 /* Combine the attributes. */
13821 attributes = chainon (prefix_attributes, attributes);
13822
13823 /* If it's an `=', then we have a constant-initializer or a
13824 pure-specifier. It is not correct to parse the
13825 initializer before registering the member declaration
13826 since the member declaration should be in scope while
13827 its initializer is processed. However, the rest of the
13828 front end does not yet provide an interface that allows
13829 us to handle this correctly. */
13830 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13831 {
13832 /* In [class.mem]:
13833
13834 A pure-specifier shall be used only in the declaration of
13835 a virtual function.
13836
13837 A member-declarator can contain a constant-initializer
13838 only if it declares a static member of integral or
13839 enumeration type.
13840
13841 Therefore, if the DECLARATOR is for a function, we look
13842 for a pure-specifier; otherwise, we look for a
13843 constant-initializer. When we call `grokfield', it will
13844 perform more stringent semantics checks. */
13845 if (function_declarator_p (declarator))
13846 initializer = cp_parser_pure_specifier (parser);
13847 else
13848 /* Parse the initializer. */
13849 initializer = cp_parser_constant_initializer (parser);
13850 }
13851 /* Otherwise, there is no initializer. */
13852 else
13853 initializer = NULL_TREE;
13854
13855 /* See if we are probably looking at a function
13856 definition. We are certainly not looking at a
13857 member-declarator. Calling `grokfield' has
13858 side-effects, so we must not do it unless we are sure
13859 that we are looking at a member-declarator. */
13860 if (cp_parser_token_starts_function_definition_p
13861 (cp_lexer_peek_token (parser->lexer)))
13862 {
13863 /* The grammar does not allow a pure-specifier to be
13864 used when a member function is defined. (It is
13865 possible that this fact is an oversight in the
13866 standard, since a pure function may be defined
13867 outside of the class-specifier. */
13868 if (initializer)
13869 error ("pure-specifier on function-definition");
13870 decl = cp_parser_save_member_function_body (parser,
13871 &decl_specifiers,
13872 declarator,
13873 attributes);
13874 /* If the member was not a friend, declare it here. */
13875 if (!friend_p)
13876 finish_member_declaration (decl);
13877 /* Peek at the next token. */
13878 token = cp_lexer_peek_token (parser->lexer);
13879 /* If the next token is a semicolon, consume it. */
13880 if (token->type == CPP_SEMICOLON)
13881 cp_lexer_consume_token (parser->lexer);
13882 return;
13883 }
13884 else
13885 /* Create the declaration. */
13886 decl = grokfield (declarator, &decl_specifiers,
13887 initializer, /*init_const_expr_p=*/true,
13888 asm_specification,
13889 attributes);
13890 }
13891
13892 /* Reset PREFIX_ATTRIBUTES. */
13893 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13894 attributes = TREE_CHAIN (attributes);
13895 if (attributes)
13896 TREE_CHAIN (attributes) = NULL_TREE;
13897
13898 /* If there is any qualification still in effect, clear it
13899 now; we will be starting fresh with the next declarator. */
13900 parser->scope = NULL_TREE;
13901 parser->qualifying_scope = NULL_TREE;
13902 parser->object_scope = NULL_TREE;
13903 /* If it's a `,', then there are more declarators. */
13904 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13905 cp_lexer_consume_token (parser->lexer);
13906 /* If the next token isn't a `;', then we have a parse error. */
13907 else if (cp_lexer_next_token_is_not (parser->lexer,
13908 CPP_SEMICOLON))
13909 {
13910 cp_parser_error (parser, "expected %<;%>");
13911 /* Skip tokens until we find a `;'. */
13912 cp_parser_skip_to_end_of_statement (parser);
13913
13914 break;
13915 }
13916
13917 if (decl)
13918 {
13919 /* Add DECL to the list of members. */
13920 if (!friend_p)
13921 finish_member_declaration (decl);
13922
13923 if (TREE_CODE (decl) == FUNCTION_DECL)
13924 cp_parser_save_default_args (parser, decl);
13925 }
13926 }
13927 }
13928
13929 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13930 }
13931
13932 /* Parse a pure-specifier.
13933
13934 pure-specifier:
13935 = 0
13936
13937 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13938 Otherwise, ERROR_MARK_NODE is returned. */
13939
13940 static tree
13941 cp_parser_pure_specifier (cp_parser* parser)
13942 {
13943 cp_token *token;
13944
13945 /* Look for the `=' token. */
13946 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13947 return error_mark_node;
13948 /* Look for the `0' token. */
13949 token = cp_lexer_consume_token (parser->lexer);
13950 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13951 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13952 {
13953 cp_parser_error (parser,
13954 "invalid pure specifier (only `= 0' is allowed)");
13955 cp_parser_skip_to_end_of_statement (parser);
13956 return error_mark_node;
13957 }
13958 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13959 {
13960 error ("templates may not be %<virtual%>");
13961 return error_mark_node;
13962 }
13963
13964 return integer_zero_node;
13965 }
13966
13967 /* Parse a constant-initializer.
13968
13969 constant-initializer:
13970 = constant-expression
13971
13972 Returns a representation of the constant-expression. */
13973
13974 static tree
13975 cp_parser_constant_initializer (cp_parser* parser)
13976 {
13977 /* Look for the `=' token. */
13978 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13979 return error_mark_node;
13980
13981 /* It is invalid to write:
13982
13983 struct S { static const int i = { 7 }; };
13984
13985 */
13986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13987 {
13988 cp_parser_error (parser,
13989 "a brace-enclosed initializer is not allowed here");
13990 /* Consume the opening brace. */
13991 cp_lexer_consume_token (parser->lexer);
13992 /* Skip the initializer. */
13993 cp_parser_skip_to_closing_brace (parser);
13994 /* Look for the trailing `}'. */
13995 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13996
13997 return error_mark_node;
13998 }
13999
14000 return cp_parser_constant_expression (parser,
14001 /*allow_non_constant=*/false,
14002 NULL);
14003 }
14004
14005 /* Derived classes [gram.class.derived] */
14006
14007 /* Parse a base-clause.
14008
14009 base-clause:
14010 : base-specifier-list
14011
14012 base-specifier-list:
14013 base-specifier
14014 base-specifier-list , base-specifier
14015
14016 Returns a TREE_LIST representing the base-classes, in the order in
14017 which they were declared. The representation of each node is as
14018 described by cp_parser_base_specifier.
14019
14020 In the case that no bases are specified, this function will return
14021 NULL_TREE, not ERROR_MARK_NODE. */
14022
14023 static tree
14024 cp_parser_base_clause (cp_parser* parser)
14025 {
14026 tree bases = NULL_TREE;
14027
14028 /* Look for the `:' that begins the list. */
14029 cp_parser_require (parser, CPP_COLON, "`:'");
14030
14031 /* Scan the base-specifier-list. */
14032 while (true)
14033 {
14034 cp_token *token;
14035 tree base;
14036
14037 /* Look for the base-specifier. */
14038 base = cp_parser_base_specifier (parser);
14039 /* Add BASE to the front of the list. */
14040 if (base != error_mark_node)
14041 {
14042 TREE_CHAIN (base) = bases;
14043 bases = base;
14044 }
14045 /* Peek at the next token. */
14046 token = cp_lexer_peek_token (parser->lexer);
14047 /* If it's not a comma, then the list is complete. */
14048 if (token->type != CPP_COMMA)
14049 break;
14050 /* Consume the `,'. */
14051 cp_lexer_consume_token (parser->lexer);
14052 }
14053
14054 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14055 base class had a qualified name. However, the next name that
14056 appears is certainly not qualified. */
14057 parser->scope = NULL_TREE;
14058 parser->qualifying_scope = NULL_TREE;
14059 parser->object_scope = NULL_TREE;
14060
14061 return nreverse (bases);
14062 }
14063
14064 /* Parse a base-specifier.
14065
14066 base-specifier:
14067 :: [opt] nested-name-specifier [opt] class-name
14068 virtual access-specifier [opt] :: [opt] nested-name-specifier
14069 [opt] class-name
14070 access-specifier virtual [opt] :: [opt] nested-name-specifier
14071 [opt] class-name
14072
14073 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14074 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14075 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14076 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14077
14078 static tree
14079 cp_parser_base_specifier (cp_parser* parser)
14080 {
14081 cp_token *token;
14082 bool done = false;
14083 bool virtual_p = false;
14084 bool duplicate_virtual_error_issued_p = false;
14085 bool duplicate_access_error_issued_p = false;
14086 bool class_scope_p, template_p;
14087 tree access = access_default_node;
14088 tree type;
14089
14090 /* Process the optional `virtual' and `access-specifier'. */
14091 while (!done)
14092 {
14093 /* Peek at the next token. */
14094 token = cp_lexer_peek_token (parser->lexer);
14095 /* Process `virtual'. */
14096 switch (token->keyword)
14097 {
14098 case RID_VIRTUAL:
14099 /* If `virtual' appears more than once, issue an error. */
14100 if (virtual_p && !duplicate_virtual_error_issued_p)
14101 {
14102 cp_parser_error (parser,
14103 "%<virtual%> specified more than once in base-specified");
14104 duplicate_virtual_error_issued_p = true;
14105 }
14106
14107 virtual_p = true;
14108
14109 /* Consume the `virtual' token. */
14110 cp_lexer_consume_token (parser->lexer);
14111
14112 break;
14113
14114 case RID_PUBLIC:
14115 case RID_PROTECTED:
14116 case RID_PRIVATE:
14117 /* If more than one access specifier appears, issue an
14118 error. */
14119 if (access != access_default_node
14120 && !duplicate_access_error_issued_p)
14121 {
14122 cp_parser_error (parser,
14123 "more than one access specifier in base-specified");
14124 duplicate_access_error_issued_p = true;
14125 }
14126
14127 access = ridpointers[(int) token->keyword];
14128
14129 /* Consume the access-specifier. */
14130 cp_lexer_consume_token (parser->lexer);
14131
14132 break;
14133
14134 default:
14135 done = true;
14136 break;
14137 }
14138 }
14139 /* It is not uncommon to see programs mechanically, erroneously, use
14140 the 'typename' keyword to denote (dependent) qualified types
14141 as base classes. */
14142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14143 {
14144 if (!processing_template_decl)
14145 error ("keyword %<typename%> not allowed outside of templates");
14146 else
14147 error ("keyword %<typename%> not allowed in this context "
14148 "(the base class is implicitly a type)");
14149 cp_lexer_consume_token (parser->lexer);
14150 }
14151
14152 /* Look for the optional `::' operator. */
14153 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14154 /* Look for the nested-name-specifier. The simplest way to
14155 implement:
14156
14157 [temp.res]
14158
14159 The keyword `typename' is not permitted in a base-specifier or
14160 mem-initializer; in these contexts a qualified name that
14161 depends on a template-parameter is implicitly assumed to be a
14162 type name.
14163
14164 is to pretend that we have seen the `typename' keyword at this
14165 point. */
14166 cp_parser_nested_name_specifier_opt (parser,
14167 /*typename_keyword_p=*/true,
14168 /*check_dependency_p=*/true,
14169 typename_type,
14170 /*is_declaration=*/true);
14171 /* If the base class is given by a qualified name, assume that names
14172 we see are type names or templates, as appropriate. */
14173 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14174 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14175
14176 /* Finally, look for the class-name. */
14177 type = cp_parser_class_name (parser,
14178 class_scope_p,
14179 template_p,
14180 typename_type,
14181 /*check_dependency_p=*/true,
14182 /*class_head_p=*/false,
14183 /*is_declaration=*/true);
14184
14185 if (type == error_mark_node)
14186 return error_mark_node;
14187
14188 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14189 }
14190
14191 /* Exception handling [gram.exception] */
14192
14193 /* Parse an (optional) exception-specification.
14194
14195 exception-specification:
14196 throw ( type-id-list [opt] )
14197
14198 Returns a TREE_LIST representing the exception-specification. The
14199 TREE_VALUE of each node is a type. */
14200
14201 static tree
14202 cp_parser_exception_specification_opt (cp_parser* parser)
14203 {
14204 cp_token *token;
14205 tree type_id_list;
14206
14207 /* Peek at the next token. */
14208 token = cp_lexer_peek_token (parser->lexer);
14209 /* If it's not `throw', then there's no exception-specification. */
14210 if (!cp_parser_is_keyword (token, RID_THROW))
14211 return NULL_TREE;
14212
14213 /* Consume the `throw'. */
14214 cp_lexer_consume_token (parser->lexer);
14215
14216 /* Look for the `('. */
14217 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14218
14219 /* Peek at the next token. */
14220 token = cp_lexer_peek_token (parser->lexer);
14221 /* If it's not a `)', then there is a type-id-list. */
14222 if (token->type != CPP_CLOSE_PAREN)
14223 {
14224 const char *saved_message;
14225
14226 /* Types may not be defined in an exception-specification. */
14227 saved_message = parser->type_definition_forbidden_message;
14228 parser->type_definition_forbidden_message
14229 = "types may not be defined in an exception-specification";
14230 /* Parse the type-id-list. */
14231 type_id_list = cp_parser_type_id_list (parser);
14232 /* Restore the saved message. */
14233 parser->type_definition_forbidden_message = saved_message;
14234 }
14235 else
14236 type_id_list = empty_except_spec;
14237
14238 /* Look for the `)'. */
14239 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14240
14241 return type_id_list;
14242 }
14243
14244 /* Parse an (optional) type-id-list.
14245
14246 type-id-list:
14247 type-id
14248 type-id-list , type-id
14249
14250 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14251 in the order that the types were presented. */
14252
14253 static tree
14254 cp_parser_type_id_list (cp_parser* parser)
14255 {
14256 tree types = NULL_TREE;
14257
14258 while (true)
14259 {
14260 cp_token *token;
14261 tree type;
14262
14263 /* Get the next type-id. */
14264 type = cp_parser_type_id (parser);
14265 /* Add it to the list. */
14266 types = add_exception_specifier (types, type, /*complain=*/1);
14267 /* Peek at the next token. */
14268 token = cp_lexer_peek_token (parser->lexer);
14269 /* If it is not a `,', we are done. */
14270 if (token->type != CPP_COMMA)
14271 break;
14272 /* Consume the `,'. */
14273 cp_lexer_consume_token (parser->lexer);
14274 }
14275
14276 return nreverse (types);
14277 }
14278
14279 /* Parse a try-block.
14280
14281 try-block:
14282 try compound-statement handler-seq */
14283
14284 static tree
14285 cp_parser_try_block (cp_parser* parser)
14286 {
14287 tree try_block;
14288
14289 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14290 try_block = begin_try_block ();
14291 cp_parser_compound_statement (parser, NULL, true);
14292 finish_try_block (try_block);
14293 cp_parser_handler_seq (parser);
14294 finish_handler_sequence (try_block);
14295
14296 return try_block;
14297 }
14298
14299 /* Parse a function-try-block.
14300
14301 function-try-block:
14302 try ctor-initializer [opt] function-body handler-seq */
14303
14304 static bool
14305 cp_parser_function_try_block (cp_parser* parser)
14306 {
14307 tree compound_stmt;
14308 tree try_block;
14309 bool ctor_initializer_p;
14310
14311 /* Look for the `try' keyword. */
14312 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14313 return false;
14314 /* Let the rest of the front-end know where we are. */
14315 try_block = begin_function_try_block (&compound_stmt);
14316 /* Parse the function-body. */
14317 ctor_initializer_p
14318 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14319 /* We're done with the `try' part. */
14320 finish_function_try_block (try_block);
14321 /* Parse the handlers. */
14322 cp_parser_handler_seq (parser);
14323 /* We're done with the handlers. */
14324 finish_function_handler_sequence (try_block, compound_stmt);
14325
14326 return ctor_initializer_p;
14327 }
14328
14329 /* Parse a handler-seq.
14330
14331 handler-seq:
14332 handler handler-seq [opt] */
14333
14334 static void
14335 cp_parser_handler_seq (cp_parser* parser)
14336 {
14337 while (true)
14338 {
14339 cp_token *token;
14340
14341 /* Parse the handler. */
14342 cp_parser_handler (parser);
14343 /* Peek at the next token. */
14344 token = cp_lexer_peek_token (parser->lexer);
14345 /* If it's not `catch' then there are no more handlers. */
14346 if (!cp_parser_is_keyword (token, RID_CATCH))
14347 break;
14348 }
14349 }
14350
14351 /* Parse a handler.
14352
14353 handler:
14354 catch ( exception-declaration ) compound-statement */
14355
14356 static void
14357 cp_parser_handler (cp_parser* parser)
14358 {
14359 tree handler;
14360 tree declaration;
14361
14362 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14363 handler = begin_handler ();
14364 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14365 declaration = cp_parser_exception_declaration (parser);
14366 finish_handler_parms (declaration, handler);
14367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14368 cp_parser_compound_statement (parser, NULL, false);
14369 finish_handler (handler);
14370 }
14371
14372 /* Parse an exception-declaration.
14373
14374 exception-declaration:
14375 type-specifier-seq declarator
14376 type-specifier-seq abstract-declarator
14377 type-specifier-seq
14378 ...
14379
14380 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14381 ellipsis variant is used. */
14382
14383 static tree
14384 cp_parser_exception_declaration (cp_parser* parser)
14385 {
14386 cp_decl_specifier_seq type_specifiers;
14387 cp_declarator *declarator;
14388 const char *saved_message;
14389
14390 /* If it's an ellipsis, it's easy to handle. */
14391 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14392 {
14393 /* Consume the `...' token. */
14394 cp_lexer_consume_token (parser->lexer);
14395 return NULL_TREE;
14396 }
14397
14398 /* Types may not be defined in exception-declarations. */
14399 saved_message = parser->type_definition_forbidden_message;
14400 parser->type_definition_forbidden_message
14401 = "types may not be defined in exception-declarations";
14402
14403 /* Parse the type-specifier-seq. */
14404 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14405 &type_specifiers);
14406 /* If it's a `)', then there is no declarator. */
14407 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14408 declarator = NULL;
14409 else
14410 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14411 /*ctor_dtor_or_conv_p=*/NULL,
14412 /*parenthesized_p=*/NULL,
14413 /*member_p=*/false);
14414
14415 /* Restore the saved message. */
14416 parser->type_definition_forbidden_message = saved_message;
14417
14418 if (!type_specifiers.any_specifiers_p)
14419 return error_mark_node;
14420
14421 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14422 }
14423
14424 /* Parse a throw-expression.
14425
14426 throw-expression:
14427 throw assignment-expression [opt]
14428
14429 Returns a THROW_EXPR representing the throw-expression. */
14430
14431 static tree
14432 cp_parser_throw_expression (cp_parser* parser)
14433 {
14434 tree expression;
14435 cp_token* token;
14436
14437 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14438 token = cp_lexer_peek_token (parser->lexer);
14439 /* Figure out whether or not there is an assignment-expression
14440 following the "throw" keyword. */
14441 if (token->type == CPP_COMMA
14442 || token->type == CPP_SEMICOLON
14443 || token->type == CPP_CLOSE_PAREN
14444 || token->type == CPP_CLOSE_SQUARE
14445 || token->type == CPP_CLOSE_BRACE
14446 || token->type == CPP_COLON)
14447 expression = NULL_TREE;
14448 else
14449 expression = cp_parser_assignment_expression (parser,
14450 /*cast_p=*/false);
14451
14452 return build_throw (expression);
14453 }
14454
14455 /* GNU Extensions */
14456
14457 /* Parse an (optional) asm-specification.
14458
14459 asm-specification:
14460 asm ( string-literal )
14461
14462 If the asm-specification is present, returns a STRING_CST
14463 corresponding to the string-literal. Otherwise, returns
14464 NULL_TREE. */
14465
14466 static tree
14467 cp_parser_asm_specification_opt (cp_parser* parser)
14468 {
14469 cp_token *token;
14470 tree asm_specification;
14471
14472 /* Peek at the next token. */
14473 token = cp_lexer_peek_token (parser->lexer);
14474 /* If the next token isn't the `asm' keyword, then there's no
14475 asm-specification. */
14476 if (!cp_parser_is_keyword (token, RID_ASM))
14477 return NULL_TREE;
14478
14479 /* Consume the `asm' token. */
14480 cp_lexer_consume_token (parser->lexer);
14481 /* Look for the `('. */
14482 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14483
14484 /* Look for the string-literal. */
14485 asm_specification = cp_parser_string_literal (parser, false, false);
14486
14487 /* Look for the `)'. */
14488 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14489
14490 return asm_specification;
14491 }
14492
14493 /* Parse an asm-operand-list.
14494
14495 asm-operand-list:
14496 asm-operand
14497 asm-operand-list , asm-operand
14498
14499 asm-operand:
14500 string-literal ( expression )
14501 [ string-literal ] string-literal ( expression )
14502
14503 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14504 each node is the expression. The TREE_PURPOSE is itself a
14505 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14506 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14507 is a STRING_CST for the string literal before the parenthesis. */
14508
14509 static tree
14510 cp_parser_asm_operand_list (cp_parser* parser)
14511 {
14512 tree asm_operands = NULL_TREE;
14513
14514 while (true)
14515 {
14516 tree string_literal;
14517 tree expression;
14518 tree name;
14519
14520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14521 {
14522 /* Consume the `[' token. */
14523 cp_lexer_consume_token (parser->lexer);
14524 /* Read the operand name. */
14525 name = cp_parser_identifier (parser);
14526 if (name != error_mark_node)
14527 name = build_string (IDENTIFIER_LENGTH (name),
14528 IDENTIFIER_POINTER (name));
14529 /* Look for the closing `]'. */
14530 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14531 }
14532 else
14533 name = NULL_TREE;
14534 /* Look for the string-literal. */
14535 string_literal = cp_parser_string_literal (parser, false, false);
14536
14537 /* Look for the `('. */
14538 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14539 /* Parse the expression. */
14540 expression = cp_parser_expression (parser, /*cast_p=*/false);
14541 /* Look for the `)'. */
14542 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14543
14544 /* Add this operand to the list. */
14545 asm_operands = tree_cons (build_tree_list (name, string_literal),
14546 expression,
14547 asm_operands);
14548 /* If the next token is not a `,', there are no more
14549 operands. */
14550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14551 break;
14552 /* Consume the `,'. */
14553 cp_lexer_consume_token (parser->lexer);
14554 }
14555
14556 return nreverse (asm_operands);
14557 }
14558
14559 /* Parse an asm-clobber-list.
14560
14561 asm-clobber-list:
14562 string-literal
14563 asm-clobber-list , string-literal
14564
14565 Returns a TREE_LIST, indicating the clobbers in the order that they
14566 appeared. The TREE_VALUE of each node is a STRING_CST. */
14567
14568 static tree
14569 cp_parser_asm_clobber_list (cp_parser* parser)
14570 {
14571 tree clobbers = NULL_TREE;
14572
14573 while (true)
14574 {
14575 tree string_literal;
14576
14577 /* Look for the string literal. */
14578 string_literal = cp_parser_string_literal (parser, false, false);
14579 /* Add it to the list. */
14580 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14581 /* If the next token is not a `,', then the list is
14582 complete. */
14583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14584 break;
14585 /* Consume the `,' token. */
14586 cp_lexer_consume_token (parser->lexer);
14587 }
14588
14589 return clobbers;
14590 }
14591
14592 /* Parse an (optional) series of attributes.
14593
14594 attributes:
14595 attributes attribute
14596
14597 attribute:
14598 __attribute__ (( attribute-list [opt] ))
14599
14600 The return value is as for cp_parser_attribute_list. */
14601
14602 static tree
14603 cp_parser_attributes_opt (cp_parser* parser)
14604 {
14605 tree attributes = NULL_TREE;
14606
14607 while (true)
14608 {
14609 cp_token *token;
14610 tree attribute_list;
14611
14612 /* Peek at the next token. */
14613 token = cp_lexer_peek_token (parser->lexer);
14614 /* If it's not `__attribute__', then we're done. */
14615 if (token->keyword != RID_ATTRIBUTE)
14616 break;
14617
14618 /* Consume the `__attribute__' keyword. */
14619 cp_lexer_consume_token (parser->lexer);
14620 /* Look for the two `(' tokens. */
14621 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14622 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14623
14624 /* Peek at the next token. */
14625 token = cp_lexer_peek_token (parser->lexer);
14626 if (token->type != CPP_CLOSE_PAREN)
14627 /* Parse the attribute-list. */
14628 attribute_list = cp_parser_attribute_list (parser);
14629 else
14630 /* If the next token is a `)', then there is no attribute
14631 list. */
14632 attribute_list = NULL;
14633
14634 /* Look for the two `)' tokens. */
14635 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14636 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14637
14638 /* Add these new attributes to the list. */
14639 attributes = chainon (attributes, attribute_list);
14640 }
14641
14642 return attributes;
14643 }
14644
14645 /* Parse an attribute-list.
14646
14647 attribute-list:
14648 attribute
14649 attribute-list , attribute
14650
14651 attribute:
14652 identifier
14653 identifier ( identifier )
14654 identifier ( identifier , expression-list )
14655 identifier ( expression-list )
14656
14657 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14658 to an attribute. The TREE_PURPOSE of each node is the identifier
14659 indicating which attribute is in use. The TREE_VALUE represents
14660 the arguments, if any. */
14661
14662 static tree
14663 cp_parser_attribute_list (cp_parser* parser)
14664 {
14665 tree attribute_list = NULL_TREE;
14666 bool save_translate_strings_p = parser->translate_strings_p;
14667
14668 parser->translate_strings_p = false;
14669 while (true)
14670 {
14671 cp_token *token;
14672 tree identifier;
14673 tree attribute;
14674
14675 /* Look for the identifier. We also allow keywords here; for
14676 example `__attribute__ ((const))' is legal. */
14677 token = cp_lexer_peek_token (parser->lexer);
14678 if (token->type == CPP_NAME
14679 || token->type == CPP_KEYWORD)
14680 {
14681 tree arguments = NULL_TREE;
14682
14683 /* Consume the token. */
14684 token = cp_lexer_consume_token (parser->lexer);
14685
14686 /* Save away the identifier that indicates which attribute
14687 this is. */
14688 identifier = token->value;
14689 attribute = build_tree_list (identifier, NULL_TREE);
14690
14691 /* Peek at the next token. */
14692 token = cp_lexer_peek_token (parser->lexer);
14693 /* If it's an `(', then parse the attribute arguments. */
14694 if (token->type == CPP_OPEN_PAREN)
14695 {
14696 arguments = cp_parser_parenthesized_expression_list
14697 (parser, true, /*cast_p=*/false,
14698 /*non_constant_p=*/NULL);
14699 /* Save the arguments away. */
14700 TREE_VALUE (attribute) = arguments;
14701 }
14702
14703 if (arguments != error_mark_node)
14704 {
14705 /* Add this attribute to the list. */
14706 TREE_CHAIN (attribute) = attribute_list;
14707 attribute_list = attribute;
14708 }
14709
14710 token = cp_lexer_peek_token (parser->lexer);
14711 }
14712 /* Now, look for more attributes. If the next token isn't a
14713 `,', we're done. */
14714 if (token->type != CPP_COMMA)
14715 break;
14716
14717 /* Consume the comma and keep going. */
14718 cp_lexer_consume_token (parser->lexer);
14719 }
14720 parser->translate_strings_p = save_translate_strings_p;
14721
14722 /* We built up the list in reverse order. */
14723 return nreverse (attribute_list);
14724 }
14725
14726 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14727 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14728 current value of the PEDANTIC flag, regardless of whether or not
14729 the `__extension__' keyword is present. The caller is responsible
14730 for restoring the value of the PEDANTIC flag. */
14731
14732 static bool
14733 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14734 {
14735 /* Save the old value of the PEDANTIC flag. */
14736 *saved_pedantic = pedantic;
14737
14738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14739 {
14740 /* Consume the `__extension__' token. */
14741 cp_lexer_consume_token (parser->lexer);
14742 /* We're not being pedantic while the `__extension__' keyword is
14743 in effect. */
14744 pedantic = 0;
14745
14746 return true;
14747 }
14748
14749 return false;
14750 }
14751
14752 /* Parse a label declaration.
14753
14754 label-declaration:
14755 __label__ label-declarator-seq ;
14756
14757 label-declarator-seq:
14758 identifier , label-declarator-seq
14759 identifier */
14760
14761 static void
14762 cp_parser_label_declaration (cp_parser* parser)
14763 {
14764 /* Look for the `__label__' keyword. */
14765 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14766
14767 while (true)
14768 {
14769 tree identifier;
14770
14771 /* Look for an identifier. */
14772 identifier = cp_parser_identifier (parser);
14773 /* If we failed, stop. */
14774 if (identifier == error_mark_node)
14775 break;
14776 /* Declare it as a label. */
14777 finish_label_decl (identifier);
14778 /* If the next token is a `;', stop. */
14779 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14780 break;
14781 /* Look for the `,' separating the label declarations. */
14782 cp_parser_require (parser, CPP_COMMA, "`,'");
14783 }
14784
14785 /* Look for the final `;'. */
14786 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14787 }
14788
14789 /* Support Functions */
14790
14791 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14792 NAME should have one of the representations used for an
14793 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14794 is returned. If PARSER->SCOPE is a dependent type, then a
14795 SCOPE_REF is returned.
14796
14797 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14798 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14799 was formed. Abstractly, such entities should not be passed to this
14800 function, because they do not need to be looked up, but it is
14801 simpler to check for this special case here, rather than at the
14802 call-sites.
14803
14804 In cases not explicitly covered above, this function returns a
14805 DECL, OVERLOAD, or baselink representing the result of the lookup.
14806 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14807 is returned.
14808
14809 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14810 (e.g., "struct") that was used. In that case bindings that do not
14811 refer to types are ignored.
14812
14813 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14814 ignored.
14815
14816 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14817 are ignored.
14818
14819 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14820 types.
14821
14822 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14823 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14824 NULL_TREE otherwise. */
14825
14826 static tree
14827 cp_parser_lookup_name (cp_parser *parser, tree name,
14828 enum tag_types tag_type,
14829 bool is_template,
14830 bool is_namespace,
14831 bool check_dependency,
14832 tree *ambiguous_decls)
14833 {
14834 int flags = 0;
14835 tree decl;
14836 tree object_type = parser->context->object_type;
14837
14838 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14839 flags |= LOOKUP_COMPLAIN;
14840
14841 /* Assume that the lookup will be unambiguous. */
14842 if (ambiguous_decls)
14843 *ambiguous_decls = NULL_TREE;
14844
14845 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14846 no longer valid. Note that if we are parsing tentatively, and
14847 the parse fails, OBJECT_TYPE will be automatically restored. */
14848 parser->context->object_type = NULL_TREE;
14849
14850 if (name == error_mark_node)
14851 return error_mark_node;
14852
14853 /* A template-id has already been resolved; there is no lookup to
14854 do. */
14855 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14856 return name;
14857 if (BASELINK_P (name))
14858 {
14859 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14860 == TEMPLATE_ID_EXPR);
14861 return name;
14862 }
14863
14864 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14865 it should already have been checked to make sure that the name
14866 used matches the type being destroyed. */
14867 if (TREE_CODE (name) == BIT_NOT_EXPR)
14868 {
14869 tree type;
14870
14871 /* Figure out to which type this destructor applies. */
14872 if (parser->scope)
14873 type = parser->scope;
14874 else if (object_type)
14875 type = object_type;
14876 else
14877 type = current_class_type;
14878 /* If that's not a class type, there is no destructor. */
14879 if (!type || !CLASS_TYPE_P (type))
14880 return error_mark_node;
14881 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14882 lazily_declare_fn (sfk_destructor, type);
14883 if (!CLASSTYPE_DESTRUCTORS (type))
14884 return error_mark_node;
14885 /* If it was a class type, return the destructor. */
14886 return CLASSTYPE_DESTRUCTORS (type);
14887 }
14888
14889 /* By this point, the NAME should be an ordinary identifier. If
14890 the id-expression was a qualified name, the qualifying scope is
14891 stored in PARSER->SCOPE at this point. */
14892 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14893
14894 /* Perform the lookup. */
14895 if (parser->scope)
14896 {
14897 bool dependent_p;
14898
14899 if (parser->scope == error_mark_node)
14900 return error_mark_node;
14901
14902 /* If the SCOPE is dependent, the lookup must be deferred until
14903 the template is instantiated -- unless we are explicitly
14904 looking up names in uninstantiated templates. Even then, we
14905 cannot look up the name if the scope is not a class type; it
14906 might, for example, be a template type parameter. */
14907 dependent_p = (TYPE_P (parser->scope)
14908 && !(parser->in_declarator_p
14909 && currently_open_class (parser->scope))
14910 && dependent_type_p (parser->scope));
14911 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14912 && dependent_p)
14913 {
14914 if (tag_type)
14915 {
14916 tree type;
14917
14918 /* The resolution to Core Issue 180 says that `struct
14919 A::B' should be considered a type-name, even if `A'
14920 is dependent. */
14921 type = make_typename_type (parser->scope, name, tag_type,
14922 /*complain=*/tf_error);
14923 decl = TYPE_NAME (type);
14924 }
14925 else if (is_template
14926 && (cp_parser_next_token_ends_template_argument_p (parser)
14927 || cp_lexer_next_token_is (parser->lexer,
14928 CPP_CLOSE_PAREN)))
14929 decl = make_unbound_class_template (parser->scope,
14930 name, NULL_TREE,
14931 /*complain=*/tf_error);
14932 else
14933 decl = build_qualified_name (/*type=*/NULL_TREE,
14934 parser->scope, name,
14935 is_template);
14936 }
14937 else
14938 {
14939 tree pushed_scope = NULL_TREE;
14940
14941 /* If PARSER->SCOPE is a dependent type, then it must be a
14942 class type, and we must not be checking dependencies;
14943 otherwise, we would have processed this lookup above. So
14944 that PARSER->SCOPE is not considered a dependent base by
14945 lookup_member, we must enter the scope here. */
14946 if (dependent_p)
14947 pushed_scope = push_scope (parser->scope);
14948 /* If the PARSER->SCOPE is a template specialization, it
14949 may be instantiated during name lookup. In that case,
14950 errors may be issued. Even if we rollback the current
14951 tentative parse, those errors are valid. */
14952 decl = lookup_qualified_name (parser->scope, name,
14953 tag_type != none_type,
14954 /*complain=*/true);
14955 if (pushed_scope)
14956 pop_scope (pushed_scope);
14957 }
14958 parser->qualifying_scope = parser->scope;
14959 parser->object_scope = NULL_TREE;
14960 }
14961 else if (object_type)
14962 {
14963 tree object_decl = NULL_TREE;
14964 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14965 OBJECT_TYPE is not a class. */
14966 if (CLASS_TYPE_P (object_type))
14967 /* If the OBJECT_TYPE is a template specialization, it may
14968 be instantiated during name lookup. In that case, errors
14969 may be issued. Even if we rollback the current tentative
14970 parse, those errors are valid. */
14971 object_decl = lookup_member (object_type,
14972 name,
14973 /*protect=*/0,
14974 tag_type != none_type);
14975 /* Look it up in the enclosing context, too. */
14976 decl = lookup_name_real (name, tag_type != none_type,
14977 /*nonclass=*/0,
14978 /*block_p=*/true, is_namespace, flags);
14979 parser->object_scope = object_type;
14980 parser->qualifying_scope = NULL_TREE;
14981 if (object_decl)
14982 decl = object_decl;
14983 }
14984 else
14985 {
14986 decl = lookup_name_real (name, tag_type != none_type,
14987 /*nonclass=*/0,
14988 /*block_p=*/true, is_namespace, flags);
14989 parser->qualifying_scope = NULL_TREE;
14990 parser->object_scope = NULL_TREE;
14991 }
14992
14993 /* If the lookup failed, let our caller know. */
14994 if (!decl || decl == error_mark_node)
14995 return error_mark_node;
14996
14997 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14998 if (TREE_CODE (decl) == TREE_LIST)
14999 {
15000 if (ambiguous_decls)
15001 *ambiguous_decls = decl;
15002 /* The error message we have to print is too complicated for
15003 cp_parser_error, so we incorporate its actions directly. */
15004 if (!cp_parser_simulate_error (parser))
15005 {
15006 error ("reference to %qD is ambiguous", name);
15007 print_candidates (decl);
15008 }
15009 return error_mark_node;
15010 }
15011
15012 gcc_assert (DECL_P (decl)
15013 || TREE_CODE (decl) == OVERLOAD
15014 || TREE_CODE (decl) == SCOPE_REF
15015 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15016 || BASELINK_P (decl));
15017
15018 /* If we have resolved the name of a member declaration, check to
15019 see if the declaration is accessible. When the name resolves to
15020 set of overloaded functions, accessibility is checked when
15021 overload resolution is done.
15022
15023 During an explicit instantiation, access is not checked at all,
15024 as per [temp.explicit]. */
15025 if (DECL_P (decl))
15026 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15027
15028 return decl;
15029 }
15030
15031 /* Like cp_parser_lookup_name, but for use in the typical case where
15032 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15033 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15034
15035 static tree
15036 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15037 {
15038 return cp_parser_lookup_name (parser, name,
15039 none_type,
15040 /*is_template=*/false,
15041 /*is_namespace=*/false,
15042 /*check_dependency=*/true,
15043 /*ambiguous_decls=*/NULL);
15044 }
15045
15046 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15047 the current context, return the TYPE_DECL. If TAG_NAME_P is
15048 true, the DECL indicates the class being defined in a class-head,
15049 or declared in an elaborated-type-specifier.
15050
15051 Otherwise, return DECL. */
15052
15053 static tree
15054 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15055 {
15056 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15057 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15058
15059 struct A {
15060 template <typename T> struct B;
15061 };
15062
15063 template <typename T> struct A::B {};
15064
15065 Similarly, in an elaborated-type-specifier:
15066
15067 namespace N { struct X{}; }
15068
15069 struct A {
15070 template <typename T> friend struct N::X;
15071 };
15072
15073 However, if the DECL refers to a class type, and we are in
15074 the scope of the class, then the name lookup automatically
15075 finds the TYPE_DECL created by build_self_reference rather
15076 than a TEMPLATE_DECL. For example, in:
15077
15078 template <class T> struct S {
15079 S s;
15080 };
15081
15082 there is no need to handle such case. */
15083
15084 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15085 return DECL_TEMPLATE_RESULT (decl);
15086
15087 return decl;
15088 }
15089
15090 /* If too many, or too few, template-parameter lists apply to the
15091 declarator, issue an error message. Returns TRUE if all went well,
15092 and FALSE otherwise. */
15093
15094 static bool
15095 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15096 cp_declarator *declarator)
15097 {
15098 unsigned num_templates;
15099
15100 /* We haven't seen any classes that involve template parameters yet. */
15101 num_templates = 0;
15102
15103 switch (declarator->kind)
15104 {
15105 case cdk_id:
15106 if (declarator->u.id.qualifying_scope)
15107 {
15108 tree scope;
15109 tree member;
15110
15111 scope = declarator->u.id.qualifying_scope;
15112 member = declarator->u.id.unqualified_name;
15113
15114 while (scope && CLASS_TYPE_P (scope))
15115 {
15116 /* You're supposed to have one `template <...>'
15117 for every template class, but you don't need one
15118 for a full specialization. For example:
15119
15120 template <class T> struct S{};
15121 template <> struct S<int> { void f(); };
15122 void S<int>::f () {}
15123
15124 is correct; there shouldn't be a `template <>' for
15125 the definition of `S<int>::f'. */
15126 if (CLASSTYPE_TEMPLATE_INFO (scope)
15127 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15128 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15129 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15130 ++num_templates;
15131
15132 scope = TYPE_CONTEXT (scope);
15133 }
15134 }
15135 else if (TREE_CODE (declarator->u.id.unqualified_name)
15136 == TEMPLATE_ID_EXPR)
15137 /* If the DECLARATOR has the form `X<y>' then it uses one
15138 additional level of template parameters. */
15139 ++num_templates;
15140
15141 return cp_parser_check_template_parameters (parser,
15142 num_templates);
15143
15144 case cdk_function:
15145 case cdk_array:
15146 case cdk_pointer:
15147 case cdk_reference:
15148 case cdk_ptrmem:
15149 return (cp_parser_check_declarator_template_parameters
15150 (parser, declarator->declarator));
15151
15152 case cdk_error:
15153 return true;
15154
15155 default:
15156 gcc_unreachable ();
15157 }
15158 return false;
15159 }
15160
15161 /* NUM_TEMPLATES were used in the current declaration. If that is
15162 invalid, return FALSE and issue an error messages. Otherwise,
15163 return TRUE. */
15164
15165 static bool
15166 cp_parser_check_template_parameters (cp_parser* parser,
15167 unsigned num_templates)
15168 {
15169 /* If there are more template classes than parameter lists, we have
15170 something like:
15171
15172 template <class T> void S<T>::R<T>::f (); */
15173 if (parser->num_template_parameter_lists < num_templates)
15174 {
15175 error ("too few template-parameter-lists");
15176 return false;
15177 }
15178 /* If there are the same number of template classes and parameter
15179 lists, that's OK. */
15180 if (parser->num_template_parameter_lists == num_templates)
15181 return true;
15182 /* If there are more, but only one more, then we are referring to a
15183 member template. That's OK too. */
15184 if (parser->num_template_parameter_lists == num_templates + 1)
15185 return true;
15186 /* Otherwise, there are too many template parameter lists. We have
15187 something like:
15188
15189 template <class T> template <class U> void S::f(); */
15190 error ("too many template-parameter-lists");
15191 return false;
15192 }
15193
15194 /* Parse an optional `::' token indicating that the following name is
15195 from the global namespace. If so, PARSER->SCOPE is set to the
15196 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15197 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15198 Returns the new value of PARSER->SCOPE, if the `::' token is
15199 present, and NULL_TREE otherwise. */
15200
15201 static tree
15202 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15203 {
15204 cp_token *token;
15205
15206 /* Peek at the next token. */
15207 token = cp_lexer_peek_token (parser->lexer);
15208 /* If we're looking at a `::' token then we're starting from the
15209 global namespace, not our current location. */
15210 if (token->type == CPP_SCOPE)
15211 {
15212 /* Consume the `::' token. */
15213 cp_lexer_consume_token (parser->lexer);
15214 /* Set the SCOPE so that we know where to start the lookup. */
15215 parser->scope = global_namespace;
15216 parser->qualifying_scope = global_namespace;
15217 parser->object_scope = NULL_TREE;
15218
15219 return parser->scope;
15220 }
15221 else if (!current_scope_valid_p)
15222 {
15223 parser->scope = NULL_TREE;
15224 parser->qualifying_scope = NULL_TREE;
15225 parser->object_scope = NULL_TREE;
15226 }
15227
15228 return NULL_TREE;
15229 }
15230
15231 /* Returns TRUE if the upcoming token sequence is the start of a
15232 constructor declarator. If FRIEND_P is true, the declarator is
15233 preceded by the `friend' specifier. */
15234
15235 static bool
15236 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15237 {
15238 bool constructor_p;
15239 tree type_decl = NULL_TREE;
15240 bool nested_name_p;
15241 cp_token *next_token;
15242
15243 /* The common case is that this is not a constructor declarator, so
15244 try to avoid doing lots of work if at all possible. It's not
15245 valid declare a constructor at function scope. */
15246 if (at_function_scope_p ())
15247 return false;
15248 /* And only certain tokens can begin a constructor declarator. */
15249 next_token = cp_lexer_peek_token (parser->lexer);
15250 if (next_token->type != CPP_NAME
15251 && next_token->type != CPP_SCOPE
15252 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15253 && next_token->type != CPP_TEMPLATE_ID)
15254 return false;
15255
15256 /* Parse tentatively; we are going to roll back all of the tokens
15257 consumed here. */
15258 cp_parser_parse_tentatively (parser);
15259 /* Assume that we are looking at a constructor declarator. */
15260 constructor_p = true;
15261
15262 /* Look for the optional `::' operator. */
15263 cp_parser_global_scope_opt (parser,
15264 /*current_scope_valid_p=*/false);
15265 /* Look for the nested-name-specifier. */
15266 nested_name_p
15267 = (cp_parser_nested_name_specifier_opt (parser,
15268 /*typename_keyword_p=*/false,
15269 /*check_dependency_p=*/false,
15270 /*type_p=*/false,
15271 /*is_declaration=*/false)
15272 != NULL_TREE);
15273 /* Outside of a class-specifier, there must be a
15274 nested-name-specifier. */
15275 if (!nested_name_p &&
15276 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15277 || friend_p))
15278 constructor_p = false;
15279 /* If we still think that this might be a constructor-declarator,
15280 look for a class-name. */
15281 if (constructor_p)
15282 {
15283 /* If we have:
15284
15285 template <typename T> struct S { S(); };
15286 template <typename T> S<T>::S ();
15287
15288 we must recognize that the nested `S' names a class.
15289 Similarly, for:
15290
15291 template <typename T> S<T>::S<T> ();
15292
15293 we must recognize that the nested `S' names a template. */
15294 type_decl = cp_parser_class_name (parser,
15295 /*typename_keyword_p=*/false,
15296 /*template_keyword_p=*/false,
15297 none_type,
15298 /*check_dependency_p=*/false,
15299 /*class_head_p=*/false,
15300 /*is_declaration=*/false);
15301 /* If there was no class-name, then this is not a constructor. */
15302 constructor_p = !cp_parser_error_occurred (parser);
15303 }
15304
15305 /* If we're still considering a constructor, we have to see a `(',
15306 to begin the parameter-declaration-clause, followed by either a
15307 `)', an `...', or a decl-specifier. We need to check for a
15308 type-specifier to avoid being fooled into thinking that:
15309
15310 S::S (f) (int);
15311
15312 is a constructor. (It is actually a function named `f' that
15313 takes one parameter (of type `int') and returns a value of type
15314 `S::S'. */
15315 if (constructor_p
15316 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15317 {
15318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15319 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15320 /* A parameter declaration begins with a decl-specifier,
15321 which is either the "attribute" keyword, a storage class
15322 specifier, or (usually) a type-specifier. */
15323 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15324 && !cp_parser_storage_class_specifier_opt (parser))
15325 {
15326 tree type;
15327 tree pushed_scope = NULL_TREE;
15328 unsigned saved_num_template_parameter_lists;
15329
15330 /* Names appearing in the type-specifier should be looked up
15331 in the scope of the class. */
15332 if (current_class_type)
15333 type = NULL_TREE;
15334 else
15335 {
15336 type = TREE_TYPE (type_decl);
15337 if (TREE_CODE (type) == TYPENAME_TYPE)
15338 {
15339 type = resolve_typename_type (type,
15340 /*only_current_p=*/false);
15341 if (type == error_mark_node)
15342 {
15343 cp_parser_abort_tentative_parse (parser);
15344 return false;
15345 }
15346 }
15347 pushed_scope = push_scope (type);
15348 }
15349
15350 /* Inside the constructor parameter list, surrounding
15351 template-parameter-lists do not apply. */
15352 saved_num_template_parameter_lists
15353 = parser->num_template_parameter_lists;
15354 parser->num_template_parameter_lists = 0;
15355
15356 /* Look for the type-specifier. */
15357 cp_parser_type_specifier (parser,
15358 CP_PARSER_FLAGS_NONE,
15359 /*decl_specs=*/NULL,
15360 /*is_declarator=*/true,
15361 /*declares_class_or_enum=*/NULL,
15362 /*is_cv_qualifier=*/NULL);
15363
15364 parser->num_template_parameter_lists
15365 = saved_num_template_parameter_lists;
15366
15367 /* Leave the scope of the class. */
15368 if (pushed_scope)
15369 pop_scope (pushed_scope);
15370
15371 constructor_p = !cp_parser_error_occurred (parser);
15372 }
15373 }
15374 else
15375 constructor_p = false;
15376 /* We did not really want to consume any tokens. */
15377 cp_parser_abort_tentative_parse (parser);
15378
15379 return constructor_p;
15380 }
15381
15382 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15383 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15384 they must be performed once we are in the scope of the function.
15385
15386 Returns the function defined. */
15387
15388 static tree
15389 cp_parser_function_definition_from_specifiers_and_declarator
15390 (cp_parser* parser,
15391 cp_decl_specifier_seq *decl_specifiers,
15392 tree attributes,
15393 const cp_declarator *declarator)
15394 {
15395 tree fn;
15396 bool success_p;
15397
15398 /* Begin the function-definition. */
15399 success_p = start_function (decl_specifiers, declarator, attributes);
15400
15401 /* The things we're about to see are not directly qualified by any
15402 template headers we've seen thus far. */
15403 reset_specialization ();
15404
15405 /* If there were names looked up in the decl-specifier-seq that we
15406 did not check, check them now. We must wait until we are in the
15407 scope of the function to perform the checks, since the function
15408 might be a friend. */
15409 perform_deferred_access_checks ();
15410
15411 if (!success_p)
15412 {
15413 /* Skip the entire function. */
15414 cp_parser_skip_to_end_of_block_or_statement (parser);
15415 fn = error_mark_node;
15416 }
15417 else
15418 fn = cp_parser_function_definition_after_declarator (parser,
15419 /*inline_p=*/false);
15420
15421 return fn;
15422 }
15423
15424 /* Parse the part of a function-definition that follows the
15425 declarator. INLINE_P is TRUE iff this function is an inline
15426 function defined with a class-specifier.
15427
15428 Returns the function defined. */
15429
15430 static tree
15431 cp_parser_function_definition_after_declarator (cp_parser* parser,
15432 bool inline_p)
15433 {
15434 tree fn;
15435 bool ctor_initializer_p = false;
15436 bool saved_in_unbraced_linkage_specification_p;
15437 unsigned saved_num_template_parameter_lists;
15438
15439 /* If the next token is `return', then the code may be trying to
15440 make use of the "named return value" extension that G++ used to
15441 support. */
15442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15443 {
15444 /* Consume the `return' keyword. */
15445 cp_lexer_consume_token (parser->lexer);
15446 /* Look for the identifier that indicates what value is to be
15447 returned. */
15448 cp_parser_identifier (parser);
15449 /* Issue an error message. */
15450 error ("named return values are no longer supported");
15451 /* Skip tokens until we reach the start of the function body. */
15452 while (true)
15453 {
15454 cp_token *token = cp_lexer_peek_token (parser->lexer);
15455 if (token->type == CPP_OPEN_BRACE
15456 || token->type == CPP_EOF
15457 || token->type == CPP_PRAGMA_EOL)
15458 break;
15459 cp_lexer_consume_token (parser->lexer);
15460 }
15461 }
15462 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15463 anything declared inside `f'. */
15464 saved_in_unbraced_linkage_specification_p
15465 = parser->in_unbraced_linkage_specification_p;
15466 parser->in_unbraced_linkage_specification_p = false;
15467 /* Inside the function, surrounding template-parameter-lists do not
15468 apply. */
15469 saved_num_template_parameter_lists
15470 = parser->num_template_parameter_lists;
15471 parser->num_template_parameter_lists = 0;
15472 /* If the next token is `try', then we are looking at a
15473 function-try-block. */
15474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15475 ctor_initializer_p = cp_parser_function_try_block (parser);
15476 /* A function-try-block includes the function-body, so we only do
15477 this next part if we're not processing a function-try-block. */
15478 else
15479 ctor_initializer_p
15480 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15481
15482 /* Finish the function. */
15483 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15484 (inline_p ? 2 : 0));
15485 /* Generate code for it, if necessary. */
15486 expand_or_defer_fn (fn);
15487 /* Restore the saved values. */
15488 parser->in_unbraced_linkage_specification_p
15489 = saved_in_unbraced_linkage_specification_p;
15490 parser->num_template_parameter_lists
15491 = saved_num_template_parameter_lists;
15492
15493 return fn;
15494 }
15495
15496 /* Parse a template-declaration, assuming that the `export' (and
15497 `extern') keywords, if present, has already been scanned. MEMBER_P
15498 is as for cp_parser_template_declaration. */
15499
15500 static void
15501 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15502 {
15503 tree decl = NULL_TREE;
15504 tree checks;
15505 tree parameter_list;
15506 bool friend_p = false;
15507 bool need_lang_pop;
15508
15509 /* Look for the `template' keyword. */
15510 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15511 return;
15512
15513 /* And the `<'. */
15514 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15515 return;
15516 /* [temp]
15517
15518 A template ... shall not have C linkage. */
15519 if (current_lang_name == lang_name_c)
15520 {
15521 error ("template with C linkage");
15522 /* Give it C++ linkage to avoid confusing other parts of the
15523 front end. */
15524 push_lang_context (lang_name_cplusplus);
15525 need_lang_pop = true;
15526 }
15527 else
15528 need_lang_pop = false;
15529
15530 /* We cannot perform access checks on the template parameter
15531 declarations until we know what is being declared, just as we
15532 cannot check the decl-specifier list. */
15533 push_deferring_access_checks (dk_deferred);
15534
15535 /* If the next token is `>', then we have an invalid
15536 specialization. Rather than complain about an invalid template
15537 parameter, issue an error message here. */
15538 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15539 {
15540 cp_parser_error (parser, "invalid explicit specialization");
15541 begin_specialization ();
15542 parameter_list = NULL_TREE;
15543 }
15544 else
15545 /* Parse the template parameters. */
15546 parameter_list = cp_parser_template_parameter_list (parser);
15547
15548 /* Get the deferred access checks from the parameter list. These
15549 will be checked once we know what is being declared, as for a
15550 member template the checks must be performed in the scope of the
15551 class containing the member. */
15552 checks = get_deferred_access_checks ();
15553
15554 /* Look for the `>'. */
15555 cp_parser_skip_to_end_of_template_parameter_list (parser);
15556 /* We just processed one more parameter list. */
15557 ++parser->num_template_parameter_lists;
15558 /* If the next token is `template', there are more template
15559 parameters. */
15560 if (cp_lexer_next_token_is_keyword (parser->lexer,
15561 RID_TEMPLATE))
15562 cp_parser_template_declaration_after_export (parser, member_p);
15563 else
15564 {
15565 /* There are no access checks when parsing a template, as we do not
15566 know if a specialization will be a friend. */
15567 push_deferring_access_checks (dk_no_check);
15568 decl = cp_parser_single_declaration (parser,
15569 checks,
15570 member_p,
15571 &friend_p);
15572 pop_deferring_access_checks ();
15573
15574 /* If this is a member template declaration, let the front
15575 end know. */
15576 if (member_p && !friend_p && decl)
15577 {
15578 if (TREE_CODE (decl) == TYPE_DECL)
15579 cp_parser_check_access_in_redeclaration (decl);
15580
15581 decl = finish_member_template_decl (decl);
15582 }
15583 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15584 make_friend_class (current_class_type, TREE_TYPE (decl),
15585 /*complain=*/true);
15586 }
15587 /* We are done with the current parameter list. */
15588 --parser->num_template_parameter_lists;
15589
15590 pop_deferring_access_checks ();
15591
15592 /* Finish up. */
15593 finish_template_decl (parameter_list);
15594
15595 /* Register member declarations. */
15596 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15597 finish_member_declaration (decl);
15598 /* For the erroneous case of a template with C linkage, we pushed an
15599 implicit C++ linkage scope; exit that scope now. */
15600 if (need_lang_pop)
15601 pop_lang_context ();
15602 /* If DECL is a function template, we must return to parse it later.
15603 (Even though there is no definition, there might be default
15604 arguments that need handling.) */
15605 if (member_p && decl
15606 && (TREE_CODE (decl) == FUNCTION_DECL
15607 || DECL_FUNCTION_TEMPLATE_P (decl)))
15608 TREE_VALUE (parser->unparsed_functions_queues)
15609 = tree_cons (NULL_TREE, decl,
15610 TREE_VALUE (parser->unparsed_functions_queues));
15611 }
15612
15613 /* Perform the deferred access checks from a template-parameter-list.
15614 CHECKS is a TREE_LIST of access checks, as returned by
15615 get_deferred_access_checks. */
15616
15617 static void
15618 cp_parser_perform_template_parameter_access_checks (tree checks)
15619 {
15620 ++processing_template_parmlist;
15621 perform_access_checks (checks);
15622 --processing_template_parmlist;
15623 }
15624
15625 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15626 `function-definition' sequence. MEMBER_P is true, this declaration
15627 appears in a class scope.
15628
15629 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15630 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15631
15632 static tree
15633 cp_parser_single_declaration (cp_parser* parser,
15634 tree checks,
15635 bool member_p,
15636 bool* friend_p)
15637 {
15638 int declares_class_or_enum;
15639 tree decl = NULL_TREE;
15640 cp_decl_specifier_seq decl_specifiers;
15641 bool function_definition_p = false;
15642
15643 /* This function is only used when processing a template
15644 declaration. */
15645 gcc_assert (innermost_scope_kind () == sk_template_parms
15646 || innermost_scope_kind () == sk_template_spec);
15647
15648 /* Defer access checks until we know what is being declared. */
15649 push_deferring_access_checks (dk_deferred);
15650
15651 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15652 alternative. */
15653 cp_parser_decl_specifier_seq (parser,
15654 CP_PARSER_FLAGS_OPTIONAL,
15655 &decl_specifiers,
15656 &declares_class_or_enum);
15657 if (friend_p)
15658 *friend_p = cp_parser_friend_p (&decl_specifiers);
15659
15660 /* There are no template typedefs. */
15661 if (decl_specifiers.specs[(int) ds_typedef])
15662 {
15663 error ("template declaration of %qs", "typedef");
15664 decl = error_mark_node;
15665 }
15666
15667 /* Gather up the access checks that occurred the
15668 decl-specifier-seq. */
15669 stop_deferring_access_checks ();
15670
15671 /* Check for the declaration of a template class. */
15672 if (declares_class_or_enum)
15673 {
15674 if (cp_parser_declares_only_class_p (parser))
15675 {
15676 decl = shadow_tag (&decl_specifiers);
15677
15678 /* In this case:
15679
15680 struct C {
15681 friend template <typename T> struct A<T>::B;
15682 };
15683
15684 A<T>::B will be represented by a TYPENAME_TYPE, and
15685 therefore not recognized by shadow_tag. */
15686 if (friend_p && *friend_p
15687 && !decl
15688 && decl_specifiers.type
15689 && TYPE_P (decl_specifiers.type))
15690 decl = decl_specifiers.type;
15691
15692 if (decl && decl != error_mark_node)
15693 decl = TYPE_NAME (decl);
15694 else
15695 decl = error_mark_node;
15696
15697 /* Perform access checks for template parameters. */
15698 cp_parser_perform_template_parameter_access_checks (checks);
15699 }
15700 }
15701 /* If it's not a template class, try for a template function. If
15702 the next token is a `;', then this declaration does not declare
15703 anything. But, if there were errors in the decl-specifiers, then
15704 the error might well have come from an attempted class-specifier.
15705 In that case, there's no need to warn about a missing declarator. */
15706 if (!decl
15707 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15708 || decl_specifiers.type != error_mark_node))
15709 decl = cp_parser_init_declarator (parser,
15710 &decl_specifiers,
15711 checks,
15712 /*function_definition_allowed_p=*/true,
15713 member_p,
15714 declares_class_or_enum,
15715 &function_definition_p);
15716
15717 pop_deferring_access_checks ();
15718
15719 /* Clear any current qualification; whatever comes next is the start
15720 of something new. */
15721 parser->scope = NULL_TREE;
15722 parser->qualifying_scope = NULL_TREE;
15723 parser->object_scope = NULL_TREE;
15724 /* Look for a trailing `;' after the declaration. */
15725 if (!function_definition_p
15726 && (decl == error_mark_node
15727 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15728 cp_parser_skip_to_end_of_block_or_statement (parser);
15729
15730 return decl;
15731 }
15732
15733 /* Parse a cast-expression that is not the operand of a unary "&". */
15734
15735 static tree
15736 cp_parser_simple_cast_expression (cp_parser *parser)
15737 {
15738 return cp_parser_cast_expression (parser, /*address_p=*/false,
15739 /*cast_p=*/false);
15740 }
15741
15742 /* Parse a functional cast to TYPE. Returns an expression
15743 representing the cast. */
15744
15745 static tree
15746 cp_parser_functional_cast (cp_parser* parser, tree type)
15747 {
15748 tree expression_list;
15749 tree cast;
15750
15751 expression_list
15752 = cp_parser_parenthesized_expression_list (parser, false,
15753 /*cast_p=*/true,
15754 /*non_constant_p=*/NULL);
15755
15756 cast = build_functional_cast (type, expression_list);
15757 /* [expr.const]/1: In an integral constant expression "only type
15758 conversions to integral or enumeration type can be used". */
15759 if (TREE_CODE (type) == TYPE_DECL)
15760 type = TREE_TYPE (type);
15761 if (cast != error_mark_node
15762 && !cast_valid_in_integral_constant_expression_p (type)
15763 && (cp_parser_non_integral_constant_expression
15764 (parser, "a call to a constructor")))
15765 return error_mark_node;
15766 return cast;
15767 }
15768
15769 /* Save the tokens that make up the body of a member function defined
15770 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15771 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15772 specifiers applied to the declaration. Returns the FUNCTION_DECL
15773 for the member function. */
15774
15775 static tree
15776 cp_parser_save_member_function_body (cp_parser* parser,
15777 cp_decl_specifier_seq *decl_specifiers,
15778 cp_declarator *declarator,
15779 tree attributes)
15780 {
15781 cp_token *first;
15782 cp_token *last;
15783 tree fn;
15784
15785 /* Create the function-declaration. */
15786 fn = start_method (decl_specifiers, declarator, attributes);
15787 /* If something went badly wrong, bail out now. */
15788 if (fn == error_mark_node)
15789 {
15790 /* If there's a function-body, skip it. */
15791 if (cp_parser_token_starts_function_definition_p
15792 (cp_lexer_peek_token (parser->lexer)))
15793 cp_parser_skip_to_end_of_block_or_statement (parser);
15794 return error_mark_node;
15795 }
15796
15797 /* Remember it, if there default args to post process. */
15798 cp_parser_save_default_args (parser, fn);
15799
15800 /* Save away the tokens that make up the body of the
15801 function. */
15802 first = parser->lexer->next_token;
15803 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15804 /* Handle function try blocks. */
15805 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15806 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15807 last = parser->lexer->next_token;
15808
15809 /* Save away the inline definition; we will process it when the
15810 class is complete. */
15811 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15812 DECL_PENDING_INLINE_P (fn) = 1;
15813
15814 /* We need to know that this was defined in the class, so that
15815 friend templates are handled correctly. */
15816 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15817
15818 /* We're done with the inline definition. */
15819 finish_method (fn);
15820
15821 /* Add FN to the queue of functions to be parsed later. */
15822 TREE_VALUE (parser->unparsed_functions_queues)
15823 = tree_cons (NULL_TREE, fn,
15824 TREE_VALUE (parser->unparsed_functions_queues));
15825
15826 return fn;
15827 }
15828
15829 /* Parse a template-argument-list, as well as the trailing ">" (but
15830 not the opening ">"). See cp_parser_template_argument_list for the
15831 return value. */
15832
15833 static tree
15834 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15835 {
15836 tree arguments;
15837 tree saved_scope;
15838 tree saved_qualifying_scope;
15839 tree saved_object_scope;
15840 bool saved_greater_than_is_operator_p;
15841 bool saved_skip_evaluation;
15842
15843 /* [temp.names]
15844
15845 When parsing a template-id, the first non-nested `>' is taken as
15846 the end of the template-argument-list rather than a greater-than
15847 operator. */
15848 saved_greater_than_is_operator_p
15849 = parser->greater_than_is_operator_p;
15850 parser->greater_than_is_operator_p = false;
15851 /* Parsing the argument list may modify SCOPE, so we save it
15852 here. */
15853 saved_scope = parser->scope;
15854 saved_qualifying_scope = parser->qualifying_scope;
15855 saved_object_scope = parser->object_scope;
15856 /* We need to evaluate the template arguments, even though this
15857 template-id may be nested within a "sizeof". */
15858 saved_skip_evaluation = skip_evaluation;
15859 skip_evaluation = false;
15860 /* Parse the template-argument-list itself. */
15861 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15862 arguments = NULL_TREE;
15863 else
15864 arguments = cp_parser_template_argument_list (parser);
15865 /* Look for the `>' that ends the template-argument-list. If we find
15866 a '>>' instead, it's probably just a typo. */
15867 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15868 {
15869 if (!saved_greater_than_is_operator_p)
15870 {
15871 /* If we're in a nested template argument list, the '>>' has
15872 to be a typo for '> >'. We emit the error message, but we
15873 continue parsing and we push a '>' as next token, so that
15874 the argument list will be parsed correctly. Note that the
15875 global source location is still on the token before the
15876 '>>', so we need to say explicitly where we want it. */
15877 cp_token *token = cp_lexer_peek_token (parser->lexer);
15878 error ("%H%<>>%> should be %<> >%> "
15879 "within a nested template argument list",
15880 &token->location);
15881
15882 /* ??? Proper recovery should terminate two levels of
15883 template argument list here. */
15884 token->type = CPP_GREATER;
15885 }
15886 else
15887 {
15888 /* If this is not a nested template argument list, the '>>'
15889 is a typo for '>'. Emit an error message and continue.
15890 Same deal about the token location, but here we can get it
15891 right by consuming the '>>' before issuing the diagnostic. */
15892 cp_lexer_consume_token (parser->lexer);
15893 error ("spurious %<>>%>, use %<>%> to terminate "
15894 "a template argument list");
15895 }
15896 }
15897 else
15898 cp_parser_skip_to_end_of_template_parameter_list (parser);
15899 /* The `>' token might be a greater-than operator again now. */
15900 parser->greater_than_is_operator_p
15901 = saved_greater_than_is_operator_p;
15902 /* Restore the SAVED_SCOPE. */
15903 parser->scope = saved_scope;
15904 parser->qualifying_scope = saved_qualifying_scope;
15905 parser->object_scope = saved_object_scope;
15906 skip_evaluation = saved_skip_evaluation;
15907
15908 return arguments;
15909 }
15910
15911 /* MEMBER_FUNCTION is a member function, or a friend. If default
15912 arguments, or the body of the function have not yet been parsed,
15913 parse them now. */
15914
15915 static void
15916 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15917 {
15918 /* If this member is a template, get the underlying
15919 FUNCTION_DECL. */
15920 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15921 member_function = DECL_TEMPLATE_RESULT (member_function);
15922
15923 /* There should not be any class definitions in progress at this
15924 point; the bodies of members are only parsed outside of all class
15925 definitions. */
15926 gcc_assert (parser->num_classes_being_defined == 0);
15927 /* While we're parsing the member functions we might encounter more
15928 classes. We want to handle them right away, but we don't want
15929 them getting mixed up with functions that are currently in the
15930 queue. */
15931 parser->unparsed_functions_queues
15932 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15933
15934 /* Make sure that any template parameters are in scope. */
15935 maybe_begin_member_template_processing (member_function);
15936
15937 /* If the body of the function has not yet been parsed, parse it
15938 now. */
15939 if (DECL_PENDING_INLINE_P (member_function))
15940 {
15941 tree function_scope;
15942 cp_token_cache *tokens;
15943
15944 /* The function is no longer pending; we are processing it. */
15945 tokens = DECL_PENDING_INLINE_INFO (member_function);
15946 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15947 DECL_PENDING_INLINE_P (member_function) = 0;
15948
15949 /* If this is a local class, enter the scope of the containing
15950 function. */
15951 function_scope = current_function_decl;
15952 if (function_scope)
15953 push_function_context_to (function_scope);
15954
15955
15956 /* Push the body of the function onto the lexer stack. */
15957 cp_parser_push_lexer_for_tokens (parser, tokens);
15958
15959 /* Let the front end know that we going to be defining this
15960 function. */
15961 start_preparsed_function (member_function, NULL_TREE,
15962 SF_PRE_PARSED | SF_INCLASS_INLINE);
15963
15964 /* Don't do access checking if it is a templated function. */
15965 if (processing_template_decl)
15966 push_deferring_access_checks (dk_no_check);
15967
15968 /* Now, parse the body of the function. */
15969 cp_parser_function_definition_after_declarator (parser,
15970 /*inline_p=*/true);
15971
15972 if (processing_template_decl)
15973 pop_deferring_access_checks ();
15974
15975 /* Leave the scope of the containing function. */
15976 if (function_scope)
15977 pop_function_context_from (function_scope);
15978 cp_parser_pop_lexer (parser);
15979 }
15980
15981 /* Remove any template parameters from the symbol table. */
15982 maybe_end_member_template_processing ();
15983
15984 /* Restore the queue. */
15985 parser->unparsed_functions_queues
15986 = TREE_CHAIN (parser->unparsed_functions_queues);
15987 }
15988
15989 /* If DECL contains any default args, remember it on the unparsed
15990 functions queue. */
15991
15992 static void
15993 cp_parser_save_default_args (cp_parser* parser, tree decl)
15994 {
15995 tree probe;
15996
15997 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15998 probe;
15999 probe = TREE_CHAIN (probe))
16000 if (TREE_PURPOSE (probe))
16001 {
16002 TREE_PURPOSE (parser->unparsed_functions_queues)
16003 = tree_cons (current_class_type, decl,
16004 TREE_PURPOSE (parser->unparsed_functions_queues));
16005 break;
16006 }
16007 }
16008
16009 /* FN is a FUNCTION_DECL which may contains a parameter with an
16010 unparsed DEFAULT_ARG. Parse the default args now. This function
16011 assumes that the current scope is the scope in which the default
16012 argument should be processed. */
16013
16014 static void
16015 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16016 {
16017 bool saved_local_variables_forbidden_p;
16018 tree parm;
16019
16020 /* While we're parsing the default args, we might (due to the
16021 statement expression extension) encounter more classes. We want
16022 to handle them right away, but we don't want them getting mixed
16023 up with default args that are currently in the queue. */
16024 parser->unparsed_functions_queues
16025 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16026
16027 /* Local variable names (and the `this' keyword) may not appear
16028 in a default argument. */
16029 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16030 parser->local_variables_forbidden_p = true;
16031
16032 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16033 parm;
16034 parm = TREE_CHAIN (parm))
16035 {
16036 cp_token_cache *tokens;
16037 tree default_arg = TREE_PURPOSE (parm);
16038 tree parsed_arg;
16039 VEC(tree,gc) *insts;
16040 tree copy;
16041 unsigned ix;
16042
16043 if (!default_arg)
16044 continue;
16045
16046 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16047 /* This can happen for a friend declaration for a function
16048 already declared with default arguments. */
16049 continue;
16050
16051 /* Push the saved tokens for the default argument onto the parser's
16052 lexer stack. */
16053 tokens = DEFARG_TOKENS (default_arg);
16054 cp_parser_push_lexer_for_tokens (parser, tokens);
16055
16056 /* Parse the assignment-expression. */
16057 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16058
16059 if (!processing_template_decl)
16060 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16061
16062 TREE_PURPOSE (parm) = parsed_arg;
16063
16064 /* Update any instantiations we've already created. */
16065 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16066 VEC_iterate (tree, insts, ix, copy); ix++)
16067 TREE_PURPOSE (copy) = parsed_arg;
16068
16069 /* If the token stream has not been completely used up, then
16070 there was extra junk after the end of the default
16071 argument. */
16072 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16073 cp_parser_error (parser, "expected %<,%>");
16074
16075 /* Revert to the main lexer. */
16076 cp_parser_pop_lexer (parser);
16077 }
16078
16079 /* Make sure no default arg is missing. */
16080 check_default_args (fn);
16081
16082 /* Restore the state of local_variables_forbidden_p. */
16083 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16084
16085 /* Restore the queue. */
16086 parser->unparsed_functions_queues
16087 = TREE_CHAIN (parser->unparsed_functions_queues);
16088 }
16089
16090 /* Parse the operand of `sizeof' (or a similar operator). Returns
16091 either a TYPE or an expression, depending on the form of the
16092 input. The KEYWORD indicates which kind of expression we have
16093 encountered. */
16094
16095 static tree
16096 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16097 {
16098 static const char *format;
16099 tree expr = NULL_TREE;
16100 const char *saved_message;
16101 bool saved_integral_constant_expression_p;
16102 bool saved_non_integral_constant_expression_p;
16103
16104 /* Initialize FORMAT the first time we get here. */
16105 if (!format)
16106 format = "types may not be defined in '%s' expressions";
16107
16108 /* Types cannot be defined in a `sizeof' expression. Save away the
16109 old message. */
16110 saved_message = parser->type_definition_forbidden_message;
16111 /* And create the new one. */
16112 parser->type_definition_forbidden_message
16113 = XNEWVEC (const char, strlen (format)
16114 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16115 + 1 /* `\0' */);
16116 sprintf ((char *) parser->type_definition_forbidden_message,
16117 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16118
16119 /* The restrictions on constant-expressions do not apply inside
16120 sizeof expressions. */
16121 saved_integral_constant_expression_p
16122 = parser->integral_constant_expression_p;
16123 saved_non_integral_constant_expression_p
16124 = parser->non_integral_constant_expression_p;
16125 parser->integral_constant_expression_p = false;
16126
16127 /* Do not actually evaluate the expression. */
16128 ++skip_evaluation;
16129 /* If it's a `(', then we might be looking at the type-id
16130 construction. */
16131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16132 {
16133 tree type;
16134 bool saved_in_type_id_in_expr_p;
16135
16136 /* We can't be sure yet whether we're looking at a type-id or an
16137 expression. */
16138 cp_parser_parse_tentatively (parser);
16139 /* Consume the `('. */
16140 cp_lexer_consume_token (parser->lexer);
16141 /* Parse the type-id. */
16142 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16143 parser->in_type_id_in_expr_p = true;
16144 type = cp_parser_type_id (parser);
16145 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16146 /* Now, look for the trailing `)'. */
16147 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16148 /* If all went well, then we're done. */
16149 if (cp_parser_parse_definitely (parser))
16150 {
16151 cp_decl_specifier_seq decl_specs;
16152
16153 /* Build a trivial decl-specifier-seq. */
16154 clear_decl_specs (&decl_specs);
16155 decl_specs.type = type;
16156
16157 /* Call grokdeclarator to figure out what type this is. */
16158 expr = grokdeclarator (NULL,
16159 &decl_specs,
16160 TYPENAME,
16161 /*initialized=*/0,
16162 /*attrlist=*/NULL);
16163 }
16164 }
16165
16166 /* If the type-id production did not work out, then we must be
16167 looking at the unary-expression production. */
16168 if (!expr)
16169 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16170 /*cast_p=*/false);
16171 /* Go back to evaluating expressions. */
16172 --skip_evaluation;
16173
16174 /* Free the message we created. */
16175 free ((char *) parser->type_definition_forbidden_message);
16176 /* And restore the old one. */
16177 parser->type_definition_forbidden_message = saved_message;
16178 parser->integral_constant_expression_p
16179 = saved_integral_constant_expression_p;
16180 parser->non_integral_constant_expression_p
16181 = saved_non_integral_constant_expression_p;
16182
16183 return expr;
16184 }
16185
16186 /* If the current declaration has no declarator, return true. */
16187
16188 static bool
16189 cp_parser_declares_only_class_p (cp_parser *parser)
16190 {
16191 /* If the next token is a `;' or a `,' then there is no
16192 declarator. */
16193 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16194 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16195 }
16196
16197 /* Update the DECL_SPECS to reflect the storage class indicated by
16198 KEYWORD. */
16199
16200 static void
16201 cp_parser_set_storage_class (cp_parser *parser,
16202 cp_decl_specifier_seq *decl_specs,
16203 enum rid keyword)
16204 {
16205 cp_storage_class storage_class;
16206
16207 if (parser->in_unbraced_linkage_specification_p)
16208 {
16209 error ("invalid use of %qD in linkage specification",
16210 ridpointers[keyword]);
16211 return;
16212 }
16213 else if (decl_specs->storage_class != sc_none)
16214 {
16215 decl_specs->conflicting_specifiers_p = true;
16216 return;
16217 }
16218
16219 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16220 && decl_specs->specs[(int) ds_thread])
16221 {
16222 error ("%<__thread%> before %qD", ridpointers[keyword]);
16223 decl_specs->specs[(int) ds_thread] = 0;
16224 }
16225
16226 switch (keyword)
16227 {
16228 case RID_AUTO:
16229 storage_class = sc_auto;
16230 break;
16231 case RID_REGISTER:
16232 storage_class = sc_register;
16233 break;
16234 case RID_STATIC:
16235 storage_class = sc_static;
16236 break;
16237 case RID_EXTERN:
16238 storage_class = sc_extern;
16239 break;
16240 case RID_MUTABLE:
16241 storage_class = sc_mutable;
16242 break;
16243 default:
16244 gcc_unreachable ();
16245 }
16246 decl_specs->storage_class = storage_class;
16247
16248 /* A storage class specifier cannot be applied alongside a typedef
16249 specifier. If there is a typedef specifier present then set
16250 conflicting_specifiers_p which will trigger an error later
16251 on in grokdeclarator. */
16252 if (decl_specs->specs[(int)ds_typedef])
16253 decl_specs->conflicting_specifiers_p = true;
16254 }
16255
16256 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16257 is true, the type is a user-defined type; otherwise it is a
16258 built-in type specified by a keyword. */
16259
16260 static void
16261 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16262 tree type_spec,
16263 bool user_defined_p)
16264 {
16265 decl_specs->any_specifiers_p = true;
16266
16267 /* If the user tries to redeclare bool or wchar_t (with, for
16268 example, in "typedef int wchar_t;") we remember that this is what
16269 happened. In system headers, we ignore these declarations so
16270 that G++ can work with system headers that are not C++-safe. */
16271 if (decl_specs->specs[(int) ds_typedef]
16272 && !user_defined_p
16273 && (type_spec == boolean_type_node
16274 || type_spec == wchar_type_node)
16275 && (decl_specs->type
16276 || decl_specs->specs[(int) ds_long]
16277 || decl_specs->specs[(int) ds_short]
16278 || decl_specs->specs[(int) ds_unsigned]
16279 || decl_specs->specs[(int) ds_signed]))
16280 {
16281 decl_specs->redefined_builtin_type = type_spec;
16282 if (!decl_specs->type)
16283 {
16284 decl_specs->type = type_spec;
16285 decl_specs->user_defined_type_p = false;
16286 }
16287 }
16288 else if (decl_specs->type)
16289 decl_specs->multiple_types_p = true;
16290 else
16291 {
16292 decl_specs->type = type_spec;
16293 decl_specs->user_defined_type_p = user_defined_p;
16294 decl_specs->redefined_builtin_type = NULL_TREE;
16295 }
16296 }
16297
16298 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16299 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16300
16301 static bool
16302 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16303 {
16304 return decl_specifiers->specs[(int) ds_friend] != 0;
16305 }
16306
16307 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16308 issue an error message indicating that TOKEN_DESC was expected.
16309
16310 Returns the token consumed, if the token had the appropriate type.
16311 Otherwise, returns NULL. */
16312
16313 static cp_token *
16314 cp_parser_require (cp_parser* parser,
16315 enum cpp_ttype type,
16316 const char* token_desc)
16317 {
16318 if (cp_lexer_next_token_is (parser->lexer, type))
16319 return cp_lexer_consume_token (parser->lexer);
16320 else
16321 {
16322 /* Output the MESSAGE -- unless we're parsing tentatively. */
16323 if (!cp_parser_simulate_error (parser))
16324 {
16325 char *message = concat ("expected ", token_desc, NULL);
16326 cp_parser_error (parser, message);
16327 free (message);
16328 }
16329 return NULL;
16330 }
16331 }
16332
16333 /* An error message is produced if the next token is not '>'.
16334 All further tokens are skipped until the desired token is
16335 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16336
16337 static void
16338 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16339 {
16340 /* Current level of '< ... >'. */
16341 unsigned level = 0;
16342 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16343 unsigned nesting_depth = 0;
16344
16345 /* Are we ready, yet? If not, issue error message. */
16346 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16347 return;
16348
16349 /* Skip tokens until the desired token is found. */
16350 while (true)
16351 {
16352 /* Peek at the next token. */
16353 switch (cp_lexer_peek_token (parser->lexer)->type)
16354 {
16355 case CPP_LESS:
16356 if (!nesting_depth)
16357 ++level;
16358 break;
16359
16360 case CPP_GREATER:
16361 if (!nesting_depth && level-- == 0)
16362 {
16363 /* We've reached the token we want, consume it and stop. */
16364 cp_lexer_consume_token (parser->lexer);
16365 return;
16366 }
16367 break;
16368
16369 case CPP_OPEN_PAREN:
16370 case CPP_OPEN_SQUARE:
16371 ++nesting_depth;
16372 break;
16373
16374 case CPP_CLOSE_PAREN:
16375 case CPP_CLOSE_SQUARE:
16376 if (nesting_depth-- == 0)
16377 return;
16378 break;
16379
16380 case CPP_EOF:
16381 case CPP_PRAGMA_EOL:
16382 case CPP_SEMICOLON:
16383 case CPP_OPEN_BRACE:
16384 case CPP_CLOSE_BRACE:
16385 /* The '>' was probably forgotten, don't look further. */
16386 return;
16387
16388 default:
16389 break;
16390 }
16391
16392 /* Consume this token. */
16393 cp_lexer_consume_token (parser->lexer);
16394 }
16395 }
16396
16397 /* If the next token is the indicated keyword, consume it. Otherwise,
16398 issue an error message indicating that TOKEN_DESC was expected.
16399
16400 Returns the token consumed, if the token had the appropriate type.
16401 Otherwise, returns NULL. */
16402
16403 static cp_token *
16404 cp_parser_require_keyword (cp_parser* parser,
16405 enum rid keyword,
16406 const char* token_desc)
16407 {
16408 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16409
16410 if (token && token->keyword != keyword)
16411 {
16412 dyn_string_t error_msg;
16413
16414 /* Format the error message. */
16415 error_msg = dyn_string_new (0);
16416 dyn_string_append_cstr (error_msg, "expected ");
16417 dyn_string_append_cstr (error_msg, token_desc);
16418 cp_parser_error (parser, error_msg->s);
16419 dyn_string_delete (error_msg);
16420 return NULL;
16421 }
16422
16423 return token;
16424 }
16425
16426 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16427 function-definition. */
16428
16429 static bool
16430 cp_parser_token_starts_function_definition_p (cp_token* token)
16431 {
16432 return (/* An ordinary function-body begins with an `{'. */
16433 token->type == CPP_OPEN_BRACE
16434 /* A ctor-initializer begins with a `:'. */
16435 || token->type == CPP_COLON
16436 /* A function-try-block begins with `try'. */
16437 || token->keyword == RID_TRY
16438 /* The named return value extension begins with `return'. */
16439 || token->keyword == RID_RETURN);
16440 }
16441
16442 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16443 definition. */
16444
16445 static bool
16446 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16447 {
16448 cp_token *token;
16449
16450 token = cp_lexer_peek_token (parser->lexer);
16451 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16452 }
16453
16454 /* Returns TRUE iff the next token is the "," or ">" ending a
16455 template-argument. */
16456
16457 static bool
16458 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16459 {
16460 cp_token *token;
16461
16462 token = cp_lexer_peek_token (parser->lexer);
16463 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16464 }
16465
16466 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16467 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16468
16469 static bool
16470 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16471 size_t n)
16472 {
16473 cp_token *token;
16474
16475 token = cp_lexer_peek_nth_token (parser->lexer, n);
16476 if (token->type == CPP_LESS)
16477 return true;
16478 /* Check for the sequence `<::' in the original code. It would be lexed as
16479 `[:', where `[' is a digraph, and there is no whitespace before
16480 `:'. */
16481 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16482 {
16483 cp_token *token2;
16484 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16485 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16486 return true;
16487 }
16488 return false;
16489 }
16490
16491 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16492 or none_type otherwise. */
16493
16494 static enum tag_types
16495 cp_parser_token_is_class_key (cp_token* token)
16496 {
16497 switch (token->keyword)
16498 {
16499 case RID_CLASS:
16500 return class_type;
16501 case RID_STRUCT:
16502 return record_type;
16503 case RID_UNION:
16504 return union_type;
16505
16506 default:
16507 return none_type;
16508 }
16509 }
16510
16511 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16512
16513 static void
16514 cp_parser_check_class_key (enum tag_types class_key, tree type)
16515 {
16516 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16517 pedwarn ("%qs tag used in naming %q#T",
16518 class_key == union_type ? "union"
16519 : class_key == record_type ? "struct" : "class",
16520 type);
16521 }
16522
16523 /* Issue an error message if DECL is redeclared with different
16524 access than its original declaration [class.access.spec/3].
16525 This applies to nested classes and nested class templates.
16526 [class.mem/1]. */
16527
16528 static void
16529 cp_parser_check_access_in_redeclaration (tree decl)
16530 {
16531 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16532 return;
16533
16534 if ((TREE_PRIVATE (decl)
16535 != (current_access_specifier == access_private_node))
16536 || (TREE_PROTECTED (decl)
16537 != (current_access_specifier == access_protected_node)))
16538 error ("%qD redeclared with different access", decl);
16539 }
16540
16541 /* Look for the `template' keyword, as a syntactic disambiguator.
16542 Return TRUE iff it is present, in which case it will be
16543 consumed. */
16544
16545 static bool
16546 cp_parser_optional_template_keyword (cp_parser *parser)
16547 {
16548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16549 {
16550 /* The `template' keyword can only be used within templates;
16551 outside templates the parser can always figure out what is a
16552 template and what is not. */
16553 if (!processing_template_decl)
16554 {
16555 error ("%<template%> (as a disambiguator) is only allowed "
16556 "within templates");
16557 /* If this part of the token stream is rescanned, the same
16558 error message would be generated. So, we purge the token
16559 from the stream. */
16560 cp_lexer_purge_token (parser->lexer);
16561 return false;
16562 }
16563 else
16564 {
16565 /* Consume the `template' keyword. */
16566 cp_lexer_consume_token (parser->lexer);
16567 return true;
16568 }
16569 }
16570
16571 return false;
16572 }
16573
16574 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16575 set PARSER->SCOPE, and perform other related actions. */
16576
16577 static void
16578 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16579 {
16580 tree value;
16581 tree check;
16582
16583 /* Get the stored value. */
16584 value = cp_lexer_consume_token (parser->lexer)->value;
16585 /* Perform any access checks that were deferred. */
16586 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16587 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16588 /* Set the scope from the stored value. */
16589 parser->scope = TREE_VALUE (value);
16590 parser->qualifying_scope = TREE_TYPE (value);
16591 parser->object_scope = NULL_TREE;
16592 }
16593
16594 /* Consume tokens up through a non-nested END token. */
16595
16596 static void
16597 cp_parser_cache_group (cp_parser *parser,
16598 enum cpp_ttype end,
16599 unsigned depth)
16600 {
16601 while (true)
16602 {
16603 cp_token *token;
16604
16605 /* Abort a parenthesized expression if we encounter a brace. */
16606 if ((end == CPP_CLOSE_PAREN || depth == 0)
16607 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16608 return;
16609 /* If we've reached the end of the file, stop. */
16610 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16611 || (end != CPP_PRAGMA_EOL
16612 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16613 return;
16614 /* Consume the next token. */
16615 token = cp_lexer_consume_token (parser->lexer);
16616 /* See if it starts a new group. */
16617 if (token->type == CPP_OPEN_BRACE)
16618 {
16619 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16620 if (depth == 0)
16621 return;
16622 }
16623 else if (token->type == CPP_OPEN_PAREN)
16624 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16625 else if (token->type == CPP_PRAGMA)
16626 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16627 else if (token->type == end)
16628 return;
16629 }
16630 }
16631
16632 /* Begin parsing tentatively. We always save tokens while parsing
16633 tentatively so that if the tentative parsing fails we can restore the
16634 tokens. */
16635
16636 static void
16637 cp_parser_parse_tentatively (cp_parser* parser)
16638 {
16639 /* Enter a new parsing context. */
16640 parser->context = cp_parser_context_new (parser->context);
16641 /* Begin saving tokens. */
16642 cp_lexer_save_tokens (parser->lexer);
16643 /* In order to avoid repetitive access control error messages,
16644 access checks are queued up until we are no longer parsing
16645 tentatively. */
16646 push_deferring_access_checks (dk_deferred);
16647 }
16648
16649 /* Commit to the currently active tentative parse. */
16650
16651 static void
16652 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16653 {
16654 cp_parser_context *context;
16655 cp_lexer *lexer;
16656
16657 /* Mark all of the levels as committed. */
16658 lexer = parser->lexer;
16659 for (context = parser->context; context->next; context = context->next)
16660 {
16661 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16662 break;
16663 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16664 while (!cp_lexer_saving_tokens (lexer))
16665 lexer = lexer->next;
16666 cp_lexer_commit_tokens (lexer);
16667 }
16668 }
16669
16670 /* Abort the currently active tentative parse. All consumed tokens
16671 will be rolled back, and no diagnostics will be issued. */
16672
16673 static void
16674 cp_parser_abort_tentative_parse (cp_parser* parser)
16675 {
16676 cp_parser_simulate_error (parser);
16677 /* Now, pretend that we want to see if the construct was
16678 successfully parsed. */
16679 cp_parser_parse_definitely (parser);
16680 }
16681
16682 /* Stop parsing tentatively. If a parse error has occurred, restore the
16683 token stream. Otherwise, commit to the tokens we have consumed.
16684 Returns true if no error occurred; false otherwise. */
16685
16686 static bool
16687 cp_parser_parse_definitely (cp_parser* parser)
16688 {
16689 bool error_occurred;
16690 cp_parser_context *context;
16691
16692 /* Remember whether or not an error occurred, since we are about to
16693 destroy that information. */
16694 error_occurred = cp_parser_error_occurred (parser);
16695 /* Remove the topmost context from the stack. */
16696 context = parser->context;
16697 parser->context = context->next;
16698 /* If no parse errors occurred, commit to the tentative parse. */
16699 if (!error_occurred)
16700 {
16701 /* Commit to the tokens read tentatively, unless that was
16702 already done. */
16703 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16704 cp_lexer_commit_tokens (parser->lexer);
16705
16706 pop_to_parent_deferring_access_checks ();
16707 }
16708 /* Otherwise, if errors occurred, roll back our state so that things
16709 are just as they were before we began the tentative parse. */
16710 else
16711 {
16712 cp_lexer_rollback_tokens (parser->lexer);
16713 pop_deferring_access_checks ();
16714 }
16715 /* Add the context to the front of the free list. */
16716 context->next = cp_parser_context_free_list;
16717 cp_parser_context_free_list = context;
16718
16719 return !error_occurred;
16720 }
16721
16722 /* Returns true if we are parsing tentatively and are not committed to
16723 this tentative parse. */
16724
16725 static bool
16726 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16727 {
16728 return (cp_parser_parsing_tentatively (parser)
16729 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16730 }
16731
16732 /* Returns nonzero iff an error has occurred during the most recent
16733 tentative parse. */
16734
16735 static bool
16736 cp_parser_error_occurred (cp_parser* parser)
16737 {
16738 return (cp_parser_parsing_tentatively (parser)
16739 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16740 }
16741
16742 /* Returns nonzero if GNU extensions are allowed. */
16743
16744 static bool
16745 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16746 {
16747 return parser->allow_gnu_extensions_p;
16748 }
16749 \f
16750 /* Objective-C++ Productions */
16751
16752
16753 /* Parse an Objective-C expression, which feeds into a primary-expression
16754 above.
16755
16756 objc-expression:
16757 objc-message-expression
16758 objc-string-literal
16759 objc-encode-expression
16760 objc-protocol-expression
16761 objc-selector-expression
16762
16763 Returns a tree representation of the expression. */
16764
16765 static tree
16766 cp_parser_objc_expression (cp_parser* parser)
16767 {
16768 /* Try to figure out what kind of declaration is present. */
16769 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16770
16771 switch (kwd->type)
16772 {
16773 case CPP_OPEN_SQUARE:
16774 return cp_parser_objc_message_expression (parser);
16775
16776 case CPP_OBJC_STRING:
16777 kwd = cp_lexer_consume_token (parser->lexer);
16778 return objc_build_string_object (kwd->value);
16779
16780 case CPP_KEYWORD:
16781 switch (kwd->keyword)
16782 {
16783 case RID_AT_ENCODE:
16784 return cp_parser_objc_encode_expression (parser);
16785
16786 case RID_AT_PROTOCOL:
16787 return cp_parser_objc_protocol_expression (parser);
16788
16789 case RID_AT_SELECTOR:
16790 return cp_parser_objc_selector_expression (parser);
16791
16792 default:
16793 break;
16794 }
16795 default:
16796 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16797 cp_parser_skip_to_end_of_block_or_statement (parser);
16798 }
16799
16800 return error_mark_node;
16801 }
16802
16803 /* Parse an Objective-C message expression.
16804
16805 objc-message-expression:
16806 [ objc-message-receiver objc-message-args ]
16807
16808 Returns a representation of an Objective-C message. */
16809
16810 static tree
16811 cp_parser_objc_message_expression (cp_parser* parser)
16812 {
16813 tree receiver, messageargs;
16814
16815 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16816 receiver = cp_parser_objc_message_receiver (parser);
16817 messageargs = cp_parser_objc_message_args (parser);
16818 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16819
16820 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16821 }
16822
16823 /* Parse an objc-message-receiver.
16824
16825 objc-message-receiver:
16826 expression
16827 simple-type-specifier
16828
16829 Returns a representation of the type or expression. */
16830
16831 static tree
16832 cp_parser_objc_message_receiver (cp_parser* parser)
16833 {
16834 tree rcv;
16835
16836 /* An Objective-C message receiver may be either (1) a type
16837 or (2) an expression. */
16838 cp_parser_parse_tentatively (parser);
16839 rcv = cp_parser_expression (parser, false);
16840
16841 if (cp_parser_parse_definitely (parser))
16842 return rcv;
16843
16844 rcv = cp_parser_simple_type_specifier (parser,
16845 /*decl_specs=*/NULL,
16846 CP_PARSER_FLAGS_NONE);
16847
16848 return objc_get_class_reference (rcv);
16849 }
16850
16851 /* Parse the arguments and selectors comprising an Objective-C message.
16852
16853 objc-message-args:
16854 objc-selector
16855 objc-selector-args
16856 objc-selector-args , objc-comma-args
16857
16858 objc-selector-args:
16859 objc-selector [opt] : assignment-expression
16860 objc-selector-args objc-selector [opt] : assignment-expression
16861
16862 objc-comma-args:
16863 assignment-expression
16864 objc-comma-args , assignment-expression
16865
16866 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16867 selector arguments and TREE_VALUE containing a list of comma
16868 arguments. */
16869
16870 static tree
16871 cp_parser_objc_message_args (cp_parser* parser)
16872 {
16873 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16874 bool maybe_unary_selector_p = true;
16875 cp_token *token = cp_lexer_peek_token (parser->lexer);
16876
16877 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16878 {
16879 tree selector = NULL_TREE, arg;
16880
16881 if (token->type != CPP_COLON)
16882 selector = cp_parser_objc_selector (parser);
16883
16884 /* Detect if we have a unary selector. */
16885 if (maybe_unary_selector_p
16886 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16887 return build_tree_list (selector, NULL_TREE);
16888
16889 maybe_unary_selector_p = false;
16890 cp_parser_require (parser, CPP_COLON, "`:'");
16891 arg = cp_parser_assignment_expression (parser, false);
16892
16893 sel_args
16894 = chainon (sel_args,
16895 build_tree_list (selector, arg));
16896
16897 token = cp_lexer_peek_token (parser->lexer);
16898 }
16899
16900 /* Handle non-selector arguments, if any. */
16901 while (token->type == CPP_COMMA)
16902 {
16903 tree arg;
16904
16905 cp_lexer_consume_token (parser->lexer);
16906 arg = cp_parser_assignment_expression (parser, false);
16907
16908 addl_args
16909 = chainon (addl_args,
16910 build_tree_list (NULL_TREE, arg));
16911
16912 token = cp_lexer_peek_token (parser->lexer);
16913 }
16914
16915 return build_tree_list (sel_args, addl_args);
16916 }
16917
16918 /* Parse an Objective-C encode expression.
16919
16920 objc-encode-expression:
16921 @encode objc-typename
16922
16923 Returns an encoded representation of the type argument. */
16924
16925 static tree
16926 cp_parser_objc_encode_expression (cp_parser* parser)
16927 {
16928 tree type;
16929
16930 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16931 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16932 type = complete_type (cp_parser_type_id (parser));
16933 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16934
16935 if (!type)
16936 {
16937 error ("%<@encode%> must specify a type as an argument");
16938 return error_mark_node;
16939 }
16940
16941 return objc_build_encode_expr (type);
16942 }
16943
16944 /* Parse an Objective-C @defs expression. */
16945
16946 static tree
16947 cp_parser_objc_defs_expression (cp_parser *parser)
16948 {
16949 tree name;
16950
16951 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16952 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16953 name = cp_parser_identifier (parser);
16954 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16955
16956 return objc_get_class_ivars (name);
16957 }
16958
16959 /* Parse an Objective-C protocol expression.
16960
16961 objc-protocol-expression:
16962 @protocol ( identifier )
16963
16964 Returns a representation of the protocol expression. */
16965
16966 static tree
16967 cp_parser_objc_protocol_expression (cp_parser* parser)
16968 {
16969 tree proto;
16970
16971 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16972 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16973 proto = cp_parser_identifier (parser);
16974 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16975
16976 return objc_build_protocol_expr (proto);
16977 }
16978
16979 /* Parse an Objective-C selector expression.
16980
16981 objc-selector-expression:
16982 @selector ( objc-method-signature )
16983
16984 objc-method-signature:
16985 objc-selector
16986 objc-selector-seq
16987
16988 objc-selector-seq:
16989 objc-selector :
16990 objc-selector-seq objc-selector :
16991
16992 Returns a representation of the method selector. */
16993
16994 static tree
16995 cp_parser_objc_selector_expression (cp_parser* parser)
16996 {
16997 tree sel_seq = NULL_TREE;
16998 bool maybe_unary_selector_p = true;
16999 cp_token *token;
17000
17001 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17002 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17003 token = cp_lexer_peek_token (parser->lexer);
17004
17005 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17006 || token->type == CPP_SCOPE)
17007 {
17008 tree selector = NULL_TREE;
17009
17010 if (token->type != CPP_COLON
17011 || token->type == CPP_SCOPE)
17012 selector = cp_parser_objc_selector (parser);
17013
17014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17015 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17016 {
17017 /* Detect if we have a unary selector. */
17018 if (maybe_unary_selector_p)
17019 {
17020 sel_seq = selector;
17021 goto finish_selector;
17022 }
17023 else
17024 {
17025 cp_parser_error (parser, "expected %<:%>");
17026 }
17027 }
17028 maybe_unary_selector_p = false;
17029 token = cp_lexer_consume_token (parser->lexer);
17030
17031 if (token->type == CPP_SCOPE)
17032 {
17033 sel_seq
17034 = chainon (sel_seq,
17035 build_tree_list (selector, NULL_TREE));
17036 sel_seq
17037 = chainon (sel_seq,
17038 build_tree_list (NULL_TREE, NULL_TREE));
17039 }
17040 else
17041 sel_seq
17042 = chainon (sel_seq,
17043 build_tree_list (selector, NULL_TREE));
17044
17045 token = cp_lexer_peek_token (parser->lexer);
17046 }
17047
17048 finish_selector:
17049 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17050
17051 return objc_build_selector_expr (sel_seq);
17052 }
17053
17054 /* Parse a list of identifiers.
17055
17056 objc-identifier-list:
17057 identifier
17058 objc-identifier-list , identifier
17059
17060 Returns a TREE_LIST of identifier nodes. */
17061
17062 static tree
17063 cp_parser_objc_identifier_list (cp_parser* parser)
17064 {
17065 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17066 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17067
17068 while (sep->type == CPP_COMMA)
17069 {
17070 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17071 list = chainon (list,
17072 build_tree_list (NULL_TREE,
17073 cp_parser_identifier (parser)));
17074 sep = cp_lexer_peek_token (parser->lexer);
17075 }
17076
17077 return list;
17078 }
17079
17080 /* Parse an Objective-C alias declaration.
17081
17082 objc-alias-declaration:
17083 @compatibility_alias identifier identifier ;
17084
17085 This function registers the alias mapping with the Objective-C front-end.
17086 It returns nothing. */
17087
17088 static void
17089 cp_parser_objc_alias_declaration (cp_parser* parser)
17090 {
17091 tree alias, orig;
17092
17093 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17094 alias = cp_parser_identifier (parser);
17095 orig = cp_parser_identifier (parser);
17096 objc_declare_alias (alias, orig);
17097 cp_parser_consume_semicolon_at_end_of_statement (parser);
17098 }
17099
17100 /* Parse an Objective-C class forward-declaration.
17101
17102 objc-class-declaration:
17103 @class objc-identifier-list ;
17104
17105 The function registers the forward declarations with the Objective-C
17106 front-end. It returns nothing. */
17107
17108 static void
17109 cp_parser_objc_class_declaration (cp_parser* parser)
17110 {
17111 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17112 objc_declare_class (cp_parser_objc_identifier_list (parser));
17113 cp_parser_consume_semicolon_at_end_of_statement (parser);
17114 }
17115
17116 /* Parse a list of Objective-C protocol references.
17117
17118 objc-protocol-refs-opt:
17119 objc-protocol-refs [opt]
17120
17121 objc-protocol-refs:
17122 < objc-identifier-list >
17123
17124 Returns a TREE_LIST of identifiers, if any. */
17125
17126 static tree
17127 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17128 {
17129 tree protorefs = NULL_TREE;
17130
17131 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17132 {
17133 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17134 protorefs = cp_parser_objc_identifier_list (parser);
17135 cp_parser_require (parser, CPP_GREATER, "`>'");
17136 }
17137
17138 return protorefs;
17139 }
17140
17141 /* Parse a Objective-C visibility specification. */
17142
17143 static void
17144 cp_parser_objc_visibility_spec (cp_parser* parser)
17145 {
17146 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17147
17148 switch (vis->keyword)
17149 {
17150 case RID_AT_PRIVATE:
17151 objc_set_visibility (2);
17152 break;
17153 case RID_AT_PROTECTED:
17154 objc_set_visibility (0);
17155 break;
17156 case RID_AT_PUBLIC:
17157 objc_set_visibility (1);
17158 break;
17159 default:
17160 return;
17161 }
17162
17163 /* Eat '@private'/'@protected'/'@public'. */
17164 cp_lexer_consume_token (parser->lexer);
17165 }
17166
17167 /* Parse an Objective-C method type. */
17168
17169 static void
17170 cp_parser_objc_method_type (cp_parser* parser)
17171 {
17172 objc_set_method_type
17173 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17174 ? PLUS_EXPR
17175 : MINUS_EXPR);
17176 }
17177
17178 /* Parse an Objective-C protocol qualifier. */
17179
17180 static tree
17181 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17182 {
17183 tree quals = NULL_TREE, node;
17184 cp_token *token = cp_lexer_peek_token (parser->lexer);
17185
17186 node = token->value;
17187
17188 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17189 && (node == ridpointers [(int) RID_IN]
17190 || node == ridpointers [(int) RID_OUT]
17191 || node == ridpointers [(int) RID_INOUT]
17192 || node == ridpointers [(int) RID_BYCOPY]
17193 || node == ridpointers [(int) RID_BYREF]
17194 || node == ridpointers [(int) RID_ONEWAY]))
17195 {
17196 quals = tree_cons (NULL_TREE, node, quals);
17197 cp_lexer_consume_token (parser->lexer);
17198 token = cp_lexer_peek_token (parser->lexer);
17199 node = token->value;
17200 }
17201
17202 return quals;
17203 }
17204
17205 /* Parse an Objective-C typename. */
17206
17207 static tree
17208 cp_parser_objc_typename (cp_parser* parser)
17209 {
17210 tree typename = NULL_TREE;
17211
17212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17213 {
17214 tree proto_quals, cp_type = NULL_TREE;
17215
17216 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17217 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17218
17219 /* An ObjC type name may consist of just protocol qualifiers, in which
17220 case the type shall default to 'id'. */
17221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17222 cp_type = cp_parser_type_id (parser);
17223
17224 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17225 typename = build_tree_list (proto_quals, cp_type);
17226 }
17227
17228 return typename;
17229 }
17230
17231 /* Check to see if TYPE refers to an Objective-C selector name. */
17232
17233 static bool
17234 cp_parser_objc_selector_p (enum cpp_ttype type)
17235 {
17236 return (type == CPP_NAME || type == CPP_KEYWORD
17237 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17238 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17239 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17240 || type == CPP_XOR || type == CPP_XOR_EQ);
17241 }
17242
17243 /* Parse an Objective-C selector. */
17244
17245 static tree
17246 cp_parser_objc_selector (cp_parser* parser)
17247 {
17248 cp_token *token = cp_lexer_consume_token (parser->lexer);
17249
17250 if (!cp_parser_objc_selector_p (token->type))
17251 {
17252 error ("invalid Objective-C++ selector name");
17253 return error_mark_node;
17254 }
17255
17256 /* C++ operator names are allowed to appear in ObjC selectors. */
17257 switch (token->type)
17258 {
17259 case CPP_AND_AND: return get_identifier ("and");
17260 case CPP_AND_EQ: return get_identifier ("and_eq");
17261 case CPP_AND: return get_identifier ("bitand");
17262 case CPP_OR: return get_identifier ("bitor");
17263 case CPP_COMPL: return get_identifier ("compl");
17264 case CPP_NOT: return get_identifier ("not");
17265 case CPP_NOT_EQ: return get_identifier ("not_eq");
17266 case CPP_OR_OR: return get_identifier ("or");
17267 case CPP_OR_EQ: return get_identifier ("or_eq");
17268 case CPP_XOR: return get_identifier ("xor");
17269 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17270 default: return token->value;
17271 }
17272 }
17273
17274 /* Parse an Objective-C params list. */
17275
17276 static tree
17277 cp_parser_objc_method_keyword_params (cp_parser* parser)
17278 {
17279 tree params = NULL_TREE;
17280 bool maybe_unary_selector_p = true;
17281 cp_token *token = cp_lexer_peek_token (parser->lexer);
17282
17283 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17284 {
17285 tree selector = NULL_TREE, typename, identifier;
17286
17287 if (token->type != CPP_COLON)
17288 selector = cp_parser_objc_selector (parser);
17289
17290 /* Detect if we have a unary selector. */
17291 if (maybe_unary_selector_p
17292 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17293 return selector;
17294
17295 maybe_unary_selector_p = false;
17296 cp_parser_require (parser, CPP_COLON, "`:'");
17297 typename = cp_parser_objc_typename (parser);
17298 identifier = cp_parser_identifier (parser);
17299
17300 params
17301 = chainon (params,
17302 objc_build_keyword_decl (selector,
17303 typename,
17304 identifier));
17305
17306 token = cp_lexer_peek_token (parser->lexer);
17307 }
17308
17309 return params;
17310 }
17311
17312 /* Parse the non-keyword Objective-C params. */
17313
17314 static tree
17315 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17316 {
17317 tree params = make_node (TREE_LIST);
17318 cp_token *token = cp_lexer_peek_token (parser->lexer);
17319 *ellipsisp = false; /* Initially, assume no ellipsis. */
17320
17321 while (token->type == CPP_COMMA)
17322 {
17323 cp_parameter_declarator *parmdecl;
17324 tree parm;
17325
17326 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17327 token = cp_lexer_peek_token (parser->lexer);
17328
17329 if (token->type == CPP_ELLIPSIS)
17330 {
17331 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17332 *ellipsisp = true;
17333 break;
17334 }
17335
17336 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17337 parm = grokdeclarator (parmdecl->declarator,
17338 &parmdecl->decl_specifiers,
17339 PARM, /*initialized=*/0,
17340 /*attrlist=*/NULL);
17341
17342 chainon (params, build_tree_list (NULL_TREE, parm));
17343 token = cp_lexer_peek_token (parser->lexer);
17344 }
17345
17346 return params;
17347 }
17348
17349 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17350
17351 static void
17352 cp_parser_objc_interstitial_code (cp_parser* parser)
17353 {
17354 cp_token *token = cp_lexer_peek_token (parser->lexer);
17355
17356 /* If the next token is `extern' and the following token is a string
17357 literal, then we have a linkage specification. */
17358 if (token->keyword == RID_EXTERN
17359 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17360 cp_parser_linkage_specification (parser);
17361 /* Handle #pragma, if any. */
17362 else if (token->type == CPP_PRAGMA)
17363 cp_parser_pragma (parser, pragma_external);
17364 /* Allow stray semicolons. */
17365 else if (token->type == CPP_SEMICOLON)
17366 cp_lexer_consume_token (parser->lexer);
17367 /* Finally, try to parse a block-declaration, or a function-definition. */
17368 else
17369 cp_parser_block_declaration (parser, /*statement_p=*/false);
17370 }
17371
17372 /* Parse a method signature. */
17373
17374 static tree
17375 cp_parser_objc_method_signature (cp_parser* parser)
17376 {
17377 tree rettype, kwdparms, optparms;
17378 bool ellipsis = false;
17379
17380 cp_parser_objc_method_type (parser);
17381 rettype = cp_parser_objc_typename (parser);
17382 kwdparms = cp_parser_objc_method_keyword_params (parser);
17383 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17384
17385 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17386 }
17387
17388 /* Pars an Objective-C method prototype list. */
17389
17390 static void
17391 cp_parser_objc_method_prototype_list (cp_parser* parser)
17392 {
17393 cp_token *token = cp_lexer_peek_token (parser->lexer);
17394
17395 while (token->keyword != RID_AT_END)
17396 {
17397 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17398 {
17399 objc_add_method_declaration
17400 (cp_parser_objc_method_signature (parser));
17401 cp_parser_consume_semicolon_at_end_of_statement (parser);
17402 }
17403 else
17404 /* Allow for interspersed non-ObjC++ code. */
17405 cp_parser_objc_interstitial_code (parser);
17406
17407 token = cp_lexer_peek_token (parser->lexer);
17408 }
17409
17410 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17411 objc_finish_interface ();
17412 }
17413
17414 /* Parse an Objective-C method definition list. */
17415
17416 static void
17417 cp_parser_objc_method_definition_list (cp_parser* parser)
17418 {
17419 cp_token *token = cp_lexer_peek_token (parser->lexer);
17420
17421 while (token->keyword != RID_AT_END)
17422 {
17423 tree meth;
17424
17425 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17426 {
17427 push_deferring_access_checks (dk_deferred);
17428 objc_start_method_definition
17429 (cp_parser_objc_method_signature (parser));
17430
17431 /* For historical reasons, we accept an optional semicolon. */
17432 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17433 cp_lexer_consume_token (parser->lexer);
17434
17435 perform_deferred_access_checks ();
17436 stop_deferring_access_checks ();
17437 meth = cp_parser_function_definition_after_declarator (parser,
17438 false);
17439 pop_deferring_access_checks ();
17440 objc_finish_method_definition (meth);
17441 }
17442 else
17443 /* Allow for interspersed non-ObjC++ code. */
17444 cp_parser_objc_interstitial_code (parser);
17445
17446 token = cp_lexer_peek_token (parser->lexer);
17447 }
17448
17449 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17450 objc_finish_implementation ();
17451 }
17452
17453 /* Parse Objective-C ivars. */
17454
17455 static void
17456 cp_parser_objc_class_ivars (cp_parser* parser)
17457 {
17458 cp_token *token = cp_lexer_peek_token (parser->lexer);
17459
17460 if (token->type != CPP_OPEN_BRACE)
17461 return; /* No ivars specified. */
17462
17463 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17464 token = cp_lexer_peek_token (parser->lexer);
17465
17466 while (token->type != CPP_CLOSE_BRACE)
17467 {
17468 cp_decl_specifier_seq declspecs;
17469 int decl_class_or_enum_p;
17470 tree prefix_attributes;
17471
17472 cp_parser_objc_visibility_spec (parser);
17473
17474 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17475 break;
17476
17477 cp_parser_decl_specifier_seq (parser,
17478 CP_PARSER_FLAGS_OPTIONAL,
17479 &declspecs,
17480 &decl_class_or_enum_p);
17481 prefix_attributes = declspecs.attributes;
17482 declspecs.attributes = NULL_TREE;
17483
17484 /* Keep going until we hit the `;' at the end of the
17485 declaration. */
17486 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17487 {
17488 tree width = NULL_TREE, attributes, first_attribute, decl;
17489 cp_declarator *declarator = NULL;
17490 int ctor_dtor_or_conv_p;
17491
17492 /* Check for a (possibly unnamed) bitfield declaration. */
17493 token = cp_lexer_peek_token (parser->lexer);
17494 if (token->type == CPP_COLON)
17495 goto eat_colon;
17496
17497 if (token->type == CPP_NAME
17498 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17499 == CPP_COLON))
17500 {
17501 /* Get the name of the bitfield. */
17502 declarator = make_id_declarator (NULL_TREE,
17503 cp_parser_identifier (parser),
17504 sfk_none);
17505
17506 eat_colon:
17507 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17508 /* Get the width of the bitfield. */
17509 width
17510 = cp_parser_constant_expression (parser,
17511 /*allow_non_constant=*/false,
17512 NULL);
17513 }
17514 else
17515 {
17516 /* Parse the declarator. */
17517 declarator
17518 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17519 &ctor_dtor_or_conv_p,
17520 /*parenthesized_p=*/NULL,
17521 /*member_p=*/false);
17522 }
17523
17524 /* Look for attributes that apply to the ivar. */
17525 attributes = cp_parser_attributes_opt (parser);
17526 /* Remember which attributes are prefix attributes and
17527 which are not. */
17528 first_attribute = attributes;
17529 /* Combine the attributes. */
17530 attributes = chainon (prefix_attributes, attributes);
17531
17532 if (width)
17533 {
17534 /* Create the bitfield declaration. */
17535 decl = grokbitfield (declarator, &declspecs, width);
17536 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17537 }
17538 else
17539 decl = grokfield (declarator, &declspecs,
17540 NULL_TREE, /*init_const_expr_p=*/false,
17541 NULL_TREE, attributes);
17542
17543 /* Add the instance variable. */
17544 objc_add_instance_variable (decl);
17545
17546 /* Reset PREFIX_ATTRIBUTES. */
17547 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17548 attributes = TREE_CHAIN (attributes);
17549 if (attributes)
17550 TREE_CHAIN (attributes) = NULL_TREE;
17551
17552 token = cp_lexer_peek_token (parser->lexer);
17553
17554 if (token->type == CPP_COMMA)
17555 {
17556 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17557 continue;
17558 }
17559 break;
17560 }
17561
17562 cp_parser_consume_semicolon_at_end_of_statement (parser);
17563 token = cp_lexer_peek_token (parser->lexer);
17564 }
17565
17566 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17567 /* For historical reasons, we accept an optional semicolon. */
17568 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17569 cp_lexer_consume_token (parser->lexer);
17570 }
17571
17572 /* Parse an Objective-C protocol declaration. */
17573
17574 static void
17575 cp_parser_objc_protocol_declaration (cp_parser* parser)
17576 {
17577 tree proto, protorefs;
17578 cp_token *tok;
17579
17580 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17582 {
17583 error ("identifier expected after %<@protocol%>");
17584 goto finish;
17585 }
17586
17587 /* See if we have a forward declaration or a definition. */
17588 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17589
17590 /* Try a forward declaration first. */
17591 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17592 {
17593 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17594 finish:
17595 cp_parser_consume_semicolon_at_end_of_statement (parser);
17596 }
17597
17598 /* Ok, we got a full-fledged definition (or at least should). */
17599 else
17600 {
17601 proto = cp_parser_identifier (parser);
17602 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17603 objc_start_protocol (proto, protorefs);
17604 cp_parser_objc_method_prototype_list (parser);
17605 }
17606 }
17607
17608 /* Parse an Objective-C superclass or category. */
17609
17610 static void
17611 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17612 tree *categ)
17613 {
17614 cp_token *next = cp_lexer_peek_token (parser->lexer);
17615
17616 *super = *categ = NULL_TREE;
17617 if (next->type == CPP_COLON)
17618 {
17619 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17620 *super = cp_parser_identifier (parser);
17621 }
17622 else if (next->type == CPP_OPEN_PAREN)
17623 {
17624 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17625 *categ = cp_parser_identifier (parser);
17626 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17627 }
17628 }
17629
17630 /* Parse an Objective-C class interface. */
17631
17632 static void
17633 cp_parser_objc_class_interface (cp_parser* parser)
17634 {
17635 tree name, super, categ, protos;
17636
17637 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17638 name = cp_parser_identifier (parser);
17639 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17640 protos = cp_parser_objc_protocol_refs_opt (parser);
17641
17642 /* We have either a class or a category on our hands. */
17643 if (categ)
17644 objc_start_category_interface (name, categ, protos);
17645 else
17646 {
17647 objc_start_class_interface (name, super, protos);
17648 /* Handle instance variable declarations, if any. */
17649 cp_parser_objc_class_ivars (parser);
17650 objc_continue_interface ();
17651 }
17652
17653 cp_parser_objc_method_prototype_list (parser);
17654 }
17655
17656 /* Parse an Objective-C class implementation. */
17657
17658 static void
17659 cp_parser_objc_class_implementation (cp_parser* parser)
17660 {
17661 tree name, super, categ;
17662
17663 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17664 name = cp_parser_identifier (parser);
17665 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17666
17667 /* We have either a class or a category on our hands. */
17668 if (categ)
17669 objc_start_category_implementation (name, categ);
17670 else
17671 {
17672 objc_start_class_implementation (name, super);
17673 /* Handle instance variable declarations, if any. */
17674 cp_parser_objc_class_ivars (parser);
17675 objc_continue_implementation ();
17676 }
17677
17678 cp_parser_objc_method_definition_list (parser);
17679 }
17680
17681 /* Consume the @end token and finish off the implementation. */
17682
17683 static void
17684 cp_parser_objc_end_implementation (cp_parser* parser)
17685 {
17686 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17687 objc_finish_implementation ();
17688 }
17689
17690 /* Parse an Objective-C declaration. */
17691
17692 static void
17693 cp_parser_objc_declaration (cp_parser* parser)
17694 {
17695 /* Try to figure out what kind of declaration is present. */
17696 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17697
17698 switch (kwd->keyword)
17699 {
17700 case RID_AT_ALIAS:
17701 cp_parser_objc_alias_declaration (parser);
17702 break;
17703 case RID_AT_CLASS:
17704 cp_parser_objc_class_declaration (parser);
17705 break;
17706 case RID_AT_PROTOCOL:
17707 cp_parser_objc_protocol_declaration (parser);
17708 break;
17709 case RID_AT_INTERFACE:
17710 cp_parser_objc_class_interface (parser);
17711 break;
17712 case RID_AT_IMPLEMENTATION:
17713 cp_parser_objc_class_implementation (parser);
17714 break;
17715 case RID_AT_END:
17716 cp_parser_objc_end_implementation (parser);
17717 break;
17718 default:
17719 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17720 cp_parser_skip_to_end_of_block_or_statement (parser);
17721 }
17722 }
17723
17724 /* Parse an Objective-C try-catch-finally statement.
17725
17726 objc-try-catch-finally-stmt:
17727 @try compound-statement objc-catch-clause-seq [opt]
17728 objc-finally-clause [opt]
17729
17730 objc-catch-clause-seq:
17731 objc-catch-clause objc-catch-clause-seq [opt]
17732
17733 objc-catch-clause:
17734 @catch ( exception-declaration ) compound-statement
17735
17736 objc-finally-clause
17737 @finally compound-statement
17738
17739 Returns NULL_TREE. */
17740
17741 static tree
17742 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17743 location_t location;
17744 tree stmt;
17745
17746 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17747 location = cp_lexer_peek_token (parser->lexer)->location;
17748 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17749 node, lest it get absorbed into the surrounding block. */
17750 stmt = push_stmt_list ();
17751 cp_parser_compound_statement (parser, NULL, false);
17752 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17753
17754 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17755 {
17756 cp_parameter_declarator *parmdecl;
17757 tree parm;
17758
17759 cp_lexer_consume_token (parser->lexer);
17760 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17761 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17762 parm = grokdeclarator (parmdecl->declarator,
17763 &parmdecl->decl_specifiers,
17764 PARM, /*initialized=*/0,
17765 /*attrlist=*/NULL);
17766 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17767 objc_begin_catch_clause (parm);
17768 cp_parser_compound_statement (parser, NULL, false);
17769 objc_finish_catch_clause ();
17770 }
17771
17772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17773 {
17774 cp_lexer_consume_token (parser->lexer);
17775 location = cp_lexer_peek_token (parser->lexer)->location;
17776 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17777 node, lest it get absorbed into the surrounding block. */
17778 stmt = push_stmt_list ();
17779 cp_parser_compound_statement (parser, NULL, false);
17780 objc_build_finally_clause (location, pop_stmt_list (stmt));
17781 }
17782
17783 return objc_finish_try_stmt ();
17784 }
17785
17786 /* Parse an Objective-C synchronized statement.
17787
17788 objc-synchronized-stmt:
17789 @synchronized ( expression ) compound-statement
17790
17791 Returns NULL_TREE. */
17792
17793 static tree
17794 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17795 location_t location;
17796 tree lock, stmt;
17797
17798 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17799
17800 location = cp_lexer_peek_token (parser->lexer)->location;
17801 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17802 lock = cp_parser_expression (parser, false);
17803 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17804
17805 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17806 node, lest it get absorbed into the surrounding block. */
17807 stmt = push_stmt_list ();
17808 cp_parser_compound_statement (parser, NULL, false);
17809
17810 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17811 }
17812
17813 /* Parse an Objective-C throw statement.
17814
17815 objc-throw-stmt:
17816 @throw assignment-expression [opt] ;
17817
17818 Returns a constructed '@throw' statement. */
17819
17820 static tree
17821 cp_parser_objc_throw_statement (cp_parser *parser) {
17822 tree expr = NULL_TREE;
17823
17824 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17825
17826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17827 expr = cp_parser_assignment_expression (parser, false);
17828
17829 cp_parser_consume_semicolon_at_end_of_statement (parser);
17830
17831 return objc_build_throw_stmt (expr);
17832 }
17833
17834 /* Parse an Objective-C statement. */
17835
17836 static tree
17837 cp_parser_objc_statement (cp_parser * parser) {
17838 /* Try to figure out what kind of declaration is present. */
17839 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17840
17841 switch (kwd->keyword)
17842 {
17843 case RID_AT_TRY:
17844 return cp_parser_objc_try_catch_finally_statement (parser);
17845 case RID_AT_SYNCHRONIZED:
17846 return cp_parser_objc_synchronized_statement (parser);
17847 case RID_AT_THROW:
17848 return cp_parser_objc_throw_statement (parser);
17849 default:
17850 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17851 cp_parser_skip_to_end_of_block_or_statement (parser);
17852 }
17853
17854 return error_mark_node;
17855 }
17856 \f
17857 /* OpenMP 2.5 parsing routines. */
17858
17859 /* All OpenMP clauses. OpenMP 2.5. */
17860 typedef enum pragma_omp_clause {
17861 PRAGMA_OMP_CLAUSE_NONE = 0,
17862
17863 PRAGMA_OMP_CLAUSE_COPYIN,
17864 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17865 PRAGMA_OMP_CLAUSE_DEFAULT,
17866 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17867 PRAGMA_OMP_CLAUSE_IF,
17868 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17869 PRAGMA_OMP_CLAUSE_NOWAIT,
17870 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17871 PRAGMA_OMP_CLAUSE_ORDERED,
17872 PRAGMA_OMP_CLAUSE_PRIVATE,
17873 PRAGMA_OMP_CLAUSE_REDUCTION,
17874 PRAGMA_OMP_CLAUSE_SCHEDULE,
17875 PRAGMA_OMP_CLAUSE_SHARED
17876 } pragma_omp_clause;
17877
17878 /* Returns name of the next clause.
17879 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17880 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17881 returned and the token is consumed. */
17882
17883 static pragma_omp_clause
17884 cp_parser_omp_clause_name (cp_parser *parser)
17885 {
17886 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17887
17888 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17889 result = PRAGMA_OMP_CLAUSE_IF;
17890 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17891 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17892 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17893 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17894 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17895 {
17896 tree id = cp_lexer_peek_token (parser->lexer)->value;
17897 const char *p = IDENTIFIER_POINTER (id);
17898
17899 switch (p[0])
17900 {
17901 case 'c':
17902 if (!strcmp ("copyin", p))
17903 result = PRAGMA_OMP_CLAUSE_COPYIN;
17904 else if (!strcmp ("copyprivate", p))
17905 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17906 break;
17907 case 'f':
17908 if (!strcmp ("firstprivate", p))
17909 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17910 break;
17911 case 'l':
17912 if (!strcmp ("lastprivate", p))
17913 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17914 break;
17915 case 'n':
17916 if (!strcmp ("nowait", p))
17917 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17918 else if (!strcmp ("num_threads", p))
17919 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17920 break;
17921 case 'o':
17922 if (!strcmp ("ordered", p))
17923 result = PRAGMA_OMP_CLAUSE_ORDERED;
17924 break;
17925 case 'r':
17926 if (!strcmp ("reduction", p))
17927 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17928 break;
17929 case 's':
17930 if (!strcmp ("schedule", p))
17931 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17932 else if (!strcmp ("shared", p))
17933 result = PRAGMA_OMP_CLAUSE_SHARED;
17934 break;
17935 }
17936 }
17937
17938 if (result != PRAGMA_OMP_CLAUSE_NONE)
17939 cp_lexer_consume_token (parser->lexer);
17940
17941 return result;
17942 }
17943
17944 /* Validate that a clause of the given type does not already exist. */
17945
17946 static void
17947 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17948 {
17949 tree c;
17950
17951 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17952 if (OMP_CLAUSE_CODE (c) == code)
17953 {
17954 error ("too many %qs clauses", name);
17955 break;
17956 }
17957 }
17958
17959 /* OpenMP 2.5:
17960 variable-list:
17961 identifier
17962 variable-list , identifier
17963
17964 In addition, we match a closing parenthesis. An opening parenthesis
17965 will have been consumed by the caller.
17966
17967 If KIND is nonzero, create the appropriate node and install the decl
17968 in OMP_CLAUSE_DECL and add the node to the head of the list.
17969
17970 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17971 return the list created. */
17972
17973 static tree
17974 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17975 tree list)
17976 {
17977 while (1)
17978 {
17979 tree name, decl;
17980
17981 name = cp_parser_id_expression (parser, /*template_p=*/false,
17982 /*check_dependency_p=*/true,
17983 /*template_p=*/NULL,
17984 /*declarator_p=*/false,
17985 /*optional_p=*/false);
17986 if (name == error_mark_node)
17987 goto skip_comma;
17988
17989 decl = cp_parser_lookup_name_simple (parser, name);
17990 if (decl == error_mark_node)
17991 cp_parser_name_lookup_error (parser, name, decl, NULL);
17992 else if (kind != 0)
17993 {
17994 tree u = build_omp_clause (kind);
17995 OMP_CLAUSE_DECL (u) = decl;
17996 OMP_CLAUSE_CHAIN (u) = list;
17997 list = u;
17998 }
17999 else
18000 list = tree_cons (decl, NULL_TREE, list);
18001
18002 get_comma:
18003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18004 break;
18005 cp_lexer_consume_token (parser->lexer);
18006 }
18007
18008 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18009 {
18010 int ending;
18011
18012 /* Try to resync to an unnested comma. Copied from
18013 cp_parser_parenthesized_expression_list. */
18014 skip_comma:
18015 ending = cp_parser_skip_to_closing_parenthesis (parser,
18016 /*recovering=*/true,
18017 /*or_comma=*/true,
18018 /*consume_paren=*/true);
18019 if (ending < 0)
18020 goto get_comma;
18021 }
18022
18023 return list;
18024 }
18025
18026 /* Similarly, but expect leading and trailing parenthesis. This is a very
18027 common case for omp clauses. */
18028
18029 static tree
18030 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18031 {
18032 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18033 return cp_parser_omp_var_list_no_open (parser, kind, list);
18034 return list;
18035 }
18036
18037 /* OpenMP 2.5:
18038 default ( shared | none ) */
18039
18040 static tree
18041 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18042 {
18043 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18044 tree c;
18045
18046 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18047 return list;
18048 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18049 {
18050 tree id = cp_lexer_peek_token (parser->lexer)->value;
18051 const char *p = IDENTIFIER_POINTER (id);
18052
18053 switch (p[0])
18054 {
18055 case 'n':
18056 if (strcmp ("none", p) != 0)
18057 goto invalid_kind;
18058 kind = OMP_CLAUSE_DEFAULT_NONE;
18059 break;
18060
18061 case 's':
18062 if (strcmp ("shared", p) != 0)
18063 goto invalid_kind;
18064 kind = OMP_CLAUSE_DEFAULT_SHARED;
18065 break;
18066
18067 default:
18068 goto invalid_kind;
18069 }
18070
18071 cp_lexer_consume_token (parser->lexer);
18072 }
18073 else
18074 {
18075 invalid_kind:
18076 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18077 }
18078
18079 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18080 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18081 /*or_comma=*/false,
18082 /*consume_paren=*/true);
18083
18084 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18085 return list;
18086
18087 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18088 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18089 OMP_CLAUSE_CHAIN (c) = list;
18090 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18091
18092 return c;
18093 }
18094
18095 /* OpenMP 2.5:
18096 if ( expression ) */
18097
18098 static tree
18099 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18100 {
18101 tree t, c;
18102
18103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18104 return list;
18105
18106 t = cp_parser_condition (parser);
18107
18108 if (t == error_mark_node
18109 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18110 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18111 /*or_comma=*/false,
18112 /*consume_paren=*/true);
18113
18114 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18115
18116 c = build_omp_clause (OMP_CLAUSE_IF);
18117 OMP_CLAUSE_IF_EXPR (c) = t;
18118 OMP_CLAUSE_CHAIN (c) = list;
18119
18120 return c;
18121 }
18122
18123 /* OpenMP 2.5:
18124 nowait */
18125
18126 static tree
18127 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18128 {
18129 tree c;
18130
18131 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18132
18133 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18134 OMP_CLAUSE_CHAIN (c) = list;
18135 return c;
18136 }
18137
18138 /* OpenMP 2.5:
18139 num_threads ( expression ) */
18140
18141 static tree
18142 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18143 {
18144 tree t, c;
18145
18146 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18147 return list;
18148
18149 t = cp_parser_expression (parser, false);
18150
18151 if (t == error_mark_node
18152 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18154 /*or_comma=*/false,
18155 /*consume_paren=*/true);
18156
18157 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18158
18159 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18160 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18161 OMP_CLAUSE_CHAIN (c) = list;
18162
18163 return c;
18164 }
18165
18166 /* OpenMP 2.5:
18167 ordered */
18168
18169 static tree
18170 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18171 {
18172 tree c;
18173
18174 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18175
18176 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18177 OMP_CLAUSE_CHAIN (c) = list;
18178 return c;
18179 }
18180
18181 /* OpenMP 2.5:
18182 reduction ( reduction-operator : variable-list )
18183
18184 reduction-operator:
18185 One of: + * - & ^ | && || */
18186
18187 static tree
18188 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18189 {
18190 enum tree_code code;
18191 tree nlist, c;
18192
18193 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18194 return list;
18195
18196 switch (cp_lexer_peek_token (parser->lexer)->type)
18197 {
18198 case CPP_PLUS:
18199 code = PLUS_EXPR;
18200 break;
18201 case CPP_MULT:
18202 code = MULT_EXPR;
18203 break;
18204 case CPP_MINUS:
18205 code = MINUS_EXPR;
18206 break;
18207 case CPP_AND:
18208 code = BIT_AND_EXPR;
18209 break;
18210 case CPP_XOR:
18211 code = BIT_XOR_EXPR;
18212 break;
18213 case CPP_OR:
18214 code = BIT_IOR_EXPR;
18215 break;
18216 case CPP_AND_AND:
18217 code = TRUTH_ANDIF_EXPR;
18218 break;
18219 case CPP_OR_OR:
18220 code = TRUTH_ORIF_EXPR;
18221 break;
18222 default:
18223 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18224 resync_fail:
18225 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18226 /*or_comma=*/false,
18227 /*consume_paren=*/true);
18228 return list;
18229 }
18230 cp_lexer_consume_token (parser->lexer);
18231
18232 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18233 goto resync_fail;
18234
18235 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18236 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18237 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18238
18239 return nlist;
18240 }
18241
18242 /* OpenMP 2.5:
18243 schedule ( schedule-kind )
18244 schedule ( schedule-kind , expression )
18245
18246 schedule-kind:
18247 static | dynamic | guided | runtime */
18248
18249 static tree
18250 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18251 {
18252 tree c, t;
18253
18254 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18255 return list;
18256
18257 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18258
18259 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18260 {
18261 tree id = cp_lexer_peek_token (parser->lexer)->value;
18262 const char *p = IDENTIFIER_POINTER (id);
18263
18264 switch (p[0])
18265 {
18266 case 'd':
18267 if (strcmp ("dynamic", p) != 0)
18268 goto invalid_kind;
18269 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18270 break;
18271
18272 case 'g':
18273 if (strcmp ("guided", p) != 0)
18274 goto invalid_kind;
18275 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18276 break;
18277
18278 case 'r':
18279 if (strcmp ("runtime", p) != 0)
18280 goto invalid_kind;
18281 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18282 break;
18283
18284 default:
18285 goto invalid_kind;
18286 }
18287 }
18288 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18289 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18290 else
18291 goto invalid_kind;
18292 cp_lexer_consume_token (parser->lexer);
18293
18294 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18295 {
18296 cp_lexer_consume_token (parser->lexer);
18297
18298 t = cp_parser_assignment_expression (parser, false);
18299
18300 if (t == error_mark_node)
18301 goto resync_fail;
18302 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18303 error ("schedule %<runtime%> does not take "
18304 "a %<chunk_size%> parameter");
18305 else
18306 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18307
18308 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18309 goto resync_fail;
18310 }
18311 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18312 goto resync_fail;
18313
18314 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18315 OMP_CLAUSE_CHAIN (c) = list;
18316 return c;
18317
18318 invalid_kind:
18319 cp_parser_error (parser, "invalid schedule kind");
18320 resync_fail:
18321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18322 /*or_comma=*/false,
18323 /*consume_paren=*/true);
18324 return list;
18325 }
18326
18327 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18328 is a bitmask in MASK. Return the list of clauses found; the result
18329 of clause default goes in *pdefault. */
18330
18331 static tree
18332 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18333 const char *where, cp_token *pragma_tok)
18334 {
18335 tree clauses = NULL;
18336
18337 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18338 {
18339 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18340 const char *c_name;
18341 tree prev = clauses;
18342
18343 switch (c_kind)
18344 {
18345 case PRAGMA_OMP_CLAUSE_COPYIN:
18346 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18347 c_name = "copyin";
18348 break;
18349 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18351 clauses);
18352 c_name = "copyprivate";
18353 break;
18354 case PRAGMA_OMP_CLAUSE_DEFAULT:
18355 clauses = cp_parser_omp_clause_default (parser, clauses);
18356 c_name = "default";
18357 break;
18358 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18359 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18360 clauses);
18361 c_name = "firstprivate";
18362 break;
18363 case PRAGMA_OMP_CLAUSE_IF:
18364 clauses = cp_parser_omp_clause_if (parser, clauses);
18365 c_name = "if";
18366 break;
18367 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18368 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18369 clauses);
18370 c_name = "lastprivate";
18371 break;
18372 case PRAGMA_OMP_CLAUSE_NOWAIT:
18373 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18374 c_name = "nowait";
18375 break;
18376 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18377 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18378 c_name = "num_threads";
18379 break;
18380 case PRAGMA_OMP_CLAUSE_ORDERED:
18381 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18382 c_name = "ordered";
18383 break;
18384 case PRAGMA_OMP_CLAUSE_PRIVATE:
18385 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18386 clauses);
18387 c_name = "private";
18388 break;
18389 case PRAGMA_OMP_CLAUSE_REDUCTION:
18390 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18391 c_name = "reduction";
18392 break;
18393 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18394 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18395 c_name = "schedule";
18396 break;
18397 case PRAGMA_OMP_CLAUSE_SHARED:
18398 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18399 clauses);
18400 c_name = "shared";
18401 break;
18402 default:
18403 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18404 goto saw_error;
18405 }
18406
18407 if (((mask >> c_kind) & 1) == 0)
18408 {
18409 /* Remove the invalid clause(s) from the list to avoid
18410 confusing the rest of the compiler. */
18411 clauses = prev;
18412 error ("%qs is not valid for %qs", c_name, where);
18413 }
18414 }
18415 saw_error:
18416 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18417 return finish_omp_clauses (clauses);
18418 }
18419
18420 /* OpenMP 2.5:
18421 structured-block:
18422 statement
18423
18424 In practice, we're also interested in adding the statement to an
18425 outer node. So it is convenient if we work around the fact that
18426 cp_parser_statement calls add_stmt. */
18427
18428 static unsigned
18429 cp_parser_begin_omp_structured_block (cp_parser *parser)
18430 {
18431 unsigned save = parser->in_statement;
18432
18433 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18434 This preserves the "not within loop or switch" style error messages
18435 for nonsense cases like
18436 void foo() {
18437 #pragma omp single
18438 break;
18439 }
18440 */
18441 if (parser->in_statement)
18442 parser->in_statement = IN_OMP_BLOCK;
18443
18444 return save;
18445 }
18446
18447 static void
18448 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18449 {
18450 parser->in_statement = save;
18451 }
18452
18453 static tree
18454 cp_parser_omp_structured_block (cp_parser *parser)
18455 {
18456 tree stmt = begin_omp_structured_block ();
18457 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18458
18459 cp_parser_statement (parser, NULL_TREE, false);
18460
18461 cp_parser_end_omp_structured_block (parser, save);
18462 return finish_omp_structured_block (stmt);
18463 }
18464
18465 /* OpenMP 2.5:
18466 # pragma omp atomic new-line
18467 expression-stmt
18468
18469 expression-stmt:
18470 x binop= expr | x++ | ++x | x-- | --x
18471 binop:
18472 +, *, -, /, &, ^, |, <<, >>
18473
18474 where x is an lvalue expression with scalar type. */
18475
18476 static void
18477 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18478 {
18479 tree lhs, rhs;
18480 enum tree_code code;
18481
18482 cp_parser_require_pragma_eol (parser, pragma_tok);
18483
18484 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18485 /*cast_p=*/false);
18486 switch (TREE_CODE (lhs))
18487 {
18488 case ERROR_MARK:
18489 goto saw_error;
18490
18491 case PREINCREMENT_EXPR:
18492 case POSTINCREMENT_EXPR:
18493 lhs = TREE_OPERAND (lhs, 0);
18494 code = PLUS_EXPR;
18495 rhs = integer_one_node;
18496 break;
18497
18498 case PREDECREMENT_EXPR:
18499 case POSTDECREMENT_EXPR:
18500 lhs = TREE_OPERAND (lhs, 0);
18501 code = MINUS_EXPR;
18502 rhs = integer_one_node;
18503 break;
18504
18505 default:
18506 switch (cp_lexer_peek_token (parser->lexer)->type)
18507 {
18508 case CPP_MULT_EQ:
18509 code = MULT_EXPR;
18510 break;
18511 case CPP_DIV_EQ:
18512 code = TRUNC_DIV_EXPR;
18513 break;
18514 case CPP_PLUS_EQ:
18515 code = PLUS_EXPR;
18516 break;
18517 case CPP_MINUS_EQ:
18518 code = MINUS_EXPR;
18519 break;
18520 case CPP_LSHIFT_EQ:
18521 code = LSHIFT_EXPR;
18522 break;
18523 case CPP_RSHIFT_EQ:
18524 code = RSHIFT_EXPR;
18525 break;
18526 case CPP_AND_EQ:
18527 code = BIT_AND_EXPR;
18528 break;
18529 case CPP_OR_EQ:
18530 code = BIT_IOR_EXPR;
18531 break;
18532 case CPP_XOR_EQ:
18533 code = BIT_XOR_EXPR;
18534 break;
18535 default:
18536 cp_parser_error (parser,
18537 "invalid operator for %<#pragma omp atomic%>");
18538 goto saw_error;
18539 }
18540 cp_lexer_consume_token (parser->lexer);
18541
18542 rhs = cp_parser_expression (parser, false);
18543 if (rhs == error_mark_node)
18544 goto saw_error;
18545 break;
18546 }
18547 finish_omp_atomic (code, lhs, rhs);
18548 cp_parser_consume_semicolon_at_end_of_statement (parser);
18549 return;
18550
18551 saw_error:
18552 cp_parser_skip_to_end_of_block_or_statement (parser);
18553 }
18554
18555
18556 /* OpenMP 2.5:
18557 # pragma omp barrier new-line */
18558
18559 static void
18560 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18561 {
18562 cp_parser_require_pragma_eol (parser, pragma_tok);
18563 finish_omp_barrier ();
18564 }
18565
18566 /* OpenMP 2.5:
18567 # pragma omp critical [(name)] new-line
18568 structured-block */
18569
18570 static tree
18571 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18572 {
18573 tree stmt, name = NULL;
18574
18575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18576 {
18577 cp_lexer_consume_token (parser->lexer);
18578
18579 name = cp_parser_identifier (parser);
18580
18581 if (name == error_mark_node
18582 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18584 /*or_comma=*/false,
18585 /*consume_paren=*/true);
18586 if (name == error_mark_node)
18587 name = NULL;
18588 }
18589 cp_parser_require_pragma_eol (parser, pragma_tok);
18590
18591 stmt = cp_parser_omp_structured_block (parser);
18592 return c_finish_omp_critical (stmt, name);
18593 }
18594
18595 /* OpenMP 2.5:
18596 # pragma omp flush flush-vars[opt] new-line
18597
18598 flush-vars:
18599 ( variable-list ) */
18600
18601 static void
18602 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18603 {
18604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18605 (void) cp_parser_omp_var_list (parser, 0, NULL);
18606 cp_parser_require_pragma_eol (parser, pragma_tok);
18607
18608 finish_omp_flush ();
18609 }
18610
18611 /* Parse the restricted form of the for statment allowed by OpenMP. */
18612
18613 static tree
18614 cp_parser_omp_for_loop (cp_parser *parser)
18615 {
18616 tree init, cond, incr, body, decl, pre_body;
18617 location_t loc;
18618
18619 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18620 {
18621 cp_parser_error (parser, "for statement expected");
18622 return NULL;
18623 }
18624 loc = cp_lexer_consume_token (parser->lexer)->location;
18625 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18626 return NULL;
18627
18628 init = decl = NULL;
18629 pre_body = push_stmt_list ();
18630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18631 {
18632 cp_decl_specifier_seq type_specifiers;
18633
18634 /* First, try to parse as an initialized declaration. See
18635 cp_parser_condition, from whence the bulk of this is copied. */
18636
18637 cp_parser_parse_tentatively (parser);
18638 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18639 &type_specifiers);
18640 if (!cp_parser_error_occurred (parser))
18641 {
18642 tree asm_specification, attributes;
18643 cp_declarator *declarator;
18644
18645 declarator = cp_parser_declarator (parser,
18646 CP_PARSER_DECLARATOR_NAMED,
18647 /*ctor_dtor_or_conv_p=*/NULL,
18648 /*parenthesized_p=*/NULL,
18649 /*member_p=*/false);
18650 attributes = cp_parser_attributes_opt (parser);
18651 asm_specification = cp_parser_asm_specification_opt (parser);
18652
18653 cp_parser_require (parser, CPP_EQ, "`='");
18654 if (cp_parser_parse_definitely (parser))
18655 {
18656 tree pushed_scope;
18657
18658 decl = start_decl (declarator, &type_specifiers,
18659 /*initialized_p=*/false, attributes,
18660 /*prefix_attributes=*/NULL_TREE,
18661 &pushed_scope);
18662
18663 init = cp_parser_assignment_expression (parser, false);
18664
18665 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18666 asm_specification, LOOKUP_ONLYCONVERTING);
18667
18668 if (pushed_scope)
18669 pop_scope (pushed_scope);
18670 }
18671 }
18672 else
18673 cp_parser_abort_tentative_parse (parser);
18674
18675 /* If parsing as an initialized declaration failed, try again as
18676 a simple expression. */
18677 if (decl == NULL)
18678 init = cp_parser_expression (parser, false);
18679 }
18680 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18681 pre_body = pop_stmt_list (pre_body);
18682
18683 cond = NULL;
18684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18685 cond = cp_parser_condition (parser);
18686 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18687
18688 incr = NULL;
18689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18690 incr = cp_parser_expression (parser, false);
18691
18692 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18693 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18694 /*or_comma=*/false,
18695 /*consume_paren=*/true);
18696
18697 /* Note that we saved the original contents of this flag when we entered
18698 the structured block, and so we don't need to re-save it here. */
18699 parser->in_statement = IN_OMP_FOR;
18700
18701 /* Note that the grammar doesn't call for a structured block here,
18702 though the loop as a whole is a structured block. */
18703 body = push_stmt_list ();
18704 cp_parser_statement (parser, NULL_TREE, false);
18705 body = pop_stmt_list (body);
18706
18707 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18708 }
18709
18710 /* OpenMP 2.5:
18711 #pragma omp for for-clause[optseq] new-line
18712 for-loop */
18713
18714 #define OMP_FOR_CLAUSE_MASK \
18715 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18716 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18717 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18718 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18719 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18720 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18721 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18722
18723 static tree
18724 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18725 {
18726 tree clauses, sb, ret;
18727 unsigned int save;
18728
18729 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18730 "#pragma omp for", pragma_tok);
18731
18732 sb = begin_omp_structured_block ();
18733 save = cp_parser_begin_omp_structured_block (parser);
18734
18735 ret = cp_parser_omp_for_loop (parser);
18736 if (ret)
18737 OMP_FOR_CLAUSES (ret) = clauses;
18738
18739 cp_parser_end_omp_structured_block (parser, save);
18740 add_stmt (finish_omp_structured_block (sb));
18741
18742 return ret;
18743 }
18744
18745 /* OpenMP 2.5:
18746 # pragma omp master new-line
18747 structured-block */
18748
18749 static tree
18750 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18751 {
18752 cp_parser_require_pragma_eol (parser, pragma_tok);
18753 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18754 }
18755
18756 /* OpenMP 2.5:
18757 # pragma omp ordered new-line
18758 structured-block */
18759
18760 static tree
18761 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18762 {
18763 cp_parser_require_pragma_eol (parser, pragma_tok);
18764 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18765 }
18766
18767 /* OpenMP 2.5:
18768
18769 section-scope:
18770 { section-sequence }
18771
18772 section-sequence:
18773 section-directive[opt] structured-block
18774 section-sequence section-directive structured-block */
18775
18776 static tree
18777 cp_parser_omp_sections_scope (cp_parser *parser)
18778 {
18779 tree stmt, substmt;
18780 bool error_suppress = false;
18781 cp_token *tok;
18782
18783 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18784 return NULL_TREE;
18785
18786 stmt = push_stmt_list ();
18787
18788 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18789 {
18790 unsigned save;
18791
18792 substmt = begin_omp_structured_block ();
18793 save = cp_parser_begin_omp_structured_block (parser);
18794
18795 while (1)
18796 {
18797 cp_parser_statement (parser, NULL_TREE, false);
18798
18799 tok = cp_lexer_peek_token (parser->lexer);
18800 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18801 break;
18802 if (tok->type == CPP_CLOSE_BRACE)
18803 break;
18804 if (tok->type == CPP_EOF)
18805 break;
18806 }
18807
18808 cp_parser_end_omp_structured_block (parser, save);
18809 substmt = finish_omp_structured_block (substmt);
18810 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18811 add_stmt (substmt);
18812 }
18813
18814 while (1)
18815 {
18816 tok = cp_lexer_peek_token (parser->lexer);
18817 if (tok->type == CPP_CLOSE_BRACE)
18818 break;
18819 if (tok->type == CPP_EOF)
18820 break;
18821
18822 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18823 {
18824 cp_lexer_consume_token (parser->lexer);
18825 cp_parser_require_pragma_eol (parser, tok);
18826 error_suppress = false;
18827 }
18828 else if (!error_suppress)
18829 {
18830 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18831 error_suppress = true;
18832 }
18833
18834 substmt = cp_parser_omp_structured_block (parser);
18835 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18836 add_stmt (substmt);
18837 }
18838 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18839
18840 substmt = pop_stmt_list (stmt);
18841
18842 stmt = make_node (OMP_SECTIONS);
18843 TREE_TYPE (stmt) = void_type_node;
18844 OMP_SECTIONS_BODY (stmt) = substmt;
18845
18846 add_stmt (stmt);
18847 return stmt;
18848 }
18849
18850 /* OpenMP 2.5:
18851 # pragma omp sections sections-clause[optseq] newline
18852 sections-scope */
18853
18854 #define OMP_SECTIONS_CLAUSE_MASK \
18855 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18856 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18857 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18858 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18859 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18860
18861 static tree
18862 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18863 {
18864 tree clauses, ret;
18865
18866 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18867 "#pragma omp sections", pragma_tok);
18868
18869 ret = cp_parser_omp_sections_scope (parser);
18870 if (ret)
18871 OMP_SECTIONS_CLAUSES (ret) = clauses;
18872
18873 return ret;
18874 }
18875
18876 /* OpenMP 2.5:
18877 # pragma parallel parallel-clause new-line
18878 # pragma parallel for parallel-for-clause new-line
18879 # pragma parallel sections parallel-sections-clause new-line */
18880
18881 #define OMP_PARALLEL_CLAUSE_MASK \
18882 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18883 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18884 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18885 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18886 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18887 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18888 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18889 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18890
18891 static tree
18892 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18893 {
18894 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18895 const char *p_name = "#pragma omp parallel";
18896 tree stmt, clauses, par_clause, ws_clause, block;
18897 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18898 unsigned int save;
18899
18900 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18901 {
18902 cp_lexer_consume_token (parser->lexer);
18903 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18904 p_name = "#pragma omp parallel for";
18905 mask |= OMP_FOR_CLAUSE_MASK;
18906 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18907 }
18908 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18909 {
18910 tree id = cp_lexer_peek_token (parser->lexer)->value;
18911 const char *p = IDENTIFIER_POINTER (id);
18912 if (strcmp (p, "sections") == 0)
18913 {
18914 cp_lexer_consume_token (parser->lexer);
18915 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18916 p_name = "#pragma omp parallel sections";
18917 mask |= OMP_SECTIONS_CLAUSE_MASK;
18918 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18919 }
18920 }
18921
18922 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18923 block = begin_omp_parallel ();
18924 save = cp_parser_begin_omp_structured_block (parser);
18925
18926 switch (p_kind)
18927 {
18928 case PRAGMA_OMP_PARALLEL:
18929 cp_parser_already_scoped_statement (parser);
18930 par_clause = clauses;
18931 break;
18932
18933 case PRAGMA_OMP_PARALLEL_FOR:
18934 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18935 stmt = cp_parser_omp_for_loop (parser);
18936 if (stmt)
18937 OMP_FOR_CLAUSES (stmt) = ws_clause;
18938 break;
18939
18940 case PRAGMA_OMP_PARALLEL_SECTIONS:
18941 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18942 stmt = cp_parser_omp_sections_scope (parser);
18943 if (stmt)
18944 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18945 break;
18946
18947 default:
18948 gcc_unreachable ();
18949 }
18950
18951 cp_parser_end_omp_structured_block (parser, save);
18952 stmt = finish_omp_parallel (par_clause, block);
18953 if (p_kind != PRAGMA_OMP_PARALLEL)
18954 OMP_PARALLEL_COMBINED (stmt) = 1;
18955 return stmt;
18956 }
18957
18958 /* OpenMP 2.5:
18959 # pragma omp single single-clause[optseq] new-line
18960 structured-block */
18961
18962 #define OMP_SINGLE_CLAUSE_MASK \
18963 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18964 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18965 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18966 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18967
18968 static tree
18969 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18970 {
18971 tree stmt = make_node (OMP_SINGLE);
18972 TREE_TYPE (stmt) = void_type_node;
18973
18974 OMP_SINGLE_CLAUSES (stmt)
18975 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18976 "#pragma omp single", pragma_tok);
18977 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18978
18979 return add_stmt (stmt);
18980 }
18981
18982 /* OpenMP 2.5:
18983 # pragma omp threadprivate (variable-list) */
18984
18985 static void
18986 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18987 {
18988 tree vars;
18989
18990 vars = cp_parser_omp_var_list (parser, 0, NULL);
18991 cp_parser_require_pragma_eol (parser, pragma_tok);
18992
18993 if (!targetm.have_tls)
18994 sorry ("threadprivate variables not supported in this target");
18995
18996 finish_omp_threadprivate (vars);
18997 }
18998
18999 /* Main entry point to OpenMP statement pragmas. */
19000
19001 static void
19002 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19003 {
19004 tree stmt;
19005
19006 switch (pragma_tok->pragma_kind)
19007 {
19008 case PRAGMA_OMP_ATOMIC:
19009 cp_parser_omp_atomic (parser, pragma_tok);
19010 return;
19011 case PRAGMA_OMP_CRITICAL:
19012 stmt = cp_parser_omp_critical (parser, pragma_tok);
19013 break;
19014 case PRAGMA_OMP_FOR:
19015 stmt = cp_parser_omp_for (parser, pragma_tok);
19016 break;
19017 case PRAGMA_OMP_MASTER:
19018 stmt = cp_parser_omp_master (parser, pragma_tok);
19019 break;
19020 case PRAGMA_OMP_ORDERED:
19021 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19022 break;
19023 case PRAGMA_OMP_PARALLEL:
19024 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19025 break;
19026 case PRAGMA_OMP_SECTIONS:
19027 stmt = cp_parser_omp_sections (parser, pragma_tok);
19028 break;
19029 case PRAGMA_OMP_SINGLE:
19030 stmt = cp_parser_omp_single (parser, pragma_tok);
19031 break;
19032 default:
19033 gcc_unreachable ();
19034 }
19035
19036 if (stmt)
19037 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19038 }
19039 \f
19040 /* The parser. */
19041
19042 static GTY (()) cp_parser *the_parser;
19043
19044 \f
19045 /* Special handling for the first token or line in the file. The first
19046 thing in the file might be #pragma GCC pch_preprocess, which loads a
19047 PCH file, which is a GC collection point. So we need to handle this
19048 first pragma without benefit of an existing lexer structure.
19049
19050 Always returns one token to the caller in *FIRST_TOKEN. This is
19051 either the true first token of the file, or the first token after
19052 the initial pragma. */
19053
19054 static void
19055 cp_parser_initial_pragma (cp_token *first_token)
19056 {
19057 tree name = NULL;
19058
19059 cp_lexer_get_preprocessor_token (NULL, first_token);
19060 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19061 return;
19062
19063 cp_lexer_get_preprocessor_token (NULL, first_token);
19064 if (first_token->type == CPP_STRING)
19065 {
19066 name = first_token->value;
19067
19068 cp_lexer_get_preprocessor_token (NULL, first_token);
19069 if (first_token->type != CPP_PRAGMA_EOL)
19070 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19071 }
19072 else
19073 error ("expected string literal");
19074
19075 /* Skip to the end of the pragma. */
19076 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19077 cp_lexer_get_preprocessor_token (NULL, first_token);
19078
19079 /* Now actually load the PCH file. */
19080 if (name)
19081 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19082
19083 /* Read one more token to return to our caller. We have to do this
19084 after reading the PCH file in, since its pointers have to be
19085 live. */
19086 cp_lexer_get_preprocessor_token (NULL, first_token);
19087 }
19088
19089 /* Normal parsing of a pragma token. Here we can (and must) use the
19090 regular lexer. */
19091
19092 static bool
19093 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19094 {
19095 cp_token *pragma_tok;
19096 unsigned int id;
19097
19098 pragma_tok = cp_lexer_consume_token (parser->lexer);
19099 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19100 parser->lexer->in_pragma = true;
19101
19102 id = pragma_tok->pragma_kind;
19103 switch (id)
19104 {
19105 case PRAGMA_GCC_PCH_PREPROCESS:
19106 error ("%<#pragma GCC pch_preprocess%> must be first");
19107 break;
19108
19109 case PRAGMA_OMP_BARRIER:
19110 switch (context)
19111 {
19112 case pragma_compound:
19113 cp_parser_omp_barrier (parser, pragma_tok);
19114 return false;
19115 case pragma_stmt:
19116 error ("%<#pragma omp barrier%> may only be "
19117 "used in compound statements");
19118 break;
19119 default:
19120 goto bad_stmt;
19121 }
19122 break;
19123
19124 case PRAGMA_OMP_FLUSH:
19125 switch (context)
19126 {
19127 case pragma_compound:
19128 cp_parser_omp_flush (parser, pragma_tok);
19129 return false;
19130 case pragma_stmt:
19131 error ("%<#pragma omp flush%> may only be "
19132 "used in compound statements");
19133 break;
19134 default:
19135 goto bad_stmt;
19136 }
19137 break;
19138
19139 case PRAGMA_OMP_THREADPRIVATE:
19140 cp_parser_omp_threadprivate (parser, pragma_tok);
19141 return false;
19142
19143 case PRAGMA_OMP_ATOMIC:
19144 case PRAGMA_OMP_CRITICAL:
19145 case PRAGMA_OMP_FOR:
19146 case PRAGMA_OMP_MASTER:
19147 case PRAGMA_OMP_ORDERED:
19148 case PRAGMA_OMP_PARALLEL:
19149 case PRAGMA_OMP_SECTIONS:
19150 case PRAGMA_OMP_SINGLE:
19151 if (context == pragma_external)
19152 goto bad_stmt;
19153 cp_parser_omp_construct (parser, pragma_tok);
19154 return true;
19155
19156 case PRAGMA_OMP_SECTION:
19157 error ("%<#pragma omp section%> may only be used in "
19158 "%<#pragma omp sections%> construct");
19159 break;
19160
19161 default:
19162 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19163 c_invoke_pragma_handler (id);
19164 break;
19165
19166 bad_stmt:
19167 cp_parser_error (parser, "expected declaration specifiers");
19168 break;
19169 }
19170
19171 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19172 return false;
19173 }
19174
19175 /* The interface the pragma parsers have to the lexer. */
19176
19177 enum cpp_ttype
19178 pragma_lex (tree *value)
19179 {
19180 cp_token *tok;
19181 enum cpp_ttype ret;
19182
19183 tok = cp_lexer_peek_token (the_parser->lexer);
19184
19185 ret = tok->type;
19186 *value = tok->value;
19187
19188 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19189 ret = CPP_EOF;
19190 else if (ret == CPP_STRING)
19191 *value = cp_parser_string_literal (the_parser, false, false);
19192 else
19193 {
19194 cp_lexer_consume_token (the_parser->lexer);
19195 if (ret == CPP_KEYWORD)
19196 ret = CPP_NAME;
19197 }
19198
19199 return ret;
19200 }
19201
19202 \f
19203 /* External interface. */
19204
19205 /* Parse one entire translation unit. */
19206
19207 void
19208 c_parse_file (void)
19209 {
19210 bool error_occurred;
19211 static bool already_called = false;
19212
19213 if (already_called)
19214 {
19215 sorry ("inter-module optimizations not implemented for C++");
19216 return;
19217 }
19218 already_called = true;
19219
19220 the_parser = cp_parser_new ();
19221 push_deferring_access_checks (flag_access_control
19222 ? dk_no_deferred : dk_no_check);
19223 error_occurred = cp_parser_translation_unit (the_parser);
19224 the_parser = NULL;
19225 }
19226
19227 /* This variable must be provided by every front end. */
19228
19229 int yydebug;
19230
19231 #include "gt-cp-parser.h"