]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
cp-tree.def (STATIC_ASSERT): New.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 /* A C++ token. */
49
50 typedef struct cp_token GTY (())
51 {
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
57 /* Token flags. */
58 unsigned char flags;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The input file stack index at which this token was found. */
70 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71 /* The value associated with this token, if any. */
72 tree value;
73 /* The location at which this token was found. */
74 location_t location;
75 } cp_token;
76
77 /* We use a stack of token pointer for saving token sets. */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
81
82 static const cp_token eof_token =
83 {
84 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
86 0
87 #else
88 {0, 0}
89 #endif
90 };
91
92 /* The cp_lexer structure represents the C++ lexer. It is responsible
93 for managing the token stream from the preprocessor and supplying
94 it to the parser. Tokens are never added to the cp_lexer after
95 it is created. */
96
97 typedef struct cp_lexer GTY (())
98 {
99 /* The memory allocated for the buffer. NULL if this lexer does not
100 own the token buffer. */
101 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102 /* If the lexer owns the buffer, this is the number of tokens in the
103 buffer. */
104 size_t buffer_length;
105
106 /* A pointer just past the last available token. The tokens
107 in this lexer are [buffer, last_token). */
108 cp_token_position GTY ((skip)) last_token;
109
110 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
111 no more available tokens. */
112 cp_token_position GTY ((skip)) next_token;
113
114 /* A stack indicating positions at which cp_lexer_save_tokens was
115 called. The top entry is the most recent position at which we
116 began saving tokens. If the stack is non-empty, we are saving
117 tokens. */
118 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
119
120 /* The next lexer in a linked list of lexers. */
121 struct cp_lexer *next;
122
123 /* True if we should output debugging information. */
124 bool debugging_p;
125
126 /* True if we're in the context of parsing a pragma, and should not
127 increment past the end-of-line marker. */
128 bool in_pragma;
129 } cp_lexer;
130
131 /* cp_token_cache is a range of tokens. There is no need to represent
132 allocate heap memory for it, since tokens are never removed from the
133 lexer's array. There is also no need for the GC to walk through
134 a cp_token_cache, since everything in here is referenced through
135 a lexer. */
136
137 typedef struct cp_token_cache GTY(())
138 {
139 /* The beginning of the token range. */
140 cp_token * GTY((skip)) first;
141
142 /* Points immediately after the last token in the range. */
143 cp_token * GTY ((skip)) last;
144 } cp_token_cache;
145
146 /* Prototypes. */
147
148 static cp_lexer *cp_lexer_new_main
149 (void);
150 static cp_lexer *cp_lexer_new_from_tokens
151 (cp_token_cache *tokens);
152 static void cp_lexer_destroy
153 (cp_lexer *);
154 static int cp_lexer_saving_tokens
155 (const cp_lexer *);
156 static cp_token_position cp_lexer_token_position
157 (cp_lexer *, bool);
158 static cp_token *cp_lexer_token_at
159 (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161 (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
163 (cp_lexer *);
164 static cp_token *cp_lexer_peek_nth_token
165 (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169 (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171 (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
173 (cp_lexer *);
174 static void cp_lexer_purge_token
175 (cp_lexer *);
176 static void cp_lexer_purge_tokens_after
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
179 (cp_lexer *);
180 static void cp_lexer_commit_tokens
181 (cp_lexer *);
182 static void cp_lexer_rollback_tokens
183 (cp_lexer *);
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186 (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
188 (cp_lexer *);
189 static void cp_lexer_start_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192 (cp_lexer *) ATTRIBUTE_UNUSED;
193 #else
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195 about passing NULL to functions that require non-NULL arguments
196 (fputs, fprintf). It will never be used, so all we need is a value
197 of the right type that's guaranteed not to be NULL. */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
202
203 static cp_token_cache *cp_token_cache_new
204 (cp_token *, cp_token *);
205
206 static void cp_parser_initial_pragma
207 (cp_token *);
208
209 /* Manifest constants. */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
212
213 /* A token type for keywords, as opposed to ordinary identifiers. */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
215
216 /* A token type for template-ids. If a template-id is processed while
217 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218 the value of the CPP_TEMPLATE_ID is whatever was returned by
219 cp_parser_template_id. */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
221
222 /* A token type for nested-name-specifiers. If a
223 nested-name-specifier is processed while parsing tentatively, it is
224 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226 cp_parser_nested_name_specifier_opt. */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
228
229 /* A token type for tokens that are not tokens at all; these are used
230 to represent slots in the array where there used to be a token
231 that has now been deleted. */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
233
234 /* The number of token types, including C++-specific ones. */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
236
237 /* Variables. */
238
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
243
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
245 preprocessor. */
246
247 static cp_lexer *
248 cp_lexer_new_main (void)
249 {
250 cp_token first_token;
251 cp_lexer *lexer;
252 cp_token *pos;
253 size_t alloc;
254 size_t space;
255 cp_token *buffer;
256
257 /* It's possible that parsing the first pragma will load a PCH file,
258 which is a GC collection point. So we have to do that before
259 allocating any memory. */
260 cp_parser_initial_pragma (&first_token);
261
262 /* Tell c_lex_with_flags not to merge string constants. */
263 c_lex_return_raw_strings = true;
264
265 c_common_no_more_pch ();
266
267 /* Allocate the memory. */
268 lexer = GGC_CNEW (cp_lexer);
269
270 #ifdef ENABLE_CHECKING
271 /* Initially we are not debugging. */
272 lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275 CP_SAVED_TOKEN_STACK);
276
277 /* Create the buffer. */
278 alloc = CP_LEXER_BUFFER_SIZE;
279 buffer = GGC_NEWVEC (cp_token, alloc);
280
281 /* Put the first token in the buffer. */
282 space = alloc;
283 pos = buffer;
284 *pos = first_token;
285
286 /* Get the remaining tokens from the preprocessor. */
287 while (pos->type != CPP_EOF)
288 {
289 pos++;
290 if (!--space)
291 {
292 space = alloc;
293 alloc *= 2;
294 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295 pos = buffer + space;
296 }
297 cp_lexer_get_preprocessor_token (lexer, pos);
298 }
299 lexer->buffer = buffer;
300 lexer->buffer_length = alloc - space;
301 lexer->last_token = pos;
302 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
303
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
310 return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
328
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
335 #endif
336
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
338 return lexer;
339 }
340
341 /* Frees all resources associated with LEXER. */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346 if (lexer->buffer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
349 ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output. */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359 return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369 return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375 return pos;
376 }
377
378 /* nonzero if we are presently saving tokens. */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN. Return true
387 if we reach EOF. */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 cp_token *token)
392 {
393 static int is_extern_c = 0;
394
395 /* Get a new token from the preprocessor. */
396 token->type
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->input_file_stack_index = input_file_stack_tick;
399 token->keyword = RID_MAX;
400 token->pragma_kind = PRAGMA_NONE;
401 token->in_system_header = in_system_header;
402
403 /* On some systems, some header files are surrounded by an
404 implicit extern "C" block. Set a flag in the token if it
405 comes from such a header. */
406 is_extern_c += pending_lang_change;
407 pending_lang_change = 0;
408 token->implicit_extern_c = is_extern_c > 0;
409
410 /* Check to see if this token is a keyword. */
411 if (token->type == CPP_NAME)
412 {
413 if (C_IS_RESERVED_WORD (token->value))
414 {
415 /* Mark this token as a keyword. */
416 token->type = CPP_KEYWORD;
417 /* Record which keyword. */
418 token->keyword = C_RID_CODE (token->value);
419 /* Update the value. Some keywords are mapped to particular
420 entities, rather than simply having the value of the
421 corresponding IDENTIFIER_NODE. For example, `__const' is
422 mapped to `const'. */
423 token->value = ridpointers[token->keyword];
424 }
425 else
426 {
427 token->ambiguous_p = false;
428 token->keyword = RID_MAX;
429 }
430 }
431 /* Handle Objective-C++ keywords. */
432 else if (token->type == CPP_AT_NAME)
433 {
434 token->type = CPP_KEYWORD;
435 switch (C_RID_CODE (token->value))
436 {
437 /* Map 'class' to '@class', 'private' to '@private', etc. */
438 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442 case RID_THROW: token->keyword = RID_AT_THROW; break;
443 case RID_TRY: token->keyword = RID_AT_TRY; break;
444 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445 default: token->keyword = C_RID_CODE (token->value);
446 }
447 }
448 else if (token->type == CPP_PRAGMA)
449 {
450 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
451 token->pragma_kind = TREE_INT_CST_LOW (token->value);
452 token->value = NULL;
453 }
454 }
455
456 /* Update the globals input_location and in_system_header and the
457 input file stack from TOKEN. */
458 static inline void
459 cp_lexer_set_source_position_from_token (cp_token *token)
460 {
461 if (token->type != CPP_EOF)
462 {
463 input_location = token->location;
464 in_system_header = token->in_system_header;
465 restore_input_file_stack (token->input_file_stack_index);
466 }
467 }
468
469 /* Return a pointer to the next token in the token stream, but do not
470 consume it. */
471
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
474 {
475 if (cp_lexer_debugging_p (lexer))
476 {
477 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479 putc ('\n', cp_lexer_debug_stream);
480 }
481 return lexer->next_token;
482 }
483
484 /* Return true if the next token has the indicated TYPE. */
485
486 static inline bool
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
488 {
489 return cp_lexer_peek_token (lexer)->type == type;
490 }
491
492 /* Return true if the next token does not have the indicated TYPE. */
493
494 static inline bool
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
496 {
497 return !cp_lexer_next_token_is (lexer, type);
498 }
499
500 /* Return true if the next token is the indicated KEYWORD. */
501
502 static inline bool
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
504 {
505 return cp_lexer_peek_token (lexer)->keyword == keyword;
506 }
507
508 /* Return true if the next token is a keyword for a decl-specifier. */
509
510 static bool
511 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
512 {
513 cp_token *token;
514
515 token = cp_lexer_peek_token (lexer);
516 switch (token->keyword)
517 {
518 /* Storage classes. */
519 case RID_AUTO:
520 case RID_REGISTER:
521 case RID_STATIC:
522 case RID_EXTERN:
523 case RID_MUTABLE:
524 case RID_THREAD:
525 /* Elaborated type specifiers. */
526 case RID_ENUM:
527 case RID_CLASS:
528 case RID_STRUCT:
529 case RID_UNION:
530 case RID_TYPENAME:
531 /* Simple type specifiers. */
532 case RID_CHAR:
533 case RID_WCHAR:
534 case RID_BOOL:
535 case RID_SHORT:
536 case RID_INT:
537 case RID_LONG:
538 case RID_SIGNED:
539 case RID_UNSIGNED:
540 case RID_FLOAT:
541 case RID_DOUBLE:
542 case RID_VOID:
543 /* GNU extensions. */
544 case RID_ATTRIBUTE:
545 case RID_TYPEOF:
546 return true;
547
548 default:
549 return false;
550 }
551 }
552
553 /* Return a pointer to the Nth token in the token stream. If N is 1,
554 then this is precisely equivalent to cp_lexer_peek_token (except
555 that it is not inline). One would like to disallow that case, but
556 there is one case (cp_parser_nth_token_starts_template_id) where
557 the caller passes a variable for N and it might be 1. */
558
559 static cp_token *
560 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
561 {
562 cp_token *token;
563
564 /* N is 1-based, not zero-based. */
565 gcc_assert (n > 0);
566
567 if (cp_lexer_debugging_p (lexer))
568 fprintf (cp_lexer_debug_stream,
569 "cp_lexer: peeking ahead %ld at token: ", (long)n);
570
571 --n;
572 token = lexer->next_token;
573 gcc_assert (!n || token != &eof_token);
574 while (n != 0)
575 {
576 ++token;
577 if (token == lexer->last_token)
578 {
579 token = (cp_token *)&eof_token;
580 break;
581 }
582
583 if (token->type != CPP_PURGED)
584 --n;
585 }
586
587 if (cp_lexer_debugging_p (lexer))
588 {
589 cp_lexer_print_token (cp_lexer_debug_stream, token);
590 putc ('\n', cp_lexer_debug_stream);
591 }
592
593 return token;
594 }
595
596 /* Return the next token, and advance the lexer's next_token pointer
597 to point to the next non-purged token. */
598
599 static cp_token *
600 cp_lexer_consume_token (cp_lexer* lexer)
601 {
602 cp_token *token = lexer->next_token;
603
604 gcc_assert (token != &eof_token);
605 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
606
607 do
608 {
609 lexer->next_token++;
610 if (lexer->next_token == lexer->last_token)
611 {
612 lexer->next_token = (cp_token *)&eof_token;
613 break;
614 }
615
616 }
617 while (lexer->next_token->type == CPP_PURGED);
618
619 cp_lexer_set_source_position_from_token (token);
620
621 /* Provide debugging output. */
622 if (cp_lexer_debugging_p (lexer))
623 {
624 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
625 cp_lexer_print_token (cp_lexer_debug_stream, token);
626 putc ('\n', cp_lexer_debug_stream);
627 }
628
629 return token;
630 }
631
632 /* Permanently remove the next token from the token stream, and
633 advance the next_token pointer to refer to the next non-purged
634 token. */
635
636 static void
637 cp_lexer_purge_token (cp_lexer *lexer)
638 {
639 cp_token *tok = lexer->next_token;
640
641 gcc_assert (tok != &eof_token);
642 tok->type = CPP_PURGED;
643 tok->location = UNKNOWN_LOCATION;
644 tok->value = NULL_TREE;
645 tok->keyword = RID_MAX;
646
647 do
648 {
649 tok++;
650 if (tok == lexer->last_token)
651 {
652 tok = (cp_token *)&eof_token;
653 break;
654 }
655 }
656 while (tok->type == CPP_PURGED);
657 lexer->next_token = tok;
658 }
659
660 /* Permanently remove all tokens after TOK, up to, but not
661 including, the token that will be returned next by
662 cp_lexer_peek_token. */
663
664 static void
665 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
666 {
667 cp_token *peek = lexer->next_token;
668
669 if (peek == &eof_token)
670 peek = lexer->last_token;
671
672 gcc_assert (tok < peek);
673
674 for ( tok += 1; tok != peek; tok += 1)
675 {
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->value = NULL_TREE;
679 tok->keyword = RID_MAX;
680 }
681 }
682
683 /* Begin saving tokens. All tokens consumed after this point will be
684 preserved. */
685
686 static void
687 cp_lexer_save_tokens (cp_lexer* lexer)
688 {
689 /* Provide debugging output. */
690 if (cp_lexer_debugging_p (lexer))
691 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
692
693 VEC_safe_push (cp_token_position, heap,
694 lexer->saved_tokens, lexer->next_token);
695 }
696
697 /* Commit to the portion of the token stream most recently saved. */
698
699 static void
700 cp_lexer_commit_tokens (cp_lexer* lexer)
701 {
702 /* Provide debugging output. */
703 if (cp_lexer_debugging_p (lexer))
704 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
705
706 VEC_pop (cp_token_position, lexer->saved_tokens);
707 }
708
709 /* Return all tokens saved since the last call to cp_lexer_save_tokens
710 to the token stream. Stop saving tokens. */
711
712 static void
713 cp_lexer_rollback_tokens (cp_lexer* lexer)
714 {
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer))
717 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
718
719 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
720 }
721
722 /* Print a representation of the TOKEN on the STREAM. */
723
724 #ifdef ENABLE_CHECKING
725
726 static void
727 cp_lexer_print_token (FILE * stream, cp_token *token)
728 {
729 /* We don't use cpp_type2name here because the parser defines
730 a few tokens of its own. */
731 static const char *const token_names[] = {
732 /* cpplib-defined token types */
733 #define OP(e, s) #e,
734 #define TK(e, s) #e,
735 TTYPE_TABLE
736 #undef OP
737 #undef TK
738 /* C++ parser token types - see "Manifest constants", above. */
739 "KEYWORD",
740 "TEMPLATE_ID",
741 "NESTED_NAME_SPECIFIER",
742 "PURGED"
743 };
744
745 /* If we have a name for the token, print it out. Otherwise, we
746 simply give the numeric code. */
747 gcc_assert (token->type < ARRAY_SIZE(token_names));
748 fputs (token_names[token->type], stream);
749
750 /* For some tokens, print the associated data. */
751 switch (token->type)
752 {
753 case CPP_KEYWORD:
754 /* Some keywords have a value that is not an IDENTIFIER_NODE.
755 For example, `struct' is mapped to an INTEGER_CST. */
756 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
757 break;
758 /* else fall through */
759 case CPP_NAME:
760 fputs (IDENTIFIER_POINTER (token->value), stream);
761 break;
762
763 case CPP_STRING:
764 case CPP_WSTRING:
765 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
766 break;
767
768 default:
769 break;
770 }
771 }
772
773 /* Start emitting debugging information. */
774
775 static void
776 cp_lexer_start_debugging (cp_lexer* lexer)
777 {
778 lexer->debugging_p = true;
779 }
780
781 /* Stop emitting debugging information. */
782
783 static void
784 cp_lexer_stop_debugging (cp_lexer* lexer)
785 {
786 lexer->debugging_p = false;
787 }
788
789 #endif /* ENABLE_CHECKING */
790
791 /* Create a new cp_token_cache, representing a range of tokens. */
792
793 static cp_token_cache *
794 cp_token_cache_new (cp_token *first, cp_token *last)
795 {
796 cp_token_cache *cache = GGC_NEW (cp_token_cache);
797 cache->first = first;
798 cache->last = last;
799 return cache;
800 }
801
802 \f
803 /* Decl-specifiers. */
804
805 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
806
807 static void
808 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
809 {
810 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
811 }
812
813 /* Declarators. */
814
815 /* Nothing other than the parser should be creating declarators;
816 declarators are a semi-syntactic representation of C++ entities.
817 Other parts of the front end that need to create entities (like
818 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
819
820 static cp_declarator *make_call_declarator
821 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
822 static cp_declarator *make_array_declarator
823 (cp_declarator *, tree);
824 static cp_declarator *make_pointer_declarator
825 (cp_cv_quals, cp_declarator *);
826 static cp_declarator *make_reference_declarator
827 (cp_cv_quals, cp_declarator *);
828 static cp_parameter_declarator *make_parameter_declarator
829 (cp_decl_specifier_seq *, cp_declarator *, tree);
830 static cp_declarator *make_ptrmem_declarator
831 (cp_cv_quals, tree, cp_declarator *);
832
833 /* An erroneous declarator. */
834 static cp_declarator *cp_error_declarator;
835
836 /* The obstack on which declarators and related data structures are
837 allocated. */
838 static struct obstack declarator_obstack;
839
840 /* Alloc BYTES from the declarator memory pool. */
841
842 static inline void *
843 alloc_declarator (size_t bytes)
844 {
845 return obstack_alloc (&declarator_obstack, bytes);
846 }
847
848 /* Allocate a declarator of the indicated KIND. Clear fields that are
849 common to all declarators. */
850
851 static cp_declarator *
852 make_declarator (cp_declarator_kind kind)
853 {
854 cp_declarator *declarator;
855
856 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
857 declarator->kind = kind;
858 declarator->attributes = NULL_TREE;
859 declarator->declarator = NULL;
860
861 return declarator;
862 }
863
864 /* Make a declarator for a generalized identifier. If
865 QUALIFYING_SCOPE is non-NULL, the identifier is
866 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
867 UNQUALIFIED_NAME. SFK indicates the kind of special function this
868 is, if any. */
869
870 static cp_declarator *
871 make_id_declarator (tree qualifying_scope, tree unqualified_name,
872 special_function_kind sfk)
873 {
874 cp_declarator *declarator;
875
876 /* It is valid to write:
877
878 class C { void f(); };
879 typedef C D;
880 void D::f();
881
882 The standard is not clear about whether `typedef const C D' is
883 legal; as of 2002-09-15 the committee is considering that
884 question. EDG 3.0 allows that syntax. Therefore, we do as
885 well. */
886 if (qualifying_scope && TYPE_P (qualifying_scope))
887 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
888
889 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
890 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
891 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
892
893 declarator = make_declarator (cdk_id);
894 declarator->u.id.qualifying_scope = qualifying_scope;
895 declarator->u.id.unqualified_name = unqualified_name;
896 declarator->u.id.sfk = sfk;
897
898 return declarator;
899 }
900
901 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
902 of modifiers such as const or volatile to apply to the pointer
903 type, represented as identifiers. */
904
905 cp_declarator *
906 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
907 {
908 cp_declarator *declarator;
909
910 declarator = make_declarator (cdk_pointer);
911 declarator->declarator = target;
912 declarator->u.pointer.qualifiers = cv_qualifiers;
913 declarator->u.pointer.class_type = NULL_TREE;
914
915 return declarator;
916 }
917
918 /* Like make_pointer_declarator -- but for references. */
919
920 cp_declarator *
921 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
922 {
923 cp_declarator *declarator;
924
925 declarator = make_declarator (cdk_reference);
926 declarator->declarator = target;
927 declarator->u.pointer.qualifiers = cv_qualifiers;
928 declarator->u.pointer.class_type = NULL_TREE;
929
930 return declarator;
931 }
932
933 /* Like make_pointer_declarator -- but for a pointer to a non-static
934 member of CLASS_TYPE. */
935
936 cp_declarator *
937 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
938 cp_declarator *pointee)
939 {
940 cp_declarator *declarator;
941
942 declarator = make_declarator (cdk_ptrmem);
943 declarator->declarator = pointee;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = class_type;
946
947 return declarator;
948 }
949
950 /* Make a declarator for the function given by TARGET, with the
951 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
952 "const"-qualified member function. The EXCEPTION_SPECIFICATION
953 indicates what exceptions can be thrown. */
954
955 cp_declarator *
956 make_call_declarator (cp_declarator *target,
957 cp_parameter_declarator *parms,
958 cp_cv_quals cv_qualifiers,
959 tree exception_specification)
960 {
961 cp_declarator *declarator;
962
963 declarator = make_declarator (cdk_function);
964 declarator->declarator = target;
965 declarator->u.function.parameters = parms;
966 declarator->u.function.qualifiers = cv_qualifiers;
967 declarator->u.function.exception_specification = exception_specification;
968
969 return declarator;
970 }
971
972 /* Make a declarator for an array of BOUNDS elements, each of which is
973 defined by ELEMENT. */
974
975 cp_declarator *
976 make_array_declarator (cp_declarator *element, tree bounds)
977 {
978 cp_declarator *declarator;
979
980 declarator = make_declarator (cdk_array);
981 declarator->declarator = element;
982 declarator->u.array.bounds = bounds;
983
984 return declarator;
985 }
986
987 cp_parameter_declarator *no_parameters;
988
989 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
990 DECLARATOR and DEFAULT_ARGUMENT. */
991
992 cp_parameter_declarator *
993 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
994 cp_declarator *declarator,
995 tree default_argument)
996 {
997 cp_parameter_declarator *parameter;
998
999 parameter = ((cp_parameter_declarator *)
1000 alloc_declarator (sizeof (cp_parameter_declarator)));
1001 parameter->next = NULL;
1002 if (decl_specifiers)
1003 parameter->decl_specifiers = *decl_specifiers;
1004 else
1005 clear_decl_specs (&parameter->decl_specifiers);
1006 parameter->declarator = declarator;
1007 parameter->default_argument = default_argument;
1008 parameter->ellipsis_p = false;
1009
1010 return parameter;
1011 }
1012
1013 /* Returns true iff DECLARATOR is a declaration for a function. */
1014
1015 static bool
1016 function_declarator_p (const cp_declarator *declarator)
1017 {
1018 while (declarator)
1019 {
1020 if (declarator->kind == cdk_function
1021 && declarator->declarator->kind == cdk_id)
1022 return true;
1023 if (declarator->kind == cdk_id
1024 || declarator->kind == cdk_error)
1025 return false;
1026 declarator = declarator->declarator;
1027 }
1028 return false;
1029 }
1030
1031 /* The parser. */
1032
1033 /* Overview
1034 --------
1035
1036 A cp_parser parses the token stream as specified by the C++
1037 grammar. Its job is purely parsing, not semantic analysis. For
1038 example, the parser breaks the token stream into declarators,
1039 expressions, statements, and other similar syntactic constructs.
1040 It does not check that the types of the expressions on either side
1041 of an assignment-statement are compatible, or that a function is
1042 not declared with a parameter of type `void'.
1043
1044 The parser invokes routines elsewhere in the compiler to perform
1045 semantic analysis and to build up the abstract syntax tree for the
1046 code processed.
1047
1048 The parser (and the template instantiation code, which is, in a
1049 way, a close relative of parsing) are the only parts of the
1050 compiler that should be calling push_scope and pop_scope, or
1051 related functions. The parser (and template instantiation code)
1052 keeps track of what scope is presently active; everything else
1053 should simply honor that. (The code that generates static
1054 initializers may also need to set the scope, in order to check
1055 access control correctly when emitting the initializers.)
1056
1057 Methodology
1058 -----------
1059
1060 The parser is of the standard recursive-descent variety. Upcoming
1061 tokens in the token stream are examined in order to determine which
1062 production to use when parsing a non-terminal. Some C++ constructs
1063 require arbitrary look ahead to disambiguate. For example, it is
1064 impossible, in the general case, to tell whether a statement is an
1065 expression or declaration without scanning the entire statement.
1066 Therefore, the parser is capable of "parsing tentatively." When the
1067 parser is not sure what construct comes next, it enters this mode.
1068 Then, while we attempt to parse the construct, the parser queues up
1069 error messages, rather than issuing them immediately, and saves the
1070 tokens it consumes. If the construct is parsed successfully, the
1071 parser "commits", i.e., it issues any queued error messages and
1072 the tokens that were being preserved are permanently discarded.
1073 If, however, the construct is not parsed successfully, the parser
1074 rolls back its state completely so that it can resume parsing using
1075 a different alternative.
1076
1077 Future Improvements
1078 -------------------
1079
1080 The performance of the parser could probably be improved substantially.
1081 We could often eliminate the need to parse tentatively by looking ahead
1082 a little bit. In some places, this approach might not entirely eliminate
1083 the need to parse tentatively, but it might still speed up the average
1084 case. */
1085
1086 /* Flags that are passed to some parsing functions. These values can
1087 be bitwise-ored together. */
1088
1089 typedef enum cp_parser_flags
1090 {
1091 /* No flags. */
1092 CP_PARSER_FLAGS_NONE = 0x0,
1093 /* The construct is optional. If it is not present, then no error
1094 should be issued. */
1095 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1096 /* When parsing a type-specifier, do not allow user-defined types. */
1097 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1098 } cp_parser_flags;
1099
1100 /* The different kinds of declarators we want to parse. */
1101
1102 typedef enum cp_parser_declarator_kind
1103 {
1104 /* We want an abstract declarator. */
1105 CP_PARSER_DECLARATOR_ABSTRACT,
1106 /* We want a named declarator. */
1107 CP_PARSER_DECLARATOR_NAMED,
1108 /* We don't mind, but the name must be an unqualified-id. */
1109 CP_PARSER_DECLARATOR_EITHER
1110 } cp_parser_declarator_kind;
1111
1112 /* The precedence values used to parse binary expressions. The minimum value
1113 of PREC must be 1, because zero is reserved to quickly discriminate
1114 binary operators from other tokens. */
1115
1116 enum cp_parser_prec
1117 {
1118 PREC_NOT_OPERATOR,
1119 PREC_LOGICAL_OR_EXPRESSION,
1120 PREC_LOGICAL_AND_EXPRESSION,
1121 PREC_INCLUSIVE_OR_EXPRESSION,
1122 PREC_EXCLUSIVE_OR_EXPRESSION,
1123 PREC_AND_EXPRESSION,
1124 PREC_EQUALITY_EXPRESSION,
1125 PREC_RELATIONAL_EXPRESSION,
1126 PREC_SHIFT_EXPRESSION,
1127 PREC_ADDITIVE_EXPRESSION,
1128 PREC_MULTIPLICATIVE_EXPRESSION,
1129 PREC_PM_EXPRESSION,
1130 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1131 };
1132
1133 /* A mapping from a token type to a corresponding tree node type, with a
1134 precedence value. */
1135
1136 typedef struct cp_parser_binary_operations_map_node
1137 {
1138 /* The token type. */
1139 enum cpp_ttype token_type;
1140 /* The corresponding tree code. */
1141 enum tree_code tree_type;
1142 /* The precedence of this operator. */
1143 enum cp_parser_prec prec;
1144 } cp_parser_binary_operations_map_node;
1145
1146 /* The status of a tentative parse. */
1147
1148 typedef enum cp_parser_status_kind
1149 {
1150 /* No errors have occurred. */
1151 CP_PARSER_STATUS_KIND_NO_ERROR,
1152 /* An error has occurred. */
1153 CP_PARSER_STATUS_KIND_ERROR,
1154 /* We are committed to this tentative parse, whether or not an error
1155 has occurred. */
1156 CP_PARSER_STATUS_KIND_COMMITTED
1157 } cp_parser_status_kind;
1158
1159 typedef struct cp_parser_expression_stack_entry
1160 {
1161 tree lhs;
1162 enum tree_code tree_type;
1163 int prec;
1164 } cp_parser_expression_stack_entry;
1165
1166 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1167 entries because precedence levels on the stack are monotonically
1168 increasing. */
1169 typedef struct cp_parser_expression_stack_entry
1170 cp_parser_expression_stack[NUM_PREC_VALUES];
1171
1172 /* Context that is saved and restored when parsing tentatively. */
1173 typedef struct cp_parser_context GTY (())
1174 {
1175 /* If this is a tentative parsing context, the status of the
1176 tentative parse. */
1177 enum cp_parser_status_kind status;
1178 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1179 that are looked up in this context must be looked up both in the
1180 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1181 the context of the containing expression. */
1182 tree object_type;
1183
1184 /* The next parsing context in the stack. */
1185 struct cp_parser_context *next;
1186 } cp_parser_context;
1187
1188 /* Prototypes. */
1189
1190 /* Constructors and destructors. */
1191
1192 static cp_parser_context *cp_parser_context_new
1193 (cp_parser_context *);
1194
1195 /* Class variables. */
1196
1197 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1198
1199 /* The operator-precedence table used by cp_parser_binary_expression.
1200 Transformed into an associative array (binops_by_token) by
1201 cp_parser_new. */
1202
1203 static const cp_parser_binary_operations_map_node binops[] = {
1204 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1205 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1206
1207 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1208 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1209 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1210
1211 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1212 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1213
1214 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1215 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1216
1217 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1218 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1219 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1220 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1221
1222 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1223 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1224
1225 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1226
1227 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1228
1229 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1230
1231 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1232
1233 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1234 };
1235
1236 /* The same as binops, but initialized by cp_parser_new so that
1237 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1238 for speed. */
1239 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1240
1241 /* Constructors and destructors. */
1242
1243 /* Construct a new context. The context below this one on the stack
1244 is given by NEXT. */
1245
1246 static cp_parser_context *
1247 cp_parser_context_new (cp_parser_context* next)
1248 {
1249 cp_parser_context *context;
1250
1251 /* Allocate the storage. */
1252 if (cp_parser_context_free_list != NULL)
1253 {
1254 /* Pull the first entry from the free list. */
1255 context = cp_parser_context_free_list;
1256 cp_parser_context_free_list = context->next;
1257 memset (context, 0, sizeof (*context));
1258 }
1259 else
1260 context = GGC_CNEW (cp_parser_context);
1261
1262 /* No errors have occurred yet in this context. */
1263 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1264 /* If this is not the bottomost context, copy information that we
1265 need from the previous context. */
1266 if (next)
1267 {
1268 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1269 expression, then we are parsing one in this context, too. */
1270 context->object_type = next->object_type;
1271 /* Thread the stack. */
1272 context->next = next;
1273 }
1274
1275 return context;
1276 }
1277
1278 /* The cp_parser structure represents the C++ parser. */
1279
1280 typedef struct cp_parser GTY(())
1281 {
1282 /* The lexer from which we are obtaining tokens. */
1283 cp_lexer *lexer;
1284
1285 /* The scope in which names should be looked up. If NULL_TREE, then
1286 we look up names in the scope that is currently open in the
1287 source program. If non-NULL, this is either a TYPE or
1288 NAMESPACE_DECL for the scope in which we should look. It can
1289 also be ERROR_MARK, when we've parsed a bogus scope.
1290
1291 This value is not cleared automatically after a name is looked
1292 up, so we must be careful to clear it before starting a new look
1293 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1294 will look up `Z' in the scope of `X', rather than the current
1295 scope.) Unfortunately, it is difficult to tell when name lookup
1296 is complete, because we sometimes peek at a token, look it up,
1297 and then decide not to consume it. */
1298 tree scope;
1299
1300 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1301 last lookup took place. OBJECT_SCOPE is used if an expression
1302 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1303 respectively. QUALIFYING_SCOPE is used for an expression of the
1304 form "X::Y"; it refers to X. */
1305 tree object_scope;
1306 tree qualifying_scope;
1307
1308 /* A stack of parsing contexts. All but the bottom entry on the
1309 stack will be tentative contexts.
1310
1311 We parse tentatively in order to determine which construct is in
1312 use in some situations. For example, in order to determine
1313 whether a statement is an expression-statement or a
1314 declaration-statement we parse it tentatively as a
1315 declaration-statement. If that fails, we then reparse the same
1316 token stream as an expression-statement. */
1317 cp_parser_context *context;
1318
1319 /* True if we are parsing GNU C++. If this flag is not set, then
1320 GNU extensions are not recognized. */
1321 bool allow_gnu_extensions_p;
1322
1323 /* TRUE if the `>' token should be interpreted as the greater-than
1324 operator. FALSE if it is the end of a template-id or
1325 template-parameter-list. */
1326 bool greater_than_is_operator_p;
1327
1328 /* TRUE if default arguments are allowed within a parameter list
1329 that starts at this point. FALSE if only a gnu extension makes
1330 them permissible. */
1331 bool default_arg_ok_p;
1332
1333 /* TRUE if we are parsing an integral constant-expression. See
1334 [expr.const] for a precise definition. */
1335 bool integral_constant_expression_p;
1336
1337 /* TRUE if we are parsing an integral constant-expression -- but a
1338 non-constant expression should be permitted as well. This flag
1339 is used when parsing an array bound so that GNU variable-length
1340 arrays are tolerated. */
1341 bool allow_non_integral_constant_expression_p;
1342
1343 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1344 been seen that makes the expression non-constant. */
1345 bool non_integral_constant_expression_p;
1346
1347 /* TRUE if local variable names and `this' are forbidden in the
1348 current context. */
1349 bool local_variables_forbidden_p;
1350
1351 /* TRUE if the declaration we are parsing is part of a
1352 linkage-specification of the form `extern string-literal
1353 declaration'. */
1354 bool in_unbraced_linkage_specification_p;
1355
1356 /* TRUE if we are presently parsing a declarator, after the
1357 direct-declarator. */
1358 bool in_declarator_p;
1359
1360 /* TRUE if we are presently parsing a template-argument-list. */
1361 bool in_template_argument_list_p;
1362
1363 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1364 to IN_OMP_BLOCK if parsing OpenMP structured block and
1365 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1366 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1367 iteration-statement, OpenMP block or loop within that switch. */
1368 #define IN_SWITCH_STMT 1
1369 #define IN_ITERATION_STMT 2
1370 #define IN_OMP_BLOCK 4
1371 #define IN_OMP_FOR 8
1372 unsigned char in_statement;
1373
1374 /* TRUE if we are presently parsing the body of a switch statement.
1375 Note that this doesn't quite overlap with in_statement above.
1376 The difference relates to giving the right sets of error messages:
1377 "case not in switch" vs "break statement used with OpenMP...". */
1378 bool in_switch_statement_p;
1379
1380 /* TRUE if we are parsing a type-id in an expression context. In
1381 such a situation, both "type (expr)" and "type (type)" are valid
1382 alternatives. */
1383 bool in_type_id_in_expr_p;
1384
1385 /* TRUE if we are currently in a header file where declarations are
1386 implicitly extern "C". */
1387 bool implicit_extern_c;
1388
1389 /* TRUE if strings in expressions should be translated to the execution
1390 character set. */
1391 bool translate_strings_p;
1392
1393 /* If non-NULL, then we are parsing a construct where new type
1394 definitions are not permitted. The string stored here will be
1395 issued as an error message if a type is defined. */
1396 const char *type_definition_forbidden_message;
1397
1398 /* A list of lists. The outer list is a stack, used for member
1399 functions of local classes. At each level there are two sub-list,
1400 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1401 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1402 TREE_VALUE's. The functions are chained in reverse declaration
1403 order.
1404
1405 The TREE_PURPOSE sublist contains those functions with default
1406 arguments that need post processing, and the TREE_VALUE sublist
1407 contains those functions with definitions that need post
1408 processing.
1409
1410 These lists can only be processed once the outermost class being
1411 defined is complete. */
1412 tree unparsed_functions_queues;
1413
1414 /* The number of classes whose definitions are currently in
1415 progress. */
1416 unsigned num_classes_being_defined;
1417
1418 /* The number of template parameter lists that apply directly to the
1419 current declaration. */
1420 unsigned num_template_parameter_lists;
1421 } cp_parser;
1422
1423 /* Prototypes. */
1424
1425 /* Constructors and destructors. */
1426
1427 static cp_parser *cp_parser_new
1428 (void);
1429
1430 /* Routines to parse various constructs.
1431
1432 Those that return `tree' will return the error_mark_node (rather
1433 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1434 Sometimes, they will return an ordinary node if error-recovery was
1435 attempted, even though a parse error occurred. So, to check
1436 whether or not a parse error occurred, you should always use
1437 cp_parser_error_occurred. If the construct is optional (indicated
1438 either by an `_opt' in the name of the function that does the
1439 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1440 the construct is not present. */
1441
1442 /* Lexical conventions [gram.lex] */
1443
1444 static tree cp_parser_identifier
1445 (cp_parser *);
1446 static tree cp_parser_string_literal
1447 (cp_parser *, bool, bool);
1448
1449 /* Basic concepts [gram.basic] */
1450
1451 static bool cp_parser_translation_unit
1452 (cp_parser *);
1453
1454 /* Expressions [gram.expr] */
1455
1456 static tree cp_parser_primary_expression
1457 (cp_parser *, bool, bool, bool, cp_id_kind *);
1458 static tree cp_parser_id_expression
1459 (cp_parser *, bool, bool, bool *, bool, bool);
1460 static tree cp_parser_unqualified_id
1461 (cp_parser *, bool, bool, bool, bool);
1462 static tree cp_parser_nested_name_specifier_opt
1463 (cp_parser *, bool, bool, bool, bool);
1464 static tree cp_parser_nested_name_specifier
1465 (cp_parser *, bool, bool, bool, bool);
1466 static tree cp_parser_class_or_namespace_name
1467 (cp_parser *, bool, bool, bool, bool, bool);
1468 static tree cp_parser_postfix_expression
1469 (cp_parser *, bool, bool);
1470 static tree cp_parser_postfix_open_square_expression
1471 (cp_parser *, tree, bool);
1472 static tree cp_parser_postfix_dot_deref_expression
1473 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1474 static tree cp_parser_parenthesized_expression_list
1475 (cp_parser *, bool, bool, bool *);
1476 static void cp_parser_pseudo_destructor_name
1477 (cp_parser *, tree *, tree *);
1478 static tree cp_parser_unary_expression
1479 (cp_parser *, bool, bool);
1480 static enum tree_code cp_parser_unary_operator
1481 (cp_token *);
1482 static tree cp_parser_new_expression
1483 (cp_parser *);
1484 static tree cp_parser_new_placement
1485 (cp_parser *);
1486 static tree cp_parser_new_type_id
1487 (cp_parser *, tree *);
1488 static cp_declarator *cp_parser_new_declarator_opt
1489 (cp_parser *);
1490 static cp_declarator *cp_parser_direct_new_declarator
1491 (cp_parser *);
1492 static tree cp_parser_new_initializer
1493 (cp_parser *);
1494 static tree cp_parser_delete_expression
1495 (cp_parser *);
1496 static tree cp_parser_cast_expression
1497 (cp_parser *, bool, bool);
1498 static tree cp_parser_binary_expression
1499 (cp_parser *, bool);
1500 static tree cp_parser_question_colon_clause
1501 (cp_parser *, tree);
1502 static tree cp_parser_assignment_expression
1503 (cp_parser *, bool);
1504 static enum tree_code cp_parser_assignment_operator_opt
1505 (cp_parser *);
1506 static tree cp_parser_expression
1507 (cp_parser *, bool);
1508 static tree cp_parser_constant_expression
1509 (cp_parser *, bool, bool *);
1510 static tree cp_parser_builtin_offsetof
1511 (cp_parser *);
1512
1513 /* Statements [gram.stmt.stmt] */
1514
1515 static void cp_parser_statement
1516 (cp_parser *, tree, bool);
1517 static void cp_parser_label_for_labeled_statement
1518 (cp_parser *);
1519 static tree cp_parser_expression_statement
1520 (cp_parser *, tree);
1521 static tree cp_parser_compound_statement
1522 (cp_parser *, tree, bool);
1523 static void cp_parser_statement_seq_opt
1524 (cp_parser *, tree);
1525 static tree cp_parser_selection_statement
1526 (cp_parser *);
1527 static tree cp_parser_condition
1528 (cp_parser *);
1529 static tree cp_parser_iteration_statement
1530 (cp_parser *);
1531 static void cp_parser_for_init_statement
1532 (cp_parser *);
1533 static tree cp_parser_jump_statement
1534 (cp_parser *);
1535 static void cp_parser_declaration_statement
1536 (cp_parser *);
1537
1538 static tree cp_parser_implicitly_scoped_statement
1539 (cp_parser *);
1540 static void cp_parser_already_scoped_statement
1541 (cp_parser *);
1542
1543 /* Declarations [gram.dcl.dcl] */
1544
1545 static void cp_parser_declaration_seq_opt
1546 (cp_parser *);
1547 static void cp_parser_declaration
1548 (cp_parser *);
1549 static void cp_parser_block_declaration
1550 (cp_parser *, bool);
1551 static void cp_parser_simple_declaration
1552 (cp_parser *, bool);
1553 static void cp_parser_decl_specifier_seq
1554 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1555 static tree cp_parser_storage_class_specifier_opt
1556 (cp_parser *);
1557 static tree cp_parser_function_specifier_opt
1558 (cp_parser *, cp_decl_specifier_seq *);
1559 static tree cp_parser_type_specifier
1560 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1561 int *, bool *);
1562 static tree cp_parser_simple_type_specifier
1563 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1564 static tree cp_parser_type_name
1565 (cp_parser *);
1566 static tree cp_parser_elaborated_type_specifier
1567 (cp_parser *, bool, bool);
1568 static tree cp_parser_enum_specifier
1569 (cp_parser *);
1570 static void cp_parser_enumerator_list
1571 (cp_parser *, tree);
1572 static void cp_parser_enumerator_definition
1573 (cp_parser *, tree);
1574 static tree cp_parser_namespace_name
1575 (cp_parser *);
1576 static void cp_parser_namespace_definition
1577 (cp_parser *);
1578 static void cp_parser_namespace_body
1579 (cp_parser *);
1580 static tree cp_parser_qualified_namespace_specifier
1581 (cp_parser *);
1582 static void cp_parser_namespace_alias_definition
1583 (cp_parser *);
1584 static bool cp_parser_using_declaration
1585 (cp_parser *, bool);
1586 static void cp_parser_using_directive
1587 (cp_parser *);
1588 static void cp_parser_asm_definition
1589 (cp_parser *);
1590 static void cp_parser_linkage_specification
1591 (cp_parser *);
1592 static void cp_parser_static_assert
1593 (cp_parser *, bool);
1594
1595 /* Declarators [gram.dcl.decl] */
1596
1597 static tree cp_parser_init_declarator
1598 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1599 static cp_declarator *cp_parser_declarator
1600 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1601 static cp_declarator *cp_parser_direct_declarator
1602 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1603 static enum tree_code cp_parser_ptr_operator
1604 (cp_parser *, tree *, cp_cv_quals *);
1605 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1606 (cp_parser *);
1607 static tree cp_parser_declarator_id
1608 (cp_parser *, bool);
1609 static tree cp_parser_type_id
1610 (cp_parser *);
1611 static void cp_parser_type_specifier_seq
1612 (cp_parser *, bool, cp_decl_specifier_seq *);
1613 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1614 (cp_parser *);
1615 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1616 (cp_parser *, bool *);
1617 static cp_parameter_declarator *cp_parser_parameter_declaration
1618 (cp_parser *, bool, bool *);
1619 static void cp_parser_function_body
1620 (cp_parser *);
1621 static tree cp_parser_initializer
1622 (cp_parser *, bool *, bool *);
1623 static tree cp_parser_initializer_clause
1624 (cp_parser *, bool *);
1625 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1626 (cp_parser *, bool *);
1627
1628 static bool cp_parser_ctor_initializer_opt_and_function_body
1629 (cp_parser *);
1630
1631 /* Classes [gram.class] */
1632
1633 static tree cp_parser_class_name
1634 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1635 static tree cp_parser_class_specifier
1636 (cp_parser *);
1637 static tree cp_parser_class_head
1638 (cp_parser *, bool *, tree *);
1639 static enum tag_types cp_parser_class_key
1640 (cp_parser *);
1641 static void cp_parser_member_specification_opt
1642 (cp_parser *);
1643 static void cp_parser_member_declaration
1644 (cp_parser *);
1645 static tree cp_parser_pure_specifier
1646 (cp_parser *);
1647 static tree cp_parser_constant_initializer
1648 (cp_parser *);
1649
1650 /* Derived classes [gram.class.derived] */
1651
1652 static tree cp_parser_base_clause
1653 (cp_parser *);
1654 static tree cp_parser_base_specifier
1655 (cp_parser *);
1656
1657 /* Special member functions [gram.special] */
1658
1659 static tree cp_parser_conversion_function_id
1660 (cp_parser *);
1661 static tree cp_parser_conversion_type_id
1662 (cp_parser *);
1663 static cp_declarator *cp_parser_conversion_declarator_opt
1664 (cp_parser *);
1665 static bool cp_parser_ctor_initializer_opt
1666 (cp_parser *);
1667 static void cp_parser_mem_initializer_list
1668 (cp_parser *);
1669 static tree cp_parser_mem_initializer
1670 (cp_parser *);
1671 static tree cp_parser_mem_initializer_id
1672 (cp_parser *);
1673
1674 /* Overloading [gram.over] */
1675
1676 static tree cp_parser_operator_function_id
1677 (cp_parser *);
1678 static tree cp_parser_operator
1679 (cp_parser *);
1680
1681 /* Templates [gram.temp] */
1682
1683 static void cp_parser_template_declaration
1684 (cp_parser *, bool);
1685 static tree cp_parser_template_parameter_list
1686 (cp_parser *);
1687 static tree cp_parser_template_parameter
1688 (cp_parser *, bool *);
1689 static tree cp_parser_type_parameter
1690 (cp_parser *);
1691 static tree cp_parser_template_id
1692 (cp_parser *, bool, bool, bool);
1693 static tree cp_parser_template_name
1694 (cp_parser *, bool, bool, bool, bool *);
1695 static tree cp_parser_template_argument_list
1696 (cp_parser *);
1697 static tree cp_parser_template_argument
1698 (cp_parser *);
1699 static void cp_parser_explicit_instantiation
1700 (cp_parser *);
1701 static void cp_parser_explicit_specialization
1702 (cp_parser *);
1703
1704 /* Exception handling [gram.exception] */
1705
1706 static tree cp_parser_try_block
1707 (cp_parser *);
1708 static bool cp_parser_function_try_block
1709 (cp_parser *);
1710 static void cp_parser_handler_seq
1711 (cp_parser *);
1712 static void cp_parser_handler
1713 (cp_parser *);
1714 static tree cp_parser_exception_declaration
1715 (cp_parser *);
1716 static tree cp_parser_throw_expression
1717 (cp_parser *);
1718 static tree cp_parser_exception_specification_opt
1719 (cp_parser *);
1720 static tree cp_parser_type_id_list
1721 (cp_parser *);
1722
1723 /* GNU Extensions */
1724
1725 static tree cp_parser_asm_specification_opt
1726 (cp_parser *);
1727 static tree cp_parser_asm_operand_list
1728 (cp_parser *);
1729 static tree cp_parser_asm_clobber_list
1730 (cp_parser *);
1731 static tree cp_parser_attributes_opt
1732 (cp_parser *);
1733 static tree cp_parser_attribute_list
1734 (cp_parser *);
1735 static bool cp_parser_extension_opt
1736 (cp_parser *, int *);
1737 static void cp_parser_label_declaration
1738 (cp_parser *);
1739
1740 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1741 static bool cp_parser_pragma
1742 (cp_parser *, enum pragma_context);
1743
1744 /* Objective-C++ Productions */
1745
1746 static tree cp_parser_objc_message_receiver
1747 (cp_parser *);
1748 static tree cp_parser_objc_message_args
1749 (cp_parser *);
1750 static tree cp_parser_objc_message_expression
1751 (cp_parser *);
1752 static tree cp_parser_objc_encode_expression
1753 (cp_parser *);
1754 static tree cp_parser_objc_defs_expression
1755 (cp_parser *);
1756 static tree cp_parser_objc_protocol_expression
1757 (cp_parser *);
1758 static tree cp_parser_objc_selector_expression
1759 (cp_parser *);
1760 static tree cp_parser_objc_expression
1761 (cp_parser *);
1762 static bool cp_parser_objc_selector_p
1763 (enum cpp_ttype);
1764 static tree cp_parser_objc_selector
1765 (cp_parser *);
1766 static tree cp_parser_objc_protocol_refs_opt
1767 (cp_parser *);
1768 static void cp_parser_objc_declaration
1769 (cp_parser *);
1770 static tree cp_parser_objc_statement
1771 (cp_parser *);
1772
1773 /* Utility Routines */
1774
1775 static tree cp_parser_lookup_name
1776 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1777 static tree cp_parser_lookup_name_simple
1778 (cp_parser *, tree);
1779 static tree cp_parser_maybe_treat_template_as_class
1780 (tree, bool);
1781 static bool cp_parser_check_declarator_template_parameters
1782 (cp_parser *, cp_declarator *);
1783 static bool cp_parser_check_template_parameters
1784 (cp_parser *, unsigned);
1785 static tree cp_parser_simple_cast_expression
1786 (cp_parser *);
1787 static tree cp_parser_global_scope_opt
1788 (cp_parser *, bool);
1789 static bool cp_parser_constructor_declarator_p
1790 (cp_parser *, bool);
1791 static tree cp_parser_function_definition_from_specifiers_and_declarator
1792 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1793 static tree cp_parser_function_definition_after_declarator
1794 (cp_parser *, bool);
1795 static void cp_parser_template_declaration_after_export
1796 (cp_parser *, bool);
1797 static void cp_parser_perform_template_parameter_access_checks
1798 (tree);
1799 static tree cp_parser_single_declaration
1800 (cp_parser *, tree, bool, bool *);
1801 static tree cp_parser_functional_cast
1802 (cp_parser *, tree);
1803 static tree cp_parser_save_member_function_body
1804 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1805 static tree cp_parser_enclosed_template_argument_list
1806 (cp_parser *);
1807 static void cp_parser_save_default_args
1808 (cp_parser *, tree);
1809 static void cp_parser_late_parsing_for_member
1810 (cp_parser *, tree);
1811 static void cp_parser_late_parsing_default_args
1812 (cp_parser *, tree);
1813 static tree cp_parser_sizeof_operand
1814 (cp_parser *, enum rid);
1815 static bool cp_parser_declares_only_class_p
1816 (cp_parser *);
1817 static void cp_parser_set_storage_class
1818 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1819 static void cp_parser_set_decl_spec_type
1820 (cp_decl_specifier_seq *, tree, bool);
1821 static bool cp_parser_friend_p
1822 (const cp_decl_specifier_seq *);
1823 static cp_token *cp_parser_require
1824 (cp_parser *, enum cpp_ttype, const char *);
1825 static cp_token *cp_parser_require_keyword
1826 (cp_parser *, enum rid, const char *);
1827 static bool cp_parser_token_starts_function_definition_p
1828 (cp_token *);
1829 static bool cp_parser_next_token_starts_class_definition_p
1830 (cp_parser *);
1831 static bool cp_parser_next_token_ends_template_argument_p
1832 (cp_parser *);
1833 static bool cp_parser_nth_token_starts_template_argument_list_p
1834 (cp_parser *, size_t);
1835 static enum tag_types cp_parser_token_is_class_key
1836 (cp_token *);
1837 static void cp_parser_check_class_key
1838 (enum tag_types, tree type);
1839 static void cp_parser_check_access_in_redeclaration
1840 (tree type);
1841 static bool cp_parser_optional_template_keyword
1842 (cp_parser *);
1843 static void cp_parser_pre_parsed_nested_name_specifier
1844 (cp_parser *);
1845 static void cp_parser_cache_group
1846 (cp_parser *, enum cpp_ttype, unsigned);
1847 static void cp_parser_parse_tentatively
1848 (cp_parser *);
1849 static void cp_parser_commit_to_tentative_parse
1850 (cp_parser *);
1851 static void cp_parser_abort_tentative_parse
1852 (cp_parser *);
1853 static bool cp_parser_parse_definitely
1854 (cp_parser *);
1855 static inline bool cp_parser_parsing_tentatively
1856 (cp_parser *);
1857 static bool cp_parser_uncommitted_to_tentative_parse_p
1858 (cp_parser *);
1859 static void cp_parser_error
1860 (cp_parser *, const char *);
1861 static void cp_parser_name_lookup_error
1862 (cp_parser *, tree, tree, const char *);
1863 static bool cp_parser_simulate_error
1864 (cp_parser *);
1865 static bool cp_parser_check_type_definition
1866 (cp_parser *);
1867 static void cp_parser_check_for_definition_in_return_type
1868 (cp_declarator *, tree);
1869 static void cp_parser_check_for_invalid_template_id
1870 (cp_parser *, tree);
1871 static bool cp_parser_non_integral_constant_expression
1872 (cp_parser *, const char *);
1873 static void cp_parser_diagnose_invalid_type_name
1874 (cp_parser *, tree, tree);
1875 static bool cp_parser_parse_and_diagnose_invalid_type_name
1876 (cp_parser *);
1877 static int cp_parser_skip_to_closing_parenthesis
1878 (cp_parser *, bool, bool, bool);
1879 static void cp_parser_skip_to_end_of_statement
1880 (cp_parser *);
1881 static void cp_parser_consume_semicolon_at_end_of_statement
1882 (cp_parser *);
1883 static void cp_parser_skip_to_end_of_block_or_statement
1884 (cp_parser *);
1885 static void cp_parser_skip_to_closing_brace
1886 (cp_parser *);
1887 static void cp_parser_skip_to_end_of_template_parameter_list
1888 (cp_parser *);
1889 static void cp_parser_skip_to_pragma_eol
1890 (cp_parser*, cp_token *);
1891 static bool cp_parser_error_occurred
1892 (cp_parser *);
1893 static bool cp_parser_allow_gnu_extensions_p
1894 (cp_parser *);
1895 static bool cp_parser_is_string_literal
1896 (cp_token *);
1897 static bool cp_parser_is_keyword
1898 (cp_token *, enum rid);
1899 static tree cp_parser_make_typename_type
1900 (cp_parser *, tree, tree);
1901
1902 /* Returns nonzero if we are parsing tentatively. */
1903
1904 static inline bool
1905 cp_parser_parsing_tentatively (cp_parser* parser)
1906 {
1907 return parser->context->next != NULL;
1908 }
1909
1910 /* Returns nonzero if TOKEN is a string literal. */
1911
1912 static bool
1913 cp_parser_is_string_literal (cp_token* token)
1914 {
1915 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1916 }
1917
1918 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1919
1920 static bool
1921 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1922 {
1923 return token->keyword == keyword;
1924 }
1925
1926 /* If not parsing tentatively, issue a diagnostic of the form
1927 FILE:LINE: MESSAGE before TOKEN
1928 where TOKEN is the next token in the input stream. MESSAGE
1929 (specified by the caller) is usually of the form "expected
1930 OTHER-TOKEN". */
1931
1932 static void
1933 cp_parser_error (cp_parser* parser, const char* message)
1934 {
1935 if (!cp_parser_simulate_error (parser))
1936 {
1937 cp_token *token = cp_lexer_peek_token (parser->lexer);
1938 /* This diagnostic makes more sense if it is tagged to the line
1939 of the token we just peeked at. */
1940 cp_lexer_set_source_position_from_token (token);
1941
1942 if (token->type == CPP_PRAGMA)
1943 {
1944 error ("%<#pragma%> is not allowed here");
1945 cp_parser_skip_to_pragma_eol (parser, token);
1946 return;
1947 }
1948
1949 c_parse_error (message,
1950 /* Because c_parser_error does not understand
1951 CPP_KEYWORD, keywords are treated like
1952 identifiers. */
1953 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1954 token->value);
1955 }
1956 }
1957
1958 /* Issue an error about name-lookup failing. NAME is the
1959 IDENTIFIER_NODE DECL is the result of
1960 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1961 the thing that we hoped to find. */
1962
1963 static void
1964 cp_parser_name_lookup_error (cp_parser* parser,
1965 tree name,
1966 tree decl,
1967 const char* desired)
1968 {
1969 /* If name lookup completely failed, tell the user that NAME was not
1970 declared. */
1971 if (decl == error_mark_node)
1972 {
1973 if (parser->scope && parser->scope != global_namespace)
1974 error ("%<%D::%D%> has not been declared",
1975 parser->scope, name);
1976 else if (parser->scope == global_namespace)
1977 error ("%<::%D%> has not been declared", name);
1978 else if (parser->object_scope
1979 && !CLASS_TYPE_P (parser->object_scope))
1980 error ("request for member %qD in non-class type %qT",
1981 name, parser->object_scope);
1982 else if (parser->object_scope)
1983 error ("%<%T::%D%> has not been declared",
1984 parser->object_scope, name);
1985 else
1986 error ("%qD has not been declared", name);
1987 }
1988 else if (parser->scope && parser->scope != global_namespace)
1989 error ("%<%D::%D%> %s", parser->scope, name, desired);
1990 else if (parser->scope == global_namespace)
1991 error ("%<::%D%> %s", name, desired);
1992 else
1993 error ("%qD %s", name, desired);
1994 }
1995
1996 /* If we are parsing tentatively, remember that an error has occurred
1997 during this tentative parse. Returns true if the error was
1998 simulated; false if a message should be issued by the caller. */
1999
2000 static bool
2001 cp_parser_simulate_error (cp_parser* parser)
2002 {
2003 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2004 {
2005 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2006 return true;
2007 }
2008 return false;
2009 }
2010
2011 /* Check for repeated decl-specifiers. */
2012
2013 static void
2014 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2015 {
2016 cp_decl_spec ds;
2017
2018 for (ds = ds_first; ds != ds_last; ++ds)
2019 {
2020 unsigned count = decl_specs->specs[(int)ds];
2021 if (count < 2)
2022 continue;
2023 /* The "long" specifier is a special case because of "long long". */
2024 if (ds == ds_long)
2025 {
2026 if (count > 2)
2027 error ("%<long long long%> is too long for GCC");
2028 else if (pedantic && !in_system_header && warn_long_long)
2029 pedwarn ("ISO C++ does not support %<long long%>");
2030 }
2031 else if (count > 1)
2032 {
2033 static const char *const decl_spec_names[] = {
2034 "signed",
2035 "unsigned",
2036 "short",
2037 "long",
2038 "const",
2039 "volatile",
2040 "restrict",
2041 "inline",
2042 "virtual",
2043 "explicit",
2044 "friend",
2045 "typedef",
2046 "__complex",
2047 "__thread"
2048 };
2049 error ("duplicate %qs", decl_spec_names[(int)ds]);
2050 }
2051 }
2052 }
2053
2054 /* This function is called when a type is defined. If type
2055 definitions are forbidden at this point, an error message is
2056 issued. */
2057
2058 static bool
2059 cp_parser_check_type_definition (cp_parser* parser)
2060 {
2061 /* If types are forbidden here, issue a message. */
2062 if (parser->type_definition_forbidden_message)
2063 {
2064 /* Use `%s' to print the string in case there are any escape
2065 characters in the message. */
2066 error ("%s", parser->type_definition_forbidden_message);
2067 return false;
2068 }
2069 return true;
2070 }
2071
2072 /* This function is called when the DECLARATOR is processed. The TYPE
2073 was a type defined in the decl-specifiers. If it is invalid to
2074 define a type in the decl-specifiers for DECLARATOR, an error is
2075 issued. */
2076
2077 static void
2078 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2079 tree type)
2080 {
2081 /* [dcl.fct] forbids type definitions in return types.
2082 Unfortunately, it's not easy to know whether or not we are
2083 processing a return type until after the fact. */
2084 while (declarator
2085 && (declarator->kind == cdk_pointer
2086 || declarator->kind == cdk_reference
2087 || declarator->kind == cdk_ptrmem))
2088 declarator = declarator->declarator;
2089 if (declarator
2090 && declarator->kind == cdk_function)
2091 {
2092 error ("new types may not be defined in a return type");
2093 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2094 type);
2095 }
2096 }
2097
2098 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2099 "<" in any valid C++ program. If the next token is indeed "<",
2100 issue a message warning the user about what appears to be an
2101 invalid attempt to form a template-id. */
2102
2103 static void
2104 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2105 tree type)
2106 {
2107 cp_token_position start = 0;
2108
2109 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2110 {
2111 if (TYPE_P (type))
2112 error ("%qT is not a template", type);
2113 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2114 error ("%qE is not a template", type);
2115 else
2116 error ("invalid template-id");
2117 /* Remember the location of the invalid "<". */
2118 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2119 start = cp_lexer_token_position (parser->lexer, true);
2120 /* Consume the "<". */
2121 cp_lexer_consume_token (parser->lexer);
2122 /* Parse the template arguments. */
2123 cp_parser_enclosed_template_argument_list (parser);
2124 /* Permanently remove the invalid template arguments so that
2125 this error message is not issued again. */
2126 if (start)
2127 cp_lexer_purge_tokens_after (parser->lexer, start);
2128 }
2129 }
2130
2131 /* If parsing an integral constant-expression, issue an error message
2132 about the fact that THING appeared and return true. Otherwise,
2133 return false. In either case, set
2134 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2135
2136 static bool
2137 cp_parser_non_integral_constant_expression (cp_parser *parser,
2138 const char *thing)
2139 {
2140 parser->non_integral_constant_expression_p = true;
2141 if (parser->integral_constant_expression_p)
2142 {
2143 if (!parser->allow_non_integral_constant_expression_p)
2144 {
2145 error ("%s cannot appear in a constant-expression", thing);
2146 return true;
2147 }
2148 }
2149 return false;
2150 }
2151
2152 /* Emit a diagnostic for an invalid type name. SCOPE is the
2153 qualifying scope (or NULL, if none) for ID. This function commits
2154 to the current active tentative parse, if any. (Otherwise, the
2155 problematic construct might be encountered again later, resulting
2156 in duplicate error messages.) */
2157
2158 static void
2159 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2160 {
2161 tree decl, old_scope;
2162 /* Try to lookup the identifier. */
2163 old_scope = parser->scope;
2164 parser->scope = scope;
2165 decl = cp_parser_lookup_name_simple (parser, id);
2166 parser->scope = old_scope;
2167 /* If the lookup found a template-name, it means that the user forgot
2168 to specify an argument list. Emit a useful error message. */
2169 if (TREE_CODE (decl) == TEMPLATE_DECL)
2170 error ("invalid use of template-name %qE without an argument list", decl);
2171 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2172 error ("invalid use of destructor %qD as a type", id);
2173 else if (TREE_CODE (decl) == TYPE_DECL)
2174 /* Something like 'unsigned A a;' */
2175 error ("invalid combination of multiple type-specifiers");
2176 else if (!parser->scope)
2177 {
2178 /* Issue an error message. */
2179 error ("%qE does not name a type", id);
2180 /* If we're in a template class, it's possible that the user was
2181 referring to a type from a base class. For example:
2182
2183 template <typename T> struct A { typedef T X; };
2184 template <typename T> struct B : public A<T> { X x; };
2185
2186 The user should have said "typename A<T>::X". */
2187 if (processing_template_decl && current_class_type
2188 && TYPE_BINFO (current_class_type))
2189 {
2190 tree b;
2191
2192 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2193 b;
2194 b = TREE_CHAIN (b))
2195 {
2196 tree base_type = BINFO_TYPE (b);
2197 if (CLASS_TYPE_P (base_type)
2198 && dependent_type_p (base_type))
2199 {
2200 tree field;
2201 /* Go from a particular instantiation of the
2202 template (which will have an empty TYPE_FIELDs),
2203 to the main version. */
2204 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2205 for (field = TYPE_FIELDS (base_type);
2206 field;
2207 field = TREE_CHAIN (field))
2208 if (TREE_CODE (field) == TYPE_DECL
2209 && DECL_NAME (field) == id)
2210 {
2211 inform ("(perhaps %<typename %T::%E%> was intended)",
2212 BINFO_TYPE (b), id);
2213 break;
2214 }
2215 if (field)
2216 break;
2217 }
2218 }
2219 }
2220 }
2221 /* Here we diagnose qualified-ids where the scope is actually correct,
2222 but the identifier does not resolve to a valid type name. */
2223 else if (parser->scope != error_mark_node)
2224 {
2225 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2226 error ("%qE in namespace %qE does not name a type",
2227 id, parser->scope);
2228 else if (TYPE_P (parser->scope))
2229 error ("%qE in class %qT does not name a type", id, parser->scope);
2230 else
2231 gcc_unreachable ();
2232 }
2233 cp_parser_commit_to_tentative_parse (parser);
2234 }
2235
2236 /* Check for a common situation where a type-name should be present,
2237 but is not, and issue a sensible error message. Returns true if an
2238 invalid type-name was detected.
2239
2240 The situation handled by this function are variable declarations of the
2241 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2242 Usually, `ID' should name a type, but if we got here it means that it
2243 does not. We try to emit the best possible error message depending on
2244 how exactly the id-expression looks like. */
2245
2246 static bool
2247 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2248 {
2249 tree id;
2250
2251 cp_parser_parse_tentatively (parser);
2252 id = cp_parser_id_expression (parser,
2253 /*template_keyword_p=*/false,
2254 /*check_dependency_p=*/true,
2255 /*template_p=*/NULL,
2256 /*declarator_p=*/true,
2257 /*optional_p=*/false);
2258 /* After the id-expression, there should be a plain identifier,
2259 otherwise this is not a simple variable declaration. Also, if
2260 the scope is dependent, we cannot do much. */
2261 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2262 || (parser->scope && TYPE_P (parser->scope)
2263 && dependent_type_p (parser->scope)))
2264 {
2265 cp_parser_abort_tentative_parse (parser);
2266 return false;
2267 }
2268 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2269 return false;
2270
2271 /* Emit a diagnostic for the invalid type. */
2272 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2273 /* Skip to the end of the declaration; there's no point in
2274 trying to process it. */
2275 cp_parser_skip_to_end_of_block_or_statement (parser);
2276 return true;
2277 }
2278
2279 /* Consume tokens up to, and including, the next non-nested closing `)'.
2280 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2281 are doing error recovery. Returns -1 if OR_COMMA is true and we
2282 found an unnested comma. */
2283
2284 static int
2285 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2286 bool recovering,
2287 bool or_comma,
2288 bool consume_paren)
2289 {
2290 unsigned paren_depth = 0;
2291 unsigned brace_depth = 0;
2292
2293 if (recovering && !or_comma
2294 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2295 return 0;
2296
2297 while (true)
2298 {
2299 cp_token * token = cp_lexer_peek_token (parser->lexer);
2300
2301 switch (token->type)
2302 {
2303 case CPP_EOF:
2304 case CPP_PRAGMA_EOL:
2305 /* If we've run out of tokens, then there is no closing `)'. */
2306 return 0;
2307
2308 case CPP_SEMICOLON:
2309 /* This matches the processing in skip_to_end_of_statement. */
2310 if (!brace_depth)
2311 return 0;
2312 break;
2313
2314 case CPP_OPEN_BRACE:
2315 ++brace_depth;
2316 break;
2317 case CPP_CLOSE_BRACE:
2318 if (!brace_depth--)
2319 return 0;
2320 break;
2321
2322 case CPP_COMMA:
2323 if (recovering && or_comma && !brace_depth && !paren_depth)
2324 return -1;
2325 break;
2326
2327 case CPP_OPEN_PAREN:
2328 if (!brace_depth)
2329 ++paren_depth;
2330 break;
2331
2332 case CPP_CLOSE_PAREN:
2333 if (!brace_depth && !paren_depth--)
2334 {
2335 if (consume_paren)
2336 cp_lexer_consume_token (parser->lexer);
2337 return 1;
2338 }
2339 break;
2340
2341 default:
2342 break;
2343 }
2344
2345 /* Consume the token. */
2346 cp_lexer_consume_token (parser->lexer);
2347 }
2348 }
2349
2350 /* Consume tokens until we reach the end of the current statement.
2351 Normally, that will be just before consuming a `;'. However, if a
2352 non-nested `}' comes first, then we stop before consuming that. */
2353
2354 static void
2355 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2356 {
2357 unsigned nesting_depth = 0;
2358
2359 while (true)
2360 {
2361 cp_token *token = cp_lexer_peek_token (parser->lexer);
2362
2363 switch (token->type)
2364 {
2365 case CPP_EOF:
2366 case CPP_PRAGMA_EOL:
2367 /* If we've run out of tokens, stop. */
2368 return;
2369
2370 case CPP_SEMICOLON:
2371 /* If the next token is a `;', we have reached the end of the
2372 statement. */
2373 if (!nesting_depth)
2374 return;
2375 break;
2376
2377 case CPP_CLOSE_BRACE:
2378 /* If this is a non-nested '}', stop before consuming it.
2379 That way, when confronted with something like:
2380
2381 { 3 + }
2382
2383 we stop before consuming the closing '}', even though we
2384 have not yet reached a `;'. */
2385 if (nesting_depth == 0)
2386 return;
2387
2388 /* If it is the closing '}' for a block that we have
2389 scanned, stop -- but only after consuming the token.
2390 That way given:
2391
2392 void f g () { ... }
2393 typedef int I;
2394
2395 we will stop after the body of the erroneously declared
2396 function, but before consuming the following `typedef'
2397 declaration. */
2398 if (--nesting_depth == 0)
2399 {
2400 cp_lexer_consume_token (parser->lexer);
2401 return;
2402 }
2403
2404 case CPP_OPEN_BRACE:
2405 ++nesting_depth;
2406 break;
2407
2408 default:
2409 break;
2410 }
2411
2412 /* Consume the token. */
2413 cp_lexer_consume_token (parser->lexer);
2414 }
2415 }
2416
2417 /* This function is called at the end of a statement or declaration.
2418 If the next token is a semicolon, it is consumed; otherwise, error
2419 recovery is attempted. */
2420
2421 static void
2422 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2423 {
2424 /* Look for the trailing `;'. */
2425 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2426 {
2427 /* If there is additional (erroneous) input, skip to the end of
2428 the statement. */
2429 cp_parser_skip_to_end_of_statement (parser);
2430 /* If the next token is now a `;', consume it. */
2431 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2432 cp_lexer_consume_token (parser->lexer);
2433 }
2434 }
2435
2436 /* Skip tokens until we have consumed an entire block, or until we
2437 have consumed a non-nested `;'. */
2438
2439 static void
2440 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2441 {
2442 int nesting_depth = 0;
2443
2444 while (nesting_depth >= 0)
2445 {
2446 cp_token *token = cp_lexer_peek_token (parser->lexer);
2447
2448 switch (token->type)
2449 {
2450 case CPP_EOF:
2451 case CPP_PRAGMA_EOL:
2452 /* If we've run out of tokens, stop. */
2453 return;
2454
2455 case CPP_SEMICOLON:
2456 /* Stop if this is an unnested ';'. */
2457 if (!nesting_depth)
2458 nesting_depth = -1;
2459 break;
2460
2461 case CPP_CLOSE_BRACE:
2462 /* Stop if this is an unnested '}', or closes the outermost
2463 nesting level. */
2464 nesting_depth--;
2465 if (!nesting_depth)
2466 nesting_depth = -1;
2467 break;
2468
2469 case CPP_OPEN_BRACE:
2470 /* Nest. */
2471 nesting_depth++;
2472 break;
2473
2474 default:
2475 break;
2476 }
2477
2478 /* Consume the token. */
2479 cp_lexer_consume_token (parser->lexer);
2480 }
2481 }
2482
2483 /* Skip tokens until a non-nested closing curly brace is the next
2484 token. */
2485
2486 static void
2487 cp_parser_skip_to_closing_brace (cp_parser *parser)
2488 {
2489 unsigned nesting_depth = 0;
2490
2491 while (true)
2492 {
2493 cp_token *token = cp_lexer_peek_token (parser->lexer);
2494
2495 switch (token->type)
2496 {
2497 case CPP_EOF:
2498 case CPP_PRAGMA_EOL:
2499 /* If we've run out of tokens, stop. */
2500 return;
2501
2502 case CPP_CLOSE_BRACE:
2503 /* If the next token is a non-nested `}', then we have reached
2504 the end of the current block. */
2505 if (nesting_depth-- == 0)
2506 return;
2507 break;
2508
2509 case CPP_OPEN_BRACE:
2510 /* If it the next token is a `{', then we are entering a new
2511 block. Consume the entire block. */
2512 ++nesting_depth;
2513 break;
2514
2515 default:
2516 break;
2517 }
2518
2519 /* Consume the token. */
2520 cp_lexer_consume_token (parser->lexer);
2521 }
2522 }
2523
2524 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2525 parameter is the PRAGMA token, allowing us to purge the entire pragma
2526 sequence. */
2527
2528 static void
2529 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2530 {
2531 cp_token *token;
2532
2533 parser->lexer->in_pragma = false;
2534
2535 do
2536 token = cp_lexer_consume_token (parser->lexer);
2537 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2538
2539 /* Ensure that the pragma is not parsed again. */
2540 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2541 }
2542
2543 /* Require pragma end of line, resyncing with it as necessary. The
2544 arguments are as for cp_parser_skip_to_pragma_eol. */
2545
2546 static void
2547 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2548 {
2549 parser->lexer->in_pragma = false;
2550 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2551 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2552 }
2553
2554 /* This is a simple wrapper around make_typename_type. When the id is
2555 an unresolved identifier node, we can provide a superior diagnostic
2556 using cp_parser_diagnose_invalid_type_name. */
2557
2558 static tree
2559 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2560 {
2561 tree result;
2562 if (TREE_CODE (id) == IDENTIFIER_NODE)
2563 {
2564 result = make_typename_type (scope, id, typename_type,
2565 /*complain=*/tf_none);
2566 if (result == error_mark_node)
2567 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2568 return result;
2569 }
2570 return make_typename_type (scope, id, typename_type, tf_error);
2571 }
2572
2573
2574 /* Create a new C++ parser. */
2575
2576 static cp_parser *
2577 cp_parser_new (void)
2578 {
2579 cp_parser *parser;
2580 cp_lexer *lexer;
2581 unsigned i;
2582
2583 /* cp_lexer_new_main is called before calling ggc_alloc because
2584 cp_lexer_new_main might load a PCH file. */
2585 lexer = cp_lexer_new_main ();
2586
2587 /* Initialize the binops_by_token so that we can get the tree
2588 directly from the token. */
2589 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2590 binops_by_token[binops[i].token_type] = binops[i];
2591
2592 parser = GGC_CNEW (cp_parser);
2593 parser->lexer = lexer;
2594 parser->context = cp_parser_context_new (NULL);
2595
2596 /* For now, we always accept GNU extensions. */
2597 parser->allow_gnu_extensions_p = 1;
2598
2599 /* The `>' token is a greater-than operator, not the end of a
2600 template-id. */
2601 parser->greater_than_is_operator_p = true;
2602
2603 parser->default_arg_ok_p = true;
2604
2605 /* We are not parsing a constant-expression. */
2606 parser->integral_constant_expression_p = false;
2607 parser->allow_non_integral_constant_expression_p = false;
2608 parser->non_integral_constant_expression_p = false;
2609
2610 /* Local variable names are not forbidden. */
2611 parser->local_variables_forbidden_p = false;
2612
2613 /* We are not processing an `extern "C"' declaration. */
2614 parser->in_unbraced_linkage_specification_p = false;
2615
2616 /* We are not processing a declarator. */
2617 parser->in_declarator_p = false;
2618
2619 /* We are not processing a template-argument-list. */
2620 parser->in_template_argument_list_p = false;
2621
2622 /* We are not in an iteration statement. */
2623 parser->in_statement = 0;
2624
2625 /* We are not in a switch statement. */
2626 parser->in_switch_statement_p = false;
2627
2628 /* We are not parsing a type-id inside an expression. */
2629 parser->in_type_id_in_expr_p = false;
2630
2631 /* Declarations aren't implicitly extern "C". */
2632 parser->implicit_extern_c = false;
2633
2634 /* String literals should be translated to the execution character set. */
2635 parser->translate_strings_p = true;
2636
2637 /* The unparsed function queue is empty. */
2638 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2639
2640 /* There are no classes being defined. */
2641 parser->num_classes_being_defined = 0;
2642
2643 /* No template parameters apply. */
2644 parser->num_template_parameter_lists = 0;
2645
2646 return parser;
2647 }
2648
2649 /* Create a cp_lexer structure which will emit the tokens in CACHE
2650 and push it onto the parser's lexer stack. This is used for delayed
2651 parsing of in-class method bodies and default arguments, and should
2652 not be confused with tentative parsing. */
2653 static void
2654 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2655 {
2656 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2657 lexer->next = parser->lexer;
2658 parser->lexer = lexer;
2659
2660 /* Move the current source position to that of the first token in the
2661 new lexer. */
2662 cp_lexer_set_source_position_from_token (lexer->next_token);
2663 }
2664
2665 /* Pop the top lexer off the parser stack. This is never used for the
2666 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2667 static void
2668 cp_parser_pop_lexer (cp_parser *parser)
2669 {
2670 cp_lexer *lexer = parser->lexer;
2671 parser->lexer = lexer->next;
2672 cp_lexer_destroy (lexer);
2673
2674 /* Put the current source position back where it was before this
2675 lexer was pushed. */
2676 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2677 }
2678
2679 /* Lexical conventions [gram.lex] */
2680
2681 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2682 identifier. */
2683
2684 static tree
2685 cp_parser_identifier (cp_parser* parser)
2686 {
2687 cp_token *token;
2688
2689 /* Look for the identifier. */
2690 token = cp_parser_require (parser, CPP_NAME, "identifier");
2691 /* Return the value. */
2692 return token ? token->value : error_mark_node;
2693 }
2694
2695 /* Parse a sequence of adjacent string constants. Returns a
2696 TREE_STRING representing the combined, nul-terminated string
2697 constant. If TRANSLATE is true, translate the string to the
2698 execution character set. If WIDE_OK is true, a wide string is
2699 invalid here.
2700
2701 C++98 [lex.string] says that if a narrow string literal token is
2702 adjacent to a wide string literal token, the behavior is undefined.
2703 However, C99 6.4.5p4 says that this results in a wide string literal.
2704 We follow C99 here, for consistency with the C front end.
2705
2706 This code is largely lifted from lex_string() in c-lex.c.
2707
2708 FUTURE: ObjC++ will need to handle @-strings here. */
2709 static tree
2710 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2711 {
2712 tree value;
2713 bool wide = false;
2714 size_t count;
2715 struct obstack str_ob;
2716 cpp_string str, istr, *strs;
2717 cp_token *tok;
2718
2719 tok = cp_lexer_peek_token (parser->lexer);
2720 if (!cp_parser_is_string_literal (tok))
2721 {
2722 cp_parser_error (parser, "expected string-literal");
2723 return error_mark_node;
2724 }
2725
2726 /* Try to avoid the overhead of creating and destroying an obstack
2727 for the common case of just one string. */
2728 if (!cp_parser_is_string_literal
2729 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2730 {
2731 cp_lexer_consume_token (parser->lexer);
2732
2733 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2734 str.len = TREE_STRING_LENGTH (tok->value);
2735 count = 1;
2736 if (tok->type == CPP_WSTRING)
2737 wide = true;
2738
2739 strs = &str;
2740 }
2741 else
2742 {
2743 gcc_obstack_init (&str_ob);
2744 count = 0;
2745
2746 do
2747 {
2748 cp_lexer_consume_token (parser->lexer);
2749 count++;
2750 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2751 str.len = TREE_STRING_LENGTH (tok->value);
2752 if (tok->type == CPP_WSTRING)
2753 wide = true;
2754
2755 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2756
2757 tok = cp_lexer_peek_token (parser->lexer);
2758 }
2759 while (cp_parser_is_string_literal (tok));
2760
2761 strs = (cpp_string *) obstack_finish (&str_ob);
2762 }
2763
2764 if (wide && !wide_ok)
2765 {
2766 cp_parser_error (parser, "a wide string is invalid in this context");
2767 wide = false;
2768 }
2769
2770 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2771 (parse_in, strs, count, &istr, wide))
2772 {
2773 value = build_string (istr.len, (char *)istr.text);
2774 free ((void *)istr.text);
2775
2776 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2777 value = fix_string_type (value);
2778 }
2779 else
2780 /* cpp_interpret_string has issued an error. */
2781 value = error_mark_node;
2782
2783 if (count > 1)
2784 obstack_free (&str_ob, 0);
2785
2786 return value;
2787 }
2788
2789
2790 /* Basic concepts [gram.basic] */
2791
2792 /* Parse a translation-unit.
2793
2794 translation-unit:
2795 declaration-seq [opt]
2796
2797 Returns TRUE if all went well. */
2798
2799 static bool
2800 cp_parser_translation_unit (cp_parser* parser)
2801 {
2802 /* The address of the first non-permanent object on the declarator
2803 obstack. */
2804 static void *declarator_obstack_base;
2805
2806 bool success;
2807
2808 /* Create the declarator obstack, if necessary. */
2809 if (!cp_error_declarator)
2810 {
2811 gcc_obstack_init (&declarator_obstack);
2812 /* Create the error declarator. */
2813 cp_error_declarator = make_declarator (cdk_error);
2814 /* Create the empty parameter list. */
2815 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2816 /* Remember where the base of the declarator obstack lies. */
2817 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2818 }
2819
2820 cp_parser_declaration_seq_opt (parser);
2821
2822 /* If there are no tokens left then all went well. */
2823 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2824 {
2825 /* Get rid of the token array; we don't need it any more. */
2826 cp_lexer_destroy (parser->lexer);
2827 parser->lexer = NULL;
2828
2829 /* This file might have been a context that's implicitly extern
2830 "C". If so, pop the lang context. (Only relevant for PCH.) */
2831 if (parser->implicit_extern_c)
2832 {
2833 pop_lang_context ();
2834 parser->implicit_extern_c = false;
2835 }
2836
2837 /* Finish up. */
2838 finish_translation_unit ();
2839
2840 success = true;
2841 }
2842 else
2843 {
2844 cp_parser_error (parser, "expected declaration");
2845 success = false;
2846 }
2847
2848 /* Make sure the declarator obstack was fully cleaned up. */
2849 gcc_assert (obstack_next_free (&declarator_obstack)
2850 == declarator_obstack_base);
2851
2852 /* All went well. */
2853 return success;
2854 }
2855
2856 /* Expressions [gram.expr] */
2857
2858 /* Parse a primary-expression.
2859
2860 primary-expression:
2861 literal
2862 this
2863 ( expression )
2864 id-expression
2865
2866 GNU Extensions:
2867
2868 primary-expression:
2869 ( compound-statement )
2870 __builtin_va_arg ( assignment-expression , type-id )
2871 __builtin_offsetof ( type-id , offsetof-expression )
2872
2873 Objective-C++ Extension:
2874
2875 primary-expression:
2876 objc-expression
2877
2878 literal:
2879 __null
2880
2881 ADDRESS_P is true iff this expression was immediately preceded by
2882 "&" and therefore might denote a pointer-to-member. CAST_P is true
2883 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2884 true iff this expression is a template argument.
2885
2886 Returns a representation of the expression. Upon return, *IDK
2887 indicates what kind of id-expression (if any) was present. */
2888
2889 static tree
2890 cp_parser_primary_expression (cp_parser *parser,
2891 bool address_p,
2892 bool cast_p,
2893 bool template_arg_p,
2894 cp_id_kind *idk)
2895 {
2896 cp_token *token;
2897
2898 /* Assume the primary expression is not an id-expression. */
2899 *idk = CP_ID_KIND_NONE;
2900
2901 /* Peek at the next token. */
2902 token = cp_lexer_peek_token (parser->lexer);
2903 switch (token->type)
2904 {
2905 /* literal:
2906 integer-literal
2907 character-literal
2908 floating-literal
2909 string-literal
2910 boolean-literal */
2911 case CPP_CHAR:
2912 case CPP_WCHAR:
2913 case CPP_NUMBER:
2914 token = cp_lexer_consume_token (parser->lexer);
2915 /* Floating-point literals are only allowed in an integral
2916 constant expression if they are cast to an integral or
2917 enumeration type. */
2918 if (TREE_CODE (token->value) == REAL_CST
2919 && parser->integral_constant_expression_p
2920 && pedantic)
2921 {
2922 /* CAST_P will be set even in invalid code like "int(2.7 +
2923 ...)". Therefore, we have to check that the next token
2924 is sure to end the cast. */
2925 if (cast_p)
2926 {
2927 cp_token *next_token;
2928
2929 next_token = cp_lexer_peek_token (parser->lexer);
2930 if (/* The comma at the end of an
2931 enumerator-definition. */
2932 next_token->type != CPP_COMMA
2933 /* The curly brace at the end of an enum-specifier. */
2934 && next_token->type != CPP_CLOSE_BRACE
2935 /* The end of a statement. */
2936 && next_token->type != CPP_SEMICOLON
2937 /* The end of the cast-expression. */
2938 && next_token->type != CPP_CLOSE_PAREN
2939 /* The end of an array bound. */
2940 && next_token->type != CPP_CLOSE_SQUARE
2941 /* The closing ">" in a template-argument-list. */
2942 && (next_token->type != CPP_GREATER
2943 || parser->greater_than_is_operator_p))
2944 cast_p = false;
2945 }
2946
2947 /* If we are within a cast, then the constraint that the
2948 cast is to an integral or enumeration type will be
2949 checked at that point. If we are not within a cast, then
2950 this code is invalid. */
2951 if (!cast_p)
2952 cp_parser_non_integral_constant_expression
2953 (parser, "floating-point literal");
2954 }
2955 return token->value;
2956
2957 case CPP_STRING:
2958 case CPP_WSTRING:
2959 /* ??? Should wide strings be allowed when parser->translate_strings_p
2960 is false (i.e. in attributes)? If not, we can kill the third
2961 argument to cp_parser_string_literal. */
2962 return cp_parser_string_literal (parser,
2963 parser->translate_strings_p,
2964 true);
2965
2966 case CPP_OPEN_PAREN:
2967 {
2968 tree expr;
2969 bool saved_greater_than_is_operator_p;
2970
2971 /* Consume the `('. */
2972 cp_lexer_consume_token (parser->lexer);
2973 /* Within a parenthesized expression, a `>' token is always
2974 the greater-than operator. */
2975 saved_greater_than_is_operator_p
2976 = parser->greater_than_is_operator_p;
2977 parser->greater_than_is_operator_p = true;
2978 /* If we see `( { ' then we are looking at the beginning of
2979 a GNU statement-expression. */
2980 if (cp_parser_allow_gnu_extensions_p (parser)
2981 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2982 {
2983 /* Statement-expressions are not allowed by the standard. */
2984 if (pedantic)
2985 pedwarn ("ISO C++ forbids braced-groups within expressions");
2986
2987 /* And they're not allowed outside of a function-body; you
2988 cannot, for example, write:
2989
2990 int i = ({ int j = 3; j + 1; });
2991
2992 at class or namespace scope. */
2993 if (!at_function_scope_p ())
2994 error ("statement-expressions are allowed only inside functions");
2995 /* Start the statement-expression. */
2996 expr = begin_stmt_expr ();
2997 /* Parse the compound-statement. */
2998 cp_parser_compound_statement (parser, expr, false);
2999 /* Finish up. */
3000 expr = finish_stmt_expr (expr, false);
3001 }
3002 else
3003 {
3004 /* Parse the parenthesized expression. */
3005 expr = cp_parser_expression (parser, cast_p);
3006 /* Let the front end know that this expression was
3007 enclosed in parentheses. This matters in case, for
3008 example, the expression is of the form `A::B', since
3009 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3010 not. */
3011 finish_parenthesized_expr (expr);
3012 }
3013 /* The `>' token might be the end of a template-id or
3014 template-parameter-list now. */
3015 parser->greater_than_is_operator_p
3016 = saved_greater_than_is_operator_p;
3017 /* Consume the `)'. */
3018 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3019 cp_parser_skip_to_end_of_statement (parser);
3020
3021 return expr;
3022 }
3023
3024 case CPP_KEYWORD:
3025 switch (token->keyword)
3026 {
3027 /* These two are the boolean literals. */
3028 case RID_TRUE:
3029 cp_lexer_consume_token (parser->lexer);
3030 return boolean_true_node;
3031 case RID_FALSE:
3032 cp_lexer_consume_token (parser->lexer);
3033 return boolean_false_node;
3034
3035 /* The `__null' literal. */
3036 case RID_NULL:
3037 cp_lexer_consume_token (parser->lexer);
3038 return null_node;
3039
3040 /* Recognize the `this' keyword. */
3041 case RID_THIS:
3042 cp_lexer_consume_token (parser->lexer);
3043 if (parser->local_variables_forbidden_p)
3044 {
3045 error ("%<this%> may not be used in this context");
3046 return error_mark_node;
3047 }
3048 /* Pointers cannot appear in constant-expressions. */
3049 if (cp_parser_non_integral_constant_expression (parser,
3050 "`this'"))
3051 return error_mark_node;
3052 return finish_this_expr ();
3053
3054 /* The `operator' keyword can be the beginning of an
3055 id-expression. */
3056 case RID_OPERATOR:
3057 goto id_expression;
3058
3059 case RID_FUNCTION_NAME:
3060 case RID_PRETTY_FUNCTION_NAME:
3061 case RID_C99_FUNCTION_NAME:
3062 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3063 __func__ are the names of variables -- but they are
3064 treated specially. Therefore, they are handled here,
3065 rather than relying on the generic id-expression logic
3066 below. Grammatically, these names are id-expressions.
3067
3068 Consume the token. */
3069 token = cp_lexer_consume_token (parser->lexer);
3070 /* Look up the name. */
3071 return finish_fname (token->value);
3072
3073 case RID_VA_ARG:
3074 {
3075 tree expression;
3076 tree type;
3077
3078 /* The `__builtin_va_arg' construct is used to handle
3079 `va_arg'. Consume the `__builtin_va_arg' token. */
3080 cp_lexer_consume_token (parser->lexer);
3081 /* Look for the opening `('. */
3082 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3083 /* Now, parse the assignment-expression. */
3084 expression = cp_parser_assignment_expression (parser,
3085 /*cast_p=*/false);
3086 /* Look for the `,'. */
3087 cp_parser_require (parser, CPP_COMMA, "`,'");
3088 /* Parse the type-id. */
3089 type = cp_parser_type_id (parser);
3090 /* Look for the closing `)'. */
3091 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3092 /* Using `va_arg' in a constant-expression is not
3093 allowed. */
3094 if (cp_parser_non_integral_constant_expression (parser,
3095 "`va_arg'"))
3096 return error_mark_node;
3097 return build_x_va_arg (expression, type);
3098 }
3099
3100 case RID_OFFSETOF:
3101 return cp_parser_builtin_offsetof (parser);
3102
3103 /* Objective-C++ expressions. */
3104 case RID_AT_ENCODE:
3105 case RID_AT_PROTOCOL:
3106 case RID_AT_SELECTOR:
3107 return cp_parser_objc_expression (parser);
3108
3109 default:
3110 cp_parser_error (parser, "expected primary-expression");
3111 return error_mark_node;
3112 }
3113
3114 /* An id-expression can start with either an identifier, a
3115 `::' as the beginning of a qualified-id, or the "operator"
3116 keyword. */
3117 case CPP_NAME:
3118 case CPP_SCOPE:
3119 case CPP_TEMPLATE_ID:
3120 case CPP_NESTED_NAME_SPECIFIER:
3121 {
3122 tree id_expression;
3123 tree decl;
3124 const char *error_msg;
3125 bool template_p;
3126 bool done;
3127
3128 id_expression:
3129 /* Parse the id-expression. */
3130 id_expression
3131 = cp_parser_id_expression (parser,
3132 /*template_keyword_p=*/false,
3133 /*check_dependency_p=*/true,
3134 &template_p,
3135 /*declarator_p=*/false,
3136 /*optional_p=*/false);
3137 if (id_expression == error_mark_node)
3138 return error_mark_node;
3139 token = cp_lexer_peek_token (parser->lexer);
3140 done = (token->type != CPP_OPEN_SQUARE
3141 && token->type != CPP_OPEN_PAREN
3142 && token->type != CPP_DOT
3143 && token->type != CPP_DEREF
3144 && token->type != CPP_PLUS_PLUS
3145 && token->type != CPP_MINUS_MINUS);
3146 /* If we have a template-id, then no further lookup is
3147 required. If the template-id was for a template-class, we
3148 will sometimes have a TYPE_DECL at this point. */
3149 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3150 || TREE_CODE (id_expression) == TYPE_DECL)
3151 decl = id_expression;
3152 /* Look up the name. */
3153 else
3154 {
3155 tree ambiguous_decls;
3156
3157 decl = cp_parser_lookup_name (parser, id_expression,
3158 none_type,
3159 template_p,
3160 /*is_namespace=*/false,
3161 /*check_dependency=*/true,
3162 &ambiguous_decls);
3163 /* If the lookup was ambiguous, an error will already have
3164 been issued. */
3165 if (ambiguous_decls)
3166 return error_mark_node;
3167
3168 /* In Objective-C++, an instance variable (ivar) may be preferred
3169 to whatever cp_parser_lookup_name() found. */
3170 decl = objc_lookup_ivar (decl, id_expression);
3171
3172 /* If name lookup gives us a SCOPE_REF, then the
3173 qualifying scope was dependent. */
3174 if (TREE_CODE (decl) == SCOPE_REF)
3175 return decl;
3176 /* Check to see if DECL is a local variable in a context
3177 where that is forbidden. */
3178 if (parser->local_variables_forbidden_p
3179 && local_variable_p (decl))
3180 {
3181 /* It might be that we only found DECL because we are
3182 trying to be generous with pre-ISO scoping rules.
3183 For example, consider:
3184
3185 int i;
3186 void g() {
3187 for (int i = 0; i < 10; ++i) {}
3188 extern void f(int j = i);
3189 }
3190
3191 Here, name look up will originally find the out
3192 of scope `i'. We need to issue a warning message,
3193 but then use the global `i'. */
3194 decl = check_for_out_of_scope_variable (decl);
3195 if (local_variable_p (decl))
3196 {
3197 error ("local variable %qD may not appear in this context",
3198 decl);
3199 return error_mark_node;
3200 }
3201 }
3202 }
3203
3204 decl = (finish_id_expression
3205 (id_expression, decl, parser->scope,
3206 idk,
3207 parser->integral_constant_expression_p,
3208 parser->allow_non_integral_constant_expression_p,
3209 &parser->non_integral_constant_expression_p,
3210 template_p, done, address_p,
3211 template_arg_p,
3212 &error_msg));
3213 if (error_msg)
3214 cp_parser_error (parser, error_msg);
3215 return decl;
3216 }
3217
3218 /* Anything else is an error. */
3219 default:
3220 /* ...unless we have an Objective-C++ message or string literal, that is. */
3221 if (c_dialect_objc ()
3222 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3223 return cp_parser_objc_expression (parser);
3224
3225 cp_parser_error (parser, "expected primary-expression");
3226 return error_mark_node;
3227 }
3228 }
3229
3230 /* Parse an id-expression.
3231
3232 id-expression:
3233 unqualified-id
3234 qualified-id
3235
3236 qualified-id:
3237 :: [opt] nested-name-specifier template [opt] unqualified-id
3238 :: identifier
3239 :: operator-function-id
3240 :: template-id
3241
3242 Return a representation of the unqualified portion of the
3243 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3244 a `::' or nested-name-specifier.
3245
3246 Often, if the id-expression was a qualified-id, the caller will
3247 want to make a SCOPE_REF to represent the qualified-id. This
3248 function does not do this in order to avoid wastefully creating
3249 SCOPE_REFs when they are not required.
3250
3251 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3252 `template' keyword.
3253
3254 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3255 uninstantiated templates.
3256
3257 If *TEMPLATE_P is non-NULL, it is set to true iff the
3258 `template' keyword is used to explicitly indicate that the entity
3259 named is a template.
3260
3261 If DECLARATOR_P is true, the id-expression is appearing as part of
3262 a declarator, rather than as part of an expression. */
3263
3264 static tree
3265 cp_parser_id_expression (cp_parser *parser,
3266 bool template_keyword_p,
3267 bool check_dependency_p,
3268 bool *template_p,
3269 bool declarator_p,
3270 bool optional_p)
3271 {
3272 bool global_scope_p;
3273 bool nested_name_specifier_p;
3274
3275 /* Assume the `template' keyword was not used. */
3276 if (template_p)
3277 *template_p = template_keyword_p;
3278
3279 /* Look for the optional `::' operator. */
3280 global_scope_p
3281 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3282 != NULL_TREE);
3283 /* Look for the optional nested-name-specifier. */
3284 nested_name_specifier_p
3285 = (cp_parser_nested_name_specifier_opt (parser,
3286 /*typename_keyword_p=*/false,
3287 check_dependency_p,
3288 /*type_p=*/false,
3289 declarator_p)
3290 != NULL_TREE);
3291 /* If there is a nested-name-specifier, then we are looking at
3292 the first qualified-id production. */
3293 if (nested_name_specifier_p)
3294 {
3295 tree saved_scope;
3296 tree saved_object_scope;
3297 tree saved_qualifying_scope;
3298 tree unqualified_id;
3299 bool is_template;
3300
3301 /* See if the next token is the `template' keyword. */
3302 if (!template_p)
3303 template_p = &is_template;
3304 *template_p = cp_parser_optional_template_keyword (parser);
3305 /* Name lookup we do during the processing of the
3306 unqualified-id might obliterate SCOPE. */
3307 saved_scope = parser->scope;
3308 saved_object_scope = parser->object_scope;
3309 saved_qualifying_scope = parser->qualifying_scope;
3310 /* Process the final unqualified-id. */
3311 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3312 check_dependency_p,
3313 declarator_p,
3314 /*optional_p=*/false);
3315 /* Restore the SAVED_SCOPE for our caller. */
3316 parser->scope = saved_scope;
3317 parser->object_scope = saved_object_scope;
3318 parser->qualifying_scope = saved_qualifying_scope;
3319
3320 return unqualified_id;
3321 }
3322 /* Otherwise, if we are in global scope, then we are looking at one
3323 of the other qualified-id productions. */
3324 else if (global_scope_p)
3325 {
3326 cp_token *token;
3327 tree id;
3328
3329 /* Peek at the next token. */
3330 token = cp_lexer_peek_token (parser->lexer);
3331
3332 /* If it's an identifier, and the next token is not a "<", then
3333 we can avoid the template-id case. This is an optimization
3334 for this common case. */
3335 if (token->type == CPP_NAME
3336 && !cp_parser_nth_token_starts_template_argument_list_p
3337 (parser, 2))
3338 return cp_parser_identifier (parser);
3339
3340 cp_parser_parse_tentatively (parser);
3341 /* Try a template-id. */
3342 id = cp_parser_template_id (parser,
3343 /*template_keyword_p=*/false,
3344 /*check_dependency_p=*/true,
3345 declarator_p);
3346 /* If that worked, we're done. */
3347 if (cp_parser_parse_definitely (parser))
3348 return id;
3349
3350 /* Peek at the next token. (Changes in the token buffer may
3351 have invalidated the pointer obtained above.) */
3352 token = cp_lexer_peek_token (parser->lexer);
3353
3354 switch (token->type)
3355 {
3356 case CPP_NAME:
3357 return cp_parser_identifier (parser);
3358
3359 case CPP_KEYWORD:
3360 if (token->keyword == RID_OPERATOR)
3361 return cp_parser_operator_function_id (parser);
3362 /* Fall through. */
3363
3364 default:
3365 cp_parser_error (parser, "expected id-expression");
3366 return error_mark_node;
3367 }
3368 }
3369 else
3370 return cp_parser_unqualified_id (parser, template_keyword_p,
3371 /*check_dependency_p=*/true,
3372 declarator_p,
3373 optional_p);
3374 }
3375
3376 /* Parse an unqualified-id.
3377
3378 unqualified-id:
3379 identifier
3380 operator-function-id
3381 conversion-function-id
3382 ~ class-name
3383 template-id
3384
3385 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3386 keyword, in a construct like `A::template ...'.
3387
3388 Returns a representation of unqualified-id. For the `identifier'
3389 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3390 production a BIT_NOT_EXPR is returned; the operand of the
3391 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3392 other productions, see the documentation accompanying the
3393 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3394 names are looked up in uninstantiated templates. If DECLARATOR_P
3395 is true, the unqualified-id is appearing as part of a declarator,
3396 rather than as part of an expression. */
3397
3398 static tree
3399 cp_parser_unqualified_id (cp_parser* parser,
3400 bool template_keyword_p,
3401 bool check_dependency_p,
3402 bool declarator_p,
3403 bool optional_p)
3404 {
3405 cp_token *token;
3406
3407 /* Peek at the next token. */
3408 token = cp_lexer_peek_token (parser->lexer);
3409
3410 switch (token->type)
3411 {
3412 case CPP_NAME:
3413 {
3414 tree id;
3415
3416 /* We don't know yet whether or not this will be a
3417 template-id. */
3418 cp_parser_parse_tentatively (parser);
3419 /* Try a template-id. */
3420 id = cp_parser_template_id (parser, template_keyword_p,
3421 check_dependency_p,
3422 declarator_p);
3423 /* If it worked, we're done. */
3424 if (cp_parser_parse_definitely (parser))
3425 return id;
3426 /* Otherwise, it's an ordinary identifier. */
3427 return cp_parser_identifier (parser);
3428 }
3429
3430 case CPP_TEMPLATE_ID:
3431 return cp_parser_template_id (parser, template_keyword_p,
3432 check_dependency_p,
3433 declarator_p);
3434
3435 case CPP_COMPL:
3436 {
3437 tree type_decl;
3438 tree qualifying_scope;
3439 tree object_scope;
3440 tree scope;
3441 bool done;
3442
3443 /* Consume the `~' token. */
3444 cp_lexer_consume_token (parser->lexer);
3445 /* Parse the class-name. The standard, as written, seems to
3446 say that:
3447
3448 template <typename T> struct S { ~S (); };
3449 template <typename T> S<T>::~S() {}
3450
3451 is invalid, since `~' must be followed by a class-name, but
3452 `S<T>' is dependent, and so not known to be a class.
3453 That's not right; we need to look in uninstantiated
3454 templates. A further complication arises from:
3455
3456 template <typename T> void f(T t) {
3457 t.T::~T();
3458 }
3459
3460 Here, it is not possible to look up `T' in the scope of `T'
3461 itself. We must look in both the current scope, and the
3462 scope of the containing complete expression.
3463
3464 Yet another issue is:
3465
3466 struct S {
3467 int S;
3468 ~S();
3469 };
3470
3471 S::~S() {}
3472
3473 The standard does not seem to say that the `S' in `~S'
3474 should refer to the type `S' and not the data member
3475 `S::S'. */
3476
3477 /* DR 244 says that we look up the name after the "~" in the
3478 same scope as we looked up the qualifying name. That idea
3479 isn't fully worked out; it's more complicated than that. */
3480 scope = parser->scope;
3481 object_scope = parser->object_scope;
3482 qualifying_scope = parser->qualifying_scope;
3483
3484 /* Check for invalid scopes. */
3485 if (scope == error_mark_node)
3486 {
3487 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3488 cp_lexer_consume_token (parser->lexer);
3489 return error_mark_node;
3490 }
3491 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3492 {
3493 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3494 error ("scope %qT before %<~%> is not a class-name", scope);
3495 cp_parser_simulate_error (parser);
3496 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3497 cp_lexer_consume_token (parser->lexer);
3498 return error_mark_node;
3499 }
3500 gcc_assert (!scope || TYPE_P (scope));
3501
3502 /* If the name is of the form "X::~X" it's OK. */
3503 token = cp_lexer_peek_token (parser->lexer);
3504 if (scope
3505 && token->type == CPP_NAME
3506 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3507 == CPP_OPEN_PAREN)
3508 && constructor_name_p (token->value, scope))
3509 {
3510 cp_lexer_consume_token (parser->lexer);
3511 return build_nt (BIT_NOT_EXPR, scope);
3512 }
3513
3514 /* If there was an explicit qualification (S::~T), first look
3515 in the scope given by the qualification (i.e., S). */
3516 done = false;
3517 type_decl = NULL_TREE;
3518 if (scope)
3519 {
3520 cp_parser_parse_tentatively (parser);
3521 type_decl = cp_parser_class_name (parser,
3522 /*typename_keyword_p=*/false,
3523 /*template_keyword_p=*/false,
3524 none_type,
3525 /*check_dependency=*/false,
3526 /*class_head_p=*/false,
3527 declarator_p);
3528 if (cp_parser_parse_definitely (parser))
3529 done = true;
3530 }
3531 /* In "N::S::~S", look in "N" as well. */
3532 if (!done && scope && qualifying_scope)
3533 {
3534 cp_parser_parse_tentatively (parser);
3535 parser->scope = qualifying_scope;
3536 parser->object_scope = NULL_TREE;
3537 parser->qualifying_scope = NULL_TREE;
3538 type_decl
3539 = cp_parser_class_name (parser,
3540 /*typename_keyword_p=*/false,
3541 /*template_keyword_p=*/false,
3542 none_type,
3543 /*check_dependency=*/false,
3544 /*class_head_p=*/false,
3545 declarator_p);
3546 if (cp_parser_parse_definitely (parser))
3547 done = true;
3548 }
3549 /* In "p->S::~T", look in the scope given by "*p" as well. */
3550 else if (!done && object_scope)
3551 {
3552 cp_parser_parse_tentatively (parser);
3553 parser->scope = object_scope;
3554 parser->object_scope = NULL_TREE;
3555 parser->qualifying_scope = NULL_TREE;
3556 type_decl
3557 = cp_parser_class_name (parser,
3558 /*typename_keyword_p=*/false,
3559 /*template_keyword_p=*/false,
3560 none_type,
3561 /*check_dependency=*/false,
3562 /*class_head_p=*/false,
3563 declarator_p);
3564 if (cp_parser_parse_definitely (parser))
3565 done = true;
3566 }
3567 /* Look in the surrounding context. */
3568 if (!done)
3569 {
3570 parser->scope = NULL_TREE;
3571 parser->object_scope = NULL_TREE;
3572 parser->qualifying_scope = NULL_TREE;
3573 type_decl
3574 = cp_parser_class_name (parser,
3575 /*typename_keyword_p=*/false,
3576 /*template_keyword_p=*/false,
3577 none_type,
3578 /*check_dependency=*/false,
3579 /*class_head_p=*/false,
3580 declarator_p);
3581 }
3582 /* If an error occurred, assume that the name of the
3583 destructor is the same as the name of the qualifying
3584 class. That allows us to keep parsing after running
3585 into ill-formed destructor names. */
3586 if (type_decl == error_mark_node && scope)
3587 return build_nt (BIT_NOT_EXPR, scope);
3588 else if (type_decl == error_mark_node)
3589 return error_mark_node;
3590
3591 /* Check that destructor name and scope match. */
3592 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3593 {
3594 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3595 error ("declaration of %<~%T%> as member of %qT",
3596 type_decl, scope);
3597 cp_parser_simulate_error (parser);
3598 return error_mark_node;
3599 }
3600
3601 /* [class.dtor]
3602
3603 A typedef-name that names a class shall not be used as the
3604 identifier in the declarator for a destructor declaration. */
3605 if (declarator_p
3606 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3607 && !DECL_SELF_REFERENCE_P (type_decl)
3608 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3609 error ("typedef-name %qD used as destructor declarator",
3610 type_decl);
3611
3612 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3613 }
3614
3615 case CPP_KEYWORD:
3616 if (token->keyword == RID_OPERATOR)
3617 {
3618 tree id;
3619
3620 /* This could be a template-id, so we try that first. */
3621 cp_parser_parse_tentatively (parser);
3622 /* Try a template-id. */
3623 id = cp_parser_template_id (parser, template_keyword_p,
3624 /*check_dependency_p=*/true,
3625 declarator_p);
3626 /* If that worked, we're done. */
3627 if (cp_parser_parse_definitely (parser))
3628 return id;
3629 /* We still don't know whether we're looking at an
3630 operator-function-id or a conversion-function-id. */
3631 cp_parser_parse_tentatively (parser);
3632 /* Try an operator-function-id. */
3633 id = cp_parser_operator_function_id (parser);
3634 /* If that didn't work, try a conversion-function-id. */
3635 if (!cp_parser_parse_definitely (parser))
3636 id = cp_parser_conversion_function_id (parser);
3637
3638 return id;
3639 }
3640 /* Fall through. */
3641
3642 default:
3643 if (optional_p)
3644 return NULL_TREE;
3645 cp_parser_error (parser, "expected unqualified-id");
3646 return error_mark_node;
3647 }
3648 }
3649
3650 /* Parse an (optional) nested-name-specifier.
3651
3652 nested-name-specifier:
3653 class-or-namespace-name :: nested-name-specifier [opt]
3654 class-or-namespace-name :: template nested-name-specifier [opt]
3655
3656 PARSER->SCOPE should be set appropriately before this function is
3657 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3658 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3659 in name lookups.
3660
3661 Sets PARSER->SCOPE to the class (TYPE) or namespace
3662 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3663 it unchanged if there is no nested-name-specifier. Returns the new
3664 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3665
3666 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3667 part of a declaration and/or decl-specifier. */
3668
3669 static tree
3670 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3671 bool typename_keyword_p,
3672 bool check_dependency_p,
3673 bool type_p,
3674 bool is_declaration)
3675 {
3676 bool success = false;
3677 cp_token_position start = 0;
3678 cp_token *token;
3679
3680 /* Remember where the nested-name-specifier starts. */
3681 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3682 {
3683 start = cp_lexer_token_position (parser->lexer, false);
3684 push_deferring_access_checks (dk_deferred);
3685 }
3686
3687 while (true)
3688 {
3689 tree new_scope;
3690 tree old_scope;
3691 tree saved_qualifying_scope;
3692 bool template_keyword_p;
3693
3694 /* Spot cases that cannot be the beginning of a
3695 nested-name-specifier. */
3696 token = cp_lexer_peek_token (parser->lexer);
3697
3698 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3699 the already parsed nested-name-specifier. */
3700 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3701 {
3702 /* Grab the nested-name-specifier and continue the loop. */
3703 cp_parser_pre_parsed_nested_name_specifier (parser);
3704 /* If we originally encountered this nested-name-specifier
3705 with IS_DECLARATION set to false, we will not have
3706 resolved TYPENAME_TYPEs, so we must do so here. */
3707 if (is_declaration
3708 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3709 {
3710 new_scope = resolve_typename_type (parser->scope,
3711 /*only_current_p=*/false);
3712 if (new_scope != error_mark_node)
3713 parser->scope = new_scope;
3714 }
3715 success = true;
3716 continue;
3717 }
3718
3719 /* Spot cases that cannot be the beginning of a
3720 nested-name-specifier. On the second and subsequent times
3721 through the loop, we look for the `template' keyword. */
3722 if (success && token->keyword == RID_TEMPLATE)
3723 ;
3724 /* A template-id can start a nested-name-specifier. */
3725 else if (token->type == CPP_TEMPLATE_ID)
3726 ;
3727 else
3728 {
3729 /* If the next token is not an identifier, then it is
3730 definitely not a class-or-namespace-name. */
3731 if (token->type != CPP_NAME)
3732 break;
3733 /* If the following token is neither a `<' (to begin a
3734 template-id), nor a `::', then we are not looking at a
3735 nested-name-specifier. */
3736 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3737 if (token->type != CPP_SCOPE
3738 && !cp_parser_nth_token_starts_template_argument_list_p
3739 (parser, 2))
3740 break;
3741 }
3742
3743 /* The nested-name-specifier is optional, so we parse
3744 tentatively. */
3745 cp_parser_parse_tentatively (parser);
3746
3747 /* Look for the optional `template' keyword, if this isn't the
3748 first time through the loop. */
3749 if (success)
3750 template_keyword_p = cp_parser_optional_template_keyword (parser);
3751 else
3752 template_keyword_p = false;
3753
3754 /* Save the old scope since the name lookup we are about to do
3755 might destroy it. */
3756 old_scope = parser->scope;
3757 saved_qualifying_scope = parser->qualifying_scope;
3758 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3759 look up names in "X<T>::I" in order to determine that "Y" is
3760 a template. So, if we have a typename at this point, we make
3761 an effort to look through it. */
3762 if (is_declaration
3763 && !typename_keyword_p
3764 && parser->scope
3765 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3766 parser->scope = resolve_typename_type (parser->scope,
3767 /*only_current_p=*/false);
3768 /* Parse the qualifying entity. */
3769 new_scope
3770 = cp_parser_class_or_namespace_name (parser,
3771 typename_keyword_p,
3772 template_keyword_p,
3773 check_dependency_p,
3774 type_p,
3775 is_declaration);
3776 /* Look for the `::' token. */
3777 cp_parser_require (parser, CPP_SCOPE, "`::'");
3778
3779 /* If we found what we wanted, we keep going; otherwise, we're
3780 done. */
3781 if (!cp_parser_parse_definitely (parser))
3782 {
3783 bool error_p = false;
3784
3785 /* Restore the OLD_SCOPE since it was valid before the
3786 failed attempt at finding the last
3787 class-or-namespace-name. */
3788 parser->scope = old_scope;
3789 parser->qualifying_scope = saved_qualifying_scope;
3790 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3791 break;
3792 /* If the next token is an identifier, and the one after
3793 that is a `::', then any valid interpretation would have
3794 found a class-or-namespace-name. */
3795 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3796 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3797 == CPP_SCOPE)
3798 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3799 != CPP_COMPL))
3800 {
3801 token = cp_lexer_consume_token (parser->lexer);
3802 if (!error_p)
3803 {
3804 if (!token->ambiguous_p)
3805 {
3806 tree decl;
3807 tree ambiguous_decls;
3808
3809 decl = cp_parser_lookup_name (parser, token->value,
3810 none_type,
3811 /*is_template=*/false,
3812 /*is_namespace=*/false,
3813 /*check_dependency=*/true,
3814 &ambiguous_decls);
3815 if (TREE_CODE (decl) == TEMPLATE_DECL)
3816 error ("%qD used without template parameters", decl);
3817 else if (ambiguous_decls)
3818 {
3819 error ("reference to %qD is ambiguous",
3820 token->value);
3821 print_candidates (ambiguous_decls);
3822 decl = error_mark_node;
3823 }
3824 else
3825 cp_parser_name_lookup_error
3826 (parser, token->value, decl,
3827 "is not a class or namespace");
3828 }
3829 parser->scope = error_mark_node;
3830 error_p = true;
3831 /* Treat this as a successful nested-name-specifier
3832 due to:
3833
3834 [basic.lookup.qual]
3835
3836 If the name found is not a class-name (clause
3837 _class_) or namespace-name (_namespace.def_), the
3838 program is ill-formed. */
3839 success = true;
3840 }
3841 cp_lexer_consume_token (parser->lexer);
3842 }
3843 break;
3844 }
3845 /* We've found one valid nested-name-specifier. */
3846 success = true;
3847 /* Name lookup always gives us a DECL. */
3848 if (TREE_CODE (new_scope) == TYPE_DECL)
3849 new_scope = TREE_TYPE (new_scope);
3850 /* Uses of "template" must be followed by actual templates. */
3851 if (template_keyword_p
3852 && !(CLASS_TYPE_P (new_scope)
3853 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3854 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3855 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3856 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3857 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3858 == TEMPLATE_ID_EXPR)))
3859 pedwarn (TYPE_P (new_scope)
3860 ? "%qT is not a template"
3861 : "%qD is not a template",
3862 new_scope);
3863 /* If it is a class scope, try to complete it; we are about to
3864 be looking up names inside the class. */
3865 if (TYPE_P (new_scope)
3866 /* Since checking types for dependency can be expensive,
3867 avoid doing it if the type is already complete. */
3868 && !COMPLETE_TYPE_P (new_scope)
3869 /* Do not try to complete dependent types. */
3870 && !dependent_type_p (new_scope))
3871 new_scope = complete_type (new_scope);
3872 /* Make sure we look in the right scope the next time through
3873 the loop. */
3874 parser->scope = new_scope;
3875 }
3876
3877 /* If parsing tentatively, replace the sequence of tokens that makes
3878 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3879 token. That way, should we re-parse the token stream, we will
3880 not have to repeat the effort required to do the parse, nor will
3881 we issue duplicate error messages. */
3882 if (success && start)
3883 {
3884 cp_token *token;
3885 tree access_checks;
3886
3887 token = cp_lexer_token_at (parser->lexer, start);
3888 /* Reset the contents of the START token. */
3889 token->type = CPP_NESTED_NAME_SPECIFIER;
3890 /* Retrieve any deferred checks. Do not pop this access checks yet
3891 so the memory will not be reclaimed during token replacing below. */
3892 access_checks = get_deferred_access_checks ();
3893 token->value = build_tree_list (copy_list (access_checks),
3894 parser->scope);
3895 TREE_TYPE (token->value) = parser->qualifying_scope;
3896 token->keyword = RID_MAX;
3897
3898 /* Purge all subsequent tokens. */
3899 cp_lexer_purge_tokens_after (parser->lexer, start);
3900 }
3901
3902 if (start)
3903 pop_to_parent_deferring_access_checks ();
3904
3905 return success ? parser->scope : NULL_TREE;
3906 }
3907
3908 /* Parse a nested-name-specifier. See
3909 cp_parser_nested_name_specifier_opt for details. This function
3910 behaves identically, except that it will an issue an error if no
3911 nested-name-specifier is present. */
3912
3913 static tree
3914 cp_parser_nested_name_specifier (cp_parser *parser,
3915 bool typename_keyword_p,
3916 bool check_dependency_p,
3917 bool type_p,
3918 bool is_declaration)
3919 {
3920 tree scope;
3921
3922 /* Look for the nested-name-specifier. */
3923 scope = cp_parser_nested_name_specifier_opt (parser,
3924 typename_keyword_p,
3925 check_dependency_p,
3926 type_p,
3927 is_declaration);
3928 /* If it was not present, issue an error message. */
3929 if (!scope)
3930 {
3931 cp_parser_error (parser, "expected nested-name-specifier");
3932 parser->scope = NULL_TREE;
3933 }
3934
3935 return scope;
3936 }
3937
3938 /* Parse a class-or-namespace-name.
3939
3940 class-or-namespace-name:
3941 class-name
3942 namespace-name
3943
3944 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3945 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3946 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3947 TYPE_P is TRUE iff the next name should be taken as a class-name,
3948 even the same name is declared to be another entity in the same
3949 scope.
3950
3951 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3952 specified by the class-or-namespace-name. If neither is found the
3953 ERROR_MARK_NODE is returned. */
3954
3955 static tree
3956 cp_parser_class_or_namespace_name (cp_parser *parser,
3957 bool typename_keyword_p,
3958 bool template_keyword_p,
3959 bool check_dependency_p,
3960 bool type_p,
3961 bool is_declaration)
3962 {
3963 tree saved_scope;
3964 tree saved_qualifying_scope;
3965 tree saved_object_scope;
3966 tree scope;
3967 bool only_class_p;
3968
3969 /* Before we try to parse the class-name, we must save away the
3970 current PARSER->SCOPE since cp_parser_class_name will destroy
3971 it. */
3972 saved_scope = parser->scope;
3973 saved_qualifying_scope = parser->qualifying_scope;
3974 saved_object_scope = parser->object_scope;
3975 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3976 there is no need to look for a namespace-name. */
3977 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3978 if (!only_class_p)
3979 cp_parser_parse_tentatively (parser);
3980 scope = cp_parser_class_name (parser,
3981 typename_keyword_p,
3982 template_keyword_p,
3983 type_p ? class_type : none_type,
3984 check_dependency_p,
3985 /*class_head_p=*/false,
3986 is_declaration);
3987 /* If that didn't work, try for a namespace-name. */
3988 if (!only_class_p && !cp_parser_parse_definitely (parser))
3989 {
3990 /* Restore the saved scope. */
3991 parser->scope = saved_scope;
3992 parser->qualifying_scope = saved_qualifying_scope;
3993 parser->object_scope = saved_object_scope;
3994 /* If we are not looking at an identifier followed by the scope
3995 resolution operator, then this is not part of a
3996 nested-name-specifier. (Note that this function is only used
3997 to parse the components of a nested-name-specifier.) */
3998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3999 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4000 return error_mark_node;
4001 scope = cp_parser_namespace_name (parser);
4002 }
4003
4004 return scope;
4005 }
4006
4007 /* Parse a postfix-expression.
4008
4009 postfix-expression:
4010 primary-expression
4011 postfix-expression [ expression ]
4012 postfix-expression ( expression-list [opt] )
4013 simple-type-specifier ( expression-list [opt] )
4014 typename :: [opt] nested-name-specifier identifier
4015 ( expression-list [opt] )
4016 typename :: [opt] nested-name-specifier template [opt] template-id
4017 ( expression-list [opt] )
4018 postfix-expression . template [opt] id-expression
4019 postfix-expression -> template [opt] id-expression
4020 postfix-expression . pseudo-destructor-name
4021 postfix-expression -> pseudo-destructor-name
4022 postfix-expression ++
4023 postfix-expression --
4024 dynamic_cast < type-id > ( expression )
4025 static_cast < type-id > ( expression )
4026 reinterpret_cast < type-id > ( expression )
4027 const_cast < type-id > ( expression )
4028 typeid ( expression )
4029 typeid ( type-id )
4030
4031 GNU Extension:
4032
4033 postfix-expression:
4034 ( type-id ) { initializer-list , [opt] }
4035
4036 This extension is a GNU version of the C99 compound-literal
4037 construct. (The C99 grammar uses `type-name' instead of `type-id',
4038 but they are essentially the same concept.)
4039
4040 If ADDRESS_P is true, the postfix expression is the operand of the
4041 `&' operator. CAST_P is true if this expression is the target of a
4042 cast.
4043
4044 Returns a representation of the expression. */
4045
4046 static tree
4047 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4048 {
4049 cp_token *token;
4050 enum rid keyword;
4051 cp_id_kind idk = CP_ID_KIND_NONE;
4052 tree postfix_expression = NULL_TREE;
4053
4054 /* Peek at the next token. */
4055 token = cp_lexer_peek_token (parser->lexer);
4056 /* Some of the productions are determined by keywords. */
4057 keyword = token->keyword;
4058 switch (keyword)
4059 {
4060 case RID_DYNCAST:
4061 case RID_STATCAST:
4062 case RID_REINTCAST:
4063 case RID_CONSTCAST:
4064 {
4065 tree type;
4066 tree expression;
4067 const char *saved_message;
4068
4069 /* All of these can be handled in the same way from the point
4070 of view of parsing. Begin by consuming the token
4071 identifying the cast. */
4072 cp_lexer_consume_token (parser->lexer);
4073
4074 /* New types cannot be defined in the cast. */
4075 saved_message = parser->type_definition_forbidden_message;
4076 parser->type_definition_forbidden_message
4077 = "types may not be defined in casts";
4078
4079 /* Look for the opening `<'. */
4080 cp_parser_require (parser, CPP_LESS, "`<'");
4081 /* Parse the type to which we are casting. */
4082 type = cp_parser_type_id (parser);
4083 /* Look for the closing `>'. */
4084 cp_parser_require (parser, CPP_GREATER, "`>'");
4085 /* Restore the old message. */
4086 parser->type_definition_forbidden_message = saved_message;
4087
4088 /* And the expression which is being cast. */
4089 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4090 expression = cp_parser_expression (parser, /*cast_p=*/true);
4091 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4092
4093 /* Only type conversions to integral or enumeration types
4094 can be used in constant-expressions. */
4095 if (!cast_valid_in_integral_constant_expression_p (type)
4096 && (cp_parser_non_integral_constant_expression
4097 (parser,
4098 "a cast to a type other than an integral or "
4099 "enumeration type")))
4100 return error_mark_node;
4101
4102 switch (keyword)
4103 {
4104 case RID_DYNCAST:
4105 postfix_expression
4106 = build_dynamic_cast (type, expression);
4107 break;
4108 case RID_STATCAST:
4109 postfix_expression
4110 = build_static_cast (type, expression);
4111 break;
4112 case RID_REINTCAST:
4113 postfix_expression
4114 = build_reinterpret_cast (type, expression);
4115 break;
4116 case RID_CONSTCAST:
4117 postfix_expression
4118 = build_const_cast (type, expression);
4119 break;
4120 default:
4121 gcc_unreachable ();
4122 }
4123 }
4124 break;
4125
4126 case RID_TYPEID:
4127 {
4128 tree type;
4129 const char *saved_message;
4130 bool saved_in_type_id_in_expr_p;
4131
4132 /* Consume the `typeid' token. */
4133 cp_lexer_consume_token (parser->lexer);
4134 /* Look for the `(' token. */
4135 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4136 /* Types cannot be defined in a `typeid' expression. */
4137 saved_message = parser->type_definition_forbidden_message;
4138 parser->type_definition_forbidden_message
4139 = "types may not be defined in a `typeid\' expression";
4140 /* We can't be sure yet whether we're looking at a type-id or an
4141 expression. */
4142 cp_parser_parse_tentatively (parser);
4143 /* Try a type-id first. */
4144 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4145 parser->in_type_id_in_expr_p = true;
4146 type = cp_parser_type_id (parser);
4147 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4148 /* Look for the `)' token. Otherwise, we can't be sure that
4149 we're not looking at an expression: consider `typeid (int
4150 (3))', for example. */
4151 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4152 /* If all went well, simply lookup the type-id. */
4153 if (cp_parser_parse_definitely (parser))
4154 postfix_expression = get_typeid (type);
4155 /* Otherwise, fall back to the expression variant. */
4156 else
4157 {
4158 tree expression;
4159
4160 /* Look for an expression. */
4161 expression = cp_parser_expression (parser, /*cast_p=*/false);
4162 /* Compute its typeid. */
4163 postfix_expression = build_typeid (expression);
4164 /* Look for the `)' token. */
4165 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4166 }
4167 /* Restore the saved message. */
4168 parser->type_definition_forbidden_message = saved_message;
4169 /* `typeid' may not appear in an integral constant expression. */
4170 if (cp_parser_non_integral_constant_expression(parser,
4171 "`typeid' operator"))
4172 return error_mark_node;
4173 }
4174 break;
4175
4176 case RID_TYPENAME:
4177 {
4178 tree type;
4179 /* The syntax permitted here is the same permitted for an
4180 elaborated-type-specifier. */
4181 type = cp_parser_elaborated_type_specifier (parser,
4182 /*is_friend=*/false,
4183 /*is_declaration=*/false);
4184 postfix_expression = cp_parser_functional_cast (parser, type);
4185 }
4186 break;
4187
4188 default:
4189 {
4190 tree type;
4191
4192 /* If the next thing is a simple-type-specifier, we may be
4193 looking at a functional cast. We could also be looking at
4194 an id-expression. So, we try the functional cast, and if
4195 that doesn't work we fall back to the primary-expression. */
4196 cp_parser_parse_tentatively (parser);
4197 /* Look for the simple-type-specifier. */
4198 type = cp_parser_simple_type_specifier (parser,
4199 /*decl_specs=*/NULL,
4200 CP_PARSER_FLAGS_NONE);
4201 /* Parse the cast itself. */
4202 if (!cp_parser_error_occurred (parser))
4203 postfix_expression
4204 = cp_parser_functional_cast (parser, type);
4205 /* If that worked, we're done. */
4206 if (cp_parser_parse_definitely (parser))
4207 break;
4208
4209 /* If the functional-cast didn't work out, try a
4210 compound-literal. */
4211 if (cp_parser_allow_gnu_extensions_p (parser)
4212 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4213 {
4214 VEC(constructor_elt,gc) *initializer_list = NULL;
4215 bool saved_in_type_id_in_expr_p;
4216
4217 cp_parser_parse_tentatively (parser);
4218 /* Consume the `('. */
4219 cp_lexer_consume_token (parser->lexer);
4220 /* Parse the type. */
4221 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4222 parser->in_type_id_in_expr_p = true;
4223 type = cp_parser_type_id (parser);
4224 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4225 /* Look for the `)'. */
4226 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4227 /* Look for the `{'. */
4228 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4229 /* If things aren't going well, there's no need to
4230 keep going. */
4231 if (!cp_parser_error_occurred (parser))
4232 {
4233 bool non_constant_p;
4234 /* Parse the initializer-list. */
4235 initializer_list
4236 = cp_parser_initializer_list (parser, &non_constant_p);
4237 /* Allow a trailing `,'. */
4238 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4239 cp_lexer_consume_token (parser->lexer);
4240 /* Look for the final `}'. */
4241 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4242 }
4243 /* If that worked, we're definitely looking at a
4244 compound-literal expression. */
4245 if (cp_parser_parse_definitely (parser))
4246 {
4247 /* Warn the user that a compound literal is not
4248 allowed in standard C++. */
4249 if (pedantic)
4250 pedwarn ("ISO C++ forbids compound-literals");
4251 /* Form the representation of the compound-literal. */
4252 postfix_expression
4253 = finish_compound_literal (type, initializer_list);
4254 break;
4255 }
4256 }
4257
4258 /* It must be a primary-expression. */
4259 postfix_expression
4260 = cp_parser_primary_expression (parser, address_p, cast_p,
4261 /*template_arg_p=*/false,
4262 &idk);
4263 }
4264 break;
4265 }
4266
4267 /* Keep looping until the postfix-expression is complete. */
4268 while (true)
4269 {
4270 if (idk == CP_ID_KIND_UNQUALIFIED
4271 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4272 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4273 /* It is not a Koenig lookup function call. */
4274 postfix_expression
4275 = unqualified_name_lookup_error (postfix_expression);
4276
4277 /* Peek at the next token. */
4278 token = cp_lexer_peek_token (parser->lexer);
4279
4280 switch (token->type)
4281 {
4282 case CPP_OPEN_SQUARE:
4283 postfix_expression
4284 = cp_parser_postfix_open_square_expression (parser,
4285 postfix_expression,
4286 false);
4287 idk = CP_ID_KIND_NONE;
4288 break;
4289
4290 case CPP_OPEN_PAREN:
4291 /* postfix-expression ( expression-list [opt] ) */
4292 {
4293 bool koenig_p;
4294 bool is_builtin_constant_p;
4295 bool saved_integral_constant_expression_p = false;
4296 bool saved_non_integral_constant_expression_p = false;
4297 tree args;
4298
4299 is_builtin_constant_p
4300 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4301 if (is_builtin_constant_p)
4302 {
4303 /* The whole point of __builtin_constant_p is to allow
4304 non-constant expressions to appear as arguments. */
4305 saved_integral_constant_expression_p
4306 = parser->integral_constant_expression_p;
4307 saved_non_integral_constant_expression_p
4308 = parser->non_integral_constant_expression_p;
4309 parser->integral_constant_expression_p = false;
4310 }
4311 args = (cp_parser_parenthesized_expression_list
4312 (parser, /*is_attribute_list=*/false,
4313 /*cast_p=*/false,
4314 /*non_constant_p=*/NULL));
4315 if (is_builtin_constant_p)
4316 {
4317 parser->integral_constant_expression_p
4318 = saved_integral_constant_expression_p;
4319 parser->non_integral_constant_expression_p
4320 = saved_non_integral_constant_expression_p;
4321 }
4322
4323 if (args == error_mark_node)
4324 {
4325 postfix_expression = error_mark_node;
4326 break;
4327 }
4328
4329 /* Function calls are not permitted in
4330 constant-expressions. */
4331 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4332 && cp_parser_non_integral_constant_expression (parser,
4333 "a function call"))
4334 {
4335 postfix_expression = error_mark_node;
4336 break;
4337 }
4338
4339 koenig_p = false;
4340 if (idk == CP_ID_KIND_UNQUALIFIED)
4341 {
4342 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4343 {
4344 if (args)
4345 {
4346 koenig_p = true;
4347 postfix_expression
4348 = perform_koenig_lookup (postfix_expression, args);
4349 }
4350 else
4351 postfix_expression
4352 = unqualified_fn_lookup_error (postfix_expression);
4353 }
4354 /* We do not perform argument-dependent lookup if
4355 normal lookup finds a non-function, in accordance
4356 with the expected resolution of DR 218. */
4357 else if (args && is_overloaded_fn (postfix_expression))
4358 {
4359 tree fn = get_first_fn (postfix_expression);
4360
4361 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4362 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4363
4364 /* Only do argument dependent lookup if regular
4365 lookup does not find a set of member functions.
4366 [basic.lookup.koenig]/2a */
4367 if (!DECL_FUNCTION_MEMBER_P (fn))
4368 {
4369 koenig_p = true;
4370 postfix_expression
4371 = perform_koenig_lookup (postfix_expression, args);
4372 }
4373 }
4374 }
4375
4376 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4377 {
4378 tree instance = TREE_OPERAND (postfix_expression, 0);
4379 tree fn = TREE_OPERAND (postfix_expression, 1);
4380
4381 if (processing_template_decl
4382 && (type_dependent_expression_p (instance)
4383 || (!BASELINK_P (fn)
4384 && TREE_CODE (fn) != FIELD_DECL)
4385 || type_dependent_expression_p (fn)
4386 || any_type_dependent_arguments_p (args)))
4387 {
4388 postfix_expression
4389 = build_min_nt (CALL_EXPR, postfix_expression,
4390 args, NULL_TREE);
4391 break;
4392 }
4393
4394 if (BASELINK_P (fn))
4395 postfix_expression
4396 = (build_new_method_call
4397 (instance, fn, args, NULL_TREE,
4398 (idk == CP_ID_KIND_QUALIFIED
4399 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4400 /*fn_p=*/NULL));
4401 else
4402 postfix_expression
4403 = finish_call_expr (postfix_expression, args,
4404 /*disallow_virtual=*/false,
4405 /*koenig_p=*/false);
4406 }
4407 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4408 || TREE_CODE (postfix_expression) == MEMBER_REF
4409 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4410 postfix_expression = (build_offset_ref_call_from_tree
4411 (postfix_expression, args));
4412 else if (idk == CP_ID_KIND_QUALIFIED)
4413 /* A call to a static class member, or a namespace-scope
4414 function. */
4415 postfix_expression
4416 = finish_call_expr (postfix_expression, args,
4417 /*disallow_virtual=*/true,
4418 koenig_p);
4419 else
4420 /* All other function calls. */
4421 postfix_expression
4422 = finish_call_expr (postfix_expression, args,
4423 /*disallow_virtual=*/false,
4424 koenig_p);
4425
4426 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4427 idk = CP_ID_KIND_NONE;
4428 }
4429 break;
4430
4431 case CPP_DOT:
4432 case CPP_DEREF:
4433 /* postfix-expression . template [opt] id-expression
4434 postfix-expression . pseudo-destructor-name
4435 postfix-expression -> template [opt] id-expression
4436 postfix-expression -> pseudo-destructor-name */
4437
4438 /* Consume the `.' or `->' operator. */
4439 cp_lexer_consume_token (parser->lexer);
4440
4441 postfix_expression
4442 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4443 postfix_expression,
4444 false, &idk);
4445 break;
4446
4447 case CPP_PLUS_PLUS:
4448 /* postfix-expression ++ */
4449 /* Consume the `++' token. */
4450 cp_lexer_consume_token (parser->lexer);
4451 /* Generate a representation for the complete expression. */
4452 postfix_expression
4453 = finish_increment_expr (postfix_expression,
4454 POSTINCREMENT_EXPR);
4455 /* Increments may not appear in constant-expressions. */
4456 if (cp_parser_non_integral_constant_expression (parser,
4457 "an increment"))
4458 postfix_expression = error_mark_node;
4459 idk = CP_ID_KIND_NONE;
4460 break;
4461
4462 case CPP_MINUS_MINUS:
4463 /* postfix-expression -- */
4464 /* Consume the `--' token. */
4465 cp_lexer_consume_token (parser->lexer);
4466 /* Generate a representation for the complete expression. */
4467 postfix_expression
4468 = finish_increment_expr (postfix_expression,
4469 POSTDECREMENT_EXPR);
4470 /* Decrements may not appear in constant-expressions. */
4471 if (cp_parser_non_integral_constant_expression (parser,
4472 "a decrement"))
4473 postfix_expression = error_mark_node;
4474 idk = CP_ID_KIND_NONE;
4475 break;
4476
4477 default:
4478 return postfix_expression;
4479 }
4480 }
4481
4482 /* We should never get here. */
4483 gcc_unreachable ();
4484 return error_mark_node;
4485 }
4486
4487 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4488 by cp_parser_builtin_offsetof. We're looking for
4489
4490 postfix-expression [ expression ]
4491
4492 FOR_OFFSETOF is set if we're being called in that context, which
4493 changes how we deal with integer constant expressions. */
4494
4495 static tree
4496 cp_parser_postfix_open_square_expression (cp_parser *parser,
4497 tree postfix_expression,
4498 bool for_offsetof)
4499 {
4500 tree index;
4501
4502 /* Consume the `[' token. */
4503 cp_lexer_consume_token (parser->lexer);
4504
4505 /* Parse the index expression. */
4506 /* ??? For offsetof, there is a question of what to allow here. If
4507 offsetof is not being used in an integral constant expression context,
4508 then we *could* get the right answer by computing the value at runtime.
4509 If we are in an integral constant expression context, then we might
4510 could accept any constant expression; hard to say without analysis.
4511 Rather than open the barn door too wide right away, allow only integer
4512 constant expressions here. */
4513 if (for_offsetof)
4514 index = cp_parser_constant_expression (parser, false, NULL);
4515 else
4516 index = cp_parser_expression (parser, /*cast_p=*/false);
4517
4518 /* Look for the closing `]'. */
4519 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4520
4521 /* Build the ARRAY_REF. */
4522 postfix_expression = grok_array_decl (postfix_expression, index);
4523
4524 /* When not doing offsetof, array references are not permitted in
4525 constant-expressions. */
4526 if (!for_offsetof
4527 && (cp_parser_non_integral_constant_expression
4528 (parser, "an array reference")))
4529 postfix_expression = error_mark_node;
4530
4531 return postfix_expression;
4532 }
4533
4534 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4535 by cp_parser_builtin_offsetof. We're looking for
4536
4537 postfix-expression . template [opt] id-expression
4538 postfix-expression . pseudo-destructor-name
4539 postfix-expression -> template [opt] id-expression
4540 postfix-expression -> pseudo-destructor-name
4541
4542 FOR_OFFSETOF is set if we're being called in that context. That sorta
4543 limits what of the above we'll actually accept, but nevermind.
4544 TOKEN_TYPE is the "." or "->" token, which will already have been
4545 removed from the stream. */
4546
4547 static tree
4548 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4549 enum cpp_ttype token_type,
4550 tree postfix_expression,
4551 bool for_offsetof, cp_id_kind *idk)
4552 {
4553 tree name;
4554 bool dependent_p;
4555 bool pseudo_destructor_p;
4556 tree scope = NULL_TREE;
4557
4558 /* If this is a `->' operator, dereference the pointer. */
4559 if (token_type == CPP_DEREF)
4560 postfix_expression = build_x_arrow (postfix_expression);
4561 /* Check to see whether or not the expression is type-dependent. */
4562 dependent_p = type_dependent_expression_p (postfix_expression);
4563 /* The identifier following the `->' or `.' is not qualified. */
4564 parser->scope = NULL_TREE;
4565 parser->qualifying_scope = NULL_TREE;
4566 parser->object_scope = NULL_TREE;
4567 *idk = CP_ID_KIND_NONE;
4568 /* Enter the scope corresponding to the type of the object
4569 given by the POSTFIX_EXPRESSION. */
4570 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4571 {
4572 scope = TREE_TYPE (postfix_expression);
4573 /* According to the standard, no expression should ever have
4574 reference type. Unfortunately, we do not currently match
4575 the standard in this respect in that our internal representation
4576 of an expression may have reference type even when the standard
4577 says it does not. Therefore, we have to manually obtain the
4578 underlying type here. */
4579 scope = non_reference (scope);
4580 /* The type of the POSTFIX_EXPRESSION must be complete. */
4581 if (scope == unknown_type_node)
4582 {
4583 error ("%qE does not have class type", postfix_expression);
4584 scope = NULL_TREE;
4585 }
4586 else
4587 scope = complete_type_or_else (scope, NULL_TREE);
4588 /* Let the name lookup machinery know that we are processing a
4589 class member access expression. */
4590 parser->context->object_type = scope;
4591 /* If something went wrong, we want to be able to discern that case,
4592 as opposed to the case where there was no SCOPE due to the type
4593 of expression being dependent. */
4594 if (!scope)
4595 scope = error_mark_node;
4596 /* If the SCOPE was erroneous, make the various semantic analysis
4597 functions exit quickly -- and without issuing additional error
4598 messages. */
4599 if (scope == error_mark_node)
4600 postfix_expression = error_mark_node;
4601 }
4602
4603 /* Assume this expression is not a pseudo-destructor access. */
4604 pseudo_destructor_p = false;
4605
4606 /* If the SCOPE is a scalar type, then, if this is a valid program,
4607 we must be looking at a pseudo-destructor-name. */
4608 if (scope && SCALAR_TYPE_P (scope))
4609 {
4610 tree s;
4611 tree type;
4612
4613 cp_parser_parse_tentatively (parser);
4614 /* Parse the pseudo-destructor-name. */
4615 s = NULL_TREE;
4616 cp_parser_pseudo_destructor_name (parser, &s, &type);
4617 if (cp_parser_parse_definitely (parser))
4618 {
4619 pseudo_destructor_p = true;
4620 postfix_expression
4621 = finish_pseudo_destructor_expr (postfix_expression,
4622 s, TREE_TYPE (type));
4623 }
4624 }
4625
4626 if (!pseudo_destructor_p)
4627 {
4628 /* If the SCOPE is not a scalar type, we are looking at an
4629 ordinary class member access expression, rather than a
4630 pseudo-destructor-name. */
4631 bool template_p;
4632 /* Parse the id-expression. */
4633 name = (cp_parser_id_expression
4634 (parser,
4635 cp_parser_optional_template_keyword (parser),
4636 /*check_dependency_p=*/true,
4637 &template_p,
4638 /*declarator_p=*/false,
4639 /*optional_p=*/false));
4640 /* In general, build a SCOPE_REF if the member name is qualified.
4641 However, if the name was not dependent and has already been
4642 resolved; there is no need to build the SCOPE_REF. For example;
4643
4644 struct X { void f(); };
4645 template <typename T> void f(T* t) { t->X::f(); }
4646
4647 Even though "t" is dependent, "X::f" is not and has been resolved
4648 to a BASELINK; there is no need to include scope information. */
4649
4650 /* But we do need to remember that there was an explicit scope for
4651 virtual function calls. */
4652 if (parser->scope)
4653 *idk = CP_ID_KIND_QUALIFIED;
4654
4655 /* If the name is a template-id that names a type, we will get a
4656 TYPE_DECL here. That is invalid code. */
4657 if (TREE_CODE (name) == TYPE_DECL)
4658 {
4659 error ("invalid use of %qD", name);
4660 postfix_expression = error_mark_node;
4661 }
4662 else
4663 {
4664 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4665 {
4666 name = build_qualified_name (/*type=*/NULL_TREE,
4667 parser->scope,
4668 name,
4669 template_p);
4670 parser->scope = NULL_TREE;
4671 parser->qualifying_scope = NULL_TREE;
4672 parser->object_scope = NULL_TREE;
4673 }
4674 if (scope && name && BASELINK_P (name))
4675 adjust_result_of_qualified_name_lookup
4676 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4677 postfix_expression
4678 = finish_class_member_access_expr (postfix_expression, name,
4679 template_p);
4680 }
4681 }
4682
4683 /* We no longer need to look up names in the scope of the object on
4684 the left-hand side of the `.' or `->' operator. */
4685 parser->context->object_type = NULL_TREE;
4686
4687 /* Outside of offsetof, these operators may not appear in
4688 constant-expressions. */
4689 if (!for_offsetof
4690 && (cp_parser_non_integral_constant_expression
4691 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4692 postfix_expression = error_mark_node;
4693
4694 return postfix_expression;
4695 }
4696
4697 /* Parse a parenthesized expression-list.
4698
4699 expression-list:
4700 assignment-expression
4701 expression-list, assignment-expression
4702
4703 attribute-list:
4704 expression-list
4705 identifier
4706 identifier, expression-list
4707
4708 CAST_P is true if this expression is the target of a cast.
4709
4710 Returns a TREE_LIST. The TREE_VALUE of each node is a
4711 representation of an assignment-expression. Note that a TREE_LIST
4712 is returned even if there is only a single expression in the list.
4713 error_mark_node is returned if the ( and or ) are
4714 missing. NULL_TREE is returned on no expressions. The parentheses
4715 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4716 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4717 indicates whether or not all of the expressions in the list were
4718 constant. */
4719
4720 static tree
4721 cp_parser_parenthesized_expression_list (cp_parser* parser,
4722 bool is_attribute_list,
4723 bool cast_p,
4724 bool *non_constant_p)
4725 {
4726 tree expression_list = NULL_TREE;
4727 bool fold_expr_p = is_attribute_list;
4728 tree identifier = NULL_TREE;
4729
4730 /* Assume all the expressions will be constant. */
4731 if (non_constant_p)
4732 *non_constant_p = false;
4733
4734 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4735 return error_mark_node;
4736
4737 /* Consume expressions until there are no more. */
4738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4739 while (true)
4740 {
4741 tree expr;
4742
4743 /* At the beginning of attribute lists, check to see if the
4744 next token is an identifier. */
4745 if (is_attribute_list
4746 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4747 {
4748 cp_token *token;
4749
4750 /* Consume the identifier. */
4751 token = cp_lexer_consume_token (parser->lexer);
4752 /* Save the identifier. */
4753 identifier = token->value;
4754 }
4755 else
4756 {
4757 /* Parse the next assignment-expression. */
4758 if (non_constant_p)
4759 {
4760 bool expr_non_constant_p;
4761 expr = (cp_parser_constant_expression
4762 (parser, /*allow_non_constant_p=*/true,
4763 &expr_non_constant_p));
4764 if (expr_non_constant_p)
4765 *non_constant_p = true;
4766 }
4767 else
4768 expr = cp_parser_assignment_expression (parser, cast_p);
4769
4770 if (fold_expr_p)
4771 expr = fold_non_dependent_expr (expr);
4772
4773 /* Add it to the list. We add error_mark_node
4774 expressions to the list, so that we can still tell if
4775 the correct form for a parenthesized expression-list
4776 is found. That gives better errors. */
4777 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4778
4779 if (expr == error_mark_node)
4780 goto skip_comma;
4781 }
4782
4783 /* After the first item, attribute lists look the same as
4784 expression lists. */
4785 is_attribute_list = false;
4786
4787 get_comma:;
4788 /* If the next token isn't a `,', then we are done. */
4789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4790 break;
4791
4792 /* Otherwise, consume the `,' and keep going. */
4793 cp_lexer_consume_token (parser->lexer);
4794 }
4795
4796 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4797 {
4798 int ending;
4799
4800 skip_comma:;
4801 /* We try and resync to an unnested comma, as that will give the
4802 user better diagnostics. */
4803 ending = cp_parser_skip_to_closing_parenthesis (parser,
4804 /*recovering=*/true,
4805 /*or_comma=*/true,
4806 /*consume_paren=*/true);
4807 if (ending < 0)
4808 goto get_comma;
4809 if (!ending)
4810 return error_mark_node;
4811 }
4812
4813 /* We built up the list in reverse order so we must reverse it now. */
4814 expression_list = nreverse (expression_list);
4815 if (identifier)
4816 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4817
4818 return expression_list;
4819 }
4820
4821 /* Parse a pseudo-destructor-name.
4822
4823 pseudo-destructor-name:
4824 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4825 :: [opt] nested-name-specifier template template-id :: ~ type-name
4826 :: [opt] nested-name-specifier [opt] ~ type-name
4827
4828 If either of the first two productions is used, sets *SCOPE to the
4829 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4830 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4831 or ERROR_MARK_NODE if the parse fails. */
4832
4833 static void
4834 cp_parser_pseudo_destructor_name (cp_parser* parser,
4835 tree* scope,
4836 tree* type)
4837 {
4838 bool nested_name_specifier_p;
4839
4840 /* Assume that things will not work out. */
4841 *type = error_mark_node;
4842
4843 /* Look for the optional `::' operator. */
4844 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4845 /* Look for the optional nested-name-specifier. */
4846 nested_name_specifier_p
4847 = (cp_parser_nested_name_specifier_opt (parser,
4848 /*typename_keyword_p=*/false,
4849 /*check_dependency_p=*/true,
4850 /*type_p=*/false,
4851 /*is_declaration=*/true)
4852 != NULL_TREE);
4853 /* Now, if we saw a nested-name-specifier, we might be doing the
4854 second production. */
4855 if (nested_name_specifier_p
4856 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4857 {
4858 /* Consume the `template' keyword. */
4859 cp_lexer_consume_token (parser->lexer);
4860 /* Parse the template-id. */
4861 cp_parser_template_id (parser,
4862 /*template_keyword_p=*/true,
4863 /*check_dependency_p=*/false,
4864 /*is_declaration=*/true);
4865 /* Look for the `::' token. */
4866 cp_parser_require (parser, CPP_SCOPE, "`::'");
4867 }
4868 /* If the next token is not a `~', then there might be some
4869 additional qualification. */
4870 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4871 {
4872 /* Look for the type-name. */
4873 *scope = TREE_TYPE (cp_parser_type_name (parser));
4874
4875 if (*scope == error_mark_node)
4876 return;
4877
4878 /* If we don't have ::~, then something has gone wrong. Since
4879 the only caller of this function is looking for something
4880 after `.' or `->' after a scalar type, most likely the
4881 program is trying to get a member of a non-aggregate
4882 type. */
4883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4884 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4885 {
4886 cp_parser_error (parser, "request for member of non-aggregate type");
4887 return;
4888 }
4889
4890 /* Look for the `::' token. */
4891 cp_parser_require (parser, CPP_SCOPE, "`::'");
4892 }
4893 else
4894 *scope = NULL_TREE;
4895
4896 /* Look for the `~'. */
4897 cp_parser_require (parser, CPP_COMPL, "`~'");
4898 /* Look for the type-name again. We are not responsible for
4899 checking that it matches the first type-name. */
4900 *type = cp_parser_type_name (parser);
4901 }
4902
4903 /* Parse a unary-expression.
4904
4905 unary-expression:
4906 postfix-expression
4907 ++ cast-expression
4908 -- cast-expression
4909 unary-operator cast-expression
4910 sizeof unary-expression
4911 sizeof ( type-id )
4912 new-expression
4913 delete-expression
4914
4915 GNU Extensions:
4916
4917 unary-expression:
4918 __extension__ cast-expression
4919 __alignof__ unary-expression
4920 __alignof__ ( type-id )
4921 __real__ cast-expression
4922 __imag__ cast-expression
4923 && identifier
4924
4925 ADDRESS_P is true iff the unary-expression is appearing as the
4926 operand of the `&' operator. CAST_P is true if this expression is
4927 the target of a cast.
4928
4929 Returns a representation of the expression. */
4930
4931 static tree
4932 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4933 {
4934 cp_token *token;
4935 enum tree_code unary_operator;
4936
4937 /* Peek at the next token. */
4938 token = cp_lexer_peek_token (parser->lexer);
4939 /* Some keywords give away the kind of expression. */
4940 if (token->type == CPP_KEYWORD)
4941 {
4942 enum rid keyword = token->keyword;
4943
4944 switch (keyword)
4945 {
4946 case RID_ALIGNOF:
4947 case RID_SIZEOF:
4948 {
4949 tree operand;
4950 enum tree_code op;
4951
4952 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4953 /* Consume the token. */
4954 cp_lexer_consume_token (parser->lexer);
4955 /* Parse the operand. */
4956 operand = cp_parser_sizeof_operand (parser, keyword);
4957
4958 if (TYPE_P (operand))
4959 return cxx_sizeof_or_alignof_type (operand, op, true);
4960 else
4961 return cxx_sizeof_or_alignof_expr (operand, op);
4962 }
4963
4964 case RID_NEW:
4965 return cp_parser_new_expression (parser);
4966
4967 case RID_DELETE:
4968 return cp_parser_delete_expression (parser);
4969
4970 case RID_EXTENSION:
4971 {
4972 /* The saved value of the PEDANTIC flag. */
4973 int saved_pedantic;
4974 tree expr;
4975
4976 /* Save away the PEDANTIC flag. */
4977 cp_parser_extension_opt (parser, &saved_pedantic);
4978 /* Parse the cast-expression. */
4979 expr = cp_parser_simple_cast_expression (parser);
4980 /* Restore the PEDANTIC flag. */
4981 pedantic = saved_pedantic;
4982
4983 return expr;
4984 }
4985
4986 case RID_REALPART:
4987 case RID_IMAGPART:
4988 {
4989 tree expression;
4990
4991 /* Consume the `__real__' or `__imag__' token. */
4992 cp_lexer_consume_token (parser->lexer);
4993 /* Parse the cast-expression. */
4994 expression = cp_parser_simple_cast_expression (parser);
4995 /* Create the complete representation. */
4996 return build_x_unary_op ((keyword == RID_REALPART
4997 ? REALPART_EXPR : IMAGPART_EXPR),
4998 expression);
4999 }
5000 break;
5001
5002 default:
5003 break;
5004 }
5005 }
5006
5007 /* Look for the `:: new' and `:: delete', which also signal the
5008 beginning of a new-expression, or delete-expression,
5009 respectively. If the next token is `::', then it might be one of
5010 these. */
5011 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5012 {
5013 enum rid keyword;
5014
5015 /* See if the token after the `::' is one of the keywords in
5016 which we're interested. */
5017 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5018 /* If it's `new', we have a new-expression. */
5019 if (keyword == RID_NEW)
5020 return cp_parser_new_expression (parser);
5021 /* Similarly, for `delete'. */
5022 else if (keyword == RID_DELETE)
5023 return cp_parser_delete_expression (parser);
5024 }
5025
5026 /* Look for a unary operator. */
5027 unary_operator = cp_parser_unary_operator (token);
5028 /* The `++' and `--' operators can be handled similarly, even though
5029 they are not technically unary-operators in the grammar. */
5030 if (unary_operator == ERROR_MARK)
5031 {
5032 if (token->type == CPP_PLUS_PLUS)
5033 unary_operator = PREINCREMENT_EXPR;
5034 else if (token->type == CPP_MINUS_MINUS)
5035 unary_operator = PREDECREMENT_EXPR;
5036 /* Handle the GNU address-of-label extension. */
5037 else if (cp_parser_allow_gnu_extensions_p (parser)
5038 && token->type == CPP_AND_AND)
5039 {
5040 tree identifier;
5041
5042 /* Consume the '&&' token. */
5043 cp_lexer_consume_token (parser->lexer);
5044 /* Look for the identifier. */
5045 identifier = cp_parser_identifier (parser);
5046 /* Create an expression representing the address. */
5047 return finish_label_address_expr (identifier);
5048 }
5049 }
5050 if (unary_operator != ERROR_MARK)
5051 {
5052 tree cast_expression;
5053 tree expression = error_mark_node;
5054 const char *non_constant_p = NULL;
5055
5056 /* Consume the operator token. */
5057 token = cp_lexer_consume_token (parser->lexer);
5058 /* Parse the cast-expression. */
5059 cast_expression
5060 = cp_parser_cast_expression (parser,
5061 unary_operator == ADDR_EXPR,
5062 /*cast_p=*/false);
5063 /* Now, build an appropriate representation. */
5064 switch (unary_operator)
5065 {
5066 case INDIRECT_REF:
5067 non_constant_p = "`*'";
5068 expression = build_x_indirect_ref (cast_expression, "unary *");
5069 break;
5070
5071 case ADDR_EXPR:
5072 non_constant_p = "`&'";
5073 /* Fall through. */
5074 case BIT_NOT_EXPR:
5075 expression = build_x_unary_op (unary_operator, cast_expression);
5076 break;
5077
5078 case PREINCREMENT_EXPR:
5079 case PREDECREMENT_EXPR:
5080 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5081 ? "`++'" : "`--'");
5082 /* Fall through. */
5083 case UNARY_PLUS_EXPR:
5084 case NEGATE_EXPR:
5085 case TRUTH_NOT_EXPR:
5086 expression = finish_unary_op_expr (unary_operator, cast_expression);
5087 break;
5088
5089 default:
5090 gcc_unreachable ();
5091 }
5092
5093 if (non_constant_p
5094 && cp_parser_non_integral_constant_expression (parser,
5095 non_constant_p))
5096 expression = error_mark_node;
5097
5098 return expression;
5099 }
5100
5101 return cp_parser_postfix_expression (parser, address_p, cast_p);
5102 }
5103
5104 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5105 unary-operator, the corresponding tree code is returned. */
5106
5107 static enum tree_code
5108 cp_parser_unary_operator (cp_token* token)
5109 {
5110 switch (token->type)
5111 {
5112 case CPP_MULT:
5113 return INDIRECT_REF;
5114
5115 case CPP_AND:
5116 return ADDR_EXPR;
5117
5118 case CPP_PLUS:
5119 return UNARY_PLUS_EXPR;
5120
5121 case CPP_MINUS:
5122 return NEGATE_EXPR;
5123
5124 case CPP_NOT:
5125 return TRUTH_NOT_EXPR;
5126
5127 case CPP_COMPL:
5128 return BIT_NOT_EXPR;
5129
5130 default:
5131 return ERROR_MARK;
5132 }
5133 }
5134
5135 /* Parse a new-expression.
5136
5137 new-expression:
5138 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5139 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5140
5141 Returns a representation of the expression. */
5142
5143 static tree
5144 cp_parser_new_expression (cp_parser* parser)
5145 {
5146 bool global_scope_p;
5147 tree placement;
5148 tree type;
5149 tree initializer;
5150 tree nelts;
5151
5152 /* Look for the optional `::' operator. */
5153 global_scope_p
5154 = (cp_parser_global_scope_opt (parser,
5155 /*current_scope_valid_p=*/false)
5156 != NULL_TREE);
5157 /* Look for the `new' operator. */
5158 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5159 /* There's no easy way to tell a new-placement from the
5160 `( type-id )' construct. */
5161 cp_parser_parse_tentatively (parser);
5162 /* Look for a new-placement. */
5163 placement = cp_parser_new_placement (parser);
5164 /* If that didn't work out, there's no new-placement. */
5165 if (!cp_parser_parse_definitely (parser))
5166 placement = NULL_TREE;
5167
5168 /* If the next token is a `(', then we have a parenthesized
5169 type-id. */
5170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5171 {
5172 /* Consume the `('. */
5173 cp_lexer_consume_token (parser->lexer);
5174 /* Parse the type-id. */
5175 type = cp_parser_type_id (parser);
5176 /* Look for the closing `)'. */
5177 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5178 /* There should not be a direct-new-declarator in this production,
5179 but GCC used to allowed this, so we check and emit a sensible error
5180 message for this case. */
5181 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5182 {
5183 error ("array bound forbidden after parenthesized type-id");
5184 inform ("try removing the parentheses around the type-id");
5185 cp_parser_direct_new_declarator (parser);
5186 }
5187 nelts = NULL_TREE;
5188 }
5189 /* Otherwise, there must be a new-type-id. */
5190 else
5191 type = cp_parser_new_type_id (parser, &nelts);
5192
5193 /* If the next token is a `(', then we have a new-initializer. */
5194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5195 initializer = cp_parser_new_initializer (parser);
5196 else
5197 initializer = NULL_TREE;
5198
5199 /* A new-expression may not appear in an integral constant
5200 expression. */
5201 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5202 return error_mark_node;
5203
5204 /* Create a representation of the new-expression. */
5205 return build_new (placement, type, nelts, initializer, global_scope_p);
5206 }
5207
5208 /* Parse a new-placement.
5209
5210 new-placement:
5211 ( expression-list )
5212
5213 Returns the same representation as for an expression-list. */
5214
5215 static tree
5216 cp_parser_new_placement (cp_parser* parser)
5217 {
5218 tree expression_list;
5219
5220 /* Parse the expression-list. */
5221 expression_list = (cp_parser_parenthesized_expression_list
5222 (parser, false, /*cast_p=*/false,
5223 /*non_constant_p=*/NULL));
5224
5225 return expression_list;
5226 }
5227
5228 /* Parse a new-type-id.
5229
5230 new-type-id:
5231 type-specifier-seq new-declarator [opt]
5232
5233 Returns the TYPE allocated. If the new-type-id indicates an array
5234 type, *NELTS is set to the number of elements in the last array
5235 bound; the TYPE will not include the last array bound. */
5236
5237 static tree
5238 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5239 {
5240 cp_decl_specifier_seq type_specifier_seq;
5241 cp_declarator *new_declarator;
5242 cp_declarator *declarator;
5243 cp_declarator *outer_declarator;
5244 const char *saved_message;
5245 tree type;
5246
5247 /* The type-specifier sequence must not contain type definitions.
5248 (It cannot contain declarations of new types either, but if they
5249 are not definitions we will catch that because they are not
5250 complete.) */
5251 saved_message = parser->type_definition_forbidden_message;
5252 parser->type_definition_forbidden_message
5253 = "types may not be defined in a new-type-id";
5254 /* Parse the type-specifier-seq. */
5255 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5256 &type_specifier_seq);
5257 /* Restore the old message. */
5258 parser->type_definition_forbidden_message = saved_message;
5259 /* Parse the new-declarator. */
5260 new_declarator = cp_parser_new_declarator_opt (parser);
5261
5262 /* Determine the number of elements in the last array dimension, if
5263 any. */
5264 *nelts = NULL_TREE;
5265 /* Skip down to the last array dimension. */
5266 declarator = new_declarator;
5267 outer_declarator = NULL;
5268 while (declarator && (declarator->kind == cdk_pointer
5269 || declarator->kind == cdk_ptrmem))
5270 {
5271 outer_declarator = declarator;
5272 declarator = declarator->declarator;
5273 }
5274 while (declarator
5275 && declarator->kind == cdk_array
5276 && declarator->declarator
5277 && declarator->declarator->kind == cdk_array)
5278 {
5279 outer_declarator = declarator;
5280 declarator = declarator->declarator;
5281 }
5282
5283 if (declarator && declarator->kind == cdk_array)
5284 {
5285 *nelts = declarator->u.array.bounds;
5286 if (*nelts == error_mark_node)
5287 *nelts = integer_one_node;
5288
5289 if (outer_declarator)
5290 outer_declarator->declarator = declarator->declarator;
5291 else
5292 new_declarator = NULL;
5293 }
5294
5295 type = groktypename (&type_specifier_seq, new_declarator);
5296 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5297 {
5298 *nelts = array_type_nelts_top (type);
5299 type = TREE_TYPE (type);
5300 }
5301 return type;
5302 }
5303
5304 /* Parse an (optional) new-declarator.
5305
5306 new-declarator:
5307 ptr-operator new-declarator [opt]
5308 direct-new-declarator
5309
5310 Returns the declarator. */
5311
5312 static cp_declarator *
5313 cp_parser_new_declarator_opt (cp_parser* parser)
5314 {
5315 enum tree_code code;
5316 tree type;
5317 cp_cv_quals cv_quals;
5318
5319 /* We don't know if there's a ptr-operator next, or not. */
5320 cp_parser_parse_tentatively (parser);
5321 /* Look for a ptr-operator. */
5322 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5323 /* If that worked, look for more new-declarators. */
5324 if (cp_parser_parse_definitely (parser))
5325 {
5326 cp_declarator *declarator;
5327
5328 /* Parse another optional declarator. */
5329 declarator = cp_parser_new_declarator_opt (parser);
5330
5331 /* Create the representation of the declarator. */
5332 if (type)
5333 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5334 else if (code == INDIRECT_REF)
5335 declarator = make_pointer_declarator (cv_quals, declarator);
5336 else
5337 declarator = make_reference_declarator (cv_quals, declarator);
5338
5339 return declarator;
5340 }
5341
5342 /* If the next token is a `[', there is a direct-new-declarator. */
5343 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5344 return cp_parser_direct_new_declarator (parser);
5345
5346 return NULL;
5347 }
5348
5349 /* Parse a direct-new-declarator.
5350
5351 direct-new-declarator:
5352 [ expression ]
5353 direct-new-declarator [constant-expression]
5354
5355 */
5356
5357 static cp_declarator *
5358 cp_parser_direct_new_declarator (cp_parser* parser)
5359 {
5360 cp_declarator *declarator = NULL;
5361
5362 while (true)
5363 {
5364 tree expression;
5365
5366 /* Look for the opening `['. */
5367 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5368 /* The first expression is not required to be constant. */
5369 if (!declarator)
5370 {
5371 expression = cp_parser_expression (parser, /*cast_p=*/false);
5372 /* The standard requires that the expression have integral
5373 type. DR 74 adds enumeration types. We believe that the
5374 real intent is that these expressions be handled like the
5375 expression in a `switch' condition, which also allows
5376 classes with a single conversion to integral or
5377 enumeration type. */
5378 if (!processing_template_decl)
5379 {
5380 expression
5381 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5382 expression,
5383 /*complain=*/true);
5384 if (!expression)
5385 {
5386 error ("expression in new-declarator must have integral "
5387 "or enumeration type");
5388 expression = error_mark_node;
5389 }
5390 }
5391 }
5392 /* But all the other expressions must be. */
5393 else
5394 expression
5395 = cp_parser_constant_expression (parser,
5396 /*allow_non_constant=*/false,
5397 NULL);
5398 /* Look for the closing `]'. */
5399 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5400
5401 /* Add this bound to the declarator. */
5402 declarator = make_array_declarator (declarator, expression);
5403
5404 /* If the next token is not a `[', then there are no more
5405 bounds. */
5406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5407 break;
5408 }
5409
5410 return declarator;
5411 }
5412
5413 /* Parse a new-initializer.
5414
5415 new-initializer:
5416 ( expression-list [opt] )
5417
5418 Returns a representation of the expression-list. If there is no
5419 expression-list, VOID_ZERO_NODE is returned. */
5420
5421 static tree
5422 cp_parser_new_initializer (cp_parser* parser)
5423 {
5424 tree expression_list;
5425
5426 expression_list = (cp_parser_parenthesized_expression_list
5427 (parser, false, /*cast_p=*/false,
5428 /*non_constant_p=*/NULL));
5429 if (!expression_list)
5430 expression_list = void_zero_node;
5431
5432 return expression_list;
5433 }
5434
5435 /* Parse a delete-expression.
5436
5437 delete-expression:
5438 :: [opt] delete cast-expression
5439 :: [opt] delete [ ] cast-expression
5440
5441 Returns a representation of the expression. */
5442
5443 static tree
5444 cp_parser_delete_expression (cp_parser* parser)
5445 {
5446 bool global_scope_p;
5447 bool array_p;
5448 tree expression;
5449
5450 /* Look for the optional `::' operator. */
5451 global_scope_p
5452 = (cp_parser_global_scope_opt (parser,
5453 /*current_scope_valid_p=*/false)
5454 != NULL_TREE);
5455 /* Look for the `delete' keyword. */
5456 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5457 /* See if the array syntax is in use. */
5458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5459 {
5460 /* Consume the `[' token. */
5461 cp_lexer_consume_token (parser->lexer);
5462 /* Look for the `]' token. */
5463 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5464 /* Remember that this is the `[]' construct. */
5465 array_p = true;
5466 }
5467 else
5468 array_p = false;
5469
5470 /* Parse the cast-expression. */
5471 expression = cp_parser_simple_cast_expression (parser);
5472
5473 /* A delete-expression may not appear in an integral constant
5474 expression. */
5475 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5476 return error_mark_node;
5477
5478 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5479 }
5480
5481 /* Parse a cast-expression.
5482
5483 cast-expression:
5484 unary-expression
5485 ( type-id ) cast-expression
5486
5487 ADDRESS_P is true iff the unary-expression is appearing as the
5488 operand of the `&' operator. CAST_P is true if this expression is
5489 the target of a cast.
5490
5491 Returns a representation of the expression. */
5492
5493 static tree
5494 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5495 {
5496 /* If it's a `(', then we might be looking at a cast. */
5497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5498 {
5499 tree type = NULL_TREE;
5500 tree expr = NULL_TREE;
5501 bool compound_literal_p;
5502 const char *saved_message;
5503
5504 /* There's no way to know yet whether or not this is a cast.
5505 For example, `(int (3))' is a unary-expression, while `(int)
5506 3' is a cast. So, we resort to parsing tentatively. */
5507 cp_parser_parse_tentatively (parser);
5508 /* Types may not be defined in a cast. */
5509 saved_message = parser->type_definition_forbidden_message;
5510 parser->type_definition_forbidden_message
5511 = "types may not be defined in casts";
5512 /* Consume the `('. */
5513 cp_lexer_consume_token (parser->lexer);
5514 /* A very tricky bit is that `(struct S) { 3 }' is a
5515 compound-literal (which we permit in C++ as an extension).
5516 But, that construct is not a cast-expression -- it is a
5517 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5518 is legal; if the compound-literal were a cast-expression,
5519 you'd need an extra set of parentheses.) But, if we parse
5520 the type-id, and it happens to be a class-specifier, then we
5521 will commit to the parse at that point, because we cannot
5522 undo the action that is done when creating a new class. So,
5523 then we cannot back up and do a postfix-expression.
5524
5525 Therefore, we scan ahead to the closing `)', and check to see
5526 if the token after the `)' is a `{'. If so, we are not
5527 looking at a cast-expression.
5528
5529 Save tokens so that we can put them back. */
5530 cp_lexer_save_tokens (parser->lexer);
5531 /* Skip tokens until the next token is a closing parenthesis.
5532 If we find the closing `)', and the next token is a `{', then
5533 we are looking at a compound-literal. */
5534 compound_literal_p
5535 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5536 /*consume_paren=*/true)
5537 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5538 /* Roll back the tokens we skipped. */
5539 cp_lexer_rollback_tokens (parser->lexer);
5540 /* If we were looking at a compound-literal, simulate an error
5541 so that the call to cp_parser_parse_definitely below will
5542 fail. */
5543 if (compound_literal_p)
5544 cp_parser_simulate_error (parser);
5545 else
5546 {
5547 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5548 parser->in_type_id_in_expr_p = true;
5549 /* Look for the type-id. */
5550 type = cp_parser_type_id (parser);
5551 /* Look for the closing `)'. */
5552 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5553 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5554 }
5555
5556 /* Restore the saved message. */
5557 parser->type_definition_forbidden_message = saved_message;
5558
5559 /* If ok so far, parse the dependent expression. We cannot be
5560 sure it is a cast. Consider `(T ())'. It is a parenthesized
5561 ctor of T, but looks like a cast to function returning T
5562 without a dependent expression. */
5563 if (!cp_parser_error_occurred (parser))
5564 expr = cp_parser_cast_expression (parser,
5565 /*address_p=*/false,
5566 /*cast_p=*/true);
5567
5568 if (cp_parser_parse_definitely (parser))
5569 {
5570 /* Warn about old-style casts, if so requested. */
5571 if (warn_old_style_cast
5572 && !in_system_header
5573 && !VOID_TYPE_P (type)
5574 && current_lang_name != lang_name_c)
5575 warning (OPT_Wold_style_cast, "use of old-style cast");
5576
5577 /* Only type conversions to integral or enumeration types
5578 can be used in constant-expressions. */
5579 if (!cast_valid_in_integral_constant_expression_p (type)
5580 && (cp_parser_non_integral_constant_expression
5581 (parser,
5582 "a cast to a type other than an integral or "
5583 "enumeration type")))
5584 return error_mark_node;
5585
5586 /* Perform the cast. */
5587 expr = build_c_cast (type, expr);
5588 return expr;
5589 }
5590 }
5591
5592 /* If we get here, then it's not a cast, so it must be a
5593 unary-expression. */
5594 return cp_parser_unary_expression (parser, address_p, cast_p);
5595 }
5596
5597 /* Parse a binary expression of the general form:
5598
5599 pm-expression:
5600 cast-expression
5601 pm-expression .* cast-expression
5602 pm-expression ->* cast-expression
5603
5604 multiplicative-expression:
5605 pm-expression
5606 multiplicative-expression * pm-expression
5607 multiplicative-expression / pm-expression
5608 multiplicative-expression % pm-expression
5609
5610 additive-expression:
5611 multiplicative-expression
5612 additive-expression + multiplicative-expression
5613 additive-expression - multiplicative-expression
5614
5615 shift-expression:
5616 additive-expression
5617 shift-expression << additive-expression
5618 shift-expression >> additive-expression
5619
5620 relational-expression:
5621 shift-expression
5622 relational-expression < shift-expression
5623 relational-expression > shift-expression
5624 relational-expression <= shift-expression
5625 relational-expression >= shift-expression
5626
5627 GNU Extension:
5628
5629 relational-expression:
5630 relational-expression <? shift-expression
5631 relational-expression >? shift-expression
5632
5633 equality-expression:
5634 relational-expression
5635 equality-expression == relational-expression
5636 equality-expression != relational-expression
5637
5638 and-expression:
5639 equality-expression
5640 and-expression & equality-expression
5641
5642 exclusive-or-expression:
5643 and-expression
5644 exclusive-or-expression ^ and-expression
5645
5646 inclusive-or-expression:
5647 exclusive-or-expression
5648 inclusive-or-expression | exclusive-or-expression
5649
5650 logical-and-expression:
5651 inclusive-or-expression
5652 logical-and-expression && inclusive-or-expression
5653
5654 logical-or-expression:
5655 logical-and-expression
5656 logical-or-expression || logical-and-expression
5657
5658 All these are implemented with a single function like:
5659
5660 binary-expression:
5661 simple-cast-expression
5662 binary-expression <token> binary-expression
5663
5664 CAST_P is true if this expression is the target of a cast.
5665
5666 The binops_by_token map is used to get the tree codes for each <token> type.
5667 binary-expressions are associated according to a precedence table. */
5668
5669 #define TOKEN_PRECEDENCE(token) \
5670 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5671 ? PREC_NOT_OPERATOR \
5672 : binops_by_token[token->type].prec)
5673
5674 static tree
5675 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5676 {
5677 cp_parser_expression_stack stack;
5678 cp_parser_expression_stack_entry *sp = &stack[0];
5679 tree lhs, rhs;
5680 cp_token *token;
5681 enum tree_code tree_type;
5682 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5683 bool overloaded_p;
5684
5685 /* Parse the first expression. */
5686 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5687
5688 for (;;)
5689 {
5690 /* Get an operator token. */
5691 token = cp_lexer_peek_token (parser->lexer);
5692
5693 new_prec = TOKEN_PRECEDENCE (token);
5694
5695 /* Popping an entry off the stack means we completed a subexpression:
5696 - either we found a token which is not an operator (`>' where it is not
5697 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5698 will happen repeatedly;
5699 - or, we found an operator which has lower priority. This is the case
5700 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5701 parsing `3 * 4'. */
5702 if (new_prec <= prec)
5703 {
5704 if (sp == stack)
5705 break;
5706 else
5707 goto pop;
5708 }
5709
5710 get_rhs:
5711 tree_type = binops_by_token[token->type].tree_type;
5712
5713 /* We used the operator token. */
5714 cp_lexer_consume_token (parser->lexer);
5715
5716 /* Extract another operand. It may be the RHS of this expression
5717 or the LHS of a new, higher priority expression. */
5718 rhs = cp_parser_simple_cast_expression (parser);
5719
5720 /* Get another operator token. Look up its precedence to avoid
5721 building a useless (immediately popped) stack entry for common
5722 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5723 token = cp_lexer_peek_token (parser->lexer);
5724 lookahead_prec = TOKEN_PRECEDENCE (token);
5725 if (lookahead_prec > new_prec)
5726 {
5727 /* ... and prepare to parse the RHS of the new, higher priority
5728 expression. Since precedence levels on the stack are
5729 monotonically increasing, we do not have to care about
5730 stack overflows. */
5731 sp->prec = prec;
5732 sp->tree_type = tree_type;
5733 sp->lhs = lhs;
5734 sp++;
5735 lhs = rhs;
5736 prec = new_prec;
5737 new_prec = lookahead_prec;
5738 goto get_rhs;
5739
5740 pop:
5741 /* If the stack is not empty, we have parsed into LHS the right side
5742 (`4' in the example above) of an expression we had suspended.
5743 We can use the information on the stack to recover the LHS (`3')
5744 from the stack together with the tree code (`MULT_EXPR'), and
5745 the precedence of the higher level subexpression
5746 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5747 which will be used to actually build the additive expression. */
5748 --sp;
5749 prec = sp->prec;
5750 tree_type = sp->tree_type;
5751 rhs = lhs;
5752 lhs = sp->lhs;
5753 }
5754
5755 overloaded_p = false;
5756 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5757
5758 /* If the binary operator required the use of an overloaded operator,
5759 then this expression cannot be an integral constant-expression.
5760 An overloaded operator can be used even if both operands are
5761 otherwise permissible in an integral constant-expression if at
5762 least one of the operands is of enumeration type. */
5763
5764 if (overloaded_p
5765 && (cp_parser_non_integral_constant_expression
5766 (parser, "calls to overloaded operators")))
5767 return error_mark_node;
5768 }
5769
5770 return lhs;
5771 }
5772
5773
5774 /* Parse the `? expression : assignment-expression' part of a
5775 conditional-expression. The LOGICAL_OR_EXPR is the
5776 logical-or-expression that started the conditional-expression.
5777 Returns a representation of the entire conditional-expression.
5778
5779 This routine is used by cp_parser_assignment_expression.
5780
5781 ? expression : assignment-expression
5782
5783 GNU Extensions:
5784
5785 ? : assignment-expression */
5786
5787 static tree
5788 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5789 {
5790 tree expr;
5791 tree assignment_expr;
5792
5793 /* Consume the `?' token. */
5794 cp_lexer_consume_token (parser->lexer);
5795 if (cp_parser_allow_gnu_extensions_p (parser)
5796 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5797 /* Implicit true clause. */
5798 expr = NULL_TREE;
5799 else
5800 /* Parse the expression. */
5801 expr = cp_parser_expression (parser, /*cast_p=*/false);
5802
5803 /* The next token should be a `:'. */
5804 cp_parser_require (parser, CPP_COLON, "`:'");
5805 /* Parse the assignment-expression. */
5806 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5807
5808 /* Build the conditional-expression. */
5809 return build_x_conditional_expr (logical_or_expr,
5810 expr,
5811 assignment_expr);
5812 }
5813
5814 /* Parse an assignment-expression.
5815
5816 assignment-expression:
5817 conditional-expression
5818 logical-or-expression assignment-operator assignment_expression
5819 throw-expression
5820
5821 CAST_P is true if this expression is the target of a cast.
5822
5823 Returns a representation for the expression. */
5824
5825 static tree
5826 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5827 {
5828 tree expr;
5829
5830 /* If the next token is the `throw' keyword, then we're looking at
5831 a throw-expression. */
5832 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5833 expr = cp_parser_throw_expression (parser);
5834 /* Otherwise, it must be that we are looking at a
5835 logical-or-expression. */
5836 else
5837 {
5838 /* Parse the binary expressions (logical-or-expression). */
5839 expr = cp_parser_binary_expression (parser, cast_p);
5840 /* If the next token is a `?' then we're actually looking at a
5841 conditional-expression. */
5842 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5843 return cp_parser_question_colon_clause (parser, expr);
5844 else
5845 {
5846 enum tree_code assignment_operator;
5847
5848 /* If it's an assignment-operator, we're using the second
5849 production. */
5850 assignment_operator
5851 = cp_parser_assignment_operator_opt (parser);
5852 if (assignment_operator != ERROR_MARK)
5853 {
5854 tree rhs;
5855
5856 /* Parse the right-hand side of the assignment. */
5857 rhs = cp_parser_assignment_expression (parser, cast_p);
5858 /* An assignment may not appear in a
5859 constant-expression. */
5860 if (cp_parser_non_integral_constant_expression (parser,
5861 "an assignment"))
5862 return error_mark_node;
5863 /* Build the assignment expression. */
5864 expr = build_x_modify_expr (expr,
5865 assignment_operator,
5866 rhs);
5867 }
5868 }
5869 }
5870
5871 return expr;
5872 }
5873
5874 /* Parse an (optional) assignment-operator.
5875
5876 assignment-operator: one of
5877 = *= /= %= += -= >>= <<= &= ^= |=
5878
5879 GNU Extension:
5880
5881 assignment-operator: one of
5882 <?= >?=
5883
5884 If the next token is an assignment operator, the corresponding tree
5885 code is returned, and the token is consumed. For example, for
5886 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5887 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5888 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5889 operator, ERROR_MARK is returned. */
5890
5891 static enum tree_code
5892 cp_parser_assignment_operator_opt (cp_parser* parser)
5893 {
5894 enum tree_code op;
5895 cp_token *token;
5896
5897 /* Peek at the next toen. */
5898 token = cp_lexer_peek_token (parser->lexer);
5899
5900 switch (token->type)
5901 {
5902 case CPP_EQ:
5903 op = NOP_EXPR;
5904 break;
5905
5906 case CPP_MULT_EQ:
5907 op = MULT_EXPR;
5908 break;
5909
5910 case CPP_DIV_EQ:
5911 op = TRUNC_DIV_EXPR;
5912 break;
5913
5914 case CPP_MOD_EQ:
5915 op = TRUNC_MOD_EXPR;
5916 break;
5917
5918 case CPP_PLUS_EQ:
5919 op = PLUS_EXPR;
5920 break;
5921
5922 case CPP_MINUS_EQ:
5923 op = MINUS_EXPR;
5924 break;
5925
5926 case CPP_RSHIFT_EQ:
5927 op = RSHIFT_EXPR;
5928 break;
5929
5930 case CPP_LSHIFT_EQ:
5931 op = LSHIFT_EXPR;
5932 break;
5933
5934 case CPP_AND_EQ:
5935 op = BIT_AND_EXPR;
5936 break;
5937
5938 case CPP_XOR_EQ:
5939 op = BIT_XOR_EXPR;
5940 break;
5941
5942 case CPP_OR_EQ:
5943 op = BIT_IOR_EXPR;
5944 break;
5945
5946 default:
5947 /* Nothing else is an assignment operator. */
5948 op = ERROR_MARK;
5949 }
5950
5951 /* If it was an assignment operator, consume it. */
5952 if (op != ERROR_MARK)
5953 cp_lexer_consume_token (parser->lexer);
5954
5955 return op;
5956 }
5957
5958 /* Parse an expression.
5959
5960 expression:
5961 assignment-expression
5962 expression , assignment-expression
5963
5964 CAST_P is true if this expression is the target of a cast.
5965
5966 Returns a representation of the expression. */
5967
5968 static tree
5969 cp_parser_expression (cp_parser* parser, bool cast_p)
5970 {
5971 tree expression = NULL_TREE;
5972
5973 while (true)
5974 {
5975 tree assignment_expression;
5976
5977 /* Parse the next assignment-expression. */
5978 assignment_expression
5979 = cp_parser_assignment_expression (parser, cast_p);
5980 /* If this is the first assignment-expression, we can just
5981 save it away. */
5982 if (!expression)
5983 expression = assignment_expression;
5984 else
5985 expression = build_x_compound_expr (expression,
5986 assignment_expression);
5987 /* If the next token is not a comma, then we are done with the
5988 expression. */
5989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5990 break;
5991 /* Consume the `,'. */
5992 cp_lexer_consume_token (parser->lexer);
5993 /* A comma operator cannot appear in a constant-expression. */
5994 if (cp_parser_non_integral_constant_expression (parser,
5995 "a comma operator"))
5996 expression = error_mark_node;
5997 }
5998
5999 return expression;
6000 }
6001
6002 /* Parse a constant-expression.
6003
6004 constant-expression:
6005 conditional-expression
6006
6007 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6008 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6009 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6010 is false, NON_CONSTANT_P should be NULL. */
6011
6012 static tree
6013 cp_parser_constant_expression (cp_parser* parser,
6014 bool allow_non_constant_p,
6015 bool *non_constant_p)
6016 {
6017 bool saved_integral_constant_expression_p;
6018 bool saved_allow_non_integral_constant_expression_p;
6019 bool saved_non_integral_constant_expression_p;
6020 tree expression;
6021
6022 /* It might seem that we could simply parse the
6023 conditional-expression, and then check to see if it were
6024 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6025 one that the compiler can figure out is constant, possibly after
6026 doing some simplifications or optimizations. The standard has a
6027 precise definition of constant-expression, and we must honor
6028 that, even though it is somewhat more restrictive.
6029
6030 For example:
6031
6032 int i[(2, 3)];
6033
6034 is not a legal declaration, because `(2, 3)' is not a
6035 constant-expression. The `,' operator is forbidden in a
6036 constant-expression. However, GCC's constant-folding machinery
6037 will fold this operation to an INTEGER_CST for `3'. */
6038
6039 /* Save the old settings. */
6040 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6041 saved_allow_non_integral_constant_expression_p
6042 = parser->allow_non_integral_constant_expression_p;
6043 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6044 /* We are now parsing a constant-expression. */
6045 parser->integral_constant_expression_p = true;
6046 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6047 parser->non_integral_constant_expression_p = false;
6048 /* Although the grammar says "conditional-expression", we parse an
6049 "assignment-expression", which also permits "throw-expression"
6050 and the use of assignment operators. In the case that
6051 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6052 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6053 actually essential that we look for an assignment-expression.
6054 For example, cp_parser_initializer_clauses uses this function to
6055 determine whether a particular assignment-expression is in fact
6056 constant. */
6057 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6058 /* Restore the old settings. */
6059 parser->integral_constant_expression_p
6060 = saved_integral_constant_expression_p;
6061 parser->allow_non_integral_constant_expression_p
6062 = saved_allow_non_integral_constant_expression_p;
6063 if (allow_non_constant_p)
6064 *non_constant_p = parser->non_integral_constant_expression_p;
6065 else if (parser->non_integral_constant_expression_p)
6066 expression = error_mark_node;
6067 parser->non_integral_constant_expression_p
6068 = saved_non_integral_constant_expression_p;
6069
6070 return expression;
6071 }
6072
6073 /* Parse __builtin_offsetof.
6074
6075 offsetof-expression:
6076 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6077
6078 offsetof-member-designator:
6079 id-expression
6080 | offsetof-member-designator "." id-expression
6081 | offsetof-member-designator "[" expression "]" */
6082
6083 static tree
6084 cp_parser_builtin_offsetof (cp_parser *parser)
6085 {
6086 int save_ice_p, save_non_ice_p;
6087 tree type, expr;
6088 cp_id_kind dummy;
6089
6090 /* We're about to accept non-integral-constant things, but will
6091 definitely yield an integral constant expression. Save and
6092 restore these values around our local parsing. */
6093 save_ice_p = parser->integral_constant_expression_p;
6094 save_non_ice_p = parser->non_integral_constant_expression_p;
6095
6096 /* Consume the "__builtin_offsetof" token. */
6097 cp_lexer_consume_token (parser->lexer);
6098 /* Consume the opening `('. */
6099 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6100 /* Parse the type-id. */
6101 type = cp_parser_type_id (parser);
6102 /* Look for the `,'. */
6103 cp_parser_require (parser, CPP_COMMA, "`,'");
6104
6105 /* Build the (type *)null that begins the traditional offsetof macro. */
6106 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6107
6108 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6109 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6110 true, &dummy);
6111 while (true)
6112 {
6113 cp_token *token = cp_lexer_peek_token (parser->lexer);
6114 switch (token->type)
6115 {
6116 case CPP_OPEN_SQUARE:
6117 /* offsetof-member-designator "[" expression "]" */
6118 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6119 break;
6120
6121 case CPP_DOT:
6122 /* offsetof-member-designator "." identifier */
6123 cp_lexer_consume_token (parser->lexer);
6124 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6125 true, &dummy);
6126 break;
6127
6128 case CPP_CLOSE_PAREN:
6129 /* Consume the ")" token. */
6130 cp_lexer_consume_token (parser->lexer);
6131 goto success;
6132
6133 default:
6134 /* Error. We know the following require will fail, but
6135 that gives the proper error message. */
6136 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6137 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6138 expr = error_mark_node;
6139 goto failure;
6140 }
6141 }
6142
6143 success:
6144 /* If we're processing a template, we can't finish the semantics yet.
6145 Otherwise we can fold the entire expression now. */
6146 if (processing_template_decl)
6147 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6148 else
6149 expr = finish_offsetof (expr);
6150
6151 failure:
6152 parser->integral_constant_expression_p = save_ice_p;
6153 parser->non_integral_constant_expression_p = save_non_ice_p;
6154
6155 return expr;
6156 }
6157
6158 /* Statements [gram.stmt.stmt] */
6159
6160 /* Parse a statement.
6161
6162 statement:
6163 labeled-statement
6164 expression-statement
6165 compound-statement
6166 selection-statement
6167 iteration-statement
6168 jump-statement
6169 declaration-statement
6170 try-block
6171
6172 IN_COMPOUND is true when the statement is nested inside a
6173 cp_parser_compound_statement; this matters for certain pragmas. */
6174
6175 static void
6176 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6177 bool in_compound)
6178 {
6179 tree statement;
6180 cp_token *token;
6181 location_t statement_location;
6182
6183 restart:
6184 /* There is no statement yet. */
6185 statement = NULL_TREE;
6186 /* Peek at the next token. */
6187 token = cp_lexer_peek_token (parser->lexer);
6188 /* Remember the location of the first token in the statement. */
6189 statement_location = token->location;
6190 /* If this is a keyword, then that will often determine what kind of
6191 statement we have. */
6192 if (token->type == CPP_KEYWORD)
6193 {
6194 enum rid keyword = token->keyword;
6195
6196 switch (keyword)
6197 {
6198 case RID_CASE:
6199 case RID_DEFAULT:
6200 /* Looks like a labeled-statement with a case label.
6201 Parse the label, and then use tail recursion to parse
6202 the statement. */
6203 cp_parser_label_for_labeled_statement (parser);
6204 goto restart;
6205
6206 case RID_IF:
6207 case RID_SWITCH:
6208 statement = cp_parser_selection_statement (parser);
6209 break;
6210
6211 case RID_WHILE:
6212 case RID_DO:
6213 case RID_FOR:
6214 statement = cp_parser_iteration_statement (parser);
6215 break;
6216
6217 case RID_BREAK:
6218 case RID_CONTINUE:
6219 case RID_RETURN:
6220 case RID_GOTO:
6221 statement = cp_parser_jump_statement (parser);
6222 break;
6223
6224 /* Objective-C++ exception-handling constructs. */
6225 case RID_AT_TRY:
6226 case RID_AT_CATCH:
6227 case RID_AT_FINALLY:
6228 case RID_AT_SYNCHRONIZED:
6229 case RID_AT_THROW:
6230 statement = cp_parser_objc_statement (parser);
6231 break;
6232
6233 case RID_TRY:
6234 statement = cp_parser_try_block (parser);
6235 break;
6236
6237 default:
6238 /* It might be a keyword like `int' that can start a
6239 declaration-statement. */
6240 break;
6241 }
6242 }
6243 else if (token->type == CPP_NAME)
6244 {
6245 /* If the next token is a `:', then we are looking at a
6246 labeled-statement. */
6247 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6248 if (token->type == CPP_COLON)
6249 {
6250 /* Looks like a labeled-statement with an ordinary label.
6251 Parse the label, and then use tail recursion to parse
6252 the statement. */
6253 cp_parser_label_for_labeled_statement (parser);
6254 goto restart;
6255 }
6256 }
6257 /* Anything that starts with a `{' must be a compound-statement. */
6258 else if (token->type == CPP_OPEN_BRACE)
6259 statement = cp_parser_compound_statement (parser, NULL, false);
6260 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6261 a statement all its own. */
6262 else if (token->type == CPP_PRAGMA)
6263 {
6264 /* Only certain OpenMP pragmas are attached to statements, and thus
6265 are considered statements themselves. All others are not. In
6266 the context of a compound, accept the pragma as a "statement" and
6267 return so that we can check for a close brace. Otherwise we
6268 require a real statement and must go back and read one. */
6269 if (in_compound)
6270 cp_parser_pragma (parser, pragma_compound);
6271 else if (!cp_parser_pragma (parser, pragma_stmt))
6272 goto restart;
6273 return;
6274 }
6275 else if (token->type == CPP_EOF)
6276 {
6277 cp_parser_error (parser, "expected statement");
6278 return;
6279 }
6280
6281 /* Everything else must be a declaration-statement or an
6282 expression-statement. Try for the declaration-statement
6283 first, unless we are looking at a `;', in which case we know that
6284 we have an expression-statement. */
6285 if (!statement)
6286 {
6287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6288 {
6289 cp_parser_parse_tentatively (parser);
6290 /* Try to parse the declaration-statement. */
6291 cp_parser_declaration_statement (parser);
6292 /* If that worked, we're done. */
6293 if (cp_parser_parse_definitely (parser))
6294 return;
6295 }
6296 /* Look for an expression-statement instead. */
6297 statement = cp_parser_expression_statement (parser, in_statement_expr);
6298 }
6299
6300 /* Set the line number for the statement. */
6301 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6302 SET_EXPR_LOCATION (statement, statement_location);
6303 }
6304
6305 /* Parse the label for a labeled-statement, i.e.
6306
6307 identifier :
6308 case constant-expression :
6309 default :
6310
6311 GNU Extension:
6312 case constant-expression ... constant-expression : statement
6313
6314 When a label is parsed without errors, the label is added to the
6315 parse tree by the finish_* functions, so this function doesn't
6316 have to return the label. */
6317
6318 static void
6319 cp_parser_label_for_labeled_statement (cp_parser* parser)
6320 {
6321 cp_token *token;
6322
6323 /* The next token should be an identifier. */
6324 token = cp_lexer_peek_token (parser->lexer);
6325 if (token->type != CPP_NAME
6326 && token->type != CPP_KEYWORD)
6327 {
6328 cp_parser_error (parser, "expected labeled-statement");
6329 return;
6330 }
6331
6332 switch (token->keyword)
6333 {
6334 case RID_CASE:
6335 {
6336 tree expr, expr_hi;
6337 cp_token *ellipsis;
6338
6339 /* Consume the `case' token. */
6340 cp_lexer_consume_token (parser->lexer);
6341 /* Parse the constant-expression. */
6342 expr = cp_parser_constant_expression (parser,
6343 /*allow_non_constant_p=*/false,
6344 NULL);
6345
6346 ellipsis = cp_lexer_peek_token (parser->lexer);
6347 if (ellipsis->type == CPP_ELLIPSIS)
6348 {
6349 /* Consume the `...' token. */
6350 cp_lexer_consume_token (parser->lexer);
6351 expr_hi =
6352 cp_parser_constant_expression (parser,
6353 /*allow_non_constant_p=*/false,
6354 NULL);
6355 /* We don't need to emit warnings here, as the common code
6356 will do this for us. */
6357 }
6358 else
6359 expr_hi = NULL_TREE;
6360
6361 if (parser->in_switch_statement_p)
6362 finish_case_label (expr, expr_hi);
6363 else
6364 error ("case label %qE not within a switch statement", expr);
6365 }
6366 break;
6367
6368 case RID_DEFAULT:
6369 /* Consume the `default' token. */
6370 cp_lexer_consume_token (parser->lexer);
6371
6372 if (parser->in_switch_statement_p)
6373 finish_case_label (NULL_TREE, NULL_TREE);
6374 else
6375 error ("case label not within a switch statement");
6376 break;
6377
6378 default:
6379 /* Anything else must be an ordinary label. */
6380 finish_label_stmt (cp_parser_identifier (parser));
6381 break;
6382 }
6383
6384 /* Require the `:' token. */
6385 cp_parser_require (parser, CPP_COLON, "`:'");
6386 }
6387
6388 /* Parse an expression-statement.
6389
6390 expression-statement:
6391 expression [opt] ;
6392
6393 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6394 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6395 indicates whether this expression-statement is part of an
6396 expression statement. */
6397
6398 static tree
6399 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6400 {
6401 tree statement = NULL_TREE;
6402
6403 /* If the next token is a ';', then there is no expression
6404 statement. */
6405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6406 statement = cp_parser_expression (parser, /*cast_p=*/false);
6407
6408 /* Consume the final `;'. */
6409 cp_parser_consume_semicolon_at_end_of_statement (parser);
6410
6411 if (in_statement_expr
6412 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6413 /* This is the final expression statement of a statement
6414 expression. */
6415 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6416 else if (statement)
6417 statement = finish_expr_stmt (statement);
6418 else
6419 finish_stmt ();
6420
6421 return statement;
6422 }
6423
6424 /* Parse a compound-statement.
6425
6426 compound-statement:
6427 { statement-seq [opt] }
6428
6429 Returns a tree representing the statement. */
6430
6431 static tree
6432 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6433 bool in_try)
6434 {
6435 tree compound_stmt;
6436
6437 /* Consume the `{'. */
6438 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6439 return error_mark_node;
6440 /* Begin the compound-statement. */
6441 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6442 /* Parse an (optional) statement-seq. */
6443 cp_parser_statement_seq_opt (parser, in_statement_expr);
6444 /* Finish the compound-statement. */
6445 finish_compound_stmt (compound_stmt);
6446 /* Consume the `}'. */
6447 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6448
6449 return compound_stmt;
6450 }
6451
6452 /* Parse an (optional) statement-seq.
6453
6454 statement-seq:
6455 statement
6456 statement-seq [opt] statement */
6457
6458 static void
6459 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6460 {
6461 /* Scan statements until there aren't any more. */
6462 while (true)
6463 {
6464 cp_token *token = cp_lexer_peek_token (parser->lexer);
6465
6466 /* If we're looking at a `}', then we've run out of statements. */
6467 if (token->type == CPP_CLOSE_BRACE
6468 || token->type == CPP_EOF
6469 || token->type == CPP_PRAGMA_EOL)
6470 break;
6471
6472 /* Parse the statement. */
6473 cp_parser_statement (parser, in_statement_expr, true);
6474 }
6475 }
6476
6477 /* Parse a selection-statement.
6478
6479 selection-statement:
6480 if ( condition ) statement
6481 if ( condition ) statement else statement
6482 switch ( condition ) statement
6483
6484 Returns the new IF_STMT or SWITCH_STMT. */
6485
6486 static tree
6487 cp_parser_selection_statement (cp_parser* parser)
6488 {
6489 cp_token *token;
6490 enum rid keyword;
6491
6492 /* Peek at the next token. */
6493 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6494
6495 /* See what kind of keyword it is. */
6496 keyword = token->keyword;
6497 switch (keyword)
6498 {
6499 case RID_IF:
6500 case RID_SWITCH:
6501 {
6502 tree statement;
6503 tree condition;
6504
6505 /* Look for the `('. */
6506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6507 {
6508 cp_parser_skip_to_end_of_statement (parser);
6509 return error_mark_node;
6510 }
6511
6512 /* Begin the selection-statement. */
6513 if (keyword == RID_IF)
6514 statement = begin_if_stmt ();
6515 else
6516 statement = begin_switch_stmt ();
6517
6518 /* Parse the condition. */
6519 condition = cp_parser_condition (parser);
6520 /* Look for the `)'. */
6521 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6522 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6523 /*consume_paren=*/true);
6524
6525 if (keyword == RID_IF)
6526 {
6527 /* Add the condition. */
6528 finish_if_stmt_cond (condition, statement);
6529
6530 /* Parse the then-clause. */
6531 cp_parser_implicitly_scoped_statement (parser);
6532 finish_then_clause (statement);
6533
6534 /* If the next token is `else', parse the else-clause. */
6535 if (cp_lexer_next_token_is_keyword (parser->lexer,
6536 RID_ELSE))
6537 {
6538 /* Consume the `else' keyword. */
6539 cp_lexer_consume_token (parser->lexer);
6540 begin_else_clause (statement);
6541 /* Parse the else-clause. */
6542 cp_parser_implicitly_scoped_statement (parser);
6543 finish_else_clause (statement);
6544 }
6545
6546 /* Now we're all done with the if-statement. */
6547 finish_if_stmt (statement);
6548 }
6549 else
6550 {
6551 bool in_switch_statement_p;
6552 unsigned char in_statement;
6553
6554 /* Add the condition. */
6555 finish_switch_cond (condition, statement);
6556
6557 /* Parse the body of the switch-statement. */
6558 in_switch_statement_p = parser->in_switch_statement_p;
6559 in_statement = parser->in_statement;
6560 parser->in_switch_statement_p = true;
6561 parser->in_statement |= IN_SWITCH_STMT;
6562 cp_parser_implicitly_scoped_statement (parser);
6563 parser->in_switch_statement_p = in_switch_statement_p;
6564 parser->in_statement = in_statement;
6565
6566 /* Now we're all done with the switch-statement. */
6567 finish_switch_stmt (statement);
6568 }
6569
6570 return statement;
6571 }
6572 break;
6573
6574 default:
6575 cp_parser_error (parser, "expected selection-statement");
6576 return error_mark_node;
6577 }
6578 }
6579
6580 /* Parse a condition.
6581
6582 condition:
6583 expression
6584 type-specifier-seq declarator = assignment-expression
6585
6586 GNU Extension:
6587
6588 condition:
6589 type-specifier-seq declarator asm-specification [opt]
6590 attributes [opt] = assignment-expression
6591
6592 Returns the expression that should be tested. */
6593
6594 static tree
6595 cp_parser_condition (cp_parser* parser)
6596 {
6597 cp_decl_specifier_seq type_specifiers;
6598 const char *saved_message;
6599
6600 /* Try the declaration first. */
6601 cp_parser_parse_tentatively (parser);
6602 /* New types are not allowed in the type-specifier-seq for a
6603 condition. */
6604 saved_message = parser->type_definition_forbidden_message;
6605 parser->type_definition_forbidden_message
6606 = "types may not be defined in conditions";
6607 /* Parse the type-specifier-seq. */
6608 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6609 &type_specifiers);
6610 /* Restore the saved message. */
6611 parser->type_definition_forbidden_message = saved_message;
6612 /* If all is well, we might be looking at a declaration. */
6613 if (!cp_parser_error_occurred (parser))
6614 {
6615 tree decl;
6616 tree asm_specification;
6617 tree attributes;
6618 cp_declarator *declarator;
6619 tree initializer = NULL_TREE;
6620
6621 /* Parse the declarator. */
6622 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6623 /*ctor_dtor_or_conv_p=*/NULL,
6624 /*parenthesized_p=*/NULL,
6625 /*member_p=*/false);
6626 /* Parse the attributes. */
6627 attributes = cp_parser_attributes_opt (parser);
6628 /* Parse the asm-specification. */
6629 asm_specification = cp_parser_asm_specification_opt (parser);
6630 /* If the next token is not an `=', then we might still be
6631 looking at an expression. For example:
6632
6633 if (A(a).x)
6634
6635 looks like a decl-specifier-seq and a declarator -- but then
6636 there is no `=', so this is an expression. */
6637 cp_parser_require (parser, CPP_EQ, "`='");
6638 /* If we did see an `=', then we are looking at a declaration
6639 for sure. */
6640 if (cp_parser_parse_definitely (parser))
6641 {
6642 tree pushed_scope;
6643 bool non_constant_p;
6644
6645 /* Create the declaration. */
6646 decl = start_decl (declarator, &type_specifiers,
6647 /*initialized_p=*/true,
6648 attributes, /*prefix_attributes=*/NULL_TREE,
6649 &pushed_scope);
6650 /* Parse the assignment-expression. */
6651 initializer
6652 = cp_parser_constant_expression (parser,
6653 /*allow_non_constant_p=*/true,
6654 &non_constant_p);
6655 if (!non_constant_p)
6656 initializer = fold_non_dependent_expr (initializer);
6657
6658 /* Process the initializer. */
6659 cp_finish_decl (decl,
6660 initializer, !non_constant_p,
6661 asm_specification,
6662 LOOKUP_ONLYCONVERTING);
6663
6664 if (pushed_scope)
6665 pop_scope (pushed_scope);
6666
6667 return convert_from_reference (decl);
6668 }
6669 }
6670 /* If we didn't even get past the declarator successfully, we are
6671 definitely not looking at a declaration. */
6672 else
6673 cp_parser_abort_tentative_parse (parser);
6674
6675 /* Otherwise, we are looking at an expression. */
6676 return cp_parser_expression (parser, /*cast_p=*/false);
6677 }
6678
6679 /* Parse an iteration-statement.
6680
6681 iteration-statement:
6682 while ( condition ) statement
6683 do statement while ( expression ) ;
6684 for ( for-init-statement condition [opt] ; expression [opt] )
6685 statement
6686
6687 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6688
6689 static tree
6690 cp_parser_iteration_statement (cp_parser* parser)
6691 {
6692 cp_token *token;
6693 enum rid keyword;
6694 tree statement;
6695 unsigned char in_statement;
6696
6697 /* Peek at the next token. */
6698 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6699 if (!token)
6700 return error_mark_node;
6701
6702 /* Remember whether or not we are already within an iteration
6703 statement. */
6704 in_statement = parser->in_statement;
6705
6706 /* See what kind of keyword it is. */
6707 keyword = token->keyword;
6708 switch (keyword)
6709 {
6710 case RID_WHILE:
6711 {
6712 tree condition;
6713
6714 /* Begin the while-statement. */
6715 statement = begin_while_stmt ();
6716 /* Look for the `('. */
6717 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6718 /* Parse the condition. */
6719 condition = cp_parser_condition (parser);
6720 finish_while_stmt_cond (condition, statement);
6721 /* Look for the `)'. */
6722 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6723 /* Parse the dependent statement. */
6724 parser->in_statement = IN_ITERATION_STMT;
6725 cp_parser_already_scoped_statement (parser);
6726 parser->in_statement = in_statement;
6727 /* We're done with the while-statement. */
6728 finish_while_stmt (statement);
6729 }
6730 break;
6731
6732 case RID_DO:
6733 {
6734 tree expression;
6735
6736 /* Begin the do-statement. */
6737 statement = begin_do_stmt ();
6738 /* Parse the body of the do-statement. */
6739 parser->in_statement = IN_ITERATION_STMT;
6740 cp_parser_implicitly_scoped_statement (parser);
6741 parser->in_statement = in_statement;
6742 finish_do_body (statement);
6743 /* Look for the `while' keyword. */
6744 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6745 /* Look for the `('. */
6746 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6747 /* Parse the expression. */
6748 expression = cp_parser_expression (parser, /*cast_p=*/false);
6749 /* We're done with the do-statement. */
6750 finish_do_stmt (expression, statement);
6751 /* Look for the `)'. */
6752 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6753 /* Look for the `;'. */
6754 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6755 }
6756 break;
6757
6758 case RID_FOR:
6759 {
6760 tree condition = NULL_TREE;
6761 tree expression = NULL_TREE;
6762
6763 /* Begin the for-statement. */
6764 statement = begin_for_stmt ();
6765 /* Look for the `('. */
6766 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6767 /* Parse the initialization. */
6768 cp_parser_for_init_statement (parser);
6769 finish_for_init_stmt (statement);
6770
6771 /* If there's a condition, process it. */
6772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6773 condition = cp_parser_condition (parser);
6774 finish_for_cond (condition, statement);
6775 /* Look for the `;'. */
6776 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6777
6778 /* If there's an expression, process it. */
6779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6780 expression = cp_parser_expression (parser, /*cast_p=*/false);
6781 finish_for_expr (expression, statement);
6782 /* Look for the `)'. */
6783 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6784
6785 /* Parse the body of the for-statement. */
6786 parser->in_statement = IN_ITERATION_STMT;
6787 cp_parser_already_scoped_statement (parser);
6788 parser->in_statement = in_statement;
6789
6790 /* We're done with the for-statement. */
6791 finish_for_stmt (statement);
6792 }
6793 break;
6794
6795 default:
6796 cp_parser_error (parser, "expected iteration-statement");
6797 statement = error_mark_node;
6798 break;
6799 }
6800
6801 return statement;
6802 }
6803
6804 /* Parse a for-init-statement.
6805
6806 for-init-statement:
6807 expression-statement
6808 simple-declaration */
6809
6810 static void
6811 cp_parser_for_init_statement (cp_parser* parser)
6812 {
6813 /* If the next token is a `;', then we have an empty
6814 expression-statement. Grammatically, this is also a
6815 simple-declaration, but an invalid one, because it does not
6816 declare anything. Therefore, if we did not handle this case
6817 specially, we would issue an error message about an invalid
6818 declaration. */
6819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6820 {
6821 /* We're going to speculatively look for a declaration, falling back
6822 to an expression, if necessary. */
6823 cp_parser_parse_tentatively (parser);
6824 /* Parse the declaration. */
6825 cp_parser_simple_declaration (parser,
6826 /*function_definition_allowed_p=*/false);
6827 /* If the tentative parse failed, then we shall need to look for an
6828 expression-statement. */
6829 if (cp_parser_parse_definitely (parser))
6830 return;
6831 }
6832
6833 cp_parser_expression_statement (parser, false);
6834 }
6835
6836 /* Parse a jump-statement.
6837
6838 jump-statement:
6839 break ;
6840 continue ;
6841 return expression [opt] ;
6842 goto identifier ;
6843
6844 GNU extension:
6845
6846 jump-statement:
6847 goto * expression ;
6848
6849 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6850
6851 static tree
6852 cp_parser_jump_statement (cp_parser* parser)
6853 {
6854 tree statement = error_mark_node;
6855 cp_token *token;
6856 enum rid keyword;
6857
6858 /* Peek at the next token. */
6859 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6860 if (!token)
6861 return error_mark_node;
6862
6863 /* See what kind of keyword it is. */
6864 keyword = token->keyword;
6865 switch (keyword)
6866 {
6867 case RID_BREAK:
6868 switch (parser->in_statement)
6869 {
6870 case 0:
6871 error ("break statement not within loop or switch");
6872 break;
6873 default:
6874 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6875 || parser->in_statement == IN_ITERATION_STMT);
6876 statement = finish_break_stmt ();
6877 break;
6878 case IN_OMP_BLOCK:
6879 error ("invalid exit from OpenMP structured block");
6880 break;
6881 case IN_OMP_FOR:
6882 error ("break statement used with OpenMP for loop");
6883 break;
6884 }
6885 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6886 break;
6887
6888 case RID_CONTINUE:
6889 switch (parser->in_statement & ~IN_SWITCH_STMT)
6890 {
6891 case 0:
6892 error ("continue statement not within a loop");
6893 break;
6894 case IN_ITERATION_STMT:
6895 case IN_OMP_FOR:
6896 statement = finish_continue_stmt ();
6897 break;
6898 case IN_OMP_BLOCK:
6899 error ("invalid exit from OpenMP structured block");
6900 break;
6901 default:
6902 gcc_unreachable ();
6903 }
6904 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6905 break;
6906
6907 case RID_RETURN:
6908 {
6909 tree expr;
6910
6911 /* If the next token is a `;', then there is no
6912 expression. */
6913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6914 expr = cp_parser_expression (parser, /*cast_p=*/false);
6915 else
6916 expr = NULL_TREE;
6917 /* Build the return-statement. */
6918 statement = finish_return_stmt (expr);
6919 /* Look for the final `;'. */
6920 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6921 }
6922 break;
6923
6924 case RID_GOTO:
6925 /* Create the goto-statement. */
6926 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6927 {
6928 /* Issue a warning about this use of a GNU extension. */
6929 if (pedantic)
6930 pedwarn ("ISO C++ forbids computed gotos");
6931 /* Consume the '*' token. */
6932 cp_lexer_consume_token (parser->lexer);
6933 /* Parse the dependent expression. */
6934 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6935 }
6936 else
6937 finish_goto_stmt (cp_parser_identifier (parser));
6938 /* Look for the final `;'. */
6939 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6940 break;
6941
6942 default:
6943 cp_parser_error (parser, "expected jump-statement");
6944 break;
6945 }
6946
6947 return statement;
6948 }
6949
6950 /* Parse a declaration-statement.
6951
6952 declaration-statement:
6953 block-declaration */
6954
6955 static void
6956 cp_parser_declaration_statement (cp_parser* parser)
6957 {
6958 void *p;
6959
6960 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6961 p = obstack_alloc (&declarator_obstack, 0);
6962
6963 /* Parse the block-declaration. */
6964 cp_parser_block_declaration (parser, /*statement_p=*/true);
6965
6966 /* Free any declarators allocated. */
6967 obstack_free (&declarator_obstack, p);
6968
6969 /* Finish off the statement. */
6970 finish_stmt ();
6971 }
6972
6973 /* Some dependent statements (like `if (cond) statement'), are
6974 implicitly in their own scope. In other words, if the statement is
6975 a single statement (as opposed to a compound-statement), it is
6976 none-the-less treated as if it were enclosed in braces. Any
6977 declarations appearing in the dependent statement are out of scope
6978 after control passes that point. This function parses a statement,
6979 but ensures that is in its own scope, even if it is not a
6980 compound-statement.
6981
6982 Returns the new statement. */
6983
6984 static tree
6985 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6986 {
6987 tree statement;
6988
6989 /* Mark if () ; with a special NOP_EXPR. */
6990 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6991 {
6992 cp_lexer_consume_token (parser->lexer);
6993 statement = add_stmt (build_empty_stmt ());
6994 }
6995 /* if a compound is opened, we simply parse the statement directly. */
6996 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6997 statement = cp_parser_compound_statement (parser, NULL, false);
6998 /* If the token is not a `{', then we must take special action. */
6999 else
7000 {
7001 /* Create a compound-statement. */
7002 statement = begin_compound_stmt (0);
7003 /* Parse the dependent-statement. */
7004 cp_parser_statement (parser, NULL_TREE, false);
7005 /* Finish the dummy compound-statement. */
7006 finish_compound_stmt (statement);
7007 }
7008
7009 /* Return the statement. */
7010 return statement;
7011 }
7012
7013 /* For some dependent statements (like `while (cond) statement'), we
7014 have already created a scope. Therefore, even if the dependent
7015 statement is a compound-statement, we do not want to create another
7016 scope. */
7017
7018 static void
7019 cp_parser_already_scoped_statement (cp_parser* parser)
7020 {
7021 /* If the token is a `{', then we must take special action. */
7022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7023 cp_parser_statement (parser, NULL_TREE, false);
7024 else
7025 {
7026 /* Avoid calling cp_parser_compound_statement, so that we
7027 don't create a new scope. Do everything else by hand. */
7028 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7029 cp_parser_statement_seq_opt (parser, NULL_TREE);
7030 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7031 }
7032 }
7033
7034 /* Declarations [gram.dcl.dcl] */
7035
7036 /* Parse an optional declaration-sequence.
7037
7038 declaration-seq:
7039 declaration
7040 declaration-seq declaration */
7041
7042 static void
7043 cp_parser_declaration_seq_opt (cp_parser* parser)
7044 {
7045 while (true)
7046 {
7047 cp_token *token;
7048
7049 token = cp_lexer_peek_token (parser->lexer);
7050
7051 if (token->type == CPP_CLOSE_BRACE
7052 || token->type == CPP_EOF
7053 || token->type == CPP_PRAGMA_EOL)
7054 break;
7055
7056 if (token->type == CPP_SEMICOLON)
7057 {
7058 /* A declaration consisting of a single semicolon is
7059 invalid. Allow it unless we're being pedantic. */
7060 cp_lexer_consume_token (parser->lexer);
7061 if (pedantic && !in_system_header)
7062 pedwarn ("extra %<;%>");
7063 continue;
7064 }
7065
7066 /* If we're entering or exiting a region that's implicitly
7067 extern "C", modify the lang context appropriately. */
7068 if (!parser->implicit_extern_c && token->implicit_extern_c)
7069 {
7070 push_lang_context (lang_name_c);
7071 parser->implicit_extern_c = true;
7072 }
7073 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7074 {
7075 pop_lang_context ();
7076 parser->implicit_extern_c = false;
7077 }
7078
7079 if (token->type == CPP_PRAGMA)
7080 {
7081 /* A top-level declaration can consist solely of a #pragma.
7082 A nested declaration cannot, so this is done here and not
7083 in cp_parser_declaration. (A #pragma at block scope is
7084 handled in cp_parser_statement.) */
7085 cp_parser_pragma (parser, pragma_external);
7086 continue;
7087 }
7088
7089 /* Parse the declaration itself. */
7090 cp_parser_declaration (parser);
7091 }
7092 }
7093
7094 /* Parse a declaration.
7095
7096 declaration:
7097 block-declaration
7098 function-definition
7099 template-declaration
7100 explicit-instantiation
7101 explicit-specialization
7102 linkage-specification
7103 namespace-definition
7104
7105 GNU extension:
7106
7107 declaration:
7108 __extension__ declaration */
7109
7110 static void
7111 cp_parser_declaration (cp_parser* parser)
7112 {
7113 cp_token token1;
7114 cp_token token2;
7115 int saved_pedantic;
7116 void *p;
7117
7118 /* Check for the `__extension__' keyword. */
7119 if (cp_parser_extension_opt (parser, &saved_pedantic))
7120 {
7121 /* Parse the qualified declaration. */
7122 cp_parser_declaration (parser);
7123 /* Restore the PEDANTIC flag. */
7124 pedantic = saved_pedantic;
7125
7126 return;
7127 }
7128
7129 /* Try to figure out what kind of declaration is present. */
7130 token1 = *cp_lexer_peek_token (parser->lexer);
7131
7132 if (token1.type != CPP_EOF)
7133 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7134 else
7135 {
7136 token2.type = CPP_EOF;
7137 token2.keyword = RID_MAX;
7138 }
7139
7140 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7141 p = obstack_alloc (&declarator_obstack, 0);
7142
7143 /* If the next token is `extern' and the following token is a string
7144 literal, then we have a linkage specification. */
7145 if (token1.keyword == RID_EXTERN
7146 && cp_parser_is_string_literal (&token2))
7147 cp_parser_linkage_specification (parser);
7148 /* If the next token is `template', then we have either a template
7149 declaration, an explicit instantiation, or an explicit
7150 specialization. */
7151 else if (token1.keyword == RID_TEMPLATE)
7152 {
7153 /* `template <>' indicates a template specialization. */
7154 if (token2.type == CPP_LESS
7155 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7156 cp_parser_explicit_specialization (parser);
7157 /* `template <' indicates a template declaration. */
7158 else if (token2.type == CPP_LESS)
7159 cp_parser_template_declaration (parser, /*member_p=*/false);
7160 /* Anything else must be an explicit instantiation. */
7161 else
7162 cp_parser_explicit_instantiation (parser);
7163 }
7164 /* If the next token is `export', then we have a template
7165 declaration. */
7166 else if (token1.keyword == RID_EXPORT)
7167 cp_parser_template_declaration (parser, /*member_p=*/false);
7168 /* If the next token is `extern', 'static' or 'inline' and the one
7169 after that is `template', we have a GNU extended explicit
7170 instantiation directive. */
7171 else if (cp_parser_allow_gnu_extensions_p (parser)
7172 && (token1.keyword == RID_EXTERN
7173 || token1.keyword == RID_STATIC
7174 || token1.keyword == RID_INLINE)
7175 && token2.keyword == RID_TEMPLATE)
7176 cp_parser_explicit_instantiation (parser);
7177 /* If the next token is `namespace', check for a named or unnamed
7178 namespace definition. */
7179 else if (token1.keyword == RID_NAMESPACE
7180 && (/* A named namespace definition. */
7181 (token2.type == CPP_NAME
7182 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7183 != CPP_EQ))
7184 /* An unnamed namespace definition. */
7185 || token2.type == CPP_OPEN_BRACE
7186 || token2.keyword == RID_ATTRIBUTE))
7187 cp_parser_namespace_definition (parser);
7188 /* Objective-C++ declaration/definition. */
7189 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7190 cp_parser_objc_declaration (parser);
7191 /* We must have either a block declaration or a function
7192 definition. */
7193 else
7194 /* Try to parse a block-declaration, or a function-definition. */
7195 cp_parser_block_declaration (parser, /*statement_p=*/false);
7196
7197 /* Free any declarators allocated. */
7198 obstack_free (&declarator_obstack, p);
7199 }
7200
7201 /* Parse a block-declaration.
7202
7203 block-declaration:
7204 simple-declaration
7205 asm-definition
7206 namespace-alias-definition
7207 using-declaration
7208 using-directive
7209
7210 GNU Extension:
7211
7212 block-declaration:
7213 __extension__ block-declaration
7214 label-declaration
7215
7216 C++0x Extension:
7217
7218 block-declaration:
7219 static_assert-declaration
7220
7221 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7222 part of a declaration-statement. */
7223
7224 static void
7225 cp_parser_block_declaration (cp_parser *parser,
7226 bool statement_p)
7227 {
7228 cp_token *token1;
7229 int saved_pedantic;
7230
7231 /* Check for the `__extension__' keyword. */
7232 if (cp_parser_extension_opt (parser, &saved_pedantic))
7233 {
7234 /* Parse the qualified declaration. */
7235 cp_parser_block_declaration (parser, statement_p);
7236 /* Restore the PEDANTIC flag. */
7237 pedantic = saved_pedantic;
7238
7239 return;
7240 }
7241
7242 /* Peek at the next token to figure out which kind of declaration is
7243 present. */
7244 token1 = cp_lexer_peek_token (parser->lexer);
7245
7246 /* If the next keyword is `asm', we have an asm-definition. */
7247 if (token1->keyword == RID_ASM)
7248 {
7249 if (statement_p)
7250 cp_parser_commit_to_tentative_parse (parser);
7251 cp_parser_asm_definition (parser);
7252 }
7253 /* If the next keyword is `namespace', we have a
7254 namespace-alias-definition. */
7255 else if (token1->keyword == RID_NAMESPACE)
7256 cp_parser_namespace_alias_definition (parser);
7257 /* If the next keyword is `using', we have either a
7258 using-declaration or a using-directive. */
7259 else if (token1->keyword == RID_USING)
7260 {
7261 cp_token *token2;
7262
7263 if (statement_p)
7264 cp_parser_commit_to_tentative_parse (parser);
7265 /* If the token after `using' is `namespace', then we have a
7266 using-directive. */
7267 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7268 if (token2->keyword == RID_NAMESPACE)
7269 cp_parser_using_directive (parser);
7270 /* Otherwise, it's a using-declaration. */
7271 else
7272 cp_parser_using_declaration (parser,
7273 /*access_declaration_p=*/false);
7274 }
7275 /* If the next keyword is `__label__' we have a label declaration. */
7276 else if (token1->keyword == RID_LABEL)
7277 {
7278 if (statement_p)
7279 cp_parser_commit_to_tentative_parse (parser);
7280 cp_parser_label_declaration (parser);
7281 }
7282 /* If the next token is `static_assert' we have a static assertion. */
7283 else if (token1->keyword == RID_STATIC_ASSERT)
7284 cp_parser_static_assert (parser, /*member_p=*/false);
7285 /* Anything else must be a simple-declaration. */
7286 else
7287 cp_parser_simple_declaration (parser, !statement_p);
7288 }
7289
7290 /* Parse a simple-declaration.
7291
7292 simple-declaration:
7293 decl-specifier-seq [opt] init-declarator-list [opt] ;
7294
7295 init-declarator-list:
7296 init-declarator
7297 init-declarator-list , init-declarator
7298
7299 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7300 function-definition as a simple-declaration. */
7301
7302 static void
7303 cp_parser_simple_declaration (cp_parser* parser,
7304 bool function_definition_allowed_p)
7305 {
7306 cp_decl_specifier_seq decl_specifiers;
7307 int declares_class_or_enum;
7308 bool saw_declarator;
7309
7310 /* Defer access checks until we know what is being declared; the
7311 checks for names appearing in the decl-specifier-seq should be
7312 done as if we were in the scope of the thing being declared. */
7313 push_deferring_access_checks (dk_deferred);
7314
7315 /* Parse the decl-specifier-seq. We have to keep track of whether
7316 or not the decl-specifier-seq declares a named class or
7317 enumeration type, since that is the only case in which the
7318 init-declarator-list is allowed to be empty.
7319
7320 [dcl.dcl]
7321
7322 In a simple-declaration, the optional init-declarator-list can be
7323 omitted only when declaring a class or enumeration, that is when
7324 the decl-specifier-seq contains either a class-specifier, an
7325 elaborated-type-specifier, or an enum-specifier. */
7326 cp_parser_decl_specifier_seq (parser,
7327 CP_PARSER_FLAGS_OPTIONAL,
7328 &decl_specifiers,
7329 &declares_class_or_enum);
7330 /* We no longer need to defer access checks. */
7331 stop_deferring_access_checks ();
7332
7333 /* In a block scope, a valid declaration must always have a
7334 decl-specifier-seq. By not trying to parse declarators, we can
7335 resolve the declaration/expression ambiguity more quickly. */
7336 if (!function_definition_allowed_p
7337 && !decl_specifiers.any_specifiers_p)
7338 {
7339 cp_parser_error (parser, "expected declaration");
7340 goto done;
7341 }
7342
7343 /* If the next two tokens are both identifiers, the code is
7344 erroneous. The usual cause of this situation is code like:
7345
7346 T t;
7347
7348 where "T" should name a type -- but does not. */
7349 if (!decl_specifiers.type
7350 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7351 {
7352 /* If parsing tentatively, we should commit; we really are
7353 looking at a declaration. */
7354 cp_parser_commit_to_tentative_parse (parser);
7355 /* Give up. */
7356 goto done;
7357 }
7358
7359 /* If we have seen at least one decl-specifier, and the next token
7360 is not a parenthesis, then we must be looking at a declaration.
7361 (After "int (" we might be looking at a functional cast.) */
7362 if (decl_specifiers.any_specifiers_p
7363 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7364 cp_parser_commit_to_tentative_parse (parser);
7365
7366 /* Keep going until we hit the `;' at the end of the simple
7367 declaration. */
7368 saw_declarator = false;
7369 while (cp_lexer_next_token_is_not (parser->lexer,
7370 CPP_SEMICOLON))
7371 {
7372 cp_token *token;
7373 bool function_definition_p;
7374 tree decl;
7375
7376 if (saw_declarator)
7377 {
7378 /* If we are processing next declarator, coma is expected */
7379 token = cp_lexer_peek_token (parser->lexer);
7380 gcc_assert (token->type == CPP_COMMA);
7381 cp_lexer_consume_token (parser->lexer);
7382 }
7383 else
7384 saw_declarator = true;
7385
7386 /* Parse the init-declarator. */
7387 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7388 /*checks=*/NULL_TREE,
7389 function_definition_allowed_p,
7390 /*member_p=*/false,
7391 declares_class_or_enum,
7392 &function_definition_p);
7393 /* If an error occurred while parsing tentatively, exit quickly.
7394 (That usually happens when in the body of a function; each
7395 statement is treated as a declaration-statement until proven
7396 otherwise.) */
7397 if (cp_parser_error_occurred (parser))
7398 goto done;
7399 /* Handle function definitions specially. */
7400 if (function_definition_p)
7401 {
7402 /* If the next token is a `,', then we are probably
7403 processing something like:
7404
7405 void f() {}, *p;
7406
7407 which is erroneous. */
7408 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7409 error ("mixing declarations and function-definitions is forbidden");
7410 /* Otherwise, we're done with the list of declarators. */
7411 else
7412 {
7413 pop_deferring_access_checks ();
7414 return;
7415 }
7416 }
7417 /* The next token should be either a `,' or a `;'. */
7418 token = cp_lexer_peek_token (parser->lexer);
7419 /* If it's a `,', there are more declarators to come. */
7420 if (token->type == CPP_COMMA)
7421 /* will be consumed next time around */;
7422 /* If it's a `;', we are done. */
7423 else if (token->type == CPP_SEMICOLON)
7424 break;
7425 /* Anything else is an error. */
7426 else
7427 {
7428 /* If we have already issued an error message we don't need
7429 to issue another one. */
7430 if (decl != error_mark_node
7431 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7432 cp_parser_error (parser, "expected %<,%> or %<;%>");
7433 /* Skip tokens until we reach the end of the statement. */
7434 cp_parser_skip_to_end_of_statement (parser);
7435 /* If the next token is now a `;', consume it. */
7436 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7437 cp_lexer_consume_token (parser->lexer);
7438 goto done;
7439 }
7440 /* After the first time around, a function-definition is not
7441 allowed -- even if it was OK at first. For example:
7442
7443 int i, f() {}
7444
7445 is not valid. */
7446 function_definition_allowed_p = false;
7447 }
7448
7449 /* Issue an error message if no declarators are present, and the
7450 decl-specifier-seq does not itself declare a class or
7451 enumeration. */
7452 if (!saw_declarator)
7453 {
7454 if (cp_parser_declares_only_class_p (parser))
7455 shadow_tag (&decl_specifiers);
7456 /* Perform any deferred access checks. */
7457 perform_deferred_access_checks ();
7458 }
7459
7460 /* Consume the `;'. */
7461 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7462
7463 done:
7464 pop_deferring_access_checks ();
7465 }
7466
7467 /* Parse a decl-specifier-seq.
7468
7469 decl-specifier-seq:
7470 decl-specifier-seq [opt] decl-specifier
7471
7472 decl-specifier:
7473 storage-class-specifier
7474 type-specifier
7475 function-specifier
7476 friend
7477 typedef
7478
7479 GNU Extension:
7480
7481 decl-specifier:
7482 attributes
7483
7484 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7485
7486 The parser flags FLAGS is used to control type-specifier parsing.
7487
7488 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7489 flags:
7490
7491 1: one of the decl-specifiers is an elaborated-type-specifier
7492 (i.e., a type declaration)
7493 2: one of the decl-specifiers is an enum-specifier or a
7494 class-specifier (i.e., a type definition)
7495
7496 */
7497
7498 static void
7499 cp_parser_decl_specifier_seq (cp_parser* parser,
7500 cp_parser_flags flags,
7501 cp_decl_specifier_seq *decl_specs,
7502 int* declares_class_or_enum)
7503 {
7504 bool constructor_possible_p = !parser->in_declarator_p;
7505
7506 /* Clear DECL_SPECS. */
7507 clear_decl_specs (decl_specs);
7508
7509 /* Assume no class or enumeration type is declared. */
7510 *declares_class_or_enum = 0;
7511
7512 /* Keep reading specifiers until there are no more to read. */
7513 while (true)
7514 {
7515 bool constructor_p;
7516 bool found_decl_spec;
7517 cp_token *token;
7518
7519 /* Peek at the next token. */
7520 token = cp_lexer_peek_token (parser->lexer);
7521 /* Handle attributes. */
7522 if (token->keyword == RID_ATTRIBUTE)
7523 {
7524 /* Parse the attributes. */
7525 decl_specs->attributes
7526 = chainon (decl_specs->attributes,
7527 cp_parser_attributes_opt (parser));
7528 continue;
7529 }
7530 /* Assume we will find a decl-specifier keyword. */
7531 found_decl_spec = true;
7532 /* If the next token is an appropriate keyword, we can simply
7533 add it to the list. */
7534 switch (token->keyword)
7535 {
7536 /* decl-specifier:
7537 friend */
7538 case RID_FRIEND:
7539 if (!at_class_scope_p ())
7540 {
7541 error ("%<friend%> used outside of class");
7542 cp_lexer_purge_token (parser->lexer);
7543 }
7544 else
7545 {
7546 ++decl_specs->specs[(int) ds_friend];
7547 /* Consume the token. */
7548 cp_lexer_consume_token (parser->lexer);
7549 }
7550 break;
7551
7552 /* function-specifier:
7553 inline
7554 virtual
7555 explicit */
7556 case RID_INLINE:
7557 case RID_VIRTUAL:
7558 case RID_EXPLICIT:
7559 cp_parser_function_specifier_opt (parser, decl_specs);
7560 break;
7561
7562 /* decl-specifier:
7563 typedef */
7564 case RID_TYPEDEF:
7565 ++decl_specs->specs[(int) ds_typedef];
7566 /* Consume the token. */
7567 cp_lexer_consume_token (parser->lexer);
7568 /* A constructor declarator cannot appear in a typedef. */
7569 constructor_possible_p = false;
7570 /* The "typedef" keyword can only occur in a declaration; we
7571 may as well commit at this point. */
7572 cp_parser_commit_to_tentative_parse (parser);
7573
7574 if (decl_specs->storage_class != sc_none)
7575 decl_specs->conflicting_specifiers_p = true;
7576 break;
7577
7578 /* storage-class-specifier:
7579 auto
7580 register
7581 static
7582 extern
7583 mutable
7584
7585 GNU Extension:
7586 thread */
7587 case RID_AUTO:
7588 case RID_REGISTER:
7589 case RID_STATIC:
7590 case RID_EXTERN:
7591 case RID_MUTABLE:
7592 /* Consume the token. */
7593 cp_lexer_consume_token (parser->lexer);
7594 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7595 break;
7596 case RID_THREAD:
7597 /* Consume the token. */
7598 cp_lexer_consume_token (parser->lexer);
7599 ++decl_specs->specs[(int) ds_thread];
7600 break;
7601
7602 default:
7603 /* We did not yet find a decl-specifier yet. */
7604 found_decl_spec = false;
7605 break;
7606 }
7607
7608 /* Constructors are a special case. The `S' in `S()' is not a
7609 decl-specifier; it is the beginning of the declarator. */
7610 constructor_p
7611 = (!found_decl_spec
7612 && constructor_possible_p
7613 && (cp_parser_constructor_declarator_p
7614 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7615
7616 /* If we don't have a DECL_SPEC yet, then we must be looking at
7617 a type-specifier. */
7618 if (!found_decl_spec && !constructor_p)
7619 {
7620 int decl_spec_declares_class_or_enum;
7621 bool is_cv_qualifier;
7622 tree type_spec;
7623
7624 type_spec
7625 = cp_parser_type_specifier (parser, flags,
7626 decl_specs,
7627 /*is_declaration=*/true,
7628 &decl_spec_declares_class_or_enum,
7629 &is_cv_qualifier);
7630
7631 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7632
7633 /* If this type-specifier referenced a user-defined type
7634 (a typedef, class-name, etc.), then we can't allow any
7635 more such type-specifiers henceforth.
7636
7637 [dcl.spec]
7638
7639 The longest sequence of decl-specifiers that could
7640 possibly be a type name is taken as the
7641 decl-specifier-seq of a declaration. The sequence shall
7642 be self-consistent as described below.
7643
7644 [dcl.type]
7645
7646 As a general rule, at most one type-specifier is allowed
7647 in the complete decl-specifier-seq of a declaration. The
7648 only exceptions are the following:
7649
7650 -- const or volatile can be combined with any other
7651 type-specifier.
7652
7653 -- signed or unsigned can be combined with char, long,
7654 short, or int.
7655
7656 -- ..
7657
7658 Example:
7659
7660 typedef char* Pc;
7661 void g (const int Pc);
7662
7663 Here, Pc is *not* part of the decl-specifier seq; it's
7664 the declarator. Therefore, once we see a type-specifier
7665 (other than a cv-qualifier), we forbid any additional
7666 user-defined types. We *do* still allow things like `int
7667 int' to be considered a decl-specifier-seq, and issue the
7668 error message later. */
7669 if (type_spec && !is_cv_qualifier)
7670 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7671 /* A constructor declarator cannot follow a type-specifier. */
7672 if (type_spec)
7673 {
7674 constructor_possible_p = false;
7675 found_decl_spec = true;
7676 }
7677 }
7678
7679 /* If we still do not have a DECL_SPEC, then there are no more
7680 decl-specifiers. */
7681 if (!found_decl_spec)
7682 break;
7683
7684 decl_specs->any_specifiers_p = true;
7685 /* After we see one decl-specifier, further decl-specifiers are
7686 always optional. */
7687 flags |= CP_PARSER_FLAGS_OPTIONAL;
7688 }
7689
7690 cp_parser_check_decl_spec (decl_specs);
7691
7692 /* Don't allow a friend specifier with a class definition. */
7693 if (decl_specs->specs[(int) ds_friend] != 0
7694 && (*declares_class_or_enum & 2))
7695 error ("class definition may not be declared a friend");
7696 }
7697
7698 /* Parse an (optional) storage-class-specifier.
7699
7700 storage-class-specifier:
7701 auto
7702 register
7703 static
7704 extern
7705 mutable
7706
7707 GNU Extension:
7708
7709 storage-class-specifier:
7710 thread
7711
7712 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7713
7714 static tree
7715 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7716 {
7717 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7718 {
7719 case RID_AUTO:
7720 case RID_REGISTER:
7721 case RID_STATIC:
7722 case RID_EXTERN:
7723 case RID_MUTABLE:
7724 case RID_THREAD:
7725 /* Consume the token. */
7726 return cp_lexer_consume_token (parser->lexer)->value;
7727
7728 default:
7729 return NULL_TREE;
7730 }
7731 }
7732
7733 /* Parse an (optional) function-specifier.
7734
7735 function-specifier:
7736 inline
7737 virtual
7738 explicit
7739
7740 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7741 Updates DECL_SPECS, if it is non-NULL. */
7742
7743 static tree
7744 cp_parser_function_specifier_opt (cp_parser* parser,
7745 cp_decl_specifier_seq *decl_specs)
7746 {
7747 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7748 {
7749 case RID_INLINE:
7750 if (decl_specs)
7751 ++decl_specs->specs[(int) ds_inline];
7752 break;
7753
7754 case RID_VIRTUAL:
7755 /* 14.5.2.3 [temp.mem]
7756
7757 A member function template shall not be virtual. */
7758 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7759 error ("templates may not be %<virtual%>");
7760 else if (decl_specs)
7761 ++decl_specs->specs[(int) ds_virtual];
7762 break;
7763
7764 case RID_EXPLICIT:
7765 if (decl_specs)
7766 ++decl_specs->specs[(int) ds_explicit];
7767 break;
7768
7769 default:
7770 return NULL_TREE;
7771 }
7772
7773 /* Consume the token. */
7774 return cp_lexer_consume_token (parser->lexer)->value;
7775 }
7776
7777 /* Parse a linkage-specification.
7778
7779 linkage-specification:
7780 extern string-literal { declaration-seq [opt] }
7781 extern string-literal declaration */
7782
7783 static void
7784 cp_parser_linkage_specification (cp_parser* parser)
7785 {
7786 tree linkage;
7787
7788 /* Look for the `extern' keyword. */
7789 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7790
7791 /* Look for the string-literal. */
7792 linkage = cp_parser_string_literal (parser, false, false);
7793
7794 /* Transform the literal into an identifier. If the literal is a
7795 wide-character string, or contains embedded NULs, then we can't
7796 handle it as the user wants. */
7797 if (strlen (TREE_STRING_POINTER (linkage))
7798 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7799 {
7800 cp_parser_error (parser, "invalid linkage-specification");
7801 /* Assume C++ linkage. */
7802 linkage = lang_name_cplusplus;
7803 }
7804 else
7805 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7806
7807 /* We're now using the new linkage. */
7808 push_lang_context (linkage);
7809
7810 /* If the next token is a `{', then we're using the first
7811 production. */
7812 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7813 {
7814 /* Consume the `{' token. */
7815 cp_lexer_consume_token (parser->lexer);
7816 /* Parse the declarations. */
7817 cp_parser_declaration_seq_opt (parser);
7818 /* Look for the closing `}'. */
7819 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7820 }
7821 /* Otherwise, there's just one declaration. */
7822 else
7823 {
7824 bool saved_in_unbraced_linkage_specification_p;
7825
7826 saved_in_unbraced_linkage_specification_p
7827 = parser->in_unbraced_linkage_specification_p;
7828 parser->in_unbraced_linkage_specification_p = true;
7829 cp_parser_declaration (parser);
7830 parser->in_unbraced_linkage_specification_p
7831 = saved_in_unbraced_linkage_specification_p;
7832 }
7833
7834 /* We're done with the linkage-specification. */
7835 pop_lang_context ();
7836 }
7837
7838 /* Parse a static_assert-declaration.
7839
7840 static_assert-declaration:
7841 static_assert ( constant-expression , string-literal ) ;
7842
7843 If MEMBER_P, this static_assert is a class member. */
7844
7845 static void
7846 cp_parser_static_assert(cp_parser *parser, bool member_p)
7847 {
7848 tree condition;
7849 tree message;
7850 cp_token *token;
7851 location_t saved_loc;
7852
7853 /* Peek at the `static_assert' token so we can keep track of exactly
7854 where the static assertion started. */
7855 token = cp_lexer_peek_token (parser->lexer);
7856 saved_loc = token->location;
7857
7858 /* Look for the `static_assert' keyword. */
7859 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
7860 "`static_assert'"))
7861 return;
7862
7863 /* We know we are in a static assertion; commit to any tentative
7864 parse. */
7865 if (cp_parser_parsing_tentatively (parser))
7866 cp_parser_commit_to_tentative_parse (parser);
7867
7868 /* Parse the `(' starting the static assertion condition. */
7869 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7870
7871 /* Parse the constant-expression. */
7872 condition =
7873 cp_parser_constant_expression (parser,
7874 /*allow_non_constant_p=*/false,
7875 /*non_constant_p=*/NULL);
7876
7877 /* Parse the separating `,'. */
7878 cp_parser_require (parser, CPP_COMMA, "`,'");
7879
7880 /* Parse the string-literal message. */
7881 message = cp_parser_string_literal (parser,
7882 /*translate=*/false,
7883 /*wide_ok=*/true);
7884
7885 /* A `)' completes the static assertion. */
7886 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7887 cp_parser_skip_to_closing_parenthesis (parser,
7888 /*recovering=*/true,
7889 /*or_comma=*/false,
7890 /*consume_paren=*/true);
7891
7892 /* A semicolon terminates the declaration. */
7893 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7894
7895 /* Complete the static assertion, which may mean either processing
7896 the static assert now or saving it for template instantiation. */
7897 finish_static_assert (condition, message, saved_loc, member_p);
7898 }
7899
7900 /* Special member functions [gram.special] */
7901
7902 /* Parse a conversion-function-id.
7903
7904 conversion-function-id:
7905 operator conversion-type-id
7906
7907 Returns an IDENTIFIER_NODE representing the operator. */
7908
7909 static tree
7910 cp_parser_conversion_function_id (cp_parser* parser)
7911 {
7912 tree type;
7913 tree saved_scope;
7914 tree saved_qualifying_scope;
7915 tree saved_object_scope;
7916 tree pushed_scope = NULL_TREE;
7917
7918 /* Look for the `operator' token. */
7919 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7920 return error_mark_node;
7921 /* When we parse the conversion-type-id, the current scope will be
7922 reset. However, we need that information in able to look up the
7923 conversion function later, so we save it here. */
7924 saved_scope = parser->scope;
7925 saved_qualifying_scope = parser->qualifying_scope;
7926 saved_object_scope = parser->object_scope;
7927 /* We must enter the scope of the class so that the names of
7928 entities declared within the class are available in the
7929 conversion-type-id. For example, consider:
7930
7931 struct S {
7932 typedef int I;
7933 operator I();
7934 };
7935
7936 S::operator I() { ... }
7937
7938 In order to see that `I' is a type-name in the definition, we
7939 must be in the scope of `S'. */
7940 if (saved_scope)
7941 pushed_scope = push_scope (saved_scope);
7942 /* Parse the conversion-type-id. */
7943 type = cp_parser_conversion_type_id (parser);
7944 /* Leave the scope of the class, if any. */
7945 if (pushed_scope)
7946 pop_scope (pushed_scope);
7947 /* Restore the saved scope. */
7948 parser->scope = saved_scope;
7949 parser->qualifying_scope = saved_qualifying_scope;
7950 parser->object_scope = saved_object_scope;
7951 /* If the TYPE is invalid, indicate failure. */
7952 if (type == error_mark_node)
7953 return error_mark_node;
7954 return mangle_conv_op_name_for_type (type);
7955 }
7956
7957 /* Parse a conversion-type-id:
7958
7959 conversion-type-id:
7960 type-specifier-seq conversion-declarator [opt]
7961
7962 Returns the TYPE specified. */
7963
7964 static tree
7965 cp_parser_conversion_type_id (cp_parser* parser)
7966 {
7967 tree attributes;
7968 cp_decl_specifier_seq type_specifiers;
7969 cp_declarator *declarator;
7970 tree type_specified;
7971
7972 /* Parse the attributes. */
7973 attributes = cp_parser_attributes_opt (parser);
7974 /* Parse the type-specifiers. */
7975 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7976 &type_specifiers);
7977 /* If that didn't work, stop. */
7978 if (type_specifiers.type == error_mark_node)
7979 return error_mark_node;
7980 /* Parse the conversion-declarator. */
7981 declarator = cp_parser_conversion_declarator_opt (parser);
7982
7983 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7984 /*initialized=*/0, &attributes);
7985 if (attributes)
7986 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7987 return type_specified;
7988 }
7989
7990 /* Parse an (optional) conversion-declarator.
7991
7992 conversion-declarator:
7993 ptr-operator conversion-declarator [opt]
7994
7995 */
7996
7997 static cp_declarator *
7998 cp_parser_conversion_declarator_opt (cp_parser* parser)
7999 {
8000 enum tree_code code;
8001 tree class_type;
8002 cp_cv_quals cv_quals;
8003
8004 /* We don't know if there's a ptr-operator next, or not. */
8005 cp_parser_parse_tentatively (parser);
8006 /* Try the ptr-operator. */
8007 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8008 /* If it worked, look for more conversion-declarators. */
8009 if (cp_parser_parse_definitely (parser))
8010 {
8011 cp_declarator *declarator;
8012
8013 /* Parse another optional declarator. */
8014 declarator = cp_parser_conversion_declarator_opt (parser);
8015
8016 /* Create the representation of the declarator. */
8017 if (class_type)
8018 declarator = make_ptrmem_declarator (cv_quals, class_type,
8019 declarator);
8020 else if (code == INDIRECT_REF)
8021 declarator = make_pointer_declarator (cv_quals, declarator);
8022 else
8023 declarator = make_reference_declarator (cv_quals, declarator);
8024
8025 return declarator;
8026 }
8027
8028 return NULL;
8029 }
8030
8031 /* Parse an (optional) ctor-initializer.
8032
8033 ctor-initializer:
8034 : mem-initializer-list
8035
8036 Returns TRUE iff the ctor-initializer was actually present. */
8037
8038 static bool
8039 cp_parser_ctor_initializer_opt (cp_parser* parser)
8040 {
8041 /* If the next token is not a `:', then there is no
8042 ctor-initializer. */
8043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8044 {
8045 /* Do default initialization of any bases and members. */
8046 if (DECL_CONSTRUCTOR_P (current_function_decl))
8047 finish_mem_initializers (NULL_TREE);
8048
8049 return false;
8050 }
8051
8052 /* Consume the `:' token. */
8053 cp_lexer_consume_token (parser->lexer);
8054 /* And the mem-initializer-list. */
8055 cp_parser_mem_initializer_list (parser);
8056
8057 return true;
8058 }
8059
8060 /* Parse a mem-initializer-list.
8061
8062 mem-initializer-list:
8063 mem-initializer
8064 mem-initializer , mem-initializer-list */
8065
8066 static void
8067 cp_parser_mem_initializer_list (cp_parser* parser)
8068 {
8069 tree mem_initializer_list = NULL_TREE;
8070
8071 /* Let the semantic analysis code know that we are starting the
8072 mem-initializer-list. */
8073 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8074 error ("only constructors take base initializers");
8075
8076 /* Loop through the list. */
8077 while (true)
8078 {
8079 tree mem_initializer;
8080
8081 /* Parse the mem-initializer. */
8082 mem_initializer = cp_parser_mem_initializer (parser);
8083 /* Add it to the list, unless it was erroneous. */
8084 if (mem_initializer != error_mark_node)
8085 {
8086 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8087 mem_initializer_list = mem_initializer;
8088 }
8089 /* If the next token is not a `,', we're done. */
8090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8091 break;
8092 /* Consume the `,' token. */
8093 cp_lexer_consume_token (parser->lexer);
8094 }
8095
8096 /* Perform semantic analysis. */
8097 if (DECL_CONSTRUCTOR_P (current_function_decl))
8098 finish_mem_initializers (mem_initializer_list);
8099 }
8100
8101 /* Parse a mem-initializer.
8102
8103 mem-initializer:
8104 mem-initializer-id ( expression-list [opt] )
8105
8106 GNU extension:
8107
8108 mem-initializer:
8109 ( expression-list [opt] )
8110
8111 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8112 class) or FIELD_DECL (for a non-static data member) to initialize;
8113 the TREE_VALUE is the expression-list. An empty initialization
8114 list is represented by void_list_node. */
8115
8116 static tree
8117 cp_parser_mem_initializer (cp_parser* parser)
8118 {
8119 tree mem_initializer_id;
8120 tree expression_list;
8121 tree member;
8122
8123 /* Find out what is being initialized. */
8124 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8125 {
8126 pedwarn ("anachronistic old-style base class initializer");
8127 mem_initializer_id = NULL_TREE;
8128 }
8129 else
8130 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8131 member = expand_member_init (mem_initializer_id);
8132 if (member && !DECL_P (member))
8133 in_base_initializer = 1;
8134
8135 expression_list
8136 = cp_parser_parenthesized_expression_list (parser, false,
8137 /*cast_p=*/false,
8138 /*non_constant_p=*/NULL);
8139 if (expression_list == error_mark_node)
8140 return error_mark_node;
8141 if (!expression_list)
8142 expression_list = void_type_node;
8143
8144 in_base_initializer = 0;
8145
8146 return member ? build_tree_list (member, expression_list) : error_mark_node;
8147 }
8148
8149 /* Parse a mem-initializer-id.
8150
8151 mem-initializer-id:
8152 :: [opt] nested-name-specifier [opt] class-name
8153 identifier
8154
8155 Returns a TYPE indicating the class to be initializer for the first
8156 production. Returns an IDENTIFIER_NODE indicating the data member
8157 to be initialized for the second production. */
8158
8159 static tree
8160 cp_parser_mem_initializer_id (cp_parser* parser)
8161 {
8162 bool global_scope_p;
8163 bool nested_name_specifier_p;
8164 bool template_p = false;
8165 tree id;
8166
8167 /* `typename' is not allowed in this context ([temp.res]). */
8168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8169 {
8170 error ("keyword %<typename%> not allowed in this context (a qualified "
8171 "member initializer is implicitly a type)");
8172 cp_lexer_consume_token (parser->lexer);
8173 }
8174 /* Look for the optional `::' operator. */
8175 global_scope_p
8176 = (cp_parser_global_scope_opt (parser,
8177 /*current_scope_valid_p=*/false)
8178 != NULL_TREE);
8179 /* Look for the optional nested-name-specifier. The simplest way to
8180 implement:
8181
8182 [temp.res]
8183
8184 The keyword `typename' is not permitted in a base-specifier or
8185 mem-initializer; in these contexts a qualified name that
8186 depends on a template-parameter is implicitly assumed to be a
8187 type name.
8188
8189 is to assume that we have seen the `typename' keyword at this
8190 point. */
8191 nested_name_specifier_p
8192 = (cp_parser_nested_name_specifier_opt (parser,
8193 /*typename_keyword_p=*/true,
8194 /*check_dependency_p=*/true,
8195 /*type_p=*/true,
8196 /*is_declaration=*/true)
8197 != NULL_TREE);
8198 if (nested_name_specifier_p)
8199 template_p = cp_parser_optional_template_keyword (parser);
8200 /* If there is a `::' operator or a nested-name-specifier, then we
8201 are definitely looking for a class-name. */
8202 if (global_scope_p || nested_name_specifier_p)
8203 return cp_parser_class_name (parser,
8204 /*typename_keyword_p=*/true,
8205 /*template_keyword_p=*/template_p,
8206 none_type,
8207 /*check_dependency_p=*/true,
8208 /*class_head_p=*/false,
8209 /*is_declaration=*/true);
8210 /* Otherwise, we could also be looking for an ordinary identifier. */
8211 cp_parser_parse_tentatively (parser);
8212 /* Try a class-name. */
8213 id = cp_parser_class_name (parser,
8214 /*typename_keyword_p=*/true,
8215 /*template_keyword_p=*/false,
8216 none_type,
8217 /*check_dependency_p=*/true,
8218 /*class_head_p=*/false,
8219 /*is_declaration=*/true);
8220 /* If we found one, we're done. */
8221 if (cp_parser_parse_definitely (parser))
8222 return id;
8223 /* Otherwise, look for an ordinary identifier. */
8224 return cp_parser_identifier (parser);
8225 }
8226
8227 /* Overloading [gram.over] */
8228
8229 /* Parse an operator-function-id.
8230
8231 operator-function-id:
8232 operator operator
8233
8234 Returns an IDENTIFIER_NODE for the operator which is a
8235 human-readable spelling of the identifier, e.g., `operator +'. */
8236
8237 static tree
8238 cp_parser_operator_function_id (cp_parser* parser)
8239 {
8240 /* Look for the `operator' keyword. */
8241 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8242 return error_mark_node;
8243 /* And then the name of the operator itself. */
8244 return cp_parser_operator (parser);
8245 }
8246
8247 /* Parse an operator.
8248
8249 operator:
8250 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8251 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8252 || ++ -- , ->* -> () []
8253
8254 GNU Extensions:
8255
8256 operator:
8257 <? >? <?= >?=
8258
8259 Returns an IDENTIFIER_NODE for the operator which is a
8260 human-readable spelling of the identifier, e.g., `operator +'. */
8261
8262 static tree
8263 cp_parser_operator (cp_parser* parser)
8264 {
8265 tree id = NULL_TREE;
8266 cp_token *token;
8267
8268 /* Peek at the next token. */
8269 token = cp_lexer_peek_token (parser->lexer);
8270 /* Figure out which operator we have. */
8271 switch (token->type)
8272 {
8273 case CPP_KEYWORD:
8274 {
8275 enum tree_code op;
8276
8277 /* The keyword should be either `new' or `delete'. */
8278 if (token->keyword == RID_NEW)
8279 op = NEW_EXPR;
8280 else if (token->keyword == RID_DELETE)
8281 op = DELETE_EXPR;
8282 else
8283 break;
8284
8285 /* Consume the `new' or `delete' token. */
8286 cp_lexer_consume_token (parser->lexer);
8287
8288 /* Peek at the next token. */
8289 token = cp_lexer_peek_token (parser->lexer);
8290 /* If it's a `[' token then this is the array variant of the
8291 operator. */
8292 if (token->type == CPP_OPEN_SQUARE)
8293 {
8294 /* Consume the `[' token. */
8295 cp_lexer_consume_token (parser->lexer);
8296 /* Look for the `]' token. */
8297 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8298 id = ansi_opname (op == NEW_EXPR
8299 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8300 }
8301 /* Otherwise, we have the non-array variant. */
8302 else
8303 id = ansi_opname (op);
8304
8305 return id;
8306 }
8307
8308 case CPP_PLUS:
8309 id = ansi_opname (PLUS_EXPR);
8310 break;
8311
8312 case CPP_MINUS:
8313 id = ansi_opname (MINUS_EXPR);
8314 break;
8315
8316 case CPP_MULT:
8317 id = ansi_opname (MULT_EXPR);
8318 break;
8319
8320 case CPP_DIV:
8321 id = ansi_opname (TRUNC_DIV_EXPR);
8322 break;
8323
8324 case CPP_MOD:
8325 id = ansi_opname (TRUNC_MOD_EXPR);
8326 break;
8327
8328 case CPP_XOR:
8329 id = ansi_opname (BIT_XOR_EXPR);
8330 break;
8331
8332 case CPP_AND:
8333 id = ansi_opname (BIT_AND_EXPR);
8334 break;
8335
8336 case CPP_OR:
8337 id = ansi_opname (BIT_IOR_EXPR);
8338 break;
8339
8340 case CPP_COMPL:
8341 id = ansi_opname (BIT_NOT_EXPR);
8342 break;
8343
8344 case CPP_NOT:
8345 id = ansi_opname (TRUTH_NOT_EXPR);
8346 break;
8347
8348 case CPP_EQ:
8349 id = ansi_assopname (NOP_EXPR);
8350 break;
8351
8352 case CPP_LESS:
8353 id = ansi_opname (LT_EXPR);
8354 break;
8355
8356 case CPP_GREATER:
8357 id = ansi_opname (GT_EXPR);
8358 break;
8359
8360 case CPP_PLUS_EQ:
8361 id = ansi_assopname (PLUS_EXPR);
8362 break;
8363
8364 case CPP_MINUS_EQ:
8365 id = ansi_assopname (MINUS_EXPR);
8366 break;
8367
8368 case CPP_MULT_EQ:
8369 id = ansi_assopname (MULT_EXPR);
8370 break;
8371
8372 case CPP_DIV_EQ:
8373 id = ansi_assopname (TRUNC_DIV_EXPR);
8374 break;
8375
8376 case CPP_MOD_EQ:
8377 id = ansi_assopname (TRUNC_MOD_EXPR);
8378 break;
8379
8380 case CPP_XOR_EQ:
8381 id = ansi_assopname (BIT_XOR_EXPR);
8382 break;
8383
8384 case CPP_AND_EQ:
8385 id = ansi_assopname (BIT_AND_EXPR);
8386 break;
8387
8388 case CPP_OR_EQ:
8389 id = ansi_assopname (BIT_IOR_EXPR);
8390 break;
8391
8392 case CPP_LSHIFT:
8393 id = ansi_opname (LSHIFT_EXPR);
8394 break;
8395
8396 case CPP_RSHIFT:
8397 id = ansi_opname (RSHIFT_EXPR);
8398 break;
8399
8400 case CPP_LSHIFT_EQ:
8401 id = ansi_assopname (LSHIFT_EXPR);
8402 break;
8403
8404 case CPP_RSHIFT_EQ:
8405 id = ansi_assopname (RSHIFT_EXPR);
8406 break;
8407
8408 case CPP_EQ_EQ:
8409 id = ansi_opname (EQ_EXPR);
8410 break;
8411
8412 case CPP_NOT_EQ:
8413 id = ansi_opname (NE_EXPR);
8414 break;
8415
8416 case CPP_LESS_EQ:
8417 id = ansi_opname (LE_EXPR);
8418 break;
8419
8420 case CPP_GREATER_EQ:
8421 id = ansi_opname (GE_EXPR);
8422 break;
8423
8424 case CPP_AND_AND:
8425 id = ansi_opname (TRUTH_ANDIF_EXPR);
8426 break;
8427
8428 case CPP_OR_OR:
8429 id = ansi_opname (TRUTH_ORIF_EXPR);
8430 break;
8431
8432 case CPP_PLUS_PLUS:
8433 id = ansi_opname (POSTINCREMENT_EXPR);
8434 break;
8435
8436 case CPP_MINUS_MINUS:
8437 id = ansi_opname (PREDECREMENT_EXPR);
8438 break;
8439
8440 case CPP_COMMA:
8441 id = ansi_opname (COMPOUND_EXPR);
8442 break;
8443
8444 case CPP_DEREF_STAR:
8445 id = ansi_opname (MEMBER_REF);
8446 break;
8447
8448 case CPP_DEREF:
8449 id = ansi_opname (COMPONENT_REF);
8450 break;
8451
8452 case CPP_OPEN_PAREN:
8453 /* Consume the `('. */
8454 cp_lexer_consume_token (parser->lexer);
8455 /* Look for the matching `)'. */
8456 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8457 return ansi_opname (CALL_EXPR);
8458
8459 case CPP_OPEN_SQUARE:
8460 /* Consume the `['. */
8461 cp_lexer_consume_token (parser->lexer);
8462 /* Look for the matching `]'. */
8463 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8464 return ansi_opname (ARRAY_REF);
8465
8466 default:
8467 /* Anything else is an error. */
8468 break;
8469 }
8470
8471 /* If we have selected an identifier, we need to consume the
8472 operator token. */
8473 if (id)
8474 cp_lexer_consume_token (parser->lexer);
8475 /* Otherwise, no valid operator name was present. */
8476 else
8477 {
8478 cp_parser_error (parser, "expected operator");
8479 id = error_mark_node;
8480 }
8481
8482 return id;
8483 }
8484
8485 /* Parse a template-declaration.
8486
8487 template-declaration:
8488 export [opt] template < template-parameter-list > declaration
8489
8490 If MEMBER_P is TRUE, this template-declaration occurs within a
8491 class-specifier.
8492
8493 The grammar rule given by the standard isn't correct. What
8494 is really meant is:
8495
8496 template-declaration:
8497 export [opt] template-parameter-list-seq
8498 decl-specifier-seq [opt] init-declarator [opt] ;
8499 export [opt] template-parameter-list-seq
8500 function-definition
8501
8502 template-parameter-list-seq:
8503 template-parameter-list-seq [opt]
8504 template < template-parameter-list > */
8505
8506 static void
8507 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8508 {
8509 /* Check for `export'. */
8510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8511 {
8512 /* Consume the `export' token. */
8513 cp_lexer_consume_token (parser->lexer);
8514 /* Warn that we do not support `export'. */
8515 warning (0, "keyword %<export%> not implemented, and will be ignored");
8516 }
8517
8518 cp_parser_template_declaration_after_export (parser, member_p);
8519 }
8520
8521 /* Parse a template-parameter-list.
8522
8523 template-parameter-list:
8524 template-parameter
8525 template-parameter-list , template-parameter
8526
8527 Returns a TREE_LIST. Each node represents a template parameter.
8528 The nodes are connected via their TREE_CHAINs. */
8529
8530 static tree
8531 cp_parser_template_parameter_list (cp_parser* parser)
8532 {
8533 tree parameter_list = NULL_TREE;
8534
8535 begin_template_parm_list ();
8536 while (true)
8537 {
8538 tree parameter;
8539 cp_token *token;
8540 bool is_non_type;
8541
8542 /* Parse the template-parameter. */
8543 parameter = cp_parser_template_parameter (parser, &is_non_type);
8544 /* Add it to the list. */
8545 if (parameter != error_mark_node)
8546 parameter_list = process_template_parm (parameter_list,
8547 parameter,
8548 is_non_type);
8549 else
8550 {
8551 tree err_parm = build_tree_list (parameter, parameter);
8552 TREE_VALUE (err_parm) = error_mark_node;
8553 parameter_list = chainon (parameter_list, err_parm);
8554 }
8555
8556 /* Peek at the next token. */
8557 token = cp_lexer_peek_token (parser->lexer);
8558 /* If it's not a `,', we're done. */
8559 if (token->type != CPP_COMMA)
8560 break;
8561 /* Otherwise, consume the `,' token. */
8562 cp_lexer_consume_token (parser->lexer);
8563 }
8564
8565 return end_template_parm_list (parameter_list);
8566 }
8567
8568 /* Parse a template-parameter.
8569
8570 template-parameter:
8571 type-parameter
8572 parameter-declaration
8573
8574 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8575 the parameter. The TREE_PURPOSE is the default value, if any.
8576 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8577 iff this parameter is a non-type parameter. */
8578
8579 static tree
8580 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8581 {
8582 cp_token *token;
8583 cp_parameter_declarator *parameter_declarator;
8584 tree parm;
8585
8586 /* Assume it is a type parameter or a template parameter. */
8587 *is_non_type = false;
8588 /* Peek at the next token. */
8589 token = cp_lexer_peek_token (parser->lexer);
8590 /* If it is `class' or `template', we have a type-parameter. */
8591 if (token->keyword == RID_TEMPLATE)
8592 return cp_parser_type_parameter (parser);
8593 /* If it is `class' or `typename' we do not know yet whether it is a
8594 type parameter or a non-type parameter. Consider:
8595
8596 template <typename T, typename T::X X> ...
8597
8598 or:
8599
8600 template <class C, class D*> ...
8601
8602 Here, the first parameter is a type parameter, and the second is
8603 a non-type parameter. We can tell by looking at the token after
8604 the identifier -- if it is a `,', `=', or `>' then we have a type
8605 parameter. */
8606 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8607 {
8608 /* Peek at the token after `class' or `typename'. */
8609 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8610 /* If it's an identifier, skip it. */
8611 if (token->type == CPP_NAME)
8612 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8613 /* Now, see if the token looks like the end of a template
8614 parameter. */
8615 if (token->type == CPP_COMMA
8616 || token->type == CPP_EQ
8617 || token->type == CPP_GREATER)
8618 return cp_parser_type_parameter (parser);
8619 }
8620
8621 /* Otherwise, it is a non-type parameter.
8622
8623 [temp.param]
8624
8625 When parsing a default template-argument for a non-type
8626 template-parameter, the first non-nested `>' is taken as the end
8627 of the template parameter-list rather than a greater-than
8628 operator. */
8629 *is_non_type = true;
8630 parameter_declarator
8631 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8632 /*parenthesized_p=*/NULL);
8633 parm = grokdeclarator (parameter_declarator->declarator,
8634 &parameter_declarator->decl_specifiers,
8635 PARM, /*initialized=*/0,
8636 /*attrlist=*/NULL);
8637 if (parm == error_mark_node)
8638 return error_mark_node;
8639 return build_tree_list (parameter_declarator->default_argument, parm);
8640 }
8641
8642 /* Parse a type-parameter.
8643
8644 type-parameter:
8645 class identifier [opt]
8646 class identifier [opt] = type-id
8647 typename identifier [opt]
8648 typename identifier [opt] = type-id
8649 template < template-parameter-list > class identifier [opt]
8650 template < template-parameter-list > class identifier [opt]
8651 = id-expression
8652
8653 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8654 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8655 the declaration of the parameter. */
8656
8657 static tree
8658 cp_parser_type_parameter (cp_parser* parser)
8659 {
8660 cp_token *token;
8661 tree parameter;
8662
8663 /* Look for a keyword to tell us what kind of parameter this is. */
8664 token = cp_parser_require (parser, CPP_KEYWORD,
8665 "`class', `typename', or `template'");
8666 if (!token)
8667 return error_mark_node;
8668
8669 switch (token->keyword)
8670 {
8671 case RID_CLASS:
8672 case RID_TYPENAME:
8673 {
8674 tree identifier;
8675 tree default_argument;
8676
8677 /* If the next token is an identifier, then it names the
8678 parameter. */
8679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8680 identifier = cp_parser_identifier (parser);
8681 else
8682 identifier = NULL_TREE;
8683
8684 /* Create the parameter. */
8685 parameter = finish_template_type_parm (class_type_node, identifier);
8686
8687 /* If the next token is an `=', we have a default argument. */
8688 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8689 {
8690 /* Consume the `=' token. */
8691 cp_lexer_consume_token (parser->lexer);
8692 /* Parse the default-argument. */
8693 push_deferring_access_checks (dk_no_deferred);
8694 default_argument = cp_parser_type_id (parser);
8695 pop_deferring_access_checks ();
8696 }
8697 else
8698 default_argument = NULL_TREE;
8699
8700 /* Create the combined representation of the parameter and the
8701 default argument. */
8702 parameter = build_tree_list (default_argument, parameter);
8703 }
8704 break;
8705
8706 case RID_TEMPLATE:
8707 {
8708 tree parameter_list;
8709 tree identifier;
8710 tree default_argument;
8711
8712 /* Look for the `<'. */
8713 cp_parser_require (parser, CPP_LESS, "`<'");
8714 /* Parse the template-parameter-list. */
8715 parameter_list = cp_parser_template_parameter_list (parser);
8716 /* Look for the `>'. */
8717 cp_parser_require (parser, CPP_GREATER, "`>'");
8718 /* Look for the `class' keyword. */
8719 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8720 /* If the next token is an `=', then there is a
8721 default-argument. If the next token is a `>', we are at
8722 the end of the parameter-list. If the next token is a `,',
8723 then we are at the end of this parameter. */
8724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8725 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8726 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8727 {
8728 identifier = cp_parser_identifier (parser);
8729 /* Treat invalid names as if the parameter were nameless. */
8730 if (identifier == error_mark_node)
8731 identifier = NULL_TREE;
8732 }
8733 else
8734 identifier = NULL_TREE;
8735
8736 /* Create the template parameter. */
8737 parameter = finish_template_template_parm (class_type_node,
8738 identifier);
8739
8740 /* If the next token is an `=', then there is a
8741 default-argument. */
8742 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8743 {
8744 bool is_template;
8745
8746 /* Consume the `='. */
8747 cp_lexer_consume_token (parser->lexer);
8748 /* Parse the id-expression. */
8749 push_deferring_access_checks (dk_no_deferred);
8750 default_argument
8751 = cp_parser_id_expression (parser,
8752 /*template_keyword_p=*/false,
8753 /*check_dependency_p=*/true,
8754 /*template_p=*/&is_template,
8755 /*declarator_p=*/false,
8756 /*optional_p=*/false);
8757 if (TREE_CODE (default_argument) == TYPE_DECL)
8758 /* If the id-expression was a template-id that refers to
8759 a template-class, we already have the declaration here,
8760 so no further lookup is needed. */
8761 ;
8762 else
8763 /* Look up the name. */
8764 default_argument
8765 = cp_parser_lookup_name (parser, default_argument,
8766 none_type,
8767 /*is_template=*/is_template,
8768 /*is_namespace=*/false,
8769 /*check_dependency=*/true,
8770 /*ambiguous_decls=*/NULL);
8771 /* See if the default argument is valid. */
8772 default_argument
8773 = check_template_template_default_arg (default_argument);
8774 pop_deferring_access_checks ();
8775 }
8776 else
8777 default_argument = NULL_TREE;
8778
8779 /* Create the combined representation of the parameter and the
8780 default argument. */
8781 parameter = build_tree_list (default_argument, parameter);
8782 }
8783 break;
8784
8785 default:
8786 gcc_unreachable ();
8787 break;
8788 }
8789
8790 return parameter;
8791 }
8792
8793 /* Parse a template-id.
8794
8795 template-id:
8796 template-name < template-argument-list [opt] >
8797
8798 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8799 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8800 returned. Otherwise, if the template-name names a function, or set
8801 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8802 names a class, returns a TYPE_DECL for the specialization.
8803
8804 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8805 uninstantiated templates. */
8806
8807 static tree
8808 cp_parser_template_id (cp_parser *parser,
8809 bool template_keyword_p,
8810 bool check_dependency_p,
8811 bool is_declaration)
8812 {
8813 tree template;
8814 tree arguments;
8815 tree template_id;
8816 cp_token_position start_of_id = 0;
8817 tree access_check = NULL_TREE;
8818 cp_token *next_token, *next_token_2;
8819 bool is_identifier;
8820
8821 /* If the next token corresponds to a template-id, there is no need
8822 to reparse it. */
8823 next_token = cp_lexer_peek_token (parser->lexer);
8824 if (next_token->type == CPP_TEMPLATE_ID)
8825 {
8826 tree value;
8827 tree check;
8828
8829 /* Get the stored value. */
8830 value = cp_lexer_consume_token (parser->lexer)->value;
8831 /* Perform any access checks that were deferred. */
8832 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8833 perform_or_defer_access_check (TREE_PURPOSE (check),
8834 TREE_VALUE (check),
8835 TREE_VALUE (check));
8836 /* Return the stored value. */
8837 return TREE_VALUE (value);
8838 }
8839
8840 /* Avoid performing name lookup if there is no possibility of
8841 finding a template-id. */
8842 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8843 || (next_token->type == CPP_NAME
8844 && !cp_parser_nth_token_starts_template_argument_list_p
8845 (parser, 2)))
8846 {
8847 cp_parser_error (parser, "expected template-id");
8848 return error_mark_node;
8849 }
8850
8851 /* Remember where the template-id starts. */
8852 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8853 start_of_id = cp_lexer_token_position (parser->lexer, false);
8854
8855 push_deferring_access_checks (dk_deferred);
8856
8857 /* Parse the template-name. */
8858 is_identifier = false;
8859 template = cp_parser_template_name (parser, template_keyword_p,
8860 check_dependency_p,
8861 is_declaration,
8862 &is_identifier);
8863 if (template == error_mark_node || is_identifier)
8864 {
8865 pop_deferring_access_checks ();
8866 return template;
8867 }
8868
8869 /* If we find the sequence `[:' after a template-name, it's probably
8870 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8871 parse correctly the argument list. */
8872 next_token = cp_lexer_peek_token (parser->lexer);
8873 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8874 if (next_token->type == CPP_OPEN_SQUARE
8875 && next_token->flags & DIGRAPH
8876 && next_token_2->type == CPP_COLON
8877 && !(next_token_2->flags & PREV_WHITE))
8878 {
8879 cp_parser_parse_tentatively (parser);
8880 /* Change `:' into `::'. */
8881 next_token_2->type = CPP_SCOPE;
8882 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8883 CPP_LESS. */
8884 cp_lexer_consume_token (parser->lexer);
8885 /* Parse the arguments. */
8886 arguments = cp_parser_enclosed_template_argument_list (parser);
8887 if (!cp_parser_parse_definitely (parser))
8888 {
8889 /* If we couldn't parse an argument list, then we revert our changes
8890 and return simply an error. Maybe this is not a template-id
8891 after all. */
8892 next_token_2->type = CPP_COLON;
8893 cp_parser_error (parser, "expected %<<%>");
8894 pop_deferring_access_checks ();
8895 return error_mark_node;
8896 }
8897 /* Otherwise, emit an error about the invalid digraph, but continue
8898 parsing because we got our argument list. */
8899 pedwarn ("%<<::%> cannot begin a template-argument list");
8900 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8901 "between %<<%> and %<::%>");
8902 if (!flag_permissive)
8903 {
8904 static bool hint;
8905 if (!hint)
8906 {
8907 inform ("(if you use -fpermissive G++ will accept your code)");
8908 hint = true;
8909 }
8910 }
8911 }
8912 else
8913 {
8914 /* Look for the `<' that starts the template-argument-list. */
8915 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8916 {
8917 pop_deferring_access_checks ();
8918 return error_mark_node;
8919 }
8920 /* Parse the arguments. */
8921 arguments = cp_parser_enclosed_template_argument_list (parser);
8922 }
8923
8924 /* Build a representation of the specialization. */
8925 if (TREE_CODE (template) == IDENTIFIER_NODE)
8926 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8927 else if (DECL_CLASS_TEMPLATE_P (template)
8928 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8929 {
8930 bool entering_scope;
8931 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8932 template (rather than some instantiation thereof) only if
8933 is not nested within some other construct. For example, in
8934 "template <typename T> void f(T) { A<T>::", A<T> is just an
8935 instantiation of A. */
8936 entering_scope = (template_parm_scope_p ()
8937 && cp_lexer_next_token_is (parser->lexer,
8938 CPP_SCOPE));
8939 template_id
8940 = finish_template_type (template, arguments, entering_scope);
8941 }
8942 else
8943 {
8944 /* If it's not a class-template or a template-template, it should be
8945 a function-template. */
8946 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8947 || TREE_CODE (template) == OVERLOAD
8948 || BASELINK_P (template)));
8949
8950 template_id = lookup_template_function (template, arguments);
8951 }
8952
8953 /* Retrieve any deferred checks. Do not pop this access checks yet
8954 so the memory will not be reclaimed during token replacing below. */
8955 access_check = get_deferred_access_checks ();
8956
8957 /* If parsing tentatively, replace the sequence of tokens that makes
8958 up the template-id with a CPP_TEMPLATE_ID token. That way,
8959 should we re-parse the token stream, we will not have to repeat
8960 the effort required to do the parse, nor will we issue duplicate
8961 error messages about problems during instantiation of the
8962 template. */
8963 if (start_of_id)
8964 {
8965 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8966
8967 /* Reset the contents of the START_OF_ID token. */
8968 token->type = CPP_TEMPLATE_ID;
8969 token->value = build_tree_list (access_check, template_id);
8970 token->keyword = RID_MAX;
8971
8972 /* Purge all subsequent tokens. */
8973 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8974
8975 /* ??? Can we actually assume that, if template_id ==
8976 error_mark_node, we will have issued a diagnostic to the
8977 user, as opposed to simply marking the tentative parse as
8978 failed? */
8979 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8980 error ("parse error in template argument list");
8981 }
8982
8983 pop_deferring_access_checks ();
8984 return template_id;
8985 }
8986
8987 /* Parse a template-name.
8988
8989 template-name:
8990 identifier
8991
8992 The standard should actually say:
8993
8994 template-name:
8995 identifier
8996 operator-function-id
8997
8998 A defect report has been filed about this issue.
8999
9000 A conversion-function-id cannot be a template name because they cannot
9001 be part of a template-id. In fact, looking at this code:
9002
9003 a.operator K<int>()
9004
9005 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9006 It is impossible to call a templated conversion-function-id with an
9007 explicit argument list, since the only allowed template parameter is
9008 the type to which it is converting.
9009
9010 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9011 `template' keyword, in a construction like:
9012
9013 T::template f<3>()
9014
9015 In that case `f' is taken to be a template-name, even though there
9016 is no way of knowing for sure.
9017
9018 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9019 name refers to a set of overloaded functions, at least one of which
9020 is a template, or an IDENTIFIER_NODE with the name of the template,
9021 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9022 names are looked up inside uninstantiated templates. */
9023
9024 static tree
9025 cp_parser_template_name (cp_parser* parser,
9026 bool template_keyword_p,
9027 bool check_dependency_p,
9028 bool is_declaration,
9029 bool *is_identifier)
9030 {
9031 tree identifier;
9032 tree decl;
9033 tree fns;
9034
9035 /* If the next token is `operator', then we have either an
9036 operator-function-id or a conversion-function-id. */
9037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9038 {
9039 /* We don't know whether we're looking at an
9040 operator-function-id or a conversion-function-id. */
9041 cp_parser_parse_tentatively (parser);
9042 /* Try an operator-function-id. */
9043 identifier = cp_parser_operator_function_id (parser);
9044 /* If that didn't work, try a conversion-function-id. */
9045 if (!cp_parser_parse_definitely (parser))
9046 {
9047 cp_parser_error (parser, "expected template-name");
9048 return error_mark_node;
9049 }
9050 }
9051 /* Look for the identifier. */
9052 else
9053 identifier = cp_parser_identifier (parser);
9054
9055 /* If we didn't find an identifier, we don't have a template-id. */
9056 if (identifier == error_mark_node)
9057 return error_mark_node;
9058
9059 /* If the name immediately followed the `template' keyword, then it
9060 is a template-name. However, if the next token is not `<', then
9061 we do not treat it as a template-name, since it is not being used
9062 as part of a template-id. This enables us to handle constructs
9063 like:
9064
9065 template <typename T> struct S { S(); };
9066 template <typename T> S<T>::S();
9067
9068 correctly. We would treat `S' as a template -- if it were `S<T>'
9069 -- but we do not if there is no `<'. */
9070
9071 if (processing_template_decl
9072 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9073 {
9074 /* In a declaration, in a dependent context, we pretend that the
9075 "template" keyword was present in order to improve error
9076 recovery. For example, given:
9077
9078 template <typename T> void f(T::X<int>);
9079
9080 we want to treat "X<int>" as a template-id. */
9081 if (is_declaration
9082 && !template_keyword_p
9083 && parser->scope && TYPE_P (parser->scope)
9084 && check_dependency_p
9085 && dependent_type_p (parser->scope)
9086 /* Do not do this for dtors (or ctors), since they never
9087 need the template keyword before their name. */
9088 && !constructor_name_p (identifier, parser->scope))
9089 {
9090 cp_token_position start = 0;
9091
9092 /* Explain what went wrong. */
9093 error ("non-template %qD used as template", identifier);
9094 inform ("use %<%T::template %D%> to indicate that it is a template",
9095 parser->scope, identifier);
9096 /* If parsing tentatively, find the location of the "<" token. */
9097 if (cp_parser_simulate_error (parser))
9098 start = cp_lexer_token_position (parser->lexer, true);
9099 /* Parse the template arguments so that we can issue error
9100 messages about them. */
9101 cp_lexer_consume_token (parser->lexer);
9102 cp_parser_enclosed_template_argument_list (parser);
9103 /* Skip tokens until we find a good place from which to
9104 continue parsing. */
9105 cp_parser_skip_to_closing_parenthesis (parser,
9106 /*recovering=*/true,
9107 /*or_comma=*/true,
9108 /*consume_paren=*/false);
9109 /* If parsing tentatively, permanently remove the
9110 template argument list. That will prevent duplicate
9111 error messages from being issued about the missing
9112 "template" keyword. */
9113 if (start)
9114 cp_lexer_purge_tokens_after (parser->lexer, start);
9115 if (is_identifier)
9116 *is_identifier = true;
9117 return identifier;
9118 }
9119
9120 /* If the "template" keyword is present, then there is generally
9121 no point in doing name-lookup, so we just return IDENTIFIER.
9122 But, if the qualifying scope is non-dependent then we can
9123 (and must) do name-lookup normally. */
9124 if (template_keyword_p
9125 && (!parser->scope
9126 || (TYPE_P (parser->scope)
9127 && dependent_type_p (parser->scope))))
9128 return identifier;
9129 }
9130
9131 /* Look up the name. */
9132 decl = cp_parser_lookup_name (parser, identifier,
9133 none_type,
9134 /*is_template=*/false,
9135 /*is_namespace=*/false,
9136 check_dependency_p,
9137 /*ambiguous_decls=*/NULL);
9138 decl = maybe_get_template_decl_from_type_decl (decl);
9139
9140 /* If DECL is a template, then the name was a template-name. */
9141 if (TREE_CODE (decl) == TEMPLATE_DECL)
9142 ;
9143 else
9144 {
9145 tree fn = NULL_TREE;
9146
9147 /* The standard does not explicitly indicate whether a name that
9148 names a set of overloaded declarations, some of which are
9149 templates, is a template-name. However, such a name should
9150 be a template-name; otherwise, there is no way to form a
9151 template-id for the overloaded templates. */
9152 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9153 if (TREE_CODE (fns) == OVERLOAD)
9154 for (fn = fns; fn; fn = OVL_NEXT (fn))
9155 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9156 break;
9157
9158 if (!fn)
9159 {
9160 /* The name does not name a template. */
9161 cp_parser_error (parser, "expected template-name");
9162 return error_mark_node;
9163 }
9164 }
9165
9166 /* If DECL is dependent, and refers to a function, then just return
9167 its name; we will look it up again during template instantiation. */
9168 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9169 {
9170 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9171 if (TYPE_P (scope) && dependent_type_p (scope))
9172 return identifier;
9173 }
9174
9175 return decl;
9176 }
9177
9178 /* Parse a template-argument-list.
9179
9180 template-argument-list:
9181 template-argument
9182 template-argument-list , template-argument
9183
9184 Returns a TREE_VEC containing the arguments. */
9185
9186 static tree
9187 cp_parser_template_argument_list (cp_parser* parser)
9188 {
9189 tree fixed_args[10];
9190 unsigned n_args = 0;
9191 unsigned alloced = 10;
9192 tree *arg_ary = fixed_args;
9193 tree vec;
9194 bool saved_in_template_argument_list_p;
9195 bool saved_ice_p;
9196 bool saved_non_ice_p;
9197
9198 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9199 parser->in_template_argument_list_p = true;
9200 /* Even if the template-id appears in an integral
9201 constant-expression, the contents of the argument list do
9202 not. */
9203 saved_ice_p = parser->integral_constant_expression_p;
9204 parser->integral_constant_expression_p = false;
9205 saved_non_ice_p = parser->non_integral_constant_expression_p;
9206 parser->non_integral_constant_expression_p = false;
9207 /* Parse the arguments. */
9208 do
9209 {
9210 tree argument;
9211
9212 if (n_args)
9213 /* Consume the comma. */
9214 cp_lexer_consume_token (parser->lexer);
9215
9216 /* Parse the template-argument. */
9217 argument = cp_parser_template_argument (parser);
9218 if (n_args == alloced)
9219 {
9220 alloced *= 2;
9221
9222 if (arg_ary == fixed_args)
9223 {
9224 arg_ary = XNEWVEC (tree, alloced);
9225 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9226 }
9227 else
9228 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9229 }
9230 arg_ary[n_args++] = argument;
9231 }
9232 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9233
9234 vec = make_tree_vec (n_args);
9235
9236 while (n_args--)
9237 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9238
9239 if (arg_ary != fixed_args)
9240 free (arg_ary);
9241 parser->non_integral_constant_expression_p = saved_non_ice_p;
9242 parser->integral_constant_expression_p = saved_ice_p;
9243 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9244 return vec;
9245 }
9246
9247 /* Parse a template-argument.
9248
9249 template-argument:
9250 assignment-expression
9251 type-id
9252 id-expression
9253
9254 The representation is that of an assignment-expression, type-id, or
9255 id-expression -- except that the qualified id-expression is
9256 evaluated, so that the value returned is either a DECL or an
9257 OVERLOAD.
9258
9259 Although the standard says "assignment-expression", it forbids
9260 throw-expressions or assignments in the template argument.
9261 Therefore, we use "conditional-expression" instead. */
9262
9263 static tree
9264 cp_parser_template_argument (cp_parser* parser)
9265 {
9266 tree argument;
9267 bool template_p;
9268 bool address_p;
9269 bool maybe_type_id = false;
9270 cp_token *token;
9271 cp_id_kind idk;
9272
9273 /* There's really no way to know what we're looking at, so we just
9274 try each alternative in order.
9275
9276 [temp.arg]
9277
9278 In a template-argument, an ambiguity between a type-id and an
9279 expression is resolved to a type-id, regardless of the form of
9280 the corresponding template-parameter.
9281
9282 Therefore, we try a type-id first. */
9283 cp_parser_parse_tentatively (parser);
9284 argument = cp_parser_type_id (parser);
9285 /* If there was no error parsing the type-id but the next token is a '>>',
9286 we probably found a typo for '> >'. But there are type-id which are
9287 also valid expressions. For instance:
9288
9289 struct X { int operator >> (int); };
9290 template <int V> struct Foo {};
9291 Foo<X () >> 5> r;
9292
9293 Here 'X()' is a valid type-id of a function type, but the user just
9294 wanted to write the expression "X() >> 5". Thus, we remember that we
9295 found a valid type-id, but we still try to parse the argument as an
9296 expression to see what happens. */
9297 if (!cp_parser_error_occurred (parser)
9298 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9299 {
9300 maybe_type_id = true;
9301 cp_parser_abort_tentative_parse (parser);
9302 }
9303 else
9304 {
9305 /* If the next token isn't a `,' or a `>', then this argument wasn't
9306 really finished. This means that the argument is not a valid
9307 type-id. */
9308 if (!cp_parser_next_token_ends_template_argument_p (parser))
9309 cp_parser_error (parser, "expected template-argument");
9310 /* If that worked, we're done. */
9311 if (cp_parser_parse_definitely (parser))
9312 return argument;
9313 }
9314 /* We're still not sure what the argument will be. */
9315 cp_parser_parse_tentatively (parser);
9316 /* Try a template. */
9317 argument = cp_parser_id_expression (parser,
9318 /*template_keyword_p=*/false,
9319 /*check_dependency_p=*/true,
9320 &template_p,
9321 /*declarator_p=*/false,
9322 /*optional_p=*/false);
9323 /* If the next token isn't a `,' or a `>', then this argument wasn't
9324 really finished. */
9325 if (!cp_parser_next_token_ends_template_argument_p (parser))
9326 cp_parser_error (parser, "expected template-argument");
9327 if (!cp_parser_error_occurred (parser))
9328 {
9329 /* Figure out what is being referred to. If the id-expression
9330 was for a class template specialization, then we will have a
9331 TYPE_DECL at this point. There is no need to do name lookup
9332 at this point in that case. */
9333 if (TREE_CODE (argument) != TYPE_DECL)
9334 argument = cp_parser_lookup_name (parser, argument,
9335 none_type,
9336 /*is_template=*/template_p,
9337 /*is_namespace=*/false,
9338 /*check_dependency=*/true,
9339 /*ambiguous_decls=*/NULL);
9340 if (TREE_CODE (argument) != TEMPLATE_DECL
9341 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9342 cp_parser_error (parser, "expected template-name");
9343 }
9344 if (cp_parser_parse_definitely (parser))
9345 return argument;
9346 /* It must be a non-type argument. There permitted cases are given
9347 in [temp.arg.nontype]:
9348
9349 -- an integral constant-expression of integral or enumeration
9350 type; or
9351
9352 -- the name of a non-type template-parameter; or
9353
9354 -- the name of an object or function with external linkage...
9355
9356 -- the address of an object or function with external linkage...
9357
9358 -- a pointer to member... */
9359 /* Look for a non-type template parameter. */
9360 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9361 {
9362 cp_parser_parse_tentatively (parser);
9363 argument = cp_parser_primary_expression (parser,
9364 /*adress_p=*/false,
9365 /*cast_p=*/false,
9366 /*template_arg_p=*/true,
9367 &idk);
9368 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9369 || !cp_parser_next_token_ends_template_argument_p (parser))
9370 cp_parser_simulate_error (parser);
9371 if (cp_parser_parse_definitely (parser))
9372 return argument;
9373 }
9374
9375 /* If the next token is "&", the argument must be the address of an
9376 object or function with external linkage. */
9377 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9378 if (address_p)
9379 cp_lexer_consume_token (parser->lexer);
9380 /* See if we might have an id-expression. */
9381 token = cp_lexer_peek_token (parser->lexer);
9382 if (token->type == CPP_NAME
9383 || token->keyword == RID_OPERATOR
9384 || token->type == CPP_SCOPE
9385 || token->type == CPP_TEMPLATE_ID
9386 || token->type == CPP_NESTED_NAME_SPECIFIER)
9387 {
9388 cp_parser_parse_tentatively (parser);
9389 argument = cp_parser_primary_expression (parser,
9390 address_p,
9391 /*cast_p=*/false,
9392 /*template_arg_p=*/true,
9393 &idk);
9394 if (cp_parser_error_occurred (parser)
9395 || !cp_parser_next_token_ends_template_argument_p (parser))
9396 cp_parser_abort_tentative_parse (parser);
9397 else
9398 {
9399 if (TREE_CODE (argument) == INDIRECT_REF)
9400 {
9401 gcc_assert (REFERENCE_REF_P (argument));
9402 argument = TREE_OPERAND (argument, 0);
9403 }
9404
9405 if (TREE_CODE (argument) == VAR_DECL)
9406 {
9407 /* A variable without external linkage might still be a
9408 valid constant-expression, so no error is issued here
9409 if the external-linkage check fails. */
9410 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9411 cp_parser_simulate_error (parser);
9412 }
9413 else if (is_overloaded_fn (argument))
9414 /* All overloaded functions are allowed; if the external
9415 linkage test does not pass, an error will be issued
9416 later. */
9417 ;
9418 else if (address_p
9419 && (TREE_CODE (argument) == OFFSET_REF
9420 || TREE_CODE (argument) == SCOPE_REF))
9421 /* A pointer-to-member. */
9422 ;
9423 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9424 ;
9425 else
9426 cp_parser_simulate_error (parser);
9427
9428 if (cp_parser_parse_definitely (parser))
9429 {
9430 if (address_p)
9431 argument = build_x_unary_op (ADDR_EXPR, argument);
9432 return argument;
9433 }
9434 }
9435 }
9436 /* If the argument started with "&", there are no other valid
9437 alternatives at this point. */
9438 if (address_p)
9439 {
9440 cp_parser_error (parser, "invalid non-type template argument");
9441 return error_mark_node;
9442 }
9443
9444 /* If the argument wasn't successfully parsed as a type-id followed
9445 by '>>', the argument can only be a constant expression now.
9446 Otherwise, we try parsing the constant-expression tentatively,
9447 because the argument could really be a type-id. */
9448 if (maybe_type_id)
9449 cp_parser_parse_tentatively (parser);
9450 argument = cp_parser_constant_expression (parser,
9451 /*allow_non_constant_p=*/false,
9452 /*non_constant_p=*/NULL);
9453 argument = fold_non_dependent_expr (argument);
9454 if (!maybe_type_id)
9455 return argument;
9456 if (!cp_parser_next_token_ends_template_argument_p (parser))
9457 cp_parser_error (parser, "expected template-argument");
9458 if (cp_parser_parse_definitely (parser))
9459 return argument;
9460 /* We did our best to parse the argument as a non type-id, but that
9461 was the only alternative that matched (albeit with a '>' after
9462 it). We can assume it's just a typo from the user, and a
9463 diagnostic will then be issued. */
9464 return cp_parser_type_id (parser);
9465 }
9466
9467 /* Parse an explicit-instantiation.
9468
9469 explicit-instantiation:
9470 template declaration
9471
9472 Although the standard says `declaration', what it really means is:
9473
9474 explicit-instantiation:
9475 template decl-specifier-seq [opt] declarator [opt] ;
9476
9477 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9478 supposed to be allowed. A defect report has been filed about this
9479 issue.
9480
9481 GNU Extension:
9482
9483 explicit-instantiation:
9484 storage-class-specifier template
9485 decl-specifier-seq [opt] declarator [opt] ;
9486 function-specifier template
9487 decl-specifier-seq [opt] declarator [opt] ; */
9488
9489 static void
9490 cp_parser_explicit_instantiation (cp_parser* parser)
9491 {
9492 int declares_class_or_enum;
9493 cp_decl_specifier_seq decl_specifiers;
9494 tree extension_specifier = NULL_TREE;
9495
9496 /* Look for an (optional) storage-class-specifier or
9497 function-specifier. */
9498 if (cp_parser_allow_gnu_extensions_p (parser))
9499 {
9500 extension_specifier
9501 = cp_parser_storage_class_specifier_opt (parser);
9502 if (!extension_specifier)
9503 extension_specifier
9504 = cp_parser_function_specifier_opt (parser,
9505 /*decl_specs=*/NULL);
9506 }
9507
9508 /* Look for the `template' keyword. */
9509 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9510 /* Let the front end know that we are processing an explicit
9511 instantiation. */
9512 begin_explicit_instantiation ();
9513 /* [temp.explicit] says that we are supposed to ignore access
9514 control while processing explicit instantiation directives. */
9515 push_deferring_access_checks (dk_no_check);
9516 /* Parse a decl-specifier-seq. */
9517 cp_parser_decl_specifier_seq (parser,
9518 CP_PARSER_FLAGS_OPTIONAL,
9519 &decl_specifiers,
9520 &declares_class_or_enum);
9521 /* If there was exactly one decl-specifier, and it declared a class,
9522 and there's no declarator, then we have an explicit type
9523 instantiation. */
9524 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9525 {
9526 tree type;
9527
9528 type = check_tag_decl (&decl_specifiers);
9529 /* Turn access control back on for names used during
9530 template instantiation. */
9531 pop_deferring_access_checks ();
9532 if (type)
9533 do_type_instantiation (type, extension_specifier,
9534 /*complain=*/tf_error);
9535 }
9536 else
9537 {
9538 cp_declarator *declarator;
9539 tree decl;
9540
9541 /* Parse the declarator. */
9542 declarator
9543 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9544 /*ctor_dtor_or_conv_p=*/NULL,
9545 /*parenthesized_p=*/NULL,
9546 /*member_p=*/false);
9547 if (declares_class_or_enum & 2)
9548 cp_parser_check_for_definition_in_return_type (declarator,
9549 decl_specifiers.type);
9550 if (declarator != cp_error_declarator)
9551 {
9552 decl = grokdeclarator (declarator, &decl_specifiers,
9553 NORMAL, 0, &decl_specifiers.attributes);
9554 /* Turn access control back on for names used during
9555 template instantiation. */
9556 pop_deferring_access_checks ();
9557 /* Do the explicit instantiation. */
9558 do_decl_instantiation (decl, extension_specifier);
9559 }
9560 else
9561 {
9562 pop_deferring_access_checks ();
9563 /* Skip the body of the explicit instantiation. */
9564 cp_parser_skip_to_end_of_statement (parser);
9565 }
9566 }
9567 /* We're done with the instantiation. */
9568 end_explicit_instantiation ();
9569
9570 cp_parser_consume_semicolon_at_end_of_statement (parser);
9571 }
9572
9573 /* Parse an explicit-specialization.
9574
9575 explicit-specialization:
9576 template < > declaration
9577
9578 Although the standard says `declaration', what it really means is:
9579
9580 explicit-specialization:
9581 template <> decl-specifier [opt] init-declarator [opt] ;
9582 template <> function-definition
9583 template <> explicit-specialization
9584 template <> template-declaration */
9585
9586 static void
9587 cp_parser_explicit_specialization (cp_parser* parser)
9588 {
9589 bool need_lang_pop;
9590 /* Look for the `template' keyword. */
9591 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9592 /* Look for the `<'. */
9593 cp_parser_require (parser, CPP_LESS, "`<'");
9594 /* Look for the `>'. */
9595 cp_parser_require (parser, CPP_GREATER, "`>'");
9596 /* We have processed another parameter list. */
9597 ++parser->num_template_parameter_lists;
9598 /* [temp]
9599
9600 A template ... explicit specialization ... shall not have C
9601 linkage. */
9602 if (current_lang_name == lang_name_c)
9603 {
9604 error ("template specialization with C linkage");
9605 /* Give it C++ linkage to avoid confusing other parts of the
9606 front end. */
9607 push_lang_context (lang_name_cplusplus);
9608 need_lang_pop = true;
9609 }
9610 else
9611 need_lang_pop = false;
9612 /* Let the front end know that we are beginning a specialization. */
9613 if (!begin_specialization ())
9614 {
9615 end_specialization ();
9616 cp_parser_skip_to_end_of_block_or_statement (parser);
9617 return;
9618 }
9619
9620 /* If the next keyword is `template', we need to figure out whether
9621 or not we're looking a template-declaration. */
9622 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9623 {
9624 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9625 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9626 cp_parser_template_declaration_after_export (parser,
9627 /*member_p=*/false);
9628 else
9629 cp_parser_explicit_specialization (parser);
9630 }
9631 else
9632 /* Parse the dependent declaration. */
9633 cp_parser_single_declaration (parser,
9634 /*checks=*/NULL_TREE,
9635 /*member_p=*/false,
9636 /*friend_p=*/NULL);
9637 /* We're done with the specialization. */
9638 end_specialization ();
9639 /* For the erroneous case of a template with C linkage, we pushed an
9640 implicit C++ linkage scope; exit that scope now. */
9641 if (need_lang_pop)
9642 pop_lang_context ();
9643 /* We're done with this parameter list. */
9644 --parser->num_template_parameter_lists;
9645 }
9646
9647 /* Parse a type-specifier.
9648
9649 type-specifier:
9650 simple-type-specifier
9651 class-specifier
9652 enum-specifier
9653 elaborated-type-specifier
9654 cv-qualifier
9655
9656 GNU Extension:
9657
9658 type-specifier:
9659 __complex__
9660
9661 Returns a representation of the type-specifier. For a
9662 class-specifier, enum-specifier, or elaborated-type-specifier, a
9663 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9664
9665 The parser flags FLAGS is used to control type-specifier parsing.
9666
9667 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9668 in a decl-specifier-seq.
9669
9670 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9671 class-specifier, enum-specifier, or elaborated-type-specifier, then
9672 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9673 if a type is declared; 2 if it is defined. Otherwise, it is set to
9674 zero.
9675
9676 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9677 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9678 is set to FALSE. */
9679
9680 static tree
9681 cp_parser_type_specifier (cp_parser* parser,
9682 cp_parser_flags flags,
9683 cp_decl_specifier_seq *decl_specs,
9684 bool is_declaration,
9685 int* declares_class_or_enum,
9686 bool* is_cv_qualifier)
9687 {
9688 tree type_spec = NULL_TREE;
9689 cp_token *token;
9690 enum rid keyword;
9691 cp_decl_spec ds = ds_last;
9692
9693 /* Assume this type-specifier does not declare a new type. */
9694 if (declares_class_or_enum)
9695 *declares_class_or_enum = 0;
9696 /* And that it does not specify a cv-qualifier. */
9697 if (is_cv_qualifier)
9698 *is_cv_qualifier = false;
9699 /* Peek at the next token. */
9700 token = cp_lexer_peek_token (parser->lexer);
9701
9702 /* If we're looking at a keyword, we can use that to guide the
9703 production we choose. */
9704 keyword = token->keyword;
9705 switch (keyword)
9706 {
9707 case RID_ENUM:
9708 /* Look for the enum-specifier. */
9709 type_spec = cp_parser_enum_specifier (parser);
9710 /* If that worked, we're done. */
9711 if (type_spec)
9712 {
9713 if (declares_class_or_enum)
9714 *declares_class_or_enum = 2;
9715 if (decl_specs)
9716 cp_parser_set_decl_spec_type (decl_specs,
9717 type_spec,
9718 /*user_defined_p=*/true);
9719 return type_spec;
9720 }
9721 else
9722 goto elaborated_type_specifier;
9723
9724 /* Any of these indicate either a class-specifier, or an
9725 elaborated-type-specifier. */
9726 case RID_CLASS:
9727 case RID_STRUCT:
9728 case RID_UNION:
9729 /* Parse tentatively so that we can back up if we don't find a
9730 class-specifier. */
9731 cp_parser_parse_tentatively (parser);
9732 /* Look for the class-specifier. */
9733 type_spec = cp_parser_class_specifier (parser);
9734 /* If that worked, we're done. */
9735 if (cp_parser_parse_definitely (parser))
9736 {
9737 if (declares_class_or_enum)
9738 *declares_class_or_enum = 2;
9739 if (decl_specs)
9740 cp_parser_set_decl_spec_type (decl_specs,
9741 type_spec,
9742 /*user_defined_p=*/true);
9743 return type_spec;
9744 }
9745
9746 /* Fall through. */
9747 elaborated_type_specifier:
9748 /* We're declaring (not defining) a class or enum. */
9749 if (declares_class_or_enum)
9750 *declares_class_or_enum = 1;
9751
9752 /* Fall through. */
9753 case RID_TYPENAME:
9754 /* Look for an elaborated-type-specifier. */
9755 type_spec
9756 = (cp_parser_elaborated_type_specifier
9757 (parser,
9758 decl_specs && decl_specs->specs[(int) ds_friend],
9759 is_declaration));
9760 if (decl_specs)
9761 cp_parser_set_decl_spec_type (decl_specs,
9762 type_spec,
9763 /*user_defined_p=*/true);
9764 return type_spec;
9765
9766 case RID_CONST:
9767 ds = ds_const;
9768 if (is_cv_qualifier)
9769 *is_cv_qualifier = true;
9770 break;
9771
9772 case RID_VOLATILE:
9773 ds = ds_volatile;
9774 if (is_cv_qualifier)
9775 *is_cv_qualifier = true;
9776 break;
9777
9778 case RID_RESTRICT:
9779 ds = ds_restrict;
9780 if (is_cv_qualifier)
9781 *is_cv_qualifier = true;
9782 break;
9783
9784 case RID_COMPLEX:
9785 /* The `__complex__' keyword is a GNU extension. */
9786 ds = ds_complex;
9787 break;
9788
9789 default:
9790 break;
9791 }
9792
9793 /* Handle simple keywords. */
9794 if (ds != ds_last)
9795 {
9796 if (decl_specs)
9797 {
9798 ++decl_specs->specs[(int)ds];
9799 decl_specs->any_specifiers_p = true;
9800 }
9801 return cp_lexer_consume_token (parser->lexer)->value;
9802 }
9803
9804 /* If we do not already have a type-specifier, assume we are looking
9805 at a simple-type-specifier. */
9806 type_spec = cp_parser_simple_type_specifier (parser,
9807 decl_specs,
9808 flags);
9809
9810 /* If we didn't find a type-specifier, and a type-specifier was not
9811 optional in this context, issue an error message. */
9812 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9813 {
9814 cp_parser_error (parser, "expected type specifier");
9815 return error_mark_node;
9816 }
9817
9818 return type_spec;
9819 }
9820
9821 /* Parse a simple-type-specifier.
9822
9823 simple-type-specifier:
9824 :: [opt] nested-name-specifier [opt] type-name
9825 :: [opt] nested-name-specifier template template-id
9826 char
9827 wchar_t
9828 bool
9829 short
9830 int
9831 long
9832 signed
9833 unsigned
9834 float
9835 double
9836 void
9837
9838 GNU Extension:
9839
9840 simple-type-specifier:
9841 __typeof__ unary-expression
9842 __typeof__ ( type-id )
9843
9844 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9845 appropriately updated. */
9846
9847 static tree
9848 cp_parser_simple_type_specifier (cp_parser* parser,
9849 cp_decl_specifier_seq *decl_specs,
9850 cp_parser_flags flags)
9851 {
9852 tree type = NULL_TREE;
9853 cp_token *token;
9854
9855 /* Peek at the next token. */
9856 token = cp_lexer_peek_token (parser->lexer);
9857
9858 /* If we're looking at a keyword, things are easy. */
9859 switch (token->keyword)
9860 {
9861 case RID_CHAR:
9862 if (decl_specs)
9863 decl_specs->explicit_char_p = true;
9864 type = char_type_node;
9865 break;
9866 case RID_WCHAR:
9867 type = wchar_type_node;
9868 break;
9869 case RID_BOOL:
9870 type = boolean_type_node;
9871 break;
9872 case RID_SHORT:
9873 if (decl_specs)
9874 ++decl_specs->specs[(int) ds_short];
9875 type = short_integer_type_node;
9876 break;
9877 case RID_INT:
9878 if (decl_specs)
9879 decl_specs->explicit_int_p = true;
9880 type = integer_type_node;
9881 break;
9882 case RID_LONG:
9883 if (decl_specs)
9884 ++decl_specs->specs[(int) ds_long];
9885 type = long_integer_type_node;
9886 break;
9887 case RID_SIGNED:
9888 if (decl_specs)
9889 ++decl_specs->specs[(int) ds_signed];
9890 type = integer_type_node;
9891 break;
9892 case RID_UNSIGNED:
9893 if (decl_specs)
9894 ++decl_specs->specs[(int) ds_unsigned];
9895 type = unsigned_type_node;
9896 break;
9897 case RID_FLOAT:
9898 type = float_type_node;
9899 break;
9900 case RID_DOUBLE:
9901 type = double_type_node;
9902 break;
9903 case RID_VOID:
9904 type = void_type_node;
9905 break;
9906
9907 case RID_TYPEOF:
9908 /* Consume the `typeof' token. */
9909 cp_lexer_consume_token (parser->lexer);
9910 /* Parse the operand to `typeof'. */
9911 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9912 /* If it is not already a TYPE, take its type. */
9913 if (!TYPE_P (type))
9914 type = finish_typeof (type);
9915
9916 if (decl_specs)
9917 cp_parser_set_decl_spec_type (decl_specs, type,
9918 /*user_defined_p=*/true);
9919
9920 return type;
9921
9922 default:
9923 break;
9924 }
9925
9926 /* If the type-specifier was for a built-in type, we're done. */
9927 if (type)
9928 {
9929 tree id;
9930
9931 /* Record the type. */
9932 if (decl_specs
9933 && (token->keyword != RID_SIGNED
9934 && token->keyword != RID_UNSIGNED
9935 && token->keyword != RID_SHORT
9936 && token->keyword != RID_LONG))
9937 cp_parser_set_decl_spec_type (decl_specs,
9938 type,
9939 /*user_defined=*/false);
9940 if (decl_specs)
9941 decl_specs->any_specifiers_p = true;
9942
9943 /* Consume the token. */
9944 id = cp_lexer_consume_token (parser->lexer)->value;
9945
9946 /* There is no valid C++ program where a non-template type is
9947 followed by a "<". That usually indicates that the user thought
9948 that the type was a template. */
9949 cp_parser_check_for_invalid_template_id (parser, type);
9950
9951 return TYPE_NAME (type);
9952 }
9953
9954 /* The type-specifier must be a user-defined type. */
9955 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9956 {
9957 bool qualified_p;
9958 bool global_p;
9959
9960 /* Don't gobble tokens or issue error messages if this is an
9961 optional type-specifier. */
9962 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9963 cp_parser_parse_tentatively (parser);
9964
9965 /* Look for the optional `::' operator. */
9966 global_p
9967 = (cp_parser_global_scope_opt (parser,
9968 /*current_scope_valid_p=*/false)
9969 != NULL_TREE);
9970 /* Look for the nested-name specifier. */
9971 qualified_p
9972 = (cp_parser_nested_name_specifier_opt (parser,
9973 /*typename_keyword_p=*/false,
9974 /*check_dependency_p=*/true,
9975 /*type_p=*/false,
9976 /*is_declaration=*/false)
9977 != NULL_TREE);
9978 /* If we have seen a nested-name-specifier, and the next token
9979 is `template', then we are using the template-id production. */
9980 if (parser->scope
9981 && cp_parser_optional_template_keyword (parser))
9982 {
9983 /* Look for the template-id. */
9984 type = cp_parser_template_id (parser,
9985 /*template_keyword_p=*/true,
9986 /*check_dependency_p=*/true,
9987 /*is_declaration=*/false);
9988 /* If the template-id did not name a type, we are out of
9989 luck. */
9990 if (TREE_CODE (type) != TYPE_DECL)
9991 {
9992 cp_parser_error (parser, "expected template-id for type");
9993 type = NULL_TREE;
9994 }
9995 }
9996 /* Otherwise, look for a type-name. */
9997 else
9998 type = cp_parser_type_name (parser);
9999 /* Keep track of all name-lookups performed in class scopes. */
10000 if (type
10001 && !global_p
10002 && !qualified_p
10003 && TREE_CODE (type) == TYPE_DECL
10004 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10005 maybe_note_name_used_in_class (DECL_NAME (type), type);
10006 /* If it didn't work out, we don't have a TYPE. */
10007 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10008 && !cp_parser_parse_definitely (parser))
10009 type = NULL_TREE;
10010 if (type && decl_specs)
10011 cp_parser_set_decl_spec_type (decl_specs, type,
10012 /*user_defined=*/true);
10013 }
10014
10015 /* If we didn't get a type-name, issue an error message. */
10016 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10017 {
10018 cp_parser_error (parser, "expected type-name");
10019 return error_mark_node;
10020 }
10021
10022 /* There is no valid C++ program where a non-template type is
10023 followed by a "<". That usually indicates that the user thought
10024 that the type was a template. */
10025 if (type && type != error_mark_node)
10026 {
10027 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10028 If it is, then the '<'...'>' enclose protocol names rather than
10029 template arguments, and so everything is fine. */
10030 if (c_dialect_objc ()
10031 && (objc_is_id (type) || objc_is_class_name (type)))
10032 {
10033 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10034 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10035
10036 /* Clobber the "unqualified" type previously entered into
10037 DECL_SPECS with the new, improved protocol-qualified version. */
10038 if (decl_specs)
10039 decl_specs->type = qual_type;
10040
10041 return qual_type;
10042 }
10043
10044 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10045 }
10046
10047 return type;
10048 }
10049
10050 /* Parse a type-name.
10051
10052 type-name:
10053 class-name
10054 enum-name
10055 typedef-name
10056
10057 enum-name:
10058 identifier
10059
10060 typedef-name:
10061 identifier
10062
10063 Returns a TYPE_DECL for the type. */
10064
10065 static tree
10066 cp_parser_type_name (cp_parser* parser)
10067 {
10068 tree type_decl;
10069 tree identifier;
10070
10071 /* We can't know yet whether it is a class-name or not. */
10072 cp_parser_parse_tentatively (parser);
10073 /* Try a class-name. */
10074 type_decl = cp_parser_class_name (parser,
10075 /*typename_keyword_p=*/false,
10076 /*template_keyword_p=*/false,
10077 none_type,
10078 /*check_dependency_p=*/true,
10079 /*class_head_p=*/false,
10080 /*is_declaration=*/false);
10081 /* If it's not a class-name, keep looking. */
10082 if (!cp_parser_parse_definitely (parser))
10083 {
10084 /* It must be a typedef-name or an enum-name. */
10085 identifier = cp_parser_identifier (parser);
10086 if (identifier == error_mark_node)
10087 return error_mark_node;
10088
10089 /* Look up the type-name. */
10090 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10091
10092 if (TREE_CODE (type_decl) != TYPE_DECL
10093 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10094 {
10095 /* See if this is an Objective-C type. */
10096 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10097 tree type = objc_get_protocol_qualified_type (identifier, protos);
10098 if (type)
10099 type_decl = TYPE_NAME (type);
10100 }
10101
10102 /* Issue an error if we did not find a type-name. */
10103 if (TREE_CODE (type_decl) != TYPE_DECL)
10104 {
10105 if (!cp_parser_simulate_error (parser))
10106 cp_parser_name_lookup_error (parser, identifier, type_decl,
10107 "is not a type");
10108 type_decl = error_mark_node;
10109 }
10110 /* Remember that the name was used in the definition of the
10111 current class so that we can check later to see if the
10112 meaning would have been different after the class was
10113 entirely defined. */
10114 else if (type_decl != error_mark_node
10115 && !parser->scope)
10116 maybe_note_name_used_in_class (identifier, type_decl);
10117 }
10118
10119 return type_decl;
10120 }
10121
10122
10123 /* Parse an elaborated-type-specifier. Note that the grammar given
10124 here incorporates the resolution to DR68.
10125
10126 elaborated-type-specifier:
10127 class-key :: [opt] nested-name-specifier [opt] identifier
10128 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10129 enum :: [opt] nested-name-specifier [opt] identifier
10130 typename :: [opt] nested-name-specifier identifier
10131 typename :: [opt] nested-name-specifier template [opt]
10132 template-id
10133
10134 GNU extension:
10135
10136 elaborated-type-specifier:
10137 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10138 class-key attributes :: [opt] nested-name-specifier [opt]
10139 template [opt] template-id
10140 enum attributes :: [opt] nested-name-specifier [opt] identifier
10141
10142 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10143 declared `friend'. If IS_DECLARATION is TRUE, then this
10144 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10145 something is being declared.
10146
10147 Returns the TYPE specified. */
10148
10149 static tree
10150 cp_parser_elaborated_type_specifier (cp_parser* parser,
10151 bool is_friend,
10152 bool is_declaration)
10153 {
10154 enum tag_types tag_type;
10155 tree identifier;
10156 tree type = NULL_TREE;
10157 tree attributes = NULL_TREE;
10158
10159 /* See if we're looking at the `enum' keyword. */
10160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10161 {
10162 /* Consume the `enum' token. */
10163 cp_lexer_consume_token (parser->lexer);
10164 /* Remember that it's an enumeration type. */
10165 tag_type = enum_type;
10166 /* Parse the attributes. */
10167 attributes = cp_parser_attributes_opt (parser);
10168 }
10169 /* Or, it might be `typename'. */
10170 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10171 RID_TYPENAME))
10172 {
10173 /* Consume the `typename' token. */
10174 cp_lexer_consume_token (parser->lexer);
10175 /* Remember that it's a `typename' type. */
10176 tag_type = typename_type;
10177 /* The `typename' keyword is only allowed in templates. */
10178 if (!processing_template_decl)
10179 pedwarn ("using %<typename%> outside of template");
10180 }
10181 /* Otherwise it must be a class-key. */
10182 else
10183 {
10184 tag_type = cp_parser_class_key (parser);
10185 if (tag_type == none_type)
10186 return error_mark_node;
10187 /* Parse the attributes. */
10188 attributes = cp_parser_attributes_opt (parser);
10189 }
10190
10191 /* Look for the `::' operator. */
10192 cp_parser_global_scope_opt (parser,
10193 /*current_scope_valid_p=*/false);
10194 /* Look for the nested-name-specifier. */
10195 if (tag_type == typename_type)
10196 {
10197 if (!cp_parser_nested_name_specifier (parser,
10198 /*typename_keyword_p=*/true,
10199 /*check_dependency_p=*/true,
10200 /*type_p=*/true,
10201 is_declaration))
10202 return error_mark_node;
10203 }
10204 else
10205 /* Even though `typename' is not present, the proposed resolution
10206 to Core Issue 180 says that in `class A<T>::B', `B' should be
10207 considered a type-name, even if `A<T>' is dependent. */
10208 cp_parser_nested_name_specifier_opt (parser,
10209 /*typename_keyword_p=*/true,
10210 /*check_dependency_p=*/true,
10211 /*type_p=*/true,
10212 is_declaration);
10213 /* For everything but enumeration types, consider a template-id. */
10214 /* For an enumeration type, consider only a plain identifier. */
10215 if (tag_type != enum_type)
10216 {
10217 bool template_p = false;
10218 tree decl;
10219
10220 /* Allow the `template' keyword. */
10221 template_p = cp_parser_optional_template_keyword (parser);
10222 /* If we didn't see `template', we don't know if there's a
10223 template-id or not. */
10224 if (!template_p)
10225 cp_parser_parse_tentatively (parser);
10226 /* Parse the template-id. */
10227 decl = cp_parser_template_id (parser, template_p,
10228 /*check_dependency_p=*/true,
10229 is_declaration);
10230 /* If we didn't find a template-id, look for an ordinary
10231 identifier. */
10232 if (!template_p && !cp_parser_parse_definitely (parser))
10233 ;
10234 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10235 in effect, then we must assume that, upon instantiation, the
10236 template will correspond to a class. */
10237 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10238 && tag_type == typename_type)
10239 type = make_typename_type (parser->scope, decl,
10240 typename_type,
10241 /*complain=*/tf_error);
10242 else
10243 type = TREE_TYPE (decl);
10244 }
10245
10246 if (!type)
10247 {
10248 identifier = cp_parser_identifier (parser);
10249
10250 if (identifier == error_mark_node)
10251 {
10252 parser->scope = NULL_TREE;
10253 return error_mark_node;
10254 }
10255
10256 /* For a `typename', we needn't call xref_tag. */
10257 if (tag_type == typename_type
10258 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10259 return cp_parser_make_typename_type (parser, parser->scope,
10260 identifier);
10261 /* Look up a qualified name in the usual way. */
10262 if (parser->scope)
10263 {
10264 tree decl;
10265
10266 decl = cp_parser_lookup_name (parser, identifier,
10267 tag_type,
10268 /*is_template=*/false,
10269 /*is_namespace=*/false,
10270 /*check_dependency=*/true,
10271 /*ambiguous_decls=*/NULL);
10272
10273 /* If we are parsing friend declaration, DECL may be a
10274 TEMPLATE_DECL tree node here. However, we need to check
10275 whether this TEMPLATE_DECL results in valid code. Consider
10276 the following example:
10277
10278 namespace N {
10279 template <class T> class C {};
10280 }
10281 class X {
10282 template <class T> friend class N::C; // #1, valid code
10283 };
10284 template <class T> class Y {
10285 friend class N::C; // #2, invalid code
10286 };
10287
10288 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10289 name lookup of `N::C'. We see that friend declaration must
10290 be template for the code to be valid. Note that
10291 processing_template_decl does not work here since it is
10292 always 1 for the above two cases. */
10293
10294 decl = (cp_parser_maybe_treat_template_as_class
10295 (decl, /*tag_name_p=*/is_friend
10296 && parser->num_template_parameter_lists));
10297
10298 if (TREE_CODE (decl) != TYPE_DECL)
10299 {
10300 cp_parser_diagnose_invalid_type_name (parser,
10301 parser->scope,
10302 identifier);
10303 return error_mark_node;
10304 }
10305
10306 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10307 check_elaborated_type_specifier
10308 (tag_type, decl,
10309 (parser->num_template_parameter_lists
10310 || DECL_SELF_REFERENCE_P (decl)));
10311
10312 type = TREE_TYPE (decl);
10313 }
10314 else
10315 {
10316 /* An elaborated-type-specifier sometimes introduces a new type and
10317 sometimes names an existing type. Normally, the rule is that it
10318 introduces a new type only if there is not an existing type of
10319 the same name already in scope. For example, given:
10320
10321 struct S {};
10322 void f() { struct S s; }
10323
10324 the `struct S' in the body of `f' is the same `struct S' as in
10325 the global scope; the existing definition is used. However, if
10326 there were no global declaration, this would introduce a new
10327 local class named `S'.
10328
10329 An exception to this rule applies to the following code:
10330
10331 namespace N { struct S; }
10332
10333 Here, the elaborated-type-specifier names a new type
10334 unconditionally; even if there is already an `S' in the
10335 containing scope this declaration names a new type.
10336 This exception only applies if the elaborated-type-specifier
10337 forms the complete declaration:
10338
10339 [class.name]
10340
10341 A declaration consisting solely of `class-key identifier ;' is
10342 either a redeclaration of the name in the current scope or a
10343 forward declaration of the identifier as a class name. It
10344 introduces the name into the current scope.
10345
10346 We are in this situation precisely when the next token is a `;'.
10347
10348 An exception to the exception is that a `friend' declaration does
10349 *not* name a new type; i.e., given:
10350
10351 struct S { friend struct T; };
10352
10353 `T' is not a new type in the scope of `S'.
10354
10355 Also, `new struct S' or `sizeof (struct S)' never results in the
10356 definition of a new type; a new type can only be declared in a
10357 declaration context. */
10358
10359 tag_scope ts;
10360 bool template_p;
10361
10362 if (is_friend)
10363 /* Friends have special name lookup rules. */
10364 ts = ts_within_enclosing_non_class;
10365 else if (is_declaration
10366 && cp_lexer_next_token_is (parser->lexer,
10367 CPP_SEMICOLON))
10368 /* This is a `class-key identifier ;' */
10369 ts = ts_current;
10370 else
10371 ts = ts_global;
10372
10373 template_p =
10374 (parser->num_template_parameter_lists
10375 && (cp_parser_next_token_starts_class_definition_p (parser)
10376 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10377 /* An unqualified name was used to reference this type, so
10378 there were no qualifying templates. */
10379 if (!cp_parser_check_template_parameters (parser,
10380 /*num_templates=*/0))
10381 return error_mark_node;
10382 type = xref_tag (tag_type, identifier, ts, template_p);
10383 }
10384 }
10385
10386 if (type == error_mark_node)
10387 return error_mark_node;
10388
10389 /* Allow attributes on forward declarations of classes. */
10390 if (attributes)
10391 {
10392 if (TREE_CODE (type) == TYPENAME_TYPE)
10393 warning (OPT_Wattributes,
10394 "attributes ignored on uninstantiated type");
10395 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10396 && ! processing_explicit_instantiation)
10397 warning (OPT_Wattributes,
10398 "attributes ignored on template instantiation");
10399 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10400 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10401 else
10402 warning (OPT_Wattributes,
10403 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10404 }
10405
10406 if (tag_type != enum_type)
10407 cp_parser_check_class_key (tag_type, type);
10408
10409 /* A "<" cannot follow an elaborated type specifier. If that
10410 happens, the user was probably trying to form a template-id. */
10411 cp_parser_check_for_invalid_template_id (parser, type);
10412
10413 return type;
10414 }
10415
10416 /* Parse an enum-specifier.
10417
10418 enum-specifier:
10419 enum identifier [opt] { enumerator-list [opt] }
10420
10421 GNU Extensions:
10422 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10423 attributes[opt]
10424
10425 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10426 if the token stream isn't an enum-specifier after all. */
10427
10428 static tree
10429 cp_parser_enum_specifier (cp_parser* parser)
10430 {
10431 tree identifier;
10432 tree type;
10433 tree attributes;
10434
10435 /* Parse tentatively so that we can back up if we don't find a
10436 enum-specifier. */
10437 cp_parser_parse_tentatively (parser);
10438
10439 /* Caller guarantees that the current token is 'enum', an identifier
10440 possibly follows, and the token after that is an opening brace.
10441 If we don't have an identifier, fabricate an anonymous name for
10442 the enumeration being defined. */
10443 cp_lexer_consume_token (parser->lexer);
10444
10445 attributes = cp_parser_attributes_opt (parser);
10446
10447 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10448 identifier = cp_parser_identifier (parser);
10449 else
10450 identifier = make_anon_name ();
10451
10452 /* Look for the `{' but don't consume it yet. */
10453 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10454 cp_parser_simulate_error (parser);
10455
10456 if (!cp_parser_parse_definitely (parser))
10457 return NULL_TREE;
10458
10459 /* Issue an error message if type-definitions are forbidden here. */
10460 if (!cp_parser_check_type_definition (parser))
10461 type = error_mark_node;
10462 else
10463 /* Create the new type. We do this before consuming the opening
10464 brace so the enum will be recorded as being on the line of its
10465 tag (or the 'enum' keyword, if there is no tag). */
10466 type = start_enum (identifier);
10467
10468 /* Consume the opening brace. */
10469 cp_lexer_consume_token (parser->lexer);
10470
10471 if (type == error_mark_node)
10472 {
10473 cp_parser_skip_to_end_of_block_or_statement (parser);
10474 return error_mark_node;
10475 }
10476
10477 /* If the next token is not '}', then there are some enumerators. */
10478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10479 cp_parser_enumerator_list (parser, type);
10480
10481 /* Consume the final '}'. */
10482 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10483
10484 /* Look for trailing attributes to apply to this enumeration, and
10485 apply them if appropriate. */
10486 if (cp_parser_allow_gnu_extensions_p (parser))
10487 {
10488 tree trailing_attr = cp_parser_attributes_opt (parser);
10489 cplus_decl_attributes (&type,
10490 trailing_attr,
10491 (int) ATTR_FLAG_TYPE_IN_PLACE);
10492 }
10493
10494 /* Finish up the enumeration. */
10495 finish_enum (type);
10496
10497 return type;
10498 }
10499
10500 /* Parse an enumerator-list. The enumerators all have the indicated
10501 TYPE.
10502
10503 enumerator-list:
10504 enumerator-definition
10505 enumerator-list , enumerator-definition */
10506
10507 static void
10508 cp_parser_enumerator_list (cp_parser* parser, tree type)
10509 {
10510 while (true)
10511 {
10512 /* Parse an enumerator-definition. */
10513 cp_parser_enumerator_definition (parser, type);
10514
10515 /* If the next token is not a ',', we've reached the end of
10516 the list. */
10517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10518 break;
10519 /* Otherwise, consume the `,' and keep going. */
10520 cp_lexer_consume_token (parser->lexer);
10521 /* If the next token is a `}', there is a trailing comma. */
10522 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10523 {
10524 if (pedantic && !in_system_header)
10525 pedwarn ("comma at end of enumerator list");
10526 break;
10527 }
10528 }
10529 }
10530
10531 /* Parse an enumerator-definition. The enumerator has the indicated
10532 TYPE.
10533
10534 enumerator-definition:
10535 enumerator
10536 enumerator = constant-expression
10537
10538 enumerator:
10539 identifier */
10540
10541 static void
10542 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10543 {
10544 tree identifier;
10545 tree value;
10546
10547 /* Look for the identifier. */
10548 identifier = cp_parser_identifier (parser);
10549 if (identifier == error_mark_node)
10550 return;
10551
10552 /* If the next token is an '=', then there is an explicit value. */
10553 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10554 {
10555 /* Consume the `=' token. */
10556 cp_lexer_consume_token (parser->lexer);
10557 /* Parse the value. */
10558 value = cp_parser_constant_expression (parser,
10559 /*allow_non_constant_p=*/false,
10560 NULL);
10561 }
10562 else
10563 value = NULL_TREE;
10564
10565 /* Create the enumerator. */
10566 build_enumerator (identifier, value, type);
10567 }
10568
10569 /* Parse a namespace-name.
10570
10571 namespace-name:
10572 original-namespace-name
10573 namespace-alias
10574
10575 Returns the NAMESPACE_DECL for the namespace. */
10576
10577 static tree
10578 cp_parser_namespace_name (cp_parser* parser)
10579 {
10580 tree identifier;
10581 tree namespace_decl;
10582
10583 /* Get the name of the namespace. */
10584 identifier = cp_parser_identifier (parser);
10585 if (identifier == error_mark_node)
10586 return error_mark_node;
10587
10588 /* Look up the identifier in the currently active scope. Look only
10589 for namespaces, due to:
10590
10591 [basic.lookup.udir]
10592
10593 When looking up a namespace-name in a using-directive or alias
10594 definition, only namespace names are considered.
10595
10596 And:
10597
10598 [basic.lookup.qual]
10599
10600 During the lookup of a name preceding the :: scope resolution
10601 operator, object, function, and enumerator names are ignored.
10602
10603 (Note that cp_parser_class_or_namespace_name only calls this
10604 function if the token after the name is the scope resolution
10605 operator.) */
10606 namespace_decl = cp_parser_lookup_name (parser, identifier,
10607 none_type,
10608 /*is_template=*/false,
10609 /*is_namespace=*/true,
10610 /*check_dependency=*/true,
10611 /*ambiguous_decls=*/NULL);
10612 /* If it's not a namespace, issue an error. */
10613 if (namespace_decl == error_mark_node
10614 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10615 {
10616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10617 error ("%qD is not a namespace-name", identifier);
10618 cp_parser_error (parser, "expected namespace-name");
10619 namespace_decl = error_mark_node;
10620 }
10621
10622 return namespace_decl;
10623 }
10624
10625 /* Parse a namespace-definition.
10626
10627 namespace-definition:
10628 named-namespace-definition
10629 unnamed-namespace-definition
10630
10631 named-namespace-definition:
10632 original-namespace-definition
10633 extension-namespace-definition
10634
10635 original-namespace-definition:
10636 namespace identifier { namespace-body }
10637
10638 extension-namespace-definition:
10639 namespace original-namespace-name { namespace-body }
10640
10641 unnamed-namespace-definition:
10642 namespace { namespace-body } */
10643
10644 static void
10645 cp_parser_namespace_definition (cp_parser* parser)
10646 {
10647 tree identifier, attribs;
10648
10649 /* Look for the `namespace' keyword. */
10650 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10651
10652 /* Get the name of the namespace. We do not attempt to distinguish
10653 between an original-namespace-definition and an
10654 extension-namespace-definition at this point. The semantic
10655 analysis routines are responsible for that. */
10656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10657 identifier = cp_parser_identifier (parser);
10658 else
10659 identifier = NULL_TREE;
10660
10661 /* Parse any specified attributes. */
10662 attribs = cp_parser_attributes_opt (parser);
10663
10664 /* Look for the `{' to start the namespace. */
10665 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10666 /* Start the namespace. */
10667 push_namespace_with_attribs (identifier, attribs);
10668 /* Parse the body of the namespace. */
10669 cp_parser_namespace_body (parser);
10670 /* Finish the namespace. */
10671 pop_namespace ();
10672 /* Look for the final `}'. */
10673 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10674 }
10675
10676 /* Parse a namespace-body.
10677
10678 namespace-body:
10679 declaration-seq [opt] */
10680
10681 static void
10682 cp_parser_namespace_body (cp_parser* parser)
10683 {
10684 cp_parser_declaration_seq_opt (parser);
10685 }
10686
10687 /* Parse a namespace-alias-definition.
10688
10689 namespace-alias-definition:
10690 namespace identifier = qualified-namespace-specifier ; */
10691
10692 static void
10693 cp_parser_namespace_alias_definition (cp_parser* parser)
10694 {
10695 tree identifier;
10696 tree namespace_specifier;
10697
10698 /* Look for the `namespace' keyword. */
10699 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10700 /* Look for the identifier. */
10701 identifier = cp_parser_identifier (parser);
10702 if (identifier == error_mark_node)
10703 return;
10704 /* Look for the `=' token. */
10705 cp_parser_require (parser, CPP_EQ, "`='");
10706 /* Look for the qualified-namespace-specifier. */
10707 namespace_specifier
10708 = cp_parser_qualified_namespace_specifier (parser);
10709 /* Look for the `;' token. */
10710 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10711
10712 /* Register the alias in the symbol table. */
10713 do_namespace_alias (identifier, namespace_specifier);
10714 }
10715
10716 /* Parse a qualified-namespace-specifier.
10717
10718 qualified-namespace-specifier:
10719 :: [opt] nested-name-specifier [opt] namespace-name
10720
10721 Returns a NAMESPACE_DECL corresponding to the specified
10722 namespace. */
10723
10724 static tree
10725 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10726 {
10727 /* Look for the optional `::'. */
10728 cp_parser_global_scope_opt (parser,
10729 /*current_scope_valid_p=*/false);
10730
10731 /* Look for the optional nested-name-specifier. */
10732 cp_parser_nested_name_specifier_opt (parser,
10733 /*typename_keyword_p=*/false,
10734 /*check_dependency_p=*/true,
10735 /*type_p=*/false,
10736 /*is_declaration=*/true);
10737
10738 return cp_parser_namespace_name (parser);
10739 }
10740
10741 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10742 access declaration.
10743
10744 using-declaration:
10745 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10746 using :: unqualified-id ;
10747
10748 access-declaration:
10749 qualified-id ;
10750
10751 */
10752
10753 static bool
10754 cp_parser_using_declaration (cp_parser* parser,
10755 bool access_declaration_p)
10756 {
10757 cp_token *token;
10758 bool typename_p = false;
10759 bool global_scope_p;
10760 tree decl;
10761 tree identifier;
10762 tree qscope;
10763
10764 if (access_declaration_p)
10765 cp_parser_parse_tentatively (parser);
10766 else
10767 {
10768 /* Look for the `using' keyword. */
10769 cp_parser_require_keyword (parser, RID_USING, "`using'");
10770
10771 /* Peek at the next token. */
10772 token = cp_lexer_peek_token (parser->lexer);
10773 /* See if it's `typename'. */
10774 if (token->keyword == RID_TYPENAME)
10775 {
10776 /* Remember that we've seen it. */
10777 typename_p = true;
10778 /* Consume the `typename' token. */
10779 cp_lexer_consume_token (parser->lexer);
10780 }
10781 }
10782
10783 /* Look for the optional global scope qualification. */
10784 global_scope_p
10785 = (cp_parser_global_scope_opt (parser,
10786 /*current_scope_valid_p=*/false)
10787 != NULL_TREE);
10788
10789 /* If we saw `typename', or didn't see `::', then there must be a
10790 nested-name-specifier present. */
10791 if (typename_p || !global_scope_p)
10792 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10793 /*check_dependency_p=*/true,
10794 /*type_p=*/false,
10795 /*is_declaration=*/true);
10796 /* Otherwise, we could be in either of the two productions. In that
10797 case, treat the nested-name-specifier as optional. */
10798 else
10799 qscope = cp_parser_nested_name_specifier_opt (parser,
10800 /*typename_keyword_p=*/false,
10801 /*check_dependency_p=*/true,
10802 /*type_p=*/false,
10803 /*is_declaration=*/true);
10804 if (!qscope)
10805 qscope = global_namespace;
10806
10807 if (access_declaration_p && cp_parser_error_occurred (parser))
10808 /* Something has already gone wrong; there's no need to parse
10809 further. Since an error has occurred, the return value of
10810 cp_parser_parse_definitely will be false, as required. */
10811 return cp_parser_parse_definitely (parser);
10812
10813 /* Parse the unqualified-id. */
10814 identifier = cp_parser_unqualified_id (parser,
10815 /*template_keyword_p=*/false,
10816 /*check_dependency_p=*/true,
10817 /*declarator_p=*/true,
10818 /*optional_p=*/false);
10819
10820 if (access_declaration_p)
10821 {
10822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10823 cp_parser_simulate_error (parser);
10824 if (!cp_parser_parse_definitely (parser))
10825 return false;
10826 }
10827
10828 /* The function we call to handle a using-declaration is different
10829 depending on what scope we are in. */
10830 if (qscope == error_mark_node || identifier == error_mark_node)
10831 ;
10832 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10833 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10834 /* [namespace.udecl]
10835
10836 A using declaration shall not name a template-id. */
10837 error ("a template-id may not appear in a using-declaration");
10838 else
10839 {
10840 if (at_class_scope_p ())
10841 {
10842 /* Create the USING_DECL. */
10843 decl = do_class_using_decl (parser->scope, identifier);
10844 /* Add it to the list of members in this class. */
10845 finish_member_declaration (decl);
10846 }
10847 else
10848 {
10849 decl = cp_parser_lookup_name_simple (parser, identifier);
10850 if (decl == error_mark_node)
10851 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10852 else if (!at_namespace_scope_p ())
10853 do_local_using_decl (decl, qscope, identifier);
10854 else
10855 do_toplevel_using_decl (decl, qscope, identifier);
10856 }
10857 }
10858
10859 /* Look for the final `;'. */
10860 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10861
10862 return true;
10863 }
10864
10865 /* Parse a using-directive.
10866
10867 using-directive:
10868 using namespace :: [opt] nested-name-specifier [opt]
10869 namespace-name ; */
10870
10871 static void
10872 cp_parser_using_directive (cp_parser* parser)
10873 {
10874 tree namespace_decl;
10875 tree attribs;
10876
10877 /* Look for the `using' keyword. */
10878 cp_parser_require_keyword (parser, RID_USING, "`using'");
10879 /* And the `namespace' keyword. */
10880 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10881 /* Look for the optional `::' operator. */
10882 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10883 /* And the optional nested-name-specifier. */
10884 cp_parser_nested_name_specifier_opt (parser,
10885 /*typename_keyword_p=*/false,
10886 /*check_dependency_p=*/true,
10887 /*type_p=*/false,
10888 /*is_declaration=*/true);
10889 /* Get the namespace being used. */
10890 namespace_decl = cp_parser_namespace_name (parser);
10891 /* And any specified attributes. */
10892 attribs = cp_parser_attributes_opt (parser);
10893 /* Update the symbol table. */
10894 parse_using_directive (namespace_decl, attribs);
10895 /* Look for the final `;'. */
10896 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10897 }
10898
10899 /* Parse an asm-definition.
10900
10901 asm-definition:
10902 asm ( string-literal ) ;
10903
10904 GNU Extension:
10905
10906 asm-definition:
10907 asm volatile [opt] ( string-literal ) ;
10908 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10909 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10910 : asm-operand-list [opt] ) ;
10911 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10912 : asm-operand-list [opt]
10913 : asm-operand-list [opt] ) ; */
10914
10915 static void
10916 cp_parser_asm_definition (cp_parser* parser)
10917 {
10918 tree string;
10919 tree outputs = NULL_TREE;
10920 tree inputs = NULL_TREE;
10921 tree clobbers = NULL_TREE;
10922 tree asm_stmt;
10923 bool volatile_p = false;
10924 bool extended_p = false;
10925
10926 /* Look for the `asm' keyword. */
10927 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10928 /* See if the next token is `volatile'. */
10929 if (cp_parser_allow_gnu_extensions_p (parser)
10930 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10931 {
10932 /* Remember that we saw the `volatile' keyword. */
10933 volatile_p = true;
10934 /* Consume the token. */
10935 cp_lexer_consume_token (parser->lexer);
10936 }
10937 /* Look for the opening `('. */
10938 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10939 return;
10940 /* Look for the string. */
10941 string = cp_parser_string_literal (parser, false, false);
10942 if (string == error_mark_node)
10943 {
10944 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10945 /*consume_paren=*/true);
10946 return;
10947 }
10948
10949 /* If we're allowing GNU extensions, check for the extended assembly
10950 syntax. Unfortunately, the `:' tokens need not be separated by
10951 a space in C, and so, for compatibility, we tolerate that here
10952 too. Doing that means that we have to treat the `::' operator as
10953 two `:' tokens. */
10954 if (cp_parser_allow_gnu_extensions_p (parser)
10955 && at_function_scope_p ()
10956 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10957 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10958 {
10959 bool inputs_p = false;
10960 bool clobbers_p = false;
10961
10962 /* The extended syntax was used. */
10963 extended_p = true;
10964
10965 /* Look for outputs. */
10966 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10967 {
10968 /* Consume the `:'. */
10969 cp_lexer_consume_token (parser->lexer);
10970 /* Parse the output-operands. */
10971 if (cp_lexer_next_token_is_not (parser->lexer,
10972 CPP_COLON)
10973 && cp_lexer_next_token_is_not (parser->lexer,
10974 CPP_SCOPE)
10975 && cp_lexer_next_token_is_not (parser->lexer,
10976 CPP_CLOSE_PAREN))
10977 outputs = cp_parser_asm_operand_list (parser);
10978 }
10979 /* If the next token is `::', there are no outputs, and the
10980 next token is the beginning of the inputs. */
10981 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10982 /* The inputs are coming next. */
10983 inputs_p = true;
10984
10985 /* Look for inputs. */
10986 if (inputs_p
10987 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10988 {
10989 /* Consume the `:' or `::'. */
10990 cp_lexer_consume_token (parser->lexer);
10991 /* Parse the output-operands. */
10992 if (cp_lexer_next_token_is_not (parser->lexer,
10993 CPP_COLON)
10994 && cp_lexer_next_token_is_not (parser->lexer,
10995 CPP_CLOSE_PAREN))
10996 inputs = cp_parser_asm_operand_list (parser);
10997 }
10998 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10999 /* The clobbers are coming next. */
11000 clobbers_p = true;
11001
11002 /* Look for clobbers. */
11003 if (clobbers_p
11004 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11005 {
11006 /* Consume the `:' or `::'. */
11007 cp_lexer_consume_token (parser->lexer);
11008 /* Parse the clobbers. */
11009 if (cp_lexer_next_token_is_not (parser->lexer,
11010 CPP_CLOSE_PAREN))
11011 clobbers = cp_parser_asm_clobber_list (parser);
11012 }
11013 }
11014 /* Look for the closing `)'. */
11015 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11016 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11017 /*consume_paren=*/true);
11018 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11019
11020 /* Create the ASM_EXPR. */
11021 if (at_function_scope_p ())
11022 {
11023 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11024 inputs, clobbers);
11025 /* If the extended syntax was not used, mark the ASM_EXPR. */
11026 if (!extended_p)
11027 {
11028 tree temp = asm_stmt;
11029 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11030 temp = TREE_OPERAND (temp, 0);
11031
11032 ASM_INPUT_P (temp) = 1;
11033 }
11034 }
11035 else
11036 cgraph_add_asm_node (string);
11037 }
11038
11039 /* Declarators [gram.dcl.decl] */
11040
11041 /* Parse an init-declarator.
11042
11043 init-declarator:
11044 declarator initializer [opt]
11045
11046 GNU Extension:
11047
11048 init-declarator:
11049 declarator asm-specification [opt] attributes [opt] initializer [opt]
11050
11051 function-definition:
11052 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11053 function-body
11054 decl-specifier-seq [opt] declarator function-try-block
11055
11056 GNU Extension:
11057
11058 function-definition:
11059 __extension__ function-definition
11060
11061 The DECL_SPECIFIERS apply to this declarator. Returns a
11062 representation of the entity declared. If MEMBER_P is TRUE, then
11063 this declarator appears in a class scope. The new DECL created by
11064 this declarator is returned.
11065
11066 The CHECKS are access checks that should be performed once we know
11067 what entity is being declared (and, therefore, what classes have
11068 befriended it).
11069
11070 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11071 for a function-definition here as well. If the declarator is a
11072 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11073 be TRUE upon return. By that point, the function-definition will
11074 have been completely parsed.
11075
11076 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11077 is FALSE. */
11078
11079 static tree
11080 cp_parser_init_declarator (cp_parser* parser,
11081 cp_decl_specifier_seq *decl_specifiers,
11082 tree checks,
11083 bool function_definition_allowed_p,
11084 bool member_p,
11085 int declares_class_or_enum,
11086 bool* function_definition_p)
11087 {
11088 cp_token *token;
11089 cp_declarator *declarator;
11090 tree prefix_attributes;
11091 tree attributes;
11092 tree asm_specification;
11093 tree initializer;
11094 tree decl = NULL_TREE;
11095 tree scope;
11096 bool is_initialized;
11097 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11098 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11099 "(...)". */
11100 enum cpp_ttype initialization_kind;
11101 bool is_parenthesized_init = false;
11102 bool is_non_constant_init;
11103 int ctor_dtor_or_conv_p;
11104 bool friend_p;
11105 tree pushed_scope = NULL;
11106
11107 /* Gather the attributes that were provided with the
11108 decl-specifiers. */
11109 prefix_attributes = decl_specifiers->attributes;
11110
11111 /* Assume that this is not the declarator for a function
11112 definition. */
11113 if (function_definition_p)
11114 *function_definition_p = false;
11115
11116 /* Defer access checks while parsing the declarator; we cannot know
11117 what names are accessible until we know what is being
11118 declared. */
11119 resume_deferring_access_checks ();
11120
11121 /* Parse the declarator. */
11122 declarator
11123 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11124 &ctor_dtor_or_conv_p,
11125 /*parenthesized_p=*/NULL,
11126 /*member_p=*/false);
11127 /* Gather up the deferred checks. */
11128 stop_deferring_access_checks ();
11129
11130 /* If the DECLARATOR was erroneous, there's no need to go
11131 further. */
11132 if (declarator == cp_error_declarator)
11133 return error_mark_node;
11134
11135 if (declares_class_or_enum & 2)
11136 cp_parser_check_for_definition_in_return_type (declarator,
11137 decl_specifiers->type);
11138
11139 /* Figure out what scope the entity declared by the DECLARATOR is
11140 located in. `grokdeclarator' sometimes changes the scope, so
11141 we compute it now. */
11142 scope = get_scope_of_declarator (declarator);
11143
11144 /* If we're allowing GNU extensions, look for an asm-specification
11145 and attributes. */
11146 if (cp_parser_allow_gnu_extensions_p (parser))
11147 {
11148 /* Look for an asm-specification. */
11149 asm_specification = cp_parser_asm_specification_opt (parser);
11150 /* And attributes. */
11151 attributes = cp_parser_attributes_opt (parser);
11152 }
11153 else
11154 {
11155 asm_specification = NULL_TREE;
11156 attributes = NULL_TREE;
11157 }
11158
11159 /* Peek at the next token. */
11160 token = cp_lexer_peek_token (parser->lexer);
11161 /* Check to see if the token indicates the start of a
11162 function-definition. */
11163 if (cp_parser_token_starts_function_definition_p (token))
11164 {
11165 if (!function_definition_allowed_p)
11166 {
11167 /* If a function-definition should not appear here, issue an
11168 error message. */
11169 cp_parser_error (parser,
11170 "a function-definition is not allowed here");
11171 return error_mark_node;
11172 }
11173 else
11174 {
11175 /* Neither attributes nor an asm-specification are allowed
11176 on a function-definition. */
11177 if (asm_specification)
11178 error ("an asm-specification is not allowed on a function-definition");
11179 if (attributes)
11180 error ("attributes are not allowed on a function-definition");
11181 /* This is a function-definition. */
11182 *function_definition_p = true;
11183
11184 /* Parse the function definition. */
11185 if (member_p)
11186 decl = cp_parser_save_member_function_body (parser,
11187 decl_specifiers,
11188 declarator,
11189 prefix_attributes);
11190 else
11191 decl
11192 = (cp_parser_function_definition_from_specifiers_and_declarator
11193 (parser, decl_specifiers, prefix_attributes, declarator));
11194
11195 return decl;
11196 }
11197 }
11198
11199 /* [dcl.dcl]
11200
11201 Only in function declarations for constructors, destructors, and
11202 type conversions can the decl-specifier-seq be omitted.
11203
11204 We explicitly postpone this check past the point where we handle
11205 function-definitions because we tolerate function-definitions
11206 that are missing their return types in some modes. */
11207 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11208 {
11209 cp_parser_error (parser,
11210 "expected constructor, destructor, or type conversion");
11211 return error_mark_node;
11212 }
11213
11214 /* An `=' or an `(' indicates an initializer. */
11215 if (token->type == CPP_EQ
11216 || token->type == CPP_OPEN_PAREN)
11217 {
11218 is_initialized = true;
11219 initialization_kind = token->type;
11220 }
11221 else
11222 {
11223 /* If the init-declarator isn't initialized and isn't followed by a
11224 `,' or `;', it's not a valid init-declarator. */
11225 if (token->type != CPP_COMMA
11226 && token->type != CPP_SEMICOLON)
11227 {
11228 cp_parser_error (parser, "expected initializer");
11229 return error_mark_node;
11230 }
11231 is_initialized = false;
11232 initialization_kind = CPP_EOF;
11233 }
11234
11235 /* Because start_decl has side-effects, we should only call it if we
11236 know we're going ahead. By this point, we know that we cannot
11237 possibly be looking at any other construct. */
11238 cp_parser_commit_to_tentative_parse (parser);
11239
11240 /* If the decl specifiers were bad, issue an error now that we're
11241 sure this was intended to be a declarator. Then continue
11242 declaring the variable(s), as int, to try to cut down on further
11243 errors. */
11244 if (decl_specifiers->any_specifiers_p
11245 && decl_specifiers->type == error_mark_node)
11246 {
11247 cp_parser_error (parser, "invalid type in declaration");
11248 decl_specifiers->type = integer_type_node;
11249 }
11250
11251 /* Check to see whether or not this declaration is a friend. */
11252 friend_p = cp_parser_friend_p (decl_specifiers);
11253
11254 /* Check that the number of template-parameter-lists is OK. */
11255 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11256 return error_mark_node;
11257
11258 /* Enter the newly declared entry in the symbol table. If we're
11259 processing a declaration in a class-specifier, we wait until
11260 after processing the initializer. */
11261 if (!member_p)
11262 {
11263 if (parser->in_unbraced_linkage_specification_p)
11264 decl_specifiers->storage_class = sc_extern;
11265 decl = start_decl (declarator, decl_specifiers,
11266 is_initialized, attributes, prefix_attributes,
11267 &pushed_scope);
11268 }
11269 else if (scope)
11270 /* Enter the SCOPE. That way unqualified names appearing in the
11271 initializer will be looked up in SCOPE. */
11272 pushed_scope = push_scope (scope);
11273
11274 /* Perform deferred access control checks, now that we know in which
11275 SCOPE the declared entity resides. */
11276 if (!member_p && decl)
11277 {
11278 tree saved_current_function_decl = NULL_TREE;
11279
11280 /* If the entity being declared is a function, pretend that we
11281 are in its scope. If it is a `friend', it may have access to
11282 things that would not otherwise be accessible. */
11283 if (TREE_CODE (decl) == FUNCTION_DECL)
11284 {
11285 saved_current_function_decl = current_function_decl;
11286 current_function_decl = decl;
11287 }
11288
11289 /* Perform access checks for template parameters. */
11290 cp_parser_perform_template_parameter_access_checks (checks);
11291
11292 /* Perform the access control checks for the declarator and the
11293 the decl-specifiers. */
11294 perform_deferred_access_checks ();
11295
11296 /* Restore the saved value. */
11297 if (TREE_CODE (decl) == FUNCTION_DECL)
11298 current_function_decl = saved_current_function_decl;
11299 }
11300
11301 /* Parse the initializer. */
11302 initializer = NULL_TREE;
11303 is_parenthesized_init = false;
11304 is_non_constant_init = true;
11305 if (is_initialized)
11306 {
11307 if (function_declarator_p (declarator)
11308 && initialization_kind == CPP_EQ)
11309 initializer = cp_parser_pure_specifier (parser);
11310 else
11311 initializer = cp_parser_initializer (parser,
11312 &is_parenthesized_init,
11313 &is_non_constant_init);
11314 }
11315
11316 /* The old parser allows attributes to appear after a parenthesized
11317 initializer. Mark Mitchell proposed removing this functionality
11318 on the GCC mailing lists on 2002-08-13. This parser accepts the
11319 attributes -- but ignores them. */
11320 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11321 if (cp_parser_attributes_opt (parser))
11322 warning (OPT_Wattributes,
11323 "attributes after parenthesized initializer ignored");
11324
11325 /* For an in-class declaration, use `grokfield' to create the
11326 declaration. */
11327 if (member_p)
11328 {
11329 if (pushed_scope)
11330 {
11331 pop_scope (pushed_scope);
11332 pushed_scope = false;
11333 }
11334 decl = grokfield (declarator, decl_specifiers,
11335 initializer, !is_non_constant_init,
11336 /*asmspec=*/NULL_TREE,
11337 prefix_attributes);
11338 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11339 cp_parser_save_default_args (parser, decl);
11340 }
11341
11342 /* Finish processing the declaration. But, skip friend
11343 declarations. */
11344 if (!friend_p && decl && decl != error_mark_node)
11345 {
11346 cp_finish_decl (decl,
11347 initializer, !is_non_constant_init,
11348 asm_specification,
11349 /* If the initializer is in parentheses, then this is
11350 a direct-initialization, which means that an
11351 `explicit' constructor is OK. Otherwise, an
11352 `explicit' constructor cannot be used. */
11353 ((is_parenthesized_init || !is_initialized)
11354 ? 0 : LOOKUP_ONLYCONVERTING));
11355 }
11356 if (!friend_p && pushed_scope)
11357 pop_scope (pushed_scope);
11358
11359 return decl;
11360 }
11361
11362 /* Parse a declarator.
11363
11364 declarator:
11365 direct-declarator
11366 ptr-operator declarator
11367
11368 abstract-declarator:
11369 ptr-operator abstract-declarator [opt]
11370 direct-abstract-declarator
11371
11372 GNU Extensions:
11373
11374 declarator:
11375 attributes [opt] direct-declarator
11376 attributes [opt] ptr-operator declarator
11377
11378 abstract-declarator:
11379 attributes [opt] ptr-operator abstract-declarator [opt]
11380 attributes [opt] direct-abstract-declarator
11381
11382 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11383 detect constructor, destructor or conversion operators. It is set
11384 to -1 if the declarator is a name, and +1 if it is a
11385 function. Otherwise it is set to zero. Usually you just want to
11386 test for >0, but internally the negative value is used.
11387
11388 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11389 a decl-specifier-seq unless it declares a constructor, destructor,
11390 or conversion. It might seem that we could check this condition in
11391 semantic analysis, rather than parsing, but that makes it difficult
11392 to handle something like `f()'. We want to notice that there are
11393 no decl-specifiers, and therefore realize that this is an
11394 expression, not a declaration.)
11395
11396 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11397 the declarator is a direct-declarator of the form "(...)".
11398
11399 MEMBER_P is true iff this declarator is a member-declarator. */
11400
11401 static cp_declarator *
11402 cp_parser_declarator (cp_parser* parser,
11403 cp_parser_declarator_kind dcl_kind,
11404 int* ctor_dtor_or_conv_p,
11405 bool* parenthesized_p,
11406 bool member_p)
11407 {
11408 cp_token *token;
11409 cp_declarator *declarator;
11410 enum tree_code code;
11411 cp_cv_quals cv_quals;
11412 tree class_type;
11413 tree attributes = NULL_TREE;
11414
11415 /* Assume this is not a constructor, destructor, or type-conversion
11416 operator. */
11417 if (ctor_dtor_or_conv_p)
11418 *ctor_dtor_or_conv_p = 0;
11419
11420 if (cp_parser_allow_gnu_extensions_p (parser))
11421 attributes = cp_parser_attributes_opt (parser);
11422
11423 /* Peek at the next token. */
11424 token = cp_lexer_peek_token (parser->lexer);
11425
11426 /* Check for the ptr-operator production. */
11427 cp_parser_parse_tentatively (parser);
11428 /* Parse the ptr-operator. */
11429 code = cp_parser_ptr_operator (parser,
11430 &class_type,
11431 &cv_quals);
11432 /* If that worked, then we have a ptr-operator. */
11433 if (cp_parser_parse_definitely (parser))
11434 {
11435 /* If a ptr-operator was found, then this declarator was not
11436 parenthesized. */
11437 if (parenthesized_p)
11438 *parenthesized_p = true;
11439 /* The dependent declarator is optional if we are parsing an
11440 abstract-declarator. */
11441 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11442 cp_parser_parse_tentatively (parser);
11443
11444 /* Parse the dependent declarator. */
11445 declarator = cp_parser_declarator (parser, dcl_kind,
11446 /*ctor_dtor_or_conv_p=*/NULL,
11447 /*parenthesized_p=*/NULL,
11448 /*member_p=*/false);
11449
11450 /* If we are parsing an abstract-declarator, we must handle the
11451 case where the dependent declarator is absent. */
11452 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11453 && !cp_parser_parse_definitely (parser))
11454 declarator = NULL;
11455
11456 /* Build the representation of the ptr-operator. */
11457 if (class_type)
11458 declarator = make_ptrmem_declarator (cv_quals,
11459 class_type,
11460 declarator);
11461 else if (code == INDIRECT_REF)
11462 declarator = make_pointer_declarator (cv_quals, declarator);
11463 else
11464 declarator = make_reference_declarator (cv_quals, declarator);
11465 }
11466 /* Everything else is a direct-declarator. */
11467 else
11468 {
11469 if (parenthesized_p)
11470 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11471 CPP_OPEN_PAREN);
11472 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11473 ctor_dtor_or_conv_p,
11474 member_p);
11475 }
11476
11477 if (attributes && declarator && declarator != cp_error_declarator)
11478 declarator->attributes = attributes;
11479
11480 return declarator;
11481 }
11482
11483 /* Parse a direct-declarator or direct-abstract-declarator.
11484
11485 direct-declarator:
11486 declarator-id
11487 direct-declarator ( parameter-declaration-clause )
11488 cv-qualifier-seq [opt]
11489 exception-specification [opt]
11490 direct-declarator [ constant-expression [opt] ]
11491 ( declarator )
11492
11493 direct-abstract-declarator:
11494 direct-abstract-declarator [opt]
11495 ( parameter-declaration-clause )
11496 cv-qualifier-seq [opt]
11497 exception-specification [opt]
11498 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11499 ( abstract-declarator )
11500
11501 Returns a representation of the declarator. DCL_KIND is
11502 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11503 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11504 we are parsing a direct-declarator. It is
11505 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11506 of ambiguity we prefer an abstract declarator, as per
11507 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11508 cp_parser_declarator. */
11509
11510 static cp_declarator *
11511 cp_parser_direct_declarator (cp_parser* parser,
11512 cp_parser_declarator_kind dcl_kind,
11513 int* ctor_dtor_or_conv_p,
11514 bool member_p)
11515 {
11516 cp_token *token;
11517 cp_declarator *declarator = NULL;
11518 tree scope = NULL_TREE;
11519 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11520 bool saved_in_declarator_p = parser->in_declarator_p;
11521 bool first = true;
11522 tree pushed_scope = NULL_TREE;
11523
11524 while (true)
11525 {
11526 /* Peek at the next token. */
11527 token = cp_lexer_peek_token (parser->lexer);
11528 if (token->type == CPP_OPEN_PAREN)
11529 {
11530 /* This is either a parameter-declaration-clause, or a
11531 parenthesized declarator. When we know we are parsing a
11532 named declarator, it must be a parenthesized declarator
11533 if FIRST is true. For instance, `(int)' is a
11534 parameter-declaration-clause, with an omitted
11535 direct-abstract-declarator. But `((*))', is a
11536 parenthesized abstract declarator. Finally, when T is a
11537 template parameter `(T)' is a
11538 parameter-declaration-clause, and not a parenthesized
11539 named declarator.
11540
11541 We first try and parse a parameter-declaration-clause,
11542 and then try a nested declarator (if FIRST is true).
11543
11544 It is not an error for it not to be a
11545 parameter-declaration-clause, even when FIRST is
11546 false. Consider,
11547
11548 int i (int);
11549 int i (3);
11550
11551 The first is the declaration of a function while the
11552 second is a the definition of a variable, including its
11553 initializer.
11554
11555 Having seen only the parenthesis, we cannot know which of
11556 these two alternatives should be selected. Even more
11557 complex are examples like:
11558
11559 int i (int (a));
11560 int i (int (3));
11561
11562 The former is a function-declaration; the latter is a
11563 variable initialization.
11564
11565 Thus again, we try a parameter-declaration-clause, and if
11566 that fails, we back out and return. */
11567
11568 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11569 {
11570 cp_parameter_declarator *params;
11571 unsigned saved_num_template_parameter_lists;
11572
11573 /* In a member-declarator, the only valid interpretation
11574 of a parenthesis is the start of a
11575 parameter-declaration-clause. (It is invalid to
11576 initialize a static data member with a parenthesized
11577 initializer; only the "=" form of initialization is
11578 permitted.) */
11579 if (!member_p)
11580 cp_parser_parse_tentatively (parser);
11581
11582 /* Consume the `('. */
11583 cp_lexer_consume_token (parser->lexer);
11584 if (first)
11585 {
11586 /* If this is going to be an abstract declarator, we're
11587 in a declarator and we can't have default args. */
11588 parser->default_arg_ok_p = false;
11589 parser->in_declarator_p = true;
11590 }
11591
11592 /* Inside the function parameter list, surrounding
11593 template-parameter-lists do not apply. */
11594 saved_num_template_parameter_lists
11595 = parser->num_template_parameter_lists;
11596 parser->num_template_parameter_lists = 0;
11597
11598 /* Parse the parameter-declaration-clause. */
11599 params = cp_parser_parameter_declaration_clause (parser);
11600
11601 parser->num_template_parameter_lists
11602 = saved_num_template_parameter_lists;
11603
11604 /* If all went well, parse the cv-qualifier-seq and the
11605 exception-specification. */
11606 if (member_p || cp_parser_parse_definitely (parser))
11607 {
11608 cp_cv_quals cv_quals;
11609 tree exception_specification;
11610
11611 if (ctor_dtor_or_conv_p)
11612 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11613 first = false;
11614 /* Consume the `)'. */
11615 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11616
11617 /* Parse the cv-qualifier-seq. */
11618 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11619 /* And the exception-specification. */
11620 exception_specification
11621 = cp_parser_exception_specification_opt (parser);
11622
11623 /* Create the function-declarator. */
11624 declarator = make_call_declarator (declarator,
11625 params,
11626 cv_quals,
11627 exception_specification);
11628 /* Any subsequent parameter lists are to do with
11629 return type, so are not those of the declared
11630 function. */
11631 parser->default_arg_ok_p = false;
11632
11633 /* Repeat the main loop. */
11634 continue;
11635 }
11636 }
11637
11638 /* If this is the first, we can try a parenthesized
11639 declarator. */
11640 if (first)
11641 {
11642 bool saved_in_type_id_in_expr_p;
11643
11644 parser->default_arg_ok_p = saved_default_arg_ok_p;
11645 parser->in_declarator_p = saved_in_declarator_p;
11646
11647 /* Consume the `('. */
11648 cp_lexer_consume_token (parser->lexer);
11649 /* Parse the nested declarator. */
11650 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11651 parser->in_type_id_in_expr_p = true;
11652 declarator
11653 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11654 /*parenthesized_p=*/NULL,
11655 member_p);
11656 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11657 first = false;
11658 /* Expect a `)'. */
11659 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11660 declarator = cp_error_declarator;
11661 if (declarator == cp_error_declarator)
11662 break;
11663
11664 goto handle_declarator;
11665 }
11666 /* Otherwise, we must be done. */
11667 else
11668 break;
11669 }
11670 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11671 && token->type == CPP_OPEN_SQUARE)
11672 {
11673 /* Parse an array-declarator. */
11674 tree bounds;
11675
11676 if (ctor_dtor_or_conv_p)
11677 *ctor_dtor_or_conv_p = 0;
11678
11679 first = false;
11680 parser->default_arg_ok_p = false;
11681 parser->in_declarator_p = true;
11682 /* Consume the `['. */
11683 cp_lexer_consume_token (parser->lexer);
11684 /* Peek at the next token. */
11685 token = cp_lexer_peek_token (parser->lexer);
11686 /* If the next token is `]', then there is no
11687 constant-expression. */
11688 if (token->type != CPP_CLOSE_SQUARE)
11689 {
11690 bool non_constant_p;
11691
11692 bounds
11693 = cp_parser_constant_expression (parser,
11694 /*allow_non_constant=*/true,
11695 &non_constant_p);
11696 if (!non_constant_p)
11697 bounds = fold_non_dependent_expr (bounds);
11698 /* Normally, the array bound must be an integral constant
11699 expression. However, as an extension, we allow VLAs
11700 in function scopes. */
11701 else if (!at_function_scope_p ())
11702 {
11703 error ("array bound is not an integer constant");
11704 bounds = error_mark_node;
11705 }
11706 }
11707 else
11708 bounds = NULL_TREE;
11709 /* Look for the closing `]'. */
11710 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11711 {
11712 declarator = cp_error_declarator;
11713 break;
11714 }
11715
11716 declarator = make_array_declarator (declarator, bounds);
11717 }
11718 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11719 {
11720 tree qualifying_scope;
11721 tree unqualified_name;
11722 special_function_kind sfk;
11723 bool abstract_ok;
11724
11725 /* Parse a declarator-id */
11726 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11727 if (abstract_ok)
11728 cp_parser_parse_tentatively (parser);
11729 unqualified_name
11730 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11731 qualifying_scope = parser->scope;
11732 if (abstract_ok)
11733 {
11734 if (!cp_parser_parse_definitely (parser))
11735 unqualified_name = error_mark_node;
11736 else if (unqualified_name
11737 && (qualifying_scope
11738 || (TREE_CODE (unqualified_name)
11739 != IDENTIFIER_NODE)))
11740 {
11741 cp_parser_error (parser, "expected unqualified-id");
11742 unqualified_name = error_mark_node;
11743 }
11744 }
11745
11746 if (!unqualified_name)
11747 return NULL;
11748 if (unqualified_name == error_mark_node)
11749 {
11750 declarator = cp_error_declarator;
11751 break;
11752 }
11753
11754 if (qualifying_scope && at_namespace_scope_p ()
11755 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11756 {
11757 /* In the declaration of a member of a template class
11758 outside of the class itself, the SCOPE will sometimes
11759 be a TYPENAME_TYPE. For example, given:
11760
11761 template <typename T>
11762 int S<T>::R::i = 3;
11763
11764 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11765 this context, we must resolve S<T>::R to an ordinary
11766 type, rather than a typename type.
11767
11768 The reason we normally avoid resolving TYPENAME_TYPEs
11769 is that a specialization of `S' might render
11770 `S<T>::R' not a type. However, if `S' is
11771 specialized, then this `i' will not be used, so there
11772 is no harm in resolving the types here. */
11773 tree type;
11774
11775 /* Resolve the TYPENAME_TYPE. */
11776 type = resolve_typename_type (qualifying_scope,
11777 /*only_current_p=*/false);
11778 /* If that failed, the declarator is invalid. */
11779 if (type == error_mark_node)
11780 error ("%<%T::%D%> is not a type",
11781 TYPE_CONTEXT (qualifying_scope),
11782 TYPE_IDENTIFIER (qualifying_scope));
11783 qualifying_scope = type;
11784 }
11785
11786 sfk = sfk_none;
11787 if (unqualified_name)
11788 {
11789 tree class_type;
11790
11791 if (qualifying_scope
11792 && CLASS_TYPE_P (qualifying_scope))
11793 class_type = qualifying_scope;
11794 else
11795 class_type = current_class_type;
11796
11797 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11798 {
11799 tree name_type = TREE_TYPE (unqualified_name);
11800 if (class_type && same_type_p (name_type, class_type))
11801 {
11802 if (qualifying_scope
11803 && CLASSTYPE_USE_TEMPLATE (name_type))
11804 {
11805 error ("invalid use of constructor as a template");
11806 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11807 "name the constructor in a qualified name",
11808 class_type,
11809 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11810 class_type, name_type);
11811 declarator = cp_error_declarator;
11812 break;
11813 }
11814 else
11815 unqualified_name = constructor_name (class_type);
11816 }
11817 else
11818 {
11819 /* We do not attempt to print the declarator
11820 here because we do not have enough
11821 information about its original syntactic
11822 form. */
11823 cp_parser_error (parser, "invalid declarator");
11824 declarator = cp_error_declarator;
11825 break;
11826 }
11827 }
11828
11829 if (class_type)
11830 {
11831 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11832 sfk = sfk_destructor;
11833 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11834 sfk = sfk_conversion;
11835 else if (/* There's no way to declare a constructor
11836 for an anonymous type, even if the type
11837 got a name for linkage purposes. */
11838 !TYPE_WAS_ANONYMOUS (class_type)
11839 && constructor_name_p (unqualified_name,
11840 class_type))
11841 {
11842 unqualified_name = constructor_name (class_type);
11843 sfk = sfk_constructor;
11844 }
11845
11846 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11847 *ctor_dtor_or_conv_p = -1;
11848 }
11849 }
11850 declarator = make_id_declarator (qualifying_scope,
11851 unqualified_name,
11852 sfk);
11853 declarator->id_loc = token->location;
11854
11855 handle_declarator:;
11856 scope = get_scope_of_declarator (declarator);
11857 if (scope)
11858 /* Any names that appear after the declarator-id for a
11859 member are looked up in the containing scope. */
11860 pushed_scope = push_scope (scope);
11861 parser->in_declarator_p = true;
11862 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11863 || (declarator && declarator->kind == cdk_id))
11864 /* Default args are only allowed on function
11865 declarations. */
11866 parser->default_arg_ok_p = saved_default_arg_ok_p;
11867 else
11868 parser->default_arg_ok_p = false;
11869
11870 first = false;
11871 }
11872 /* We're done. */
11873 else
11874 break;
11875 }
11876
11877 /* For an abstract declarator, we might wind up with nothing at this
11878 point. That's an error; the declarator is not optional. */
11879 if (!declarator)
11880 cp_parser_error (parser, "expected declarator");
11881
11882 /* If we entered a scope, we must exit it now. */
11883 if (pushed_scope)
11884 pop_scope (pushed_scope);
11885
11886 parser->default_arg_ok_p = saved_default_arg_ok_p;
11887 parser->in_declarator_p = saved_in_declarator_p;
11888
11889 return declarator;
11890 }
11891
11892 /* Parse a ptr-operator.
11893
11894 ptr-operator:
11895 * cv-qualifier-seq [opt]
11896 &
11897 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11898
11899 GNU Extension:
11900
11901 ptr-operator:
11902 & cv-qualifier-seq [opt]
11903
11904 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11905 Returns ADDR_EXPR if a reference was used. In the case of a
11906 pointer-to-member, *TYPE is filled in with the TYPE containing the
11907 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11908 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11909 ERROR_MARK if an error occurred. */
11910
11911 static enum tree_code
11912 cp_parser_ptr_operator (cp_parser* parser,
11913 tree* type,
11914 cp_cv_quals *cv_quals)
11915 {
11916 enum tree_code code = ERROR_MARK;
11917 cp_token *token;
11918
11919 /* Assume that it's not a pointer-to-member. */
11920 *type = NULL_TREE;
11921 /* And that there are no cv-qualifiers. */
11922 *cv_quals = TYPE_UNQUALIFIED;
11923
11924 /* Peek at the next token. */
11925 token = cp_lexer_peek_token (parser->lexer);
11926 /* If it's a `*' or `&' we have a pointer or reference. */
11927 if (token->type == CPP_MULT || token->type == CPP_AND)
11928 {
11929 /* Remember which ptr-operator we were processing. */
11930 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11931
11932 /* Consume the `*' or `&'. */
11933 cp_lexer_consume_token (parser->lexer);
11934
11935 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11936 `&', if we are allowing GNU extensions. (The only qualifier
11937 that can legally appear after `&' is `restrict', but that is
11938 enforced during semantic analysis. */
11939 if (code == INDIRECT_REF
11940 || cp_parser_allow_gnu_extensions_p (parser))
11941 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11942 }
11943 else
11944 {
11945 /* Try the pointer-to-member case. */
11946 cp_parser_parse_tentatively (parser);
11947 /* Look for the optional `::' operator. */
11948 cp_parser_global_scope_opt (parser,
11949 /*current_scope_valid_p=*/false);
11950 /* Look for the nested-name specifier. */
11951 cp_parser_nested_name_specifier (parser,
11952 /*typename_keyword_p=*/false,
11953 /*check_dependency_p=*/true,
11954 /*type_p=*/false,
11955 /*is_declaration=*/false);
11956 /* If we found it, and the next token is a `*', then we are
11957 indeed looking at a pointer-to-member operator. */
11958 if (!cp_parser_error_occurred (parser)
11959 && cp_parser_require (parser, CPP_MULT, "`*'"))
11960 {
11961 /* Indicate that the `*' operator was used. */
11962 code = INDIRECT_REF;
11963
11964 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11965 error ("%qD is a namespace", parser->scope);
11966 else
11967 {
11968 /* The type of which the member is a member is given by the
11969 current SCOPE. */
11970 *type = parser->scope;
11971 /* The next name will not be qualified. */
11972 parser->scope = NULL_TREE;
11973 parser->qualifying_scope = NULL_TREE;
11974 parser->object_scope = NULL_TREE;
11975 /* Look for the optional cv-qualifier-seq. */
11976 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11977 }
11978 }
11979 /* If that didn't work we don't have a ptr-operator. */
11980 if (!cp_parser_parse_definitely (parser))
11981 cp_parser_error (parser, "expected ptr-operator");
11982 }
11983
11984 return code;
11985 }
11986
11987 /* Parse an (optional) cv-qualifier-seq.
11988
11989 cv-qualifier-seq:
11990 cv-qualifier cv-qualifier-seq [opt]
11991
11992 cv-qualifier:
11993 const
11994 volatile
11995
11996 GNU Extension:
11997
11998 cv-qualifier:
11999 __restrict__
12000
12001 Returns a bitmask representing the cv-qualifiers. */
12002
12003 static cp_cv_quals
12004 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12005 {
12006 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12007
12008 while (true)
12009 {
12010 cp_token *token;
12011 cp_cv_quals cv_qualifier;
12012
12013 /* Peek at the next token. */
12014 token = cp_lexer_peek_token (parser->lexer);
12015 /* See if it's a cv-qualifier. */
12016 switch (token->keyword)
12017 {
12018 case RID_CONST:
12019 cv_qualifier = TYPE_QUAL_CONST;
12020 break;
12021
12022 case RID_VOLATILE:
12023 cv_qualifier = TYPE_QUAL_VOLATILE;
12024 break;
12025
12026 case RID_RESTRICT:
12027 cv_qualifier = TYPE_QUAL_RESTRICT;
12028 break;
12029
12030 default:
12031 cv_qualifier = TYPE_UNQUALIFIED;
12032 break;
12033 }
12034
12035 if (!cv_qualifier)
12036 break;
12037
12038 if (cv_quals & cv_qualifier)
12039 {
12040 error ("duplicate cv-qualifier");
12041 cp_lexer_purge_token (parser->lexer);
12042 }
12043 else
12044 {
12045 cp_lexer_consume_token (parser->lexer);
12046 cv_quals |= cv_qualifier;
12047 }
12048 }
12049
12050 return cv_quals;
12051 }
12052
12053 /* Parse a declarator-id.
12054
12055 declarator-id:
12056 id-expression
12057 :: [opt] nested-name-specifier [opt] type-name
12058
12059 In the `id-expression' case, the value returned is as for
12060 cp_parser_id_expression if the id-expression was an unqualified-id.
12061 If the id-expression was a qualified-id, then a SCOPE_REF is
12062 returned. The first operand is the scope (either a NAMESPACE_DECL
12063 or TREE_TYPE), but the second is still just a representation of an
12064 unqualified-id. */
12065
12066 static tree
12067 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12068 {
12069 tree id;
12070 /* The expression must be an id-expression. Assume that qualified
12071 names are the names of types so that:
12072
12073 template <class T>
12074 int S<T>::R::i = 3;
12075
12076 will work; we must treat `S<T>::R' as the name of a type.
12077 Similarly, assume that qualified names are templates, where
12078 required, so that:
12079
12080 template <class T>
12081 int S<T>::R<T>::i = 3;
12082
12083 will work, too. */
12084 id = cp_parser_id_expression (parser,
12085 /*template_keyword_p=*/false,
12086 /*check_dependency_p=*/false,
12087 /*template_p=*/NULL,
12088 /*declarator_p=*/true,
12089 optional_p);
12090 if (id && BASELINK_P (id))
12091 id = BASELINK_FUNCTIONS (id);
12092 return id;
12093 }
12094
12095 /* Parse a type-id.
12096
12097 type-id:
12098 type-specifier-seq abstract-declarator [opt]
12099
12100 Returns the TYPE specified. */
12101
12102 static tree
12103 cp_parser_type_id (cp_parser* parser)
12104 {
12105 cp_decl_specifier_seq type_specifier_seq;
12106 cp_declarator *abstract_declarator;
12107
12108 /* Parse the type-specifier-seq. */
12109 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12110 &type_specifier_seq);
12111 if (type_specifier_seq.type == error_mark_node)
12112 return error_mark_node;
12113
12114 /* There might or might not be an abstract declarator. */
12115 cp_parser_parse_tentatively (parser);
12116 /* Look for the declarator. */
12117 abstract_declarator
12118 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12119 /*parenthesized_p=*/NULL,
12120 /*member_p=*/false);
12121 /* Check to see if there really was a declarator. */
12122 if (!cp_parser_parse_definitely (parser))
12123 abstract_declarator = NULL;
12124
12125 return groktypename (&type_specifier_seq, abstract_declarator);
12126 }
12127
12128 /* Parse a type-specifier-seq.
12129
12130 type-specifier-seq:
12131 type-specifier type-specifier-seq [opt]
12132
12133 GNU extension:
12134
12135 type-specifier-seq:
12136 attributes type-specifier-seq [opt]
12137
12138 If IS_CONDITION is true, we are at the start of a "condition",
12139 e.g., we've just seen "if (".
12140
12141 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12142
12143 static void
12144 cp_parser_type_specifier_seq (cp_parser* parser,
12145 bool is_condition,
12146 cp_decl_specifier_seq *type_specifier_seq)
12147 {
12148 bool seen_type_specifier = false;
12149 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12150
12151 /* Clear the TYPE_SPECIFIER_SEQ. */
12152 clear_decl_specs (type_specifier_seq);
12153
12154 /* Parse the type-specifiers and attributes. */
12155 while (true)
12156 {
12157 tree type_specifier;
12158 bool is_cv_qualifier;
12159
12160 /* Check for attributes first. */
12161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12162 {
12163 type_specifier_seq->attributes =
12164 chainon (type_specifier_seq->attributes,
12165 cp_parser_attributes_opt (parser));
12166 continue;
12167 }
12168
12169 /* Look for the type-specifier. */
12170 type_specifier = cp_parser_type_specifier (parser,
12171 flags,
12172 type_specifier_seq,
12173 /*is_declaration=*/false,
12174 NULL,
12175 &is_cv_qualifier);
12176 if (!type_specifier)
12177 {
12178 /* If the first type-specifier could not be found, this is not a
12179 type-specifier-seq at all. */
12180 if (!seen_type_specifier)
12181 {
12182 cp_parser_error (parser, "expected type-specifier");
12183 type_specifier_seq->type = error_mark_node;
12184 return;
12185 }
12186 /* If subsequent type-specifiers could not be found, the
12187 type-specifier-seq is complete. */
12188 break;
12189 }
12190
12191 seen_type_specifier = true;
12192 /* The standard says that a condition can be:
12193
12194 type-specifier-seq declarator = assignment-expression
12195
12196 However, given:
12197
12198 struct S {};
12199 if (int S = ...)
12200
12201 we should treat the "S" as a declarator, not as a
12202 type-specifier. The standard doesn't say that explicitly for
12203 type-specifier-seq, but it does say that for
12204 decl-specifier-seq in an ordinary declaration. Perhaps it
12205 would be clearer just to allow a decl-specifier-seq here, and
12206 then add a semantic restriction that if any decl-specifiers
12207 that are not type-specifiers appear, the program is invalid. */
12208 if (is_condition && !is_cv_qualifier)
12209 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12210 }
12211
12212 cp_parser_check_decl_spec (type_specifier_seq);
12213 }
12214
12215 /* Parse a parameter-declaration-clause.
12216
12217 parameter-declaration-clause:
12218 parameter-declaration-list [opt] ... [opt]
12219 parameter-declaration-list , ...
12220
12221 Returns a representation for the parameter declarations. A return
12222 value of NULL indicates a parameter-declaration-clause consisting
12223 only of an ellipsis. */
12224
12225 static cp_parameter_declarator *
12226 cp_parser_parameter_declaration_clause (cp_parser* parser)
12227 {
12228 cp_parameter_declarator *parameters;
12229 cp_token *token;
12230 bool ellipsis_p;
12231 bool is_error;
12232
12233 /* Peek at the next token. */
12234 token = cp_lexer_peek_token (parser->lexer);
12235 /* Check for trivial parameter-declaration-clauses. */
12236 if (token->type == CPP_ELLIPSIS)
12237 {
12238 /* Consume the `...' token. */
12239 cp_lexer_consume_token (parser->lexer);
12240 return NULL;
12241 }
12242 else if (token->type == CPP_CLOSE_PAREN)
12243 /* There are no parameters. */
12244 {
12245 #ifndef NO_IMPLICIT_EXTERN_C
12246 if (in_system_header && current_class_type == NULL
12247 && current_lang_name == lang_name_c)
12248 return NULL;
12249 else
12250 #endif
12251 return no_parameters;
12252 }
12253 /* Check for `(void)', too, which is a special case. */
12254 else if (token->keyword == RID_VOID
12255 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12256 == CPP_CLOSE_PAREN))
12257 {
12258 /* Consume the `void' token. */
12259 cp_lexer_consume_token (parser->lexer);
12260 /* There are no parameters. */
12261 return no_parameters;
12262 }
12263
12264 /* Parse the parameter-declaration-list. */
12265 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12266 /* If a parse error occurred while parsing the
12267 parameter-declaration-list, then the entire
12268 parameter-declaration-clause is erroneous. */
12269 if (is_error)
12270 return NULL;
12271
12272 /* Peek at the next token. */
12273 token = cp_lexer_peek_token (parser->lexer);
12274 /* If it's a `,', the clause should terminate with an ellipsis. */
12275 if (token->type == CPP_COMMA)
12276 {
12277 /* Consume the `,'. */
12278 cp_lexer_consume_token (parser->lexer);
12279 /* Expect an ellipsis. */
12280 ellipsis_p
12281 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12282 }
12283 /* It might also be `...' if the optional trailing `,' was
12284 omitted. */
12285 else if (token->type == CPP_ELLIPSIS)
12286 {
12287 /* Consume the `...' token. */
12288 cp_lexer_consume_token (parser->lexer);
12289 /* And remember that we saw it. */
12290 ellipsis_p = true;
12291 }
12292 else
12293 ellipsis_p = false;
12294
12295 /* Finish the parameter list. */
12296 if (parameters && ellipsis_p)
12297 parameters->ellipsis_p = true;
12298
12299 return parameters;
12300 }
12301
12302 /* Parse a parameter-declaration-list.
12303
12304 parameter-declaration-list:
12305 parameter-declaration
12306 parameter-declaration-list , parameter-declaration
12307
12308 Returns a representation of the parameter-declaration-list, as for
12309 cp_parser_parameter_declaration_clause. However, the
12310 `void_list_node' is never appended to the list. Upon return,
12311 *IS_ERROR will be true iff an error occurred. */
12312
12313 static cp_parameter_declarator *
12314 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12315 {
12316 cp_parameter_declarator *parameters = NULL;
12317 cp_parameter_declarator **tail = &parameters;
12318 bool saved_in_unbraced_linkage_specification_p;
12319
12320 /* Assume all will go well. */
12321 *is_error = false;
12322 /* The special considerations that apply to a function within an
12323 unbraced linkage specifications do not apply to the parameters
12324 to the function. */
12325 saved_in_unbraced_linkage_specification_p
12326 = parser->in_unbraced_linkage_specification_p;
12327 parser->in_unbraced_linkage_specification_p = false;
12328
12329 /* Look for more parameters. */
12330 while (true)
12331 {
12332 cp_parameter_declarator *parameter;
12333 bool parenthesized_p;
12334 /* Parse the parameter. */
12335 parameter
12336 = cp_parser_parameter_declaration (parser,
12337 /*template_parm_p=*/false,
12338 &parenthesized_p);
12339
12340 /* If a parse error occurred parsing the parameter declaration,
12341 then the entire parameter-declaration-list is erroneous. */
12342 if (!parameter)
12343 {
12344 *is_error = true;
12345 parameters = NULL;
12346 break;
12347 }
12348 /* Add the new parameter to the list. */
12349 *tail = parameter;
12350 tail = &parameter->next;
12351
12352 /* Peek at the next token. */
12353 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12354 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12355 /* These are for Objective-C++ */
12356 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12357 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12358 /* The parameter-declaration-list is complete. */
12359 break;
12360 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12361 {
12362 cp_token *token;
12363
12364 /* Peek at the next token. */
12365 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12366 /* If it's an ellipsis, then the list is complete. */
12367 if (token->type == CPP_ELLIPSIS)
12368 break;
12369 /* Otherwise, there must be more parameters. Consume the
12370 `,'. */
12371 cp_lexer_consume_token (parser->lexer);
12372 /* When parsing something like:
12373
12374 int i(float f, double d)
12375
12376 we can tell after seeing the declaration for "f" that we
12377 are not looking at an initialization of a variable "i",
12378 but rather at the declaration of a function "i".
12379
12380 Due to the fact that the parsing of template arguments
12381 (as specified to a template-id) requires backtracking we
12382 cannot use this technique when inside a template argument
12383 list. */
12384 if (!parser->in_template_argument_list_p
12385 && !parser->in_type_id_in_expr_p
12386 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12387 /* However, a parameter-declaration of the form
12388 "foat(f)" (which is a valid declaration of a
12389 parameter "f") can also be interpreted as an
12390 expression (the conversion of "f" to "float"). */
12391 && !parenthesized_p)
12392 cp_parser_commit_to_tentative_parse (parser);
12393 }
12394 else
12395 {
12396 cp_parser_error (parser, "expected %<,%> or %<...%>");
12397 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12398 cp_parser_skip_to_closing_parenthesis (parser,
12399 /*recovering=*/true,
12400 /*or_comma=*/false,
12401 /*consume_paren=*/false);
12402 break;
12403 }
12404 }
12405
12406 parser->in_unbraced_linkage_specification_p
12407 = saved_in_unbraced_linkage_specification_p;
12408
12409 return parameters;
12410 }
12411
12412 /* Parse a parameter declaration.
12413
12414 parameter-declaration:
12415 decl-specifier-seq declarator
12416 decl-specifier-seq declarator = assignment-expression
12417 decl-specifier-seq abstract-declarator [opt]
12418 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12419
12420 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12421 declares a template parameter. (In that case, a non-nested `>'
12422 token encountered during the parsing of the assignment-expression
12423 is not interpreted as a greater-than operator.)
12424
12425 Returns a representation of the parameter, or NULL if an error
12426 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12427 true iff the declarator is of the form "(p)". */
12428
12429 static cp_parameter_declarator *
12430 cp_parser_parameter_declaration (cp_parser *parser,
12431 bool template_parm_p,
12432 bool *parenthesized_p)
12433 {
12434 int declares_class_or_enum;
12435 bool greater_than_is_operator_p;
12436 cp_decl_specifier_seq decl_specifiers;
12437 cp_declarator *declarator;
12438 tree default_argument;
12439 cp_token *token;
12440 const char *saved_message;
12441
12442 /* In a template parameter, `>' is not an operator.
12443
12444 [temp.param]
12445
12446 When parsing a default template-argument for a non-type
12447 template-parameter, the first non-nested `>' is taken as the end
12448 of the template parameter-list rather than a greater-than
12449 operator. */
12450 greater_than_is_operator_p = !template_parm_p;
12451
12452 /* Type definitions may not appear in parameter types. */
12453 saved_message = parser->type_definition_forbidden_message;
12454 parser->type_definition_forbidden_message
12455 = "types may not be defined in parameter types";
12456
12457 /* Parse the declaration-specifiers. */
12458 cp_parser_decl_specifier_seq (parser,
12459 CP_PARSER_FLAGS_NONE,
12460 &decl_specifiers,
12461 &declares_class_or_enum);
12462 /* If an error occurred, there's no reason to attempt to parse the
12463 rest of the declaration. */
12464 if (cp_parser_error_occurred (parser))
12465 {
12466 parser->type_definition_forbidden_message = saved_message;
12467 return NULL;
12468 }
12469
12470 /* Peek at the next token. */
12471 token = cp_lexer_peek_token (parser->lexer);
12472 /* If the next token is a `)', `,', `=', `>', or `...', then there
12473 is no declarator. */
12474 if (token->type == CPP_CLOSE_PAREN
12475 || token->type == CPP_COMMA
12476 || token->type == CPP_EQ
12477 || token->type == CPP_ELLIPSIS
12478 || token->type == CPP_GREATER)
12479 {
12480 declarator = NULL;
12481 if (parenthesized_p)
12482 *parenthesized_p = false;
12483 }
12484 /* Otherwise, there should be a declarator. */
12485 else
12486 {
12487 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12488 parser->default_arg_ok_p = false;
12489
12490 /* After seeing a decl-specifier-seq, if the next token is not a
12491 "(", there is no possibility that the code is a valid
12492 expression. Therefore, if parsing tentatively, we commit at
12493 this point. */
12494 if (!parser->in_template_argument_list_p
12495 /* In an expression context, having seen:
12496
12497 (int((char ...
12498
12499 we cannot be sure whether we are looking at a
12500 function-type (taking a "char" as a parameter) or a cast
12501 of some object of type "char" to "int". */
12502 && !parser->in_type_id_in_expr_p
12503 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12504 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12505 cp_parser_commit_to_tentative_parse (parser);
12506 /* Parse the declarator. */
12507 declarator = cp_parser_declarator (parser,
12508 CP_PARSER_DECLARATOR_EITHER,
12509 /*ctor_dtor_or_conv_p=*/NULL,
12510 parenthesized_p,
12511 /*member_p=*/false);
12512 parser->default_arg_ok_p = saved_default_arg_ok_p;
12513 /* After the declarator, allow more attributes. */
12514 decl_specifiers.attributes
12515 = chainon (decl_specifiers.attributes,
12516 cp_parser_attributes_opt (parser));
12517 }
12518
12519 /* The restriction on defining new types applies only to the type
12520 of the parameter, not to the default argument. */
12521 parser->type_definition_forbidden_message = saved_message;
12522
12523 /* If the next token is `=', then process a default argument. */
12524 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12525 {
12526 bool saved_greater_than_is_operator_p;
12527 /* Consume the `='. */
12528 cp_lexer_consume_token (parser->lexer);
12529
12530 /* If we are defining a class, then the tokens that make up the
12531 default argument must be saved and processed later. */
12532 if (!template_parm_p && at_class_scope_p ()
12533 && TYPE_BEING_DEFINED (current_class_type))
12534 {
12535 unsigned depth = 0;
12536 cp_token *first_token;
12537 cp_token *token;
12538
12539 /* Add tokens until we have processed the entire default
12540 argument. We add the range [first_token, token). */
12541 first_token = cp_lexer_peek_token (parser->lexer);
12542 while (true)
12543 {
12544 bool done = false;
12545
12546 /* Peek at the next token. */
12547 token = cp_lexer_peek_token (parser->lexer);
12548 /* What we do depends on what token we have. */
12549 switch (token->type)
12550 {
12551 /* In valid code, a default argument must be
12552 immediately followed by a `,' `)', or `...'. */
12553 case CPP_COMMA:
12554 case CPP_CLOSE_PAREN:
12555 case CPP_ELLIPSIS:
12556 /* If we run into a non-nested `;', `}', or `]',
12557 then the code is invalid -- but the default
12558 argument is certainly over. */
12559 case CPP_SEMICOLON:
12560 case CPP_CLOSE_BRACE:
12561 case CPP_CLOSE_SQUARE:
12562 if (depth == 0)
12563 done = true;
12564 /* Update DEPTH, if necessary. */
12565 else if (token->type == CPP_CLOSE_PAREN
12566 || token->type == CPP_CLOSE_BRACE
12567 || token->type == CPP_CLOSE_SQUARE)
12568 --depth;
12569 break;
12570
12571 case CPP_OPEN_PAREN:
12572 case CPP_OPEN_SQUARE:
12573 case CPP_OPEN_BRACE:
12574 ++depth;
12575 break;
12576
12577 case CPP_GREATER:
12578 /* If we see a non-nested `>', and `>' is not an
12579 operator, then it marks the end of the default
12580 argument. */
12581 if (!depth && !greater_than_is_operator_p)
12582 done = true;
12583 break;
12584
12585 /* If we run out of tokens, issue an error message. */
12586 case CPP_EOF:
12587 case CPP_PRAGMA_EOL:
12588 error ("file ends in default argument");
12589 done = true;
12590 break;
12591
12592 case CPP_NAME:
12593 case CPP_SCOPE:
12594 /* In these cases, we should look for template-ids.
12595 For example, if the default argument is
12596 `X<int, double>()', we need to do name lookup to
12597 figure out whether or not `X' is a template; if
12598 so, the `,' does not end the default argument.
12599
12600 That is not yet done. */
12601 break;
12602
12603 default:
12604 break;
12605 }
12606
12607 /* If we've reached the end, stop. */
12608 if (done)
12609 break;
12610
12611 /* Add the token to the token block. */
12612 token = cp_lexer_consume_token (parser->lexer);
12613 }
12614
12615 /* Create a DEFAULT_ARG to represented the unparsed default
12616 argument. */
12617 default_argument = make_node (DEFAULT_ARG);
12618 DEFARG_TOKENS (default_argument)
12619 = cp_token_cache_new (first_token, token);
12620 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12621 }
12622 /* Outside of a class definition, we can just parse the
12623 assignment-expression. */
12624 else
12625 {
12626 bool saved_local_variables_forbidden_p;
12627
12628 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12629 set correctly. */
12630 saved_greater_than_is_operator_p
12631 = parser->greater_than_is_operator_p;
12632 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12633 /* Local variable names (and the `this' keyword) may not
12634 appear in a default argument. */
12635 saved_local_variables_forbidden_p
12636 = parser->local_variables_forbidden_p;
12637 parser->local_variables_forbidden_p = true;
12638 /* The default argument expression may cause implicitly
12639 defined member functions to be synthesized, which will
12640 result in garbage collection. We must treat this
12641 situation as if we were within the body of function so as
12642 to avoid collecting live data on the stack. */
12643 ++function_depth;
12644 /* Parse the assignment-expression. */
12645 if (template_parm_p)
12646 push_deferring_access_checks (dk_no_deferred);
12647 default_argument
12648 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12649 if (template_parm_p)
12650 pop_deferring_access_checks ();
12651 /* Restore saved state. */
12652 --function_depth;
12653 parser->greater_than_is_operator_p
12654 = saved_greater_than_is_operator_p;
12655 parser->local_variables_forbidden_p
12656 = saved_local_variables_forbidden_p;
12657 }
12658 if (!parser->default_arg_ok_p)
12659 {
12660 if (!flag_pedantic_errors)
12661 warning (0, "deprecated use of default argument for parameter of non-function");
12662 else
12663 {
12664 error ("default arguments are only permitted for function parameters");
12665 default_argument = NULL_TREE;
12666 }
12667 }
12668 }
12669 else
12670 default_argument = NULL_TREE;
12671
12672 return make_parameter_declarator (&decl_specifiers,
12673 declarator,
12674 default_argument);
12675 }
12676
12677 /* Parse a function-body.
12678
12679 function-body:
12680 compound_statement */
12681
12682 static void
12683 cp_parser_function_body (cp_parser *parser)
12684 {
12685 cp_parser_compound_statement (parser, NULL, false);
12686 }
12687
12688 /* Parse a ctor-initializer-opt followed by a function-body. Return
12689 true if a ctor-initializer was present. */
12690
12691 static bool
12692 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12693 {
12694 tree body;
12695 bool ctor_initializer_p;
12696
12697 /* Begin the function body. */
12698 body = begin_function_body ();
12699 /* Parse the optional ctor-initializer. */
12700 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12701 /* Parse the function-body. */
12702 cp_parser_function_body (parser);
12703 /* Finish the function body. */
12704 finish_function_body (body);
12705
12706 return ctor_initializer_p;
12707 }
12708
12709 /* Parse an initializer.
12710
12711 initializer:
12712 = initializer-clause
12713 ( expression-list )
12714
12715 Returns an expression representing the initializer. If no
12716 initializer is present, NULL_TREE is returned.
12717
12718 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12719 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12720 set to FALSE if there is no initializer present. If there is an
12721 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12722 is set to true; otherwise it is set to false. */
12723
12724 static tree
12725 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12726 bool* non_constant_p)
12727 {
12728 cp_token *token;
12729 tree init;
12730
12731 /* Peek at the next token. */
12732 token = cp_lexer_peek_token (parser->lexer);
12733
12734 /* Let our caller know whether or not this initializer was
12735 parenthesized. */
12736 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12737 /* Assume that the initializer is constant. */
12738 *non_constant_p = false;
12739
12740 if (token->type == CPP_EQ)
12741 {
12742 /* Consume the `='. */
12743 cp_lexer_consume_token (parser->lexer);
12744 /* Parse the initializer-clause. */
12745 init = cp_parser_initializer_clause (parser, non_constant_p);
12746 }
12747 else if (token->type == CPP_OPEN_PAREN)
12748 init = cp_parser_parenthesized_expression_list (parser, false,
12749 /*cast_p=*/false,
12750 non_constant_p);
12751 else
12752 {
12753 /* Anything else is an error. */
12754 cp_parser_error (parser, "expected initializer");
12755 init = error_mark_node;
12756 }
12757
12758 return init;
12759 }
12760
12761 /* Parse an initializer-clause.
12762
12763 initializer-clause:
12764 assignment-expression
12765 { initializer-list , [opt] }
12766 { }
12767
12768 Returns an expression representing the initializer.
12769
12770 If the `assignment-expression' production is used the value
12771 returned is simply a representation for the expression.
12772
12773 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12774 the elements of the initializer-list (or NULL, if the last
12775 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12776 NULL_TREE. There is no way to detect whether or not the optional
12777 trailing `,' was provided. NON_CONSTANT_P is as for
12778 cp_parser_initializer. */
12779
12780 static tree
12781 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12782 {
12783 tree initializer;
12784
12785 /* Assume the expression is constant. */
12786 *non_constant_p = false;
12787
12788 /* If it is not a `{', then we are looking at an
12789 assignment-expression. */
12790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12791 {
12792 initializer
12793 = cp_parser_constant_expression (parser,
12794 /*allow_non_constant_p=*/true,
12795 non_constant_p);
12796 if (!*non_constant_p)
12797 initializer = fold_non_dependent_expr (initializer);
12798 }
12799 else
12800 {
12801 /* Consume the `{' token. */
12802 cp_lexer_consume_token (parser->lexer);
12803 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12804 initializer = make_node (CONSTRUCTOR);
12805 /* If it's not a `}', then there is a non-trivial initializer. */
12806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12807 {
12808 /* Parse the initializer list. */
12809 CONSTRUCTOR_ELTS (initializer)
12810 = cp_parser_initializer_list (parser, non_constant_p);
12811 /* A trailing `,' token is allowed. */
12812 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12813 cp_lexer_consume_token (parser->lexer);
12814 }
12815 /* Now, there should be a trailing `}'. */
12816 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12817 }
12818
12819 return initializer;
12820 }
12821
12822 /* Parse an initializer-list.
12823
12824 initializer-list:
12825 initializer-clause
12826 initializer-list , initializer-clause
12827
12828 GNU Extension:
12829
12830 initializer-list:
12831 identifier : initializer-clause
12832 initializer-list, identifier : initializer-clause
12833
12834 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12835 for the initializer. If the INDEX of the elt is non-NULL, it is the
12836 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12837 as for cp_parser_initializer. */
12838
12839 static VEC(constructor_elt,gc) *
12840 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12841 {
12842 VEC(constructor_elt,gc) *v = NULL;
12843
12844 /* Assume all of the expressions are constant. */
12845 *non_constant_p = false;
12846
12847 /* Parse the rest of the list. */
12848 while (true)
12849 {
12850 cp_token *token;
12851 tree identifier;
12852 tree initializer;
12853 bool clause_non_constant_p;
12854
12855 /* If the next token is an identifier and the following one is a
12856 colon, we are looking at the GNU designated-initializer
12857 syntax. */
12858 if (cp_parser_allow_gnu_extensions_p (parser)
12859 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12860 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12861 {
12862 /* Warn the user that they are using an extension. */
12863 if (pedantic)
12864 pedwarn ("ISO C++ does not allow designated initializers");
12865 /* Consume the identifier. */
12866 identifier = cp_lexer_consume_token (parser->lexer)->value;
12867 /* Consume the `:'. */
12868 cp_lexer_consume_token (parser->lexer);
12869 }
12870 else
12871 identifier = NULL_TREE;
12872
12873 /* Parse the initializer. */
12874 initializer = cp_parser_initializer_clause (parser,
12875 &clause_non_constant_p);
12876 /* If any clause is non-constant, so is the entire initializer. */
12877 if (clause_non_constant_p)
12878 *non_constant_p = true;
12879
12880 /* Add it to the vector. */
12881 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12882
12883 /* If the next token is not a comma, we have reached the end of
12884 the list. */
12885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12886 break;
12887
12888 /* Peek at the next token. */
12889 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12890 /* If the next token is a `}', then we're still done. An
12891 initializer-clause can have a trailing `,' after the
12892 initializer-list and before the closing `}'. */
12893 if (token->type == CPP_CLOSE_BRACE)
12894 break;
12895
12896 /* Consume the `,' token. */
12897 cp_lexer_consume_token (parser->lexer);
12898 }
12899
12900 return v;
12901 }
12902
12903 /* Classes [gram.class] */
12904
12905 /* Parse a class-name.
12906
12907 class-name:
12908 identifier
12909 template-id
12910
12911 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12912 to indicate that names looked up in dependent types should be
12913 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12914 keyword has been used to indicate that the name that appears next
12915 is a template. TAG_TYPE indicates the explicit tag given before
12916 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12917 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12918 is the class being defined in a class-head.
12919
12920 Returns the TYPE_DECL representing the class. */
12921
12922 static tree
12923 cp_parser_class_name (cp_parser *parser,
12924 bool typename_keyword_p,
12925 bool template_keyword_p,
12926 enum tag_types tag_type,
12927 bool check_dependency_p,
12928 bool class_head_p,
12929 bool is_declaration)
12930 {
12931 tree decl;
12932 tree scope;
12933 bool typename_p;
12934 cp_token *token;
12935
12936 /* All class-names start with an identifier. */
12937 token = cp_lexer_peek_token (parser->lexer);
12938 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12939 {
12940 cp_parser_error (parser, "expected class-name");
12941 return error_mark_node;
12942 }
12943
12944 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12945 to a template-id, so we save it here. */
12946 scope = parser->scope;
12947 if (scope == error_mark_node)
12948 return error_mark_node;
12949
12950 /* Any name names a type if we're following the `typename' keyword
12951 in a qualified name where the enclosing scope is type-dependent. */
12952 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12953 && dependent_type_p (scope));
12954 /* Handle the common case (an identifier, but not a template-id)
12955 efficiently. */
12956 if (token->type == CPP_NAME
12957 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12958 {
12959 cp_token *identifier_token;
12960 tree identifier;
12961 bool ambiguous_p;
12962
12963 /* Look for the identifier. */
12964 identifier_token = cp_lexer_peek_token (parser->lexer);
12965 ambiguous_p = identifier_token->ambiguous_p;
12966 identifier = cp_parser_identifier (parser);
12967 /* If the next token isn't an identifier, we are certainly not
12968 looking at a class-name. */
12969 if (identifier == error_mark_node)
12970 decl = error_mark_node;
12971 /* If we know this is a type-name, there's no need to look it
12972 up. */
12973 else if (typename_p)
12974 decl = identifier;
12975 else
12976 {
12977 tree ambiguous_decls;
12978 /* If we already know that this lookup is ambiguous, then
12979 we've already issued an error message; there's no reason
12980 to check again. */
12981 if (ambiguous_p)
12982 {
12983 cp_parser_simulate_error (parser);
12984 return error_mark_node;
12985 }
12986 /* If the next token is a `::', then the name must be a type
12987 name.
12988
12989 [basic.lookup.qual]
12990
12991 During the lookup for a name preceding the :: scope
12992 resolution operator, object, function, and enumerator
12993 names are ignored. */
12994 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12995 tag_type = typename_type;
12996 /* Look up the name. */
12997 decl = cp_parser_lookup_name (parser, identifier,
12998 tag_type,
12999 /*is_template=*/false,
13000 /*is_namespace=*/false,
13001 check_dependency_p,
13002 &ambiguous_decls);
13003 if (ambiguous_decls)
13004 {
13005 error ("reference to %qD is ambiguous", identifier);
13006 print_candidates (ambiguous_decls);
13007 if (cp_parser_parsing_tentatively (parser))
13008 {
13009 identifier_token->ambiguous_p = true;
13010 cp_parser_simulate_error (parser);
13011 }
13012 return error_mark_node;
13013 }
13014 }
13015 }
13016 else
13017 {
13018 /* Try a template-id. */
13019 decl = cp_parser_template_id (parser, template_keyword_p,
13020 check_dependency_p,
13021 is_declaration);
13022 if (decl == error_mark_node)
13023 return error_mark_node;
13024 }
13025
13026 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13027
13028 /* If this is a typename, create a TYPENAME_TYPE. */
13029 if (typename_p && decl != error_mark_node)
13030 {
13031 decl = make_typename_type (scope, decl, typename_type,
13032 /*complain=*/tf_error);
13033 if (decl != error_mark_node)
13034 decl = TYPE_NAME (decl);
13035 }
13036
13037 /* Check to see that it is really the name of a class. */
13038 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13039 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13040 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13041 /* Situations like this:
13042
13043 template <typename T> struct A {
13044 typename T::template X<int>::I i;
13045 };
13046
13047 are problematic. Is `T::template X<int>' a class-name? The
13048 standard does not seem to be definitive, but there is no other
13049 valid interpretation of the following `::'. Therefore, those
13050 names are considered class-names. */
13051 {
13052 decl = make_typename_type (scope, decl, tag_type, tf_error);
13053 if (decl != error_mark_node)
13054 decl = TYPE_NAME (decl);
13055 }
13056 else if (TREE_CODE (decl) != TYPE_DECL
13057 || TREE_TYPE (decl) == error_mark_node
13058 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13059 decl = error_mark_node;
13060
13061 if (decl == error_mark_node)
13062 cp_parser_error (parser, "expected class-name");
13063
13064 return decl;
13065 }
13066
13067 /* Parse a class-specifier.
13068
13069 class-specifier:
13070 class-head { member-specification [opt] }
13071
13072 Returns the TREE_TYPE representing the class. */
13073
13074 static tree
13075 cp_parser_class_specifier (cp_parser* parser)
13076 {
13077 cp_token *token;
13078 tree type;
13079 tree attributes = NULL_TREE;
13080 int has_trailing_semicolon;
13081 bool nested_name_specifier_p;
13082 unsigned saved_num_template_parameter_lists;
13083 tree old_scope = NULL_TREE;
13084 tree scope = NULL_TREE;
13085
13086 push_deferring_access_checks (dk_no_deferred);
13087
13088 /* Parse the class-head. */
13089 type = cp_parser_class_head (parser,
13090 &nested_name_specifier_p,
13091 &attributes);
13092 /* If the class-head was a semantic disaster, skip the entire body
13093 of the class. */
13094 if (!type)
13095 {
13096 cp_parser_skip_to_end_of_block_or_statement (parser);
13097 pop_deferring_access_checks ();
13098 return error_mark_node;
13099 }
13100
13101 /* Look for the `{'. */
13102 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13103 {
13104 pop_deferring_access_checks ();
13105 return error_mark_node;
13106 }
13107
13108 /* Issue an error message if type-definitions are forbidden here. */
13109 cp_parser_check_type_definition (parser);
13110 /* Remember that we are defining one more class. */
13111 ++parser->num_classes_being_defined;
13112 /* Inside the class, surrounding template-parameter-lists do not
13113 apply. */
13114 saved_num_template_parameter_lists
13115 = parser->num_template_parameter_lists;
13116 parser->num_template_parameter_lists = 0;
13117
13118 /* Start the class. */
13119 if (nested_name_specifier_p)
13120 {
13121 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13122 old_scope = push_inner_scope (scope);
13123 }
13124 type = begin_class_definition (type, attributes);
13125
13126 if (type == error_mark_node)
13127 /* If the type is erroneous, skip the entire body of the class. */
13128 cp_parser_skip_to_closing_brace (parser);
13129 else
13130 /* Parse the member-specification. */
13131 cp_parser_member_specification_opt (parser);
13132
13133 /* Look for the trailing `}'. */
13134 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13135 /* We get better error messages by noticing a common problem: a
13136 missing trailing `;'. */
13137 token = cp_lexer_peek_token (parser->lexer);
13138 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13139 /* Look for trailing attributes to apply to this class. */
13140 if (cp_parser_allow_gnu_extensions_p (parser))
13141 attributes = cp_parser_attributes_opt (parser);
13142 if (type != error_mark_node)
13143 type = finish_struct (type, attributes);
13144 if (nested_name_specifier_p)
13145 pop_inner_scope (old_scope, scope);
13146 /* If this class is not itself within the scope of another class,
13147 then we need to parse the bodies of all of the queued function
13148 definitions. Note that the queued functions defined in a class
13149 are not always processed immediately following the
13150 class-specifier for that class. Consider:
13151
13152 struct A {
13153 struct B { void f() { sizeof (A); } };
13154 };
13155
13156 If `f' were processed before the processing of `A' were
13157 completed, there would be no way to compute the size of `A'.
13158 Note that the nesting we are interested in here is lexical --
13159 not the semantic nesting given by TYPE_CONTEXT. In particular,
13160 for:
13161
13162 struct A { struct B; };
13163 struct A::B { void f() { } };
13164
13165 there is no need to delay the parsing of `A::B::f'. */
13166 if (--parser->num_classes_being_defined == 0)
13167 {
13168 tree queue_entry;
13169 tree fn;
13170 tree class_type = NULL_TREE;
13171 tree pushed_scope = NULL_TREE;
13172
13173 /* In a first pass, parse default arguments to the functions.
13174 Then, in a second pass, parse the bodies of the functions.
13175 This two-phased approach handles cases like:
13176
13177 struct S {
13178 void f() { g(); }
13179 void g(int i = 3);
13180 };
13181
13182 */
13183 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13184 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13185 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13186 TREE_PURPOSE (parser->unparsed_functions_queues)
13187 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13188 {
13189 fn = TREE_VALUE (queue_entry);
13190 /* If there are default arguments that have not yet been processed,
13191 take care of them now. */
13192 if (class_type != TREE_PURPOSE (queue_entry))
13193 {
13194 if (pushed_scope)
13195 pop_scope (pushed_scope);
13196 class_type = TREE_PURPOSE (queue_entry);
13197 pushed_scope = push_scope (class_type);
13198 }
13199 /* Make sure that any template parameters are in scope. */
13200 maybe_begin_member_template_processing (fn);
13201 /* Parse the default argument expressions. */
13202 cp_parser_late_parsing_default_args (parser, fn);
13203 /* Remove any template parameters from the symbol table. */
13204 maybe_end_member_template_processing ();
13205 }
13206 if (pushed_scope)
13207 pop_scope (pushed_scope);
13208 /* Now parse the body of the functions. */
13209 for (TREE_VALUE (parser->unparsed_functions_queues)
13210 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13211 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13212 TREE_VALUE (parser->unparsed_functions_queues)
13213 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13214 {
13215 /* Figure out which function we need to process. */
13216 fn = TREE_VALUE (queue_entry);
13217 /* Parse the function. */
13218 cp_parser_late_parsing_for_member (parser, fn);
13219 }
13220 }
13221
13222 /* Put back any saved access checks. */
13223 pop_deferring_access_checks ();
13224
13225 /* Restore the count of active template-parameter-lists. */
13226 parser->num_template_parameter_lists
13227 = saved_num_template_parameter_lists;
13228
13229 return type;
13230 }
13231
13232 /* Parse a class-head.
13233
13234 class-head:
13235 class-key identifier [opt] base-clause [opt]
13236 class-key nested-name-specifier identifier base-clause [opt]
13237 class-key nested-name-specifier [opt] template-id
13238 base-clause [opt]
13239
13240 GNU Extensions:
13241 class-key attributes identifier [opt] base-clause [opt]
13242 class-key attributes nested-name-specifier identifier base-clause [opt]
13243 class-key attributes nested-name-specifier [opt] template-id
13244 base-clause [opt]
13245
13246 Returns the TYPE of the indicated class. Sets
13247 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13248 involving a nested-name-specifier was used, and FALSE otherwise.
13249
13250 Returns error_mark_node if this is not a class-head.
13251
13252 Returns NULL_TREE if the class-head is syntactically valid, but
13253 semantically invalid in a way that means we should skip the entire
13254 body of the class. */
13255
13256 static tree
13257 cp_parser_class_head (cp_parser* parser,
13258 bool* nested_name_specifier_p,
13259 tree *attributes_p)
13260 {
13261 tree nested_name_specifier;
13262 enum tag_types class_key;
13263 tree id = NULL_TREE;
13264 tree type = NULL_TREE;
13265 tree attributes;
13266 bool template_id_p = false;
13267 bool qualified_p = false;
13268 bool invalid_nested_name_p = false;
13269 bool invalid_explicit_specialization_p = false;
13270 tree pushed_scope = NULL_TREE;
13271 unsigned num_templates;
13272 tree bases;
13273
13274 /* Assume no nested-name-specifier will be present. */
13275 *nested_name_specifier_p = false;
13276 /* Assume no template parameter lists will be used in defining the
13277 type. */
13278 num_templates = 0;
13279
13280 /* Look for the class-key. */
13281 class_key = cp_parser_class_key (parser);
13282 if (class_key == none_type)
13283 return error_mark_node;
13284
13285 /* Parse the attributes. */
13286 attributes = cp_parser_attributes_opt (parser);
13287
13288 /* If the next token is `::', that is invalid -- but sometimes
13289 people do try to write:
13290
13291 struct ::S {};
13292
13293 Handle this gracefully by accepting the extra qualifier, and then
13294 issuing an error about it later if this really is a
13295 class-head. If it turns out just to be an elaborated type
13296 specifier, remain silent. */
13297 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13298 qualified_p = true;
13299
13300 push_deferring_access_checks (dk_no_check);
13301
13302 /* Determine the name of the class. Begin by looking for an
13303 optional nested-name-specifier. */
13304 nested_name_specifier
13305 = cp_parser_nested_name_specifier_opt (parser,
13306 /*typename_keyword_p=*/false,
13307 /*check_dependency_p=*/false,
13308 /*type_p=*/false,
13309 /*is_declaration=*/false);
13310 /* If there was a nested-name-specifier, then there *must* be an
13311 identifier. */
13312 if (nested_name_specifier)
13313 {
13314 /* Although the grammar says `identifier', it really means
13315 `class-name' or `template-name'. You are only allowed to
13316 define a class that has already been declared with this
13317 syntax.
13318
13319 The proposed resolution for Core Issue 180 says that wherever
13320 you see `class T::X' you should treat `X' as a type-name.
13321
13322 It is OK to define an inaccessible class; for example:
13323
13324 class A { class B; };
13325 class A::B {};
13326
13327 We do not know if we will see a class-name, or a
13328 template-name. We look for a class-name first, in case the
13329 class-name is a template-id; if we looked for the
13330 template-name first we would stop after the template-name. */
13331 cp_parser_parse_tentatively (parser);
13332 type = cp_parser_class_name (parser,
13333 /*typename_keyword_p=*/false,
13334 /*template_keyword_p=*/false,
13335 class_type,
13336 /*check_dependency_p=*/false,
13337 /*class_head_p=*/true,
13338 /*is_declaration=*/false);
13339 /* If that didn't work, ignore the nested-name-specifier. */
13340 if (!cp_parser_parse_definitely (parser))
13341 {
13342 invalid_nested_name_p = true;
13343 id = cp_parser_identifier (parser);
13344 if (id == error_mark_node)
13345 id = NULL_TREE;
13346 }
13347 /* If we could not find a corresponding TYPE, treat this
13348 declaration like an unqualified declaration. */
13349 if (type == error_mark_node)
13350 nested_name_specifier = NULL_TREE;
13351 /* Otherwise, count the number of templates used in TYPE and its
13352 containing scopes. */
13353 else
13354 {
13355 tree scope;
13356
13357 for (scope = TREE_TYPE (type);
13358 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13359 scope = (TYPE_P (scope)
13360 ? TYPE_CONTEXT (scope)
13361 : DECL_CONTEXT (scope)))
13362 if (TYPE_P (scope)
13363 && CLASS_TYPE_P (scope)
13364 && CLASSTYPE_TEMPLATE_INFO (scope)
13365 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13366 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13367 ++num_templates;
13368 }
13369 }
13370 /* Otherwise, the identifier is optional. */
13371 else
13372 {
13373 /* We don't know whether what comes next is a template-id,
13374 an identifier, or nothing at all. */
13375 cp_parser_parse_tentatively (parser);
13376 /* Check for a template-id. */
13377 id = cp_parser_template_id (parser,
13378 /*template_keyword_p=*/false,
13379 /*check_dependency_p=*/true,
13380 /*is_declaration=*/true);
13381 /* If that didn't work, it could still be an identifier. */
13382 if (!cp_parser_parse_definitely (parser))
13383 {
13384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13385 id = cp_parser_identifier (parser);
13386 else
13387 id = NULL_TREE;
13388 }
13389 else
13390 {
13391 template_id_p = true;
13392 ++num_templates;
13393 }
13394 }
13395
13396 pop_deferring_access_checks ();
13397
13398 if (id)
13399 cp_parser_check_for_invalid_template_id (parser, id);
13400
13401 /* If it's not a `:' or a `{' then we can't really be looking at a
13402 class-head, since a class-head only appears as part of a
13403 class-specifier. We have to detect this situation before calling
13404 xref_tag, since that has irreversible side-effects. */
13405 if (!cp_parser_next_token_starts_class_definition_p (parser))
13406 {
13407 cp_parser_error (parser, "expected %<{%> or %<:%>");
13408 return error_mark_node;
13409 }
13410
13411 /* At this point, we're going ahead with the class-specifier, even
13412 if some other problem occurs. */
13413 cp_parser_commit_to_tentative_parse (parser);
13414 /* Issue the error about the overly-qualified name now. */
13415 if (qualified_p)
13416 cp_parser_error (parser,
13417 "global qualification of class name is invalid");
13418 else if (invalid_nested_name_p)
13419 cp_parser_error (parser,
13420 "qualified name does not name a class");
13421 else if (nested_name_specifier)
13422 {
13423 tree scope;
13424
13425 /* Reject typedef-names in class heads. */
13426 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13427 {
13428 error ("invalid class name in declaration of %qD", type);
13429 type = NULL_TREE;
13430 goto done;
13431 }
13432
13433 /* Figure out in what scope the declaration is being placed. */
13434 scope = current_scope ();
13435 /* If that scope does not contain the scope in which the
13436 class was originally declared, the program is invalid. */
13437 if (scope && !is_ancestor (scope, nested_name_specifier))
13438 {
13439 error ("declaration of %qD in %qD which does not enclose %qD",
13440 type, scope, nested_name_specifier);
13441 type = NULL_TREE;
13442 goto done;
13443 }
13444 /* [dcl.meaning]
13445
13446 A declarator-id shall not be qualified exception of the
13447 definition of a ... nested class outside of its class
13448 ... [or] a the definition or explicit instantiation of a
13449 class member of a namespace outside of its namespace. */
13450 if (scope == nested_name_specifier)
13451 {
13452 pedwarn ("extra qualification ignored");
13453 nested_name_specifier = NULL_TREE;
13454 num_templates = 0;
13455 }
13456 }
13457 /* An explicit-specialization must be preceded by "template <>". If
13458 it is not, try to recover gracefully. */
13459 if (at_namespace_scope_p ()
13460 && parser->num_template_parameter_lists == 0
13461 && template_id_p)
13462 {
13463 error ("an explicit specialization must be preceded by %<template <>%>");
13464 invalid_explicit_specialization_p = true;
13465 /* Take the same action that would have been taken by
13466 cp_parser_explicit_specialization. */
13467 ++parser->num_template_parameter_lists;
13468 begin_specialization ();
13469 }
13470 /* There must be no "return" statements between this point and the
13471 end of this function; set "type "to the correct return value and
13472 use "goto done;" to return. */
13473 /* Make sure that the right number of template parameters were
13474 present. */
13475 if (!cp_parser_check_template_parameters (parser, num_templates))
13476 {
13477 /* If something went wrong, there is no point in even trying to
13478 process the class-definition. */
13479 type = NULL_TREE;
13480 goto done;
13481 }
13482
13483 /* Look up the type. */
13484 if (template_id_p)
13485 {
13486 type = TREE_TYPE (id);
13487 type = maybe_process_partial_specialization (type);
13488 if (nested_name_specifier)
13489 pushed_scope = push_scope (nested_name_specifier);
13490 }
13491 else if (nested_name_specifier)
13492 {
13493 tree class_type;
13494
13495 /* Given:
13496
13497 template <typename T> struct S { struct T };
13498 template <typename T> struct S<T>::T { };
13499
13500 we will get a TYPENAME_TYPE when processing the definition of
13501 `S::T'. We need to resolve it to the actual type before we
13502 try to define it. */
13503 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13504 {
13505 class_type = resolve_typename_type (TREE_TYPE (type),
13506 /*only_current_p=*/false);
13507 if (class_type != error_mark_node)
13508 type = TYPE_NAME (class_type);
13509 else
13510 {
13511 cp_parser_error (parser, "could not resolve typename type");
13512 type = error_mark_node;
13513 }
13514 }
13515
13516 maybe_process_partial_specialization (TREE_TYPE (type));
13517 class_type = current_class_type;
13518 /* Enter the scope indicated by the nested-name-specifier. */
13519 pushed_scope = push_scope (nested_name_specifier);
13520 /* Get the canonical version of this type. */
13521 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13522 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13523 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13524 {
13525 type = push_template_decl (type);
13526 if (type == error_mark_node)
13527 {
13528 type = NULL_TREE;
13529 goto done;
13530 }
13531 }
13532
13533 type = TREE_TYPE (type);
13534 *nested_name_specifier_p = true;
13535 }
13536 else /* The name is not a nested name. */
13537 {
13538 /* If the class was unnamed, create a dummy name. */
13539 if (!id)
13540 id = make_anon_name ();
13541 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13542 parser->num_template_parameter_lists);
13543 }
13544
13545 /* Indicate whether this class was declared as a `class' or as a
13546 `struct'. */
13547 if (TREE_CODE (type) == RECORD_TYPE)
13548 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13549 cp_parser_check_class_key (class_key, type);
13550
13551 /* If this type was already complete, and we see another definition,
13552 that's an error. */
13553 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13554 {
13555 error ("redefinition of %q#T", type);
13556 error ("previous definition of %q+#T", type);
13557 type = NULL_TREE;
13558 goto done;
13559 }
13560
13561 /* We will have entered the scope containing the class; the names of
13562 base classes should be looked up in that context. For example:
13563
13564 struct A { struct B {}; struct C; };
13565 struct A::C : B {};
13566
13567 is valid. */
13568 bases = NULL_TREE;
13569
13570 /* Get the list of base-classes, if there is one. */
13571 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13572 bases = cp_parser_base_clause (parser);
13573
13574 /* Process the base classes. */
13575 if (!xref_basetypes (type, bases))
13576 type = NULL_TREE;
13577
13578 done:
13579 /* Leave the scope given by the nested-name-specifier. We will
13580 enter the class scope itself while processing the members. */
13581 if (pushed_scope)
13582 pop_scope (pushed_scope);
13583
13584 if (invalid_explicit_specialization_p)
13585 {
13586 end_specialization ();
13587 --parser->num_template_parameter_lists;
13588 }
13589 *attributes_p = attributes;
13590 return type;
13591 }
13592
13593 /* Parse a class-key.
13594
13595 class-key:
13596 class
13597 struct
13598 union
13599
13600 Returns the kind of class-key specified, or none_type to indicate
13601 error. */
13602
13603 static enum tag_types
13604 cp_parser_class_key (cp_parser* parser)
13605 {
13606 cp_token *token;
13607 enum tag_types tag_type;
13608
13609 /* Look for the class-key. */
13610 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13611 if (!token)
13612 return none_type;
13613
13614 /* Check to see if the TOKEN is a class-key. */
13615 tag_type = cp_parser_token_is_class_key (token);
13616 if (!tag_type)
13617 cp_parser_error (parser, "expected class-key");
13618 return tag_type;
13619 }
13620
13621 /* Parse an (optional) member-specification.
13622
13623 member-specification:
13624 member-declaration member-specification [opt]
13625 access-specifier : member-specification [opt] */
13626
13627 static void
13628 cp_parser_member_specification_opt (cp_parser* parser)
13629 {
13630 while (true)
13631 {
13632 cp_token *token;
13633 enum rid keyword;
13634
13635 /* Peek at the next token. */
13636 token = cp_lexer_peek_token (parser->lexer);
13637 /* If it's a `}', or EOF then we've seen all the members. */
13638 if (token->type == CPP_CLOSE_BRACE
13639 || token->type == CPP_EOF
13640 || token->type == CPP_PRAGMA_EOL)
13641 break;
13642
13643 /* See if this token is a keyword. */
13644 keyword = token->keyword;
13645 switch (keyword)
13646 {
13647 case RID_PUBLIC:
13648 case RID_PROTECTED:
13649 case RID_PRIVATE:
13650 /* Consume the access-specifier. */
13651 cp_lexer_consume_token (parser->lexer);
13652 /* Remember which access-specifier is active. */
13653 current_access_specifier = token->value;
13654 /* Look for the `:'. */
13655 cp_parser_require (parser, CPP_COLON, "`:'");
13656 break;
13657
13658 default:
13659 /* Accept #pragmas at class scope. */
13660 if (token->type == CPP_PRAGMA)
13661 {
13662 cp_parser_pragma (parser, pragma_external);
13663 break;
13664 }
13665
13666 /* Otherwise, the next construction must be a
13667 member-declaration. */
13668 cp_parser_member_declaration (parser);
13669 }
13670 }
13671 }
13672
13673 /* Parse a member-declaration.
13674
13675 member-declaration:
13676 decl-specifier-seq [opt] member-declarator-list [opt] ;
13677 function-definition ; [opt]
13678 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13679 using-declaration
13680 template-declaration
13681
13682 member-declarator-list:
13683 member-declarator
13684 member-declarator-list , member-declarator
13685
13686 member-declarator:
13687 declarator pure-specifier [opt]
13688 declarator constant-initializer [opt]
13689 identifier [opt] : constant-expression
13690
13691 GNU Extensions:
13692
13693 member-declaration:
13694 __extension__ member-declaration
13695
13696 member-declarator:
13697 declarator attributes [opt] pure-specifier [opt]
13698 declarator attributes [opt] constant-initializer [opt]
13699 identifier [opt] attributes [opt] : constant-expression
13700
13701 C++0x Extensions:
13702
13703 member-declaration:
13704 static_assert-declaration */
13705
13706 static void
13707 cp_parser_member_declaration (cp_parser* parser)
13708 {
13709 cp_decl_specifier_seq decl_specifiers;
13710 tree prefix_attributes;
13711 tree decl;
13712 int declares_class_or_enum;
13713 bool friend_p;
13714 cp_token *token;
13715 int saved_pedantic;
13716
13717 /* Check for the `__extension__' keyword. */
13718 if (cp_parser_extension_opt (parser, &saved_pedantic))
13719 {
13720 /* Recurse. */
13721 cp_parser_member_declaration (parser);
13722 /* Restore the old value of the PEDANTIC flag. */
13723 pedantic = saved_pedantic;
13724
13725 return;
13726 }
13727
13728 /* Check for a template-declaration. */
13729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13730 {
13731 /* An explicit specialization here is an error condition, and we
13732 expect the specialization handler to detect and report this. */
13733 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13734 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13735 cp_parser_explicit_specialization (parser);
13736 else
13737 cp_parser_template_declaration (parser, /*member_p=*/true);
13738
13739 return;
13740 }
13741
13742 /* Check for a using-declaration. */
13743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13744 {
13745 /* Parse the using-declaration. */
13746 cp_parser_using_declaration (parser,
13747 /*access_declaration_p=*/false);
13748 return;
13749 }
13750
13751 /* Check for @defs. */
13752 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13753 {
13754 tree ivar, member;
13755 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13756 ivar = ivar_chains;
13757 while (ivar)
13758 {
13759 member = ivar;
13760 ivar = TREE_CHAIN (member);
13761 TREE_CHAIN (member) = NULL_TREE;
13762 finish_member_declaration (member);
13763 }
13764 return;
13765 }
13766
13767 /* If the next token is `static_assert' we have a static assertion. */
13768 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13769 {
13770 cp_parser_static_assert (parser, /*member_p=*/true);
13771 return;
13772 }
13773
13774 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13775 return;
13776
13777 /* Parse the decl-specifier-seq. */
13778 cp_parser_decl_specifier_seq (parser,
13779 CP_PARSER_FLAGS_OPTIONAL,
13780 &decl_specifiers,
13781 &declares_class_or_enum);
13782 prefix_attributes = decl_specifiers.attributes;
13783 decl_specifiers.attributes = NULL_TREE;
13784 /* Check for an invalid type-name. */
13785 if (!decl_specifiers.type
13786 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13787 return;
13788 /* If there is no declarator, then the decl-specifier-seq should
13789 specify a type. */
13790 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13791 {
13792 /* If there was no decl-specifier-seq, and the next token is a
13793 `;', then we have something like:
13794
13795 struct S { ; };
13796
13797 [class.mem]
13798
13799 Each member-declaration shall declare at least one member
13800 name of the class. */
13801 if (!decl_specifiers.any_specifiers_p)
13802 {
13803 cp_token *token = cp_lexer_peek_token (parser->lexer);
13804 if (pedantic && !token->in_system_header)
13805 pedwarn ("%Hextra %<;%>", &token->location);
13806 }
13807 else
13808 {
13809 tree type;
13810
13811 /* See if this declaration is a friend. */
13812 friend_p = cp_parser_friend_p (&decl_specifiers);
13813 /* If there were decl-specifiers, check to see if there was
13814 a class-declaration. */
13815 type = check_tag_decl (&decl_specifiers);
13816 /* Nested classes have already been added to the class, but
13817 a `friend' needs to be explicitly registered. */
13818 if (friend_p)
13819 {
13820 /* If the `friend' keyword was present, the friend must
13821 be introduced with a class-key. */
13822 if (!declares_class_or_enum)
13823 error ("a class-key must be used when declaring a friend");
13824 /* In this case:
13825
13826 template <typename T> struct A {
13827 friend struct A<T>::B;
13828 };
13829
13830 A<T>::B will be represented by a TYPENAME_TYPE, and
13831 therefore not recognized by check_tag_decl. */
13832 if (!type
13833 && decl_specifiers.type
13834 && TYPE_P (decl_specifiers.type))
13835 type = decl_specifiers.type;
13836 if (!type || !TYPE_P (type))
13837 error ("friend declaration does not name a class or "
13838 "function");
13839 else
13840 make_friend_class (current_class_type, type,
13841 /*complain=*/true);
13842 }
13843 /* If there is no TYPE, an error message will already have
13844 been issued. */
13845 else if (!type || type == error_mark_node)
13846 ;
13847 /* An anonymous aggregate has to be handled specially; such
13848 a declaration really declares a data member (with a
13849 particular type), as opposed to a nested class. */
13850 else if (ANON_AGGR_TYPE_P (type))
13851 {
13852 /* Remove constructors and such from TYPE, now that we
13853 know it is an anonymous aggregate. */
13854 fixup_anonymous_aggr (type);
13855 /* And make the corresponding data member. */
13856 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13857 /* Add it to the class. */
13858 finish_member_declaration (decl);
13859 }
13860 else
13861 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13862 }
13863 }
13864 else
13865 {
13866 /* See if these declarations will be friends. */
13867 friend_p = cp_parser_friend_p (&decl_specifiers);
13868
13869 /* Keep going until we hit the `;' at the end of the
13870 declaration. */
13871 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13872 {
13873 tree attributes = NULL_TREE;
13874 tree first_attribute;
13875
13876 /* Peek at the next token. */
13877 token = cp_lexer_peek_token (parser->lexer);
13878
13879 /* Check for a bitfield declaration. */
13880 if (token->type == CPP_COLON
13881 || (token->type == CPP_NAME
13882 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13883 == CPP_COLON))
13884 {
13885 tree identifier;
13886 tree width;
13887
13888 /* Get the name of the bitfield. Note that we cannot just
13889 check TOKEN here because it may have been invalidated by
13890 the call to cp_lexer_peek_nth_token above. */
13891 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13892 identifier = cp_parser_identifier (parser);
13893 else
13894 identifier = NULL_TREE;
13895
13896 /* Consume the `:' token. */
13897 cp_lexer_consume_token (parser->lexer);
13898 /* Get the width of the bitfield. */
13899 width
13900 = cp_parser_constant_expression (parser,
13901 /*allow_non_constant=*/false,
13902 NULL);
13903
13904 /* Look for attributes that apply to the bitfield. */
13905 attributes = cp_parser_attributes_opt (parser);
13906 /* Remember which attributes are prefix attributes and
13907 which are not. */
13908 first_attribute = attributes;
13909 /* Combine the attributes. */
13910 attributes = chainon (prefix_attributes, attributes);
13911
13912 /* Create the bitfield declaration. */
13913 decl = grokbitfield (identifier
13914 ? make_id_declarator (NULL_TREE,
13915 identifier,
13916 sfk_none)
13917 : NULL,
13918 &decl_specifiers,
13919 width);
13920 /* Apply the attributes. */
13921 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13922 }
13923 else
13924 {
13925 cp_declarator *declarator;
13926 tree initializer;
13927 tree asm_specification;
13928 int ctor_dtor_or_conv_p;
13929
13930 /* Parse the declarator. */
13931 declarator
13932 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13933 &ctor_dtor_or_conv_p,
13934 /*parenthesized_p=*/NULL,
13935 /*member_p=*/true);
13936
13937 /* If something went wrong parsing the declarator, make sure
13938 that we at least consume some tokens. */
13939 if (declarator == cp_error_declarator)
13940 {
13941 /* Skip to the end of the statement. */
13942 cp_parser_skip_to_end_of_statement (parser);
13943 /* If the next token is not a semicolon, that is
13944 probably because we just skipped over the body of
13945 a function. So, we consume a semicolon if
13946 present, but do not issue an error message if it
13947 is not present. */
13948 if (cp_lexer_next_token_is (parser->lexer,
13949 CPP_SEMICOLON))
13950 cp_lexer_consume_token (parser->lexer);
13951 return;
13952 }
13953
13954 if (declares_class_or_enum & 2)
13955 cp_parser_check_for_definition_in_return_type
13956 (declarator, decl_specifiers.type);
13957
13958 /* Look for an asm-specification. */
13959 asm_specification = cp_parser_asm_specification_opt (parser);
13960 /* Look for attributes that apply to the declaration. */
13961 attributes = cp_parser_attributes_opt (parser);
13962 /* Remember which attributes are prefix attributes and
13963 which are not. */
13964 first_attribute = attributes;
13965 /* Combine the attributes. */
13966 attributes = chainon (prefix_attributes, attributes);
13967
13968 /* If it's an `=', then we have a constant-initializer or a
13969 pure-specifier. It is not correct to parse the
13970 initializer before registering the member declaration
13971 since the member declaration should be in scope while
13972 its initializer is processed. However, the rest of the
13973 front end does not yet provide an interface that allows
13974 us to handle this correctly. */
13975 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13976 {
13977 /* In [class.mem]:
13978
13979 A pure-specifier shall be used only in the declaration of
13980 a virtual function.
13981
13982 A member-declarator can contain a constant-initializer
13983 only if it declares a static member of integral or
13984 enumeration type.
13985
13986 Therefore, if the DECLARATOR is for a function, we look
13987 for a pure-specifier; otherwise, we look for a
13988 constant-initializer. When we call `grokfield', it will
13989 perform more stringent semantics checks. */
13990 if (function_declarator_p (declarator))
13991 initializer = cp_parser_pure_specifier (parser);
13992 else
13993 /* Parse the initializer. */
13994 initializer = cp_parser_constant_initializer (parser);
13995 }
13996 /* Otherwise, there is no initializer. */
13997 else
13998 initializer = NULL_TREE;
13999
14000 /* See if we are probably looking at a function
14001 definition. We are certainly not looking at a
14002 member-declarator. Calling `grokfield' has
14003 side-effects, so we must not do it unless we are sure
14004 that we are looking at a member-declarator. */
14005 if (cp_parser_token_starts_function_definition_p
14006 (cp_lexer_peek_token (parser->lexer)))
14007 {
14008 /* The grammar does not allow a pure-specifier to be
14009 used when a member function is defined. (It is
14010 possible that this fact is an oversight in the
14011 standard, since a pure function may be defined
14012 outside of the class-specifier. */
14013 if (initializer)
14014 error ("pure-specifier on function-definition");
14015 decl = cp_parser_save_member_function_body (parser,
14016 &decl_specifiers,
14017 declarator,
14018 attributes);
14019 /* If the member was not a friend, declare it here. */
14020 if (!friend_p)
14021 finish_member_declaration (decl);
14022 /* Peek at the next token. */
14023 token = cp_lexer_peek_token (parser->lexer);
14024 /* If the next token is a semicolon, consume it. */
14025 if (token->type == CPP_SEMICOLON)
14026 cp_lexer_consume_token (parser->lexer);
14027 return;
14028 }
14029 else
14030 /* Create the declaration. */
14031 decl = grokfield (declarator, &decl_specifiers,
14032 initializer, /*init_const_expr_p=*/true,
14033 asm_specification,
14034 attributes);
14035 }
14036
14037 /* Reset PREFIX_ATTRIBUTES. */
14038 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14039 attributes = TREE_CHAIN (attributes);
14040 if (attributes)
14041 TREE_CHAIN (attributes) = NULL_TREE;
14042
14043 /* If there is any qualification still in effect, clear it
14044 now; we will be starting fresh with the next declarator. */
14045 parser->scope = NULL_TREE;
14046 parser->qualifying_scope = NULL_TREE;
14047 parser->object_scope = NULL_TREE;
14048 /* If it's a `,', then there are more declarators. */
14049 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14050 cp_lexer_consume_token (parser->lexer);
14051 /* If the next token isn't a `;', then we have a parse error. */
14052 else if (cp_lexer_next_token_is_not (parser->lexer,
14053 CPP_SEMICOLON))
14054 {
14055 cp_parser_error (parser, "expected %<;%>");
14056 /* Skip tokens until we find a `;'. */
14057 cp_parser_skip_to_end_of_statement (parser);
14058
14059 break;
14060 }
14061
14062 if (decl)
14063 {
14064 /* Add DECL to the list of members. */
14065 if (!friend_p)
14066 finish_member_declaration (decl);
14067
14068 if (TREE_CODE (decl) == FUNCTION_DECL)
14069 cp_parser_save_default_args (parser, decl);
14070 }
14071 }
14072 }
14073
14074 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14075 }
14076
14077 /* Parse a pure-specifier.
14078
14079 pure-specifier:
14080 = 0
14081
14082 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14083 Otherwise, ERROR_MARK_NODE is returned. */
14084
14085 static tree
14086 cp_parser_pure_specifier (cp_parser* parser)
14087 {
14088 cp_token *token;
14089
14090 /* Look for the `=' token. */
14091 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14092 return error_mark_node;
14093 /* Look for the `0' token. */
14094 token = cp_lexer_consume_token (parser->lexer);
14095 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14096 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14097 {
14098 cp_parser_error (parser,
14099 "invalid pure specifier (only `= 0' is allowed)");
14100 cp_parser_skip_to_end_of_statement (parser);
14101 return error_mark_node;
14102 }
14103 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14104 {
14105 error ("templates may not be %<virtual%>");
14106 return error_mark_node;
14107 }
14108
14109 return integer_zero_node;
14110 }
14111
14112 /* Parse a constant-initializer.
14113
14114 constant-initializer:
14115 = constant-expression
14116
14117 Returns a representation of the constant-expression. */
14118
14119 static tree
14120 cp_parser_constant_initializer (cp_parser* parser)
14121 {
14122 /* Look for the `=' token. */
14123 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14124 return error_mark_node;
14125
14126 /* It is invalid to write:
14127
14128 struct S { static const int i = { 7 }; };
14129
14130 */
14131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14132 {
14133 cp_parser_error (parser,
14134 "a brace-enclosed initializer is not allowed here");
14135 /* Consume the opening brace. */
14136 cp_lexer_consume_token (parser->lexer);
14137 /* Skip the initializer. */
14138 cp_parser_skip_to_closing_brace (parser);
14139 /* Look for the trailing `}'. */
14140 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14141
14142 return error_mark_node;
14143 }
14144
14145 return cp_parser_constant_expression (parser,
14146 /*allow_non_constant=*/false,
14147 NULL);
14148 }
14149
14150 /* Derived classes [gram.class.derived] */
14151
14152 /* Parse a base-clause.
14153
14154 base-clause:
14155 : base-specifier-list
14156
14157 base-specifier-list:
14158 base-specifier
14159 base-specifier-list , base-specifier
14160
14161 Returns a TREE_LIST representing the base-classes, in the order in
14162 which they were declared. The representation of each node is as
14163 described by cp_parser_base_specifier.
14164
14165 In the case that no bases are specified, this function will return
14166 NULL_TREE, not ERROR_MARK_NODE. */
14167
14168 static tree
14169 cp_parser_base_clause (cp_parser* parser)
14170 {
14171 tree bases = NULL_TREE;
14172
14173 /* Look for the `:' that begins the list. */
14174 cp_parser_require (parser, CPP_COLON, "`:'");
14175
14176 /* Scan the base-specifier-list. */
14177 while (true)
14178 {
14179 cp_token *token;
14180 tree base;
14181
14182 /* Look for the base-specifier. */
14183 base = cp_parser_base_specifier (parser);
14184 /* Add BASE to the front of the list. */
14185 if (base != error_mark_node)
14186 {
14187 TREE_CHAIN (base) = bases;
14188 bases = base;
14189 }
14190 /* Peek at the next token. */
14191 token = cp_lexer_peek_token (parser->lexer);
14192 /* If it's not a comma, then the list is complete. */
14193 if (token->type != CPP_COMMA)
14194 break;
14195 /* Consume the `,'. */
14196 cp_lexer_consume_token (parser->lexer);
14197 }
14198
14199 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14200 base class had a qualified name. However, the next name that
14201 appears is certainly not qualified. */
14202 parser->scope = NULL_TREE;
14203 parser->qualifying_scope = NULL_TREE;
14204 parser->object_scope = NULL_TREE;
14205
14206 return nreverse (bases);
14207 }
14208
14209 /* Parse a base-specifier.
14210
14211 base-specifier:
14212 :: [opt] nested-name-specifier [opt] class-name
14213 virtual access-specifier [opt] :: [opt] nested-name-specifier
14214 [opt] class-name
14215 access-specifier virtual [opt] :: [opt] nested-name-specifier
14216 [opt] class-name
14217
14218 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14219 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14220 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14221 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14222
14223 static tree
14224 cp_parser_base_specifier (cp_parser* parser)
14225 {
14226 cp_token *token;
14227 bool done = false;
14228 bool virtual_p = false;
14229 bool duplicate_virtual_error_issued_p = false;
14230 bool duplicate_access_error_issued_p = false;
14231 bool class_scope_p, template_p;
14232 tree access = access_default_node;
14233 tree type;
14234
14235 /* Process the optional `virtual' and `access-specifier'. */
14236 while (!done)
14237 {
14238 /* Peek at the next token. */
14239 token = cp_lexer_peek_token (parser->lexer);
14240 /* Process `virtual'. */
14241 switch (token->keyword)
14242 {
14243 case RID_VIRTUAL:
14244 /* If `virtual' appears more than once, issue an error. */
14245 if (virtual_p && !duplicate_virtual_error_issued_p)
14246 {
14247 cp_parser_error (parser,
14248 "%<virtual%> specified more than once in base-specified");
14249 duplicate_virtual_error_issued_p = true;
14250 }
14251
14252 virtual_p = true;
14253
14254 /* Consume the `virtual' token. */
14255 cp_lexer_consume_token (parser->lexer);
14256
14257 break;
14258
14259 case RID_PUBLIC:
14260 case RID_PROTECTED:
14261 case RID_PRIVATE:
14262 /* If more than one access specifier appears, issue an
14263 error. */
14264 if (access != access_default_node
14265 && !duplicate_access_error_issued_p)
14266 {
14267 cp_parser_error (parser,
14268 "more than one access specifier in base-specified");
14269 duplicate_access_error_issued_p = true;
14270 }
14271
14272 access = ridpointers[(int) token->keyword];
14273
14274 /* Consume the access-specifier. */
14275 cp_lexer_consume_token (parser->lexer);
14276
14277 break;
14278
14279 default:
14280 done = true;
14281 break;
14282 }
14283 }
14284 /* It is not uncommon to see programs mechanically, erroneously, use
14285 the 'typename' keyword to denote (dependent) qualified types
14286 as base classes. */
14287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14288 {
14289 if (!processing_template_decl)
14290 error ("keyword %<typename%> not allowed outside of templates");
14291 else
14292 error ("keyword %<typename%> not allowed in this context "
14293 "(the base class is implicitly a type)");
14294 cp_lexer_consume_token (parser->lexer);
14295 }
14296
14297 /* Look for the optional `::' operator. */
14298 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14299 /* Look for the nested-name-specifier. The simplest way to
14300 implement:
14301
14302 [temp.res]
14303
14304 The keyword `typename' is not permitted in a base-specifier or
14305 mem-initializer; in these contexts a qualified name that
14306 depends on a template-parameter is implicitly assumed to be a
14307 type name.
14308
14309 is to pretend that we have seen the `typename' keyword at this
14310 point. */
14311 cp_parser_nested_name_specifier_opt (parser,
14312 /*typename_keyword_p=*/true,
14313 /*check_dependency_p=*/true,
14314 typename_type,
14315 /*is_declaration=*/true);
14316 /* If the base class is given by a qualified name, assume that names
14317 we see are type names or templates, as appropriate. */
14318 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14319 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14320
14321 /* Finally, look for the class-name. */
14322 type = cp_parser_class_name (parser,
14323 class_scope_p,
14324 template_p,
14325 typename_type,
14326 /*check_dependency_p=*/true,
14327 /*class_head_p=*/false,
14328 /*is_declaration=*/true);
14329
14330 if (type == error_mark_node)
14331 return error_mark_node;
14332
14333 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14334 }
14335
14336 /* Exception handling [gram.exception] */
14337
14338 /* Parse an (optional) exception-specification.
14339
14340 exception-specification:
14341 throw ( type-id-list [opt] )
14342
14343 Returns a TREE_LIST representing the exception-specification. The
14344 TREE_VALUE of each node is a type. */
14345
14346 static tree
14347 cp_parser_exception_specification_opt (cp_parser* parser)
14348 {
14349 cp_token *token;
14350 tree type_id_list;
14351
14352 /* Peek at the next token. */
14353 token = cp_lexer_peek_token (parser->lexer);
14354 /* If it's not `throw', then there's no exception-specification. */
14355 if (!cp_parser_is_keyword (token, RID_THROW))
14356 return NULL_TREE;
14357
14358 /* Consume the `throw'. */
14359 cp_lexer_consume_token (parser->lexer);
14360
14361 /* Look for the `('. */
14362 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14363
14364 /* Peek at the next token. */
14365 token = cp_lexer_peek_token (parser->lexer);
14366 /* If it's not a `)', then there is a type-id-list. */
14367 if (token->type != CPP_CLOSE_PAREN)
14368 {
14369 const char *saved_message;
14370
14371 /* Types may not be defined in an exception-specification. */
14372 saved_message = parser->type_definition_forbidden_message;
14373 parser->type_definition_forbidden_message
14374 = "types may not be defined in an exception-specification";
14375 /* Parse the type-id-list. */
14376 type_id_list = cp_parser_type_id_list (parser);
14377 /* Restore the saved message. */
14378 parser->type_definition_forbidden_message = saved_message;
14379 }
14380 else
14381 type_id_list = empty_except_spec;
14382
14383 /* Look for the `)'. */
14384 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14385
14386 return type_id_list;
14387 }
14388
14389 /* Parse an (optional) type-id-list.
14390
14391 type-id-list:
14392 type-id
14393 type-id-list , type-id
14394
14395 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14396 in the order that the types were presented. */
14397
14398 static tree
14399 cp_parser_type_id_list (cp_parser* parser)
14400 {
14401 tree types = NULL_TREE;
14402
14403 while (true)
14404 {
14405 cp_token *token;
14406 tree type;
14407
14408 /* Get the next type-id. */
14409 type = cp_parser_type_id (parser);
14410 /* Add it to the list. */
14411 types = add_exception_specifier (types, type, /*complain=*/1);
14412 /* Peek at the next token. */
14413 token = cp_lexer_peek_token (parser->lexer);
14414 /* If it is not a `,', we are done. */
14415 if (token->type != CPP_COMMA)
14416 break;
14417 /* Consume the `,'. */
14418 cp_lexer_consume_token (parser->lexer);
14419 }
14420
14421 return nreverse (types);
14422 }
14423
14424 /* Parse a try-block.
14425
14426 try-block:
14427 try compound-statement handler-seq */
14428
14429 static tree
14430 cp_parser_try_block (cp_parser* parser)
14431 {
14432 tree try_block;
14433
14434 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14435 try_block = begin_try_block ();
14436 cp_parser_compound_statement (parser, NULL, true);
14437 finish_try_block (try_block);
14438 cp_parser_handler_seq (parser);
14439 finish_handler_sequence (try_block);
14440
14441 return try_block;
14442 }
14443
14444 /* Parse a function-try-block.
14445
14446 function-try-block:
14447 try ctor-initializer [opt] function-body handler-seq */
14448
14449 static bool
14450 cp_parser_function_try_block (cp_parser* parser)
14451 {
14452 tree compound_stmt;
14453 tree try_block;
14454 bool ctor_initializer_p;
14455
14456 /* Look for the `try' keyword. */
14457 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14458 return false;
14459 /* Let the rest of the front-end know where we are. */
14460 try_block = begin_function_try_block (&compound_stmt);
14461 /* Parse the function-body. */
14462 ctor_initializer_p
14463 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14464 /* We're done with the `try' part. */
14465 finish_function_try_block (try_block);
14466 /* Parse the handlers. */
14467 cp_parser_handler_seq (parser);
14468 /* We're done with the handlers. */
14469 finish_function_handler_sequence (try_block, compound_stmt);
14470
14471 return ctor_initializer_p;
14472 }
14473
14474 /* Parse a handler-seq.
14475
14476 handler-seq:
14477 handler handler-seq [opt] */
14478
14479 static void
14480 cp_parser_handler_seq (cp_parser* parser)
14481 {
14482 while (true)
14483 {
14484 cp_token *token;
14485
14486 /* Parse the handler. */
14487 cp_parser_handler (parser);
14488 /* Peek at the next token. */
14489 token = cp_lexer_peek_token (parser->lexer);
14490 /* If it's not `catch' then there are no more handlers. */
14491 if (!cp_parser_is_keyword (token, RID_CATCH))
14492 break;
14493 }
14494 }
14495
14496 /* Parse a handler.
14497
14498 handler:
14499 catch ( exception-declaration ) compound-statement */
14500
14501 static void
14502 cp_parser_handler (cp_parser* parser)
14503 {
14504 tree handler;
14505 tree declaration;
14506
14507 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14508 handler = begin_handler ();
14509 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14510 declaration = cp_parser_exception_declaration (parser);
14511 finish_handler_parms (declaration, handler);
14512 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14513 cp_parser_compound_statement (parser, NULL, false);
14514 finish_handler (handler);
14515 }
14516
14517 /* Parse an exception-declaration.
14518
14519 exception-declaration:
14520 type-specifier-seq declarator
14521 type-specifier-seq abstract-declarator
14522 type-specifier-seq
14523 ...
14524
14525 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14526 ellipsis variant is used. */
14527
14528 static tree
14529 cp_parser_exception_declaration (cp_parser* parser)
14530 {
14531 cp_decl_specifier_seq type_specifiers;
14532 cp_declarator *declarator;
14533 const char *saved_message;
14534
14535 /* If it's an ellipsis, it's easy to handle. */
14536 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14537 {
14538 /* Consume the `...' token. */
14539 cp_lexer_consume_token (parser->lexer);
14540 return NULL_TREE;
14541 }
14542
14543 /* Types may not be defined in exception-declarations. */
14544 saved_message = parser->type_definition_forbidden_message;
14545 parser->type_definition_forbidden_message
14546 = "types may not be defined in exception-declarations";
14547
14548 /* Parse the type-specifier-seq. */
14549 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14550 &type_specifiers);
14551 /* If it's a `)', then there is no declarator. */
14552 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14553 declarator = NULL;
14554 else
14555 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14556 /*ctor_dtor_or_conv_p=*/NULL,
14557 /*parenthesized_p=*/NULL,
14558 /*member_p=*/false);
14559
14560 /* Restore the saved message. */
14561 parser->type_definition_forbidden_message = saved_message;
14562
14563 if (!type_specifiers.any_specifiers_p)
14564 return error_mark_node;
14565
14566 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14567 }
14568
14569 /* Parse a throw-expression.
14570
14571 throw-expression:
14572 throw assignment-expression [opt]
14573
14574 Returns a THROW_EXPR representing the throw-expression. */
14575
14576 static tree
14577 cp_parser_throw_expression (cp_parser* parser)
14578 {
14579 tree expression;
14580 cp_token* token;
14581
14582 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14583 token = cp_lexer_peek_token (parser->lexer);
14584 /* Figure out whether or not there is an assignment-expression
14585 following the "throw" keyword. */
14586 if (token->type == CPP_COMMA
14587 || token->type == CPP_SEMICOLON
14588 || token->type == CPP_CLOSE_PAREN
14589 || token->type == CPP_CLOSE_SQUARE
14590 || token->type == CPP_CLOSE_BRACE
14591 || token->type == CPP_COLON)
14592 expression = NULL_TREE;
14593 else
14594 expression = cp_parser_assignment_expression (parser,
14595 /*cast_p=*/false);
14596
14597 return build_throw (expression);
14598 }
14599
14600 /* GNU Extensions */
14601
14602 /* Parse an (optional) asm-specification.
14603
14604 asm-specification:
14605 asm ( string-literal )
14606
14607 If the asm-specification is present, returns a STRING_CST
14608 corresponding to the string-literal. Otherwise, returns
14609 NULL_TREE. */
14610
14611 static tree
14612 cp_parser_asm_specification_opt (cp_parser* parser)
14613 {
14614 cp_token *token;
14615 tree asm_specification;
14616
14617 /* Peek at the next token. */
14618 token = cp_lexer_peek_token (parser->lexer);
14619 /* If the next token isn't the `asm' keyword, then there's no
14620 asm-specification. */
14621 if (!cp_parser_is_keyword (token, RID_ASM))
14622 return NULL_TREE;
14623
14624 /* Consume the `asm' token. */
14625 cp_lexer_consume_token (parser->lexer);
14626 /* Look for the `('. */
14627 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14628
14629 /* Look for the string-literal. */
14630 asm_specification = cp_parser_string_literal (parser, false, false);
14631
14632 /* Look for the `)'. */
14633 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14634
14635 return asm_specification;
14636 }
14637
14638 /* Parse an asm-operand-list.
14639
14640 asm-operand-list:
14641 asm-operand
14642 asm-operand-list , asm-operand
14643
14644 asm-operand:
14645 string-literal ( expression )
14646 [ string-literal ] string-literal ( expression )
14647
14648 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14649 each node is the expression. The TREE_PURPOSE is itself a
14650 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14651 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14652 is a STRING_CST for the string literal before the parenthesis. */
14653
14654 static tree
14655 cp_parser_asm_operand_list (cp_parser* parser)
14656 {
14657 tree asm_operands = NULL_TREE;
14658
14659 while (true)
14660 {
14661 tree string_literal;
14662 tree expression;
14663 tree name;
14664
14665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14666 {
14667 /* Consume the `[' token. */
14668 cp_lexer_consume_token (parser->lexer);
14669 /* Read the operand name. */
14670 name = cp_parser_identifier (parser);
14671 if (name != error_mark_node)
14672 name = build_string (IDENTIFIER_LENGTH (name),
14673 IDENTIFIER_POINTER (name));
14674 /* Look for the closing `]'. */
14675 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14676 }
14677 else
14678 name = NULL_TREE;
14679 /* Look for the string-literal. */
14680 string_literal = cp_parser_string_literal (parser, false, false);
14681
14682 /* Look for the `('. */
14683 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14684 /* Parse the expression. */
14685 expression = cp_parser_expression (parser, /*cast_p=*/false);
14686 /* Look for the `)'. */
14687 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14688
14689 /* Add this operand to the list. */
14690 asm_operands = tree_cons (build_tree_list (name, string_literal),
14691 expression,
14692 asm_operands);
14693 /* If the next token is not a `,', there are no more
14694 operands. */
14695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14696 break;
14697 /* Consume the `,'. */
14698 cp_lexer_consume_token (parser->lexer);
14699 }
14700
14701 return nreverse (asm_operands);
14702 }
14703
14704 /* Parse an asm-clobber-list.
14705
14706 asm-clobber-list:
14707 string-literal
14708 asm-clobber-list , string-literal
14709
14710 Returns a TREE_LIST, indicating the clobbers in the order that they
14711 appeared. The TREE_VALUE of each node is a STRING_CST. */
14712
14713 static tree
14714 cp_parser_asm_clobber_list (cp_parser* parser)
14715 {
14716 tree clobbers = NULL_TREE;
14717
14718 while (true)
14719 {
14720 tree string_literal;
14721
14722 /* Look for the string literal. */
14723 string_literal = cp_parser_string_literal (parser, false, false);
14724 /* Add it to the list. */
14725 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14726 /* If the next token is not a `,', then the list is
14727 complete. */
14728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14729 break;
14730 /* Consume the `,' token. */
14731 cp_lexer_consume_token (parser->lexer);
14732 }
14733
14734 return clobbers;
14735 }
14736
14737 /* Parse an (optional) series of attributes.
14738
14739 attributes:
14740 attributes attribute
14741
14742 attribute:
14743 __attribute__ (( attribute-list [opt] ))
14744
14745 The return value is as for cp_parser_attribute_list. */
14746
14747 static tree
14748 cp_parser_attributes_opt (cp_parser* parser)
14749 {
14750 tree attributes = NULL_TREE;
14751
14752 while (true)
14753 {
14754 cp_token *token;
14755 tree attribute_list;
14756
14757 /* Peek at the next token. */
14758 token = cp_lexer_peek_token (parser->lexer);
14759 /* If it's not `__attribute__', then we're done. */
14760 if (token->keyword != RID_ATTRIBUTE)
14761 break;
14762
14763 /* Consume the `__attribute__' keyword. */
14764 cp_lexer_consume_token (parser->lexer);
14765 /* Look for the two `(' tokens. */
14766 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14767 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14768
14769 /* Peek at the next token. */
14770 token = cp_lexer_peek_token (parser->lexer);
14771 if (token->type != CPP_CLOSE_PAREN)
14772 /* Parse the attribute-list. */
14773 attribute_list = cp_parser_attribute_list (parser);
14774 else
14775 /* If the next token is a `)', then there is no attribute
14776 list. */
14777 attribute_list = NULL;
14778
14779 /* Look for the two `)' tokens. */
14780 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14781 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14782
14783 /* Add these new attributes to the list. */
14784 attributes = chainon (attributes, attribute_list);
14785 }
14786
14787 return attributes;
14788 }
14789
14790 /* Parse an attribute-list.
14791
14792 attribute-list:
14793 attribute
14794 attribute-list , attribute
14795
14796 attribute:
14797 identifier
14798 identifier ( identifier )
14799 identifier ( identifier , expression-list )
14800 identifier ( expression-list )
14801
14802 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14803 to an attribute. The TREE_PURPOSE of each node is the identifier
14804 indicating which attribute is in use. The TREE_VALUE represents
14805 the arguments, if any. */
14806
14807 static tree
14808 cp_parser_attribute_list (cp_parser* parser)
14809 {
14810 tree attribute_list = NULL_TREE;
14811 bool save_translate_strings_p = parser->translate_strings_p;
14812
14813 parser->translate_strings_p = false;
14814 while (true)
14815 {
14816 cp_token *token;
14817 tree identifier;
14818 tree attribute;
14819
14820 /* Look for the identifier. We also allow keywords here; for
14821 example `__attribute__ ((const))' is legal. */
14822 token = cp_lexer_peek_token (parser->lexer);
14823 if (token->type == CPP_NAME
14824 || token->type == CPP_KEYWORD)
14825 {
14826 tree arguments = NULL_TREE;
14827
14828 /* Consume the token. */
14829 token = cp_lexer_consume_token (parser->lexer);
14830
14831 /* Save away the identifier that indicates which attribute
14832 this is. */
14833 identifier = token->value;
14834 attribute = build_tree_list (identifier, NULL_TREE);
14835
14836 /* Peek at the next token. */
14837 token = cp_lexer_peek_token (parser->lexer);
14838 /* If it's an `(', then parse the attribute arguments. */
14839 if (token->type == CPP_OPEN_PAREN)
14840 {
14841 arguments = cp_parser_parenthesized_expression_list
14842 (parser, true, /*cast_p=*/false,
14843 /*non_constant_p=*/NULL);
14844 /* Save the arguments away. */
14845 TREE_VALUE (attribute) = arguments;
14846 }
14847
14848 if (arguments != error_mark_node)
14849 {
14850 /* Add this attribute to the list. */
14851 TREE_CHAIN (attribute) = attribute_list;
14852 attribute_list = attribute;
14853 }
14854
14855 token = cp_lexer_peek_token (parser->lexer);
14856 }
14857 /* Now, look for more attributes. If the next token isn't a
14858 `,', we're done. */
14859 if (token->type != CPP_COMMA)
14860 break;
14861
14862 /* Consume the comma and keep going. */
14863 cp_lexer_consume_token (parser->lexer);
14864 }
14865 parser->translate_strings_p = save_translate_strings_p;
14866
14867 /* We built up the list in reverse order. */
14868 return nreverse (attribute_list);
14869 }
14870
14871 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14872 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14873 current value of the PEDANTIC flag, regardless of whether or not
14874 the `__extension__' keyword is present. The caller is responsible
14875 for restoring the value of the PEDANTIC flag. */
14876
14877 static bool
14878 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14879 {
14880 /* Save the old value of the PEDANTIC flag. */
14881 *saved_pedantic = pedantic;
14882
14883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14884 {
14885 /* Consume the `__extension__' token. */
14886 cp_lexer_consume_token (parser->lexer);
14887 /* We're not being pedantic while the `__extension__' keyword is
14888 in effect. */
14889 pedantic = 0;
14890
14891 return true;
14892 }
14893
14894 return false;
14895 }
14896
14897 /* Parse a label declaration.
14898
14899 label-declaration:
14900 __label__ label-declarator-seq ;
14901
14902 label-declarator-seq:
14903 identifier , label-declarator-seq
14904 identifier */
14905
14906 static void
14907 cp_parser_label_declaration (cp_parser* parser)
14908 {
14909 /* Look for the `__label__' keyword. */
14910 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14911
14912 while (true)
14913 {
14914 tree identifier;
14915
14916 /* Look for an identifier. */
14917 identifier = cp_parser_identifier (parser);
14918 /* If we failed, stop. */
14919 if (identifier == error_mark_node)
14920 break;
14921 /* Declare it as a label. */
14922 finish_label_decl (identifier);
14923 /* If the next token is a `;', stop. */
14924 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14925 break;
14926 /* Look for the `,' separating the label declarations. */
14927 cp_parser_require (parser, CPP_COMMA, "`,'");
14928 }
14929
14930 /* Look for the final `;'. */
14931 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14932 }
14933
14934 /* Support Functions */
14935
14936 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14937 NAME should have one of the representations used for an
14938 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14939 is returned. If PARSER->SCOPE is a dependent type, then a
14940 SCOPE_REF is returned.
14941
14942 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14943 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14944 was formed. Abstractly, such entities should not be passed to this
14945 function, because they do not need to be looked up, but it is
14946 simpler to check for this special case here, rather than at the
14947 call-sites.
14948
14949 In cases not explicitly covered above, this function returns a
14950 DECL, OVERLOAD, or baselink representing the result of the lookup.
14951 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14952 is returned.
14953
14954 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14955 (e.g., "struct") that was used. In that case bindings that do not
14956 refer to types are ignored.
14957
14958 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14959 ignored.
14960
14961 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14962 are ignored.
14963
14964 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14965 types.
14966
14967 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14968 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14969 NULL_TREE otherwise. */
14970
14971 static tree
14972 cp_parser_lookup_name (cp_parser *parser, tree name,
14973 enum tag_types tag_type,
14974 bool is_template,
14975 bool is_namespace,
14976 bool check_dependency,
14977 tree *ambiguous_decls)
14978 {
14979 int flags = 0;
14980 tree decl;
14981 tree object_type = parser->context->object_type;
14982
14983 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14984 flags |= LOOKUP_COMPLAIN;
14985
14986 /* Assume that the lookup will be unambiguous. */
14987 if (ambiguous_decls)
14988 *ambiguous_decls = NULL_TREE;
14989
14990 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14991 no longer valid. Note that if we are parsing tentatively, and
14992 the parse fails, OBJECT_TYPE will be automatically restored. */
14993 parser->context->object_type = NULL_TREE;
14994
14995 if (name == error_mark_node)
14996 return error_mark_node;
14997
14998 /* A template-id has already been resolved; there is no lookup to
14999 do. */
15000 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15001 return name;
15002 if (BASELINK_P (name))
15003 {
15004 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15005 == TEMPLATE_ID_EXPR);
15006 return name;
15007 }
15008
15009 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15010 it should already have been checked to make sure that the name
15011 used matches the type being destroyed. */
15012 if (TREE_CODE (name) == BIT_NOT_EXPR)
15013 {
15014 tree type;
15015
15016 /* Figure out to which type this destructor applies. */
15017 if (parser->scope)
15018 type = parser->scope;
15019 else if (object_type)
15020 type = object_type;
15021 else
15022 type = current_class_type;
15023 /* If that's not a class type, there is no destructor. */
15024 if (!type || !CLASS_TYPE_P (type))
15025 return error_mark_node;
15026 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15027 lazily_declare_fn (sfk_destructor, type);
15028 if (!CLASSTYPE_DESTRUCTORS (type))
15029 return error_mark_node;
15030 /* If it was a class type, return the destructor. */
15031 return CLASSTYPE_DESTRUCTORS (type);
15032 }
15033
15034 /* By this point, the NAME should be an ordinary identifier. If
15035 the id-expression was a qualified name, the qualifying scope is
15036 stored in PARSER->SCOPE at this point. */
15037 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15038
15039 /* Perform the lookup. */
15040 if (parser->scope)
15041 {
15042 bool dependent_p;
15043
15044 if (parser->scope == error_mark_node)
15045 return error_mark_node;
15046
15047 /* If the SCOPE is dependent, the lookup must be deferred until
15048 the template is instantiated -- unless we are explicitly
15049 looking up names in uninstantiated templates. Even then, we
15050 cannot look up the name if the scope is not a class type; it
15051 might, for example, be a template type parameter. */
15052 dependent_p = (TYPE_P (parser->scope)
15053 && !(parser->in_declarator_p
15054 && currently_open_class (parser->scope))
15055 && dependent_type_p (parser->scope));
15056 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15057 && dependent_p)
15058 {
15059 if (tag_type)
15060 {
15061 tree type;
15062
15063 /* The resolution to Core Issue 180 says that `struct
15064 A::B' should be considered a type-name, even if `A'
15065 is dependent. */
15066 type = make_typename_type (parser->scope, name, tag_type,
15067 /*complain=*/tf_error);
15068 decl = TYPE_NAME (type);
15069 }
15070 else if (is_template
15071 && (cp_parser_next_token_ends_template_argument_p (parser)
15072 || cp_lexer_next_token_is (parser->lexer,
15073 CPP_CLOSE_PAREN)))
15074 decl = make_unbound_class_template (parser->scope,
15075 name, NULL_TREE,
15076 /*complain=*/tf_error);
15077 else
15078 decl = build_qualified_name (/*type=*/NULL_TREE,
15079 parser->scope, name,
15080 is_template);
15081 }
15082 else
15083 {
15084 tree pushed_scope = NULL_TREE;
15085
15086 /* If PARSER->SCOPE is a dependent type, then it must be a
15087 class type, and we must not be checking dependencies;
15088 otherwise, we would have processed this lookup above. So
15089 that PARSER->SCOPE is not considered a dependent base by
15090 lookup_member, we must enter the scope here. */
15091 if (dependent_p)
15092 pushed_scope = push_scope (parser->scope);
15093 /* If the PARSER->SCOPE is a template specialization, it
15094 may be instantiated during name lookup. In that case,
15095 errors may be issued. Even if we rollback the current
15096 tentative parse, those errors are valid. */
15097 decl = lookup_qualified_name (parser->scope, name,
15098 tag_type != none_type,
15099 /*complain=*/true);
15100 if (pushed_scope)
15101 pop_scope (pushed_scope);
15102 }
15103 parser->qualifying_scope = parser->scope;
15104 parser->object_scope = NULL_TREE;
15105 }
15106 else if (object_type)
15107 {
15108 tree object_decl = NULL_TREE;
15109 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15110 OBJECT_TYPE is not a class. */
15111 if (CLASS_TYPE_P (object_type))
15112 /* If the OBJECT_TYPE is a template specialization, it may
15113 be instantiated during name lookup. In that case, errors
15114 may be issued. Even if we rollback the current tentative
15115 parse, those errors are valid. */
15116 object_decl = lookup_member (object_type,
15117 name,
15118 /*protect=*/0,
15119 tag_type != none_type);
15120 /* Look it up in the enclosing context, too. */
15121 decl = lookup_name_real (name, tag_type != none_type,
15122 /*nonclass=*/0,
15123 /*block_p=*/true, is_namespace, flags);
15124 parser->object_scope = object_type;
15125 parser->qualifying_scope = NULL_TREE;
15126 if (object_decl)
15127 decl = object_decl;
15128 }
15129 else
15130 {
15131 decl = lookup_name_real (name, tag_type != none_type,
15132 /*nonclass=*/0,
15133 /*block_p=*/true, is_namespace, flags);
15134 parser->qualifying_scope = NULL_TREE;
15135 parser->object_scope = NULL_TREE;
15136 }
15137
15138 /* If the lookup failed, let our caller know. */
15139 if (!decl || decl == error_mark_node)
15140 return error_mark_node;
15141
15142 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15143 if (TREE_CODE (decl) == TREE_LIST)
15144 {
15145 if (ambiguous_decls)
15146 *ambiguous_decls = decl;
15147 /* The error message we have to print is too complicated for
15148 cp_parser_error, so we incorporate its actions directly. */
15149 if (!cp_parser_simulate_error (parser))
15150 {
15151 error ("reference to %qD is ambiguous", name);
15152 print_candidates (decl);
15153 }
15154 return error_mark_node;
15155 }
15156
15157 gcc_assert (DECL_P (decl)
15158 || TREE_CODE (decl) == OVERLOAD
15159 || TREE_CODE (decl) == SCOPE_REF
15160 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15161 || BASELINK_P (decl));
15162
15163 /* If we have resolved the name of a member declaration, check to
15164 see if the declaration is accessible. When the name resolves to
15165 set of overloaded functions, accessibility is checked when
15166 overload resolution is done.
15167
15168 During an explicit instantiation, access is not checked at all,
15169 as per [temp.explicit]. */
15170 if (DECL_P (decl))
15171 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15172
15173 return decl;
15174 }
15175
15176 /* Like cp_parser_lookup_name, but for use in the typical case where
15177 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15178 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15179
15180 static tree
15181 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15182 {
15183 return cp_parser_lookup_name (parser, name,
15184 none_type,
15185 /*is_template=*/false,
15186 /*is_namespace=*/false,
15187 /*check_dependency=*/true,
15188 /*ambiguous_decls=*/NULL);
15189 }
15190
15191 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15192 the current context, return the TYPE_DECL. If TAG_NAME_P is
15193 true, the DECL indicates the class being defined in a class-head,
15194 or declared in an elaborated-type-specifier.
15195
15196 Otherwise, return DECL. */
15197
15198 static tree
15199 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15200 {
15201 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15202 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15203
15204 struct A {
15205 template <typename T> struct B;
15206 };
15207
15208 template <typename T> struct A::B {};
15209
15210 Similarly, in an elaborated-type-specifier:
15211
15212 namespace N { struct X{}; }
15213
15214 struct A {
15215 template <typename T> friend struct N::X;
15216 };
15217
15218 However, if the DECL refers to a class type, and we are in
15219 the scope of the class, then the name lookup automatically
15220 finds the TYPE_DECL created by build_self_reference rather
15221 than a TEMPLATE_DECL. For example, in:
15222
15223 template <class T> struct S {
15224 S s;
15225 };
15226
15227 there is no need to handle such case. */
15228
15229 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15230 return DECL_TEMPLATE_RESULT (decl);
15231
15232 return decl;
15233 }
15234
15235 /* If too many, or too few, template-parameter lists apply to the
15236 declarator, issue an error message. Returns TRUE if all went well,
15237 and FALSE otherwise. */
15238
15239 static bool
15240 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15241 cp_declarator *declarator)
15242 {
15243 unsigned num_templates;
15244
15245 /* We haven't seen any classes that involve template parameters yet. */
15246 num_templates = 0;
15247
15248 switch (declarator->kind)
15249 {
15250 case cdk_id:
15251 if (declarator->u.id.qualifying_scope)
15252 {
15253 tree scope;
15254 tree member;
15255
15256 scope = declarator->u.id.qualifying_scope;
15257 member = declarator->u.id.unqualified_name;
15258
15259 while (scope && CLASS_TYPE_P (scope))
15260 {
15261 /* You're supposed to have one `template <...>'
15262 for every template class, but you don't need one
15263 for a full specialization. For example:
15264
15265 template <class T> struct S{};
15266 template <> struct S<int> { void f(); };
15267 void S<int>::f () {}
15268
15269 is correct; there shouldn't be a `template <>' for
15270 the definition of `S<int>::f'. */
15271 if (CLASSTYPE_TEMPLATE_INFO (scope)
15272 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15273 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15274 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15275 ++num_templates;
15276
15277 scope = TYPE_CONTEXT (scope);
15278 }
15279 }
15280 else if (TREE_CODE (declarator->u.id.unqualified_name)
15281 == TEMPLATE_ID_EXPR)
15282 /* If the DECLARATOR has the form `X<y>' then it uses one
15283 additional level of template parameters. */
15284 ++num_templates;
15285
15286 return cp_parser_check_template_parameters (parser,
15287 num_templates);
15288
15289 case cdk_function:
15290 case cdk_array:
15291 case cdk_pointer:
15292 case cdk_reference:
15293 case cdk_ptrmem:
15294 return (cp_parser_check_declarator_template_parameters
15295 (parser, declarator->declarator));
15296
15297 case cdk_error:
15298 return true;
15299
15300 default:
15301 gcc_unreachable ();
15302 }
15303 return false;
15304 }
15305
15306 /* NUM_TEMPLATES were used in the current declaration. If that is
15307 invalid, return FALSE and issue an error messages. Otherwise,
15308 return TRUE. */
15309
15310 static bool
15311 cp_parser_check_template_parameters (cp_parser* parser,
15312 unsigned num_templates)
15313 {
15314 /* If there are more template classes than parameter lists, we have
15315 something like:
15316
15317 template <class T> void S<T>::R<T>::f (); */
15318 if (parser->num_template_parameter_lists < num_templates)
15319 {
15320 error ("too few template-parameter-lists");
15321 return false;
15322 }
15323 /* If there are the same number of template classes and parameter
15324 lists, that's OK. */
15325 if (parser->num_template_parameter_lists == num_templates)
15326 return true;
15327 /* If there are more, but only one more, then we are referring to a
15328 member template. That's OK too. */
15329 if (parser->num_template_parameter_lists == num_templates + 1)
15330 return true;
15331 /* Otherwise, there are too many template parameter lists. We have
15332 something like:
15333
15334 template <class T> template <class U> void S::f(); */
15335 error ("too many template-parameter-lists");
15336 return false;
15337 }
15338
15339 /* Parse an optional `::' token indicating that the following name is
15340 from the global namespace. If so, PARSER->SCOPE is set to the
15341 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15342 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15343 Returns the new value of PARSER->SCOPE, if the `::' token is
15344 present, and NULL_TREE otherwise. */
15345
15346 static tree
15347 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15348 {
15349 cp_token *token;
15350
15351 /* Peek at the next token. */
15352 token = cp_lexer_peek_token (parser->lexer);
15353 /* If we're looking at a `::' token then we're starting from the
15354 global namespace, not our current location. */
15355 if (token->type == CPP_SCOPE)
15356 {
15357 /* Consume the `::' token. */
15358 cp_lexer_consume_token (parser->lexer);
15359 /* Set the SCOPE so that we know where to start the lookup. */
15360 parser->scope = global_namespace;
15361 parser->qualifying_scope = global_namespace;
15362 parser->object_scope = NULL_TREE;
15363
15364 return parser->scope;
15365 }
15366 else if (!current_scope_valid_p)
15367 {
15368 parser->scope = NULL_TREE;
15369 parser->qualifying_scope = NULL_TREE;
15370 parser->object_scope = NULL_TREE;
15371 }
15372
15373 return NULL_TREE;
15374 }
15375
15376 /* Returns TRUE if the upcoming token sequence is the start of a
15377 constructor declarator. If FRIEND_P is true, the declarator is
15378 preceded by the `friend' specifier. */
15379
15380 static bool
15381 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15382 {
15383 bool constructor_p;
15384 tree type_decl = NULL_TREE;
15385 bool nested_name_p;
15386 cp_token *next_token;
15387
15388 /* The common case is that this is not a constructor declarator, so
15389 try to avoid doing lots of work if at all possible. It's not
15390 valid declare a constructor at function scope. */
15391 if (at_function_scope_p ())
15392 return false;
15393 /* And only certain tokens can begin a constructor declarator. */
15394 next_token = cp_lexer_peek_token (parser->lexer);
15395 if (next_token->type != CPP_NAME
15396 && next_token->type != CPP_SCOPE
15397 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15398 && next_token->type != CPP_TEMPLATE_ID)
15399 return false;
15400
15401 /* Parse tentatively; we are going to roll back all of the tokens
15402 consumed here. */
15403 cp_parser_parse_tentatively (parser);
15404 /* Assume that we are looking at a constructor declarator. */
15405 constructor_p = true;
15406
15407 /* Look for the optional `::' operator. */
15408 cp_parser_global_scope_opt (parser,
15409 /*current_scope_valid_p=*/false);
15410 /* Look for the nested-name-specifier. */
15411 nested_name_p
15412 = (cp_parser_nested_name_specifier_opt (parser,
15413 /*typename_keyword_p=*/false,
15414 /*check_dependency_p=*/false,
15415 /*type_p=*/false,
15416 /*is_declaration=*/false)
15417 != NULL_TREE);
15418 /* Outside of a class-specifier, there must be a
15419 nested-name-specifier. */
15420 if (!nested_name_p &&
15421 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15422 || friend_p))
15423 constructor_p = false;
15424 /* If we still think that this might be a constructor-declarator,
15425 look for a class-name. */
15426 if (constructor_p)
15427 {
15428 /* If we have:
15429
15430 template <typename T> struct S { S(); };
15431 template <typename T> S<T>::S ();
15432
15433 we must recognize that the nested `S' names a class.
15434 Similarly, for:
15435
15436 template <typename T> S<T>::S<T> ();
15437
15438 we must recognize that the nested `S' names a template. */
15439 type_decl = cp_parser_class_name (parser,
15440 /*typename_keyword_p=*/false,
15441 /*template_keyword_p=*/false,
15442 none_type,
15443 /*check_dependency_p=*/false,
15444 /*class_head_p=*/false,
15445 /*is_declaration=*/false);
15446 /* If there was no class-name, then this is not a constructor. */
15447 constructor_p = !cp_parser_error_occurred (parser);
15448 }
15449
15450 /* If we're still considering a constructor, we have to see a `(',
15451 to begin the parameter-declaration-clause, followed by either a
15452 `)', an `...', or a decl-specifier. We need to check for a
15453 type-specifier to avoid being fooled into thinking that:
15454
15455 S::S (f) (int);
15456
15457 is a constructor. (It is actually a function named `f' that
15458 takes one parameter (of type `int') and returns a value of type
15459 `S::S'. */
15460 if (constructor_p
15461 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15462 {
15463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15464 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15465 /* A parameter declaration begins with a decl-specifier,
15466 which is either the "attribute" keyword, a storage class
15467 specifier, or (usually) a type-specifier. */
15468 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15469 {
15470 tree type;
15471 tree pushed_scope = NULL_TREE;
15472 unsigned saved_num_template_parameter_lists;
15473
15474 /* Names appearing in the type-specifier should be looked up
15475 in the scope of the class. */
15476 if (current_class_type)
15477 type = NULL_TREE;
15478 else
15479 {
15480 type = TREE_TYPE (type_decl);
15481 if (TREE_CODE (type) == TYPENAME_TYPE)
15482 {
15483 type = resolve_typename_type (type,
15484 /*only_current_p=*/false);
15485 if (type == error_mark_node)
15486 {
15487 cp_parser_abort_tentative_parse (parser);
15488 return false;
15489 }
15490 }
15491 pushed_scope = push_scope (type);
15492 }
15493
15494 /* Inside the constructor parameter list, surrounding
15495 template-parameter-lists do not apply. */
15496 saved_num_template_parameter_lists
15497 = parser->num_template_parameter_lists;
15498 parser->num_template_parameter_lists = 0;
15499
15500 /* Look for the type-specifier. */
15501 cp_parser_type_specifier (parser,
15502 CP_PARSER_FLAGS_NONE,
15503 /*decl_specs=*/NULL,
15504 /*is_declarator=*/true,
15505 /*declares_class_or_enum=*/NULL,
15506 /*is_cv_qualifier=*/NULL);
15507
15508 parser->num_template_parameter_lists
15509 = saved_num_template_parameter_lists;
15510
15511 /* Leave the scope of the class. */
15512 if (pushed_scope)
15513 pop_scope (pushed_scope);
15514
15515 constructor_p = !cp_parser_error_occurred (parser);
15516 }
15517 }
15518 else
15519 constructor_p = false;
15520 /* We did not really want to consume any tokens. */
15521 cp_parser_abort_tentative_parse (parser);
15522
15523 return constructor_p;
15524 }
15525
15526 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15527 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15528 they must be performed once we are in the scope of the function.
15529
15530 Returns the function defined. */
15531
15532 static tree
15533 cp_parser_function_definition_from_specifiers_and_declarator
15534 (cp_parser* parser,
15535 cp_decl_specifier_seq *decl_specifiers,
15536 tree attributes,
15537 const cp_declarator *declarator)
15538 {
15539 tree fn;
15540 bool success_p;
15541
15542 /* Begin the function-definition. */
15543 success_p = start_function (decl_specifiers, declarator, attributes);
15544
15545 /* The things we're about to see are not directly qualified by any
15546 template headers we've seen thus far. */
15547 reset_specialization ();
15548
15549 /* If there were names looked up in the decl-specifier-seq that we
15550 did not check, check them now. We must wait until we are in the
15551 scope of the function to perform the checks, since the function
15552 might be a friend. */
15553 perform_deferred_access_checks ();
15554
15555 if (!success_p)
15556 {
15557 /* Skip the entire function. */
15558 cp_parser_skip_to_end_of_block_or_statement (parser);
15559 fn = error_mark_node;
15560 }
15561 else
15562 fn = cp_parser_function_definition_after_declarator (parser,
15563 /*inline_p=*/false);
15564
15565 return fn;
15566 }
15567
15568 /* Parse the part of a function-definition that follows the
15569 declarator. INLINE_P is TRUE iff this function is an inline
15570 function defined with a class-specifier.
15571
15572 Returns the function defined. */
15573
15574 static tree
15575 cp_parser_function_definition_after_declarator (cp_parser* parser,
15576 bool inline_p)
15577 {
15578 tree fn;
15579 bool ctor_initializer_p = false;
15580 bool saved_in_unbraced_linkage_specification_p;
15581 unsigned saved_num_template_parameter_lists;
15582
15583 /* If the next token is `return', then the code may be trying to
15584 make use of the "named return value" extension that G++ used to
15585 support. */
15586 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15587 {
15588 /* Consume the `return' keyword. */
15589 cp_lexer_consume_token (parser->lexer);
15590 /* Look for the identifier that indicates what value is to be
15591 returned. */
15592 cp_parser_identifier (parser);
15593 /* Issue an error message. */
15594 error ("named return values are no longer supported");
15595 /* Skip tokens until we reach the start of the function body. */
15596 while (true)
15597 {
15598 cp_token *token = cp_lexer_peek_token (parser->lexer);
15599 if (token->type == CPP_OPEN_BRACE
15600 || token->type == CPP_EOF
15601 || token->type == CPP_PRAGMA_EOL)
15602 break;
15603 cp_lexer_consume_token (parser->lexer);
15604 }
15605 }
15606 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15607 anything declared inside `f'. */
15608 saved_in_unbraced_linkage_specification_p
15609 = parser->in_unbraced_linkage_specification_p;
15610 parser->in_unbraced_linkage_specification_p = false;
15611 /* Inside the function, surrounding template-parameter-lists do not
15612 apply. */
15613 saved_num_template_parameter_lists
15614 = parser->num_template_parameter_lists;
15615 parser->num_template_parameter_lists = 0;
15616 /* If the next token is `try', then we are looking at a
15617 function-try-block. */
15618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15619 ctor_initializer_p = cp_parser_function_try_block (parser);
15620 /* A function-try-block includes the function-body, so we only do
15621 this next part if we're not processing a function-try-block. */
15622 else
15623 ctor_initializer_p
15624 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15625
15626 /* Finish the function. */
15627 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15628 (inline_p ? 2 : 0));
15629 /* Generate code for it, if necessary. */
15630 expand_or_defer_fn (fn);
15631 /* Restore the saved values. */
15632 parser->in_unbraced_linkage_specification_p
15633 = saved_in_unbraced_linkage_specification_p;
15634 parser->num_template_parameter_lists
15635 = saved_num_template_parameter_lists;
15636
15637 return fn;
15638 }
15639
15640 /* Parse a template-declaration, assuming that the `export' (and
15641 `extern') keywords, if present, has already been scanned. MEMBER_P
15642 is as for cp_parser_template_declaration. */
15643
15644 static void
15645 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15646 {
15647 tree decl = NULL_TREE;
15648 tree checks;
15649 tree parameter_list;
15650 bool friend_p = false;
15651 bool need_lang_pop;
15652
15653 /* Look for the `template' keyword. */
15654 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15655 return;
15656
15657 /* And the `<'. */
15658 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15659 return;
15660 /* [temp]
15661
15662 A template ... shall not have C linkage. */
15663 if (current_lang_name == lang_name_c)
15664 {
15665 error ("template with C linkage");
15666 /* Give it C++ linkage to avoid confusing other parts of the
15667 front end. */
15668 push_lang_context (lang_name_cplusplus);
15669 need_lang_pop = true;
15670 }
15671 else
15672 need_lang_pop = false;
15673
15674 /* We cannot perform access checks on the template parameter
15675 declarations until we know what is being declared, just as we
15676 cannot check the decl-specifier list. */
15677 push_deferring_access_checks (dk_deferred);
15678
15679 /* If the next token is `>', then we have an invalid
15680 specialization. Rather than complain about an invalid template
15681 parameter, issue an error message here. */
15682 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15683 {
15684 cp_parser_error (parser, "invalid explicit specialization");
15685 begin_specialization ();
15686 parameter_list = NULL_TREE;
15687 }
15688 else
15689 /* Parse the template parameters. */
15690 parameter_list = cp_parser_template_parameter_list (parser);
15691
15692 /* Get the deferred access checks from the parameter list. These
15693 will be checked once we know what is being declared, as for a
15694 member template the checks must be performed in the scope of the
15695 class containing the member. */
15696 checks = get_deferred_access_checks ();
15697
15698 /* Look for the `>'. */
15699 cp_parser_skip_to_end_of_template_parameter_list (parser);
15700 /* We just processed one more parameter list. */
15701 ++parser->num_template_parameter_lists;
15702 /* If the next token is `template', there are more template
15703 parameters. */
15704 if (cp_lexer_next_token_is_keyword (parser->lexer,
15705 RID_TEMPLATE))
15706 cp_parser_template_declaration_after_export (parser, member_p);
15707 else
15708 {
15709 /* There are no access checks when parsing a template, as we do not
15710 know if a specialization will be a friend. */
15711 push_deferring_access_checks (dk_no_check);
15712 decl = cp_parser_single_declaration (parser,
15713 checks,
15714 member_p,
15715 &friend_p);
15716 pop_deferring_access_checks ();
15717
15718 /* If this is a member template declaration, let the front
15719 end know. */
15720 if (member_p && !friend_p && decl)
15721 {
15722 if (TREE_CODE (decl) == TYPE_DECL)
15723 cp_parser_check_access_in_redeclaration (decl);
15724
15725 decl = finish_member_template_decl (decl);
15726 }
15727 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15728 make_friend_class (current_class_type, TREE_TYPE (decl),
15729 /*complain=*/true);
15730 }
15731 /* We are done with the current parameter list. */
15732 --parser->num_template_parameter_lists;
15733
15734 pop_deferring_access_checks ();
15735
15736 /* Finish up. */
15737 finish_template_decl (parameter_list);
15738
15739 /* Register member declarations. */
15740 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15741 finish_member_declaration (decl);
15742 /* For the erroneous case of a template with C linkage, we pushed an
15743 implicit C++ linkage scope; exit that scope now. */
15744 if (need_lang_pop)
15745 pop_lang_context ();
15746 /* If DECL is a function template, we must return to parse it later.
15747 (Even though there is no definition, there might be default
15748 arguments that need handling.) */
15749 if (member_p && decl
15750 && (TREE_CODE (decl) == FUNCTION_DECL
15751 || DECL_FUNCTION_TEMPLATE_P (decl)))
15752 TREE_VALUE (parser->unparsed_functions_queues)
15753 = tree_cons (NULL_TREE, decl,
15754 TREE_VALUE (parser->unparsed_functions_queues));
15755 }
15756
15757 /* Perform the deferred access checks from a template-parameter-list.
15758 CHECKS is a TREE_LIST of access checks, as returned by
15759 get_deferred_access_checks. */
15760
15761 static void
15762 cp_parser_perform_template_parameter_access_checks (tree checks)
15763 {
15764 ++processing_template_parmlist;
15765 perform_access_checks (checks);
15766 --processing_template_parmlist;
15767 }
15768
15769 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15770 `function-definition' sequence. MEMBER_P is true, this declaration
15771 appears in a class scope.
15772
15773 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15774 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15775
15776 static tree
15777 cp_parser_single_declaration (cp_parser* parser,
15778 tree checks,
15779 bool member_p,
15780 bool* friend_p)
15781 {
15782 int declares_class_or_enum;
15783 tree decl = NULL_TREE;
15784 cp_decl_specifier_seq decl_specifiers;
15785 bool function_definition_p = false;
15786
15787 /* This function is only used when processing a template
15788 declaration. */
15789 gcc_assert (innermost_scope_kind () == sk_template_parms
15790 || innermost_scope_kind () == sk_template_spec);
15791
15792 /* Defer access checks until we know what is being declared. */
15793 push_deferring_access_checks (dk_deferred);
15794
15795 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15796 alternative. */
15797 cp_parser_decl_specifier_seq (parser,
15798 CP_PARSER_FLAGS_OPTIONAL,
15799 &decl_specifiers,
15800 &declares_class_or_enum);
15801 if (friend_p)
15802 *friend_p = cp_parser_friend_p (&decl_specifiers);
15803
15804 /* There are no template typedefs. */
15805 if (decl_specifiers.specs[(int) ds_typedef])
15806 {
15807 error ("template declaration of %qs", "typedef");
15808 decl = error_mark_node;
15809 }
15810
15811 /* Gather up the access checks that occurred the
15812 decl-specifier-seq. */
15813 stop_deferring_access_checks ();
15814
15815 /* Check for the declaration of a template class. */
15816 if (declares_class_or_enum)
15817 {
15818 if (cp_parser_declares_only_class_p (parser))
15819 {
15820 decl = shadow_tag (&decl_specifiers);
15821
15822 /* In this case:
15823
15824 struct C {
15825 friend template <typename T> struct A<T>::B;
15826 };
15827
15828 A<T>::B will be represented by a TYPENAME_TYPE, and
15829 therefore not recognized by shadow_tag. */
15830 if (friend_p && *friend_p
15831 && !decl
15832 && decl_specifiers.type
15833 && TYPE_P (decl_specifiers.type))
15834 decl = decl_specifiers.type;
15835
15836 if (decl && decl != error_mark_node)
15837 decl = TYPE_NAME (decl);
15838 else
15839 decl = error_mark_node;
15840
15841 /* Perform access checks for template parameters. */
15842 cp_parser_perform_template_parameter_access_checks (checks);
15843 }
15844 }
15845 /* If it's not a template class, try for a template function. If
15846 the next token is a `;', then this declaration does not declare
15847 anything. But, if there were errors in the decl-specifiers, then
15848 the error might well have come from an attempted class-specifier.
15849 In that case, there's no need to warn about a missing declarator. */
15850 if (!decl
15851 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15852 || decl_specifiers.type != error_mark_node))
15853 decl = cp_parser_init_declarator (parser,
15854 &decl_specifiers,
15855 checks,
15856 /*function_definition_allowed_p=*/true,
15857 member_p,
15858 declares_class_or_enum,
15859 &function_definition_p);
15860
15861 pop_deferring_access_checks ();
15862
15863 /* Clear any current qualification; whatever comes next is the start
15864 of something new. */
15865 parser->scope = NULL_TREE;
15866 parser->qualifying_scope = NULL_TREE;
15867 parser->object_scope = NULL_TREE;
15868 /* Look for a trailing `;' after the declaration. */
15869 if (!function_definition_p
15870 && (decl == error_mark_node
15871 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15872 cp_parser_skip_to_end_of_block_or_statement (parser);
15873
15874 return decl;
15875 }
15876
15877 /* Parse a cast-expression that is not the operand of a unary "&". */
15878
15879 static tree
15880 cp_parser_simple_cast_expression (cp_parser *parser)
15881 {
15882 return cp_parser_cast_expression (parser, /*address_p=*/false,
15883 /*cast_p=*/false);
15884 }
15885
15886 /* Parse a functional cast to TYPE. Returns an expression
15887 representing the cast. */
15888
15889 static tree
15890 cp_parser_functional_cast (cp_parser* parser, tree type)
15891 {
15892 tree expression_list;
15893 tree cast;
15894
15895 expression_list
15896 = cp_parser_parenthesized_expression_list (parser, false,
15897 /*cast_p=*/true,
15898 /*non_constant_p=*/NULL);
15899
15900 cast = build_functional_cast (type, expression_list);
15901 /* [expr.const]/1: In an integral constant expression "only type
15902 conversions to integral or enumeration type can be used". */
15903 if (TREE_CODE (type) == TYPE_DECL)
15904 type = TREE_TYPE (type);
15905 if (cast != error_mark_node
15906 && !cast_valid_in_integral_constant_expression_p (type)
15907 && (cp_parser_non_integral_constant_expression
15908 (parser, "a call to a constructor")))
15909 return error_mark_node;
15910 return cast;
15911 }
15912
15913 /* Save the tokens that make up the body of a member function defined
15914 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15915 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15916 specifiers applied to the declaration. Returns the FUNCTION_DECL
15917 for the member function. */
15918
15919 static tree
15920 cp_parser_save_member_function_body (cp_parser* parser,
15921 cp_decl_specifier_seq *decl_specifiers,
15922 cp_declarator *declarator,
15923 tree attributes)
15924 {
15925 cp_token *first;
15926 cp_token *last;
15927 tree fn;
15928
15929 /* Create the function-declaration. */
15930 fn = start_method (decl_specifiers, declarator, attributes);
15931 /* If something went badly wrong, bail out now. */
15932 if (fn == error_mark_node)
15933 {
15934 /* If there's a function-body, skip it. */
15935 if (cp_parser_token_starts_function_definition_p
15936 (cp_lexer_peek_token (parser->lexer)))
15937 cp_parser_skip_to_end_of_block_or_statement (parser);
15938 return error_mark_node;
15939 }
15940
15941 /* Remember it, if there default args to post process. */
15942 cp_parser_save_default_args (parser, fn);
15943
15944 /* Save away the tokens that make up the body of the
15945 function. */
15946 first = parser->lexer->next_token;
15947 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15948 /* Handle function try blocks. */
15949 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15950 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15951 last = parser->lexer->next_token;
15952
15953 /* Save away the inline definition; we will process it when the
15954 class is complete. */
15955 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15956 DECL_PENDING_INLINE_P (fn) = 1;
15957
15958 /* We need to know that this was defined in the class, so that
15959 friend templates are handled correctly. */
15960 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15961
15962 /* We're done with the inline definition. */
15963 finish_method (fn);
15964
15965 /* Add FN to the queue of functions to be parsed later. */
15966 TREE_VALUE (parser->unparsed_functions_queues)
15967 = tree_cons (NULL_TREE, fn,
15968 TREE_VALUE (parser->unparsed_functions_queues));
15969
15970 return fn;
15971 }
15972
15973 /* Parse a template-argument-list, as well as the trailing ">" (but
15974 not the opening ">"). See cp_parser_template_argument_list for the
15975 return value. */
15976
15977 static tree
15978 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15979 {
15980 tree arguments;
15981 tree saved_scope;
15982 tree saved_qualifying_scope;
15983 tree saved_object_scope;
15984 bool saved_greater_than_is_operator_p;
15985 bool saved_skip_evaluation;
15986
15987 /* [temp.names]
15988
15989 When parsing a template-id, the first non-nested `>' is taken as
15990 the end of the template-argument-list rather than a greater-than
15991 operator. */
15992 saved_greater_than_is_operator_p
15993 = parser->greater_than_is_operator_p;
15994 parser->greater_than_is_operator_p = false;
15995 /* Parsing the argument list may modify SCOPE, so we save it
15996 here. */
15997 saved_scope = parser->scope;
15998 saved_qualifying_scope = parser->qualifying_scope;
15999 saved_object_scope = parser->object_scope;
16000 /* We need to evaluate the template arguments, even though this
16001 template-id may be nested within a "sizeof". */
16002 saved_skip_evaluation = skip_evaluation;
16003 skip_evaluation = false;
16004 /* Parse the template-argument-list itself. */
16005 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16006 arguments = NULL_TREE;
16007 else
16008 arguments = cp_parser_template_argument_list (parser);
16009 /* Look for the `>' that ends the template-argument-list. If we find
16010 a '>>' instead, it's probably just a typo. */
16011 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16012 {
16013 if (!saved_greater_than_is_operator_p)
16014 {
16015 /* If we're in a nested template argument list, the '>>' has
16016 to be a typo for '> >'. We emit the error message, but we
16017 continue parsing and we push a '>' as next token, so that
16018 the argument list will be parsed correctly. Note that the
16019 global source location is still on the token before the
16020 '>>', so we need to say explicitly where we want it. */
16021 cp_token *token = cp_lexer_peek_token (parser->lexer);
16022 error ("%H%<>>%> should be %<> >%> "
16023 "within a nested template argument list",
16024 &token->location);
16025
16026 /* ??? Proper recovery should terminate two levels of
16027 template argument list here. */
16028 token->type = CPP_GREATER;
16029 }
16030 else
16031 {
16032 /* If this is not a nested template argument list, the '>>'
16033 is a typo for '>'. Emit an error message and continue.
16034 Same deal about the token location, but here we can get it
16035 right by consuming the '>>' before issuing the diagnostic. */
16036 cp_lexer_consume_token (parser->lexer);
16037 error ("spurious %<>>%>, use %<>%> to terminate "
16038 "a template argument list");
16039 }
16040 }
16041 else
16042 cp_parser_skip_to_end_of_template_parameter_list (parser);
16043 /* The `>' token might be a greater-than operator again now. */
16044 parser->greater_than_is_operator_p
16045 = saved_greater_than_is_operator_p;
16046 /* Restore the SAVED_SCOPE. */
16047 parser->scope = saved_scope;
16048 parser->qualifying_scope = saved_qualifying_scope;
16049 parser->object_scope = saved_object_scope;
16050 skip_evaluation = saved_skip_evaluation;
16051
16052 return arguments;
16053 }
16054
16055 /* MEMBER_FUNCTION is a member function, or a friend. If default
16056 arguments, or the body of the function have not yet been parsed,
16057 parse them now. */
16058
16059 static void
16060 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16061 {
16062 /* If this member is a template, get the underlying
16063 FUNCTION_DECL. */
16064 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16065 member_function = DECL_TEMPLATE_RESULT (member_function);
16066
16067 /* There should not be any class definitions in progress at this
16068 point; the bodies of members are only parsed outside of all class
16069 definitions. */
16070 gcc_assert (parser->num_classes_being_defined == 0);
16071 /* While we're parsing the member functions we might encounter more
16072 classes. We want to handle them right away, but we don't want
16073 them getting mixed up with functions that are currently in the
16074 queue. */
16075 parser->unparsed_functions_queues
16076 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16077
16078 /* Make sure that any template parameters are in scope. */
16079 maybe_begin_member_template_processing (member_function);
16080
16081 /* If the body of the function has not yet been parsed, parse it
16082 now. */
16083 if (DECL_PENDING_INLINE_P (member_function))
16084 {
16085 tree function_scope;
16086 cp_token_cache *tokens;
16087
16088 /* The function is no longer pending; we are processing it. */
16089 tokens = DECL_PENDING_INLINE_INFO (member_function);
16090 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16091 DECL_PENDING_INLINE_P (member_function) = 0;
16092
16093 /* If this is a local class, enter the scope of the containing
16094 function. */
16095 function_scope = current_function_decl;
16096 if (function_scope)
16097 push_function_context_to (function_scope);
16098
16099
16100 /* Push the body of the function onto the lexer stack. */
16101 cp_parser_push_lexer_for_tokens (parser, tokens);
16102
16103 /* Let the front end know that we going to be defining this
16104 function. */
16105 start_preparsed_function (member_function, NULL_TREE,
16106 SF_PRE_PARSED | SF_INCLASS_INLINE);
16107
16108 /* Don't do access checking if it is a templated function. */
16109 if (processing_template_decl)
16110 push_deferring_access_checks (dk_no_check);
16111
16112 /* Now, parse the body of the function. */
16113 cp_parser_function_definition_after_declarator (parser,
16114 /*inline_p=*/true);
16115
16116 if (processing_template_decl)
16117 pop_deferring_access_checks ();
16118
16119 /* Leave the scope of the containing function. */
16120 if (function_scope)
16121 pop_function_context_from (function_scope);
16122 cp_parser_pop_lexer (parser);
16123 }
16124
16125 /* Remove any template parameters from the symbol table. */
16126 maybe_end_member_template_processing ();
16127
16128 /* Restore the queue. */
16129 parser->unparsed_functions_queues
16130 = TREE_CHAIN (parser->unparsed_functions_queues);
16131 }
16132
16133 /* If DECL contains any default args, remember it on the unparsed
16134 functions queue. */
16135
16136 static void
16137 cp_parser_save_default_args (cp_parser* parser, tree decl)
16138 {
16139 tree probe;
16140
16141 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16142 probe;
16143 probe = TREE_CHAIN (probe))
16144 if (TREE_PURPOSE (probe))
16145 {
16146 TREE_PURPOSE (parser->unparsed_functions_queues)
16147 = tree_cons (current_class_type, decl,
16148 TREE_PURPOSE (parser->unparsed_functions_queues));
16149 break;
16150 }
16151 }
16152
16153 /* FN is a FUNCTION_DECL which may contains a parameter with an
16154 unparsed DEFAULT_ARG. Parse the default args now. This function
16155 assumes that the current scope is the scope in which the default
16156 argument should be processed. */
16157
16158 static void
16159 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16160 {
16161 bool saved_local_variables_forbidden_p;
16162 tree parm;
16163
16164 /* While we're parsing the default args, we might (due to the
16165 statement expression extension) encounter more classes. We want
16166 to handle them right away, but we don't want them getting mixed
16167 up with default args that are currently in the queue. */
16168 parser->unparsed_functions_queues
16169 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16170
16171 /* Local variable names (and the `this' keyword) may not appear
16172 in a default argument. */
16173 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16174 parser->local_variables_forbidden_p = true;
16175
16176 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16177 parm;
16178 parm = TREE_CHAIN (parm))
16179 {
16180 cp_token_cache *tokens;
16181 tree default_arg = TREE_PURPOSE (parm);
16182 tree parsed_arg;
16183 VEC(tree,gc) *insts;
16184 tree copy;
16185 unsigned ix;
16186
16187 if (!default_arg)
16188 continue;
16189
16190 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16191 /* This can happen for a friend declaration for a function
16192 already declared with default arguments. */
16193 continue;
16194
16195 /* Push the saved tokens for the default argument onto the parser's
16196 lexer stack. */
16197 tokens = DEFARG_TOKENS (default_arg);
16198 cp_parser_push_lexer_for_tokens (parser, tokens);
16199
16200 /* Parse the assignment-expression. */
16201 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16202
16203 if (!processing_template_decl)
16204 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16205
16206 TREE_PURPOSE (parm) = parsed_arg;
16207
16208 /* Update any instantiations we've already created. */
16209 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16210 VEC_iterate (tree, insts, ix, copy); ix++)
16211 TREE_PURPOSE (copy) = parsed_arg;
16212
16213 /* If the token stream has not been completely used up, then
16214 there was extra junk after the end of the default
16215 argument. */
16216 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16217 cp_parser_error (parser, "expected %<,%>");
16218
16219 /* Revert to the main lexer. */
16220 cp_parser_pop_lexer (parser);
16221 }
16222
16223 /* Make sure no default arg is missing. */
16224 check_default_args (fn);
16225
16226 /* Restore the state of local_variables_forbidden_p. */
16227 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16228
16229 /* Restore the queue. */
16230 parser->unparsed_functions_queues
16231 = TREE_CHAIN (parser->unparsed_functions_queues);
16232 }
16233
16234 /* Parse the operand of `sizeof' (or a similar operator). Returns
16235 either a TYPE or an expression, depending on the form of the
16236 input. The KEYWORD indicates which kind of expression we have
16237 encountered. */
16238
16239 static tree
16240 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16241 {
16242 static const char *format;
16243 tree expr = NULL_TREE;
16244 const char *saved_message;
16245 bool saved_integral_constant_expression_p;
16246 bool saved_non_integral_constant_expression_p;
16247
16248 /* Initialize FORMAT the first time we get here. */
16249 if (!format)
16250 format = "types may not be defined in '%s' expressions";
16251
16252 /* Types cannot be defined in a `sizeof' expression. Save away the
16253 old message. */
16254 saved_message = parser->type_definition_forbidden_message;
16255 /* And create the new one. */
16256 parser->type_definition_forbidden_message
16257 = XNEWVEC (const char, strlen (format)
16258 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16259 + 1 /* `\0' */);
16260 sprintf ((char *) parser->type_definition_forbidden_message,
16261 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16262
16263 /* The restrictions on constant-expressions do not apply inside
16264 sizeof expressions. */
16265 saved_integral_constant_expression_p
16266 = parser->integral_constant_expression_p;
16267 saved_non_integral_constant_expression_p
16268 = parser->non_integral_constant_expression_p;
16269 parser->integral_constant_expression_p = false;
16270
16271 /* Do not actually evaluate the expression. */
16272 ++skip_evaluation;
16273 /* If it's a `(', then we might be looking at the type-id
16274 construction. */
16275 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16276 {
16277 tree type;
16278 bool saved_in_type_id_in_expr_p;
16279
16280 /* We can't be sure yet whether we're looking at a type-id or an
16281 expression. */
16282 cp_parser_parse_tentatively (parser);
16283 /* Consume the `('. */
16284 cp_lexer_consume_token (parser->lexer);
16285 /* Parse the type-id. */
16286 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16287 parser->in_type_id_in_expr_p = true;
16288 type = cp_parser_type_id (parser);
16289 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16290 /* Now, look for the trailing `)'. */
16291 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16292 /* If all went well, then we're done. */
16293 if (cp_parser_parse_definitely (parser))
16294 {
16295 cp_decl_specifier_seq decl_specs;
16296
16297 /* Build a trivial decl-specifier-seq. */
16298 clear_decl_specs (&decl_specs);
16299 decl_specs.type = type;
16300
16301 /* Call grokdeclarator to figure out what type this is. */
16302 expr = grokdeclarator (NULL,
16303 &decl_specs,
16304 TYPENAME,
16305 /*initialized=*/0,
16306 /*attrlist=*/NULL);
16307 }
16308 }
16309
16310 /* If the type-id production did not work out, then we must be
16311 looking at the unary-expression production. */
16312 if (!expr)
16313 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16314 /*cast_p=*/false);
16315 /* Go back to evaluating expressions. */
16316 --skip_evaluation;
16317
16318 /* Free the message we created. */
16319 free ((char *) parser->type_definition_forbidden_message);
16320 /* And restore the old one. */
16321 parser->type_definition_forbidden_message = saved_message;
16322 parser->integral_constant_expression_p
16323 = saved_integral_constant_expression_p;
16324 parser->non_integral_constant_expression_p
16325 = saved_non_integral_constant_expression_p;
16326
16327 return expr;
16328 }
16329
16330 /* If the current declaration has no declarator, return true. */
16331
16332 static bool
16333 cp_parser_declares_only_class_p (cp_parser *parser)
16334 {
16335 /* If the next token is a `;' or a `,' then there is no
16336 declarator. */
16337 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16338 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16339 }
16340
16341 /* Update the DECL_SPECS to reflect the storage class indicated by
16342 KEYWORD. */
16343
16344 static void
16345 cp_parser_set_storage_class (cp_parser *parser,
16346 cp_decl_specifier_seq *decl_specs,
16347 enum rid keyword)
16348 {
16349 cp_storage_class storage_class;
16350
16351 if (parser->in_unbraced_linkage_specification_p)
16352 {
16353 error ("invalid use of %qD in linkage specification",
16354 ridpointers[keyword]);
16355 return;
16356 }
16357 else if (decl_specs->storage_class != sc_none)
16358 {
16359 decl_specs->conflicting_specifiers_p = true;
16360 return;
16361 }
16362
16363 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16364 && decl_specs->specs[(int) ds_thread])
16365 {
16366 error ("%<__thread%> before %qD", ridpointers[keyword]);
16367 decl_specs->specs[(int) ds_thread] = 0;
16368 }
16369
16370 switch (keyword)
16371 {
16372 case RID_AUTO:
16373 storage_class = sc_auto;
16374 break;
16375 case RID_REGISTER:
16376 storage_class = sc_register;
16377 break;
16378 case RID_STATIC:
16379 storage_class = sc_static;
16380 break;
16381 case RID_EXTERN:
16382 storage_class = sc_extern;
16383 break;
16384 case RID_MUTABLE:
16385 storage_class = sc_mutable;
16386 break;
16387 default:
16388 gcc_unreachable ();
16389 }
16390 decl_specs->storage_class = storage_class;
16391
16392 /* A storage class specifier cannot be applied alongside a typedef
16393 specifier. If there is a typedef specifier present then set
16394 conflicting_specifiers_p which will trigger an error later
16395 on in grokdeclarator. */
16396 if (decl_specs->specs[(int)ds_typedef])
16397 decl_specs->conflicting_specifiers_p = true;
16398 }
16399
16400 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16401 is true, the type is a user-defined type; otherwise it is a
16402 built-in type specified by a keyword. */
16403
16404 static void
16405 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16406 tree type_spec,
16407 bool user_defined_p)
16408 {
16409 decl_specs->any_specifiers_p = true;
16410
16411 /* If the user tries to redeclare bool or wchar_t (with, for
16412 example, in "typedef int wchar_t;") we remember that this is what
16413 happened. In system headers, we ignore these declarations so
16414 that G++ can work with system headers that are not C++-safe. */
16415 if (decl_specs->specs[(int) ds_typedef]
16416 && !user_defined_p
16417 && (type_spec == boolean_type_node
16418 || type_spec == wchar_type_node)
16419 && (decl_specs->type
16420 || decl_specs->specs[(int) ds_long]
16421 || decl_specs->specs[(int) ds_short]
16422 || decl_specs->specs[(int) ds_unsigned]
16423 || decl_specs->specs[(int) ds_signed]))
16424 {
16425 decl_specs->redefined_builtin_type = type_spec;
16426 if (!decl_specs->type)
16427 {
16428 decl_specs->type = type_spec;
16429 decl_specs->user_defined_type_p = false;
16430 }
16431 }
16432 else if (decl_specs->type)
16433 decl_specs->multiple_types_p = true;
16434 else
16435 {
16436 decl_specs->type = type_spec;
16437 decl_specs->user_defined_type_p = user_defined_p;
16438 decl_specs->redefined_builtin_type = NULL_TREE;
16439 }
16440 }
16441
16442 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16443 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16444
16445 static bool
16446 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16447 {
16448 return decl_specifiers->specs[(int) ds_friend] != 0;
16449 }
16450
16451 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16452 issue an error message indicating that TOKEN_DESC was expected.
16453
16454 Returns the token consumed, if the token had the appropriate type.
16455 Otherwise, returns NULL. */
16456
16457 static cp_token *
16458 cp_parser_require (cp_parser* parser,
16459 enum cpp_ttype type,
16460 const char* token_desc)
16461 {
16462 if (cp_lexer_next_token_is (parser->lexer, type))
16463 return cp_lexer_consume_token (parser->lexer);
16464 else
16465 {
16466 /* Output the MESSAGE -- unless we're parsing tentatively. */
16467 if (!cp_parser_simulate_error (parser))
16468 {
16469 char *message = concat ("expected ", token_desc, NULL);
16470 cp_parser_error (parser, message);
16471 free (message);
16472 }
16473 return NULL;
16474 }
16475 }
16476
16477 /* An error message is produced if the next token is not '>'.
16478 All further tokens are skipped until the desired token is
16479 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16480
16481 static void
16482 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16483 {
16484 /* Current level of '< ... >'. */
16485 unsigned level = 0;
16486 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16487 unsigned nesting_depth = 0;
16488
16489 /* Are we ready, yet? If not, issue error message. */
16490 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16491 return;
16492
16493 /* Skip tokens until the desired token is found. */
16494 while (true)
16495 {
16496 /* Peek at the next token. */
16497 switch (cp_lexer_peek_token (parser->lexer)->type)
16498 {
16499 case CPP_LESS:
16500 if (!nesting_depth)
16501 ++level;
16502 break;
16503
16504 case CPP_GREATER:
16505 if (!nesting_depth && level-- == 0)
16506 {
16507 /* We've reached the token we want, consume it and stop. */
16508 cp_lexer_consume_token (parser->lexer);
16509 return;
16510 }
16511 break;
16512
16513 case CPP_OPEN_PAREN:
16514 case CPP_OPEN_SQUARE:
16515 ++nesting_depth;
16516 break;
16517
16518 case CPP_CLOSE_PAREN:
16519 case CPP_CLOSE_SQUARE:
16520 if (nesting_depth-- == 0)
16521 return;
16522 break;
16523
16524 case CPP_EOF:
16525 case CPP_PRAGMA_EOL:
16526 case CPP_SEMICOLON:
16527 case CPP_OPEN_BRACE:
16528 case CPP_CLOSE_BRACE:
16529 /* The '>' was probably forgotten, don't look further. */
16530 return;
16531
16532 default:
16533 break;
16534 }
16535
16536 /* Consume this token. */
16537 cp_lexer_consume_token (parser->lexer);
16538 }
16539 }
16540
16541 /* If the next token is the indicated keyword, consume it. Otherwise,
16542 issue an error message indicating that TOKEN_DESC was expected.
16543
16544 Returns the token consumed, if the token had the appropriate type.
16545 Otherwise, returns NULL. */
16546
16547 static cp_token *
16548 cp_parser_require_keyword (cp_parser* parser,
16549 enum rid keyword,
16550 const char* token_desc)
16551 {
16552 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16553
16554 if (token && token->keyword != keyword)
16555 {
16556 dyn_string_t error_msg;
16557
16558 /* Format the error message. */
16559 error_msg = dyn_string_new (0);
16560 dyn_string_append_cstr (error_msg, "expected ");
16561 dyn_string_append_cstr (error_msg, token_desc);
16562 cp_parser_error (parser, error_msg->s);
16563 dyn_string_delete (error_msg);
16564 return NULL;
16565 }
16566
16567 return token;
16568 }
16569
16570 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16571 function-definition. */
16572
16573 static bool
16574 cp_parser_token_starts_function_definition_p (cp_token* token)
16575 {
16576 return (/* An ordinary function-body begins with an `{'. */
16577 token->type == CPP_OPEN_BRACE
16578 /* A ctor-initializer begins with a `:'. */
16579 || token->type == CPP_COLON
16580 /* A function-try-block begins with `try'. */
16581 || token->keyword == RID_TRY
16582 /* The named return value extension begins with `return'. */
16583 || token->keyword == RID_RETURN);
16584 }
16585
16586 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16587 definition. */
16588
16589 static bool
16590 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16591 {
16592 cp_token *token;
16593
16594 token = cp_lexer_peek_token (parser->lexer);
16595 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16596 }
16597
16598 /* Returns TRUE iff the next token is the "," or ">" ending a
16599 template-argument. */
16600
16601 static bool
16602 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16603 {
16604 cp_token *token;
16605
16606 token = cp_lexer_peek_token (parser->lexer);
16607 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16608 }
16609
16610 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16611 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16612
16613 static bool
16614 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16615 size_t n)
16616 {
16617 cp_token *token;
16618
16619 token = cp_lexer_peek_nth_token (parser->lexer, n);
16620 if (token->type == CPP_LESS)
16621 return true;
16622 /* Check for the sequence `<::' in the original code. It would be lexed as
16623 `[:', where `[' is a digraph, and there is no whitespace before
16624 `:'. */
16625 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16626 {
16627 cp_token *token2;
16628 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16629 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16630 return true;
16631 }
16632 return false;
16633 }
16634
16635 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16636 or none_type otherwise. */
16637
16638 static enum tag_types
16639 cp_parser_token_is_class_key (cp_token* token)
16640 {
16641 switch (token->keyword)
16642 {
16643 case RID_CLASS:
16644 return class_type;
16645 case RID_STRUCT:
16646 return record_type;
16647 case RID_UNION:
16648 return union_type;
16649
16650 default:
16651 return none_type;
16652 }
16653 }
16654
16655 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16656
16657 static void
16658 cp_parser_check_class_key (enum tag_types class_key, tree type)
16659 {
16660 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16661 pedwarn ("%qs tag used in naming %q#T",
16662 class_key == union_type ? "union"
16663 : class_key == record_type ? "struct" : "class",
16664 type);
16665 }
16666
16667 /* Issue an error message if DECL is redeclared with different
16668 access than its original declaration [class.access.spec/3].
16669 This applies to nested classes and nested class templates.
16670 [class.mem/1]. */
16671
16672 static void
16673 cp_parser_check_access_in_redeclaration (tree decl)
16674 {
16675 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16676 return;
16677
16678 if ((TREE_PRIVATE (decl)
16679 != (current_access_specifier == access_private_node))
16680 || (TREE_PROTECTED (decl)
16681 != (current_access_specifier == access_protected_node)))
16682 error ("%qD redeclared with different access", decl);
16683 }
16684
16685 /* Look for the `template' keyword, as a syntactic disambiguator.
16686 Return TRUE iff it is present, in which case it will be
16687 consumed. */
16688
16689 static bool
16690 cp_parser_optional_template_keyword (cp_parser *parser)
16691 {
16692 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16693 {
16694 /* The `template' keyword can only be used within templates;
16695 outside templates the parser can always figure out what is a
16696 template and what is not. */
16697 if (!processing_template_decl)
16698 {
16699 error ("%<template%> (as a disambiguator) is only allowed "
16700 "within templates");
16701 /* If this part of the token stream is rescanned, the same
16702 error message would be generated. So, we purge the token
16703 from the stream. */
16704 cp_lexer_purge_token (parser->lexer);
16705 return false;
16706 }
16707 else
16708 {
16709 /* Consume the `template' keyword. */
16710 cp_lexer_consume_token (parser->lexer);
16711 return true;
16712 }
16713 }
16714
16715 return false;
16716 }
16717
16718 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16719 set PARSER->SCOPE, and perform other related actions. */
16720
16721 static void
16722 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16723 {
16724 tree value;
16725 tree check;
16726
16727 /* Get the stored value. */
16728 value = cp_lexer_consume_token (parser->lexer)->value;
16729 /* Perform any access checks that were deferred. */
16730 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16731 perform_or_defer_access_check (TREE_PURPOSE (check),
16732 TREE_VALUE (check),
16733 TREE_VALUE (check));
16734 /* Set the scope from the stored value. */
16735 parser->scope = TREE_VALUE (value);
16736 parser->qualifying_scope = TREE_TYPE (value);
16737 parser->object_scope = NULL_TREE;
16738 }
16739
16740 /* Consume tokens up through a non-nested END token. */
16741
16742 static void
16743 cp_parser_cache_group (cp_parser *parser,
16744 enum cpp_ttype end,
16745 unsigned depth)
16746 {
16747 while (true)
16748 {
16749 cp_token *token;
16750
16751 /* Abort a parenthesized expression if we encounter a brace. */
16752 if ((end == CPP_CLOSE_PAREN || depth == 0)
16753 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16754 return;
16755 /* If we've reached the end of the file, stop. */
16756 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16757 || (end != CPP_PRAGMA_EOL
16758 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16759 return;
16760 /* Consume the next token. */
16761 token = cp_lexer_consume_token (parser->lexer);
16762 /* See if it starts a new group. */
16763 if (token->type == CPP_OPEN_BRACE)
16764 {
16765 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16766 if (depth == 0)
16767 return;
16768 }
16769 else if (token->type == CPP_OPEN_PAREN)
16770 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16771 else if (token->type == CPP_PRAGMA)
16772 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16773 else if (token->type == end)
16774 return;
16775 }
16776 }
16777
16778 /* Begin parsing tentatively. We always save tokens while parsing
16779 tentatively so that if the tentative parsing fails we can restore the
16780 tokens. */
16781
16782 static void
16783 cp_parser_parse_tentatively (cp_parser* parser)
16784 {
16785 /* Enter a new parsing context. */
16786 parser->context = cp_parser_context_new (parser->context);
16787 /* Begin saving tokens. */
16788 cp_lexer_save_tokens (parser->lexer);
16789 /* In order to avoid repetitive access control error messages,
16790 access checks are queued up until we are no longer parsing
16791 tentatively. */
16792 push_deferring_access_checks (dk_deferred);
16793 }
16794
16795 /* Commit to the currently active tentative parse. */
16796
16797 static void
16798 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16799 {
16800 cp_parser_context *context;
16801 cp_lexer *lexer;
16802
16803 /* Mark all of the levels as committed. */
16804 lexer = parser->lexer;
16805 for (context = parser->context; context->next; context = context->next)
16806 {
16807 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16808 break;
16809 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16810 while (!cp_lexer_saving_tokens (lexer))
16811 lexer = lexer->next;
16812 cp_lexer_commit_tokens (lexer);
16813 }
16814 }
16815
16816 /* Abort the currently active tentative parse. All consumed tokens
16817 will be rolled back, and no diagnostics will be issued. */
16818
16819 static void
16820 cp_parser_abort_tentative_parse (cp_parser* parser)
16821 {
16822 cp_parser_simulate_error (parser);
16823 /* Now, pretend that we want to see if the construct was
16824 successfully parsed. */
16825 cp_parser_parse_definitely (parser);
16826 }
16827
16828 /* Stop parsing tentatively. If a parse error has occurred, restore the
16829 token stream. Otherwise, commit to the tokens we have consumed.
16830 Returns true if no error occurred; false otherwise. */
16831
16832 static bool
16833 cp_parser_parse_definitely (cp_parser* parser)
16834 {
16835 bool error_occurred;
16836 cp_parser_context *context;
16837
16838 /* Remember whether or not an error occurred, since we are about to
16839 destroy that information. */
16840 error_occurred = cp_parser_error_occurred (parser);
16841 /* Remove the topmost context from the stack. */
16842 context = parser->context;
16843 parser->context = context->next;
16844 /* If no parse errors occurred, commit to the tentative parse. */
16845 if (!error_occurred)
16846 {
16847 /* Commit to the tokens read tentatively, unless that was
16848 already done. */
16849 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16850 cp_lexer_commit_tokens (parser->lexer);
16851
16852 pop_to_parent_deferring_access_checks ();
16853 }
16854 /* Otherwise, if errors occurred, roll back our state so that things
16855 are just as they were before we began the tentative parse. */
16856 else
16857 {
16858 cp_lexer_rollback_tokens (parser->lexer);
16859 pop_deferring_access_checks ();
16860 }
16861 /* Add the context to the front of the free list. */
16862 context->next = cp_parser_context_free_list;
16863 cp_parser_context_free_list = context;
16864
16865 return !error_occurred;
16866 }
16867
16868 /* Returns true if we are parsing tentatively and are not committed to
16869 this tentative parse. */
16870
16871 static bool
16872 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16873 {
16874 return (cp_parser_parsing_tentatively (parser)
16875 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16876 }
16877
16878 /* Returns nonzero iff an error has occurred during the most recent
16879 tentative parse. */
16880
16881 static bool
16882 cp_parser_error_occurred (cp_parser* parser)
16883 {
16884 return (cp_parser_parsing_tentatively (parser)
16885 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16886 }
16887
16888 /* Returns nonzero if GNU extensions are allowed. */
16889
16890 static bool
16891 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16892 {
16893 return parser->allow_gnu_extensions_p;
16894 }
16895 \f
16896 /* Objective-C++ Productions */
16897
16898
16899 /* Parse an Objective-C expression, which feeds into a primary-expression
16900 above.
16901
16902 objc-expression:
16903 objc-message-expression
16904 objc-string-literal
16905 objc-encode-expression
16906 objc-protocol-expression
16907 objc-selector-expression
16908
16909 Returns a tree representation of the expression. */
16910
16911 static tree
16912 cp_parser_objc_expression (cp_parser* parser)
16913 {
16914 /* Try to figure out what kind of declaration is present. */
16915 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16916
16917 switch (kwd->type)
16918 {
16919 case CPP_OPEN_SQUARE:
16920 return cp_parser_objc_message_expression (parser);
16921
16922 case CPP_OBJC_STRING:
16923 kwd = cp_lexer_consume_token (parser->lexer);
16924 return objc_build_string_object (kwd->value);
16925
16926 case CPP_KEYWORD:
16927 switch (kwd->keyword)
16928 {
16929 case RID_AT_ENCODE:
16930 return cp_parser_objc_encode_expression (parser);
16931
16932 case RID_AT_PROTOCOL:
16933 return cp_parser_objc_protocol_expression (parser);
16934
16935 case RID_AT_SELECTOR:
16936 return cp_parser_objc_selector_expression (parser);
16937
16938 default:
16939 break;
16940 }
16941 default:
16942 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16943 cp_parser_skip_to_end_of_block_or_statement (parser);
16944 }
16945
16946 return error_mark_node;
16947 }
16948
16949 /* Parse an Objective-C message expression.
16950
16951 objc-message-expression:
16952 [ objc-message-receiver objc-message-args ]
16953
16954 Returns a representation of an Objective-C message. */
16955
16956 static tree
16957 cp_parser_objc_message_expression (cp_parser* parser)
16958 {
16959 tree receiver, messageargs;
16960
16961 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16962 receiver = cp_parser_objc_message_receiver (parser);
16963 messageargs = cp_parser_objc_message_args (parser);
16964 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16965
16966 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16967 }
16968
16969 /* Parse an objc-message-receiver.
16970
16971 objc-message-receiver:
16972 expression
16973 simple-type-specifier
16974
16975 Returns a representation of the type or expression. */
16976
16977 static tree
16978 cp_parser_objc_message_receiver (cp_parser* parser)
16979 {
16980 tree rcv;
16981
16982 /* An Objective-C message receiver may be either (1) a type
16983 or (2) an expression. */
16984 cp_parser_parse_tentatively (parser);
16985 rcv = cp_parser_expression (parser, false);
16986
16987 if (cp_parser_parse_definitely (parser))
16988 return rcv;
16989
16990 rcv = cp_parser_simple_type_specifier (parser,
16991 /*decl_specs=*/NULL,
16992 CP_PARSER_FLAGS_NONE);
16993
16994 return objc_get_class_reference (rcv);
16995 }
16996
16997 /* Parse the arguments and selectors comprising an Objective-C message.
16998
16999 objc-message-args:
17000 objc-selector
17001 objc-selector-args
17002 objc-selector-args , objc-comma-args
17003
17004 objc-selector-args:
17005 objc-selector [opt] : assignment-expression
17006 objc-selector-args objc-selector [opt] : assignment-expression
17007
17008 objc-comma-args:
17009 assignment-expression
17010 objc-comma-args , assignment-expression
17011
17012 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17013 selector arguments and TREE_VALUE containing a list of comma
17014 arguments. */
17015
17016 static tree
17017 cp_parser_objc_message_args (cp_parser* parser)
17018 {
17019 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17020 bool maybe_unary_selector_p = true;
17021 cp_token *token = cp_lexer_peek_token (parser->lexer);
17022
17023 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17024 {
17025 tree selector = NULL_TREE, arg;
17026
17027 if (token->type != CPP_COLON)
17028 selector = cp_parser_objc_selector (parser);
17029
17030 /* Detect if we have a unary selector. */
17031 if (maybe_unary_selector_p
17032 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17033 return build_tree_list (selector, NULL_TREE);
17034
17035 maybe_unary_selector_p = false;
17036 cp_parser_require (parser, CPP_COLON, "`:'");
17037 arg = cp_parser_assignment_expression (parser, false);
17038
17039 sel_args
17040 = chainon (sel_args,
17041 build_tree_list (selector, arg));
17042
17043 token = cp_lexer_peek_token (parser->lexer);
17044 }
17045
17046 /* Handle non-selector arguments, if any. */
17047 while (token->type == CPP_COMMA)
17048 {
17049 tree arg;
17050
17051 cp_lexer_consume_token (parser->lexer);
17052 arg = cp_parser_assignment_expression (parser, false);
17053
17054 addl_args
17055 = chainon (addl_args,
17056 build_tree_list (NULL_TREE, arg));
17057
17058 token = cp_lexer_peek_token (parser->lexer);
17059 }
17060
17061 return build_tree_list (sel_args, addl_args);
17062 }
17063
17064 /* Parse an Objective-C encode expression.
17065
17066 objc-encode-expression:
17067 @encode objc-typename
17068
17069 Returns an encoded representation of the type argument. */
17070
17071 static tree
17072 cp_parser_objc_encode_expression (cp_parser* parser)
17073 {
17074 tree type;
17075
17076 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17077 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17078 type = complete_type (cp_parser_type_id (parser));
17079 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17080
17081 if (!type)
17082 {
17083 error ("%<@encode%> must specify a type as an argument");
17084 return error_mark_node;
17085 }
17086
17087 return objc_build_encode_expr (type);
17088 }
17089
17090 /* Parse an Objective-C @defs expression. */
17091
17092 static tree
17093 cp_parser_objc_defs_expression (cp_parser *parser)
17094 {
17095 tree name;
17096
17097 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17098 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17099 name = cp_parser_identifier (parser);
17100 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17101
17102 return objc_get_class_ivars (name);
17103 }
17104
17105 /* Parse an Objective-C protocol expression.
17106
17107 objc-protocol-expression:
17108 @protocol ( identifier )
17109
17110 Returns a representation of the protocol expression. */
17111
17112 static tree
17113 cp_parser_objc_protocol_expression (cp_parser* parser)
17114 {
17115 tree proto;
17116
17117 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17118 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17119 proto = cp_parser_identifier (parser);
17120 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17121
17122 return objc_build_protocol_expr (proto);
17123 }
17124
17125 /* Parse an Objective-C selector expression.
17126
17127 objc-selector-expression:
17128 @selector ( objc-method-signature )
17129
17130 objc-method-signature:
17131 objc-selector
17132 objc-selector-seq
17133
17134 objc-selector-seq:
17135 objc-selector :
17136 objc-selector-seq objc-selector :
17137
17138 Returns a representation of the method selector. */
17139
17140 static tree
17141 cp_parser_objc_selector_expression (cp_parser* parser)
17142 {
17143 tree sel_seq = NULL_TREE;
17144 bool maybe_unary_selector_p = true;
17145 cp_token *token;
17146
17147 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17148 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17149 token = cp_lexer_peek_token (parser->lexer);
17150
17151 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17152 || token->type == CPP_SCOPE)
17153 {
17154 tree selector = NULL_TREE;
17155
17156 if (token->type != CPP_COLON
17157 || token->type == CPP_SCOPE)
17158 selector = cp_parser_objc_selector (parser);
17159
17160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17161 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17162 {
17163 /* Detect if we have a unary selector. */
17164 if (maybe_unary_selector_p)
17165 {
17166 sel_seq = selector;
17167 goto finish_selector;
17168 }
17169 else
17170 {
17171 cp_parser_error (parser, "expected %<:%>");
17172 }
17173 }
17174 maybe_unary_selector_p = false;
17175 token = cp_lexer_consume_token (parser->lexer);
17176
17177 if (token->type == CPP_SCOPE)
17178 {
17179 sel_seq
17180 = chainon (sel_seq,
17181 build_tree_list (selector, NULL_TREE));
17182 sel_seq
17183 = chainon (sel_seq,
17184 build_tree_list (NULL_TREE, NULL_TREE));
17185 }
17186 else
17187 sel_seq
17188 = chainon (sel_seq,
17189 build_tree_list (selector, NULL_TREE));
17190
17191 token = cp_lexer_peek_token (parser->lexer);
17192 }
17193
17194 finish_selector:
17195 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17196
17197 return objc_build_selector_expr (sel_seq);
17198 }
17199
17200 /* Parse a list of identifiers.
17201
17202 objc-identifier-list:
17203 identifier
17204 objc-identifier-list , identifier
17205
17206 Returns a TREE_LIST of identifier nodes. */
17207
17208 static tree
17209 cp_parser_objc_identifier_list (cp_parser* parser)
17210 {
17211 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17212 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17213
17214 while (sep->type == CPP_COMMA)
17215 {
17216 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17217 list = chainon (list,
17218 build_tree_list (NULL_TREE,
17219 cp_parser_identifier (parser)));
17220 sep = cp_lexer_peek_token (parser->lexer);
17221 }
17222
17223 return list;
17224 }
17225
17226 /* Parse an Objective-C alias declaration.
17227
17228 objc-alias-declaration:
17229 @compatibility_alias identifier identifier ;
17230
17231 This function registers the alias mapping with the Objective-C front-end.
17232 It returns nothing. */
17233
17234 static void
17235 cp_parser_objc_alias_declaration (cp_parser* parser)
17236 {
17237 tree alias, orig;
17238
17239 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17240 alias = cp_parser_identifier (parser);
17241 orig = cp_parser_identifier (parser);
17242 objc_declare_alias (alias, orig);
17243 cp_parser_consume_semicolon_at_end_of_statement (parser);
17244 }
17245
17246 /* Parse an Objective-C class forward-declaration.
17247
17248 objc-class-declaration:
17249 @class objc-identifier-list ;
17250
17251 The function registers the forward declarations with the Objective-C
17252 front-end. It returns nothing. */
17253
17254 static void
17255 cp_parser_objc_class_declaration (cp_parser* parser)
17256 {
17257 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17258 objc_declare_class (cp_parser_objc_identifier_list (parser));
17259 cp_parser_consume_semicolon_at_end_of_statement (parser);
17260 }
17261
17262 /* Parse a list of Objective-C protocol references.
17263
17264 objc-protocol-refs-opt:
17265 objc-protocol-refs [opt]
17266
17267 objc-protocol-refs:
17268 < objc-identifier-list >
17269
17270 Returns a TREE_LIST of identifiers, if any. */
17271
17272 static tree
17273 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17274 {
17275 tree protorefs = NULL_TREE;
17276
17277 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17278 {
17279 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17280 protorefs = cp_parser_objc_identifier_list (parser);
17281 cp_parser_require (parser, CPP_GREATER, "`>'");
17282 }
17283
17284 return protorefs;
17285 }
17286
17287 /* Parse a Objective-C visibility specification. */
17288
17289 static void
17290 cp_parser_objc_visibility_spec (cp_parser* parser)
17291 {
17292 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17293
17294 switch (vis->keyword)
17295 {
17296 case RID_AT_PRIVATE:
17297 objc_set_visibility (2);
17298 break;
17299 case RID_AT_PROTECTED:
17300 objc_set_visibility (0);
17301 break;
17302 case RID_AT_PUBLIC:
17303 objc_set_visibility (1);
17304 break;
17305 default:
17306 return;
17307 }
17308
17309 /* Eat '@private'/'@protected'/'@public'. */
17310 cp_lexer_consume_token (parser->lexer);
17311 }
17312
17313 /* Parse an Objective-C method type. */
17314
17315 static void
17316 cp_parser_objc_method_type (cp_parser* parser)
17317 {
17318 objc_set_method_type
17319 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17320 ? PLUS_EXPR
17321 : MINUS_EXPR);
17322 }
17323
17324 /* Parse an Objective-C protocol qualifier. */
17325
17326 static tree
17327 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17328 {
17329 tree quals = NULL_TREE, node;
17330 cp_token *token = cp_lexer_peek_token (parser->lexer);
17331
17332 node = token->value;
17333
17334 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17335 && (node == ridpointers [(int) RID_IN]
17336 || node == ridpointers [(int) RID_OUT]
17337 || node == ridpointers [(int) RID_INOUT]
17338 || node == ridpointers [(int) RID_BYCOPY]
17339 || node == ridpointers [(int) RID_BYREF]
17340 || node == ridpointers [(int) RID_ONEWAY]))
17341 {
17342 quals = tree_cons (NULL_TREE, node, quals);
17343 cp_lexer_consume_token (parser->lexer);
17344 token = cp_lexer_peek_token (parser->lexer);
17345 node = token->value;
17346 }
17347
17348 return quals;
17349 }
17350
17351 /* Parse an Objective-C typename. */
17352
17353 static tree
17354 cp_parser_objc_typename (cp_parser* parser)
17355 {
17356 tree typename = NULL_TREE;
17357
17358 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17359 {
17360 tree proto_quals, cp_type = NULL_TREE;
17361
17362 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17363 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17364
17365 /* An ObjC type name may consist of just protocol qualifiers, in which
17366 case the type shall default to 'id'. */
17367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17368 cp_type = cp_parser_type_id (parser);
17369
17370 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17371 typename = build_tree_list (proto_quals, cp_type);
17372 }
17373
17374 return typename;
17375 }
17376
17377 /* Check to see if TYPE refers to an Objective-C selector name. */
17378
17379 static bool
17380 cp_parser_objc_selector_p (enum cpp_ttype type)
17381 {
17382 return (type == CPP_NAME || type == CPP_KEYWORD
17383 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17384 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17385 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17386 || type == CPP_XOR || type == CPP_XOR_EQ);
17387 }
17388
17389 /* Parse an Objective-C selector. */
17390
17391 static tree
17392 cp_parser_objc_selector (cp_parser* parser)
17393 {
17394 cp_token *token = cp_lexer_consume_token (parser->lexer);
17395
17396 if (!cp_parser_objc_selector_p (token->type))
17397 {
17398 error ("invalid Objective-C++ selector name");
17399 return error_mark_node;
17400 }
17401
17402 /* C++ operator names are allowed to appear in ObjC selectors. */
17403 switch (token->type)
17404 {
17405 case CPP_AND_AND: return get_identifier ("and");
17406 case CPP_AND_EQ: return get_identifier ("and_eq");
17407 case CPP_AND: return get_identifier ("bitand");
17408 case CPP_OR: return get_identifier ("bitor");
17409 case CPP_COMPL: return get_identifier ("compl");
17410 case CPP_NOT: return get_identifier ("not");
17411 case CPP_NOT_EQ: return get_identifier ("not_eq");
17412 case CPP_OR_OR: return get_identifier ("or");
17413 case CPP_OR_EQ: return get_identifier ("or_eq");
17414 case CPP_XOR: return get_identifier ("xor");
17415 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17416 default: return token->value;
17417 }
17418 }
17419
17420 /* Parse an Objective-C params list. */
17421
17422 static tree
17423 cp_parser_objc_method_keyword_params (cp_parser* parser)
17424 {
17425 tree params = NULL_TREE;
17426 bool maybe_unary_selector_p = true;
17427 cp_token *token = cp_lexer_peek_token (parser->lexer);
17428
17429 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17430 {
17431 tree selector = NULL_TREE, typename, identifier;
17432
17433 if (token->type != CPP_COLON)
17434 selector = cp_parser_objc_selector (parser);
17435
17436 /* Detect if we have a unary selector. */
17437 if (maybe_unary_selector_p
17438 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17439 return selector;
17440
17441 maybe_unary_selector_p = false;
17442 cp_parser_require (parser, CPP_COLON, "`:'");
17443 typename = cp_parser_objc_typename (parser);
17444 identifier = cp_parser_identifier (parser);
17445
17446 params
17447 = chainon (params,
17448 objc_build_keyword_decl (selector,
17449 typename,
17450 identifier));
17451
17452 token = cp_lexer_peek_token (parser->lexer);
17453 }
17454
17455 return params;
17456 }
17457
17458 /* Parse the non-keyword Objective-C params. */
17459
17460 static tree
17461 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17462 {
17463 tree params = make_node (TREE_LIST);
17464 cp_token *token = cp_lexer_peek_token (parser->lexer);
17465 *ellipsisp = false; /* Initially, assume no ellipsis. */
17466
17467 while (token->type == CPP_COMMA)
17468 {
17469 cp_parameter_declarator *parmdecl;
17470 tree parm;
17471
17472 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17473 token = cp_lexer_peek_token (parser->lexer);
17474
17475 if (token->type == CPP_ELLIPSIS)
17476 {
17477 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17478 *ellipsisp = true;
17479 break;
17480 }
17481
17482 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17483 parm = grokdeclarator (parmdecl->declarator,
17484 &parmdecl->decl_specifiers,
17485 PARM, /*initialized=*/0,
17486 /*attrlist=*/NULL);
17487
17488 chainon (params, build_tree_list (NULL_TREE, parm));
17489 token = cp_lexer_peek_token (parser->lexer);
17490 }
17491
17492 return params;
17493 }
17494
17495 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17496
17497 static void
17498 cp_parser_objc_interstitial_code (cp_parser* parser)
17499 {
17500 cp_token *token = cp_lexer_peek_token (parser->lexer);
17501
17502 /* If the next token is `extern' and the following token is a string
17503 literal, then we have a linkage specification. */
17504 if (token->keyword == RID_EXTERN
17505 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17506 cp_parser_linkage_specification (parser);
17507 /* Handle #pragma, if any. */
17508 else if (token->type == CPP_PRAGMA)
17509 cp_parser_pragma (parser, pragma_external);
17510 /* Allow stray semicolons. */
17511 else if (token->type == CPP_SEMICOLON)
17512 cp_lexer_consume_token (parser->lexer);
17513 /* Finally, try to parse a block-declaration, or a function-definition. */
17514 else
17515 cp_parser_block_declaration (parser, /*statement_p=*/false);
17516 }
17517
17518 /* Parse a method signature. */
17519
17520 static tree
17521 cp_parser_objc_method_signature (cp_parser* parser)
17522 {
17523 tree rettype, kwdparms, optparms;
17524 bool ellipsis = false;
17525
17526 cp_parser_objc_method_type (parser);
17527 rettype = cp_parser_objc_typename (parser);
17528 kwdparms = cp_parser_objc_method_keyword_params (parser);
17529 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17530
17531 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17532 }
17533
17534 /* Pars an Objective-C method prototype list. */
17535
17536 static void
17537 cp_parser_objc_method_prototype_list (cp_parser* parser)
17538 {
17539 cp_token *token = cp_lexer_peek_token (parser->lexer);
17540
17541 while (token->keyword != RID_AT_END)
17542 {
17543 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17544 {
17545 objc_add_method_declaration
17546 (cp_parser_objc_method_signature (parser));
17547 cp_parser_consume_semicolon_at_end_of_statement (parser);
17548 }
17549 else
17550 /* Allow for interspersed non-ObjC++ code. */
17551 cp_parser_objc_interstitial_code (parser);
17552
17553 token = cp_lexer_peek_token (parser->lexer);
17554 }
17555
17556 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17557 objc_finish_interface ();
17558 }
17559
17560 /* Parse an Objective-C method definition list. */
17561
17562 static void
17563 cp_parser_objc_method_definition_list (cp_parser* parser)
17564 {
17565 cp_token *token = cp_lexer_peek_token (parser->lexer);
17566
17567 while (token->keyword != RID_AT_END)
17568 {
17569 tree meth;
17570
17571 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17572 {
17573 push_deferring_access_checks (dk_deferred);
17574 objc_start_method_definition
17575 (cp_parser_objc_method_signature (parser));
17576
17577 /* For historical reasons, we accept an optional semicolon. */
17578 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17579 cp_lexer_consume_token (parser->lexer);
17580
17581 perform_deferred_access_checks ();
17582 stop_deferring_access_checks ();
17583 meth = cp_parser_function_definition_after_declarator (parser,
17584 false);
17585 pop_deferring_access_checks ();
17586 objc_finish_method_definition (meth);
17587 }
17588 else
17589 /* Allow for interspersed non-ObjC++ code. */
17590 cp_parser_objc_interstitial_code (parser);
17591
17592 token = cp_lexer_peek_token (parser->lexer);
17593 }
17594
17595 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17596 objc_finish_implementation ();
17597 }
17598
17599 /* Parse Objective-C ivars. */
17600
17601 static void
17602 cp_parser_objc_class_ivars (cp_parser* parser)
17603 {
17604 cp_token *token = cp_lexer_peek_token (parser->lexer);
17605
17606 if (token->type != CPP_OPEN_BRACE)
17607 return; /* No ivars specified. */
17608
17609 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17610 token = cp_lexer_peek_token (parser->lexer);
17611
17612 while (token->type != CPP_CLOSE_BRACE)
17613 {
17614 cp_decl_specifier_seq declspecs;
17615 int decl_class_or_enum_p;
17616 tree prefix_attributes;
17617
17618 cp_parser_objc_visibility_spec (parser);
17619
17620 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17621 break;
17622
17623 cp_parser_decl_specifier_seq (parser,
17624 CP_PARSER_FLAGS_OPTIONAL,
17625 &declspecs,
17626 &decl_class_or_enum_p);
17627 prefix_attributes = declspecs.attributes;
17628 declspecs.attributes = NULL_TREE;
17629
17630 /* Keep going until we hit the `;' at the end of the
17631 declaration. */
17632 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17633 {
17634 tree width = NULL_TREE, attributes, first_attribute, decl;
17635 cp_declarator *declarator = NULL;
17636 int ctor_dtor_or_conv_p;
17637
17638 /* Check for a (possibly unnamed) bitfield declaration. */
17639 token = cp_lexer_peek_token (parser->lexer);
17640 if (token->type == CPP_COLON)
17641 goto eat_colon;
17642
17643 if (token->type == CPP_NAME
17644 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17645 == CPP_COLON))
17646 {
17647 /* Get the name of the bitfield. */
17648 declarator = make_id_declarator (NULL_TREE,
17649 cp_parser_identifier (parser),
17650 sfk_none);
17651
17652 eat_colon:
17653 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17654 /* Get the width of the bitfield. */
17655 width
17656 = cp_parser_constant_expression (parser,
17657 /*allow_non_constant=*/false,
17658 NULL);
17659 }
17660 else
17661 {
17662 /* Parse the declarator. */
17663 declarator
17664 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17665 &ctor_dtor_or_conv_p,
17666 /*parenthesized_p=*/NULL,
17667 /*member_p=*/false);
17668 }
17669
17670 /* Look for attributes that apply to the ivar. */
17671 attributes = cp_parser_attributes_opt (parser);
17672 /* Remember which attributes are prefix attributes and
17673 which are not. */
17674 first_attribute = attributes;
17675 /* Combine the attributes. */
17676 attributes = chainon (prefix_attributes, attributes);
17677
17678 if (width)
17679 {
17680 /* Create the bitfield declaration. */
17681 decl = grokbitfield (declarator, &declspecs, width);
17682 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17683 }
17684 else
17685 decl = grokfield (declarator, &declspecs,
17686 NULL_TREE, /*init_const_expr_p=*/false,
17687 NULL_TREE, attributes);
17688
17689 /* Add the instance variable. */
17690 objc_add_instance_variable (decl);
17691
17692 /* Reset PREFIX_ATTRIBUTES. */
17693 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17694 attributes = TREE_CHAIN (attributes);
17695 if (attributes)
17696 TREE_CHAIN (attributes) = NULL_TREE;
17697
17698 token = cp_lexer_peek_token (parser->lexer);
17699
17700 if (token->type == CPP_COMMA)
17701 {
17702 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17703 continue;
17704 }
17705 break;
17706 }
17707
17708 cp_parser_consume_semicolon_at_end_of_statement (parser);
17709 token = cp_lexer_peek_token (parser->lexer);
17710 }
17711
17712 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17713 /* For historical reasons, we accept an optional semicolon. */
17714 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17715 cp_lexer_consume_token (parser->lexer);
17716 }
17717
17718 /* Parse an Objective-C protocol declaration. */
17719
17720 static void
17721 cp_parser_objc_protocol_declaration (cp_parser* parser)
17722 {
17723 tree proto, protorefs;
17724 cp_token *tok;
17725
17726 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17728 {
17729 error ("identifier expected after %<@protocol%>");
17730 goto finish;
17731 }
17732
17733 /* See if we have a forward declaration or a definition. */
17734 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17735
17736 /* Try a forward declaration first. */
17737 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17738 {
17739 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17740 finish:
17741 cp_parser_consume_semicolon_at_end_of_statement (parser);
17742 }
17743
17744 /* Ok, we got a full-fledged definition (or at least should). */
17745 else
17746 {
17747 proto = cp_parser_identifier (parser);
17748 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17749 objc_start_protocol (proto, protorefs);
17750 cp_parser_objc_method_prototype_list (parser);
17751 }
17752 }
17753
17754 /* Parse an Objective-C superclass or category. */
17755
17756 static void
17757 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17758 tree *categ)
17759 {
17760 cp_token *next = cp_lexer_peek_token (parser->lexer);
17761
17762 *super = *categ = NULL_TREE;
17763 if (next->type == CPP_COLON)
17764 {
17765 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17766 *super = cp_parser_identifier (parser);
17767 }
17768 else if (next->type == CPP_OPEN_PAREN)
17769 {
17770 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17771 *categ = cp_parser_identifier (parser);
17772 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17773 }
17774 }
17775
17776 /* Parse an Objective-C class interface. */
17777
17778 static void
17779 cp_parser_objc_class_interface (cp_parser* parser)
17780 {
17781 tree name, super, categ, protos;
17782
17783 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17784 name = cp_parser_identifier (parser);
17785 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17786 protos = cp_parser_objc_protocol_refs_opt (parser);
17787
17788 /* We have either a class or a category on our hands. */
17789 if (categ)
17790 objc_start_category_interface (name, categ, protos);
17791 else
17792 {
17793 objc_start_class_interface (name, super, protos);
17794 /* Handle instance variable declarations, if any. */
17795 cp_parser_objc_class_ivars (parser);
17796 objc_continue_interface ();
17797 }
17798
17799 cp_parser_objc_method_prototype_list (parser);
17800 }
17801
17802 /* Parse an Objective-C class implementation. */
17803
17804 static void
17805 cp_parser_objc_class_implementation (cp_parser* parser)
17806 {
17807 tree name, super, categ;
17808
17809 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17810 name = cp_parser_identifier (parser);
17811 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17812
17813 /* We have either a class or a category on our hands. */
17814 if (categ)
17815 objc_start_category_implementation (name, categ);
17816 else
17817 {
17818 objc_start_class_implementation (name, super);
17819 /* Handle instance variable declarations, if any. */
17820 cp_parser_objc_class_ivars (parser);
17821 objc_continue_implementation ();
17822 }
17823
17824 cp_parser_objc_method_definition_list (parser);
17825 }
17826
17827 /* Consume the @end token and finish off the implementation. */
17828
17829 static void
17830 cp_parser_objc_end_implementation (cp_parser* parser)
17831 {
17832 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17833 objc_finish_implementation ();
17834 }
17835
17836 /* Parse an Objective-C declaration. */
17837
17838 static void
17839 cp_parser_objc_declaration (cp_parser* parser)
17840 {
17841 /* Try to figure out what kind of declaration is present. */
17842 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17843
17844 switch (kwd->keyword)
17845 {
17846 case RID_AT_ALIAS:
17847 cp_parser_objc_alias_declaration (parser);
17848 break;
17849 case RID_AT_CLASS:
17850 cp_parser_objc_class_declaration (parser);
17851 break;
17852 case RID_AT_PROTOCOL:
17853 cp_parser_objc_protocol_declaration (parser);
17854 break;
17855 case RID_AT_INTERFACE:
17856 cp_parser_objc_class_interface (parser);
17857 break;
17858 case RID_AT_IMPLEMENTATION:
17859 cp_parser_objc_class_implementation (parser);
17860 break;
17861 case RID_AT_END:
17862 cp_parser_objc_end_implementation (parser);
17863 break;
17864 default:
17865 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17866 cp_parser_skip_to_end_of_block_or_statement (parser);
17867 }
17868 }
17869
17870 /* Parse an Objective-C try-catch-finally statement.
17871
17872 objc-try-catch-finally-stmt:
17873 @try compound-statement objc-catch-clause-seq [opt]
17874 objc-finally-clause [opt]
17875
17876 objc-catch-clause-seq:
17877 objc-catch-clause objc-catch-clause-seq [opt]
17878
17879 objc-catch-clause:
17880 @catch ( exception-declaration ) compound-statement
17881
17882 objc-finally-clause
17883 @finally compound-statement
17884
17885 Returns NULL_TREE. */
17886
17887 static tree
17888 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17889 location_t location;
17890 tree stmt;
17891
17892 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17893 location = cp_lexer_peek_token (parser->lexer)->location;
17894 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17895 node, lest it get absorbed into the surrounding block. */
17896 stmt = push_stmt_list ();
17897 cp_parser_compound_statement (parser, NULL, false);
17898 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17899
17900 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17901 {
17902 cp_parameter_declarator *parmdecl;
17903 tree parm;
17904
17905 cp_lexer_consume_token (parser->lexer);
17906 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17907 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17908 parm = grokdeclarator (parmdecl->declarator,
17909 &parmdecl->decl_specifiers,
17910 PARM, /*initialized=*/0,
17911 /*attrlist=*/NULL);
17912 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17913 objc_begin_catch_clause (parm);
17914 cp_parser_compound_statement (parser, NULL, false);
17915 objc_finish_catch_clause ();
17916 }
17917
17918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17919 {
17920 cp_lexer_consume_token (parser->lexer);
17921 location = cp_lexer_peek_token (parser->lexer)->location;
17922 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17923 node, lest it get absorbed into the surrounding block. */
17924 stmt = push_stmt_list ();
17925 cp_parser_compound_statement (parser, NULL, false);
17926 objc_build_finally_clause (location, pop_stmt_list (stmt));
17927 }
17928
17929 return objc_finish_try_stmt ();
17930 }
17931
17932 /* Parse an Objective-C synchronized statement.
17933
17934 objc-synchronized-stmt:
17935 @synchronized ( expression ) compound-statement
17936
17937 Returns NULL_TREE. */
17938
17939 static tree
17940 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17941 location_t location;
17942 tree lock, stmt;
17943
17944 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17945
17946 location = cp_lexer_peek_token (parser->lexer)->location;
17947 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17948 lock = cp_parser_expression (parser, false);
17949 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17950
17951 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17952 node, lest it get absorbed into the surrounding block. */
17953 stmt = push_stmt_list ();
17954 cp_parser_compound_statement (parser, NULL, false);
17955
17956 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17957 }
17958
17959 /* Parse an Objective-C throw statement.
17960
17961 objc-throw-stmt:
17962 @throw assignment-expression [opt] ;
17963
17964 Returns a constructed '@throw' statement. */
17965
17966 static tree
17967 cp_parser_objc_throw_statement (cp_parser *parser) {
17968 tree expr = NULL_TREE;
17969
17970 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17971
17972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17973 expr = cp_parser_assignment_expression (parser, false);
17974
17975 cp_parser_consume_semicolon_at_end_of_statement (parser);
17976
17977 return objc_build_throw_stmt (expr);
17978 }
17979
17980 /* Parse an Objective-C statement. */
17981
17982 static tree
17983 cp_parser_objc_statement (cp_parser * parser) {
17984 /* Try to figure out what kind of declaration is present. */
17985 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17986
17987 switch (kwd->keyword)
17988 {
17989 case RID_AT_TRY:
17990 return cp_parser_objc_try_catch_finally_statement (parser);
17991 case RID_AT_SYNCHRONIZED:
17992 return cp_parser_objc_synchronized_statement (parser);
17993 case RID_AT_THROW:
17994 return cp_parser_objc_throw_statement (parser);
17995 default:
17996 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17997 cp_parser_skip_to_end_of_block_or_statement (parser);
17998 }
17999
18000 return error_mark_node;
18001 }
18002 \f
18003 /* OpenMP 2.5 parsing routines. */
18004
18005 /* All OpenMP clauses. OpenMP 2.5. */
18006 typedef enum pragma_omp_clause {
18007 PRAGMA_OMP_CLAUSE_NONE = 0,
18008
18009 PRAGMA_OMP_CLAUSE_COPYIN,
18010 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
18011 PRAGMA_OMP_CLAUSE_DEFAULT,
18012 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
18013 PRAGMA_OMP_CLAUSE_IF,
18014 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
18015 PRAGMA_OMP_CLAUSE_NOWAIT,
18016 PRAGMA_OMP_CLAUSE_NUM_THREADS,
18017 PRAGMA_OMP_CLAUSE_ORDERED,
18018 PRAGMA_OMP_CLAUSE_PRIVATE,
18019 PRAGMA_OMP_CLAUSE_REDUCTION,
18020 PRAGMA_OMP_CLAUSE_SCHEDULE,
18021 PRAGMA_OMP_CLAUSE_SHARED
18022 } pragma_omp_clause;
18023
18024 /* Returns name of the next clause.
18025 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18026 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18027 returned and the token is consumed. */
18028
18029 static pragma_omp_clause
18030 cp_parser_omp_clause_name (cp_parser *parser)
18031 {
18032 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18033
18034 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18035 result = PRAGMA_OMP_CLAUSE_IF;
18036 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18037 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18038 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18039 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18040 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18041 {
18042 tree id = cp_lexer_peek_token (parser->lexer)->value;
18043 const char *p = IDENTIFIER_POINTER (id);
18044
18045 switch (p[0])
18046 {
18047 case 'c':
18048 if (!strcmp ("copyin", p))
18049 result = PRAGMA_OMP_CLAUSE_COPYIN;
18050 else if (!strcmp ("copyprivate", p))
18051 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18052 break;
18053 case 'f':
18054 if (!strcmp ("firstprivate", p))
18055 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18056 break;
18057 case 'l':
18058 if (!strcmp ("lastprivate", p))
18059 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18060 break;
18061 case 'n':
18062 if (!strcmp ("nowait", p))
18063 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18064 else if (!strcmp ("num_threads", p))
18065 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18066 break;
18067 case 'o':
18068 if (!strcmp ("ordered", p))
18069 result = PRAGMA_OMP_CLAUSE_ORDERED;
18070 break;
18071 case 'r':
18072 if (!strcmp ("reduction", p))
18073 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18074 break;
18075 case 's':
18076 if (!strcmp ("schedule", p))
18077 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18078 else if (!strcmp ("shared", p))
18079 result = PRAGMA_OMP_CLAUSE_SHARED;
18080 break;
18081 }
18082 }
18083
18084 if (result != PRAGMA_OMP_CLAUSE_NONE)
18085 cp_lexer_consume_token (parser->lexer);
18086
18087 return result;
18088 }
18089
18090 /* Validate that a clause of the given type does not already exist. */
18091
18092 static void
18093 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18094 {
18095 tree c;
18096
18097 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18098 if (OMP_CLAUSE_CODE (c) == code)
18099 {
18100 error ("too many %qs clauses", name);
18101 break;
18102 }
18103 }
18104
18105 /* OpenMP 2.5:
18106 variable-list:
18107 identifier
18108 variable-list , identifier
18109
18110 In addition, we match a closing parenthesis. An opening parenthesis
18111 will have been consumed by the caller.
18112
18113 If KIND is nonzero, create the appropriate node and install the decl
18114 in OMP_CLAUSE_DECL and add the node to the head of the list.
18115
18116 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18117 return the list created. */
18118
18119 static tree
18120 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18121 tree list)
18122 {
18123 while (1)
18124 {
18125 tree name, decl;
18126
18127 name = cp_parser_id_expression (parser, /*template_p=*/false,
18128 /*check_dependency_p=*/true,
18129 /*template_p=*/NULL,
18130 /*declarator_p=*/false,
18131 /*optional_p=*/false);
18132 if (name == error_mark_node)
18133 goto skip_comma;
18134
18135 decl = cp_parser_lookup_name_simple (parser, name);
18136 if (decl == error_mark_node)
18137 cp_parser_name_lookup_error (parser, name, decl, NULL);
18138 else if (kind != 0)
18139 {
18140 tree u = build_omp_clause (kind);
18141 OMP_CLAUSE_DECL (u) = decl;
18142 OMP_CLAUSE_CHAIN (u) = list;
18143 list = u;
18144 }
18145 else
18146 list = tree_cons (decl, NULL_TREE, list);
18147
18148 get_comma:
18149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18150 break;
18151 cp_lexer_consume_token (parser->lexer);
18152 }
18153
18154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18155 {
18156 int ending;
18157
18158 /* Try to resync to an unnested comma. Copied from
18159 cp_parser_parenthesized_expression_list. */
18160 skip_comma:
18161 ending = cp_parser_skip_to_closing_parenthesis (parser,
18162 /*recovering=*/true,
18163 /*or_comma=*/true,
18164 /*consume_paren=*/true);
18165 if (ending < 0)
18166 goto get_comma;
18167 }
18168
18169 return list;
18170 }
18171
18172 /* Similarly, but expect leading and trailing parenthesis. This is a very
18173 common case for omp clauses. */
18174
18175 static tree
18176 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18177 {
18178 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18179 return cp_parser_omp_var_list_no_open (parser, kind, list);
18180 return list;
18181 }
18182
18183 /* OpenMP 2.5:
18184 default ( shared | none ) */
18185
18186 static tree
18187 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18188 {
18189 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18190 tree c;
18191
18192 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18193 return list;
18194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18195 {
18196 tree id = cp_lexer_peek_token (parser->lexer)->value;
18197 const char *p = IDENTIFIER_POINTER (id);
18198
18199 switch (p[0])
18200 {
18201 case 'n':
18202 if (strcmp ("none", p) != 0)
18203 goto invalid_kind;
18204 kind = OMP_CLAUSE_DEFAULT_NONE;
18205 break;
18206
18207 case 's':
18208 if (strcmp ("shared", p) != 0)
18209 goto invalid_kind;
18210 kind = OMP_CLAUSE_DEFAULT_SHARED;
18211 break;
18212
18213 default:
18214 goto invalid_kind;
18215 }
18216
18217 cp_lexer_consume_token (parser->lexer);
18218 }
18219 else
18220 {
18221 invalid_kind:
18222 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18223 }
18224
18225 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18226 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18227 /*or_comma=*/false,
18228 /*consume_paren=*/true);
18229
18230 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18231 return list;
18232
18233 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18234 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18235 OMP_CLAUSE_CHAIN (c) = list;
18236 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18237
18238 return c;
18239 }
18240
18241 /* OpenMP 2.5:
18242 if ( expression ) */
18243
18244 static tree
18245 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18246 {
18247 tree t, c;
18248
18249 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18250 return list;
18251
18252 t = cp_parser_condition (parser);
18253
18254 if (t == error_mark_node
18255 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18256 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18257 /*or_comma=*/false,
18258 /*consume_paren=*/true);
18259
18260 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18261
18262 c = build_omp_clause (OMP_CLAUSE_IF);
18263 OMP_CLAUSE_IF_EXPR (c) = t;
18264 OMP_CLAUSE_CHAIN (c) = list;
18265
18266 return c;
18267 }
18268
18269 /* OpenMP 2.5:
18270 nowait */
18271
18272 static tree
18273 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18274 {
18275 tree c;
18276
18277 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18278
18279 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18280 OMP_CLAUSE_CHAIN (c) = list;
18281 return c;
18282 }
18283
18284 /* OpenMP 2.5:
18285 num_threads ( expression ) */
18286
18287 static tree
18288 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18289 {
18290 tree t, c;
18291
18292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18293 return list;
18294
18295 t = cp_parser_expression (parser, false);
18296
18297 if (t == error_mark_node
18298 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18299 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18300 /*or_comma=*/false,
18301 /*consume_paren=*/true);
18302
18303 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18304
18305 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18306 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18307 OMP_CLAUSE_CHAIN (c) = list;
18308
18309 return c;
18310 }
18311
18312 /* OpenMP 2.5:
18313 ordered */
18314
18315 static tree
18316 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18317 {
18318 tree c;
18319
18320 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18321
18322 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18323 OMP_CLAUSE_CHAIN (c) = list;
18324 return c;
18325 }
18326
18327 /* OpenMP 2.5:
18328 reduction ( reduction-operator : variable-list )
18329
18330 reduction-operator:
18331 One of: + * - & ^ | && || */
18332
18333 static tree
18334 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18335 {
18336 enum tree_code code;
18337 tree nlist, c;
18338
18339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18340 return list;
18341
18342 switch (cp_lexer_peek_token (parser->lexer)->type)
18343 {
18344 case CPP_PLUS:
18345 code = PLUS_EXPR;
18346 break;
18347 case CPP_MULT:
18348 code = MULT_EXPR;
18349 break;
18350 case CPP_MINUS:
18351 code = MINUS_EXPR;
18352 break;
18353 case CPP_AND:
18354 code = BIT_AND_EXPR;
18355 break;
18356 case CPP_XOR:
18357 code = BIT_XOR_EXPR;
18358 break;
18359 case CPP_OR:
18360 code = BIT_IOR_EXPR;
18361 break;
18362 case CPP_AND_AND:
18363 code = TRUTH_ANDIF_EXPR;
18364 break;
18365 case CPP_OR_OR:
18366 code = TRUTH_ORIF_EXPR;
18367 break;
18368 default:
18369 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18370 resync_fail:
18371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18372 /*or_comma=*/false,
18373 /*consume_paren=*/true);
18374 return list;
18375 }
18376 cp_lexer_consume_token (parser->lexer);
18377
18378 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18379 goto resync_fail;
18380
18381 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18382 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18383 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18384
18385 return nlist;
18386 }
18387
18388 /* OpenMP 2.5:
18389 schedule ( schedule-kind )
18390 schedule ( schedule-kind , expression )
18391
18392 schedule-kind:
18393 static | dynamic | guided | runtime */
18394
18395 static tree
18396 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18397 {
18398 tree c, t;
18399
18400 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18401 return list;
18402
18403 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18404
18405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18406 {
18407 tree id = cp_lexer_peek_token (parser->lexer)->value;
18408 const char *p = IDENTIFIER_POINTER (id);
18409
18410 switch (p[0])
18411 {
18412 case 'd':
18413 if (strcmp ("dynamic", p) != 0)
18414 goto invalid_kind;
18415 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18416 break;
18417
18418 case 'g':
18419 if (strcmp ("guided", p) != 0)
18420 goto invalid_kind;
18421 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18422 break;
18423
18424 case 'r':
18425 if (strcmp ("runtime", p) != 0)
18426 goto invalid_kind;
18427 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18428 break;
18429
18430 default:
18431 goto invalid_kind;
18432 }
18433 }
18434 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18435 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18436 else
18437 goto invalid_kind;
18438 cp_lexer_consume_token (parser->lexer);
18439
18440 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18441 {
18442 cp_lexer_consume_token (parser->lexer);
18443
18444 t = cp_parser_assignment_expression (parser, false);
18445
18446 if (t == error_mark_node)
18447 goto resync_fail;
18448 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18449 error ("schedule %<runtime%> does not take "
18450 "a %<chunk_size%> parameter");
18451 else
18452 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18453
18454 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18455 goto resync_fail;
18456 }
18457 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18458 goto resync_fail;
18459
18460 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18461 OMP_CLAUSE_CHAIN (c) = list;
18462 return c;
18463
18464 invalid_kind:
18465 cp_parser_error (parser, "invalid schedule kind");
18466 resync_fail:
18467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18468 /*or_comma=*/false,
18469 /*consume_paren=*/true);
18470 return list;
18471 }
18472
18473 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18474 is a bitmask in MASK. Return the list of clauses found; the result
18475 of clause default goes in *pdefault. */
18476
18477 static tree
18478 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18479 const char *where, cp_token *pragma_tok)
18480 {
18481 tree clauses = NULL;
18482
18483 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18484 {
18485 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18486 const char *c_name;
18487 tree prev = clauses;
18488
18489 switch (c_kind)
18490 {
18491 case PRAGMA_OMP_CLAUSE_COPYIN:
18492 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18493 c_name = "copyin";
18494 break;
18495 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18496 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18497 clauses);
18498 c_name = "copyprivate";
18499 break;
18500 case PRAGMA_OMP_CLAUSE_DEFAULT:
18501 clauses = cp_parser_omp_clause_default (parser, clauses);
18502 c_name = "default";
18503 break;
18504 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18505 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18506 clauses);
18507 c_name = "firstprivate";
18508 break;
18509 case PRAGMA_OMP_CLAUSE_IF:
18510 clauses = cp_parser_omp_clause_if (parser, clauses);
18511 c_name = "if";
18512 break;
18513 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18514 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18515 clauses);
18516 c_name = "lastprivate";
18517 break;
18518 case PRAGMA_OMP_CLAUSE_NOWAIT:
18519 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18520 c_name = "nowait";
18521 break;
18522 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18523 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18524 c_name = "num_threads";
18525 break;
18526 case PRAGMA_OMP_CLAUSE_ORDERED:
18527 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18528 c_name = "ordered";
18529 break;
18530 case PRAGMA_OMP_CLAUSE_PRIVATE:
18531 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18532 clauses);
18533 c_name = "private";
18534 break;
18535 case PRAGMA_OMP_CLAUSE_REDUCTION:
18536 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18537 c_name = "reduction";
18538 break;
18539 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18540 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18541 c_name = "schedule";
18542 break;
18543 case PRAGMA_OMP_CLAUSE_SHARED:
18544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18545 clauses);
18546 c_name = "shared";
18547 break;
18548 default:
18549 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18550 goto saw_error;
18551 }
18552
18553 if (((mask >> c_kind) & 1) == 0)
18554 {
18555 /* Remove the invalid clause(s) from the list to avoid
18556 confusing the rest of the compiler. */
18557 clauses = prev;
18558 error ("%qs is not valid for %qs", c_name, where);
18559 }
18560 }
18561 saw_error:
18562 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18563 return finish_omp_clauses (clauses);
18564 }
18565
18566 /* OpenMP 2.5:
18567 structured-block:
18568 statement
18569
18570 In practice, we're also interested in adding the statement to an
18571 outer node. So it is convenient if we work around the fact that
18572 cp_parser_statement calls add_stmt. */
18573
18574 static unsigned
18575 cp_parser_begin_omp_structured_block (cp_parser *parser)
18576 {
18577 unsigned save = parser->in_statement;
18578
18579 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18580 This preserves the "not within loop or switch" style error messages
18581 for nonsense cases like
18582 void foo() {
18583 #pragma omp single
18584 break;
18585 }
18586 */
18587 if (parser->in_statement)
18588 parser->in_statement = IN_OMP_BLOCK;
18589
18590 return save;
18591 }
18592
18593 static void
18594 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18595 {
18596 parser->in_statement = save;
18597 }
18598
18599 static tree
18600 cp_parser_omp_structured_block (cp_parser *parser)
18601 {
18602 tree stmt = begin_omp_structured_block ();
18603 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18604
18605 cp_parser_statement (parser, NULL_TREE, false);
18606
18607 cp_parser_end_omp_structured_block (parser, save);
18608 return finish_omp_structured_block (stmt);
18609 }
18610
18611 /* OpenMP 2.5:
18612 # pragma omp atomic new-line
18613 expression-stmt
18614
18615 expression-stmt:
18616 x binop= expr | x++ | ++x | x-- | --x
18617 binop:
18618 +, *, -, /, &, ^, |, <<, >>
18619
18620 where x is an lvalue expression with scalar type. */
18621
18622 static void
18623 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18624 {
18625 tree lhs, rhs;
18626 enum tree_code code;
18627
18628 cp_parser_require_pragma_eol (parser, pragma_tok);
18629
18630 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18631 /*cast_p=*/false);
18632 switch (TREE_CODE (lhs))
18633 {
18634 case ERROR_MARK:
18635 goto saw_error;
18636
18637 case PREINCREMENT_EXPR:
18638 case POSTINCREMENT_EXPR:
18639 lhs = TREE_OPERAND (lhs, 0);
18640 code = PLUS_EXPR;
18641 rhs = integer_one_node;
18642 break;
18643
18644 case PREDECREMENT_EXPR:
18645 case POSTDECREMENT_EXPR:
18646 lhs = TREE_OPERAND (lhs, 0);
18647 code = MINUS_EXPR;
18648 rhs = integer_one_node;
18649 break;
18650
18651 default:
18652 switch (cp_lexer_peek_token (parser->lexer)->type)
18653 {
18654 case CPP_MULT_EQ:
18655 code = MULT_EXPR;
18656 break;
18657 case CPP_DIV_EQ:
18658 code = TRUNC_DIV_EXPR;
18659 break;
18660 case CPP_PLUS_EQ:
18661 code = PLUS_EXPR;
18662 break;
18663 case CPP_MINUS_EQ:
18664 code = MINUS_EXPR;
18665 break;
18666 case CPP_LSHIFT_EQ:
18667 code = LSHIFT_EXPR;
18668 break;
18669 case CPP_RSHIFT_EQ:
18670 code = RSHIFT_EXPR;
18671 break;
18672 case CPP_AND_EQ:
18673 code = BIT_AND_EXPR;
18674 break;
18675 case CPP_OR_EQ:
18676 code = BIT_IOR_EXPR;
18677 break;
18678 case CPP_XOR_EQ:
18679 code = BIT_XOR_EXPR;
18680 break;
18681 default:
18682 cp_parser_error (parser,
18683 "invalid operator for %<#pragma omp atomic%>");
18684 goto saw_error;
18685 }
18686 cp_lexer_consume_token (parser->lexer);
18687
18688 rhs = cp_parser_expression (parser, false);
18689 if (rhs == error_mark_node)
18690 goto saw_error;
18691 break;
18692 }
18693 finish_omp_atomic (code, lhs, rhs);
18694 cp_parser_consume_semicolon_at_end_of_statement (parser);
18695 return;
18696
18697 saw_error:
18698 cp_parser_skip_to_end_of_block_or_statement (parser);
18699 }
18700
18701
18702 /* OpenMP 2.5:
18703 # pragma omp barrier new-line */
18704
18705 static void
18706 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18707 {
18708 cp_parser_require_pragma_eol (parser, pragma_tok);
18709 finish_omp_barrier ();
18710 }
18711
18712 /* OpenMP 2.5:
18713 # pragma omp critical [(name)] new-line
18714 structured-block */
18715
18716 static tree
18717 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18718 {
18719 tree stmt, name = NULL;
18720
18721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18722 {
18723 cp_lexer_consume_token (parser->lexer);
18724
18725 name = cp_parser_identifier (parser);
18726
18727 if (name == error_mark_node
18728 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18730 /*or_comma=*/false,
18731 /*consume_paren=*/true);
18732 if (name == error_mark_node)
18733 name = NULL;
18734 }
18735 cp_parser_require_pragma_eol (parser, pragma_tok);
18736
18737 stmt = cp_parser_omp_structured_block (parser);
18738 return c_finish_omp_critical (stmt, name);
18739 }
18740
18741 /* OpenMP 2.5:
18742 # pragma omp flush flush-vars[opt] new-line
18743
18744 flush-vars:
18745 ( variable-list ) */
18746
18747 static void
18748 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18749 {
18750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18751 (void) cp_parser_omp_var_list (parser, 0, NULL);
18752 cp_parser_require_pragma_eol (parser, pragma_tok);
18753
18754 finish_omp_flush ();
18755 }
18756
18757 /* Parse the restricted form of the for statment allowed by OpenMP. */
18758
18759 static tree
18760 cp_parser_omp_for_loop (cp_parser *parser)
18761 {
18762 tree init, cond, incr, body, decl, pre_body;
18763 location_t loc;
18764
18765 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18766 {
18767 cp_parser_error (parser, "for statement expected");
18768 return NULL;
18769 }
18770 loc = cp_lexer_consume_token (parser->lexer)->location;
18771 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18772 return NULL;
18773
18774 init = decl = NULL;
18775 pre_body = push_stmt_list ();
18776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18777 {
18778 cp_decl_specifier_seq type_specifiers;
18779
18780 /* First, try to parse as an initialized declaration. See
18781 cp_parser_condition, from whence the bulk of this is copied. */
18782
18783 cp_parser_parse_tentatively (parser);
18784 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18785 &type_specifiers);
18786 if (!cp_parser_error_occurred (parser))
18787 {
18788 tree asm_specification, attributes;
18789 cp_declarator *declarator;
18790
18791 declarator = cp_parser_declarator (parser,
18792 CP_PARSER_DECLARATOR_NAMED,
18793 /*ctor_dtor_or_conv_p=*/NULL,
18794 /*parenthesized_p=*/NULL,
18795 /*member_p=*/false);
18796 attributes = cp_parser_attributes_opt (parser);
18797 asm_specification = cp_parser_asm_specification_opt (parser);
18798
18799 cp_parser_require (parser, CPP_EQ, "`='");
18800 if (cp_parser_parse_definitely (parser))
18801 {
18802 tree pushed_scope;
18803
18804 decl = start_decl (declarator, &type_specifiers,
18805 /*initialized_p=*/false, attributes,
18806 /*prefix_attributes=*/NULL_TREE,
18807 &pushed_scope);
18808
18809 init = cp_parser_assignment_expression (parser, false);
18810
18811 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18812 asm_specification, LOOKUP_ONLYCONVERTING);
18813
18814 if (pushed_scope)
18815 pop_scope (pushed_scope);
18816 }
18817 }
18818 else
18819 cp_parser_abort_tentative_parse (parser);
18820
18821 /* If parsing as an initialized declaration failed, try again as
18822 a simple expression. */
18823 if (decl == NULL)
18824 init = cp_parser_expression (parser, false);
18825 }
18826 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18827 pre_body = pop_stmt_list (pre_body);
18828
18829 cond = NULL;
18830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18831 cond = cp_parser_condition (parser);
18832 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18833
18834 incr = NULL;
18835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18836 incr = cp_parser_expression (parser, false);
18837
18838 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18839 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18840 /*or_comma=*/false,
18841 /*consume_paren=*/true);
18842
18843 /* Note that we saved the original contents of this flag when we entered
18844 the structured block, and so we don't need to re-save it here. */
18845 parser->in_statement = IN_OMP_FOR;
18846
18847 /* Note that the grammar doesn't call for a structured block here,
18848 though the loop as a whole is a structured block. */
18849 body = push_stmt_list ();
18850 cp_parser_statement (parser, NULL_TREE, false);
18851 body = pop_stmt_list (body);
18852
18853 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18854 }
18855
18856 /* OpenMP 2.5:
18857 #pragma omp for for-clause[optseq] new-line
18858 for-loop */
18859
18860 #define OMP_FOR_CLAUSE_MASK \
18861 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18862 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18863 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18864 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18865 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18866 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18867 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18868
18869 static tree
18870 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18871 {
18872 tree clauses, sb, ret;
18873 unsigned int save;
18874
18875 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18876 "#pragma omp for", pragma_tok);
18877
18878 sb = begin_omp_structured_block ();
18879 save = cp_parser_begin_omp_structured_block (parser);
18880
18881 ret = cp_parser_omp_for_loop (parser);
18882 if (ret)
18883 OMP_FOR_CLAUSES (ret) = clauses;
18884
18885 cp_parser_end_omp_structured_block (parser, save);
18886 add_stmt (finish_omp_structured_block (sb));
18887
18888 return ret;
18889 }
18890
18891 /* OpenMP 2.5:
18892 # pragma omp master new-line
18893 structured-block */
18894
18895 static tree
18896 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18897 {
18898 cp_parser_require_pragma_eol (parser, pragma_tok);
18899 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18900 }
18901
18902 /* OpenMP 2.5:
18903 # pragma omp ordered new-line
18904 structured-block */
18905
18906 static tree
18907 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18908 {
18909 cp_parser_require_pragma_eol (parser, pragma_tok);
18910 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18911 }
18912
18913 /* OpenMP 2.5:
18914
18915 section-scope:
18916 { section-sequence }
18917
18918 section-sequence:
18919 section-directive[opt] structured-block
18920 section-sequence section-directive structured-block */
18921
18922 static tree
18923 cp_parser_omp_sections_scope (cp_parser *parser)
18924 {
18925 tree stmt, substmt;
18926 bool error_suppress = false;
18927 cp_token *tok;
18928
18929 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18930 return NULL_TREE;
18931
18932 stmt = push_stmt_list ();
18933
18934 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18935 {
18936 unsigned save;
18937
18938 substmt = begin_omp_structured_block ();
18939 save = cp_parser_begin_omp_structured_block (parser);
18940
18941 while (1)
18942 {
18943 cp_parser_statement (parser, NULL_TREE, false);
18944
18945 tok = cp_lexer_peek_token (parser->lexer);
18946 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18947 break;
18948 if (tok->type == CPP_CLOSE_BRACE)
18949 break;
18950 if (tok->type == CPP_EOF)
18951 break;
18952 }
18953
18954 cp_parser_end_omp_structured_block (parser, save);
18955 substmt = finish_omp_structured_block (substmt);
18956 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18957 add_stmt (substmt);
18958 }
18959
18960 while (1)
18961 {
18962 tok = cp_lexer_peek_token (parser->lexer);
18963 if (tok->type == CPP_CLOSE_BRACE)
18964 break;
18965 if (tok->type == CPP_EOF)
18966 break;
18967
18968 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18969 {
18970 cp_lexer_consume_token (parser->lexer);
18971 cp_parser_require_pragma_eol (parser, tok);
18972 error_suppress = false;
18973 }
18974 else if (!error_suppress)
18975 {
18976 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18977 error_suppress = true;
18978 }
18979
18980 substmt = cp_parser_omp_structured_block (parser);
18981 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18982 add_stmt (substmt);
18983 }
18984 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18985
18986 substmt = pop_stmt_list (stmt);
18987
18988 stmt = make_node (OMP_SECTIONS);
18989 TREE_TYPE (stmt) = void_type_node;
18990 OMP_SECTIONS_BODY (stmt) = substmt;
18991
18992 add_stmt (stmt);
18993 return stmt;
18994 }
18995
18996 /* OpenMP 2.5:
18997 # pragma omp sections sections-clause[optseq] newline
18998 sections-scope */
18999
19000 #define OMP_SECTIONS_CLAUSE_MASK \
19001 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19002 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19003 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19004 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19005 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19006
19007 static tree
19008 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19009 {
19010 tree clauses, ret;
19011
19012 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19013 "#pragma omp sections", pragma_tok);
19014
19015 ret = cp_parser_omp_sections_scope (parser);
19016 if (ret)
19017 OMP_SECTIONS_CLAUSES (ret) = clauses;
19018
19019 return ret;
19020 }
19021
19022 /* OpenMP 2.5:
19023 # pragma parallel parallel-clause new-line
19024 # pragma parallel for parallel-for-clause new-line
19025 # pragma parallel sections parallel-sections-clause new-line */
19026
19027 #define OMP_PARALLEL_CLAUSE_MASK \
19028 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19029 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19030 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19031 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19032 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19033 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19034 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19035 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19036
19037 static tree
19038 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19039 {
19040 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19041 const char *p_name = "#pragma omp parallel";
19042 tree stmt, clauses, par_clause, ws_clause, block;
19043 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19044 unsigned int save;
19045
19046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19047 {
19048 cp_lexer_consume_token (parser->lexer);
19049 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19050 p_name = "#pragma omp parallel for";
19051 mask |= OMP_FOR_CLAUSE_MASK;
19052 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19053 }
19054 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19055 {
19056 tree id = cp_lexer_peek_token (parser->lexer)->value;
19057 const char *p = IDENTIFIER_POINTER (id);
19058 if (strcmp (p, "sections") == 0)
19059 {
19060 cp_lexer_consume_token (parser->lexer);
19061 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19062 p_name = "#pragma omp parallel sections";
19063 mask |= OMP_SECTIONS_CLAUSE_MASK;
19064 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19065 }
19066 }
19067
19068 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19069 block = begin_omp_parallel ();
19070 save = cp_parser_begin_omp_structured_block (parser);
19071
19072 switch (p_kind)
19073 {
19074 case PRAGMA_OMP_PARALLEL:
19075 cp_parser_already_scoped_statement (parser);
19076 par_clause = clauses;
19077 break;
19078
19079 case PRAGMA_OMP_PARALLEL_FOR:
19080 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19081 stmt = cp_parser_omp_for_loop (parser);
19082 if (stmt)
19083 OMP_FOR_CLAUSES (stmt) = ws_clause;
19084 break;
19085
19086 case PRAGMA_OMP_PARALLEL_SECTIONS:
19087 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19088 stmt = cp_parser_omp_sections_scope (parser);
19089 if (stmt)
19090 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19091 break;
19092
19093 default:
19094 gcc_unreachable ();
19095 }
19096
19097 cp_parser_end_omp_structured_block (parser, save);
19098 stmt = finish_omp_parallel (par_clause, block);
19099 if (p_kind != PRAGMA_OMP_PARALLEL)
19100 OMP_PARALLEL_COMBINED (stmt) = 1;
19101 return stmt;
19102 }
19103
19104 /* OpenMP 2.5:
19105 # pragma omp single single-clause[optseq] new-line
19106 structured-block */
19107
19108 #define OMP_SINGLE_CLAUSE_MASK \
19109 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19110 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19111 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19112 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19113
19114 static tree
19115 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19116 {
19117 tree stmt = make_node (OMP_SINGLE);
19118 TREE_TYPE (stmt) = void_type_node;
19119
19120 OMP_SINGLE_CLAUSES (stmt)
19121 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19122 "#pragma omp single", pragma_tok);
19123 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19124
19125 return add_stmt (stmt);
19126 }
19127
19128 /* OpenMP 2.5:
19129 # pragma omp threadprivate (variable-list) */
19130
19131 static void
19132 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19133 {
19134 tree vars;
19135
19136 vars = cp_parser_omp_var_list (parser, 0, NULL);
19137 cp_parser_require_pragma_eol (parser, pragma_tok);
19138
19139 if (!targetm.have_tls)
19140 sorry ("threadprivate variables not supported in this target");
19141
19142 finish_omp_threadprivate (vars);
19143 }
19144
19145 /* Main entry point to OpenMP statement pragmas. */
19146
19147 static void
19148 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19149 {
19150 tree stmt;
19151
19152 switch (pragma_tok->pragma_kind)
19153 {
19154 case PRAGMA_OMP_ATOMIC:
19155 cp_parser_omp_atomic (parser, pragma_tok);
19156 return;
19157 case PRAGMA_OMP_CRITICAL:
19158 stmt = cp_parser_omp_critical (parser, pragma_tok);
19159 break;
19160 case PRAGMA_OMP_FOR:
19161 stmt = cp_parser_omp_for (parser, pragma_tok);
19162 break;
19163 case PRAGMA_OMP_MASTER:
19164 stmt = cp_parser_omp_master (parser, pragma_tok);
19165 break;
19166 case PRAGMA_OMP_ORDERED:
19167 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19168 break;
19169 case PRAGMA_OMP_PARALLEL:
19170 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19171 break;
19172 case PRAGMA_OMP_SECTIONS:
19173 stmt = cp_parser_omp_sections (parser, pragma_tok);
19174 break;
19175 case PRAGMA_OMP_SINGLE:
19176 stmt = cp_parser_omp_single (parser, pragma_tok);
19177 break;
19178 default:
19179 gcc_unreachable ();
19180 }
19181
19182 if (stmt)
19183 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19184 }
19185 \f
19186 /* The parser. */
19187
19188 static GTY (()) cp_parser *the_parser;
19189
19190 \f
19191 /* Special handling for the first token or line in the file. The first
19192 thing in the file might be #pragma GCC pch_preprocess, which loads a
19193 PCH file, which is a GC collection point. So we need to handle this
19194 first pragma without benefit of an existing lexer structure.
19195
19196 Always returns one token to the caller in *FIRST_TOKEN. This is
19197 either the true first token of the file, or the first token after
19198 the initial pragma. */
19199
19200 static void
19201 cp_parser_initial_pragma (cp_token *first_token)
19202 {
19203 tree name = NULL;
19204
19205 cp_lexer_get_preprocessor_token (NULL, first_token);
19206 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19207 return;
19208
19209 cp_lexer_get_preprocessor_token (NULL, first_token);
19210 if (first_token->type == CPP_STRING)
19211 {
19212 name = first_token->value;
19213
19214 cp_lexer_get_preprocessor_token (NULL, first_token);
19215 if (first_token->type != CPP_PRAGMA_EOL)
19216 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19217 }
19218 else
19219 error ("expected string literal");
19220
19221 /* Skip to the end of the pragma. */
19222 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19223 cp_lexer_get_preprocessor_token (NULL, first_token);
19224
19225 /* Now actually load the PCH file. */
19226 if (name)
19227 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19228
19229 /* Read one more token to return to our caller. We have to do this
19230 after reading the PCH file in, since its pointers have to be
19231 live. */
19232 cp_lexer_get_preprocessor_token (NULL, first_token);
19233 }
19234
19235 /* Normal parsing of a pragma token. Here we can (and must) use the
19236 regular lexer. */
19237
19238 static bool
19239 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19240 {
19241 cp_token *pragma_tok;
19242 unsigned int id;
19243
19244 pragma_tok = cp_lexer_consume_token (parser->lexer);
19245 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19246 parser->lexer->in_pragma = true;
19247
19248 id = pragma_tok->pragma_kind;
19249 switch (id)
19250 {
19251 case PRAGMA_GCC_PCH_PREPROCESS:
19252 error ("%<#pragma GCC pch_preprocess%> must be first");
19253 break;
19254
19255 case PRAGMA_OMP_BARRIER:
19256 switch (context)
19257 {
19258 case pragma_compound:
19259 cp_parser_omp_barrier (parser, pragma_tok);
19260 return false;
19261 case pragma_stmt:
19262 error ("%<#pragma omp barrier%> may only be "
19263 "used in compound statements");
19264 break;
19265 default:
19266 goto bad_stmt;
19267 }
19268 break;
19269
19270 case PRAGMA_OMP_FLUSH:
19271 switch (context)
19272 {
19273 case pragma_compound:
19274 cp_parser_omp_flush (parser, pragma_tok);
19275 return false;
19276 case pragma_stmt:
19277 error ("%<#pragma omp flush%> may only be "
19278 "used in compound statements");
19279 break;
19280 default:
19281 goto bad_stmt;
19282 }
19283 break;
19284
19285 case PRAGMA_OMP_THREADPRIVATE:
19286 cp_parser_omp_threadprivate (parser, pragma_tok);
19287 return false;
19288
19289 case PRAGMA_OMP_ATOMIC:
19290 case PRAGMA_OMP_CRITICAL:
19291 case PRAGMA_OMP_FOR:
19292 case PRAGMA_OMP_MASTER:
19293 case PRAGMA_OMP_ORDERED:
19294 case PRAGMA_OMP_PARALLEL:
19295 case PRAGMA_OMP_SECTIONS:
19296 case PRAGMA_OMP_SINGLE:
19297 if (context == pragma_external)
19298 goto bad_stmt;
19299 cp_parser_omp_construct (parser, pragma_tok);
19300 return true;
19301
19302 case PRAGMA_OMP_SECTION:
19303 error ("%<#pragma omp section%> may only be used in "
19304 "%<#pragma omp sections%> construct");
19305 break;
19306
19307 default:
19308 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19309 c_invoke_pragma_handler (id);
19310 break;
19311
19312 bad_stmt:
19313 cp_parser_error (parser, "expected declaration specifiers");
19314 break;
19315 }
19316
19317 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19318 return false;
19319 }
19320
19321 /* The interface the pragma parsers have to the lexer. */
19322
19323 enum cpp_ttype
19324 pragma_lex (tree *value)
19325 {
19326 cp_token *tok;
19327 enum cpp_ttype ret;
19328
19329 tok = cp_lexer_peek_token (the_parser->lexer);
19330
19331 ret = tok->type;
19332 *value = tok->value;
19333
19334 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19335 ret = CPP_EOF;
19336 else if (ret == CPP_STRING)
19337 *value = cp_parser_string_literal (the_parser, false, false);
19338 else
19339 {
19340 cp_lexer_consume_token (the_parser->lexer);
19341 if (ret == CPP_KEYWORD)
19342 ret = CPP_NAME;
19343 }
19344
19345 return ret;
19346 }
19347
19348 \f
19349 /* External interface. */
19350
19351 /* Parse one entire translation unit. */
19352
19353 void
19354 c_parse_file (void)
19355 {
19356 bool error_occurred;
19357 static bool already_called = false;
19358
19359 if (already_called)
19360 {
19361 sorry ("inter-module optimizations not implemented for C++");
19362 return;
19363 }
19364 already_called = true;
19365
19366 the_parser = cp_parser_new ();
19367 push_deferring_access_checks (flag_access_control
19368 ? dk_no_deferred : dk_no_check);
19369 error_occurred = cp_parser_translation_unit (the_parser);
19370 the_parser = NULL;
19371 }
19372
19373 /* This variable must be provided by every front end. */
19374
19375 int yydebug;
19376
19377 #include "gt-cp-parser.h"