5f7ddcf35d89d5d0c012748f713b55caa3cf2132
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer. */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
46
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
49
50 struct tree_check GTY(())
51 {
52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
59 };
60
61 /* A C++ token. */
62
63 typedef struct cp_token GTY (())
64 {
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
70 /* Token flags. */
71 unsigned char flags;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* The location at which this token was found. */
90 location_t location;
91 } cp_token;
92
93 /* We use a stack of token pointer for saving token sets. */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
97
98 static cp_token eof_token =
99 {
100 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
101 0
102 };
103
104 /* The cp_lexer structure represents the C++ lexer. It is responsible
105 for managing the token stream from the preprocessor and supplying
106 it to the parser. Tokens are never added to the cp_lexer after
107 it is created. */
108
109 typedef struct cp_lexer GTY (())
110 {
111 /* The memory allocated for the buffer. NULL if this lexer does not
112 own the token buffer. */
113 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114 /* If the lexer owns the buffer, this is the number of tokens in the
115 buffer. */
116 size_t buffer_length;
117
118 /* A pointer just past the last available token. The tokens
119 in this lexer are [buffer, last_token). */
120 cp_token_position GTY ((skip)) last_token;
121
122 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
123 no more available tokens. */
124 cp_token_position GTY ((skip)) next_token;
125
126 /* A stack indicating positions at which cp_lexer_save_tokens was
127 called. The top entry is the most recent position at which we
128 began saving tokens. If the stack is non-empty, we are saving
129 tokens. */
130 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
131
132 /* The next lexer in a linked list of lexers. */
133 struct cp_lexer *next;
134
135 /* True if we should output debugging information. */
136 bool debugging_p;
137
138 /* True if we're in the context of parsing a pragma, and should not
139 increment past the end-of-line marker. */
140 bool in_pragma;
141 } cp_lexer;
142
143 /* cp_token_cache is a range of tokens. There is no need to represent
144 allocate heap memory for it, since tokens are never removed from the
145 lexer's array. There is also no need for the GC to walk through
146 a cp_token_cache, since everything in here is referenced through
147 a lexer. */
148
149 typedef struct cp_token_cache GTY(())
150 {
151 /* The beginning of the token range. */
152 cp_token * GTY((skip)) first;
153
154 /* Points immediately after the last token in the range. */
155 cp_token * GTY ((skip)) last;
156 } cp_token_cache;
157
158 /* Prototypes. */
159
160 static cp_lexer *cp_lexer_new_main
161 (void);
162 static cp_lexer *cp_lexer_new_from_tokens
163 (cp_token_cache *tokens);
164 static void cp_lexer_destroy
165 (cp_lexer *);
166 static int cp_lexer_saving_tokens
167 (const cp_lexer *);
168 static cp_token_position cp_lexer_token_position
169 (cp_lexer *, bool);
170 static cp_token *cp_lexer_token_at
171 (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173 (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
175 (cp_lexer *);
176 static cp_token *cp_lexer_peek_nth_token
177 (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181 (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183 (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
185 (cp_lexer *);
186 static void cp_lexer_purge_token
187 (cp_lexer *);
188 static void cp_lexer_purge_tokens_after
189 (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
191 (cp_lexer *);
192 static void cp_lexer_commit_tokens
193 (cp_lexer *);
194 static void cp_lexer_rollback_tokens
195 (cp_lexer *);
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198 (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
200 (cp_lexer *);
201 static void cp_lexer_start_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204 (cp_lexer *) ATTRIBUTE_UNUSED;
205 #else
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207 about passing NULL to functions that require non-NULL arguments
208 (fputs, fprintf). It will never be used, so all we need is a value
209 of the right type that's guaranteed not to be NULL. */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
214
215 static cp_token_cache *cp_token_cache_new
216 (cp_token *, cp_token *);
217
218 static void cp_parser_initial_pragma
219 (cp_token *);
220
221 /* Manifest constants. */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
224
225 /* A token type for keywords, as opposed to ordinary identifiers. */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
227
228 /* A token type for template-ids. If a template-id is processed while
229 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230 the value of the CPP_TEMPLATE_ID is whatever was returned by
231 cp_parser_template_id. */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
233
234 /* A token type for nested-name-specifiers. If a
235 nested-name-specifier is processed while parsing tentatively, it is
236 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238 cp_parser_nested_name_specifier_opt. */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
240
241 /* A token type for tokens that are not tokens at all; these are used
242 to represent slots in the array where there used to be a token
243 that has now been deleted. */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
245
246 /* The number of token types, including C++-specific ones. */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
248
249 /* Variables. */
250
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
255
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
257 preprocessor. */
258
259 static cp_lexer *
260 cp_lexer_new_main (void)
261 {
262 cp_token first_token;
263 cp_lexer *lexer;
264 cp_token *pos;
265 size_t alloc;
266 size_t space;
267 cp_token *buffer;
268
269 /* It's possible that parsing the first pragma will load a PCH file,
270 which is a GC collection point. So we have to do that before
271 allocating any memory. */
272 cp_parser_initial_pragma (&first_token);
273
274 c_common_no_more_pch ();
275
276 /* Allocate the memory. */
277 lexer = GGC_CNEW (cp_lexer);
278
279 #ifdef ENABLE_CHECKING
280 /* Initially we are not debugging. */
281 lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284 CP_SAVED_TOKEN_STACK);
285
286 /* Create the buffer. */
287 alloc = CP_LEXER_BUFFER_SIZE;
288 buffer = GGC_NEWVEC (cp_token, alloc);
289
290 /* Put the first token in the buffer. */
291 space = alloc;
292 pos = buffer;
293 *pos = first_token;
294
295 /* Get the remaining tokens from the preprocessor. */
296 while (pos->type != CPP_EOF)
297 {
298 pos++;
299 if (!--space)
300 {
301 space = alloc;
302 alloc *= 2;
303 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304 pos = buffer + space;
305 }
306 cp_lexer_get_preprocessor_token (lexer, pos);
307 }
308 lexer->buffer = buffer;
309 lexer->buffer_length = alloc - space;
310 lexer->last_token = pos;
311 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312
313 /* Subsequent preprocessor diagnostics should use compiler
314 diagnostic functions to get the compiler source location. */
315 cpp_get_options (parse_in)->client_diagnostic = true;
316 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
317
318 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 return lexer;
320 }
321
322 /* Create a new lexer whose token stream is primed with the tokens in
323 CACHE. When these tokens are exhausted, no new tokens will be read. */
324
325 static cp_lexer *
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
327 {
328 cp_token *first = cache->first;
329 cp_token *last = cache->last;
330 cp_lexer *lexer = GGC_CNEW (cp_lexer);
331
332 /* We do not own the buffer. */
333 lexer->buffer = NULL;
334 lexer->buffer_length = 0;
335 lexer->next_token = first == last ? &eof_token : first;
336 lexer->last_token = last;
337
338 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339 CP_SAVED_TOKEN_STACK);
340
341 #ifdef ENABLE_CHECKING
342 /* Initially we are not debugging. */
343 lexer->debugging_p = false;
344 #endif
345
346 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 return lexer;
348 }
349
350 /* Frees all resources associated with LEXER. */
351
352 static void
353 cp_lexer_destroy (cp_lexer *lexer)
354 {
355 if (lexer->buffer)
356 ggc_free (lexer->buffer);
357 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 ggc_free (lexer);
359 }
360
361 /* Returns nonzero if debugging information should be output. */
362
363 #ifdef ENABLE_CHECKING
364
365 static inline bool
366 cp_lexer_debugging_p (cp_lexer *lexer)
367 {
368 return lexer->debugging_p;
369 }
370
371 #endif /* ENABLE_CHECKING */
372
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
375 {
376 gcc_assert (!previous_p || lexer->next_token != &eof_token);
377
378 return lexer->next_token - previous_p;
379 }
380
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
383 {
384 return pos;
385 }
386
387 /* nonzero if we are presently saving tokens. */
388
389 static inline int
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
391 {
392 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 }
394
395 /* Store the next token from the preprocessor in *TOKEN. Return true
396 if we reach EOF. If LEXER is NULL, assume we are handling an
397 initial #pragma pch_preprocess, and thus want the lexer to return
398 processed strings. */
399
400 static void
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
402 {
403 static int is_extern_c = 0;
404
405 /* Get a new token from the preprocessor. */
406 token->type
407 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409 token->keyword = RID_MAX;
410 token->pragma_kind = PRAGMA_NONE;
411 token->in_system_header = in_system_header;
412
413 /* On some systems, some header files are surrounded by an
414 implicit extern "C" block. Set a flag in the token if it
415 comes from such a header. */
416 is_extern_c += pending_lang_change;
417 pending_lang_change = 0;
418 token->implicit_extern_c = is_extern_c > 0;
419
420 /* Check to see if this token is a keyword. */
421 if (token->type == CPP_NAME)
422 {
423 if (C_IS_RESERVED_WORD (token->u.value))
424 {
425 /* Mark this token as a keyword. */
426 token->type = CPP_KEYWORD;
427 /* Record which keyword. */
428 token->keyword = C_RID_CODE (token->u.value);
429 /* Update the value. Some keywords are mapped to particular
430 entities, rather than simply having the value of the
431 corresponding IDENTIFIER_NODE. For example, `__const' is
432 mapped to `const'. */
433 token->u.value = ridpointers[token->keyword];
434 }
435 else
436 {
437 if (warn_cxx0x_compat
438 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
440 {
441 /* Warn about the C++0x keyword (but still treat it as
442 an identifier). */
443 warning (OPT_Wc__0x_compat,
444 "identifier %<%s%> will become a keyword in C++0x",
445 IDENTIFIER_POINTER (token->u.value));
446
447 /* Clear out the C_RID_CODE so we don't warn about this
448 particular identifier-turned-keyword again. */
449 C_RID_CODE (token->u.value) = RID_MAX;
450 }
451
452 token->ambiguous_p = false;
453 token->keyword = RID_MAX;
454 }
455 }
456 /* Handle Objective-C++ keywords. */
457 else if (token->type == CPP_AT_NAME)
458 {
459 token->type = CPP_KEYWORD;
460 switch (C_RID_CODE (token->u.value))
461 {
462 /* Map 'class' to '@class', 'private' to '@private', etc. */
463 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467 case RID_THROW: token->keyword = RID_AT_THROW; break;
468 case RID_TRY: token->keyword = RID_AT_TRY; break;
469 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470 default: token->keyword = C_RID_CODE (token->u.value);
471 }
472 }
473 else if (token->type == CPP_PRAGMA)
474 {
475 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
476 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477 token->u.value = NULL_TREE;
478 }
479 }
480
481 /* Update the globals input_location and in_system_header and the
482 input file stack from TOKEN. */
483 static inline void
484 cp_lexer_set_source_position_from_token (cp_token *token)
485 {
486 if (token->type != CPP_EOF)
487 {
488 input_location = token->location;
489 in_system_header = token->in_system_header;
490 }
491 }
492
493 /* Return a pointer to the next token in the token stream, but do not
494 consume it. */
495
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
498 {
499 if (cp_lexer_debugging_p (lexer))
500 {
501 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503 putc ('\n', cp_lexer_debug_stream);
504 }
505 return lexer->next_token;
506 }
507
508 /* Return true if the next token has the indicated TYPE. */
509
510 static inline bool
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
512 {
513 return cp_lexer_peek_token (lexer)->type == type;
514 }
515
516 /* Return true if the next token does not have the indicated TYPE. */
517
518 static inline bool
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
520 {
521 return !cp_lexer_next_token_is (lexer, type);
522 }
523
524 /* Return true if the next token is the indicated KEYWORD. */
525
526 static inline bool
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
528 {
529 return cp_lexer_peek_token (lexer)->keyword == keyword;
530 }
531
532 /* Return true if the next token is a keyword for a decl-specifier. */
533
534 static bool
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 {
537 cp_token *token;
538
539 token = cp_lexer_peek_token (lexer);
540 switch (token->keyword)
541 {
542 /* Storage classes. */
543 case RID_AUTO:
544 case RID_REGISTER:
545 case RID_STATIC:
546 case RID_EXTERN:
547 case RID_MUTABLE:
548 case RID_THREAD:
549 /* Elaborated type specifiers. */
550 case RID_ENUM:
551 case RID_CLASS:
552 case RID_STRUCT:
553 case RID_UNION:
554 case RID_TYPENAME:
555 /* Simple type specifiers. */
556 case RID_CHAR:
557 case RID_WCHAR:
558 case RID_BOOL:
559 case RID_SHORT:
560 case RID_INT:
561 case RID_LONG:
562 case RID_SIGNED:
563 case RID_UNSIGNED:
564 case RID_FLOAT:
565 case RID_DOUBLE:
566 case RID_VOID:
567 /* GNU extensions. */
568 case RID_ATTRIBUTE:
569 case RID_TYPEOF:
570 /* C++0x extensions. */
571 case RID_DECLTYPE:
572 return true;
573
574 default:
575 return false;
576 }
577 }
578
579 /* Return a pointer to the Nth token in the token stream. If N is 1,
580 then this is precisely equivalent to cp_lexer_peek_token (except
581 that it is not inline). One would like to disallow that case, but
582 there is one case (cp_parser_nth_token_starts_template_id) where
583 the caller passes a variable for N and it might be 1. */
584
585 static cp_token *
586 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
587 {
588 cp_token *token;
589
590 /* N is 1-based, not zero-based. */
591 gcc_assert (n > 0);
592
593 if (cp_lexer_debugging_p (lexer))
594 fprintf (cp_lexer_debug_stream,
595 "cp_lexer: peeking ahead %ld at token: ", (long)n);
596
597 --n;
598 token = lexer->next_token;
599 gcc_assert (!n || token != &eof_token);
600 while (n != 0)
601 {
602 ++token;
603 if (token == lexer->last_token)
604 {
605 token = &eof_token;
606 break;
607 }
608
609 if (token->type != CPP_PURGED)
610 --n;
611 }
612
613 if (cp_lexer_debugging_p (lexer))
614 {
615 cp_lexer_print_token (cp_lexer_debug_stream, token);
616 putc ('\n', cp_lexer_debug_stream);
617 }
618
619 return token;
620 }
621
622 /* Return the next token, and advance the lexer's next_token pointer
623 to point to the next non-purged token. */
624
625 static cp_token *
626 cp_lexer_consume_token (cp_lexer* lexer)
627 {
628 cp_token *token = lexer->next_token;
629
630 gcc_assert (token != &eof_token);
631 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
632
633 do
634 {
635 lexer->next_token++;
636 if (lexer->next_token == lexer->last_token)
637 {
638 lexer->next_token = &eof_token;
639 break;
640 }
641
642 }
643 while (lexer->next_token->type == CPP_PURGED);
644
645 cp_lexer_set_source_position_from_token (token);
646
647 /* Provide debugging output. */
648 if (cp_lexer_debugging_p (lexer))
649 {
650 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
651 cp_lexer_print_token (cp_lexer_debug_stream, token);
652 putc ('\n', cp_lexer_debug_stream);
653 }
654
655 return token;
656 }
657
658 /* Permanently remove the next token from the token stream, and
659 advance the next_token pointer to refer to the next non-purged
660 token. */
661
662 static void
663 cp_lexer_purge_token (cp_lexer *lexer)
664 {
665 cp_token *tok = lexer->next_token;
666
667 gcc_assert (tok != &eof_token);
668 tok->type = CPP_PURGED;
669 tok->location = UNKNOWN_LOCATION;
670 tok->u.value = NULL_TREE;
671 tok->keyword = RID_MAX;
672
673 do
674 {
675 tok++;
676 if (tok == lexer->last_token)
677 {
678 tok = &eof_token;
679 break;
680 }
681 }
682 while (tok->type == CPP_PURGED);
683 lexer->next_token = tok;
684 }
685
686 /* Permanently remove all tokens after TOK, up to, but not
687 including, the token that will be returned next by
688 cp_lexer_peek_token. */
689
690 static void
691 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
692 {
693 cp_token *peek = lexer->next_token;
694
695 if (peek == &eof_token)
696 peek = lexer->last_token;
697
698 gcc_assert (tok < peek);
699
700 for ( tok += 1; tok != peek; tok += 1)
701 {
702 tok->type = CPP_PURGED;
703 tok->location = UNKNOWN_LOCATION;
704 tok->u.value = NULL_TREE;
705 tok->keyword = RID_MAX;
706 }
707 }
708
709 /* Begin saving tokens. All tokens consumed after this point will be
710 preserved. */
711
712 static void
713 cp_lexer_save_tokens (cp_lexer* lexer)
714 {
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer))
717 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
718
719 VEC_safe_push (cp_token_position, heap,
720 lexer->saved_tokens, lexer->next_token);
721 }
722
723 /* Commit to the portion of the token stream most recently saved. */
724
725 static void
726 cp_lexer_commit_tokens (cp_lexer* lexer)
727 {
728 /* Provide debugging output. */
729 if (cp_lexer_debugging_p (lexer))
730 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
731
732 VEC_pop (cp_token_position, lexer->saved_tokens);
733 }
734
735 /* Return all tokens saved since the last call to cp_lexer_save_tokens
736 to the token stream. Stop saving tokens. */
737
738 static void
739 cp_lexer_rollback_tokens (cp_lexer* lexer)
740 {
741 /* Provide debugging output. */
742 if (cp_lexer_debugging_p (lexer))
743 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
744
745 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
746 }
747
748 /* Print a representation of the TOKEN on the STREAM. */
749
750 #ifdef ENABLE_CHECKING
751
752 static void
753 cp_lexer_print_token (FILE * stream, cp_token *token)
754 {
755 /* We don't use cpp_type2name here because the parser defines
756 a few tokens of its own. */
757 static const char *const token_names[] = {
758 /* cpplib-defined token types */
759 #define OP(e, s) #e,
760 #define TK(e, s) #e,
761 TTYPE_TABLE
762 #undef OP
763 #undef TK
764 /* C++ parser token types - see "Manifest constants", above. */
765 "KEYWORD",
766 "TEMPLATE_ID",
767 "NESTED_NAME_SPECIFIER",
768 "PURGED"
769 };
770
771 /* If we have a name for the token, print it out. Otherwise, we
772 simply give the numeric code. */
773 gcc_assert (token->type < ARRAY_SIZE(token_names));
774 fputs (token_names[token->type], stream);
775
776 /* For some tokens, print the associated data. */
777 switch (token->type)
778 {
779 case CPP_KEYWORD:
780 /* Some keywords have a value that is not an IDENTIFIER_NODE.
781 For example, `struct' is mapped to an INTEGER_CST. */
782 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
783 break;
784 /* else fall through */
785 case CPP_NAME:
786 fputs (IDENTIFIER_POINTER (token->u.value), stream);
787 break;
788
789 case CPP_STRING:
790 case CPP_WSTRING:
791 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
792 break;
793
794 default:
795 break;
796 }
797 }
798
799 /* Start emitting debugging information. */
800
801 static void
802 cp_lexer_start_debugging (cp_lexer* lexer)
803 {
804 lexer->debugging_p = true;
805 }
806
807 /* Stop emitting debugging information. */
808
809 static void
810 cp_lexer_stop_debugging (cp_lexer* lexer)
811 {
812 lexer->debugging_p = false;
813 }
814
815 #endif /* ENABLE_CHECKING */
816
817 /* Create a new cp_token_cache, representing a range of tokens. */
818
819 static cp_token_cache *
820 cp_token_cache_new (cp_token *first, cp_token *last)
821 {
822 cp_token_cache *cache = GGC_NEW (cp_token_cache);
823 cache->first = first;
824 cache->last = last;
825 return cache;
826 }
827
828 \f
829 /* Decl-specifiers. */
830
831 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
832
833 static void
834 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
835 {
836 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
837 }
838
839 /* Declarators. */
840
841 /* Nothing other than the parser should be creating declarators;
842 declarators are a semi-syntactic representation of C++ entities.
843 Other parts of the front end that need to create entities (like
844 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
845
846 static cp_declarator *make_call_declarator
847 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
848 static cp_declarator *make_array_declarator
849 (cp_declarator *, tree);
850 static cp_declarator *make_pointer_declarator
851 (cp_cv_quals, cp_declarator *);
852 static cp_declarator *make_reference_declarator
853 (cp_cv_quals, cp_declarator *, bool);
854 static cp_parameter_declarator *make_parameter_declarator
855 (cp_decl_specifier_seq *, cp_declarator *, tree);
856 static cp_declarator *make_ptrmem_declarator
857 (cp_cv_quals, tree, cp_declarator *);
858
859 /* An erroneous declarator. */
860 static cp_declarator *cp_error_declarator;
861
862 /* The obstack on which declarators and related data structures are
863 allocated. */
864 static struct obstack declarator_obstack;
865
866 /* Alloc BYTES from the declarator memory pool. */
867
868 static inline void *
869 alloc_declarator (size_t bytes)
870 {
871 return obstack_alloc (&declarator_obstack, bytes);
872 }
873
874 /* Allocate a declarator of the indicated KIND. Clear fields that are
875 common to all declarators. */
876
877 static cp_declarator *
878 make_declarator (cp_declarator_kind kind)
879 {
880 cp_declarator *declarator;
881
882 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
883 declarator->kind = kind;
884 declarator->attributes = NULL_TREE;
885 declarator->declarator = NULL;
886 declarator->parameter_pack_p = false;
887
888 return declarator;
889 }
890
891 /* Make a declarator for a generalized identifier. If
892 QUALIFYING_SCOPE is non-NULL, the identifier is
893 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894 UNQUALIFIED_NAME. SFK indicates the kind of special function this
895 is, if any. */
896
897 static cp_declarator *
898 make_id_declarator (tree qualifying_scope, tree unqualified_name,
899 special_function_kind sfk)
900 {
901 cp_declarator *declarator;
902
903 /* It is valid to write:
904
905 class C { void f(); };
906 typedef C D;
907 void D::f();
908
909 The standard is not clear about whether `typedef const C D' is
910 legal; as of 2002-09-15 the committee is considering that
911 question. EDG 3.0 allows that syntax. Therefore, we do as
912 well. */
913 if (qualifying_scope && TYPE_P (qualifying_scope))
914 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
915
916 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
917 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
918 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
919
920 declarator = make_declarator (cdk_id);
921 declarator->u.id.qualifying_scope = qualifying_scope;
922 declarator->u.id.unqualified_name = unqualified_name;
923 declarator->u.id.sfk = sfk;
924
925 return declarator;
926 }
927
928 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
929 of modifiers such as const or volatile to apply to the pointer
930 type, represented as identifiers. */
931
932 cp_declarator *
933 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
934 {
935 cp_declarator *declarator;
936
937 declarator = make_declarator (cdk_pointer);
938 declarator->declarator = target;
939 declarator->u.pointer.qualifiers = cv_qualifiers;
940 declarator->u.pointer.class_type = NULL_TREE;
941 if (target)
942 {
943 declarator->parameter_pack_p = target->parameter_pack_p;
944 target->parameter_pack_p = false;
945 }
946 else
947 declarator->parameter_pack_p = false;
948
949 return declarator;
950 }
951
952 /* Like make_pointer_declarator -- but for references. */
953
954 cp_declarator *
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
956 bool rvalue_ref)
957 {
958 cp_declarator *declarator;
959
960 declarator = make_declarator (cdk_reference);
961 declarator->declarator = target;
962 declarator->u.reference.qualifiers = cv_qualifiers;
963 declarator->u.reference.rvalue_ref = rvalue_ref;
964 if (target)
965 {
966 declarator->parameter_pack_p = target->parameter_pack_p;
967 target->parameter_pack_p = false;
968 }
969 else
970 declarator->parameter_pack_p = false;
971
972 return declarator;
973 }
974
975 /* Like make_pointer_declarator -- but for a pointer to a non-static
976 member of CLASS_TYPE. */
977
978 cp_declarator *
979 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
980 cp_declarator *pointee)
981 {
982 cp_declarator *declarator;
983
984 declarator = make_declarator (cdk_ptrmem);
985 declarator->declarator = pointee;
986 declarator->u.pointer.qualifiers = cv_qualifiers;
987 declarator->u.pointer.class_type = class_type;
988
989 if (pointee)
990 {
991 declarator->parameter_pack_p = pointee->parameter_pack_p;
992 pointee->parameter_pack_p = false;
993 }
994 else
995 declarator->parameter_pack_p = false;
996
997 return declarator;
998 }
999
1000 /* Make a declarator for the function given by TARGET, with the
1001 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1002 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1003 indicates what exceptions can be thrown. */
1004
1005 cp_declarator *
1006 make_call_declarator (cp_declarator *target,
1007 cp_parameter_declarator *parms,
1008 cp_cv_quals cv_qualifiers,
1009 tree exception_specification)
1010 {
1011 cp_declarator *declarator;
1012
1013 declarator = make_declarator (cdk_function);
1014 declarator->declarator = target;
1015 declarator->u.function.parameters = parms;
1016 declarator->u.function.qualifiers = cv_qualifiers;
1017 declarator->u.function.exception_specification = exception_specification;
1018 if (target)
1019 {
1020 declarator->parameter_pack_p = target->parameter_pack_p;
1021 target->parameter_pack_p = false;
1022 }
1023 else
1024 declarator->parameter_pack_p = false;
1025
1026 return declarator;
1027 }
1028
1029 /* Make a declarator for an array of BOUNDS elements, each of which is
1030 defined by ELEMENT. */
1031
1032 cp_declarator *
1033 make_array_declarator (cp_declarator *element, tree bounds)
1034 {
1035 cp_declarator *declarator;
1036
1037 declarator = make_declarator (cdk_array);
1038 declarator->declarator = element;
1039 declarator->u.array.bounds = bounds;
1040 if (element)
1041 {
1042 declarator->parameter_pack_p = element->parameter_pack_p;
1043 element->parameter_pack_p = false;
1044 }
1045 else
1046 declarator->parameter_pack_p = false;
1047
1048 return declarator;
1049 }
1050
1051 /* Determine whether the declarator we've seen so far can be a
1052 parameter pack, when followed by an ellipsis. */
1053 static bool
1054 declarator_can_be_parameter_pack (cp_declarator *declarator)
1055 {
1056 /* Search for a declarator name, or any other declarator that goes
1057 after the point where the ellipsis could appear in a parameter
1058 pack. If we find any of these, then this declarator can not be
1059 made into a parameter pack. */
1060 bool found = false;
1061 while (declarator && !found)
1062 {
1063 switch ((int)declarator->kind)
1064 {
1065 case cdk_id:
1066 case cdk_array:
1067 found = true;
1068 break;
1069
1070 case cdk_error:
1071 return true;
1072
1073 default:
1074 declarator = declarator->declarator;
1075 break;
1076 }
1077 }
1078
1079 return !found;
1080 }
1081
1082 cp_parameter_declarator *no_parameters;
1083
1084 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1085 DECLARATOR and DEFAULT_ARGUMENT. */
1086
1087 cp_parameter_declarator *
1088 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1089 cp_declarator *declarator,
1090 tree default_argument)
1091 {
1092 cp_parameter_declarator *parameter;
1093
1094 parameter = ((cp_parameter_declarator *)
1095 alloc_declarator (sizeof (cp_parameter_declarator)));
1096 parameter->next = NULL;
1097 if (decl_specifiers)
1098 parameter->decl_specifiers = *decl_specifiers;
1099 else
1100 clear_decl_specs (&parameter->decl_specifiers);
1101 parameter->declarator = declarator;
1102 parameter->default_argument = default_argument;
1103 parameter->ellipsis_p = false;
1104
1105 return parameter;
1106 }
1107
1108 /* Returns true iff DECLARATOR is a declaration for a function. */
1109
1110 static bool
1111 function_declarator_p (const cp_declarator *declarator)
1112 {
1113 while (declarator)
1114 {
1115 if (declarator->kind == cdk_function
1116 && declarator->declarator->kind == cdk_id)
1117 return true;
1118 if (declarator->kind == cdk_id
1119 || declarator->kind == cdk_error)
1120 return false;
1121 declarator = declarator->declarator;
1122 }
1123 return false;
1124 }
1125
1126 /* The parser. */
1127
1128 /* Overview
1129 --------
1130
1131 A cp_parser parses the token stream as specified by the C++
1132 grammar. Its job is purely parsing, not semantic analysis. For
1133 example, the parser breaks the token stream into declarators,
1134 expressions, statements, and other similar syntactic constructs.
1135 It does not check that the types of the expressions on either side
1136 of an assignment-statement are compatible, or that a function is
1137 not declared with a parameter of type `void'.
1138
1139 The parser invokes routines elsewhere in the compiler to perform
1140 semantic analysis and to build up the abstract syntax tree for the
1141 code processed.
1142
1143 The parser (and the template instantiation code, which is, in a
1144 way, a close relative of parsing) are the only parts of the
1145 compiler that should be calling push_scope and pop_scope, or
1146 related functions. The parser (and template instantiation code)
1147 keeps track of what scope is presently active; everything else
1148 should simply honor that. (The code that generates static
1149 initializers may also need to set the scope, in order to check
1150 access control correctly when emitting the initializers.)
1151
1152 Methodology
1153 -----------
1154
1155 The parser is of the standard recursive-descent variety. Upcoming
1156 tokens in the token stream are examined in order to determine which
1157 production to use when parsing a non-terminal. Some C++ constructs
1158 require arbitrary look ahead to disambiguate. For example, it is
1159 impossible, in the general case, to tell whether a statement is an
1160 expression or declaration without scanning the entire statement.
1161 Therefore, the parser is capable of "parsing tentatively." When the
1162 parser is not sure what construct comes next, it enters this mode.
1163 Then, while we attempt to parse the construct, the parser queues up
1164 error messages, rather than issuing them immediately, and saves the
1165 tokens it consumes. If the construct is parsed successfully, the
1166 parser "commits", i.e., it issues any queued error messages and
1167 the tokens that were being preserved are permanently discarded.
1168 If, however, the construct is not parsed successfully, the parser
1169 rolls back its state completely so that it can resume parsing using
1170 a different alternative.
1171
1172 Future Improvements
1173 -------------------
1174
1175 The performance of the parser could probably be improved substantially.
1176 We could often eliminate the need to parse tentatively by looking ahead
1177 a little bit. In some places, this approach might not entirely eliminate
1178 the need to parse tentatively, but it might still speed up the average
1179 case. */
1180
1181 /* Flags that are passed to some parsing functions. These values can
1182 be bitwise-ored together. */
1183
1184 typedef enum cp_parser_flags
1185 {
1186 /* No flags. */
1187 CP_PARSER_FLAGS_NONE = 0x0,
1188 /* The construct is optional. If it is not present, then no error
1189 should be issued. */
1190 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1191 /* When parsing a type-specifier, do not allow user-defined types. */
1192 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1193 } cp_parser_flags;
1194
1195 /* The different kinds of declarators we want to parse. */
1196
1197 typedef enum cp_parser_declarator_kind
1198 {
1199 /* We want an abstract declarator. */
1200 CP_PARSER_DECLARATOR_ABSTRACT,
1201 /* We want a named declarator. */
1202 CP_PARSER_DECLARATOR_NAMED,
1203 /* We don't mind, but the name must be an unqualified-id. */
1204 CP_PARSER_DECLARATOR_EITHER
1205 } cp_parser_declarator_kind;
1206
1207 /* The precedence values used to parse binary expressions. The minimum value
1208 of PREC must be 1, because zero is reserved to quickly discriminate
1209 binary operators from other tokens. */
1210
1211 enum cp_parser_prec
1212 {
1213 PREC_NOT_OPERATOR,
1214 PREC_LOGICAL_OR_EXPRESSION,
1215 PREC_LOGICAL_AND_EXPRESSION,
1216 PREC_INCLUSIVE_OR_EXPRESSION,
1217 PREC_EXCLUSIVE_OR_EXPRESSION,
1218 PREC_AND_EXPRESSION,
1219 PREC_EQUALITY_EXPRESSION,
1220 PREC_RELATIONAL_EXPRESSION,
1221 PREC_SHIFT_EXPRESSION,
1222 PREC_ADDITIVE_EXPRESSION,
1223 PREC_MULTIPLICATIVE_EXPRESSION,
1224 PREC_PM_EXPRESSION,
1225 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1226 };
1227
1228 /* A mapping from a token type to a corresponding tree node type, with a
1229 precedence value. */
1230
1231 typedef struct cp_parser_binary_operations_map_node
1232 {
1233 /* The token type. */
1234 enum cpp_ttype token_type;
1235 /* The corresponding tree code. */
1236 enum tree_code tree_type;
1237 /* The precedence of this operator. */
1238 enum cp_parser_prec prec;
1239 } cp_parser_binary_operations_map_node;
1240
1241 /* The status of a tentative parse. */
1242
1243 typedef enum cp_parser_status_kind
1244 {
1245 /* No errors have occurred. */
1246 CP_PARSER_STATUS_KIND_NO_ERROR,
1247 /* An error has occurred. */
1248 CP_PARSER_STATUS_KIND_ERROR,
1249 /* We are committed to this tentative parse, whether or not an error
1250 has occurred. */
1251 CP_PARSER_STATUS_KIND_COMMITTED
1252 } cp_parser_status_kind;
1253
1254 typedef struct cp_parser_expression_stack_entry
1255 {
1256 /* Left hand side of the binary operation we are currently
1257 parsing. */
1258 tree lhs;
1259 /* Original tree code for left hand side, if it was a binary
1260 expression itself (used for -Wparentheses). */
1261 enum tree_code lhs_type;
1262 /* Tree code for the binary operation we are parsing. */
1263 enum tree_code tree_type;
1264 /* Precedence of the binary operation we are parsing. */
1265 int prec;
1266 } cp_parser_expression_stack_entry;
1267
1268 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1269 entries because precedence levels on the stack are monotonically
1270 increasing. */
1271 typedef struct cp_parser_expression_stack_entry
1272 cp_parser_expression_stack[NUM_PREC_VALUES];
1273
1274 /* Context that is saved and restored when parsing tentatively. */
1275 typedef struct cp_parser_context GTY (())
1276 {
1277 /* If this is a tentative parsing context, the status of the
1278 tentative parse. */
1279 enum cp_parser_status_kind status;
1280 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1281 that are looked up in this context must be looked up both in the
1282 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1283 the context of the containing expression. */
1284 tree object_type;
1285
1286 /* The next parsing context in the stack. */
1287 struct cp_parser_context *next;
1288 } cp_parser_context;
1289
1290 /* Prototypes. */
1291
1292 /* Constructors and destructors. */
1293
1294 static cp_parser_context *cp_parser_context_new
1295 (cp_parser_context *);
1296
1297 /* Class variables. */
1298
1299 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1300
1301 /* The operator-precedence table used by cp_parser_binary_expression.
1302 Transformed into an associative array (binops_by_token) by
1303 cp_parser_new. */
1304
1305 static const cp_parser_binary_operations_map_node binops[] = {
1306 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1307 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1308
1309 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1310 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1311 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1312
1313 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1314 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1315
1316 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1317 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1318
1319 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1320 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1321 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1322 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1323
1324 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1325 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1326
1327 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1328
1329 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1330
1331 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1332
1333 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1334
1335 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1336 };
1337
1338 /* The same as binops, but initialized by cp_parser_new so that
1339 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1340 for speed. */
1341 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1342
1343 /* Constructors and destructors. */
1344
1345 /* Construct a new context. The context below this one on the stack
1346 is given by NEXT. */
1347
1348 static cp_parser_context *
1349 cp_parser_context_new (cp_parser_context* next)
1350 {
1351 cp_parser_context *context;
1352
1353 /* Allocate the storage. */
1354 if (cp_parser_context_free_list != NULL)
1355 {
1356 /* Pull the first entry from the free list. */
1357 context = cp_parser_context_free_list;
1358 cp_parser_context_free_list = context->next;
1359 memset (context, 0, sizeof (*context));
1360 }
1361 else
1362 context = GGC_CNEW (cp_parser_context);
1363
1364 /* No errors have occurred yet in this context. */
1365 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1366 /* If this is not the bottomost context, copy information that we
1367 need from the previous context. */
1368 if (next)
1369 {
1370 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1371 expression, then we are parsing one in this context, too. */
1372 context->object_type = next->object_type;
1373 /* Thread the stack. */
1374 context->next = next;
1375 }
1376
1377 return context;
1378 }
1379
1380 /* The cp_parser structure represents the C++ parser. */
1381
1382 typedef struct cp_parser GTY(())
1383 {
1384 /* The lexer from which we are obtaining tokens. */
1385 cp_lexer *lexer;
1386
1387 /* The scope in which names should be looked up. If NULL_TREE, then
1388 we look up names in the scope that is currently open in the
1389 source program. If non-NULL, this is either a TYPE or
1390 NAMESPACE_DECL for the scope in which we should look. It can
1391 also be ERROR_MARK, when we've parsed a bogus scope.
1392
1393 This value is not cleared automatically after a name is looked
1394 up, so we must be careful to clear it before starting a new look
1395 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1396 will look up `Z' in the scope of `X', rather than the current
1397 scope.) Unfortunately, it is difficult to tell when name lookup
1398 is complete, because we sometimes peek at a token, look it up,
1399 and then decide not to consume it. */
1400 tree scope;
1401
1402 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1403 last lookup took place. OBJECT_SCOPE is used if an expression
1404 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1405 respectively. QUALIFYING_SCOPE is used for an expression of the
1406 form "X::Y"; it refers to X. */
1407 tree object_scope;
1408 tree qualifying_scope;
1409
1410 /* A stack of parsing contexts. All but the bottom entry on the
1411 stack will be tentative contexts.
1412
1413 We parse tentatively in order to determine which construct is in
1414 use in some situations. For example, in order to determine
1415 whether a statement is an expression-statement or a
1416 declaration-statement we parse it tentatively as a
1417 declaration-statement. If that fails, we then reparse the same
1418 token stream as an expression-statement. */
1419 cp_parser_context *context;
1420
1421 /* True if we are parsing GNU C++. If this flag is not set, then
1422 GNU extensions are not recognized. */
1423 bool allow_gnu_extensions_p;
1424
1425 /* TRUE if the `>' token should be interpreted as the greater-than
1426 operator. FALSE if it is the end of a template-id or
1427 template-parameter-list. In C++0x mode, this flag also applies to
1428 `>>' tokens, which are viewed as two consecutive `>' tokens when
1429 this flag is FALSE. */
1430 bool greater_than_is_operator_p;
1431
1432 /* TRUE if default arguments are allowed within a parameter list
1433 that starts at this point. FALSE if only a gnu extension makes
1434 them permissible. */
1435 bool default_arg_ok_p;
1436
1437 /* TRUE if we are parsing an integral constant-expression. See
1438 [expr.const] for a precise definition. */
1439 bool integral_constant_expression_p;
1440
1441 /* TRUE if we are parsing an integral constant-expression -- but a
1442 non-constant expression should be permitted as well. This flag
1443 is used when parsing an array bound so that GNU variable-length
1444 arrays are tolerated. */
1445 bool allow_non_integral_constant_expression_p;
1446
1447 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1448 been seen that makes the expression non-constant. */
1449 bool non_integral_constant_expression_p;
1450
1451 /* TRUE if local variable names and `this' are forbidden in the
1452 current context. */
1453 bool local_variables_forbidden_p;
1454
1455 /* TRUE if the declaration we are parsing is part of a
1456 linkage-specification of the form `extern string-literal
1457 declaration'. */
1458 bool in_unbraced_linkage_specification_p;
1459
1460 /* TRUE if we are presently parsing a declarator, after the
1461 direct-declarator. */
1462 bool in_declarator_p;
1463
1464 /* TRUE if we are presently parsing a template-argument-list. */
1465 bool in_template_argument_list_p;
1466
1467 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1468 to IN_OMP_BLOCK if parsing OpenMP structured block and
1469 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1470 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1471 iteration-statement, OpenMP block or loop within that switch. */
1472 #define IN_SWITCH_STMT 1
1473 #define IN_ITERATION_STMT 2
1474 #define IN_OMP_BLOCK 4
1475 #define IN_OMP_FOR 8
1476 #define IN_IF_STMT 16
1477 unsigned char in_statement;
1478
1479 /* TRUE if we are presently parsing the body of a switch statement.
1480 Note that this doesn't quite overlap with in_statement above.
1481 The difference relates to giving the right sets of error messages:
1482 "case not in switch" vs "break statement used with OpenMP...". */
1483 bool in_switch_statement_p;
1484
1485 /* TRUE if we are parsing a type-id in an expression context. In
1486 such a situation, both "type (expr)" and "type (type)" are valid
1487 alternatives. */
1488 bool in_type_id_in_expr_p;
1489
1490 /* TRUE if we are currently in a header file where declarations are
1491 implicitly extern "C". */
1492 bool implicit_extern_c;
1493
1494 /* TRUE if strings in expressions should be translated to the execution
1495 character set. */
1496 bool translate_strings_p;
1497
1498 /* TRUE if we are presently parsing the body of a function, but not
1499 a local class. */
1500 bool in_function_body;
1501
1502 /* If non-NULL, then we are parsing a construct where new type
1503 definitions are not permitted. The string stored here will be
1504 issued as an error message if a type is defined. */
1505 const char *type_definition_forbidden_message;
1506
1507 /* A list of lists. The outer list is a stack, used for member
1508 functions of local classes. At each level there are two sub-list,
1509 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1510 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1511 TREE_VALUE's. The functions are chained in reverse declaration
1512 order.
1513
1514 The TREE_PURPOSE sublist contains those functions with default
1515 arguments that need post processing, and the TREE_VALUE sublist
1516 contains those functions with definitions that need post
1517 processing.
1518
1519 These lists can only be processed once the outermost class being
1520 defined is complete. */
1521 tree unparsed_functions_queues;
1522
1523 /* The number of classes whose definitions are currently in
1524 progress. */
1525 unsigned num_classes_being_defined;
1526
1527 /* The number of template parameter lists that apply directly to the
1528 current declaration. */
1529 unsigned num_template_parameter_lists;
1530 } cp_parser;
1531
1532 /* Prototypes. */
1533
1534 /* Constructors and destructors. */
1535
1536 static cp_parser *cp_parser_new
1537 (void);
1538
1539 /* Routines to parse various constructs.
1540
1541 Those that return `tree' will return the error_mark_node (rather
1542 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1543 Sometimes, they will return an ordinary node if error-recovery was
1544 attempted, even though a parse error occurred. So, to check
1545 whether or not a parse error occurred, you should always use
1546 cp_parser_error_occurred. If the construct is optional (indicated
1547 either by an `_opt' in the name of the function that does the
1548 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1549 the construct is not present. */
1550
1551 /* Lexical conventions [gram.lex] */
1552
1553 static tree cp_parser_identifier
1554 (cp_parser *);
1555 static tree cp_parser_string_literal
1556 (cp_parser *, bool, bool);
1557
1558 /* Basic concepts [gram.basic] */
1559
1560 static bool cp_parser_translation_unit
1561 (cp_parser *);
1562
1563 /* Expressions [gram.expr] */
1564
1565 static tree cp_parser_primary_expression
1566 (cp_parser *, bool, bool, bool, cp_id_kind *);
1567 static tree cp_parser_id_expression
1568 (cp_parser *, bool, bool, bool *, bool, bool);
1569 static tree cp_parser_unqualified_id
1570 (cp_parser *, bool, bool, bool, bool);
1571 static tree cp_parser_nested_name_specifier_opt
1572 (cp_parser *, bool, bool, bool, bool);
1573 static tree cp_parser_nested_name_specifier
1574 (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_class_or_namespace_name
1576 (cp_parser *, bool, bool, bool, bool, bool);
1577 static tree cp_parser_postfix_expression
1578 (cp_parser *, bool, bool, bool);
1579 static tree cp_parser_postfix_open_square_expression
1580 (cp_parser *, tree, bool);
1581 static tree cp_parser_postfix_dot_deref_expression
1582 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1583 static tree cp_parser_parenthesized_expression_list
1584 (cp_parser *, bool, bool, bool, bool *);
1585 static void cp_parser_pseudo_destructor_name
1586 (cp_parser *, tree *, tree *);
1587 static tree cp_parser_unary_expression
1588 (cp_parser *, bool, bool);
1589 static enum tree_code cp_parser_unary_operator
1590 (cp_token *);
1591 static tree cp_parser_new_expression
1592 (cp_parser *);
1593 static tree cp_parser_new_placement
1594 (cp_parser *);
1595 static tree cp_parser_new_type_id
1596 (cp_parser *, tree *);
1597 static cp_declarator *cp_parser_new_declarator_opt
1598 (cp_parser *);
1599 static cp_declarator *cp_parser_direct_new_declarator
1600 (cp_parser *);
1601 static tree cp_parser_new_initializer
1602 (cp_parser *);
1603 static tree cp_parser_delete_expression
1604 (cp_parser *);
1605 static tree cp_parser_cast_expression
1606 (cp_parser *, bool, bool);
1607 static tree cp_parser_binary_expression
1608 (cp_parser *, bool);
1609 static tree cp_parser_question_colon_clause
1610 (cp_parser *, tree);
1611 static tree cp_parser_assignment_expression
1612 (cp_parser *, bool);
1613 static enum tree_code cp_parser_assignment_operator_opt
1614 (cp_parser *);
1615 static tree cp_parser_expression
1616 (cp_parser *, bool);
1617 static tree cp_parser_constant_expression
1618 (cp_parser *, bool, bool *);
1619 static tree cp_parser_builtin_offsetof
1620 (cp_parser *);
1621
1622 /* Statements [gram.stmt.stmt] */
1623
1624 static void cp_parser_statement
1625 (cp_parser *, tree, bool, bool *);
1626 static void cp_parser_label_for_labeled_statement
1627 (cp_parser *);
1628 static tree cp_parser_expression_statement
1629 (cp_parser *, tree);
1630 static tree cp_parser_compound_statement
1631 (cp_parser *, tree, bool);
1632 static void cp_parser_statement_seq_opt
1633 (cp_parser *, tree);
1634 static tree cp_parser_selection_statement
1635 (cp_parser *, bool *);
1636 static tree cp_parser_condition
1637 (cp_parser *);
1638 static tree cp_parser_iteration_statement
1639 (cp_parser *);
1640 static void cp_parser_for_init_statement
1641 (cp_parser *);
1642 static tree cp_parser_jump_statement
1643 (cp_parser *);
1644 static void cp_parser_declaration_statement
1645 (cp_parser *);
1646
1647 static tree cp_parser_implicitly_scoped_statement
1648 (cp_parser *, bool *);
1649 static void cp_parser_already_scoped_statement
1650 (cp_parser *);
1651
1652 /* Declarations [gram.dcl.dcl] */
1653
1654 static void cp_parser_declaration_seq_opt
1655 (cp_parser *);
1656 static void cp_parser_declaration
1657 (cp_parser *);
1658 static void cp_parser_block_declaration
1659 (cp_parser *, bool);
1660 static void cp_parser_simple_declaration
1661 (cp_parser *, bool);
1662 static void cp_parser_decl_specifier_seq
1663 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1664 static tree cp_parser_storage_class_specifier_opt
1665 (cp_parser *);
1666 static tree cp_parser_function_specifier_opt
1667 (cp_parser *, cp_decl_specifier_seq *);
1668 static tree cp_parser_type_specifier
1669 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1670 int *, bool *);
1671 static tree cp_parser_simple_type_specifier
1672 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1673 static tree cp_parser_type_name
1674 (cp_parser *);
1675 static tree cp_parser_nonclass_name
1676 (cp_parser* parser);
1677 static tree cp_parser_elaborated_type_specifier
1678 (cp_parser *, bool, bool);
1679 static tree cp_parser_enum_specifier
1680 (cp_parser *);
1681 static void cp_parser_enumerator_list
1682 (cp_parser *, tree);
1683 static void cp_parser_enumerator_definition
1684 (cp_parser *, tree);
1685 static tree cp_parser_namespace_name
1686 (cp_parser *);
1687 static void cp_parser_namespace_definition
1688 (cp_parser *);
1689 static void cp_parser_namespace_body
1690 (cp_parser *);
1691 static tree cp_parser_qualified_namespace_specifier
1692 (cp_parser *);
1693 static void cp_parser_namespace_alias_definition
1694 (cp_parser *);
1695 static bool cp_parser_using_declaration
1696 (cp_parser *, bool);
1697 static void cp_parser_using_directive
1698 (cp_parser *);
1699 static void cp_parser_asm_definition
1700 (cp_parser *);
1701 static void cp_parser_linkage_specification
1702 (cp_parser *);
1703 static void cp_parser_static_assert
1704 (cp_parser *, bool);
1705 static tree cp_parser_decltype
1706 (cp_parser *);
1707
1708 /* Declarators [gram.dcl.decl] */
1709
1710 static tree cp_parser_init_declarator
1711 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1712 static cp_declarator *cp_parser_declarator
1713 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1714 static cp_declarator *cp_parser_direct_declarator
1715 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1716 static enum tree_code cp_parser_ptr_operator
1717 (cp_parser *, tree *, cp_cv_quals *);
1718 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1719 (cp_parser *);
1720 static tree cp_parser_declarator_id
1721 (cp_parser *, bool);
1722 static tree cp_parser_type_id
1723 (cp_parser *);
1724 static void cp_parser_type_specifier_seq
1725 (cp_parser *, bool, cp_decl_specifier_seq *);
1726 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1727 (cp_parser *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1729 (cp_parser *, bool *);
1730 static cp_parameter_declarator *cp_parser_parameter_declaration
1731 (cp_parser *, bool, bool *);
1732 static tree cp_parser_default_argument
1733 (cp_parser *, bool);
1734 static void cp_parser_function_body
1735 (cp_parser *);
1736 static tree cp_parser_initializer
1737 (cp_parser *, bool *, bool *);
1738 static tree cp_parser_initializer_clause
1739 (cp_parser *, bool *);
1740 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1741 (cp_parser *, bool *);
1742
1743 static bool cp_parser_ctor_initializer_opt_and_function_body
1744 (cp_parser *);
1745
1746 /* Classes [gram.class] */
1747
1748 static tree cp_parser_class_name
1749 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1750 static tree cp_parser_class_specifier
1751 (cp_parser *);
1752 static tree cp_parser_class_head
1753 (cp_parser *, bool *, tree *, tree *);
1754 static enum tag_types cp_parser_class_key
1755 (cp_parser *);
1756 static void cp_parser_member_specification_opt
1757 (cp_parser *);
1758 static void cp_parser_member_declaration
1759 (cp_parser *);
1760 static tree cp_parser_pure_specifier
1761 (cp_parser *);
1762 static tree cp_parser_constant_initializer
1763 (cp_parser *);
1764
1765 /* Derived classes [gram.class.derived] */
1766
1767 static tree cp_parser_base_clause
1768 (cp_parser *);
1769 static tree cp_parser_base_specifier
1770 (cp_parser *);
1771
1772 /* Special member functions [gram.special] */
1773
1774 static tree cp_parser_conversion_function_id
1775 (cp_parser *);
1776 static tree cp_parser_conversion_type_id
1777 (cp_parser *);
1778 static cp_declarator *cp_parser_conversion_declarator_opt
1779 (cp_parser *);
1780 static bool cp_parser_ctor_initializer_opt
1781 (cp_parser *);
1782 static void cp_parser_mem_initializer_list
1783 (cp_parser *);
1784 static tree cp_parser_mem_initializer
1785 (cp_parser *);
1786 static tree cp_parser_mem_initializer_id
1787 (cp_parser *);
1788
1789 /* Overloading [gram.over] */
1790
1791 static tree cp_parser_operator_function_id
1792 (cp_parser *);
1793 static tree cp_parser_operator
1794 (cp_parser *);
1795
1796 /* Templates [gram.temp] */
1797
1798 static void cp_parser_template_declaration
1799 (cp_parser *, bool);
1800 static tree cp_parser_template_parameter_list
1801 (cp_parser *);
1802 static tree cp_parser_template_parameter
1803 (cp_parser *, bool *, bool *);
1804 static tree cp_parser_type_parameter
1805 (cp_parser *, bool *);
1806 static tree cp_parser_template_id
1807 (cp_parser *, bool, bool, bool);
1808 static tree cp_parser_template_name
1809 (cp_parser *, bool, bool, bool, bool *);
1810 static tree cp_parser_template_argument_list
1811 (cp_parser *);
1812 static tree cp_parser_template_argument
1813 (cp_parser *);
1814 static void cp_parser_explicit_instantiation
1815 (cp_parser *);
1816 static void cp_parser_explicit_specialization
1817 (cp_parser *);
1818
1819 /* Exception handling [gram.exception] */
1820
1821 static tree cp_parser_try_block
1822 (cp_parser *);
1823 static bool cp_parser_function_try_block
1824 (cp_parser *);
1825 static void cp_parser_handler_seq
1826 (cp_parser *);
1827 static void cp_parser_handler
1828 (cp_parser *);
1829 static tree cp_parser_exception_declaration
1830 (cp_parser *);
1831 static tree cp_parser_throw_expression
1832 (cp_parser *);
1833 static tree cp_parser_exception_specification_opt
1834 (cp_parser *);
1835 static tree cp_parser_type_id_list
1836 (cp_parser *);
1837
1838 /* GNU Extensions */
1839
1840 static tree cp_parser_asm_specification_opt
1841 (cp_parser *);
1842 static tree cp_parser_asm_operand_list
1843 (cp_parser *);
1844 static tree cp_parser_asm_clobber_list
1845 (cp_parser *);
1846 static tree cp_parser_attributes_opt
1847 (cp_parser *);
1848 static tree cp_parser_attribute_list
1849 (cp_parser *);
1850 static bool cp_parser_extension_opt
1851 (cp_parser *, int *);
1852 static void cp_parser_label_declaration
1853 (cp_parser *);
1854
1855 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1856 static bool cp_parser_pragma
1857 (cp_parser *, enum pragma_context);
1858
1859 /* Objective-C++ Productions */
1860
1861 static tree cp_parser_objc_message_receiver
1862 (cp_parser *);
1863 static tree cp_parser_objc_message_args
1864 (cp_parser *);
1865 static tree cp_parser_objc_message_expression
1866 (cp_parser *);
1867 static tree cp_parser_objc_encode_expression
1868 (cp_parser *);
1869 static tree cp_parser_objc_defs_expression
1870 (cp_parser *);
1871 static tree cp_parser_objc_protocol_expression
1872 (cp_parser *);
1873 static tree cp_parser_objc_selector_expression
1874 (cp_parser *);
1875 static tree cp_parser_objc_expression
1876 (cp_parser *);
1877 static bool cp_parser_objc_selector_p
1878 (enum cpp_ttype);
1879 static tree cp_parser_objc_selector
1880 (cp_parser *);
1881 static tree cp_parser_objc_protocol_refs_opt
1882 (cp_parser *);
1883 static void cp_parser_objc_declaration
1884 (cp_parser *);
1885 static tree cp_parser_objc_statement
1886 (cp_parser *);
1887
1888 /* Utility Routines */
1889
1890 static tree cp_parser_lookup_name
1891 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1892 static tree cp_parser_lookup_name_simple
1893 (cp_parser *, tree);
1894 static tree cp_parser_maybe_treat_template_as_class
1895 (tree, bool);
1896 static bool cp_parser_check_declarator_template_parameters
1897 (cp_parser *, cp_declarator *);
1898 static bool cp_parser_check_template_parameters
1899 (cp_parser *, unsigned);
1900 static tree cp_parser_simple_cast_expression
1901 (cp_parser *);
1902 static tree cp_parser_global_scope_opt
1903 (cp_parser *, bool);
1904 static bool cp_parser_constructor_declarator_p
1905 (cp_parser *, bool);
1906 static tree cp_parser_function_definition_from_specifiers_and_declarator
1907 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1908 static tree cp_parser_function_definition_after_declarator
1909 (cp_parser *, bool);
1910 static void cp_parser_template_declaration_after_export
1911 (cp_parser *, bool);
1912 static void cp_parser_perform_template_parameter_access_checks
1913 (VEC (deferred_access_check,gc)*);
1914 static tree cp_parser_single_declaration
1915 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1916 static tree cp_parser_functional_cast
1917 (cp_parser *, tree);
1918 static tree cp_parser_save_member_function_body
1919 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1920 static tree cp_parser_enclosed_template_argument_list
1921 (cp_parser *);
1922 static void cp_parser_save_default_args
1923 (cp_parser *, tree);
1924 static void cp_parser_late_parsing_for_member
1925 (cp_parser *, tree);
1926 static void cp_parser_late_parsing_default_args
1927 (cp_parser *, tree);
1928 static tree cp_parser_sizeof_operand
1929 (cp_parser *, enum rid);
1930 static tree cp_parser_trait_expr
1931 (cp_parser *, enum rid);
1932 static bool cp_parser_declares_only_class_p
1933 (cp_parser *);
1934 static void cp_parser_set_storage_class
1935 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1936 static void cp_parser_set_decl_spec_type
1937 (cp_decl_specifier_seq *, tree, bool);
1938 static bool cp_parser_friend_p
1939 (const cp_decl_specifier_seq *);
1940 static cp_token *cp_parser_require
1941 (cp_parser *, enum cpp_ttype, const char *);
1942 static cp_token *cp_parser_require_keyword
1943 (cp_parser *, enum rid, const char *);
1944 static bool cp_parser_token_starts_function_definition_p
1945 (cp_token *);
1946 static bool cp_parser_next_token_starts_class_definition_p
1947 (cp_parser *);
1948 static bool cp_parser_next_token_ends_template_argument_p
1949 (cp_parser *);
1950 static bool cp_parser_nth_token_starts_template_argument_list_p
1951 (cp_parser *, size_t);
1952 static enum tag_types cp_parser_token_is_class_key
1953 (cp_token *);
1954 static void cp_parser_check_class_key
1955 (enum tag_types, tree type);
1956 static void cp_parser_check_access_in_redeclaration
1957 (tree type);
1958 static bool cp_parser_optional_template_keyword
1959 (cp_parser *);
1960 static void cp_parser_pre_parsed_nested_name_specifier
1961 (cp_parser *);
1962 static void cp_parser_cache_group
1963 (cp_parser *, enum cpp_ttype, unsigned);
1964 static void cp_parser_parse_tentatively
1965 (cp_parser *);
1966 static void cp_parser_commit_to_tentative_parse
1967 (cp_parser *);
1968 static void cp_parser_abort_tentative_parse
1969 (cp_parser *);
1970 static bool cp_parser_parse_definitely
1971 (cp_parser *);
1972 static inline bool cp_parser_parsing_tentatively
1973 (cp_parser *);
1974 static bool cp_parser_uncommitted_to_tentative_parse_p
1975 (cp_parser *);
1976 static void cp_parser_error
1977 (cp_parser *, const char *);
1978 static void cp_parser_name_lookup_error
1979 (cp_parser *, tree, tree, const char *);
1980 static bool cp_parser_simulate_error
1981 (cp_parser *);
1982 static bool cp_parser_check_type_definition
1983 (cp_parser *);
1984 static void cp_parser_check_for_definition_in_return_type
1985 (cp_declarator *, tree);
1986 static void cp_parser_check_for_invalid_template_id
1987 (cp_parser *, tree);
1988 static bool cp_parser_non_integral_constant_expression
1989 (cp_parser *, const char *);
1990 static void cp_parser_diagnose_invalid_type_name
1991 (cp_parser *, tree, tree);
1992 static bool cp_parser_parse_and_diagnose_invalid_type_name
1993 (cp_parser *);
1994 static int cp_parser_skip_to_closing_parenthesis
1995 (cp_parser *, bool, bool, bool);
1996 static void cp_parser_skip_to_end_of_statement
1997 (cp_parser *);
1998 static void cp_parser_consume_semicolon_at_end_of_statement
1999 (cp_parser *);
2000 static void cp_parser_skip_to_end_of_block_or_statement
2001 (cp_parser *);
2002 static bool cp_parser_skip_to_closing_brace
2003 (cp_parser *);
2004 static void cp_parser_skip_to_end_of_template_parameter_list
2005 (cp_parser *);
2006 static void cp_parser_skip_to_pragma_eol
2007 (cp_parser*, cp_token *);
2008 static bool cp_parser_error_occurred
2009 (cp_parser *);
2010 static bool cp_parser_allow_gnu_extensions_p
2011 (cp_parser *);
2012 static bool cp_parser_is_string_literal
2013 (cp_token *);
2014 static bool cp_parser_is_keyword
2015 (cp_token *, enum rid);
2016 static tree cp_parser_make_typename_type
2017 (cp_parser *, tree, tree);
2018 static cp_declarator * cp_parser_make_indirect_declarator
2019 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2020
2021 /* Returns nonzero if we are parsing tentatively. */
2022
2023 static inline bool
2024 cp_parser_parsing_tentatively (cp_parser* parser)
2025 {
2026 return parser->context->next != NULL;
2027 }
2028
2029 /* Returns nonzero if TOKEN is a string literal. */
2030
2031 static bool
2032 cp_parser_is_string_literal (cp_token* token)
2033 {
2034 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2035 }
2036
2037 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2038
2039 static bool
2040 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2041 {
2042 return token->keyword == keyword;
2043 }
2044
2045 /* If not parsing tentatively, issue a diagnostic of the form
2046 FILE:LINE: MESSAGE before TOKEN
2047 where TOKEN is the next token in the input stream. MESSAGE
2048 (specified by the caller) is usually of the form "expected
2049 OTHER-TOKEN". */
2050
2051 static void
2052 cp_parser_error (cp_parser* parser, const char* message)
2053 {
2054 if (!cp_parser_simulate_error (parser))
2055 {
2056 cp_token *token = cp_lexer_peek_token (parser->lexer);
2057 /* This diagnostic makes more sense if it is tagged to the line
2058 of the token we just peeked at. */
2059 cp_lexer_set_source_position_from_token (token);
2060
2061 if (token->type == CPP_PRAGMA)
2062 {
2063 error ("%<#pragma%> is not allowed here");
2064 cp_parser_skip_to_pragma_eol (parser, token);
2065 return;
2066 }
2067
2068 c_parse_error (message,
2069 /* Because c_parser_error does not understand
2070 CPP_KEYWORD, keywords are treated like
2071 identifiers. */
2072 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2073 token->u.value);
2074 }
2075 }
2076
2077 /* Issue an error about name-lookup failing. NAME is the
2078 IDENTIFIER_NODE DECL is the result of
2079 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2080 the thing that we hoped to find. */
2081
2082 static void
2083 cp_parser_name_lookup_error (cp_parser* parser,
2084 tree name,
2085 tree decl,
2086 const char* desired)
2087 {
2088 /* If name lookup completely failed, tell the user that NAME was not
2089 declared. */
2090 if (decl == error_mark_node)
2091 {
2092 if (parser->scope && parser->scope != global_namespace)
2093 error ("%<%E::%E%> has not been declared",
2094 parser->scope, name);
2095 else if (parser->scope == global_namespace)
2096 error ("%<::%E%> has not been declared", name);
2097 else if (parser->object_scope
2098 && !CLASS_TYPE_P (parser->object_scope))
2099 error ("request for member %qE in non-class type %qT",
2100 name, parser->object_scope);
2101 else if (parser->object_scope)
2102 error ("%<%T::%E%> has not been declared",
2103 parser->object_scope, name);
2104 else
2105 error ("%qE has not been declared", name);
2106 }
2107 else if (parser->scope && parser->scope != global_namespace)
2108 error ("%<%E::%E%> %s", parser->scope, name, desired);
2109 else if (parser->scope == global_namespace)
2110 error ("%<::%E%> %s", name, desired);
2111 else
2112 error ("%qE %s", name, desired);
2113 }
2114
2115 /* If we are parsing tentatively, remember that an error has occurred
2116 during this tentative parse. Returns true if the error was
2117 simulated; false if a message should be issued by the caller. */
2118
2119 static bool
2120 cp_parser_simulate_error (cp_parser* parser)
2121 {
2122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2123 {
2124 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2125 return true;
2126 }
2127 return false;
2128 }
2129
2130 /* Check for repeated decl-specifiers. */
2131
2132 static void
2133 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2134 {
2135 cp_decl_spec ds;
2136
2137 for (ds = ds_first; ds != ds_last; ++ds)
2138 {
2139 unsigned count = decl_specs->specs[(int)ds];
2140 if (count < 2)
2141 continue;
2142 /* The "long" specifier is a special case because of "long long". */
2143 if (ds == ds_long)
2144 {
2145 if (count > 2)
2146 error ("%<long long long%> is too long for GCC");
2147 else if (pedantic && !in_system_header && warn_long_long
2148 && cxx_dialect == cxx98)
2149 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2150 }
2151 else if (count > 1)
2152 {
2153 static const char *const decl_spec_names[] = {
2154 "signed",
2155 "unsigned",
2156 "short",
2157 "long",
2158 "const",
2159 "volatile",
2160 "restrict",
2161 "inline",
2162 "virtual",
2163 "explicit",
2164 "friend",
2165 "typedef",
2166 "__complex",
2167 "__thread"
2168 };
2169 error ("duplicate %qs", decl_spec_names[(int)ds]);
2170 }
2171 }
2172 }
2173
2174 /* This function is called when a type is defined. If type
2175 definitions are forbidden at this point, an error message is
2176 issued. */
2177
2178 static bool
2179 cp_parser_check_type_definition (cp_parser* parser)
2180 {
2181 /* If types are forbidden here, issue a message. */
2182 if (parser->type_definition_forbidden_message)
2183 {
2184 /* Use `%s' to print the string in case there are any escape
2185 characters in the message. */
2186 error ("%s", parser->type_definition_forbidden_message);
2187 return false;
2188 }
2189 return true;
2190 }
2191
2192 /* This function is called when the DECLARATOR is processed. The TYPE
2193 was a type defined in the decl-specifiers. If it is invalid to
2194 define a type in the decl-specifiers for DECLARATOR, an error is
2195 issued. */
2196
2197 static void
2198 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2199 tree type)
2200 {
2201 /* [dcl.fct] forbids type definitions in return types.
2202 Unfortunately, it's not easy to know whether or not we are
2203 processing a return type until after the fact. */
2204 while (declarator
2205 && (declarator->kind == cdk_pointer
2206 || declarator->kind == cdk_reference
2207 || declarator->kind == cdk_ptrmem))
2208 declarator = declarator->declarator;
2209 if (declarator
2210 && declarator->kind == cdk_function)
2211 {
2212 error ("new types may not be defined in a return type");
2213 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2214 type);
2215 }
2216 }
2217
2218 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2219 "<" in any valid C++ program. If the next token is indeed "<",
2220 issue a message warning the user about what appears to be an
2221 invalid attempt to form a template-id. */
2222
2223 static void
2224 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2225 tree type)
2226 {
2227 cp_token_position start = 0;
2228
2229 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2230 {
2231 if (TYPE_P (type))
2232 error ("%qT is not a template", type);
2233 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2234 error ("%qE is not a template", type);
2235 else
2236 error ("invalid template-id");
2237 /* Remember the location of the invalid "<". */
2238 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2239 start = cp_lexer_token_position (parser->lexer, true);
2240 /* Consume the "<". */
2241 cp_lexer_consume_token (parser->lexer);
2242 /* Parse the template arguments. */
2243 cp_parser_enclosed_template_argument_list (parser);
2244 /* Permanently remove the invalid template arguments so that
2245 this error message is not issued again. */
2246 if (start)
2247 cp_lexer_purge_tokens_after (parser->lexer, start);
2248 }
2249 }
2250
2251 /* If parsing an integral constant-expression, issue an error message
2252 about the fact that THING appeared and return true. Otherwise,
2253 return false. In either case, set
2254 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2255
2256 static bool
2257 cp_parser_non_integral_constant_expression (cp_parser *parser,
2258 const char *thing)
2259 {
2260 parser->non_integral_constant_expression_p = true;
2261 if (parser->integral_constant_expression_p)
2262 {
2263 if (!parser->allow_non_integral_constant_expression_p)
2264 {
2265 error ("%s cannot appear in a constant-expression", thing);
2266 return true;
2267 }
2268 }
2269 return false;
2270 }
2271
2272 /* Emit a diagnostic for an invalid type name. SCOPE is the
2273 qualifying scope (or NULL, if none) for ID. This function commits
2274 to the current active tentative parse, if any. (Otherwise, the
2275 problematic construct might be encountered again later, resulting
2276 in duplicate error messages.) */
2277
2278 static void
2279 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2280 {
2281 tree decl, old_scope;
2282 /* Try to lookup the identifier. */
2283 old_scope = parser->scope;
2284 parser->scope = scope;
2285 decl = cp_parser_lookup_name_simple (parser, id);
2286 parser->scope = old_scope;
2287 /* If the lookup found a template-name, it means that the user forgot
2288 to specify an argument list. Emit a useful error message. */
2289 if (TREE_CODE (decl) == TEMPLATE_DECL)
2290 error ("invalid use of template-name %qE without an argument list", decl);
2291 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2292 error ("invalid use of destructor %qD as a type", id);
2293 else if (TREE_CODE (decl) == TYPE_DECL)
2294 /* Something like 'unsigned A a;' */
2295 error ("invalid combination of multiple type-specifiers");
2296 else if (!parser->scope)
2297 {
2298 /* Issue an error message. */
2299 error ("%qE does not name a type", id);
2300 /* If we're in a template class, it's possible that the user was
2301 referring to a type from a base class. For example:
2302
2303 template <typename T> struct A { typedef T X; };
2304 template <typename T> struct B : public A<T> { X x; };
2305
2306 The user should have said "typename A<T>::X". */
2307 if (processing_template_decl && current_class_type
2308 && TYPE_BINFO (current_class_type))
2309 {
2310 tree b;
2311
2312 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2313 b;
2314 b = TREE_CHAIN (b))
2315 {
2316 tree base_type = BINFO_TYPE (b);
2317 if (CLASS_TYPE_P (base_type)
2318 && dependent_type_p (base_type))
2319 {
2320 tree field;
2321 /* Go from a particular instantiation of the
2322 template (which will have an empty TYPE_FIELDs),
2323 to the main version. */
2324 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2325 for (field = TYPE_FIELDS (base_type);
2326 field;
2327 field = TREE_CHAIN (field))
2328 if (TREE_CODE (field) == TYPE_DECL
2329 && DECL_NAME (field) == id)
2330 {
2331 inform ("(perhaps %<typename %T::%E%> was intended)",
2332 BINFO_TYPE (b), id);
2333 break;
2334 }
2335 if (field)
2336 break;
2337 }
2338 }
2339 }
2340 }
2341 /* Here we diagnose qualified-ids where the scope is actually correct,
2342 but the identifier does not resolve to a valid type name. */
2343 else if (parser->scope != error_mark_node)
2344 {
2345 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2346 error ("%qE in namespace %qE does not name a type",
2347 id, parser->scope);
2348 else if (TYPE_P (parser->scope))
2349 error ("%qE in class %qT does not name a type", id, parser->scope);
2350 else
2351 gcc_unreachable ();
2352 }
2353 cp_parser_commit_to_tentative_parse (parser);
2354 }
2355
2356 /* Check for a common situation where a type-name should be present,
2357 but is not, and issue a sensible error message. Returns true if an
2358 invalid type-name was detected.
2359
2360 The situation handled by this function are variable declarations of the
2361 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2362 Usually, `ID' should name a type, but if we got here it means that it
2363 does not. We try to emit the best possible error message depending on
2364 how exactly the id-expression looks like. */
2365
2366 static bool
2367 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2368 {
2369 tree id;
2370
2371 cp_parser_parse_tentatively (parser);
2372 id = cp_parser_id_expression (parser,
2373 /*template_keyword_p=*/false,
2374 /*check_dependency_p=*/true,
2375 /*template_p=*/NULL,
2376 /*declarator_p=*/true,
2377 /*optional_p=*/false);
2378 /* After the id-expression, there should be a plain identifier,
2379 otherwise this is not a simple variable declaration. Also, if
2380 the scope is dependent, we cannot do much. */
2381 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2382 || (parser->scope && TYPE_P (parser->scope)
2383 && dependent_type_p (parser->scope))
2384 || TREE_CODE (id) == TYPE_DECL)
2385 {
2386 cp_parser_abort_tentative_parse (parser);
2387 return false;
2388 }
2389 if (!cp_parser_parse_definitely (parser))
2390 return false;
2391
2392 /* Emit a diagnostic for the invalid type. */
2393 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2394 /* Skip to the end of the declaration; there's no point in
2395 trying to process it. */
2396 cp_parser_skip_to_end_of_block_or_statement (parser);
2397 return true;
2398 }
2399
2400 /* Consume tokens up to, and including, the next non-nested closing `)'.
2401 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2402 are doing error recovery. Returns -1 if OR_COMMA is true and we
2403 found an unnested comma. */
2404
2405 static int
2406 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2407 bool recovering,
2408 bool or_comma,
2409 bool consume_paren)
2410 {
2411 unsigned paren_depth = 0;
2412 unsigned brace_depth = 0;
2413
2414 if (recovering && !or_comma
2415 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2416 return 0;
2417
2418 while (true)
2419 {
2420 cp_token * token = cp_lexer_peek_token (parser->lexer);
2421
2422 switch (token->type)
2423 {
2424 case CPP_EOF:
2425 case CPP_PRAGMA_EOL:
2426 /* If we've run out of tokens, then there is no closing `)'. */
2427 return 0;
2428
2429 case CPP_SEMICOLON:
2430 /* This matches the processing in skip_to_end_of_statement. */
2431 if (!brace_depth)
2432 return 0;
2433 break;
2434
2435 case CPP_OPEN_BRACE:
2436 ++brace_depth;
2437 break;
2438 case CPP_CLOSE_BRACE:
2439 if (!brace_depth--)
2440 return 0;
2441 break;
2442
2443 case CPP_COMMA:
2444 if (recovering && or_comma && !brace_depth && !paren_depth)
2445 return -1;
2446 break;
2447
2448 case CPP_OPEN_PAREN:
2449 if (!brace_depth)
2450 ++paren_depth;
2451 break;
2452
2453 case CPP_CLOSE_PAREN:
2454 if (!brace_depth && !paren_depth--)
2455 {
2456 if (consume_paren)
2457 cp_lexer_consume_token (parser->lexer);
2458 return 1;
2459 }
2460 break;
2461
2462 default:
2463 break;
2464 }
2465
2466 /* Consume the token. */
2467 cp_lexer_consume_token (parser->lexer);
2468 }
2469 }
2470
2471 /* Consume tokens until we reach the end of the current statement.
2472 Normally, that will be just before consuming a `;'. However, if a
2473 non-nested `}' comes first, then we stop before consuming that. */
2474
2475 static void
2476 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2477 {
2478 unsigned nesting_depth = 0;
2479
2480 while (true)
2481 {
2482 cp_token *token = cp_lexer_peek_token (parser->lexer);
2483
2484 switch (token->type)
2485 {
2486 case CPP_EOF:
2487 case CPP_PRAGMA_EOL:
2488 /* If we've run out of tokens, stop. */
2489 return;
2490
2491 case CPP_SEMICOLON:
2492 /* If the next token is a `;', we have reached the end of the
2493 statement. */
2494 if (!nesting_depth)
2495 return;
2496 break;
2497
2498 case CPP_CLOSE_BRACE:
2499 /* If this is a non-nested '}', stop before consuming it.
2500 That way, when confronted with something like:
2501
2502 { 3 + }
2503
2504 we stop before consuming the closing '}', even though we
2505 have not yet reached a `;'. */
2506 if (nesting_depth == 0)
2507 return;
2508
2509 /* If it is the closing '}' for a block that we have
2510 scanned, stop -- but only after consuming the token.
2511 That way given:
2512
2513 void f g () { ... }
2514 typedef int I;
2515
2516 we will stop after the body of the erroneously declared
2517 function, but before consuming the following `typedef'
2518 declaration. */
2519 if (--nesting_depth == 0)
2520 {
2521 cp_lexer_consume_token (parser->lexer);
2522 return;
2523 }
2524
2525 case CPP_OPEN_BRACE:
2526 ++nesting_depth;
2527 break;
2528
2529 default:
2530 break;
2531 }
2532
2533 /* Consume the token. */
2534 cp_lexer_consume_token (parser->lexer);
2535 }
2536 }
2537
2538 /* This function is called at the end of a statement or declaration.
2539 If the next token is a semicolon, it is consumed; otherwise, error
2540 recovery is attempted. */
2541
2542 static void
2543 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2544 {
2545 /* Look for the trailing `;'. */
2546 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2547 {
2548 /* If there is additional (erroneous) input, skip to the end of
2549 the statement. */
2550 cp_parser_skip_to_end_of_statement (parser);
2551 /* If the next token is now a `;', consume it. */
2552 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2553 cp_lexer_consume_token (parser->lexer);
2554 }
2555 }
2556
2557 /* Skip tokens until we have consumed an entire block, or until we
2558 have consumed a non-nested `;'. */
2559
2560 static void
2561 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2562 {
2563 int nesting_depth = 0;
2564
2565 while (nesting_depth >= 0)
2566 {
2567 cp_token *token = cp_lexer_peek_token (parser->lexer);
2568
2569 switch (token->type)
2570 {
2571 case CPP_EOF:
2572 case CPP_PRAGMA_EOL:
2573 /* If we've run out of tokens, stop. */
2574 return;
2575
2576 case CPP_SEMICOLON:
2577 /* Stop if this is an unnested ';'. */
2578 if (!nesting_depth)
2579 nesting_depth = -1;
2580 break;
2581
2582 case CPP_CLOSE_BRACE:
2583 /* Stop if this is an unnested '}', or closes the outermost
2584 nesting level. */
2585 nesting_depth--;
2586 if (!nesting_depth)
2587 nesting_depth = -1;
2588 break;
2589
2590 case CPP_OPEN_BRACE:
2591 /* Nest. */
2592 nesting_depth++;
2593 break;
2594
2595 default:
2596 break;
2597 }
2598
2599 /* Consume the token. */
2600 cp_lexer_consume_token (parser->lexer);
2601 }
2602 }
2603
2604 /* Skip tokens until a non-nested closing curly brace is the next
2605 token, or there are no more tokens. Return true in the first case,
2606 false otherwise. */
2607
2608 static bool
2609 cp_parser_skip_to_closing_brace (cp_parser *parser)
2610 {
2611 unsigned nesting_depth = 0;
2612
2613 while (true)
2614 {
2615 cp_token *token = cp_lexer_peek_token (parser->lexer);
2616
2617 switch (token->type)
2618 {
2619 case CPP_EOF:
2620 case CPP_PRAGMA_EOL:
2621 /* If we've run out of tokens, stop. */
2622 return false;
2623
2624 case CPP_CLOSE_BRACE:
2625 /* If the next token is a non-nested `}', then we have reached
2626 the end of the current block. */
2627 if (nesting_depth-- == 0)
2628 return true;
2629 break;
2630
2631 case CPP_OPEN_BRACE:
2632 /* If it the next token is a `{', then we are entering a new
2633 block. Consume the entire block. */
2634 ++nesting_depth;
2635 break;
2636
2637 default:
2638 break;
2639 }
2640
2641 /* Consume the token. */
2642 cp_lexer_consume_token (parser->lexer);
2643 }
2644 }
2645
2646 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2647 parameter is the PRAGMA token, allowing us to purge the entire pragma
2648 sequence. */
2649
2650 static void
2651 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2652 {
2653 cp_token *token;
2654
2655 parser->lexer->in_pragma = false;
2656
2657 do
2658 token = cp_lexer_consume_token (parser->lexer);
2659 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2660
2661 /* Ensure that the pragma is not parsed again. */
2662 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2663 }
2664
2665 /* Require pragma end of line, resyncing with it as necessary. The
2666 arguments are as for cp_parser_skip_to_pragma_eol. */
2667
2668 static void
2669 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2670 {
2671 parser->lexer->in_pragma = false;
2672 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2673 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2674 }
2675
2676 /* This is a simple wrapper around make_typename_type. When the id is
2677 an unresolved identifier node, we can provide a superior diagnostic
2678 using cp_parser_diagnose_invalid_type_name. */
2679
2680 static tree
2681 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2682 {
2683 tree result;
2684 if (TREE_CODE (id) == IDENTIFIER_NODE)
2685 {
2686 result = make_typename_type (scope, id, typename_type,
2687 /*complain=*/tf_none);
2688 if (result == error_mark_node)
2689 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2690 return result;
2691 }
2692 return make_typename_type (scope, id, typename_type, tf_error);
2693 }
2694
2695 /* This is a wrapper around the
2696 make_{pointer,ptrmem,reference}_declarator functions that decides
2697 which one to call based on the CODE and CLASS_TYPE arguments. The
2698 CODE argument should be one of the values returned by
2699 cp_parser_ptr_operator. */
2700 static cp_declarator *
2701 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2702 cp_cv_quals cv_qualifiers,
2703 cp_declarator *target)
2704 {
2705 if (code == ERROR_MARK)
2706 return cp_error_declarator;
2707
2708 if (code == INDIRECT_REF)
2709 if (class_type == NULL_TREE)
2710 return make_pointer_declarator (cv_qualifiers, target);
2711 else
2712 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2713 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2714 return make_reference_declarator (cv_qualifiers, target, false);
2715 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2716 return make_reference_declarator (cv_qualifiers, target, true);
2717 gcc_unreachable ();
2718 }
2719
2720 /* Create a new C++ parser. */
2721
2722 static cp_parser *
2723 cp_parser_new (void)
2724 {
2725 cp_parser *parser;
2726 cp_lexer *lexer;
2727 unsigned i;
2728
2729 /* cp_lexer_new_main is called before calling ggc_alloc because
2730 cp_lexer_new_main might load a PCH file. */
2731 lexer = cp_lexer_new_main ();
2732
2733 /* Initialize the binops_by_token so that we can get the tree
2734 directly from the token. */
2735 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2736 binops_by_token[binops[i].token_type] = binops[i];
2737
2738 parser = GGC_CNEW (cp_parser);
2739 parser->lexer = lexer;
2740 parser->context = cp_parser_context_new (NULL);
2741
2742 /* For now, we always accept GNU extensions. */
2743 parser->allow_gnu_extensions_p = 1;
2744
2745 /* The `>' token is a greater-than operator, not the end of a
2746 template-id. */
2747 parser->greater_than_is_operator_p = true;
2748
2749 parser->default_arg_ok_p = true;
2750
2751 /* We are not parsing a constant-expression. */
2752 parser->integral_constant_expression_p = false;
2753 parser->allow_non_integral_constant_expression_p = false;
2754 parser->non_integral_constant_expression_p = false;
2755
2756 /* Local variable names are not forbidden. */
2757 parser->local_variables_forbidden_p = false;
2758
2759 /* We are not processing an `extern "C"' declaration. */
2760 parser->in_unbraced_linkage_specification_p = false;
2761
2762 /* We are not processing a declarator. */
2763 parser->in_declarator_p = false;
2764
2765 /* We are not processing a template-argument-list. */
2766 parser->in_template_argument_list_p = false;
2767
2768 /* We are not in an iteration statement. */
2769 parser->in_statement = 0;
2770
2771 /* We are not in a switch statement. */
2772 parser->in_switch_statement_p = false;
2773
2774 /* We are not parsing a type-id inside an expression. */
2775 parser->in_type_id_in_expr_p = false;
2776
2777 /* Declarations aren't implicitly extern "C". */
2778 parser->implicit_extern_c = false;
2779
2780 /* String literals should be translated to the execution character set. */
2781 parser->translate_strings_p = true;
2782
2783 /* We are not parsing a function body. */
2784 parser->in_function_body = false;
2785
2786 /* The unparsed function queue is empty. */
2787 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2788
2789 /* There are no classes being defined. */
2790 parser->num_classes_being_defined = 0;
2791
2792 /* No template parameters apply. */
2793 parser->num_template_parameter_lists = 0;
2794
2795 return parser;
2796 }
2797
2798 /* Create a cp_lexer structure which will emit the tokens in CACHE
2799 and push it onto the parser's lexer stack. This is used for delayed
2800 parsing of in-class method bodies and default arguments, and should
2801 not be confused with tentative parsing. */
2802 static void
2803 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2804 {
2805 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2806 lexer->next = parser->lexer;
2807 parser->lexer = lexer;
2808
2809 /* Move the current source position to that of the first token in the
2810 new lexer. */
2811 cp_lexer_set_source_position_from_token (lexer->next_token);
2812 }
2813
2814 /* Pop the top lexer off the parser stack. This is never used for the
2815 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2816 static void
2817 cp_parser_pop_lexer (cp_parser *parser)
2818 {
2819 cp_lexer *lexer = parser->lexer;
2820 parser->lexer = lexer->next;
2821 cp_lexer_destroy (lexer);
2822
2823 /* Put the current source position back where it was before this
2824 lexer was pushed. */
2825 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2826 }
2827
2828 /* Lexical conventions [gram.lex] */
2829
2830 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2831 identifier. */
2832
2833 static tree
2834 cp_parser_identifier (cp_parser* parser)
2835 {
2836 cp_token *token;
2837
2838 /* Look for the identifier. */
2839 token = cp_parser_require (parser, CPP_NAME, "identifier");
2840 /* Return the value. */
2841 return token ? token->u.value : error_mark_node;
2842 }
2843
2844 /* Parse a sequence of adjacent string constants. Returns a
2845 TREE_STRING representing the combined, nul-terminated string
2846 constant. If TRANSLATE is true, translate the string to the
2847 execution character set. If WIDE_OK is true, a wide string is
2848 invalid here.
2849
2850 C++98 [lex.string] says that if a narrow string literal token is
2851 adjacent to a wide string literal token, the behavior is undefined.
2852 However, C99 6.4.5p4 says that this results in a wide string literal.
2853 We follow C99 here, for consistency with the C front end.
2854
2855 This code is largely lifted from lex_string() in c-lex.c.
2856
2857 FUTURE: ObjC++ will need to handle @-strings here. */
2858 static tree
2859 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2860 {
2861 tree value;
2862 bool wide = false;
2863 size_t count;
2864 struct obstack str_ob;
2865 cpp_string str, istr, *strs;
2866 cp_token *tok;
2867
2868 tok = cp_lexer_peek_token (parser->lexer);
2869 if (!cp_parser_is_string_literal (tok))
2870 {
2871 cp_parser_error (parser, "expected string-literal");
2872 return error_mark_node;
2873 }
2874
2875 /* Try to avoid the overhead of creating and destroying an obstack
2876 for the common case of just one string. */
2877 if (!cp_parser_is_string_literal
2878 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2879 {
2880 cp_lexer_consume_token (parser->lexer);
2881
2882 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2883 str.len = TREE_STRING_LENGTH (tok->u.value);
2884 count = 1;
2885 if (tok->type == CPP_WSTRING)
2886 wide = true;
2887
2888 strs = &str;
2889 }
2890 else
2891 {
2892 gcc_obstack_init (&str_ob);
2893 count = 0;
2894
2895 do
2896 {
2897 cp_lexer_consume_token (parser->lexer);
2898 count++;
2899 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2900 str.len = TREE_STRING_LENGTH (tok->u.value);
2901 if (tok->type == CPP_WSTRING)
2902 wide = true;
2903
2904 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2905
2906 tok = cp_lexer_peek_token (parser->lexer);
2907 }
2908 while (cp_parser_is_string_literal (tok));
2909
2910 strs = (cpp_string *) obstack_finish (&str_ob);
2911 }
2912
2913 if (wide && !wide_ok)
2914 {
2915 cp_parser_error (parser, "a wide string is invalid in this context");
2916 wide = false;
2917 }
2918
2919 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2920 (parse_in, strs, count, &istr, wide))
2921 {
2922 value = build_string (istr.len, (const char *)istr.text);
2923 free (CONST_CAST (unsigned char *, istr.text));
2924
2925 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2926 value = fix_string_type (value);
2927 }
2928 else
2929 /* cpp_interpret_string has issued an error. */
2930 value = error_mark_node;
2931
2932 if (count > 1)
2933 obstack_free (&str_ob, 0);
2934
2935 return value;
2936 }
2937
2938
2939 /* Basic concepts [gram.basic] */
2940
2941 /* Parse a translation-unit.
2942
2943 translation-unit:
2944 declaration-seq [opt]
2945
2946 Returns TRUE if all went well. */
2947
2948 static bool
2949 cp_parser_translation_unit (cp_parser* parser)
2950 {
2951 /* The address of the first non-permanent object on the declarator
2952 obstack. */
2953 static void *declarator_obstack_base;
2954
2955 bool success;
2956
2957 /* Create the declarator obstack, if necessary. */
2958 if (!cp_error_declarator)
2959 {
2960 gcc_obstack_init (&declarator_obstack);
2961 /* Create the error declarator. */
2962 cp_error_declarator = make_declarator (cdk_error);
2963 /* Create the empty parameter list. */
2964 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2965 /* Remember where the base of the declarator obstack lies. */
2966 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2967 }
2968
2969 cp_parser_declaration_seq_opt (parser);
2970
2971 /* If there are no tokens left then all went well. */
2972 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2973 {
2974 /* Get rid of the token array; we don't need it any more. */
2975 cp_lexer_destroy (parser->lexer);
2976 parser->lexer = NULL;
2977
2978 /* This file might have been a context that's implicitly extern
2979 "C". If so, pop the lang context. (Only relevant for PCH.) */
2980 if (parser->implicit_extern_c)
2981 {
2982 pop_lang_context ();
2983 parser->implicit_extern_c = false;
2984 }
2985
2986 /* Finish up. */
2987 finish_translation_unit ();
2988
2989 success = true;
2990 }
2991 else
2992 {
2993 cp_parser_error (parser, "expected declaration");
2994 success = false;
2995 }
2996
2997 /* Make sure the declarator obstack was fully cleaned up. */
2998 gcc_assert (obstack_next_free (&declarator_obstack)
2999 == declarator_obstack_base);
3000
3001 /* All went well. */
3002 return success;
3003 }
3004
3005 /* Expressions [gram.expr] */
3006
3007 /* Parse a primary-expression.
3008
3009 primary-expression:
3010 literal
3011 this
3012 ( expression )
3013 id-expression
3014
3015 GNU Extensions:
3016
3017 primary-expression:
3018 ( compound-statement )
3019 __builtin_va_arg ( assignment-expression , type-id )
3020 __builtin_offsetof ( type-id , offsetof-expression )
3021
3022 C++ Extensions:
3023 __has_nothrow_assign ( type-id )
3024 __has_nothrow_constructor ( type-id )
3025 __has_nothrow_copy ( type-id )
3026 __has_trivial_assign ( type-id )
3027 __has_trivial_constructor ( type-id )
3028 __has_trivial_copy ( type-id )
3029 __has_trivial_destructor ( type-id )
3030 __has_virtual_destructor ( type-id )
3031 __is_abstract ( type-id )
3032 __is_base_of ( type-id , type-id )
3033 __is_class ( type-id )
3034 __is_convertible_to ( type-id , type-id )
3035 __is_empty ( type-id )
3036 __is_enum ( type-id )
3037 __is_pod ( type-id )
3038 __is_polymorphic ( type-id )
3039 __is_union ( type-id )
3040
3041 Objective-C++ Extension:
3042
3043 primary-expression:
3044 objc-expression
3045
3046 literal:
3047 __null
3048
3049 ADDRESS_P is true iff this expression was immediately preceded by
3050 "&" and therefore might denote a pointer-to-member. CAST_P is true
3051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3052 true iff this expression is a template argument.
3053
3054 Returns a representation of the expression. Upon return, *IDK
3055 indicates what kind of id-expression (if any) was present. */
3056
3057 static tree
3058 cp_parser_primary_expression (cp_parser *parser,
3059 bool address_p,
3060 bool cast_p,
3061 bool template_arg_p,
3062 cp_id_kind *idk)
3063 {
3064 cp_token *token;
3065
3066 /* Assume the primary expression is not an id-expression. */
3067 *idk = CP_ID_KIND_NONE;
3068
3069 /* Peek at the next token. */
3070 token = cp_lexer_peek_token (parser->lexer);
3071 switch (token->type)
3072 {
3073 /* literal:
3074 integer-literal
3075 character-literal
3076 floating-literal
3077 string-literal
3078 boolean-literal */
3079 case CPP_CHAR:
3080 case CPP_WCHAR:
3081 case CPP_NUMBER:
3082 token = cp_lexer_consume_token (parser->lexer);
3083 /* Floating-point literals are only allowed in an integral
3084 constant expression if they are cast to an integral or
3085 enumeration type. */
3086 if (TREE_CODE (token->u.value) == REAL_CST
3087 && parser->integral_constant_expression_p
3088 && pedantic)
3089 {
3090 /* CAST_P will be set even in invalid code like "int(2.7 +
3091 ...)". Therefore, we have to check that the next token
3092 is sure to end the cast. */
3093 if (cast_p)
3094 {
3095 cp_token *next_token;
3096
3097 next_token = cp_lexer_peek_token (parser->lexer);
3098 if (/* The comma at the end of an
3099 enumerator-definition. */
3100 next_token->type != CPP_COMMA
3101 /* The curly brace at the end of an enum-specifier. */
3102 && next_token->type != CPP_CLOSE_BRACE
3103 /* The end of a statement. */
3104 && next_token->type != CPP_SEMICOLON
3105 /* The end of the cast-expression. */
3106 && next_token->type != CPP_CLOSE_PAREN
3107 /* The end of an array bound. */
3108 && next_token->type != CPP_CLOSE_SQUARE
3109 /* The closing ">" in a template-argument-list. */
3110 && (next_token->type != CPP_GREATER
3111 || parser->greater_than_is_operator_p)
3112 /* C++0x only: A ">>" treated like two ">" tokens,
3113 in a template-argument-list. */
3114 && (next_token->type != CPP_RSHIFT
3115 || (cxx_dialect == cxx98)
3116 || parser->greater_than_is_operator_p))
3117 cast_p = false;
3118 }
3119
3120 /* If we are within a cast, then the constraint that the
3121 cast is to an integral or enumeration type will be
3122 checked at that point. If we are not within a cast, then
3123 this code is invalid. */
3124 if (!cast_p)
3125 cp_parser_non_integral_constant_expression
3126 (parser, "floating-point literal");
3127 }
3128 return token->u.value;
3129
3130 case CPP_STRING:
3131 case CPP_WSTRING:
3132 /* ??? Should wide strings be allowed when parser->translate_strings_p
3133 is false (i.e. in attributes)? If not, we can kill the third
3134 argument to cp_parser_string_literal. */
3135 return cp_parser_string_literal (parser,
3136 parser->translate_strings_p,
3137 true);
3138
3139 case CPP_OPEN_PAREN:
3140 {
3141 tree expr;
3142 bool saved_greater_than_is_operator_p;
3143
3144 /* Consume the `('. */
3145 cp_lexer_consume_token (parser->lexer);
3146 /* Within a parenthesized expression, a `>' token is always
3147 the greater-than operator. */
3148 saved_greater_than_is_operator_p
3149 = parser->greater_than_is_operator_p;
3150 parser->greater_than_is_operator_p = true;
3151 /* If we see `( { ' then we are looking at the beginning of
3152 a GNU statement-expression. */
3153 if (cp_parser_allow_gnu_extensions_p (parser)
3154 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3155 {
3156 /* Statement-expressions are not allowed by the standard. */
3157 if (pedantic)
3158 pedwarn ("ISO C++ forbids braced-groups within expressions");
3159
3160 /* And they're not allowed outside of a function-body; you
3161 cannot, for example, write:
3162
3163 int i = ({ int j = 3; j + 1; });
3164
3165 at class or namespace scope. */
3166 if (!parser->in_function_body
3167 || parser->in_template_argument_list_p)
3168 {
3169 error ("statement-expressions are not allowed outside "
3170 "functions nor in template-argument lists");
3171 cp_parser_skip_to_end_of_block_or_statement (parser);
3172 expr = error_mark_node;
3173 }
3174 else
3175 {
3176 /* Start the statement-expression. */
3177 expr = begin_stmt_expr ();
3178 /* Parse the compound-statement. */
3179 cp_parser_compound_statement (parser, expr, false);
3180 /* Finish up. */
3181 expr = finish_stmt_expr (expr, false);
3182 }
3183 }
3184 else
3185 {
3186 /* Parse the parenthesized expression. */
3187 expr = cp_parser_expression (parser, cast_p);
3188 /* Let the front end know that this expression was
3189 enclosed in parentheses. This matters in case, for
3190 example, the expression is of the form `A::B', since
3191 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3192 not. */
3193 finish_parenthesized_expr (expr);
3194 }
3195 /* The `>' token might be the end of a template-id or
3196 template-parameter-list now. */
3197 parser->greater_than_is_operator_p
3198 = saved_greater_than_is_operator_p;
3199 /* Consume the `)'. */
3200 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3201 cp_parser_skip_to_end_of_statement (parser);
3202
3203 return expr;
3204 }
3205
3206 case CPP_KEYWORD:
3207 switch (token->keyword)
3208 {
3209 /* These two are the boolean literals. */
3210 case RID_TRUE:
3211 cp_lexer_consume_token (parser->lexer);
3212 return boolean_true_node;
3213 case RID_FALSE:
3214 cp_lexer_consume_token (parser->lexer);
3215 return boolean_false_node;
3216
3217 /* The `__null' literal. */
3218 case RID_NULL:
3219 cp_lexer_consume_token (parser->lexer);
3220 return null_node;
3221
3222 /* Recognize the `this' keyword. */
3223 case RID_THIS:
3224 cp_lexer_consume_token (parser->lexer);
3225 if (parser->local_variables_forbidden_p)
3226 {
3227 error ("%<this%> may not be used in this context");
3228 return error_mark_node;
3229 }
3230 /* Pointers cannot appear in constant-expressions. */
3231 if (cp_parser_non_integral_constant_expression (parser,
3232 "`this'"))
3233 return error_mark_node;
3234 return finish_this_expr ();
3235
3236 /* The `operator' keyword can be the beginning of an
3237 id-expression. */
3238 case RID_OPERATOR:
3239 goto id_expression;
3240
3241 case RID_FUNCTION_NAME:
3242 case RID_PRETTY_FUNCTION_NAME:
3243 case RID_C99_FUNCTION_NAME:
3244 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3245 __func__ are the names of variables -- but they are
3246 treated specially. Therefore, they are handled here,
3247 rather than relying on the generic id-expression logic
3248 below. Grammatically, these names are id-expressions.
3249
3250 Consume the token. */
3251 token = cp_lexer_consume_token (parser->lexer);
3252 /* Look up the name. */
3253 return finish_fname (token->u.value);
3254
3255 case RID_VA_ARG:
3256 {
3257 tree expression;
3258 tree type;
3259
3260 /* The `__builtin_va_arg' construct is used to handle
3261 `va_arg'. Consume the `__builtin_va_arg' token. */
3262 cp_lexer_consume_token (parser->lexer);
3263 /* Look for the opening `('. */
3264 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3265 /* Now, parse the assignment-expression. */
3266 expression = cp_parser_assignment_expression (parser,
3267 /*cast_p=*/false);
3268 /* Look for the `,'. */
3269 cp_parser_require (parser, CPP_COMMA, "`,'");
3270 /* Parse the type-id. */
3271 type = cp_parser_type_id (parser);
3272 /* Look for the closing `)'. */
3273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3274 /* Using `va_arg' in a constant-expression is not
3275 allowed. */
3276 if (cp_parser_non_integral_constant_expression (parser,
3277 "`va_arg'"))
3278 return error_mark_node;
3279 return build_x_va_arg (expression, type);
3280 }
3281
3282 case RID_OFFSETOF:
3283 return cp_parser_builtin_offsetof (parser);
3284
3285 case RID_HAS_NOTHROW_ASSIGN:
3286 case RID_HAS_NOTHROW_CONSTRUCTOR:
3287 case RID_HAS_NOTHROW_COPY:
3288 case RID_HAS_TRIVIAL_ASSIGN:
3289 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3290 case RID_HAS_TRIVIAL_COPY:
3291 case RID_HAS_TRIVIAL_DESTRUCTOR:
3292 case RID_HAS_VIRTUAL_DESTRUCTOR:
3293 case RID_IS_ABSTRACT:
3294 case RID_IS_BASE_OF:
3295 case RID_IS_CLASS:
3296 case RID_IS_CONVERTIBLE_TO:
3297 case RID_IS_EMPTY:
3298 case RID_IS_ENUM:
3299 case RID_IS_POD:
3300 case RID_IS_POLYMORPHIC:
3301 case RID_IS_UNION:
3302 return cp_parser_trait_expr (parser, token->keyword);
3303
3304 /* Objective-C++ expressions. */
3305 case RID_AT_ENCODE:
3306 case RID_AT_PROTOCOL:
3307 case RID_AT_SELECTOR:
3308 return cp_parser_objc_expression (parser);
3309
3310 default:
3311 cp_parser_error (parser, "expected primary-expression");
3312 return error_mark_node;
3313 }
3314
3315 /* An id-expression can start with either an identifier, a
3316 `::' as the beginning of a qualified-id, or the "operator"
3317 keyword. */
3318 case CPP_NAME:
3319 case CPP_SCOPE:
3320 case CPP_TEMPLATE_ID:
3321 case CPP_NESTED_NAME_SPECIFIER:
3322 {
3323 tree id_expression;
3324 tree decl;
3325 const char *error_msg;
3326 bool template_p;
3327 bool done;
3328
3329 id_expression:
3330 /* Parse the id-expression. */
3331 id_expression
3332 = cp_parser_id_expression (parser,
3333 /*template_keyword_p=*/false,
3334 /*check_dependency_p=*/true,
3335 &template_p,
3336 /*declarator_p=*/false,
3337 /*optional_p=*/false);
3338 if (id_expression == error_mark_node)
3339 return error_mark_node;
3340 token = cp_lexer_peek_token (parser->lexer);
3341 done = (token->type != CPP_OPEN_SQUARE
3342 && token->type != CPP_OPEN_PAREN
3343 && token->type != CPP_DOT
3344 && token->type != CPP_DEREF
3345 && token->type != CPP_PLUS_PLUS
3346 && token->type != CPP_MINUS_MINUS);
3347 /* If we have a template-id, then no further lookup is
3348 required. If the template-id was for a template-class, we
3349 will sometimes have a TYPE_DECL at this point. */
3350 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3351 || TREE_CODE (id_expression) == TYPE_DECL)
3352 decl = id_expression;
3353 /* Look up the name. */
3354 else
3355 {
3356 tree ambiguous_decls;
3357
3358 decl = cp_parser_lookup_name (parser, id_expression,
3359 none_type,
3360 template_p,
3361 /*is_namespace=*/false,
3362 /*check_dependency=*/true,
3363 &ambiguous_decls);
3364 /* If the lookup was ambiguous, an error will already have
3365 been issued. */
3366 if (ambiguous_decls)
3367 return error_mark_node;
3368
3369 /* In Objective-C++, an instance variable (ivar) may be preferred
3370 to whatever cp_parser_lookup_name() found. */
3371 decl = objc_lookup_ivar (decl, id_expression);
3372
3373 /* If name lookup gives us a SCOPE_REF, then the
3374 qualifying scope was dependent. */
3375 if (TREE_CODE (decl) == SCOPE_REF)
3376 {
3377 /* At this point, we do not know if DECL is a valid
3378 integral constant expression. We assume that it is
3379 in fact such an expression, so that code like:
3380
3381 template <int N> struct A {
3382 int a[B<N>::i];
3383 };
3384
3385 is accepted. At template-instantiation time, we
3386 will check that B<N>::i is actually a constant. */
3387 return decl;
3388 }
3389 /* Check to see if DECL is a local variable in a context
3390 where that is forbidden. */
3391 if (parser->local_variables_forbidden_p
3392 && local_variable_p (decl))
3393 {
3394 /* It might be that we only found DECL because we are
3395 trying to be generous with pre-ISO scoping rules.
3396 For example, consider:
3397
3398 int i;
3399 void g() {
3400 for (int i = 0; i < 10; ++i) {}
3401 extern void f(int j = i);
3402 }
3403
3404 Here, name look up will originally find the out
3405 of scope `i'. We need to issue a warning message,
3406 but then use the global `i'. */
3407 decl = check_for_out_of_scope_variable (decl);
3408 if (local_variable_p (decl))
3409 {
3410 error ("local variable %qD may not appear in this context",
3411 decl);
3412 return error_mark_node;
3413 }
3414 }
3415 }
3416
3417 decl = (finish_id_expression
3418 (id_expression, decl, parser->scope,
3419 idk,
3420 parser->integral_constant_expression_p,
3421 parser->allow_non_integral_constant_expression_p,
3422 &parser->non_integral_constant_expression_p,
3423 template_p, done, address_p,
3424 template_arg_p,
3425 &error_msg));
3426 if (error_msg)
3427 cp_parser_error (parser, error_msg);
3428 return decl;
3429 }
3430
3431 /* Anything else is an error. */
3432 default:
3433 /* ...unless we have an Objective-C++ message or string literal,
3434 that is. */
3435 if (c_dialect_objc ()
3436 && (token->type == CPP_OPEN_SQUARE
3437 || token->type == CPP_OBJC_STRING))
3438 return cp_parser_objc_expression (parser);
3439
3440 cp_parser_error (parser, "expected primary-expression");
3441 return error_mark_node;
3442 }
3443 }
3444
3445 /* Parse an id-expression.
3446
3447 id-expression:
3448 unqualified-id
3449 qualified-id
3450
3451 qualified-id:
3452 :: [opt] nested-name-specifier template [opt] unqualified-id
3453 :: identifier
3454 :: operator-function-id
3455 :: template-id
3456
3457 Return a representation of the unqualified portion of the
3458 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3459 a `::' or nested-name-specifier.
3460
3461 Often, if the id-expression was a qualified-id, the caller will
3462 want to make a SCOPE_REF to represent the qualified-id. This
3463 function does not do this in order to avoid wastefully creating
3464 SCOPE_REFs when they are not required.
3465
3466 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3467 `template' keyword.
3468
3469 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3470 uninstantiated templates.
3471
3472 If *TEMPLATE_P is non-NULL, it is set to true iff the
3473 `template' keyword is used to explicitly indicate that the entity
3474 named is a template.
3475
3476 If DECLARATOR_P is true, the id-expression is appearing as part of
3477 a declarator, rather than as part of an expression. */
3478
3479 static tree
3480 cp_parser_id_expression (cp_parser *parser,
3481 bool template_keyword_p,
3482 bool check_dependency_p,
3483 bool *template_p,
3484 bool declarator_p,
3485 bool optional_p)
3486 {
3487 bool global_scope_p;
3488 bool nested_name_specifier_p;
3489
3490 /* Assume the `template' keyword was not used. */
3491 if (template_p)
3492 *template_p = template_keyword_p;
3493
3494 /* Look for the optional `::' operator. */
3495 global_scope_p
3496 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3497 != NULL_TREE);
3498 /* Look for the optional nested-name-specifier. */
3499 nested_name_specifier_p
3500 = (cp_parser_nested_name_specifier_opt (parser,
3501 /*typename_keyword_p=*/false,
3502 check_dependency_p,
3503 /*type_p=*/false,
3504 declarator_p)
3505 != NULL_TREE);
3506 /* If there is a nested-name-specifier, then we are looking at
3507 the first qualified-id production. */
3508 if (nested_name_specifier_p)
3509 {
3510 tree saved_scope;
3511 tree saved_object_scope;
3512 tree saved_qualifying_scope;
3513 tree unqualified_id;
3514 bool is_template;
3515
3516 /* See if the next token is the `template' keyword. */
3517 if (!template_p)
3518 template_p = &is_template;
3519 *template_p = cp_parser_optional_template_keyword (parser);
3520 /* Name lookup we do during the processing of the
3521 unqualified-id might obliterate SCOPE. */
3522 saved_scope = parser->scope;
3523 saved_object_scope = parser->object_scope;
3524 saved_qualifying_scope = parser->qualifying_scope;
3525 /* Process the final unqualified-id. */
3526 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3527 check_dependency_p,
3528 declarator_p,
3529 /*optional_p=*/false);
3530 /* Restore the SAVED_SCOPE for our caller. */
3531 parser->scope = saved_scope;
3532 parser->object_scope = saved_object_scope;
3533 parser->qualifying_scope = saved_qualifying_scope;
3534
3535 return unqualified_id;
3536 }
3537 /* Otherwise, if we are in global scope, then we are looking at one
3538 of the other qualified-id productions. */
3539 else if (global_scope_p)
3540 {
3541 cp_token *token;
3542 tree id;
3543
3544 /* Peek at the next token. */
3545 token = cp_lexer_peek_token (parser->lexer);
3546
3547 /* If it's an identifier, and the next token is not a "<", then
3548 we can avoid the template-id case. This is an optimization
3549 for this common case. */
3550 if (token->type == CPP_NAME
3551 && !cp_parser_nth_token_starts_template_argument_list_p
3552 (parser, 2))
3553 return cp_parser_identifier (parser);
3554
3555 cp_parser_parse_tentatively (parser);
3556 /* Try a template-id. */
3557 id = cp_parser_template_id (parser,
3558 /*template_keyword_p=*/false,
3559 /*check_dependency_p=*/true,
3560 declarator_p);
3561 /* If that worked, we're done. */
3562 if (cp_parser_parse_definitely (parser))
3563 return id;
3564
3565 /* Peek at the next token. (Changes in the token buffer may
3566 have invalidated the pointer obtained above.) */
3567 token = cp_lexer_peek_token (parser->lexer);
3568
3569 switch (token->type)
3570 {
3571 case CPP_NAME:
3572 return cp_parser_identifier (parser);
3573
3574 case CPP_KEYWORD:
3575 if (token->keyword == RID_OPERATOR)
3576 return cp_parser_operator_function_id (parser);
3577 /* Fall through. */
3578
3579 default:
3580 cp_parser_error (parser, "expected id-expression");
3581 return error_mark_node;
3582 }
3583 }
3584 else
3585 return cp_parser_unqualified_id (parser, template_keyword_p,
3586 /*check_dependency_p=*/true,
3587 declarator_p,
3588 optional_p);
3589 }
3590
3591 /* Parse an unqualified-id.
3592
3593 unqualified-id:
3594 identifier
3595 operator-function-id
3596 conversion-function-id
3597 ~ class-name
3598 template-id
3599
3600 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3601 keyword, in a construct like `A::template ...'.
3602
3603 Returns a representation of unqualified-id. For the `identifier'
3604 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3605 production a BIT_NOT_EXPR is returned; the operand of the
3606 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3607 other productions, see the documentation accompanying the
3608 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3609 names are looked up in uninstantiated templates. If DECLARATOR_P
3610 is true, the unqualified-id is appearing as part of a declarator,
3611 rather than as part of an expression. */
3612
3613 static tree
3614 cp_parser_unqualified_id (cp_parser* parser,
3615 bool template_keyword_p,
3616 bool check_dependency_p,
3617 bool declarator_p,
3618 bool optional_p)
3619 {
3620 cp_token *token;
3621
3622 /* Peek at the next token. */
3623 token = cp_lexer_peek_token (parser->lexer);
3624
3625 switch (token->type)
3626 {
3627 case CPP_NAME:
3628 {
3629 tree id;
3630
3631 /* We don't know yet whether or not this will be a
3632 template-id. */
3633 cp_parser_parse_tentatively (parser);
3634 /* Try a template-id. */
3635 id = cp_parser_template_id (parser, template_keyword_p,
3636 check_dependency_p,
3637 declarator_p);
3638 /* If it worked, we're done. */
3639 if (cp_parser_parse_definitely (parser))
3640 return id;
3641 /* Otherwise, it's an ordinary identifier. */
3642 return cp_parser_identifier (parser);
3643 }
3644
3645 case CPP_TEMPLATE_ID:
3646 return cp_parser_template_id (parser, template_keyword_p,
3647 check_dependency_p,
3648 declarator_p);
3649
3650 case CPP_COMPL:
3651 {
3652 tree type_decl;
3653 tree qualifying_scope;
3654 tree object_scope;
3655 tree scope;
3656 bool done;
3657
3658 /* Consume the `~' token. */
3659 cp_lexer_consume_token (parser->lexer);
3660 /* Parse the class-name. The standard, as written, seems to
3661 say that:
3662
3663 template <typename T> struct S { ~S (); };
3664 template <typename T> S<T>::~S() {}
3665
3666 is invalid, since `~' must be followed by a class-name, but
3667 `S<T>' is dependent, and so not known to be a class.
3668 That's not right; we need to look in uninstantiated
3669 templates. A further complication arises from:
3670
3671 template <typename T> void f(T t) {
3672 t.T::~T();
3673 }
3674
3675 Here, it is not possible to look up `T' in the scope of `T'
3676 itself. We must look in both the current scope, and the
3677 scope of the containing complete expression.
3678
3679 Yet another issue is:
3680
3681 struct S {
3682 int S;
3683 ~S();
3684 };
3685
3686 S::~S() {}
3687
3688 The standard does not seem to say that the `S' in `~S'
3689 should refer to the type `S' and not the data member
3690 `S::S'. */
3691
3692 /* DR 244 says that we look up the name after the "~" in the
3693 same scope as we looked up the qualifying name. That idea
3694 isn't fully worked out; it's more complicated than that. */
3695 scope = parser->scope;
3696 object_scope = parser->object_scope;
3697 qualifying_scope = parser->qualifying_scope;
3698
3699 /* Check for invalid scopes. */
3700 if (scope == error_mark_node)
3701 {
3702 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3703 cp_lexer_consume_token (parser->lexer);
3704 return error_mark_node;
3705 }
3706 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3707 {
3708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3709 error ("scope %qT before %<~%> is not a class-name", scope);
3710 cp_parser_simulate_error (parser);
3711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3712 cp_lexer_consume_token (parser->lexer);
3713 return error_mark_node;
3714 }
3715 gcc_assert (!scope || TYPE_P (scope));
3716
3717 /* If the name is of the form "X::~X" it's OK. */
3718 token = cp_lexer_peek_token (parser->lexer);
3719 if (scope
3720 && token->type == CPP_NAME
3721 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3722 == CPP_OPEN_PAREN)
3723 && constructor_name_p (token->u.value, scope))
3724 {
3725 cp_lexer_consume_token (parser->lexer);
3726 return build_nt (BIT_NOT_EXPR, scope);
3727 }
3728
3729 /* If there was an explicit qualification (S::~T), first look
3730 in the scope given by the qualification (i.e., S). */
3731 done = false;
3732 type_decl = NULL_TREE;
3733 if (scope)
3734 {
3735 cp_parser_parse_tentatively (parser);
3736 type_decl = cp_parser_class_name (parser,
3737 /*typename_keyword_p=*/false,
3738 /*template_keyword_p=*/false,
3739 none_type,
3740 /*check_dependency=*/false,
3741 /*class_head_p=*/false,
3742 declarator_p);
3743 if (cp_parser_parse_definitely (parser))
3744 done = true;
3745 }
3746 /* In "N::S::~S", look in "N" as well. */
3747 if (!done && scope && qualifying_scope)
3748 {
3749 cp_parser_parse_tentatively (parser);
3750 parser->scope = qualifying_scope;
3751 parser->object_scope = NULL_TREE;
3752 parser->qualifying_scope = NULL_TREE;
3753 type_decl
3754 = cp_parser_class_name (parser,
3755 /*typename_keyword_p=*/false,
3756 /*template_keyword_p=*/false,
3757 none_type,
3758 /*check_dependency=*/false,
3759 /*class_head_p=*/false,
3760 declarator_p);
3761 if (cp_parser_parse_definitely (parser))
3762 done = true;
3763 }
3764 /* In "p->S::~T", look in the scope given by "*p" as well. */
3765 else if (!done && object_scope)
3766 {
3767 cp_parser_parse_tentatively (parser);
3768 parser->scope = object_scope;
3769 parser->object_scope = NULL_TREE;
3770 parser->qualifying_scope = NULL_TREE;
3771 type_decl
3772 = cp_parser_class_name (parser,
3773 /*typename_keyword_p=*/false,
3774 /*template_keyword_p=*/false,
3775 none_type,
3776 /*check_dependency=*/false,
3777 /*class_head_p=*/false,
3778 declarator_p);
3779 if (cp_parser_parse_definitely (parser))
3780 done = true;
3781 }
3782 /* Look in the surrounding context. */
3783 if (!done)
3784 {
3785 parser->scope = NULL_TREE;
3786 parser->object_scope = NULL_TREE;
3787 parser->qualifying_scope = NULL_TREE;
3788 type_decl
3789 = cp_parser_class_name (parser,
3790 /*typename_keyword_p=*/false,
3791 /*template_keyword_p=*/false,
3792 none_type,
3793 /*check_dependency=*/false,
3794 /*class_head_p=*/false,
3795 declarator_p);
3796 }
3797 /* If an error occurred, assume that the name of the
3798 destructor is the same as the name of the qualifying
3799 class. That allows us to keep parsing after running
3800 into ill-formed destructor names. */
3801 if (type_decl == error_mark_node && scope)
3802 return build_nt (BIT_NOT_EXPR, scope);
3803 else if (type_decl == error_mark_node)
3804 return error_mark_node;
3805
3806 /* Check that destructor name and scope match. */
3807 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3808 {
3809 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3810 error ("declaration of %<~%T%> as member of %qT",
3811 type_decl, scope);
3812 cp_parser_simulate_error (parser);
3813 return error_mark_node;
3814 }
3815
3816 /* [class.dtor]
3817
3818 A typedef-name that names a class shall not be used as the
3819 identifier in the declarator for a destructor declaration. */
3820 if (declarator_p
3821 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3822 && !DECL_SELF_REFERENCE_P (type_decl)
3823 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3824 error ("typedef-name %qD used as destructor declarator",
3825 type_decl);
3826
3827 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3828 }
3829
3830 case CPP_KEYWORD:
3831 if (token->keyword == RID_OPERATOR)
3832 {
3833 tree id;
3834
3835 /* This could be a template-id, so we try that first. */
3836 cp_parser_parse_tentatively (parser);
3837 /* Try a template-id. */
3838 id = cp_parser_template_id (parser, template_keyword_p,
3839 /*check_dependency_p=*/true,
3840 declarator_p);
3841 /* If that worked, we're done. */
3842 if (cp_parser_parse_definitely (parser))
3843 return id;
3844 /* We still don't know whether we're looking at an
3845 operator-function-id or a conversion-function-id. */
3846 cp_parser_parse_tentatively (parser);
3847 /* Try an operator-function-id. */
3848 id = cp_parser_operator_function_id (parser);
3849 /* If that didn't work, try a conversion-function-id. */
3850 if (!cp_parser_parse_definitely (parser))
3851 id = cp_parser_conversion_function_id (parser);
3852
3853 return id;
3854 }
3855 /* Fall through. */
3856
3857 default:
3858 if (optional_p)
3859 return NULL_TREE;
3860 cp_parser_error (parser, "expected unqualified-id");
3861 return error_mark_node;
3862 }
3863 }
3864
3865 /* Parse an (optional) nested-name-specifier.
3866
3867 nested-name-specifier:
3868 class-or-namespace-name :: nested-name-specifier [opt]
3869 class-or-namespace-name :: template nested-name-specifier [opt]
3870
3871 PARSER->SCOPE should be set appropriately before this function is
3872 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3873 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3874 in name lookups.
3875
3876 Sets PARSER->SCOPE to the class (TYPE) or namespace
3877 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3878 it unchanged if there is no nested-name-specifier. Returns the new
3879 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3880
3881 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3882 part of a declaration and/or decl-specifier. */
3883
3884 static tree
3885 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3886 bool typename_keyword_p,
3887 bool check_dependency_p,
3888 bool type_p,
3889 bool is_declaration)
3890 {
3891 bool success = false;
3892 cp_token_position start = 0;
3893 cp_token *token;
3894
3895 /* Remember where the nested-name-specifier starts. */
3896 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3897 {
3898 start = cp_lexer_token_position (parser->lexer, false);
3899 push_deferring_access_checks (dk_deferred);
3900 }
3901
3902 while (true)
3903 {
3904 tree new_scope;
3905 tree old_scope;
3906 tree saved_qualifying_scope;
3907 bool template_keyword_p;
3908
3909 /* Spot cases that cannot be the beginning of a
3910 nested-name-specifier. */
3911 token = cp_lexer_peek_token (parser->lexer);
3912
3913 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3914 the already parsed nested-name-specifier. */
3915 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3916 {
3917 /* Grab the nested-name-specifier and continue the loop. */
3918 cp_parser_pre_parsed_nested_name_specifier (parser);
3919 /* If we originally encountered this nested-name-specifier
3920 with IS_DECLARATION set to false, we will not have
3921 resolved TYPENAME_TYPEs, so we must do so here. */
3922 if (is_declaration
3923 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3924 {
3925 new_scope = resolve_typename_type (parser->scope,
3926 /*only_current_p=*/false);
3927 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3928 parser->scope = new_scope;
3929 }
3930 success = true;
3931 continue;
3932 }
3933
3934 /* Spot cases that cannot be the beginning of a
3935 nested-name-specifier. On the second and subsequent times
3936 through the loop, we look for the `template' keyword. */
3937 if (success && token->keyword == RID_TEMPLATE)
3938 ;
3939 /* A template-id can start a nested-name-specifier. */
3940 else if (token->type == CPP_TEMPLATE_ID)
3941 ;
3942 else
3943 {
3944 /* If the next token is not an identifier, then it is
3945 definitely not a class-or-namespace-name. */
3946 if (token->type != CPP_NAME)
3947 break;
3948 /* If the following token is neither a `<' (to begin a
3949 template-id), nor a `::', then we are not looking at a
3950 nested-name-specifier. */
3951 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3952 if (token->type != CPP_SCOPE
3953 && !cp_parser_nth_token_starts_template_argument_list_p
3954 (parser, 2))
3955 break;
3956 }
3957
3958 /* The nested-name-specifier is optional, so we parse
3959 tentatively. */
3960 cp_parser_parse_tentatively (parser);
3961
3962 /* Look for the optional `template' keyword, if this isn't the
3963 first time through the loop. */
3964 if (success)
3965 template_keyword_p = cp_parser_optional_template_keyword (parser);
3966 else
3967 template_keyword_p = false;
3968
3969 /* Save the old scope since the name lookup we are about to do
3970 might destroy it. */
3971 old_scope = parser->scope;
3972 saved_qualifying_scope = parser->qualifying_scope;
3973 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3974 look up names in "X<T>::I" in order to determine that "Y" is
3975 a template. So, if we have a typename at this point, we make
3976 an effort to look through it. */
3977 if (is_declaration
3978 && !typename_keyword_p
3979 && parser->scope
3980 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3981 parser->scope = resolve_typename_type (parser->scope,
3982 /*only_current_p=*/false);
3983 /* Parse the qualifying entity. */
3984 new_scope
3985 = cp_parser_class_or_namespace_name (parser,
3986 typename_keyword_p,
3987 template_keyword_p,
3988 check_dependency_p,
3989 type_p,
3990 is_declaration);
3991 /* Look for the `::' token. */
3992 cp_parser_require (parser, CPP_SCOPE, "`::'");
3993
3994 /* If we found what we wanted, we keep going; otherwise, we're
3995 done. */
3996 if (!cp_parser_parse_definitely (parser))
3997 {
3998 bool error_p = false;
3999
4000 /* Restore the OLD_SCOPE since it was valid before the
4001 failed attempt at finding the last
4002 class-or-namespace-name. */
4003 parser->scope = old_scope;
4004 parser->qualifying_scope = saved_qualifying_scope;
4005 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4006 break;
4007 /* If the next token is an identifier, and the one after
4008 that is a `::', then any valid interpretation would have
4009 found a class-or-namespace-name. */
4010 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4011 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4012 == CPP_SCOPE)
4013 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4014 != CPP_COMPL))
4015 {
4016 token = cp_lexer_consume_token (parser->lexer);
4017 if (!error_p)
4018 {
4019 if (!token->ambiguous_p)
4020 {
4021 tree decl;
4022 tree ambiguous_decls;
4023
4024 decl = cp_parser_lookup_name (parser, token->u.value,
4025 none_type,
4026 /*is_template=*/false,
4027 /*is_namespace=*/false,
4028 /*check_dependency=*/true,
4029 &ambiguous_decls);
4030 if (TREE_CODE (decl) == TEMPLATE_DECL)
4031 error ("%qD used without template parameters", decl);
4032 else if (ambiguous_decls)
4033 {
4034 error ("reference to %qD is ambiguous",
4035 token->u.value);
4036 print_candidates (ambiguous_decls);
4037 decl = error_mark_node;
4038 }
4039 else
4040 cp_parser_name_lookup_error
4041 (parser, token->u.value, decl,
4042 "is not a class or namespace");
4043 }
4044 parser->scope = error_mark_node;
4045 error_p = true;
4046 /* Treat this as a successful nested-name-specifier
4047 due to:
4048
4049 [basic.lookup.qual]
4050
4051 If the name found is not a class-name (clause
4052 _class_) or namespace-name (_namespace.def_), the
4053 program is ill-formed. */
4054 success = true;
4055 }
4056 cp_lexer_consume_token (parser->lexer);
4057 }
4058 break;
4059 }
4060 /* We've found one valid nested-name-specifier. */
4061 success = true;
4062 /* Name lookup always gives us a DECL. */
4063 if (TREE_CODE (new_scope) == TYPE_DECL)
4064 new_scope = TREE_TYPE (new_scope);
4065 /* Uses of "template" must be followed by actual templates. */
4066 if (template_keyword_p
4067 && !(CLASS_TYPE_P (new_scope)
4068 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4069 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4070 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4071 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4072 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4073 == TEMPLATE_ID_EXPR)))
4074 pedwarn (TYPE_P (new_scope)
4075 ? "%qT is not a template"
4076 : "%qD is not a template",
4077 new_scope);
4078 /* If it is a class scope, try to complete it; we are about to
4079 be looking up names inside the class. */
4080 if (TYPE_P (new_scope)
4081 /* Since checking types for dependency can be expensive,
4082 avoid doing it if the type is already complete. */
4083 && !COMPLETE_TYPE_P (new_scope)
4084 /* Do not try to complete dependent types. */
4085 && !dependent_type_p (new_scope))
4086 {
4087 new_scope = complete_type (new_scope);
4088 /* If it is a typedef to current class, use the current
4089 class instead, as the typedef won't have any names inside
4090 it yet. */
4091 if (!COMPLETE_TYPE_P (new_scope)
4092 && currently_open_class (new_scope))
4093 new_scope = TYPE_MAIN_VARIANT (new_scope);
4094 }
4095 /* Make sure we look in the right scope the next time through
4096 the loop. */
4097 parser->scope = new_scope;
4098 }
4099
4100 /* If parsing tentatively, replace the sequence of tokens that makes
4101 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4102 token. That way, should we re-parse the token stream, we will
4103 not have to repeat the effort required to do the parse, nor will
4104 we issue duplicate error messages. */
4105 if (success && start)
4106 {
4107 cp_token *token;
4108
4109 token = cp_lexer_token_at (parser->lexer, start);
4110 /* Reset the contents of the START token. */
4111 token->type = CPP_NESTED_NAME_SPECIFIER;
4112 /* Retrieve any deferred checks. Do not pop this access checks yet
4113 so the memory will not be reclaimed during token replacing below. */
4114 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4115 token->u.tree_check_value->value = parser->scope;
4116 token->u.tree_check_value->checks = get_deferred_access_checks ();
4117 token->u.tree_check_value->qualifying_scope =
4118 parser->qualifying_scope;
4119 token->keyword = RID_MAX;
4120
4121 /* Purge all subsequent tokens. */
4122 cp_lexer_purge_tokens_after (parser->lexer, start);
4123 }
4124
4125 if (start)
4126 pop_to_parent_deferring_access_checks ();
4127
4128 return success ? parser->scope : NULL_TREE;
4129 }
4130
4131 /* Parse a nested-name-specifier. See
4132 cp_parser_nested_name_specifier_opt for details. This function
4133 behaves identically, except that it will an issue an error if no
4134 nested-name-specifier is present. */
4135
4136 static tree
4137 cp_parser_nested_name_specifier (cp_parser *parser,
4138 bool typename_keyword_p,
4139 bool check_dependency_p,
4140 bool type_p,
4141 bool is_declaration)
4142 {
4143 tree scope;
4144
4145 /* Look for the nested-name-specifier. */
4146 scope = cp_parser_nested_name_specifier_opt (parser,
4147 typename_keyword_p,
4148 check_dependency_p,
4149 type_p,
4150 is_declaration);
4151 /* If it was not present, issue an error message. */
4152 if (!scope)
4153 {
4154 cp_parser_error (parser, "expected nested-name-specifier");
4155 parser->scope = NULL_TREE;
4156 }
4157
4158 return scope;
4159 }
4160
4161 /* Parse a class-or-namespace-name.
4162
4163 class-or-namespace-name:
4164 class-name
4165 namespace-name
4166
4167 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4168 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4169 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4170 TYPE_P is TRUE iff the next name should be taken as a class-name,
4171 even the same name is declared to be another entity in the same
4172 scope.
4173
4174 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4175 specified by the class-or-namespace-name. If neither is found the
4176 ERROR_MARK_NODE is returned. */
4177
4178 static tree
4179 cp_parser_class_or_namespace_name (cp_parser *parser,
4180 bool typename_keyword_p,
4181 bool template_keyword_p,
4182 bool check_dependency_p,
4183 bool type_p,
4184 bool is_declaration)
4185 {
4186 tree saved_scope;
4187 tree saved_qualifying_scope;
4188 tree saved_object_scope;
4189 tree scope;
4190 bool only_class_p;
4191
4192 /* Before we try to parse the class-name, we must save away the
4193 current PARSER->SCOPE since cp_parser_class_name will destroy
4194 it. */
4195 saved_scope = parser->scope;
4196 saved_qualifying_scope = parser->qualifying_scope;
4197 saved_object_scope = parser->object_scope;
4198 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4199 there is no need to look for a namespace-name. */
4200 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4201 if (!only_class_p)
4202 cp_parser_parse_tentatively (parser);
4203 scope = cp_parser_class_name (parser,
4204 typename_keyword_p,
4205 template_keyword_p,
4206 type_p ? class_type : none_type,
4207 check_dependency_p,
4208 /*class_head_p=*/false,
4209 is_declaration);
4210 /* If that didn't work, try for a namespace-name. */
4211 if (!only_class_p && !cp_parser_parse_definitely (parser))
4212 {
4213 /* Restore the saved scope. */
4214 parser->scope = saved_scope;
4215 parser->qualifying_scope = saved_qualifying_scope;
4216 parser->object_scope = saved_object_scope;
4217 /* If we are not looking at an identifier followed by the scope
4218 resolution operator, then this is not part of a
4219 nested-name-specifier. (Note that this function is only used
4220 to parse the components of a nested-name-specifier.) */
4221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4222 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4223 return error_mark_node;
4224 scope = cp_parser_namespace_name (parser);
4225 }
4226
4227 return scope;
4228 }
4229
4230 /* Parse a postfix-expression.
4231
4232 postfix-expression:
4233 primary-expression
4234 postfix-expression [ expression ]
4235 postfix-expression ( expression-list [opt] )
4236 simple-type-specifier ( expression-list [opt] )
4237 typename :: [opt] nested-name-specifier identifier
4238 ( expression-list [opt] )
4239 typename :: [opt] nested-name-specifier template [opt] template-id
4240 ( expression-list [opt] )
4241 postfix-expression . template [opt] id-expression
4242 postfix-expression -> template [opt] id-expression
4243 postfix-expression . pseudo-destructor-name
4244 postfix-expression -> pseudo-destructor-name
4245 postfix-expression ++
4246 postfix-expression --
4247 dynamic_cast < type-id > ( expression )
4248 static_cast < type-id > ( expression )
4249 reinterpret_cast < type-id > ( expression )
4250 const_cast < type-id > ( expression )
4251 typeid ( expression )
4252 typeid ( type-id )
4253
4254 GNU Extension:
4255
4256 postfix-expression:
4257 ( type-id ) { initializer-list , [opt] }
4258
4259 This extension is a GNU version of the C99 compound-literal
4260 construct. (The C99 grammar uses `type-name' instead of `type-id',
4261 but they are essentially the same concept.)
4262
4263 If ADDRESS_P is true, the postfix expression is the operand of the
4264 `&' operator. CAST_P is true if this expression is the target of a
4265 cast.
4266
4267 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4268 class member access expressions [expr.ref].
4269
4270 Returns a representation of the expression. */
4271
4272 static tree
4273 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4274 bool member_access_only_p)
4275 {
4276 cp_token *token;
4277 enum rid keyword;
4278 cp_id_kind idk = CP_ID_KIND_NONE;
4279 tree postfix_expression = NULL_TREE;
4280 bool is_member_access = false;
4281
4282 /* Peek at the next token. */
4283 token = cp_lexer_peek_token (parser->lexer);
4284 /* Some of the productions are determined by keywords. */
4285 keyword = token->keyword;
4286 switch (keyword)
4287 {
4288 case RID_DYNCAST:
4289 case RID_STATCAST:
4290 case RID_REINTCAST:
4291 case RID_CONSTCAST:
4292 {
4293 tree type;
4294 tree expression;
4295 const char *saved_message;
4296
4297 /* All of these can be handled in the same way from the point
4298 of view of parsing. Begin by consuming the token
4299 identifying the cast. */
4300 cp_lexer_consume_token (parser->lexer);
4301
4302 /* New types cannot be defined in the cast. */
4303 saved_message = parser->type_definition_forbidden_message;
4304 parser->type_definition_forbidden_message
4305 = "types may not be defined in casts";
4306
4307 /* Look for the opening `<'. */
4308 cp_parser_require (parser, CPP_LESS, "`<'");
4309 /* Parse the type to which we are casting. */
4310 type = cp_parser_type_id (parser);
4311 /* Look for the closing `>'. */
4312 cp_parser_require (parser, CPP_GREATER, "`>'");
4313 /* Restore the old message. */
4314 parser->type_definition_forbidden_message = saved_message;
4315
4316 /* And the expression which is being cast. */
4317 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4318 expression = cp_parser_expression (parser, /*cast_p=*/true);
4319 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4320
4321 /* Only type conversions to integral or enumeration types
4322 can be used in constant-expressions. */
4323 if (!cast_valid_in_integral_constant_expression_p (type)
4324 && (cp_parser_non_integral_constant_expression
4325 (parser,
4326 "a cast to a type other than an integral or "
4327 "enumeration type")))
4328 return error_mark_node;
4329
4330 switch (keyword)
4331 {
4332 case RID_DYNCAST:
4333 postfix_expression
4334 = build_dynamic_cast (type, expression);
4335 break;
4336 case RID_STATCAST:
4337 postfix_expression
4338 = build_static_cast (type, expression);
4339 break;
4340 case RID_REINTCAST:
4341 postfix_expression
4342 = build_reinterpret_cast (type, expression);
4343 break;
4344 case RID_CONSTCAST:
4345 postfix_expression
4346 = build_const_cast (type, expression);
4347 break;
4348 default:
4349 gcc_unreachable ();
4350 }
4351 }
4352 break;
4353
4354 case RID_TYPEID:
4355 {
4356 tree type;
4357 const char *saved_message;
4358 bool saved_in_type_id_in_expr_p;
4359
4360 /* Consume the `typeid' token. */
4361 cp_lexer_consume_token (parser->lexer);
4362 /* Look for the `(' token. */
4363 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4364 /* Types cannot be defined in a `typeid' expression. */
4365 saved_message = parser->type_definition_forbidden_message;
4366 parser->type_definition_forbidden_message
4367 = "types may not be defined in a `typeid\' expression";
4368 /* We can't be sure yet whether we're looking at a type-id or an
4369 expression. */
4370 cp_parser_parse_tentatively (parser);
4371 /* Try a type-id first. */
4372 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4373 parser->in_type_id_in_expr_p = true;
4374 type = cp_parser_type_id (parser);
4375 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4376 /* Look for the `)' token. Otherwise, we can't be sure that
4377 we're not looking at an expression: consider `typeid (int
4378 (3))', for example. */
4379 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4380 /* If all went well, simply lookup the type-id. */
4381 if (cp_parser_parse_definitely (parser))
4382 postfix_expression = get_typeid (type);
4383 /* Otherwise, fall back to the expression variant. */
4384 else
4385 {
4386 tree expression;
4387
4388 /* Look for an expression. */
4389 expression = cp_parser_expression (parser, /*cast_p=*/false);
4390 /* Compute its typeid. */
4391 postfix_expression = build_typeid (expression);
4392 /* Look for the `)' token. */
4393 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4394 }
4395 /* Restore the saved message. */
4396 parser->type_definition_forbidden_message = saved_message;
4397 /* `typeid' may not appear in an integral constant expression. */
4398 if (cp_parser_non_integral_constant_expression(parser,
4399 "`typeid' operator"))
4400 return error_mark_node;
4401 }
4402 break;
4403
4404 case RID_TYPENAME:
4405 {
4406 tree type;
4407 /* The syntax permitted here is the same permitted for an
4408 elaborated-type-specifier. */
4409 type = cp_parser_elaborated_type_specifier (parser,
4410 /*is_friend=*/false,
4411 /*is_declaration=*/false);
4412 postfix_expression = cp_parser_functional_cast (parser, type);
4413 }
4414 break;
4415
4416 default:
4417 {
4418 tree type;
4419
4420 /* If the next thing is a simple-type-specifier, we may be
4421 looking at a functional cast. We could also be looking at
4422 an id-expression. So, we try the functional cast, and if
4423 that doesn't work we fall back to the primary-expression. */
4424 cp_parser_parse_tentatively (parser);
4425 /* Look for the simple-type-specifier. */
4426 type = cp_parser_simple_type_specifier (parser,
4427 /*decl_specs=*/NULL,
4428 CP_PARSER_FLAGS_NONE);
4429 /* Parse the cast itself. */
4430 if (!cp_parser_error_occurred (parser))
4431 postfix_expression
4432 = cp_parser_functional_cast (parser, type);
4433 /* If that worked, we're done. */
4434 if (cp_parser_parse_definitely (parser))
4435 break;
4436
4437 /* If the functional-cast didn't work out, try a
4438 compound-literal. */
4439 if (cp_parser_allow_gnu_extensions_p (parser)
4440 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4441 {
4442 VEC(constructor_elt,gc) *initializer_list = NULL;
4443 bool saved_in_type_id_in_expr_p;
4444
4445 cp_parser_parse_tentatively (parser);
4446 /* Consume the `('. */
4447 cp_lexer_consume_token (parser->lexer);
4448 /* Parse the type. */
4449 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4450 parser->in_type_id_in_expr_p = true;
4451 type = cp_parser_type_id (parser);
4452 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4453 /* Look for the `)'. */
4454 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4455 /* Look for the `{'. */
4456 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4457 /* If things aren't going well, there's no need to
4458 keep going. */
4459 if (!cp_parser_error_occurred (parser))
4460 {
4461 bool non_constant_p;
4462 /* Parse the initializer-list. */
4463 initializer_list
4464 = cp_parser_initializer_list (parser, &non_constant_p);
4465 /* Allow a trailing `,'. */
4466 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4467 cp_lexer_consume_token (parser->lexer);
4468 /* Look for the final `}'. */
4469 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4470 }
4471 /* If that worked, we're definitely looking at a
4472 compound-literal expression. */
4473 if (cp_parser_parse_definitely (parser))
4474 {
4475 /* Warn the user that a compound literal is not
4476 allowed in standard C++. */
4477 if (pedantic)
4478 pedwarn ("ISO C++ forbids compound-literals");
4479 /* For simplicity, we disallow compound literals in
4480 constant-expressions. We could
4481 allow compound literals of integer type, whose
4482 initializer was a constant, in constant
4483 expressions. Permitting that usage, as a further
4484 extension, would not change the meaning of any
4485 currently accepted programs. (Of course, as
4486 compound literals are not part of ISO C++, the
4487 standard has nothing to say.) */
4488 if (cp_parser_non_integral_constant_expression
4489 (parser, "non-constant compound literals"))
4490 {
4491 postfix_expression = error_mark_node;
4492 break;
4493 }
4494 /* Form the representation of the compound-literal. */
4495 postfix_expression
4496 = finish_compound_literal (type, initializer_list);
4497 break;
4498 }
4499 }
4500
4501 /* It must be a primary-expression. */
4502 postfix_expression
4503 = cp_parser_primary_expression (parser, address_p, cast_p,
4504 /*template_arg_p=*/false,
4505 &idk);
4506 }
4507 break;
4508 }
4509
4510 /* Keep looping until the postfix-expression is complete. */
4511 while (true)
4512 {
4513 if (idk == CP_ID_KIND_UNQUALIFIED
4514 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4515 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4516 /* It is not a Koenig lookup function call. */
4517 postfix_expression
4518 = unqualified_name_lookup_error (postfix_expression);
4519
4520 /* Peek at the next token. */
4521 token = cp_lexer_peek_token (parser->lexer);
4522
4523 switch (token->type)
4524 {
4525 case CPP_OPEN_SQUARE:
4526 postfix_expression
4527 = cp_parser_postfix_open_square_expression (parser,
4528 postfix_expression,
4529 false);
4530 idk = CP_ID_KIND_NONE;
4531 is_member_access = false;
4532 break;
4533
4534 case CPP_OPEN_PAREN:
4535 /* postfix-expression ( expression-list [opt] ) */
4536 {
4537 bool koenig_p;
4538 bool is_builtin_constant_p;
4539 bool saved_integral_constant_expression_p = false;
4540 bool saved_non_integral_constant_expression_p = false;
4541 tree args;
4542
4543 is_member_access = false;
4544
4545 is_builtin_constant_p
4546 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4547 if (is_builtin_constant_p)
4548 {
4549 /* The whole point of __builtin_constant_p is to allow
4550 non-constant expressions to appear as arguments. */
4551 saved_integral_constant_expression_p
4552 = parser->integral_constant_expression_p;
4553 saved_non_integral_constant_expression_p
4554 = parser->non_integral_constant_expression_p;
4555 parser->integral_constant_expression_p = false;
4556 }
4557 args = (cp_parser_parenthesized_expression_list
4558 (parser, /*is_attribute_list=*/false,
4559 /*cast_p=*/false, /*allow_expansion_p=*/true,
4560 /*non_constant_p=*/NULL));
4561 if (is_builtin_constant_p)
4562 {
4563 parser->integral_constant_expression_p
4564 = saved_integral_constant_expression_p;
4565 parser->non_integral_constant_expression_p
4566 = saved_non_integral_constant_expression_p;
4567 }
4568
4569 if (args == error_mark_node)
4570 {
4571 postfix_expression = error_mark_node;
4572 break;
4573 }
4574
4575 /* Function calls are not permitted in
4576 constant-expressions. */
4577 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4578 && cp_parser_non_integral_constant_expression (parser,
4579 "a function call"))
4580 {
4581 postfix_expression = error_mark_node;
4582 break;
4583 }
4584
4585 koenig_p = false;
4586 if (idk == CP_ID_KIND_UNQUALIFIED)
4587 {
4588 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4589 {
4590 if (args)
4591 {
4592 koenig_p = true;
4593 postfix_expression
4594 = perform_koenig_lookup (postfix_expression, args);
4595 }
4596 else
4597 postfix_expression
4598 = unqualified_fn_lookup_error (postfix_expression);
4599 }
4600 /* We do not perform argument-dependent lookup if
4601 normal lookup finds a non-function, in accordance
4602 with the expected resolution of DR 218. */
4603 else if (args && is_overloaded_fn (postfix_expression))
4604 {
4605 tree fn = get_first_fn (postfix_expression);
4606
4607 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4608 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4609
4610 /* Only do argument dependent lookup if regular
4611 lookup does not find a set of member functions.
4612 [basic.lookup.koenig]/2a */
4613 if (!DECL_FUNCTION_MEMBER_P (fn))
4614 {
4615 koenig_p = true;
4616 postfix_expression
4617 = perform_koenig_lookup (postfix_expression, args);
4618 }
4619 }
4620 }
4621
4622 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4623 {
4624 tree instance = TREE_OPERAND (postfix_expression, 0);
4625 tree fn = TREE_OPERAND (postfix_expression, 1);
4626
4627 if (processing_template_decl
4628 && (type_dependent_expression_p (instance)
4629 || (!BASELINK_P (fn)
4630 && TREE_CODE (fn) != FIELD_DECL)
4631 || type_dependent_expression_p (fn)
4632 || any_type_dependent_arguments_p (args)))
4633 {
4634 postfix_expression
4635 = build_nt_call_list (postfix_expression, args);
4636 break;
4637 }
4638
4639 if (BASELINK_P (fn))
4640 postfix_expression
4641 = (build_new_method_call
4642 (instance, fn, args, NULL_TREE,
4643 (idk == CP_ID_KIND_QUALIFIED
4644 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4645 /*fn_p=*/NULL));
4646 else
4647 postfix_expression
4648 = finish_call_expr (postfix_expression, args,
4649 /*disallow_virtual=*/false,
4650 /*koenig_p=*/false);
4651 }
4652 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4653 || TREE_CODE (postfix_expression) == MEMBER_REF
4654 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4655 postfix_expression = (build_offset_ref_call_from_tree
4656 (postfix_expression, args));
4657 else if (idk == CP_ID_KIND_QUALIFIED)
4658 /* A call to a static class member, or a namespace-scope
4659 function. */
4660 postfix_expression
4661 = finish_call_expr (postfix_expression, args,
4662 /*disallow_virtual=*/true,
4663 koenig_p);
4664 else
4665 /* All other function calls. */
4666 postfix_expression
4667 = finish_call_expr (postfix_expression, args,
4668 /*disallow_virtual=*/false,
4669 koenig_p);
4670
4671 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4672 idk = CP_ID_KIND_NONE;
4673 }
4674 break;
4675
4676 case CPP_DOT:
4677 case CPP_DEREF:
4678 /* postfix-expression . template [opt] id-expression
4679 postfix-expression . pseudo-destructor-name
4680 postfix-expression -> template [opt] id-expression
4681 postfix-expression -> pseudo-destructor-name */
4682
4683 /* Consume the `.' or `->' operator. */
4684 cp_lexer_consume_token (parser->lexer);
4685
4686 postfix_expression
4687 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4688 postfix_expression,
4689 false, &idk);
4690
4691 is_member_access = true;
4692 break;
4693
4694 case CPP_PLUS_PLUS:
4695 /* postfix-expression ++ */
4696 /* Consume the `++' token. */
4697 cp_lexer_consume_token (parser->lexer);
4698 /* Generate a representation for the complete expression. */
4699 postfix_expression
4700 = finish_increment_expr (postfix_expression,
4701 POSTINCREMENT_EXPR);
4702 /* Increments may not appear in constant-expressions. */
4703 if (cp_parser_non_integral_constant_expression (parser,
4704 "an increment"))
4705 postfix_expression = error_mark_node;
4706 idk = CP_ID_KIND_NONE;
4707 is_member_access = false;
4708 break;
4709
4710 case CPP_MINUS_MINUS:
4711 /* postfix-expression -- */
4712 /* Consume the `--' token. */
4713 cp_lexer_consume_token (parser->lexer);
4714 /* Generate a representation for the complete expression. */
4715 postfix_expression
4716 = finish_increment_expr (postfix_expression,
4717 POSTDECREMENT_EXPR);
4718 /* Decrements may not appear in constant-expressions. */
4719 if (cp_parser_non_integral_constant_expression (parser,
4720 "a decrement"))
4721 postfix_expression = error_mark_node;
4722 idk = CP_ID_KIND_NONE;
4723 is_member_access = false;
4724 break;
4725
4726 default:
4727 if (member_access_only_p)
4728 return is_member_access? postfix_expression : error_mark_node;
4729 else
4730 return postfix_expression;
4731 }
4732 }
4733
4734 /* We should never get here. */
4735 gcc_unreachable ();
4736 return error_mark_node;
4737 }
4738
4739 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4740 by cp_parser_builtin_offsetof. We're looking for
4741
4742 postfix-expression [ expression ]
4743
4744 FOR_OFFSETOF is set if we're being called in that context, which
4745 changes how we deal with integer constant expressions. */
4746
4747 static tree
4748 cp_parser_postfix_open_square_expression (cp_parser *parser,
4749 tree postfix_expression,
4750 bool for_offsetof)
4751 {
4752 tree index;
4753
4754 /* Consume the `[' token. */
4755 cp_lexer_consume_token (parser->lexer);
4756
4757 /* Parse the index expression. */
4758 /* ??? For offsetof, there is a question of what to allow here. If
4759 offsetof is not being used in an integral constant expression context,
4760 then we *could* get the right answer by computing the value at runtime.
4761 If we are in an integral constant expression context, then we might
4762 could accept any constant expression; hard to say without analysis.
4763 Rather than open the barn door too wide right away, allow only integer
4764 constant expressions here. */
4765 if (for_offsetof)
4766 index = cp_parser_constant_expression (parser, false, NULL);
4767 else
4768 index = cp_parser_expression (parser, /*cast_p=*/false);
4769
4770 /* Look for the closing `]'. */
4771 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4772
4773 /* Build the ARRAY_REF. */
4774 postfix_expression = grok_array_decl (postfix_expression, index);
4775
4776 /* When not doing offsetof, array references are not permitted in
4777 constant-expressions. */
4778 if (!for_offsetof
4779 && (cp_parser_non_integral_constant_expression
4780 (parser, "an array reference")))
4781 postfix_expression = error_mark_node;
4782
4783 return postfix_expression;
4784 }
4785
4786 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4787 by cp_parser_builtin_offsetof. We're looking for
4788
4789 postfix-expression . template [opt] id-expression
4790 postfix-expression . pseudo-destructor-name
4791 postfix-expression -> template [opt] id-expression
4792 postfix-expression -> pseudo-destructor-name
4793
4794 FOR_OFFSETOF is set if we're being called in that context. That sorta
4795 limits what of the above we'll actually accept, but nevermind.
4796 TOKEN_TYPE is the "." or "->" token, which will already have been
4797 removed from the stream. */
4798
4799 static tree
4800 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4801 enum cpp_ttype token_type,
4802 tree postfix_expression,
4803 bool for_offsetof, cp_id_kind *idk)
4804 {
4805 tree name;
4806 bool dependent_p;
4807 bool pseudo_destructor_p;
4808 tree scope = NULL_TREE;
4809
4810 /* If this is a `->' operator, dereference the pointer. */
4811 if (token_type == CPP_DEREF)
4812 postfix_expression = build_x_arrow (postfix_expression);
4813 /* Check to see whether or not the expression is type-dependent. */
4814 dependent_p = type_dependent_expression_p (postfix_expression);
4815 /* The identifier following the `->' or `.' is not qualified. */
4816 parser->scope = NULL_TREE;
4817 parser->qualifying_scope = NULL_TREE;
4818 parser->object_scope = NULL_TREE;
4819 *idk = CP_ID_KIND_NONE;
4820 /* Enter the scope corresponding to the type of the object
4821 given by the POSTFIX_EXPRESSION. */
4822 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4823 {
4824 scope = TREE_TYPE (postfix_expression);
4825 /* According to the standard, no expression should ever have
4826 reference type. Unfortunately, we do not currently match
4827 the standard in this respect in that our internal representation
4828 of an expression may have reference type even when the standard
4829 says it does not. Therefore, we have to manually obtain the
4830 underlying type here. */
4831 scope = non_reference (scope);
4832 /* The type of the POSTFIX_EXPRESSION must be complete. */
4833 if (scope == unknown_type_node)
4834 {
4835 error ("%qE does not have class type", postfix_expression);
4836 scope = NULL_TREE;
4837 }
4838 else
4839 scope = complete_type_or_else (scope, NULL_TREE);
4840 /* Let the name lookup machinery know that we are processing a
4841 class member access expression. */
4842 parser->context->object_type = scope;
4843 /* If something went wrong, we want to be able to discern that case,
4844 as opposed to the case where there was no SCOPE due to the type
4845 of expression being dependent. */
4846 if (!scope)
4847 scope = error_mark_node;
4848 /* If the SCOPE was erroneous, make the various semantic analysis
4849 functions exit quickly -- and without issuing additional error
4850 messages. */
4851 if (scope == error_mark_node)
4852 postfix_expression = error_mark_node;
4853 }
4854
4855 /* Assume this expression is not a pseudo-destructor access. */
4856 pseudo_destructor_p = false;
4857
4858 /* If the SCOPE is a scalar type, then, if this is a valid program,
4859 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4860 is type dependent, it can be pseudo-destructor-name or something else.
4861 Try to parse it as pseudo-destructor-name first. */
4862 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4863 {
4864 tree s;
4865 tree type;
4866
4867 cp_parser_parse_tentatively (parser);
4868 /* Parse the pseudo-destructor-name. */
4869 s = NULL_TREE;
4870 cp_parser_pseudo_destructor_name (parser, &s, &type);
4871 if (dependent_p
4872 && (cp_parser_error_occurred (parser)
4873 || TREE_CODE (type) != TYPE_DECL
4874 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4875 cp_parser_abort_tentative_parse (parser);
4876 else if (cp_parser_parse_definitely (parser))
4877 {
4878 pseudo_destructor_p = true;
4879 postfix_expression
4880 = finish_pseudo_destructor_expr (postfix_expression,
4881 s, TREE_TYPE (type));
4882 }
4883 }
4884
4885 if (!pseudo_destructor_p)
4886 {
4887 /* If the SCOPE is not a scalar type, we are looking at an
4888 ordinary class member access expression, rather than a
4889 pseudo-destructor-name. */
4890 bool template_p;
4891 /* Parse the id-expression. */
4892 name = (cp_parser_id_expression
4893 (parser,
4894 cp_parser_optional_template_keyword (parser),
4895 /*check_dependency_p=*/true,
4896 &template_p,
4897 /*declarator_p=*/false,
4898 /*optional_p=*/false));
4899 /* In general, build a SCOPE_REF if the member name is qualified.
4900 However, if the name was not dependent and has already been
4901 resolved; there is no need to build the SCOPE_REF. For example;
4902
4903 struct X { void f(); };
4904 template <typename T> void f(T* t) { t->X::f(); }
4905
4906 Even though "t" is dependent, "X::f" is not and has been resolved
4907 to a BASELINK; there is no need to include scope information. */
4908
4909 /* But we do need to remember that there was an explicit scope for
4910 virtual function calls. */
4911 if (parser->scope)
4912 *idk = CP_ID_KIND_QUALIFIED;
4913
4914 /* If the name is a template-id that names a type, we will get a
4915 TYPE_DECL here. That is invalid code. */
4916 if (TREE_CODE (name) == TYPE_DECL)
4917 {
4918 error ("invalid use of %qD", name);
4919 postfix_expression = error_mark_node;
4920 }
4921 else
4922 {
4923 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4924 {
4925 name = build_qualified_name (/*type=*/NULL_TREE,
4926 parser->scope,
4927 name,
4928 template_p);
4929 parser->scope = NULL_TREE;
4930 parser->qualifying_scope = NULL_TREE;
4931 parser->object_scope = NULL_TREE;
4932 }
4933 if (scope && name && BASELINK_P (name))
4934 adjust_result_of_qualified_name_lookup
4935 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4936 postfix_expression
4937 = finish_class_member_access_expr (postfix_expression, name,
4938 template_p);
4939 }
4940 }
4941
4942 /* We no longer need to look up names in the scope of the object on
4943 the left-hand side of the `.' or `->' operator. */
4944 parser->context->object_type = NULL_TREE;
4945
4946 /* Outside of offsetof, these operators may not appear in
4947 constant-expressions. */
4948 if (!for_offsetof
4949 && (cp_parser_non_integral_constant_expression
4950 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4951 postfix_expression = error_mark_node;
4952
4953 return postfix_expression;
4954 }
4955
4956 /* Parse a parenthesized expression-list.
4957
4958 expression-list:
4959 assignment-expression
4960 expression-list, assignment-expression
4961
4962 attribute-list:
4963 expression-list
4964 identifier
4965 identifier, expression-list
4966
4967 CAST_P is true if this expression is the target of a cast.
4968
4969 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4970 argument pack.
4971
4972 Returns a TREE_LIST. The TREE_VALUE of each node is a
4973 representation of an assignment-expression. Note that a TREE_LIST
4974 is returned even if there is only a single expression in the list.
4975 error_mark_node is returned if the ( and or ) are
4976 missing. NULL_TREE is returned on no expressions. The parentheses
4977 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4978 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4979 indicates whether or not all of the expressions in the list were
4980 constant. */
4981
4982 static tree
4983 cp_parser_parenthesized_expression_list (cp_parser* parser,
4984 bool is_attribute_list,
4985 bool cast_p,
4986 bool allow_expansion_p,
4987 bool *non_constant_p)
4988 {
4989 tree expression_list = NULL_TREE;
4990 bool fold_expr_p = is_attribute_list;
4991 tree identifier = NULL_TREE;
4992 bool saved_greater_than_is_operator_p;
4993
4994 /* Assume all the expressions will be constant. */
4995 if (non_constant_p)
4996 *non_constant_p = false;
4997
4998 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4999 return error_mark_node;
5000
5001 /* Within a parenthesized expression, a `>' token is always
5002 the greater-than operator. */
5003 saved_greater_than_is_operator_p
5004 = parser->greater_than_is_operator_p;
5005 parser->greater_than_is_operator_p = true;
5006
5007 /* Consume expressions until there are no more. */
5008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5009 while (true)
5010 {
5011 tree expr;
5012
5013 /* At the beginning of attribute lists, check to see if the
5014 next token is an identifier. */
5015 if (is_attribute_list
5016 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5017 {
5018 cp_token *token;
5019
5020 /* Consume the identifier. */
5021 token = cp_lexer_consume_token (parser->lexer);
5022 /* Save the identifier. */
5023 identifier = token->u.value;
5024 }
5025 else
5026 {
5027 /* Parse the next assignment-expression. */
5028 if (non_constant_p)
5029 {
5030 bool expr_non_constant_p;
5031 expr = (cp_parser_constant_expression
5032 (parser, /*allow_non_constant_p=*/true,
5033 &expr_non_constant_p));
5034 if (expr_non_constant_p)
5035 *non_constant_p = true;
5036 }
5037 else
5038 expr = cp_parser_assignment_expression (parser, cast_p);
5039
5040 if (fold_expr_p)
5041 expr = fold_non_dependent_expr (expr);
5042
5043 /* If we have an ellipsis, then this is an expression
5044 expansion. */
5045 if (allow_expansion_p
5046 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5047 {
5048 /* Consume the `...'. */
5049 cp_lexer_consume_token (parser->lexer);
5050
5051 /* Build the argument pack. */
5052 expr = make_pack_expansion (expr);
5053 }
5054
5055 /* Add it to the list. We add error_mark_node
5056 expressions to the list, so that we can still tell if
5057 the correct form for a parenthesized expression-list
5058 is found. That gives better errors. */
5059 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5060
5061 if (expr == error_mark_node)
5062 goto skip_comma;
5063 }
5064
5065 /* After the first item, attribute lists look the same as
5066 expression lists. */
5067 is_attribute_list = false;
5068
5069 get_comma:;
5070 /* If the next token isn't a `,', then we are done. */
5071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5072 break;
5073
5074 /* Otherwise, consume the `,' and keep going. */
5075 cp_lexer_consume_token (parser->lexer);
5076 }
5077
5078 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5079 {
5080 int ending;
5081
5082 skip_comma:;
5083 /* We try and resync to an unnested comma, as that will give the
5084 user better diagnostics. */
5085 ending = cp_parser_skip_to_closing_parenthesis (parser,
5086 /*recovering=*/true,
5087 /*or_comma=*/true,
5088 /*consume_paren=*/true);
5089 if (ending < 0)
5090 goto get_comma;
5091 if (!ending)
5092 {
5093 parser->greater_than_is_operator_p
5094 = saved_greater_than_is_operator_p;
5095 return error_mark_node;
5096 }
5097 }
5098
5099 parser->greater_than_is_operator_p
5100 = saved_greater_than_is_operator_p;
5101
5102 /* We built up the list in reverse order so we must reverse it now. */
5103 expression_list = nreverse (expression_list);
5104 if (identifier)
5105 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5106
5107 return expression_list;
5108 }
5109
5110 /* Parse a pseudo-destructor-name.
5111
5112 pseudo-destructor-name:
5113 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5114 :: [opt] nested-name-specifier template template-id :: ~ type-name
5115 :: [opt] nested-name-specifier [opt] ~ type-name
5116
5117 If either of the first two productions is used, sets *SCOPE to the
5118 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5119 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5120 or ERROR_MARK_NODE if the parse fails. */
5121
5122 static void
5123 cp_parser_pseudo_destructor_name (cp_parser* parser,
5124 tree* scope,
5125 tree* type)
5126 {
5127 bool nested_name_specifier_p;
5128
5129 /* Assume that things will not work out. */
5130 *type = error_mark_node;
5131
5132 /* Look for the optional `::' operator. */
5133 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5134 /* Look for the optional nested-name-specifier. */
5135 nested_name_specifier_p
5136 = (cp_parser_nested_name_specifier_opt (parser,
5137 /*typename_keyword_p=*/false,
5138 /*check_dependency_p=*/true,
5139 /*type_p=*/false,
5140 /*is_declaration=*/true)
5141 != NULL_TREE);
5142 /* Now, if we saw a nested-name-specifier, we might be doing the
5143 second production. */
5144 if (nested_name_specifier_p
5145 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5146 {
5147 /* Consume the `template' keyword. */
5148 cp_lexer_consume_token (parser->lexer);
5149 /* Parse the template-id. */
5150 cp_parser_template_id (parser,
5151 /*template_keyword_p=*/true,
5152 /*check_dependency_p=*/false,
5153 /*is_declaration=*/true);
5154 /* Look for the `::' token. */
5155 cp_parser_require (parser, CPP_SCOPE, "`::'");
5156 }
5157 /* If the next token is not a `~', then there might be some
5158 additional qualification. */
5159 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5160 {
5161 /* At this point, we're looking for "type-name :: ~". The type-name
5162 must not be a class-name, since this is a pseudo-destructor. So,
5163 it must be either an enum-name, or a typedef-name -- both of which
5164 are just identifiers. So, we peek ahead to check that the "::"
5165 and "~" tokens are present; if they are not, then we can avoid
5166 calling type_name. */
5167 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5168 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5169 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5170 {
5171 cp_parser_error (parser, "non-scalar type");
5172 return;
5173 }
5174
5175 /* Look for the type-name. */
5176 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5177 if (*scope == error_mark_node)
5178 return;
5179
5180 /* Look for the `::' token. */
5181 cp_parser_require (parser, CPP_SCOPE, "`::'");
5182 }
5183 else
5184 *scope = NULL_TREE;
5185
5186 /* Look for the `~'. */
5187 cp_parser_require (parser, CPP_COMPL, "`~'");
5188 /* Look for the type-name again. We are not responsible for
5189 checking that it matches the first type-name. */
5190 *type = cp_parser_nonclass_name (parser);
5191 }
5192
5193 /* Parse a unary-expression.
5194
5195 unary-expression:
5196 postfix-expression
5197 ++ cast-expression
5198 -- cast-expression
5199 unary-operator cast-expression
5200 sizeof unary-expression
5201 sizeof ( type-id )
5202 new-expression
5203 delete-expression
5204
5205 GNU Extensions:
5206
5207 unary-expression:
5208 __extension__ cast-expression
5209 __alignof__ unary-expression
5210 __alignof__ ( type-id )
5211 __real__ cast-expression
5212 __imag__ cast-expression
5213 && identifier
5214
5215 ADDRESS_P is true iff the unary-expression is appearing as the
5216 operand of the `&' operator. CAST_P is true if this expression is
5217 the target of a cast.
5218
5219 Returns a representation of the expression. */
5220
5221 static tree
5222 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5223 {
5224 cp_token *token;
5225 enum tree_code unary_operator;
5226
5227 /* Peek at the next token. */
5228 token = cp_lexer_peek_token (parser->lexer);
5229 /* Some keywords give away the kind of expression. */
5230 if (token->type == CPP_KEYWORD)
5231 {
5232 enum rid keyword = token->keyword;
5233
5234 switch (keyword)
5235 {
5236 case RID_ALIGNOF:
5237 case RID_SIZEOF:
5238 {
5239 tree operand;
5240 enum tree_code op;
5241
5242 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5243 /* Consume the token. */
5244 cp_lexer_consume_token (parser->lexer);
5245 /* Parse the operand. */
5246 operand = cp_parser_sizeof_operand (parser, keyword);
5247
5248 if (TYPE_P (operand))
5249 return cxx_sizeof_or_alignof_type (operand, op, true);
5250 else
5251 return cxx_sizeof_or_alignof_expr (operand, op);
5252 }
5253
5254 case RID_NEW:
5255 return cp_parser_new_expression (parser);
5256
5257 case RID_DELETE:
5258 return cp_parser_delete_expression (parser);
5259
5260 case RID_EXTENSION:
5261 {
5262 /* The saved value of the PEDANTIC flag. */
5263 int saved_pedantic;
5264 tree expr;
5265
5266 /* Save away the PEDANTIC flag. */
5267 cp_parser_extension_opt (parser, &saved_pedantic);
5268 /* Parse the cast-expression. */
5269 expr = cp_parser_simple_cast_expression (parser);
5270 /* Restore the PEDANTIC flag. */
5271 pedantic = saved_pedantic;
5272
5273 return expr;
5274 }
5275
5276 case RID_REALPART:
5277 case RID_IMAGPART:
5278 {
5279 tree expression;
5280
5281 /* Consume the `__real__' or `__imag__' token. */
5282 cp_lexer_consume_token (parser->lexer);
5283 /* Parse the cast-expression. */
5284 expression = cp_parser_simple_cast_expression (parser);
5285 /* Create the complete representation. */
5286 return build_x_unary_op ((keyword == RID_REALPART
5287 ? REALPART_EXPR : IMAGPART_EXPR),
5288 expression);
5289 }
5290 break;
5291
5292 default:
5293 break;
5294 }
5295 }
5296
5297 /* Look for the `:: new' and `:: delete', which also signal the
5298 beginning of a new-expression, or delete-expression,
5299 respectively. If the next token is `::', then it might be one of
5300 these. */
5301 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5302 {
5303 enum rid keyword;
5304
5305 /* See if the token after the `::' is one of the keywords in
5306 which we're interested. */
5307 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5308 /* If it's `new', we have a new-expression. */
5309 if (keyword == RID_NEW)
5310 return cp_parser_new_expression (parser);
5311 /* Similarly, for `delete'. */
5312 else if (keyword == RID_DELETE)
5313 return cp_parser_delete_expression (parser);
5314 }
5315
5316 /* Look for a unary operator. */
5317 unary_operator = cp_parser_unary_operator (token);
5318 /* The `++' and `--' operators can be handled similarly, even though
5319 they are not technically unary-operators in the grammar. */
5320 if (unary_operator == ERROR_MARK)
5321 {
5322 if (token->type == CPP_PLUS_PLUS)
5323 unary_operator = PREINCREMENT_EXPR;
5324 else if (token->type == CPP_MINUS_MINUS)
5325 unary_operator = PREDECREMENT_EXPR;
5326 /* Handle the GNU address-of-label extension. */
5327 else if (cp_parser_allow_gnu_extensions_p (parser)
5328 && token->type == CPP_AND_AND)
5329 {
5330 tree identifier;
5331 tree expression;
5332
5333 /* Consume the '&&' token. */
5334 cp_lexer_consume_token (parser->lexer);
5335 /* Look for the identifier. */
5336 identifier = cp_parser_identifier (parser);
5337 /* Create an expression representing the address. */
5338 expression = finish_label_address_expr (identifier);
5339 if (cp_parser_non_integral_constant_expression (parser,
5340 "the address of a label"))
5341 expression = error_mark_node;
5342 return expression;
5343 }
5344 }
5345 if (unary_operator != ERROR_MARK)
5346 {
5347 tree cast_expression;
5348 tree expression = error_mark_node;
5349 const char *non_constant_p = NULL;
5350
5351 /* Consume the operator token. */
5352 token = cp_lexer_consume_token (parser->lexer);
5353 /* Parse the cast-expression. */
5354 cast_expression
5355 = cp_parser_cast_expression (parser,
5356 unary_operator == ADDR_EXPR,
5357 /*cast_p=*/false);
5358 /* Now, build an appropriate representation. */
5359 switch (unary_operator)
5360 {
5361 case INDIRECT_REF:
5362 non_constant_p = "`*'";
5363 expression = build_x_indirect_ref (cast_expression, "unary *");
5364 break;
5365
5366 case ADDR_EXPR:
5367 non_constant_p = "`&'";
5368 /* Fall through. */
5369 case BIT_NOT_EXPR:
5370 expression = build_x_unary_op (unary_operator, cast_expression);
5371 break;
5372
5373 case PREINCREMENT_EXPR:
5374 case PREDECREMENT_EXPR:
5375 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5376 ? "`++'" : "`--'");
5377 /* Fall through. */
5378 case UNARY_PLUS_EXPR:
5379 case NEGATE_EXPR:
5380 case TRUTH_NOT_EXPR:
5381 expression = finish_unary_op_expr (unary_operator, cast_expression);
5382 break;
5383
5384 default:
5385 gcc_unreachable ();
5386 }
5387
5388 if (non_constant_p
5389 && cp_parser_non_integral_constant_expression (parser,
5390 non_constant_p))
5391 expression = error_mark_node;
5392
5393 return expression;
5394 }
5395
5396 return cp_parser_postfix_expression (parser, address_p, cast_p,
5397 /*member_access_only_p=*/false);
5398 }
5399
5400 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5401 unary-operator, the corresponding tree code is returned. */
5402
5403 static enum tree_code
5404 cp_parser_unary_operator (cp_token* token)
5405 {
5406 switch (token->type)
5407 {
5408 case CPP_MULT:
5409 return INDIRECT_REF;
5410
5411 case CPP_AND:
5412 return ADDR_EXPR;
5413
5414 case CPP_PLUS:
5415 return UNARY_PLUS_EXPR;
5416
5417 case CPP_MINUS:
5418 return NEGATE_EXPR;
5419
5420 case CPP_NOT:
5421 return TRUTH_NOT_EXPR;
5422
5423 case CPP_COMPL:
5424 return BIT_NOT_EXPR;
5425
5426 default:
5427 return ERROR_MARK;
5428 }
5429 }
5430
5431 /* Parse a new-expression.
5432
5433 new-expression:
5434 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5435 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5436
5437 Returns a representation of the expression. */
5438
5439 static tree
5440 cp_parser_new_expression (cp_parser* parser)
5441 {
5442 bool global_scope_p;
5443 tree placement;
5444 tree type;
5445 tree initializer;
5446 tree nelts;
5447
5448 /* Look for the optional `::' operator. */
5449 global_scope_p
5450 = (cp_parser_global_scope_opt (parser,
5451 /*current_scope_valid_p=*/false)
5452 != NULL_TREE);
5453 /* Look for the `new' operator. */
5454 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5455 /* There's no easy way to tell a new-placement from the
5456 `( type-id )' construct. */
5457 cp_parser_parse_tentatively (parser);
5458 /* Look for a new-placement. */
5459 placement = cp_parser_new_placement (parser);
5460 /* If that didn't work out, there's no new-placement. */
5461 if (!cp_parser_parse_definitely (parser))
5462 placement = NULL_TREE;
5463
5464 /* If the next token is a `(', then we have a parenthesized
5465 type-id. */
5466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5467 {
5468 /* Consume the `('. */
5469 cp_lexer_consume_token (parser->lexer);
5470 /* Parse the type-id. */
5471 type = cp_parser_type_id (parser);
5472 /* Look for the closing `)'. */
5473 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5474 /* There should not be a direct-new-declarator in this production,
5475 but GCC used to allowed this, so we check and emit a sensible error
5476 message for this case. */
5477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5478 {
5479 error ("array bound forbidden after parenthesized type-id");
5480 inform ("try removing the parentheses around the type-id");
5481 cp_parser_direct_new_declarator (parser);
5482 }
5483 nelts = NULL_TREE;
5484 }
5485 /* Otherwise, there must be a new-type-id. */
5486 else
5487 type = cp_parser_new_type_id (parser, &nelts);
5488
5489 /* If the next token is a `(', then we have a new-initializer. */
5490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5491 initializer = cp_parser_new_initializer (parser);
5492 else
5493 initializer = NULL_TREE;
5494
5495 /* A new-expression may not appear in an integral constant
5496 expression. */
5497 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5498 return error_mark_node;
5499
5500 /* Create a representation of the new-expression. */
5501 return build_new (placement, type, nelts, initializer, global_scope_p);
5502 }
5503
5504 /* Parse a new-placement.
5505
5506 new-placement:
5507 ( expression-list )
5508
5509 Returns the same representation as for an expression-list. */
5510
5511 static tree
5512 cp_parser_new_placement (cp_parser* parser)
5513 {
5514 tree expression_list;
5515
5516 /* Parse the expression-list. */
5517 expression_list = (cp_parser_parenthesized_expression_list
5518 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5519 /*non_constant_p=*/NULL));
5520
5521 return expression_list;
5522 }
5523
5524 /* Parse a new-type-id.
5525
5526 new-type-id:
5527 type-specifier-seq new-declarator [opt]
5528
5529 Returns the TYPE allocated. If the new-type-id indicates an array
5530 type, *NELTS is set to the number of elements in the last array
5531 bound; the TYPE will not include the last array bound. */
5532
5533 static tree
5534 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5535 {
5536 cp_decl_specifier_seq type_specifier_seq;
5537 cp_declarator *new_declarator;
5538 cp_declarator *declarator;
5539 cp_declarator *outer_declarator;
5540 const char *saved_message;
5541 tree type;
5542
5543 /* The type-specifier sequence must not contain type definitions.
5544 (It cannot contain declarations of new types either, but if they
5545 are not definitions we will catch that because they are not
5546 complete.) */
5547 saved_message = parser->type_definition_forbidden_message;
5548 parser->type_definition_forbidden_message
5549 = "types may not be defined in a new-type-id";
5550 /* Parse the type-specifier-seq. */
5551 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5552 &type_specifier_seq);
5553 /* Restore the old message. */
5554 parser->type_definition_forbidden_message = saved_message;
5555 /* Parse the new-declarator. */
5556 new_declarator = cp_parser_new_declarator_opt (parser);
5557
5558 /* Determine the number of elements in the last array dimension, if
5559 any. */
5560 *nelts = NULL_TREE;
5561 /* Skip down to the last array dimension. */
5562 declarator = new_declarator;
5563 outer_declarator = NULL;
5564 while (declarator && (declarator->kind == cdk_pointer
5565 || declarator->kind == cdk_ptrmem))
5566 {
5567 outer_declarator = declarator;
5568 declarator = declarator->declarator;
5569 }
5570 while (declarator
5571 && declarator->kind == cdk_array
5572 && declarator->declarator
5573 && declarator->declarator->kind == cdk_array)
5574 {
5575 outer_declarator = declarator;
5576 declarator = declarator->declarator;
5577 }
5578
5579 if (declarator && declarator->kind == cdk_array)
5580 {
5581 *nelts = declarator->u.array.bounds;
5582 if (*nelts == error_mark_node)
5583 *nelts = integer_one_node;
5584
5585 if (outer_declarator)
5586 outer_declarator->declarator = declarator->declarator;
5587 else
5588 new_declarator = NULL;
5589 }
5590
5591 type = groktypename (&type_specifier_seq, new_declarator);
5592 return type;
5593 }
5594
5595 /* Parse an (optional) new-declarator.
5596
5597 new-declarator:
5598 ptr-operator new-declarator [opt]
5599 direct-new-declarator
5600
5601 Returns the declarator. */
5602
5603 static cp_declarator *
5604 cp_parser_new_declarator_opt (cp_parser* parser)
5605 {
5606 enum tree_code code;
5607 tree type;
5608 cp_cv_quals cv_quals;
5609
5610 /* We don't know if there's a ptr-operator next, or not. */
5611 cp_parser_parse_tentatively (parser);
5612 /* Look for a ptr-operator. */
5613 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5614 /* If that worked, look for more new-declarators. */
5615 if (cp_parser_parse_definitely (parser))
5616 {
5617 cp_declarator *declarator;
5618
5619 /* Parse another optional declarator. */
5620 declarator = cp_parser_new_declarator_opt (parser);
5621
5622 return cp_parser_make_indirect_declarator
5623 (code, type, cv_quals, declarator);
5624 }
5625
5626 /* If the next token is a `[', there is a direct-new-declarator. */
5627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5628 return cp_parser_direct_new_declarator (parser);
5629
5630 return NULL;
5631 }
5632
5633 /* Parse a direct-new-declarator.
5634
5635 direct-new-declarator:
5636 [ expression ]
5637 direct-new-declarator [constant-expression]
5638
5639 */
5640
5641 static cp_declarator *
5642 cp_parser_direct_new_declarator (cp_parser* parser)
5643 {
5644 cp_declarator *declarator = NULL;
5645
5646 while (true)
5647 {
5648 tree expression;
5649
5650 /* Look for the opening `['. */
5651 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5652 /* The first expression is not required to be constant. */
5653 if (!declarator)
5654 {
5655 expression = cp_parser_expression (parser, /*cast_p=*/false);
5656 /* The standard requires that the expression have integral
5657 type. DR 74 adds enumeration types. We believe that the
5658 real intent is that these expressions be handled like the
5659 expression in a `switch' condition, which also allows
5660 classes with a single conversion to integral or
5661 enumeration type. */
5662 if (!processing_template_decl)
5663 {
5664 expression
5665 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5666 expression,
5667 /*complain=*/true);
5668 if (!expression)
5669 {
5670 error ("expression in new-declarator must have integral "
5671 "or enumeration type");
5672 expression = error_mark_node;
5673 }
5674 }
5675 }
5676 /* But all the other expressions must be. */
5677 else
5678 expression
5679 = cp_parser_constant_expression (parser,
5680 /*allow_non_constant=*/false,
5681 NULL);
5682 /* Look for the closing `]'. */
5683 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5684
5685 /* Add this bound to the declarator. */
5686 declarator = make_array_declarator (declarator, expression);
5687
5688 /* If the next token is not a `[', then there are no more
5689 bounds. */
5690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5691 break;
5692 }
5693
5694 return declarator;
5695 }
5696
5697 /* Parse a new-initializer.
5698
5699 new-initializer:
5700 ( expression-list [opt] )
5701
5702 Returns a representation of the expression-list. If there is no
5703 expression-list, VOID_ZERO_NODE is returned. */
5704
5705 static tree
5706 cp_parser_new_initializer (cp_parser* parser)
5707 {
5708 tree expression_list;
5709
5710 expression_list = (cp_parser_parenthesized_expression_list
5711 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5712 /*non_constant_p=*/NULL));
5713 if (!expression_list)
5714 expression_list = void_zero_node;
5715
5716 return expression_list;
5717 }
5718
5719 /* Parse a delete-expression.
5720
5721 delete-expression:
5722 :: [opt] delete cast-expression
5723 :: [opt] delete [ ] cast-expression
5724
5725 Returns a representation of the expression. */
5726
5727 static tree
5728 cp_parser_delete_expression (cp_parser* parser)
5729 {
5730 bool global_scope_p;
5731 bool array_p;
5732 tree expression;
5733
5734 /* Look for the optional `::' operator. */
5735 global_scope_p
5736 = (cp_parser_global_scope_opt (parser,
5737 /*current_scope_valid_p=*/false)
5738 != NULL_TREE);
5739 /* Look for the `delete' keyword. */
5740 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5741 /* See if the array syntax is in use. */
5742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5743 {
5744 /* Consume the `[' token. */
5745 cp_lexer_consume_token (parser->lexer);
5746 /* Look for the `]' token. */
5747 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5748 /* Remember that this is the `[]' construct. */
5749 array_p = true;
5750 }
5751 else
5752 array_p = false;
5753
5754 /* Parse the cast-expression. */
5755 expression = cp_parser_simple_cast_expression (parser);
5756
5757 /* A delete-expression may not appear in an integral constant
5758 expression. */
5759 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5760 return error_mark_node;
5761
5762 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5763 }
5764
5765 /* Parse a cast-expression.
5766
5767 cast-expression:
5768 unary-expression
5769 ( type-id ) cast-expression
5770
5771 ADDRESS_P is true iff the unary-expression is appearing as the
5772 operand of the `&' operator. CAST_P is true if this expression is
5773 the target of a cast.
5774
5775 Returns a representation of the expression. */
5776
5777 static tree
5778 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5779 {
5780 /* If it's a `(', then we might be looking at a cast. */
5781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5782 {
5783 tree type = NULL_TREE;
5784 tree expr = NULL_TREE;
5785 bool compound_literal_p;
5786 const char *saved_message;
5787
5788 /* There's no way to know yet whether or not this is a cast.
5789 For example, `(int (3))' is a unary-expression, while `(int)
5790 3' is a cast. So, we resort to parsing tentatively. */
5791 cp_parser_parse_tentatively (parser);
5792 /* Types may not be defined in a cast. */
5793 saved_message = parser->type_definition_forbidden_message;
5794 parser->type_definition_forbidden_message
5795 = "types may not be defined in casts";
5796 /* Consume the `('. */
5797 cp_lexer_consume_token (parser->lexer);
5798 /* A very tricky bit is that `(struct S) { 3 }' is a
5799 compound-literal (which we permit in C++ as an extension).
5800 But, that construct is not a cast-expression -- it is a
5801 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5802 is legal; if the compound-literal were a cast-expression,
5803 you'd need an extra set of parentheses.) But, if we parse
5804 the type-id, and it happens to be a class-specifier, then we
5805 will commit to the parse at that point, because we cannot
5806 undo the action that is done when creating a new class. So,
5807 then we cannot back up and do a postfix-expression.
5808
5809 Therefore, we scan ahead to the closing `)', and check to see
5810 if the token after the `)' is a `{'. If so, we are not
5811 looking at a cast-expression.
5812
5813 Save tokens so that we can put them back. */
5814 cp_lexer_save_tokens (parser->lexer);
5815 /* Skip tokens until the next token is a closing parenthesis.
5816 If we find the closing `)', and the next token is a `{', then
5817 we are looking at a compound-literal. */
5818 compound_literal_p
5819 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5820 /*consume_paren=*/true)
5821 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5822 /* Roll back the tokens we skipped. */
5823 cp_lexer_rollback_tokens (parser->lexer);
5824 /* If we were looking at a compound-literal, simulate an error
5825 so that the call to cp_parser_parse_definitely below will
5826 fail. */
5827 if (compound_literal_p)
5828 cp_parser_simulate_error (parser);
5829 else
5830 {
5831 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5832 parser->in_type_id_in_expr_p = true;
5833 /* Look for the type-id. */
5834 type = cp_parser_type_id (parser);
5835 /* Look for the closing `)'. */
5836 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5837 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5838 }
5839
5840 /* Restore the saved message. */
5841 parser->type_definition_forbidden_message = saved_message;
5842
5843 /* If ok so far, parse the dependent expression. We cannot be
5844 sure it is a cast. Consider `(T ())'. It is a parenthesized
5845 ctor of T, but looks like a cast to function returning T
5846 without a dependent expression. */
5847 if (!cp_parser_error_occurred (parser))
5848 expr = cp_parser_cast_expression (parser,
5849 /*address_p=*/false,
5850 /*cast_p=*/true);
5851
5852 if (cp_parser_parse_definitely (parser))
5853 {
5854 /* Warn about old-style casts, if so requested. */
5855 if (warn_old_style_cast
5856 && !in_system_header
5857 && !VOID_TYPE_P (type)
5858 && current_lang_name != lang_name_c)
5859 warning (OPT_Wold_style_cast, "use of old-style cast");
5860
5861 /* Only type conversions to integral or enumeration types
5862 can be used in constant-expressions. */
5863 if (!cast_valid_in_integral_constant_expression_p (type)
5864 && (cp_parser_non_integral_constant_expression
5865 (parser,
5866 "a cast to a type other than an integral or "
5867 "enumeration type")))
5868 return error_mark_node;
5869
5870 /* Perform the cast. */
5871 expr = build_c_cast (type, expr);
5872 return expr;
5873 }
5874 }
5875
5876 /* If we get here, then it's not a cast, so it must be a
5877 unary-expression. */
5878 return cp_parser_unary_expression (parser, address_p, cast_p);
5879 }
5880
5881 /* Parse a binary expression of the general form:
5882
5883 pm-expression:
5884 cast-expression
5885 pm-expression .* cast-expression
5886 pm-expression ->* cast-expression
5887
5888 multiplicative-expression:
5889 pm-expression
5890 multiplicative-expression * pm-expression
5891 multiplicative-expression / pm-expression
5892 multiplicative-expression % pm-expression
5893
5894 additive-expression:
5895 multiplicative-expression
5896 additive-expression + multiplicative-expression
5897 additive-expression - multiplicative-expression
5898
5899 shift-expression:
5900 additive-expression
5901 shift-expression << additive-expression
5902 shift-expression >> additive-expression
5903
5904 relational-expression:
5905 shift-expression
5906 relational-expression < shift-expression
5907 relational-expression > shift-expression
5908 relational-expression <= shift-expression
5909 relational-expression >= shift-expression
5910
5911 GNU Extension:
5912
5913 relational-expression:
5914 relational-expression <? shift-expression
5915 relational-expression >? shift-expression
5916
5917 equality-expression:
5918 relational-expression
5919 equality-expression == relational-expression
5920 equality-expression != relational-expression
5921
5922 and-expression:
5923 equality-expression
5924 and-expression & equality-expression
5925
5926 exclusive-or-expression:
5927 and-expression
5928 exclusive-or-expression ^ and-expression
5929
5930 inclusive-or-expression:
5931 exclusive-or-expression
5932 inclusive-or-expression | exclusive-or-expression
5933
5934 logical-and-expression:
5935 inclusive-or-expression
5936 logical-and-expression && inclusive-or-expression
5937
5938 logical-or-expression:
5939 logical-and-expression
5940 logical-or-expression || logical-and-expression
5941
5942 All these are implemented with a single function like:
5943
5944 binary-expression:
5945 simple-cast-expression
5946 binary-expression <token> binary-expression
5947
5948 CAST_P is true if this expression is the target of a cast.
5949
5950 The binops_by_token map is used to get the tree codes for each <token> type.
5951 binary-expressions are associated according to a precedence table. */
5952
5953 #define TOKEN_PRECEDENCE(token) \
5954 (((token->type == CPP_GREATER \
5955 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5956 && !parser->greater_than_is_operator_p) \
5957 ? PREC_NOT_OPERATOR \
5958 : binops_by_token[token->type].prec)
5959
5960 static tree
5961 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5962 {
5963 cp_parser_expression_stack stack;
5964 cp_parser_expression_stack_entry *sp = &stack[0];
5965 tree lhs, rhs;
5966 cp_token *token;
5967 enum tree_code tree_type, lhs_type, rhs_type;
5968 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5969 bool overloaded_p;
5970
5971 /* Parse the first expression. */
5972 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5973 lhs_type = ERROR_MARK;
5974
5975 for (;;)
5976 {
5977 /* Get an operator token. */
5978 token = cp_lexer_peek_token (parser->lexer);
5979
5980 if (warn_cxx0x_compat
5981 && token->type == CPP_RSHIFT
5982 && !parser->greater_than_is_operator_p)
5983 {
5984 warning (OPT_Wc__0x_compat,
5985 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5986 &token->location);
5987 warning (OPT_Wc__0x_compat,
5988 "suggest parentheses around %<>>%> expression");
5989 }
5990
5991 new_prec = TOKEN_PRECEDENCE (token);
5992
5993 /* Popping an entry off the stack means we completed a subexpression:
5994 - either we found a token which is not an operator (`>' where it is not
5995 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5996 will happen repeatedly;
5997 - or, we found an operator which has lower priority. This is the case
5998 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5999 parsing `3 * 4'. */
6000 if (new_prec <= prec)
6001 {
6002 if (sp == stack)
6003 break;
6004 else
6005 goto pop;
6006 }
6007
6008 get_rhs:
6009 tree_type = binops_by_token[token->type].tree_type;
6010
6011 /* We used the operator token. */
6012 cp_lexer_consume_token (parser->lexer);
6013
6014 /* Extract another operand. It may be the RHS of this expression
6015 or the LHS of a new, higher priority expression. */
6016 rhs = cp_parser_simple_cast_expression (parser);
6017 rhs_type = ERROR_MARK;
6018
6019 /* Get another operator token. Look up its precedence to avoid
6020 building a useless (immediately popped) stack entry for common
6021 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6022 token = cp_lexer_peek_token (parser->lexer);
6023 lookahead_prec = TOKEN_PRECEDENCE (token);
6024 if (lookahead_prec > new_prec)
6025 {
6026 /* ... and prepare to parse the RHS of the new, higher priority
6027 expression. Since precedence levels on the stack are
6028 monotonically increasing, we do not have to care about
6029 stack overflows. */
6030 sp->prec = prec;
6031 sp->tree_type = tree_type;
6032 sp->lhs = lhs;
6033 sp->lhs_type = lhs_type;
6034 sp++;
6035 lhs = rhs;
6036 lhs_type = rhs_type;
6037 prec = new_prec;
6038 new_prec = lookahead_prec;
6039 goto get_rhs;
6040
6041 pop:
6042 /* If the stack is not empty, we have parsed into LHS the right side
6043 (`4' in the example above) of an expression we had suspended.
6044 We can use the information on the stack to recover the LHS (`3')
6045 from the stack together with the tree code (`MULT_EXPR'), and
6046 the precedence of the higher level subexpression
6047 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6048 which will be used to actually build the additive expression. */
6049 --sp;
6050 prec = sp->prec;
6051 tree_type = sp->tree_type;
6052 rhs = lhs;
6053 rhs_type = lhs_type;
6054 lhs = sp->lhs;
6055 lhs_type = sp->lhs_type;
6056 }
6057
6058 overloaded_p = false;
6059 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6060 &overloaded_p);
6061 lhs_type = tree_type;
6062
6063 /* If the binary operator required the use of an overloaded operator,
6064 then this expression cannot be an integral constant-expression.
6065 An overloaded operator can be used even if both operands are
6066 otherwise permissible in an integral constant-expression if at
6067 least one of the operands is of enumeration type. */
6068
6069 if (overloaded_p
6070 && (cp_parser_non_integral_constant_expression
6071 (parser, "calls to overloaded operators")))
6072 return error_mark_node;
6073 }
6074
6075 return lhs;
6076 }
6077
6078
6079 /* Parse the `? expression : assignment-expression' part of a
6080 conditional-expression. The LOGICAL_OR_EXPR is the
6081 logical-or-expression that started the conditional-expression.
6082 Returns a representation of the entire conditional-expression.
6083
6084 This routine is used by cp_parser_assignment_expression.
6085
6086 ? expression : assignment-expression
6087
6088 GNU Extensions:
6089
6090 ? : assignment-expression */
6091
6092 static tree
6093 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6094 {
6095 tree expr;
6096 tree assignment_expr;
6097
6098 /* Consume the `?' token. */
6099 cp_lexer_consume_token (parser->lexer);
6100 if (cp_parser_allow_gnu_extensions_p (parser)
6101 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6102 /* Implicit true clause. */
6103 expr = NULL_TREE;
6104 else
6105 /* Parse the expression. */
6106 expr = cp_parser_expression (parser, /*cast_p=*/false);
6107
6108 /* The next token should be a `:'. */
6109 cp_parser_require (parser, CPP_COLON, "`:'");
6110 /* Parse the assignment-expression. */
6111 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6112
6113 /* Build the conditional-expression. */
6114 return build_x_conditional_expr (logical_or_expr,
6115 expr,
6116 assignment_expr);
6117 }
6118
6119 /* Parse an assignment-expression.
6120
6121 assignment-expression:
6122 conditional-expression
6123 logical-or-expression assignment-operator assignment_expression
6124 throw-expression
6125
6126 CAST_P is true if this expression is the target of a cast.
6127
6128 Returns a representation for the expression. */
6129
6130 static tree
6131 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6132 {
6133 tree expr;
6134
6135 /* If the next token is the `throw' keyword, then we're looking at
6136 a throw-expression. */
6137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6138 expr = cp_parser_throw_expression (parser);
6139 /* Otherwise, it must be that we are looking at a
6140 logical-or-expression. */
6141 else
6142 {
6143 /* Parse the binary expressions (logical-or-expression). */
6144 expr = cp_parser_binary_expression (parser, cast_p);
6145 /* If the next token is a `?' then we're actually looking at a
6146 conditional-expression. */
6147 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6148 return cp_parser_question_colon_clause (parser, expr);
6149 else
6150 {
6151 enum tree_code assignment_operator;
6152
6153 /* If it's an assignment-operator, we're using the second
6154 production. */
6155 assignment_operator
6156 = cp_parser_assignment_operator_opt (parser);
6157 if (assignment_operator != ERROR_MARK)
6158 {
6159 tree rhs;
6160
6161 /* Parse the right-hand side of the assignment. */
6162 rhs = cp_parser_assignment_expression (parser, cast_p);
6163 /* An assignment may not appear in a
6164 constant-expression. */
6165 if (cp_parser_non_integral_constant_expression (parser,
6166 "an assignment"))
6167 return error_mark_node;
6168 /* Build the assignment expression. */
6169 expr = build_x_modify_expr (expr,
6170 assignment_operator,
6171 rhs);
6172 }
6173 }
6174 }
6175
6176 return expr;
6177 }
6178
6179 /* Parse an (optional) assignment-operator.
6180
6181 assignment-operator: one of
6182 = *= /= %= += -= >>= <<= &= ^= |=
6183
6184 GNU Extension:
6185
6186 assignment-operator: one of
6187 <?= >?=
6188
6189 If the next token is an assignment operator, the corresponding tree
6190 code is returned, and the token is consumed. For example, for
6191 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6192 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6193 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6194 operator, ERROR_MARK is returned. */
6195
6196 static enum tree_code
6197 cp_parser_assignment_operator_opt (cp_parser* parser)
6198 {
6199 enum tree_code op;
6200 cp_token *token;
6201
6202 /* Peek at the next toen. */
6203 token = cp_lexer_peek_token (parser->lexer);
6204
6205 switch (token->type)
6206 {
6207 case CPP_EQ:
6208 op = NOP_EXPR;
6209 break;
6210
6211 case CPP_MULT_EQ:
6212 op = MULT_EXPR;
6213 break;
6214
6215 case CPP_DIV_EQ:
6216 op = TRUNC_DIV_EXPR;
6217 break;
6218
6219 case CPP_MOD_EQ:
6220 op = TRUNC_MOD_EXPR;
6221 break;
6222
6223 case CPP_PLUS_EQ:
6224 op = PLUS_EXPR;
6225 break;
6226
6227 case CPP_MINUS_EQ:
6228 op = MINUS_EXPR;
6229 break;
6230
6231 case CPP_RSHIFT_EQ:
6232 op = RSHIFT_EXPR;
6233 break;
6234
6235 case CPP_LSHIFT_EQ:
6236 op = LSHIFT_EXPR;
6237 break;
6238
6239 case CPP_AND_EQ:
6240 op = BIT_AND_EXPR;
6241 break;
6242
6243 case CPP_XOR_EQ:
6244 op = BIT_XOR_EXPR;
6245 break;
6246
6247 case CPP_OR_EQ:
6248 op = BIT_IOR_EXPR;
6249 break;
6250
6251 default:
6252 /* Nothing else is an assignment operator. */
6253 op = ERROR_MARK;
6254 }
6255
6256 /* If it was an assignment operator, consume it. */
6257 if (op != ERROR_MARK)
6258 cp_lexer_consume_token (parser->lexer);
6259
6260 return op;
6261 }
6262
6263 /* Parse an expression.
6264
6265 expression:
6266 assignment-expression
6267 expression , assignment-expression
6268
6269 CAST_P is true if this expression is the target of a cast.
6270
6271 Returns a representation of the expression. */
6272
6273 static tree
6274 cp_parser_expression (cp_parser* parser, bool cast_p)
6275 {
6276 tree expression = NULL_TREE;
6277
6278 while (true)
6279 {
6280 tree assignment_expression;
6281
6282 /* Parse the next assignment-expression. */
6283 assignment_expression
6284 = cp_parser_assignment_expression (parser, cast_p);
6285 /* If this is the first assignment-expression, we can just
6286 save it away. */
6287 if (!expression)
6288 expression = assignment_expression;
6289 else
6290 expression = build_x_compound_expr (expression,
6291 assignment_expression);
6292 /* If the next token is not a comma, then we are done with the
6293 expression. */
6294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6295 break;
6296 /* Consume the `,'. */
6297 cp_lexer_consume_token (parser->lexer);
6298 /* A comma operator cannot appear in a constant-expression. */
6299 if (cp_parser_non_integral_constant_expression (parser,
6300 "a comma operator"))
6301 expression = error_mark_node;
6302 }
6303
6304 return expression;
6305 }
6306
6307 /* Parse a constant-expression.
6308
6309 constant-expression:
6310 conditional-expression
6311
6312 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6313 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6314 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6315 is false, NON_CONSTANT_P should be NULL. */
6316
6317 static tree
6318 cp_parser_constant_expression (cp_parser* parser,
6319 bool allow_non_constant_p,
6320 bool *non_constant_p)
6321 {
6322 bool saved_integral_constant_expression_p;
6323 bool saved_allow_non_integral_constant_expression_p;
6324 bool saved_non_integral_constant_expression_p;
6325 tree expression;
6326
6327 /* It might seem that we could simply parse the
6328 conditional-expression, and then check to see if it were
6329 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6330 one that the compiler can figure out is constant, possibly after
6331 doing some simplifications or optimizations. The standard has a
6332 precise definition of constant-expression, and we must honor
6333 that, even though it is somewhat more restrictive.
6334
6335 For example:
6336
6337 int i[(2, 3)];
6338
6339 is not a legal declaration, because `(2, 3)' is not a
6340 constant-expression. The `,' operator is forbidden in a
6341 constant-expression. However, GCC's constant-folding machinery
6342 will fold this operation to an INTEGER_CST for `3'. */
6343
6344 /* Save the old settings. */
6345 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6346 saved_allow_non_integral_constant_expression_p
6347 = parser->allow_non_integral_constant_expression_p;
6348 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6349 /* We are now parsing a constant-expression. */
6350 parser->integral_constant_expression_p = true;
6351 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6352 parser->non_integral_constant_expression_p = false;
6353 /* Although the grammar says "conditional-expression", we parse an
6354 "assignment-expression", which also permits "throw-expression"
6355 and the use of assignment operators. In the case that
6356 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6357 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6358 actually essential that we look for an assignment-expression.
6359 For example, cp_parser_initializer_clauses uses this function to
6360 determine whether a particular assignment-expression is in fact
6361 constant. */
6362 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6363 /* Restore the old settings. */
6364 parser->integral_constant_expression_p
6365 = saved_integral_constant_expression_p;
6366 parser->allow_non_integral_constant_expression_p
6367 = saved_allow_non_integral_constant_expression_p;
6368 if (allow_non_constant_p)
6369 *non_constant_p = parser->non_integral_constant_expression_p;
6370 else if (parser->non_integral_constant_expression_p)
6371 expression = error_mark_node;
6372 parser->non_integral_constant_expression_p
6373 = saved_non_integral_constant_expression_p;
6374
6375 return expression;
6376 }
6377
6378 /* Parse __builtin_offsetof.
6379
6380 offsetof-expression:
6381 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6382
6383 offsetof-member-designator:
6384 id-expression
6385 | offsetof-member-designator "." id-expression
6386 | offsetof-member-designator "[" expression "]" */
6387
6388 static tree
6389 cp_parser_builtin_offsetof (cp_parser *parser)
6390 {
6391 int save_ice_p, save_non_ice_p;
6392 tree type, expr;
6393 cp_id_kind dummy;
6394
6395 /* We're about to accept non-integral-constant things, but will
6396 definitely yield an integral constant expression. Save and
6397 restore these values around our local parsing. */
6398 save_ice_p = parser->integral_constant_expression_p;
6399 save_non_ice_p = parser->non_integral_constant_expression_p;
6400
6401 /* Consume the "__builtin_offsetof" token. */
6402 cp_lexer_consume_token (parser->lexer);
6403 /* Consume the opening `('. */
6404 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6405 /* Parse the type-id. */
6406 type = cp_parser_type_id (parser);
6407 /* Look for the `,'. */
6408 cp_parser_require (parser, CPP_COMMA, "`,'");
6409
6410 /* Build the (type *)null that begins the traditional offsetof macro. */
6411 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6412
6413 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6414 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6415 true, &dummy);
6416 while (true)
6417 {
6418 cp_token *token = cp_lexer_peek_token (parser->lexer);
6419 switch (token->type)
6420 {
6421 case CPP_OPEN_SQUARE:
6422 /* offsetof-member-designator "[" expression "]" */
6423 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6424 break;
6425
6426 case CPP_DOT:
6427 /* offsetof-member-designator "." identifier */
6428 cp_lexer_consume_token (parser->lexer);
6429 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6430 true, &dummy);
6431 break;
6432
6433 case CPP_CLOSE_PAREN:
6434 /* Consume the ")" token. */
6435 cp_lexer_consume_token (parser->lexer);
6436 goto success;
6437
6438 default:
6439 /* Error. We know the following require will fail, but
6440 that gives the proper error message. */
6441 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6442 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6443 expr = error_mark_node;
6444 goto failure;
6445 }
6446 }
6447
6448 success:
6449 /* If we're processing a template, we can't finish the semantics yet.
6450 Otherwise we can fold the entire expression now. */
6451 if (processing_template_decl)
6452 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6453 else
6454 expr = finish_offsetof (expr);
6455
6456 failure:
6457 parser->integral_constant_expression_p = save_ice_p;
6458 parser->non_integral_constant_expression_p = save_non_ice_p;
6459
6460 return expr;
6461 }
6462
6463 /* Parse a trait expression. */
6464
6465 static tree
6466 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6467 {
6468 cp_trait_kind kind;
6469 tree type1, type2 = NULL_TREE;
6470 bool binary = false;
6471 cp_decl_specifier_seq decl_specs;
6472
6473 switch (keyword)
6474 {
6475 case RID_HAS_NOTHROW_ASSIGN:
6476 kind = CPTK_HAS_NOTHROW_ASSIGN;
6477 break;
6478 case RID_HAS_NOTHROW_CONSTRUCTOR:
6479 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6480 break;
6481 case RID_HAS_NOTHROW_COPY:
6482 kind = CPTK_HAS_NOTHROW_COPY;
6483 break;
6484 case RID_HAS_TRIVIAL_ASSIGN:
6485 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6486 break;
6487 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6488 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6489 break;
6490 case RID_HAS_TRIVIAL_COPY:
6491 kind = CPTK_HAS_TRIVIAL_COPY;
6492 break;
6493 case RID_HAS_TRIVIAL_DESTRUCTOR:
6494 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6495 break;
6496 case RID_HAS_VIRTUAL_DESTRUCTOR:
6497 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6498 break;
6499 case RID_IS_ABSTRACT:
6500 kind = CPTK_IS_ABSTRACT;
6501 break;
6502 case RID_IS_BASE_OF:
6503 kind = CPTK_IS_BASE_OF;
6504 binary = true;
6505 break;
6506 case RID_IS_CLASS:
6507 kind = CPTK_IS_CLASS;
6508 break;
6509 case RID_IS_CONVERTIBLE_TO:
6510 kind = CPTK_IS_CONVERTIBLE_TO;
6511 binary = true;
6512 break;
6513 case RID_IS_EMPTY:
6514 kind = CPTK_IS_EMPTY;
6515 break;
6516 case RID_IS_ENUM:
6517 kind = CPTK_IS_ENUM;
6518 break;
6519 case RID_IS_POD:
6520 kind = CPTK_IS_POD;
6521 break;
6522 case RID_IS_POLYMORPHIC:
6523 kind = CPTK_IS_POLYMORPHIC;
6524 break;
6525 case RID_IS_UNION:
6526 kind = CPTK_IS_UNION;
6527 break;
6528 default:
6529 gcc_unreachable ();
6530 }
6531
6532 /* Consume the token. */
6533 cp_lexer_consume_token (parser->lexer);
6534
6535 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6536
6537 type1 = cp_parser_type_id (parser);
6538
6539 if (type1 == error_mark_node)
6540 return error_mark_node;
6541
6542 /* Build a trivial decl-specifier-seq. */
6543 clear_decl_specs (&decl_specs);
6544 decl_specs.type = type1;
6545
6546 /* Call grokdeclarator to figure out what type this is. */
6547 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6548 /*initialized=*/0, /*attrlist=*/NULL);
6549
6550 if (binary)
6551 {
6552 cp_parser_require (parser, CPP_COMMA, "`,'");
6553
6554 type2 = cp_parser_type_id (parser);
6555
6556 if (type2 == error_mark_node)
6557 return error_mark_node;
6558
6559 /* Build a trivial decl-specifier-seq. */
6560 clear_decl_specs (&decl_specs);
6561 decl_specs.type = type2;
6562
6563 /* Call grokdeclarator to figure out what type this is. */
6564 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6565 /*initialized=*/0, /*attrlist=*/NULL);
6566 }
6567
6568 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6569
6570 /* Complete the trait expression, which may mean either processing
6571 the trait expr now or saving it for template instantiation. */
6572 return finish_trait_expr (kind, type1, type2);
6573 }
6574
6575 /* Statements [gram.stmt.stmt] */
6576
6577 /* Parse a statement.
6578
6579 statement:
6580 labeled-statement
6581 expression-statement
6582 compound-statement
6583 selection-statement
6584 iteration-statement
6585 jump-statement
6586 declaration-statement
6587 try-block
6588
6589 IN_COMPOUND is true when the statement is nested inside a
6590 cp_parser_compound_statement; this matters for certain pragmas.
6591
6592 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6593 is a (possibly labeled) if statement which is not enclosed in braces
6594 and has an else clause. This is used to implement -Wparentheses. */
6595
6596 static void
6597 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6598 bool in_compound, bool *if_p)
6599 {
6600 tree statement;
6601 cp_token *token;
6602 location_t statement_location;
6603
6604 restart:
6605 if (if_p != NULL)
6606 *if_p = false;
6607 /* There is no statement yet. */
6608 statement = NULL_TREE;
6609 /* Peek at the next token. */
6610 token = cp_lexer_peek_token (parser->lexer);
6611 /* Remember the location of the first token in the statement. */
6612 statement_location = token->location;
6613 /* If this is a keyword, then that will often determine what kind of
6614 statement we have. */
6615 if (token->type == CPP_KEYWORD)
6616 {
6617 enum rid keyword = token->keyword;
6618
6619 switch (keyword)
6620 {
6621 case RID_CASE:
6622 case RID_DEFAULT:
6623 /* Looks like a labeled-statement with a case label.
6624 Parse the label, and then use tail recursion to parse
6625 the statement. */
6626 cp_parser_label_for_labeled_statement (parser);
6627 goto restart;
6628
6629 case RID_IF:
6630 case RID_SWITCH:
6631 statement = cp_parser_selection_statement (parser, if_p);
6632 break;
6633
6634 case RID_WHILE:
6635 case RID_DO:
6636 case RID_FOR:
6637 statement = cp_parser_iteration_statement (parser);
6638 break;
6639
6640 case RID_BREAK:
6641 case RID_CONTINUE:
6642 case RID_RETURN:
6643 case RID_GOTO:
6644 statement = cp_parser_jump_statement (parser);
6645 break;
6646
6647 /* Objective-C++ exception-handling constructs. */
6648 case RID_AT_TRY:
6649 case RID_AT_CATCH:
6650 case RID_AT_FINALLY:
6651 case RID_AT_SYNCHRONIZED:
6652 case RID_AT_THROW:
6653 statement = cp_parser_objc_statement (parser);
6654 break;
6655
6656 case RID_TRY:
6657 statement = cp_parser_try_block (parser);
6658 break;
6659
6660 case RID_NAMESPACE:
6661 /* This must be a namespace alias definition. */
6662 cp_parser_declaration_statement (parser);
6663 return;
6664
6665 default:
6666 /* It might be a keyword like `int' that can start a
6667 declaration-statement. */
6668 break;
6669 }
6670 }
6671 else if (token->type == CPP_NAME)
6672 {
6673 /* If the next token is a `:', then we are looking at a
6674 labeled-statement. */
6675 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6676 if (token->type == CPP_COLON)
6677 {
6678 /* Looks like a labeled-statement with an ordinary label.
6679 Parse the label, and then use tail recursion to parse
6680 the statement. */
6681 cp_parser_label_for_labeled_statement (parser);
6682 goto restart;
6683 }
6684 }
6685 /* Anything that starts with a `{' must be a compound-statement. */
6686 else if (token->type == CPP_OPEN_BRACE)
6687 statement = cp_parser_compound_statement (parser, NULL, false);
6688 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6689 a statement all its own. */
6690 else if (token->type == CPP_PRAGMA)
6691 {
6692 /* Only certain OpenMP pragmas are attached to statements, and thus
6693 are considered statements themselves. All others are not. In
6694 the context of a compound, accept the pragma as a "statement" and
6695 return so that we can check for a close brace. Otherwise we
6696 require a real statement and must go back and read one. */
6697 if (in_compound)
6698 cp_parser_pragma (parser, pragma_compound);
6699 else if (!cp_parser_pragma (parser, pragma_stmt))
6700 goto restart;
6701 return;
6702 }
6703 else if (token->type == CPP_EOF)
6704 {
6705 cp_parser_error (parser, "expected statement");
6706 return;
6707 }
6708
6709 /* Everything else must be a declaration-statement or an
6710 expression-statement. Try for the declaration-statement
6711 first, unless we are looking at a `;', in which case we know that
6712 we have an expression-statement. */
6713 if (!statement)
6714 {
6715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6716 {
6717 cp_parser_parse_tentatively (parser);
6718 /* Try to parse the declaration-statement. */
6719 cp_parser_declaration_statement (parser);
6720 /* If that worked, we're done. */
6721 if (cp_parser_parse_definitely (parser))
6722 return;
6723 }
6724 /* Look for an expression-statement instead. */
6725 statement = cp_parser_expression_statement (parser, in_statement_expr);
6726 }
6727
6728 /* Set the line number for the statement. */
6729 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6730 SET_EXPR_LOCATION (statement, statement_location);
6731 }
6732
6733 /* Parse the label for a labeled-statement, i.e.
6734
6735 identifier :
6736 case constant-expression :
6737 default :
6738
6739 GNU Extension:
6740 case constant-expression ... constant-expression : statement
6741
6742 When a label is parsed without errors, the label is added to the
6743 parse tree by the finish_* functions, so this function doesn't
6744 have to return the label. */
6745
6746 static void
6747 cp_parser_label_for_labeled_statement (cp_parser* parser)
6748 {
6749 cp_token *token;
6750
6751 /* The next token should be an identifier. */
6752 token = cp_lexer_peek_token (parser->lexer);
6753 if (token->type != CPP_NAME
6754 && token->type != CPP_KEYWORD)
6755 {
6756 cp_parser_error (parser, "expected labeled-statement");
6757 return;
6758 }
6759
6760 switch (token->keyword)
6761 {
6762 case RID_CASE:
6763 {
6764 tree expr, expr_hi;
6765 cp_token *ellipsis;
6766
6767 /* Consume the `case' token. */
6768 cp_lexer_consume_token (parser->lexer);
6769 /* Parse the constant-expression. */
6770 expr = cp_parser_constant_expression (parser,
6771 /*allow_non_constant_p=*/false,
6772 NULL);
6773
6774 ellipsis = cp_lexer_peek_token (parser->lexer);
6775 if (ellipsis->type == CPP_ELLIPSIS)
6776 {
6777 /* Consume the `...' token. */
6778 cp_lexer_consume_token (parser->lexer);
6779 expr_hi =
6780 cp_parser_constant_expression (parser,
6781 /*allow_non_constant_p=*/false,
6782 NULL);
6783 /* We don't need to emit warnings here, as the common code
6784 will do this for us. */
6785 }
6786 else
6787 expr_hi = NULL_TREE;
6788
6789 if (parser->in_switch_statement_p)
6790 finish_case_label (expr, expr_hi);
6791 else
6792 error ("case label %qE not within a switch statement", expr);
6793 }
6794 break;
6795
6796 case RID_DEFAULT:
6797 /* Consume the `default' token. */
6798 cp_lexer_consume_token (parser->lexer);
6799
6800 if (parser->in_switch_statement_p)
6801 finish_case_label (NULL_TREE, NULL_TREE);
6802 else
6803 error ("case label not within a switch statement");
6804 break;
6805
6806 default:
6807 /* Anything else must be an ordinary label. */
6808 finish_label_stmt (cp_parser_identifier (parser));
6809 break;
6810 }
6811
6812 /* Require the `:' token. */
6813 cp_parser_require (parser, CPP_COLON, "`:'");
6814 }
6815
6816 /* Parse an expression-statement.
6817
6818 expression-statement:
6819 expression [opt] ;
6820
6821 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6822 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6823 indicates whether this expression-statement is part of an
6824 expression statement. */
6825
6826 static tree
6827 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6828 {
6829 tree statement = NULL_TREE;
6830
6831 /* If the next token is a ';', then there is no expression
6832 statement. */
6833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6834 statement = cp_parser_expression (parser, /*cast_p=*/false);
6835
6836 /* Consume the final `;'. */
6837 cp_parser_consume_semicolon_at_end_of_statement (parser);
6838
6839 if (in_statement_expr
6840 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6841 /* This is the final expression statement of a statement
6842 expression. */
6843 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6844 else if (statement)
6845 statement = finish_expr_stmt (statement);
6846 else
6847 finish_stmt ();
6848
6849 return statement;
6850 }
6851
6852 /* Parse a compound-statement.
6853
6854 compound-statement:
6855 { statement-seq [opt] }
6856
6857 GNU extension:
6858
6859 compound-statement:
6860 { label-declaration-seq [opt] statement-seq [opt] }
6861
6862 label-declaration-seq:
6863 label-declaration
6864 label-declaration-seq label-declaration
6865
6866 Returns a tree representing the statement. */
6867
6868 static tree
6869 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6870 bool in_try)
6871 {
6872 tree compound_stmt;
6873
6874 /* Consume the `{'. */
6875 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6876 return error_mark_node;
6877 /* Begin the compound-statement. */
6878 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6879 /* If the next keyword is `__label__' we have a label declaration. */
6880 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6881 cp_parser_label_declaration (parser);
6882 /* Parse an (optional) statement-seq. */
6883 cp_parser_statement_seq_opt (parser, in_statement_expr);
6884 /* Finish the compound-statement. */
6885 finish_compound_stmt (compound_stmt);
6886 /* Consume the `}'. */
6887 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6888
6889 return compound_stmt;
6890 }
6891
6892 /* Parse an (optional) statement-seq.
6893
6894 statement-seq:
6895 statement
6896 statement-seq [opt] statement */
6897
6898 static void
6899 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6900 {
6901 /* Scan statements until there aren't any more. */
6902 while (true)
6903 {
6904 cp_token *token = cp_lexer_peek_token (parser->lexer);
6905
6906 /* If we're looking at a `}', then we've run out of statements. */
6907 if (token->type == CPP_CLOSE_BRACE
6908 || token->type == CPP_EOF
6909 || token->type == CPP_PRAGMA_EOL)
6910 break;
6911
6912 /* If we are in a compound statement and find 'else' then
6913 something went wrong. */
6914 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6915 {
6916 if (parser->in_statement & IN_IF_STMT)
6917 break;
6918 else
6919 {
6920 token = cp_lexer_consume_token (parser->lexer);
6921 error ("%<else%> without a previous %<if%>");
6922 }
6923 }
6924
6925 /* Parse the statement. */
6926 cp_parser_statement (parser, in_statement_expr, true, NULL);
6927 }
6928 }
6929
6930 /* Parse a selection-statement.
6931
6932 selection-statement:
6933 if ( condition ) statement
6934 if ( condition ) statement else statement
6935 switch ( condition ) statement
6936
6937 Returns the new IF_STMT or SWITCH_STMT.
6938
6939 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6940 is a (possibly labeled) if statement which is not enclosed in
6941 braces and has an else clause. This is used to implement
6942 -Wparentheses. */
6943
6944 static tree
6945 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6946 {
6947 cp_token *token;
6948 enum rid keyword;
6949
6950 if (if_p != NULL)
6951 *if_p = false;
6952
6953 /* Peek at the next token. */
6954 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6955
6956 /* See what kind of keyword it is. */
6957 keyword = token->keyword;
6958 switch (keyword)
6959 {
6960 case RID_IF:
6961 case RID_SWITCH:
6962 {
6963 tree statement;
6964 tree condition;
6965
6966 /* Look for the `('. */
6967 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6968 {
6969 cp_parser_skip_to_end_of_statement (parser);
6970 return error_mark_node;
6971 }
6972
6973 /* Begin the selection-statement. */
6974 if (keyword == RID_IF)
6975 statement = begin_if_stmt ();
6976 else
6977 statement = begin_switch_stmt ();
6978
6979 /* Parse the condition. */
6980 condition = cp_parser_condition (parser);
6981 /* Look for the `)'. */
6982 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6983 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6984 /*consume_paren=*/true);
6985
6986 if (keyword == RID_IF)
6987 {
6988 bool nested_if;
6989 unsigned char in_statement;
6990
6991 /* Add the condition. */
6992 finish_if_stmt_cond (condition, statement);
6993
6994 /* Parse the then-clause. */
6995 in_statement = parser->in_statement;
6996 parser->in_statement |= IN_IF_STMT;
6997 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6998 parser->in_statement = in_statement;
6999
7000 finish_then_clause (statement);
7001
7002 /* If the next token is `else', parse the else-clause. */
7003 if (cp_lexer_next_token_is_keyword (parser->lexer,
7004 RID_ELSE))
7005 {
7006 /* Consume the `else' keyword. */
7007 cp_lexer_consume_token (parser->lexer);
7008 begin_else_clause (statement);
7009 /* Parse the else-clause. */
7010 cp_parser_implicitly_scoped_statement (parser, NULL);
7011 finish_else_clause (statement);
7012
7013 /* If we are currently parsing a then-clause, then
7014 IF_P will not be NULL. We set it to true to
7015 indicate that this if statement has an else clause.
7016 This may trigger the Wparentheses warning below
7017 when we get back up to the parent if statement. */
7018 if (if_p != NULL)
7019 *if_p = true;
7020 }
7021 else
7022 {
7023 /* This if statement does not have an else clause. If
7024 NESTED_IF is true, then the then-clause is an if
7025 statement which does have an else clause. We warn
7026 about the potential ambiguity. */
7027 if (nested_if)
7028 warning (OPT_Wparentheses,
7029 ("%Hsuggest explicit braces "
7030 "to avoid ambiguous %<else%>"),
7031 EXPR_LOCUS (statement));
7032 }
7033
7034 /* Now we're all done with the if-statement. */
7035 finish_if_stmt (statement);
7036 }
7037 else
7038 {
7039 bool in_switch_statement_p;
7040 unsigned char in_statement;
7041
7042 /* Add the condition. */
7043 finish_switch_cond (condition, statement);
7044
7045 /* Parse the body of the switch-statement. */
7046 in_switch_statement_p = parser->in_switch_statement_p;
7047 in_statement = parser->in_statement;
7048 parser->in_switch_statement_p = true;
7049 parser->in_statement |= IN_SWITCH_STMT;
7050 cp_parser_implicitly_scoped_statement (parser, NULL);
7051 parser->in_switch_statement_p = in_switch_statement_p;
7052 parser->in_statement = in_statement;
7053
7054 /* Now we're all done with the switch-statement. */
7055 finish_switch_stmt (statement);
7056 }
7057
7058 return statement;
7059 }
7060 break;
7061
7062 default:
7063 cp_parser_error (parser, "expected selection-statement");
7064 return error_mark_node;
7065 }
7066 }
7067
7068 /* Parse a condition.
7069
7070 condition:
7071 expression
7072 type-specifier-seq declarator = assignment-expression
7073
7074 GNU Extension:
7075
7076 condition:
7077 type-specifier-seq declarator asm-specification [opt]
7078 attributes [opt] = assignment-expression
7079
7080 Returns the expression that should be tested. */
7081
7082 static tree
7083 cp_parser_condition (cp_parser* parser)
7084 {
7085 cp_decl_specifier_seq type_specifiers;
7086 const char *saved_message;
7087
7088 /* Try the declaration first. */
7089 cp_parser_parse_tentatively (parser);
7090 /* New types are not allowed in the type-specifier-seq for a
7091 condition. */
7092 saved_message = parser->type_definition_forbidden_message;
7093 parser->type_definition_forbidden_message
7094 = "types may not be defined in conditions";
7095 /* Parse the type-specifier-seq. */
7096 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7097 &type_specifiers);
7098 /* Restore the saved message. */
7099 parser->type_definition_forbidden_message = saved_message;
7100 /* If all is well, we might be looking at a declaration. */
7101 if (!cp_parser_error_occurred (parser))
7102 {
7103 tree decl;
7104 tree asm_specification;
7105 tree attributes;
7106 cp_declarator *declarator;
7107 tree initializer = NULL_TREE;
7108
7109 /* Parse the declarator. */
7110 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7111 /*ctor_dtor_or_conv_p=*/NULL,
7112 /*parenthesized_p=*/NULL,
7113 /*member_p=*/false);
7114 /* Parse the attributes. */
7115 attributes = cp_parser_attributes_opt (parser);
7116 /* Parse the asm-specification. */
7117 asm_specification = cp_parser_asm_specification_opt (parser);
7118 /* If the next token is not an `=', then we might still be
7119 looking at an expression. For example:
7120
7121 if (A(a).x)
7122
7123 looks like a decl-specifier-seq and a declarator -- but then
7124 there is no `=', so this is an expression. */
7125 cp_parser_require (parser, CPP_EQ, "`='");
7126 /* If we did see an `=', then we are looking at a declaration
7127 for sure. */
7128 if (cp_parser_parse_definitely (parser))
7129 {
7130 tree pushed_scope;
7131 bool non_constant_p;
7132
7133 /* Create the declaration. */
7134 decl = start_decl (declarator, &type_specifiers,
7135 /*initialized_p=*/true,
7136 attributes, /*prefix_attributes=*/NULL_TREE,
7137 &pushed_scope);
7138 /* Parse the assignment-expression. */
7139 initializer
7140 = cp_parser_constant_expression (parser,
7141 /*allow_non_constant_p=*/true,
7142 &non_constant_p);
7143 if (!non_constant_p)
7144 initializer = fold_non_dependent_expr (initializer);
7145
7146 /* Process the initializer. */
7147 cp_finish_decl (decl,
7148 initializer, !non_constant_p,
7149 asm_specification,
7150 LOOKUP_ONLYCONVERTING);
7151
7152 if (pushed_scope)
7153 pop_scope (pushed_scope);
7154
7155 return convert_from_reference (decl);
7156 }
7157 }
7158 /* If we didn't even get past the declarator successfully, we are
7159 definitely not looking at a declaration. */
7160 else
7161 cp_parser_abort_tentative_parse (parser);
7162
7163 /* Otherwise, we are looking at an expression. */
7164 return cp_parser_expression (parser, /*cast_p=*/false);
7165 }
7166
7167 /* We check for a ) immediately followed by ; with no whitespacing
7168 between. This is used to issue a warning for:
7169
7170 while (...);
7171
7172 and:
7173
7174 for (...);
7175
7176 as the semicolon is probably extraneous.
7177
7178 On parse errors, the next token might not be a ), so do nothing in
7179 that case. */
7180
7181 static void
7182 check_empty_body (cp_parser* parser, const char* type)
7183 {
7184 cp_token *token;
7185 cp_token *close_paren;
7186 expanded_location close_loc;
7187 expanded_location semi_loc;
7188
7189 close_paren = cp_lexer_peek_token (parser->lexer);
7190 if (close_paren->type != CPP_CLOSE_PAREN)
7191 return;
7192
7193 close_loc = expand_location (close_paren->location);
7194 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7195
7196 if (token->type != CPP_SEMICOLON
7197 || (token->flags & PREV_WHITE))
7198 return;
7199
7200 semi_loc = expand_location (token->location);
7201 if (close_loc.line == semi_loc.line
7202 && close_loc.column+1 == semi_loc.column)
7203 warning (OPT_Wempty_body,
7204 "suggest a space before %<;%> or explicit braces around empty "
7205 "body in %<%s%> statement",
7206 type);
7207 }
7208
7209 /* Parse an iteration-statement.
7210
7211 iteration-statement:
7212 while ( condition ) statement
7213 do statement while ( expression ) ;
7214 for ( for-init-statement condition [opt] ; expression [opt] )
7215 statement
7216
7217 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7218
7219 static tree
7220 cp_parser_iteration_statement (cp_parser* parser)
7221 {
7222 cp_token *token;
7223 enum rid keyword;
7224 tree statement;
7225 unsigned char in_statement;
7226
7227 /* Peek at the next token. */
7228 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7229 if (!token)
7230 return error_mark_node;
7231
7232 /* Remember whether or not we are already within an iteration
7233 statement. */
7234 in_statement = parser->in_statement;
7235
7236 /* See what kind of keyword it is. */
7237 keyword = token->keyword;
7238 switch (keyword)
7239 {
7240 case RID_WHILE:
7241 {
7242 tree condition;
7243
7244 /* Begin the while-statement. */
7245 statement = begin_while_stmt ();
7246 /* Look for the `('. */
7247 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7248 /* Parse the condition. */
7249 condition = cp_parser_condition (parser);
7250 finish_while_stmt_cond (condition, statement);
7251 check_empty_body (parser, "while");
7252 /* Look for the `)'. */
7253 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7254 /* Parse the dependent statement. */
7255 parser->in_statement = IN_ITERATION_STMT;
7256 cp_parser_already_scoped_statement (parser);
7257 parser->in_statement = in_statement;
7258 /* We're done with the while-statement. */
7259 finish_while_stmt (statement);
7260 }
7261 break;
7262
7263 case RID_DO:
7264 {
7265 tree expression;
7266
7267 /* Begin the do-statement. */
7268 statement = begin_do_stmt ();
7269 /* Parse the body of the do-statement. */
7270 parser->in_statement = IN_ITERATION_STMT;
7271 cp_parser_implicitly_scoped_statement (parser, NULL);
7272 parser->in_statement = in_statement;
7273 finish_do_body (statement);
7274 /* Look for the `while' keyword. */
7275 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7276 /* Look for the `('. */
7277 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7278 /* Parse the expression. */
7279 expression = cp_parser_expression (parser, /*cast_p=*/false);
7280 /* We're done with the do-statement. */
7281 finish_do_stmt (expression, statement);
7282 /* Look for the `)'. */
7283 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7284 /* Look for the `;'. */
7285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7286 }
7287 break;
7288
7289 case RID_FOR:
7290 {
7291 tree condition = NULL_TREE;
7292 tree expression = NULL_TREE;
7293
7294 /* Begin the for-statement. */
7295 statement = begin_for_stmt ();
7296 /* Look for the `('. */
7297 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7298 /* Parse the initialization. */
7299 cp_parser_for_init_statement (parser);
7300 finish_for_init_stmt (statement);
7301
7302 /* If there's a condition, process it. */
7303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7304 condition = cp_parser_condition (parser);
7305 finish_for_cond (condition, statement);
7306 /* Look for the `;'. */
7307 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7308
7309 /* If there's an expression, process it. */
7310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7311 expression = cp_parser_expression (parser, /*cast_p=*/false);
7312 finish_for_expr (expression, statement);
7313 check_empty_body (parser, "for");
7314 /* Look for the `)'. */
7315 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7316
7317 /* Parse the body of the for-statement. */
7318 parser->in_statement = IN_ITERATION_STMT;
7319 cp_parser_already_scoped_statement (parser);
7320 parser->in_statement = in_statement;
7321
7322 /* We're done with the for-statement. */
7323 finish_for_stmt (statement);
7324 }
7325 break;
7326
7327 default:
7328 cp_parser_error (parser, "expected iteration-statement");
7329 statement = error_mark_node;
7330 break;
7331 }
7332
7333 return statement;
7334 }
7335
7336 /* Parse a for-init-statement.
7337
7338 for-init-statement:
7339 expression-statement
7340 simple-declaration */
7341
7342 static void
7343 cp_parser_for_init_statement (cp_parser* parser)
7344 {
7345 /* If the next token is a `;', then we have an empty
7346 expression-statement. Grammatically, this is also a
7347 simple-declaration, but an invalid one, because it does not
7348 declare anything. Therefore, if we did not handle this case
7349 specially, we would issue an error message about an invalid
7350 declaration. */
7351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7352 {
7353 /* We're going to speculatively look for a declaration, falling back
7354 to an expression, if necessary. */
7355 cp_parser_parse_tentatively (parser);
7356 /* Parse the declaration. */
7357 cp_parser_simple_declaration (parser,
7358 /*function_definition_allowed_p=*/false);
7359 /* If the tentative parse failed, then we shall need to look for an
7360 expression-statement. */
7361 if (cp_parser_parse_definitely (parser))
7362 return;
7363 }
7364
7365 cp_parser_expression_statement (parser, false);
7366 }
7367
7368 /* Parse a jump-statement.
7369
7370 jump-statement:
7371 break ;
7372 continue ;
7373 return expression [opt] ;
7374 goto identifier ;
7375
7376 GNU extension:
7377
7378 jump-statement:
7379 goto * expression ;
7380
7381 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7382
7383 static tree
7384 cp_parser_jump_statement (cp_parser* parser)
7385 {
7386 tree statement = error_mark_node;
7387 cp_token *token;
7388 enum rid keyword;
7389 unsigned char in_statement;
7390
7391 /* Peek at the next token. */
7392 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7393 if (!token)
7394 return error_mark_node;
7395
7396 /* See what kind of keyword it is. */
7397 keyword = token->keyword;
7398 switch (keyword)
7399 {
7400 case RID_BREAK:
7401 in_statement = parser->in_statement & ~IN_IF_STMT;
7402 switch (in_statement)
7403 {
7404 case 0:
7405 error ("break statement not within loop or switch");
7406 break;
7407 default:
7408 gcc_assert ((in_statement & IN_SWITCH_STMT)
7409 || in_statement == IN_ITERATION_STMT);
7410 statement = finish_break_stmt ();
7411 break;
7412 case IN_OMP_BLOCK:
7413 error ("invalid exit from OpenMP structured block");
7414 break;
7415 case IN_OMP_FOR:
7416 error ("break statement used with OpenMP for loop");
7417 break;
7418 }
7419 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7420 break;
7421
7422 case RID_CONTINUE:
7423 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7424 {
7425 case 0:
7426 error ("continue statement not within a loop");
7427 break;
7428 case IN_ITERATION_STMT:
7429 case IN_OMP_FOR:
7430 statement = finish_continue_stmt ();
7431 break;
7432 case IN_OMP_BLOCK:
7433 error ("invalid exit from OpenMP structured block");
7434 break;
7435 default:
7436 gcc_unreachable ();
7437 }
7438 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7439 break;
7440
7441 case RID_RETURN:
7442 {
7443 tree expr;
7444
7445 /* If the next token is a `;', then there is no
7446 expression. */
7447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7448 expr = cp_parser_expression (parser, /*cast_p=*/false);
7449 else
7450 expr = NULL_TREE;
7451 /* Build the return-statement. */
7452 statement = finish_return_stmt (expr);
7453 /* Look for the final `;'. */
7454 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7455 }
7456 break;
7457
7458 case RID_GOTO:
7459 /* Create the goto-statement. */
7460 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7461 {
7462 /* Issue a warning about this use of a GNU extension. */
7463 if (pedantic)
7464 pedwarn ("ISO C++ forbids computed gotos");
7465 /* Consume the '*' token. */
7466 cp_lexer_consume_token (parser->lexer);
7467 /* Parse the dependent expression. */
7468 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7469 }
7470 else
7471 finish_goto_stmt (cp_parser_identifier (parser));
7472 /* Look for the final `;'. */
7473 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7474 break;
7475
7476 default:
7477 cp_parser_error (parser, "expected jump-statement");
7478 break;
7479 }
7480
7481 return statement;
7482 }
7483
7484 /* Parse a declaration-statement.
7485
7486 declaration-statement:
7487 block-declaration */
7488
7489 static void
7490 cp_parser_declaration_statement (cp_parser* parser)
7491 {
7492 void *p;
7493
7494 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7495 p = obstack_alloc (&declarator_obstack, 0);
7496
7497 /* Parse the block-declaration. */
7498 cp_parser_block_declaration (parser, /*statement_p=*/true);
7499
7500 /* Free any declarators allocated. */
7501 obstack_free (&declarator_obstack, p);
7502
7503 /* Finish off the statement. */
7504 finish_stmt ();
7505 }
7506
7507 /* Some dependent statements (like `if (cond) statement'), are
7508 implicitly in their own scope. In other words, if the statement is
7509 a single statement (as opposed to a compound-statement), it is
7510 none-the-less treated as if it were enclosed in braces. Any
7511 declarations appearing in the dependent statement are out of scope
7512 after control passes that point. This function parses a statement,
7513 but ensures that is in its own scope, even if it is not a
7514 compound-statement.
7515
7516 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7517 is a (possibly labeled) if statement which is not enclosed in
7518 braces and has an else clause. This is used to implement
7519 -Wparentheses.
7520
7521 Returns the new statement. */
7522
7523 static tree
7524 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7525 {
7526 tree statement;
7527
7528 if (if_p != NULL)
7529 *if_p = false;
7530
7531 /* Mark if () ; with a special NOP_EXPR. */
7532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7533 {
7534 cp_lexer_consume_token (parser->lexer);
7535 statement = add_stmt (build_empty_stmt ());
7536 }
7537 /* if a compound is opened, we simply parse the statement directly. */
7538 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7539 statement = cp_parser_compound_statement (parser, NULL, false);
7540 /* If the token is not a `{', then we must take special action. */
7541 else
7542 {
7543 /* Create a compound-statement. */
7544 statement = begin_compound_stmt (0);
7545 /* Parse the dependent-statement. */
7546 cp_parser_statement (parser, NULL_TREE, false, if_p);
7547 /* Finish the dummy compound-statement. */
7548 finish_compound_stmt (statement);
7549 }
7550
7551 /* Return the statement. */
7552 return statement;
7553 }
7554
7555 /* For some dependent statements (like `while (cond) statement'), we
7556 have already created a scope. Therefore, even if the dependent
7557 statement is a compound-statement, we do not want to create another
7558 scope. */
7559
7560 static void
7561 cp_parser_already_scoped_statement (cp_parser* parser)
7562 {
7563 /* If the token is a `{', then we must take special action. */
7564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7565 cp_parser_statement (parser, NULL_TREE, false, NULL);
7566 else
7567 {
7568 /* Avoid calling cp_parser_compound_statement, so that we
7569 don't create a new scope. Do everything else by hand. */
7570 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7571 cp_parser_statement_seq_opt (parser, NULL_TREE);
7572 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7573 }
7574 }
7575
7576 /* Declarations [gram.dcl.dcl] */
7577
7578 /* Parse an optional declaration-sequence.
7579
7580 declaration-seq:
7581 declaration
7582 declaration-seq declaration */
7583
7584 static void
7585 cp_parser_declaration_seq_opt (cp_parser* parser)
7586 {
7587 while (true)
7588 {
7589 cp_token *token;
7590
7591 token = cp_lexer_peek_token (parser->lexer);
7592
7593 if (token->type == CPP_CLOSE_BRACE
7594 || token->type == CPP_EOF
7595 || token->type == CPP_PRAGMA_EOL)
7596 break;
7597
7598 if (token->type == CPP_SEMICOLON)
7599 {
7600 /* A declaration consisting of a single semicolon is
7601 invalid. Allow it unless we're being pedantic. */
7602 cp_lexer_consume_token (parser->lexer);
7603 if (pedantic && !in_system_header)
7604 pedwarn ("extra %<;%>");
7605 continue;
7606 }
7607
7608 /* If we're entering or exiting a region that's implicitly
7609 extern "C", modify the lang context appropriately. */
7610 if (!parser->implicit_extern_c && token->implicit_extern_c)
7611 {
7612 push_lang_context (lang_name_c);
7613 parser->implicit_extern_c = true;
7614 }
7615 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7616 {
7617 pop_lang_context ();
7618 parser->implicit_extern_c = false;
7619 }
7620
7621 if (token->type == CPP_PRAGMA)
7622 {
7623 /* A top-level declaration can consist solely of a #pragma.
7624 A nested declaration cannot, so this is done here and not
7625 in cp_parser_declaration. (A #pragma at block scope is
7626 handled in cp_parser_statement.) */
7627 cp_parser_pragma (parser, pragma_external);
7628 continue;
7629 }
7630
7631 /* Parse the declaration itself. */
7632 cp_parser_declaration (parser);
7633 }
7634 }
7635
7636 /* Parse a declaration.
7637
7638 declaration:
7639 block-declaration
7640 function-definition
7641 template-declaration
7642 explicit-instantiation
7643 explicit-specialization
7644 linkage-specification
7645 namespace-definition
7646
7647 GNU extension:
7648
7649 declaration:
7650 __extension__ declaration */
7651
7652 static void
7653 cp_parser_declaration (cp_parser* parser)
7654 {
7655 cp_token token1;
7656 cp_token token2;
7657 int saved_pedantic;
7658 void *p;
7659
7660 /* Check for the `__extension__' keyword. */
7661 if (cp_parser_extension_opt (parser, &saved_pedantic))
7662 {
7663 /* Parse the qualified declaration. */
7664 cp_parser_declaration (parser);
7665 /* Restore the PEDANTIC flag. */
7666 pedantic = saved_pedantic;
7667
7668 return;
7669 }
7670
7671 /* Try to figure out what kind of declaration is present. */
7672 token1 = *cp_lexer_peek_token (parser->lexer);
7673
7674 if (token1.type != CPP_EOF)
7675 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7676 else
7677 {
7678 token2.type = CPP_EOF;
7679 token2.keyword = RID_MAX;
7680 }
7681
7682 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7683 p = obstack_alloc (&declarator_obstack, 0);
7684
7685 /* If the next token is `extern' and the following token is a string
7686 literal, then we have a linkage specification. */
7687 if (token1.keyword == RID_EXTERN
7688 && cp_parser_is_string_literal (&token2))
7689 cp_parser_linkage_specification (parser);
7690 /* If the next token is `template', then we have either a template
7691 declaration, an explicit instantiation, or an explicit
7692 specialization. */
7693 else if (token1.keyword == RID_TEMPLATE)
7694 {
7695 /* `template <>' indicates a template specialization. */
7696 if (token2.type == CPP_LESS
7697 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7698 cp_parser_explicit_specialization (parser);
7699 /* `template <' indicates a template declaration. */
7700 else if (token2.type == CPP_LESS)
7701 cp_parser_template_declaration (parser, /*member_p=*/false);
7702 /* Anything else must be an explicit instantiation. */
7703 else
7704 cp_parser_explicit_instantiation (parser);
7705 }
7706 /* If the next token is `export', then we have a template
7707 declaration. */
7708 else if (token1.keyword == RID_EXPORT)
7709 cp_parser_template_declaration (parser, /*member_p=*/false);
7710 /* If the next token is `extern', 'static' or 'inline' and the one
7711 after that is `template', we have a GNU extended explicit
7712 instantiation directive. */
7713 else if (cp_parser_allow_gnu_extensions_p (parser)
7714 && (token1.keyword == RID_EXTERN
7715 || token1.keyword == RID_STATIC
7716 || token1.keyword == RID_INLINE)
7717 && token2.keyword == RID_TEMPLATE)
7718 cp_parser_explicit_instantiation (parser);
7719 /* If the next token is `namespace', check for a named or unnamed
7720 namespace definition. */
7721 else if (token1.keyword == RID_NAMESPACE
7722 && (/* A named namespace definition. */
7723 (token2.type == CPP_NAME
7724 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7725 != CPP_EQ))
7726 /* An unnamed namespace definition. */
7727 || token2.type == CPP_OPEN_BRACE
7728 || token2.keyword == RID_ATTRIBUTE))
7729 cp_parser_namespace_definition (parser);
7730 /* An inline (associated) namespace definition. */
7731 else if (token1.keyword == RID_INLINE
7732 && token2.keyword == RID_NAMESPACE)
7733 cp_parser_namespace_definition (parser);
7734 /* Objective-C++ declaration/definition. */
7735 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7736 cp_parser_objc_declaration (parser);
7737 /* We must have either a block declaration or a function
7738 definition. */
7739 else
7740 /* Try to parse a block-declaration, or a function-definition. */
7741 cp_parser_block_declaration (parser, /*statement_p=*/false);
7742
7743 /* Free any declarators allocated. */
7744 obstack_free (&declarator_obstack, p);
7745 }
7746
7747 /* Parse a block-declaration.
7748
7749 block-declaration:
7750 simple-declaration
7751 asm-definition
7752 namespace-alias-definition
7753 using-declaration
7754 using-directive
7755
7756 GNU Extension:
7757
7758 block-declaration:
7759 __extension__ block-declaration
7760
7761 C++0x Extension:
7762
7763 block-declaration:
7764 static_assert-declaration
7765
7766 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7767 part of a declaration-statement. */
7768
7769 static void
7770 cp_parser_block_declaration (cp_parser *parser,
7771 bool statement_p)
7772 {
7773 cp_token *token1;
7774 int saved_pedantic;
7775
7776 /* Check for the `__extension__' keyword. */
7777 if (cp_parser_extension_opt (parser, &saved_pedantic))
7778 {
7779 /* Parse the qualified declaration. */
7780 cp_parser_block_declaration (parser, statement_p);
7781 /* Restore the PEDANTIC flag. */
7782 pedantic = saved_pedantic;
7783
7784 return;
7785 }
7786
7787 /* Peek at the next token to figure out which kind of declaration is
7788 present. */
7789 token1 = cp_lexer_peek_token (parser->lexer);
7790
7791 /* If the next keyword is `asm', we have an asm-definition. */
7792 if (token1->keyword == RID_ASM)
7793 {
7794 if (statement_p)
7795 cp_parser_commit_to_tentative_parse (parser);
7796 cp_parser_asm_definition (parser);
7797 }
7798 /* If the next keyword is `namespace', we have a
7799 namespace-alias-definition. */
7800 else if (token1->keyword == RID_NAMESPACE)
7801 cp_parser_namespace_alias_definition (parser);
7802 /* If the next keyword is `using', we have either a
7803 using-declaration or a using-directive. */
7804 else if (token1->keyword == RID_USING)
7805 {
7806 cp_token *token2;
7807
7808 if (statement_p)
7809 cp_parser_commit_to_tentative_parse (parser);
7810 /* If the token after `using' is `namespace', then we have a
7811 using-directive. */
7812 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7813 if (token2->keyword == RID_NAMESPACE)
7814 cp_parser_using_directive (parser);
7815 /* Otherwise, it's a using-declaration. */
7816 else
7817 cp_parser_using_declaration (parser,
7818 /*access_declaration_p=*/false);
7819 }
7820 /* If the next keyword is `__label__' we have a misplaced label
7821 declaration. */
7822 else if (token1->keyword == RID_LABEL)
7823 {
7824 cp_lexer_consume_token (parser->lexer);
7825 error ("%<__label__%> not at the beginning of a block");
7826 cp_parser_skip_to_end_of_statement (parser);
7827 /* If the next token is now a `;', consume it. */
7828 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7829 cp_lexer_consume_token (parser->lexer);
7830 }
7831 /* If the next token is `static_assert' we have a static assertion. */
7832 else if (token1->keyword == RID_STATIC_ASSERT)
7833 cp_parser_static_assert (parser, /*member_p=*/false);
7834 /* Anything else must be a simple-declaration. */
7835 else
7836 cp_parser_simple_declaration (parser, !statement_p);
7837 }
7838
7839 /* Parse a simple-declaration.
7840
7841 simple-declaration:
7842 decl-specifier-seq [opt] init-declarator-list [opt] ;
7843
7844 init-declarator-list:
7845 init-declarator
7846 init-declarator-list , init-declarator
7847
7848 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7849 function-definition as a simple-declaration. */
7850
7851 static void
7852 cp_parser_simple_declaration (cp_parser* parser,
7853 bool function_definition_allowed_p)
7854 {
7855 cp_decl_specifier_seq decl_specifiers;
7856 int declares_class_or_enum;
7857 bool saw_declarator;
7858
7859 /* Defer access checks until we know what is being declared; the
7860 checks for names appearing in the decl-specifier-seq should be
7861 done as if we were in the scope of the thing being declared. */
7862 push_deferring_access_checks (dk_deferred);
7863
7864 /* Parse the decl-specifier-seq. We have to keep track of whether
7865 or not the decl-specifier-seq declares a named class or
7866 enumeration type, since that is the only case in which the
7867 init-declarator-list is allowed to be empty.
7868
7869 [dcl.dcl]
7870
7871 In a simple-declaration, the optional init-declarator-list can be
7872 omitted only when declaring a class or enumeration, that is when
7873 the decl-specifier-seq contains either a class-specifier, an
7874 elaborated-type-specifier, or an enum-specifier. */
7875 cp_parser_decl_specifier_seq (parser,
7876 CP_PARSER_FLAGS_OPTIONAL,
7877 &decl_specifiers,
7878 &declares_class_or_enum);
7879 /* We no longer need to defer access checks. */
7880 stop_deferring_access_checks ();
7881
7882 /* In a block scope, a valid declaration must always have a
7883 decl-specifier-seq. By not trying to parse declarators, we can
7884 resolve the declaration/expression ambiguity more quickly. */
7885 if (!function_definition_allowed_p
7886 && !decl_specifiers.any_specifiers_p)
7887 {
7888 cp_parser_error (parser, "expected declaration");
7889 goto done;
7890 }
7891
7892 /* If the next two tokens are both identifiers, the code is
7893 erroneous. The usual cause of this situation is code like:
7894
7895 T t;
7896
7897 where "T" should name a type -- but does not. */
7898 if (!decl_specifiers.type
7899 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7900 {
7901 /* If parsing tentatively, we should commit; we really are
7902 looking at a declaration. */
7903 cp_parser_commit_to_tentative_parse (parser);
7904 /* Give up. */
7905 goto done;
7906 }
7907
7908 /* If we have seen at least one decl-specifier, and the next token
7909 is not a parenthesis, then we must be looking at a declaration.
7910 (After "int (" we might be looking at a functional cast.) */
7911 if (decl_specifiers.any_specifiers_p
7912 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7913 cp_parser_commit_to_tentative_parse (parser);
7914
7915 /* Keep going until we hit the `;' at the end of the simple
7916 declaration. */
7917 saw_declarator = false;
7918 while (cp_lexer_next_token_is_not (parser->lexer,
7919 CPP_SEMICOLON))
7920 {
7921 cp_token *token;
7922 bool function_definition_p;
7923 tree decl;
7924
7925 if (saw_declarator)
7926 {
7927 /* If we are processing next declarator, coma is expected */
7928 token = cp_lexer_peek_token (parser->lexer);
7929 gcc_assert (token->type == CPP_COMMA);
7930 cp_lexer_consume_token (parser->lexer);
7931 }
7932 else
7933 saw_declarator = true;
7934
7935 /* Parse the init-declarator. */
7936 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7937 /*checks=*/NULL,
7938 function_definition_allowed_p,
7939 /*member_p=*/false,
7940 declares_class_or_enum,
7941 &function_definition_p);
7942 /* If an error occurred while parsing tentatively, exit quickly.
7943 (That usually happens when in the body of a function; each
7944 statement is treated as a declaration-statement until proven
7945 otherwise.) */
7946 if (cp_parser_error_occurred (parser))
7947 goto done;
7948 /* Handle function definitions specially. */
7949 if (function_definition_p)
7950 {
7951 /* If the next token is a `,', then we are probably
7952 processing something like:
7953
7954 void f() {}, *p;
7955
7956 which is erroneous. */
7957 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7958 error ("mixing declarations and function-definitions is forbidden");
7959 /* Otherwise, we're done with the list of declarators. */
7960 else
7961 {
7962 pop_deferring_access_checks ();
7963 return;
7964 }
7965 }
7966 /* The next token should be either a `,' or a `;'. */
7967 token = cp_lexer_peek_token (parser->lexer);
7968 /* If it's a `,', there are more declarators to come. */
7969 if (token->type == CPP_COMMA)
7970 /* will be consumed next time around */;
7971 /* If it's a `;', we are done. */
7972 else if (token->type == CPP_SEMICOLON)
7973 break;
7974 /* Anything else is an error. */
7975 else
7976 {
7977 /* If we have already issued an error message we don't need
7978 to issue another one. */
7979 if (decl != error_mark_node
7980 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7981 cp_parser_error (parser, "expected %<,%> or %<;%>");
7982 /* Skip tokens until we reach the end of the statement. */
7983 cp_parser_skip_to_end_of_statement (parser);
7984 /* If the next token is now a `;', consume it. */
7985 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7986 cp_lexer_consume_token (parser->lexer);
7987 goto done;
7988 }
7989 /* After the first time around, a function-definition is not
7990 allowed -- even if it was OK at first. For example:
7991
7992 int i, f() {}
7993
7994 is not valid. */
7995 function_definition_allowed_p = false;
7996 }
7997
7998 /* Issue an error message if no declarators are present, and the
7999 decl-specifier-seq does not itself declare a class or
8000 enumeration. */
8001 if (!saw_declarator)
8002 {
8003 if (cp_parser_declares_only_class_p (parser))
8004 shadow_tag (&decl_specifiers);
8005 /* Perform any deferred access checks. */
8006 perform_deferred_access_checks ();
8007 }
8008
8009 /* Consume the `;'. */
8010 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8011
8012 done:
8013 pop_deferring_access_checks ();
8014 }
8015
8016 /* Parse a decl-specifier-seq.
8017
8018 decl-specifier-seq:
8019 decl-specifier-seq [opt] decl-specifier
8020
8021 decl-specifier:
8022 storage-class-specifier
8023 type-specifier
8024 function-specifier
8025 friend
8026 typedef
8027
8028 GNU Extension:
8029
8030 decl-specifier:
8031 attributes
8032
8033 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8034
8035 The parser flags FLAGS is used to control type-specifier parsing.
8036
8037 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8038 flags:
8039
8040 1: one of the decl-specifiers is an elaborated-type-specifier
8041 (i.e., a type declaration)
8042 2: one of the decl-specifiers is an enum-specifier or a
8043 class-specifier (i.e., a type definition)
8044
8045 */
8046
8047 static void
8048 cp_parser_decl_specifier_seq (cp_parser* parser,
8049 cp_parser_flags flags,
8050 cp_decl_specifier_seq *decl_specs,
8051 int* declares_class_or_enum)
8052 {
8053 bool constructor_possible_p = !parser->in_declarator_p;
8054
8055 /* Clear DECL_SPECS. */
8056 clear_decl_specs (decl_specs);
8057
8058 /* Assume no class or enumeration type is declared. */
8059 *declares_class_or_enum = 0;
8060
8061 /* Keep reading specifiers until there are no more to read. */
8062 while (true)
8063 {
8064 bool constructor_p;
8065 bool found_decl_spec;
8066 cp_token *token;
8067
8068 /* Peek at the next token. */
8069 token = cp_lexer_peek_token (parser->lexer);
8070 /* Handle attributes. */
8071 if (token->keyword == RID_ATTRIBUTE)
8072 {
8073 /* Parse the attributes. */
8074 decl_specs->attributes
8075 = chainon (decl_specs->attributes,
8076 cp_parser_attributes_opt (parser));
8077 continue;
8078 }
8079 /* Assume we will find a decl-specifier keyword. */
8080 found_decl_spec = true;
8081 /* If the next token is an appropriate keyword, we can simply
8082 add it to the list. */
8083 switch (token->keyword)
8084 {
8085 /* decl-specifier:
8086 friend */
8087 case RID_FRIEND:
8088 if (!at_class_scope_p ())
8089 {
8090 error ("%<friend%> used outside of class");
8091 cp_lexer_purge_token (parser->lexer);
8092 }
8093 else
8094 {
8095 ++decl_specs->specs[(int) ds_friend];
8096 /* Consume the token. */
8097 cp_lexer_consume_token (parser->lexer);
8098 }
8099 break;
8100
8101 /* function-specifier:
8102 inline
8103 virtual
8104 explicit */
8105 case RID_INLINE:
8106 case RID_VIRTUAL:
8107 case RID_EXPLICIT:
8108 cp_parser_function_specifier_opt (parser, decl_specs);
8109 break;
8110
8111 /* decl-specifier:
8112 typedef */
8113 case RID_TYPEDEF:
8114 ++decl_specs->specs[(int) ds_typedef];
8115 /* Consume the token. */
8116 cp_lexer_consume_token (parser->lexer);
8117 /* A constructor declarator cannot appear in a typedef. */
8118 constructor_possible_p = false;
8119 /* The "typedef" keyword can only occur in a declaration; we
8120 may as well commit at this point. */
8121 cp_parser_commit_to_tentative_parse (parser);
8122
8123 if (decl_specs->storage_class != sc_none)
8124 decl_specs->conflicting_specifiers_p = true;
8125 break;
8126
8127 /* storage-class-specifier:
8128 auto
8129 register
8130 static
8131 extern
8132 mutable
8133
8134 GNU Extension:
8135 thread */
8136 case RID_AUTO:
8137 case RID_REGISTER:
8138 case RID_STATIC:
8139 case RID_EXTERN:
8140 case RID_MUTABLE:
8141 /* Consume the token. */
8142 cp_lexer_consume_token (parser->lexer);
8143 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8144 break;
8145 case RID_THREAD:
8146 /* Consume the token. */
8147 cp_lexer_consume_token (parser->lexer);
8148 ++decl_specs->specs[(int) ds_thread];
8149 break;
8150
8151 default:
8152 /* We did not yet find a decl-specifier yet. */
8153 found_decl_spec = false;
8154 break;
8155 }
8156
8157 /* Constructors are a special case. The `S' in `S()' is not a
8158 decl-specifier; it is the beginning of the declarator. */
8159 constructor_p
8160 = (!found_decl_spec
8161 && constructor_possible_p
8162 && (cp_parser_constructor_declarator_p
8163 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8164
8165 /* If we don't have a DECL_SPEC yet, then we must be looking at
8166 a type-specifier. */
8167 if (!found_decl_spec && !constructor_p)
8168 {
8169 int decl_spec_declares_class_or_enum;
8170 bool is_cv_qualifier;
8171 tree type_spec;
8172
8173 type_spec
8174 = cp_parser_type_specifier (parser, flags,
8175 decl_specs,
8176 /*is_declaration=*/true,
8177 &decl_spec_declares_class_or_enum,
8178 &is_cv_qualifier);
8179
8180 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8181
8182 /* If this type-specifier referenced a user-defined type
8183 (a typedef, class-name, etc.), then we can't allow any
8184 more such type-specifiers henceforth.
8185
8186 [dcl.spec]
8187
8188 The longest sequence of decl-specifiers that could
8189 possibly be a type name is taken as the
8190 decl-specifier-seq of a declaration. The sequence shall
8191 be self-consistent as described below.
8192
8193 [dcl.type]
8194
8195 As a general rule, at most one type-specifier is allowed
8196 in the complete decl-specifier-seq of a declaration. The
8197 only exceptions are the following:
8198
8199 -- const or volatile can be combined with any other
8200 type-specifier.
8201
8202 -- signed or unsigned can be combined with char, long,
8203 short, or int.
8204
8205 -- ..
8206
8207 Example:
8208
8209 typedef char* Pc;
8210 void g (const int Pc);
8211
8212 Here, Pc is *not* part of the decl-specifier seq; it's
8213 the declarator. Therefore, once we see a type-specifier
8214 (other than a cv-qualifier), we forbid any additional
8215 user-defined types. We *do* still allow things like `int
8216 int' to be considered a decl-specifier-seq, and issue the
8217 error message later. */
8218 if (type_spec && !is_cv_qualifier)
8219 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8220 /* A constructor declarator cannot follow a type-specifier. */
8221 if (type_spec)
8222 {
8223 constructor_possible_p = false;
8224 found_decl_spec = true;
8225 }
8226 }
8227
8228 /* If we still do not have a DECL_SPEC, then there are no more
8229 decl-specifiers. */
8230 if (!found_decl_spec)
8231 break;
8232
8233 decl_specs->any_specifiers_p = true;
8234 /* After we see one decl-specifier, further decl-specifiers are
8235 always optional. */
8236 flags |= CP_PARSER_FLAGS_OPTIONAL;
8237 }
8238
8239 cp_parser_check_decl_spec (decl_specs);
8240
8241 /* Don't allow a friend specifier with a class definition. */
8242 if (decl_specs->specs[(int) ds_friend] != 0
8243 && (*declares_class_or_enum & 2))
8244 error ("class definition may not be declared a friend");
8245 }
8246
8247 /* Parse an (optional) storage-class-specifier.
8248
8249 storage-class-specifier:
8250 auto
8251 register
8252 static
8253 extern
8254 mutable
8255
8256 GNU Extension:
8257
8258 storage-class-specifier:
8259 thread
8260
8261 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8262
8263 static tree
8264 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8265 {
8266 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8267 {
8268 case RID_AUTO:
8269 case RID_REGISTER:
8270 case RID_STATIC:
8271 case RID_EXTERN:
8272 case RID_MUTABLE:
8273 case RID_THREAD:
8274 /* Consume the token. */
8275 return cp_lexer_consume_token (parser->lexer)->u.value;
8276
8277 default:
8278 return NULL_TREE;
8279 }
8280 }
8281
8282 /* Parse an (optional) function-specifier.
8283
8284 function-specifier:
8285 inline
8286 virtual
8287 explicit
8288
8289 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8290 Updates DECL_SPECS, if it is non-NULL. */
8291
8292 static tree
8293 cp_parser_function_specifier_opt (cp_parser* parser,
8294 cp_decl_specifier_seq *decl_specs)
8295 {
8296 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8297 {
8298 case RID_INLINE:
8299 if (decl_specs)
8300 ++decl_specs->specs[(int) ds_inline];
8301 break;
8302
8303 case RID_VIRTUAL:
8304 /* 14.5.2.3 [temp.mem]
8305
8306 A member function template shall not be virtual. */
8307 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8308 error ("templates may not be %<virtual%>");
8309 else if (decl_specs)
8310 ++decl_specs->specs[(int) ds_virtual];
8311 break;
8312
8313 case RID_EXPLICIT:
8314 if (decl_specs)
8315 ++decl_specs->specs[(int) ds_explicit];
8316 break;
8317
8318 default:
8319 return NULL_TREE;
8320 }
8321
8322 /* Consume the token. */
8323 return cp_lexer_consume_token (parser->lexer)->u.value;
8324 }
8325
8326 /* Parse a linkage-specification.
8327
8328 linkage-specification:
8329 extern string-literal { declaration-seq [opt] }
8330 extern string-literal declaration */
8331
8332 static void
8333 cp_parser_linkage_specification (cp_parser* parser)
8334 {
8335 tree linkage;
8336
8337 /* Look for the `extern' keyword. */
8338 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8339
8340 /* Look for the string-literal. */
8341 linkage = cp_parser_string_literal (parser, false, false);
8342
8343 /* Transform the literal into an identifier. If the literal is a
8344 wide-character string, or contains embedded NULs, then we can't
8345 handle it as the user wants. */
8346 if (strlen (TREE_STRING_POINTER (linkage))
8347 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8348 {
8349 cp_parser_error (parser, "invalid linkage-specification");
8350 /* Assume C++ linkage. */
8351 linkage = lang_name_cplusplus;
8352 }
8353 else
8354 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8355
8356 /* We're now using the new linkage. */
8357 push_lang_context (linkage);
8358
8359 /* If the next token is a `{', then we're using the first
8360 production. */
8361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8362 {
8363 /* Consume the `{' token. */
8364 cp_lexer_consume_token (parser->lexer);
8365 /* Parse the declarations. */
8366 cp_parser_declaration_seq_opt (parser);
8367 /* Look for the closing `}'. */
8368 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8369 }
8370 /* Otherwise, there's just one declaration. */
8371 else
8372 {
8373 bool saved_in_unbraced_linkage_specification_p;
8374
8375 saved_in_unbraced_linkage_specification_p
8376 = parser->in_unbraced_linkage_specification_p;
8377 parser->in_unbraced_linkage_specification_p = true;
8378 cp_parser_declaration (parser);
8379 parser->in_unbraced_linkage_specification_p
8380 = saved_in_unbraced_linkage_specification_p;
8381 }
8382
8383 /* We're done with the linkage-specification. */
8384 pop_lang_context ();
8385 }
8386
8387 /* Parse a static_assert-declaration.
8388
8389 static_assert-declaration:
8390 static_assert ( constant-expression , string-literal ) ;
8391
8392 If MEMBER_P, this static_assert is a class member. */
8393
8394 static void
8395 cp_parser_static_assert(cp_parser *parser, bool member_p)
8396 {
8397 tree condition;
8398 tree message;
8399 cp_token *token;
8400 location_t saved_loc;
8401
8402 /* Peek at the `static_assert' token so we can keep track of exactly
8403 where the static assertion started. */
8404 token = cp_lexer_peek_token (parser->lexer);
8405 saved_loc = token->location;
8406
8407 /* Look for the `static_assert' keyword. */
8408 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8409 "`static_assert'"))
8410 return;
8411
8412 /* We know we are in a static assertion; commit to any tentative
8413 parse. */
8414 if (cp_parser_parsing_tentatively (parser))
8415 cp_parser_commit_to_tentative_parse (parser);
8416
8417 /* Parse the `(' starting the static assertion condition. */
8418 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8419
8420 /* Parse the constant-expression. */
8421 condition =
8422 cp_parser_constant_expression (parser,
8423 /*allow_non_constant_p=*/false,
8424 /*non_constant_p=*/NULL);
8425
8426 /* Parse the separating `,'. */
8427 cp_parser_require (parser, CPP_COMMA, "`,'");
8428
8429 /* Parse the string-literal message. */
8430 message = cp_parser_string_literal (parser,
8431 /*translate=*/false,
8432 /*wide_ok=*/true);
8433
8434 /* A `)' completes the static assertion. */
8435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8436 cp_parser_skip_to_closing_parenthesis (parser,
8437 /*recovering=*/true,
8438 /*or_comma=*/false,
8439 /*consume_paren=*/true);
8440
8441 /* A semicolon terminates the declaration. */
8442 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8443
8444 /* Complete the static assertion, which may mean either processing
8445 the static assert now or saving it for template instantiation. */
8446 finish_static_assert (condition, message, saved_loc, member_p);
8447 }
8448
8449 /* Parse a `decltype' type. Returns the type.
8450
8451 simple-type-specifier:
8452 decltype ( expression ) */
8453
8454 static tree
8455 cp_parser_decltype (cp_parser *parser)
8456 {
8457 tree expr;
8458 bool id_expression_or_member_access_p = false;
8459 const char *saved_message;
8460 bool saved_integral_constant_expression_p;
8461 bool saved_non_integral_constant_expression_p;
8462
8463 /* Look for the `decltype' token. */
8464 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8465 return error_mark_node;
8466
8467 /* Types cannot be defined in a `decltype' expression. Save away the
8468 old message. */
8469 saved_message = parser->type_definition_forbidden_message;
8470
8471 /* And create the new one. */
8472 parser->type_definition_forbidden_message
8473 = "types may not be defined in `decltype' expressions";
8474
8475 /* The restrictions on constant-expressions do not apply inside
8476 decltype expressions. */
8477 saved_integral_constant_expression_p
8478 = parser->integral_constant_expression_p;
8479 saved_non_integral_constant_expression_p
8480 = parser->non_integral_constant_expression_p;
8481 parser->integral_constant_expression_p = false;
8482
8483 /* Do not actually evaluate the expression. */
8484 ++skip_evaluation;
8485
8486 /* Parse the opening `('. */
8487 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8488 return error_mark_node;
8489
8490 /* First, try parsing an id-expression. */
8491 cp_parser_parse_tentatively (parser);
8492 expr = cp_parser_id_expression (parser,
8493 /*template_keyword_p=*/false,
8494 /*check_dependency_p=*/true,
8495 /*template_p=*/NULL,
8496 /*declarator_p=*/false,
8497 /*optional_p=*/false);
8498
8499 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8500 {
8501 bool non_integral_constant_expression_p = false;
8502 tree id_expression = expr;
8503 cp_id_kind idk;
8504 const char *error_msg;
8505
8506 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8507 /* Lookup the name we got back from the id-expression. */
8508 expr = cp_parser_lookup_name (parser, expr,
8509 none_type,
8510 /*is_template=*/false,
8511 /*is_namespace=*/false,
8512 /*check_dependency=*/true,
8513 /*ambiguous_decls=*/NULL);
8514
8515 if (expr
8516 && expr != error_mark_node
8517 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8518 && TREE_CODE (expr) != TYPE_DECL
8519 && (TREE_CODE (expr) != BIT_NOT_EXPR
8520 || !TYPE_P (TREE_OPERAND (expr, 0)))
8521 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8522 {
8523 /* Complete lookup of the id-expression. */
8524 expr = (finish_id_expression
8525 (id_expression, expr, parser->scope, &idk,
8526 /*integral_constant_expression_p=*/false,
8527 /*allow_non_integral_constant_expression_p=*/true,
8528 &non_integral_constant_expression_p,
8529 /*template_p=*/false,
8530 /*done=*/true,
8531 /*address_p=*/false,
8532 /*template_arg_p=*/false,
8533 &error_msg));
8534
8535 if (expr == error_mark_node)
8536 /* We found an id-expression, but it was something that we
8537 should not have found. This is an error, not something
8538 we can recover from, so note that we found an
8539 id-expression and we'll recover as gracefully as
8540 possible. */
8541 id_expression_or_member_access_p = true;
8542 }
8543
8544 if (expr
8545 && expr != error_mark_node
8546 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8547 /* We have an id-expression. */
8548 id_expression_or_member_access_p = true;
8549 }
8550
8551 if (!id_expression_or_member_access_p)
8552 {
8553 /* Abort the id-expression parse. */
8554 cp_parser_abort_tentative_parse (parser);
8555
8556 /* Parsing tentatively, again. */
8557 cp_parser_parse_tentatively (parser);
8558
8559 /* Parse a class member access. */
8560 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8561 /*cast_p=*/false,
8562 /*member_access_only_p=*/true);
8563
8564 if (expr
8565 && expr != error_mark_node
8566 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8567 /* We have an id-expression. */
8568 id_expression_or_member_access_p = true;
8569 }
8570
8571 if (id_expression_or_member_access_p)
8572 /* We have parsed the complete id-expression or member access. */
8573 cp_parser_parse_definitely (parser);
8574 else
8575 {
8576 /* Abort our attempt to parse an id-expression or member access
8577 expression. */
8578 cp_parser_abort_tentative_parse (parser);
8579
8580 /* Parse a full expression. */
8581 expr = cp_parser_expression (parser, /*cast_p=*/false);
8582 }
8583
8584 /* Go back to evaluating expressions. */
8585 --skip_evaluation;
8586
8587 /* Restore the old message and the integral constant expression
8588 flags. */
8589 parser->type_definition_forbidden_message = saved_message;
8590 parser->integral_constant_expression_p
8591 = saved_integral_constant_expression_p;
8592 parser->non_integral_constant_expression_p
8593 = saved_non_integral_constant_expression_p;
8594
8595 if (expr == error_mark_node)
8596 {
8597 /* Skip everything up to the closing `)'. */
8598 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8599 /*consume_paren=*/true);
8600 return error_mark_node;
8601 }
8602
8603 /* Parse to the closing `)'. */
8604 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8605 {
8606 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8607 /*consume_paren=*/true);
8608 return error_mark_node;
8609 }
8610
8611 return finish_decltype_type (expr, id_expression_or_member_access_p);
8612 }
8613
8614 /* Special member functions [gram.special] */
8615
8616 /* Parse a conversion-function-id.
8617
8618 conversion-function-id:
8619 operator conversion-type-id
8620
8621 Returns an IDENTIFIER_NODE representing the operator. */
8622
8623 static tree
8624 cp_parser_conversion_function_id (cp_parser* parser)
8625 {
8626 tree type;
8627 tree saved_scope;
8628 tree saved_qualifying_scope;
8629 tree saved_object_scope;
8630 tree pushed_scope = NULL_TREE;
8631
8632 /* Look for the `operator' token. */
8633 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8634 return error_mark_node;
8635 /* When we parse the conversion-type-id, the current scope will be
8636 reset. However, we need that information in able to look up the
8637 conversion function later, so we save it here. */
8638 saved_scope = parser->scope;
8639 saved_qualifying_scope = parser->qualifying_scope;
8640 saved_object_scope = parser->object_scope;
8641 /* We must enter the scope of the class so that the names of
8642 entities declared within the class are available in the
8643 conversion-type-id. For example, consider:
8644
8645 struct S {
8646 typedef int I;
8647 operator I();
8648 };
8649
8650 S::operator I() { ... }
8651
8652 In order to see that `I' is a type-name in the definition, we
8653 must be in the scope of `S'. */
8654 if (saved_scope)
8655 pushed_scope = push_scope (saved_scope);
8656 /* Parse the conversion-type-id. */
8657 type = cp_parser_conversion_type_id (parser);
8658 /* Leave the scope of the class, if any. */
8659 if (pushed_scope)
8660 pop_scope (pushed_scope);
8661 /* Restore the saved scope. */
8662 parser->scope = saved_scope;
8663 parser->qualifying_scope = saved_qualifying_scope;
8664 parser->object_scope = saved_object_scope;
8665 /* If the TYPE is invalid, indicate failure. */
8666 if (type == error_mark_node)
8667 return error_mark_node;
8668 return mangle_conv_op_name_for_type (type);
8669 }
8670
8671 /* Parse a conversion-type-id:
8672
8673 conversion-type-id:
8674 type-specifier-seq conversion-declarator [opt]
8675
8676 Returns the TYPE specified. */
8677
8678 static tree
8679 cp_parser_conversion_type_id (cp_parser* parser)
8680 {
8681 tree attributes;
8682 cp_decl_specifier_seq type_specifiers;
8683 cp_declarator *declarator;
8684 tree type_specified;
8685
8686 /* Parse the attributes. */
8687 attributes = cp_parser_attributes_opt (parser);
8688 /* Parse the type-specifiers. */
8689 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8690 &type_specifiers);
8691 /* If that didn't work, stop. */
8692 if (type_specifiers.type == error_mark_node)
8693 return error_mark_node;
8694 /* Parse the conversion-declarator. */
8695 declarator = cp_parser_conversion_declarator_opt (parser);
8696
8697 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8698 /*initialized=*/0, &attributes);
8699 if (attributes)
8700 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8701 return type_specified;
8702 }
8703
8704 /* Parse an (optional) conversion-declarator.
8705
8706 conversion-declarator:
8707 ptr-operator conversion-declarator [opt]
8708
8709 */
8710
8711 static cp_declarator *
8712 cp_parser_conversion_declarator_opt (cp_parser* parser)
8713 {
8714 enum tree_code code;
8715 tree class_type;
8716 cp_cv_quals cv_quals;
8717
8718 /* We don't know if there's a ptr-operator next, or not. */
8719 cp_parser_parse_tentatively (parser);
8720 /* Try the ptr-operator. */
8721 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8722 /* If it worked, look for more conversion-declarators. */
8723 if (cp_parser_parse_definitely (parser))
8724 {
8725 cp_declarator *declarator;
8726
8727 /* Parse another optional declarator. */
8728 declarator = cp_parser_conversion_declarator_opt (parser);
8729
8730 return cp_parser_make_indirect_declarator
8731 (code, class_type, cv_quals, declarator);
8732 }
8733
8734 return NULL;
8735 }
8736
8737 /* Parse an (optional) ctor-initializer.
8738
8739 ctor-initializer:
8740 : mem-initializer-list
8741
8742 Returns TRUE iff the ctor-initializer was actually present. */
8743
8744 static bool
8745 cp_parser_ctor_initializer_opt (cp_parser* parser)
8746 {
8747 /* If the next token is not a `:', then there is no
8748 ctor-initializer. */
8749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8750 {
8751 /* Do default initialization of any bases and members. */
8752 if (DECL_CONSTRUCTOR_P (current_function_decl))
8753 finish_mem_initializers (NULL_TREE);
8754
8755 return false;
8756 }
8757
8758 /* Consume the `:' token. */
8759 cp_lexer_consume_token (parser->lexer);
8760 /* And the mem-initializer-list. */
8761 cp_parser_mem_initializer_list (parser);
8762
8763 return true;
8764 }
8765
8766 /* Parse a mem-initializer-list.
8767
8768 mem-initializer-list:
8769 mem-initializer ... [opt]
8770 mem-initializer ... [opt] , mem-initializer-list */
8771
8772 static void
8773 cp_parser_mem_initializer_list (cp_parser* parser)
8774 {
8775 tree mem_initializer_list = NULL_TREE;
8776
8777 /* Let the semantic analysis code know that we are starting the
8778 mem-initializer-list. */
8779 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8780 error ("only constructors take base initializers");
8781
8782 /* Loop through the list. */
8783 while (true)
8784 {
8785 tree mem_initializer;
8786
8787 /* Parse the mem-initializer. */
8788 mem_initializer = cp_parser_mem_initializer (parser);
8789 /* If the next token is a `...', we're expanding member initializers. */
8790 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8791 {
8792 /* Consume the `...'. */
8793 cp_lexer_consume_token (parser->lexer);
8794
8795 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8796 can be expanded but members cannot. */
8797 if (mem_initializer != error_mark_node
8798 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8799 {
8800 error ("cannot expand initializer for member %<%D%>",
8801 TREE_PURPOSE (mem_initializer));
8802 mem_initializer = error_mark_node;
8803 }
8804
8805 /* Construct the pack expansion type. */
8806 if (mem_initializer != error_mark_node)
8807 mem_initializer = make_pack_expansion (mem_initializer);
8808 }
8809 /* Add it to the list, unless it was erroneous. */
8810 if (mem_initializer != error_mark_node)
8811 {
8812 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8813 mem_initializer_list = mem_initializer;
8814 }
8815 /* If the next token is not a `,', we're done. */
8816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8817 break;
8818 /* Consume the `,' token. */
8819 cp_lexer_consume_token (parser->lexer);
8820 }
8821
8822 /* Perform semantic analysis. */
8823 if (DECL_CONSTRUCTOR_P (current_function_decl))
8824 finish_mem_initializers (mem_initializer_list);
8825 }
8826
8827 /* Parse a mem-initializer.
8828
8829 mem-initializer:
8830 mem-initializer-id ( expression-list [opt] )
8831
8832 GNU extension:
8833
8834 mem-initializer:
8835 ( expression-list [opt] )
8836
8837 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8838 class) or FIELD_DECL (for a non-static data member) to initialize;
8839 the TREE_VALUE is the expression-list. An empty initialization
8840 list is represented by void_list_node. */
8841
8842 static tree
8843 cp_parser_mem_initializer (cp_parser* parser)
8844 {
8845 tree mem_initializer_id;
8846 tree expression_list;
8847 tree member;
8848
8849 /* Find out what is being initialized. */
8850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8851 {
8852 pedwarn ("anachronistic old-style base class initializer");
8853 mem_initializer_id = NULL_TREE;
8854 }
8855 else
8856 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8857 member = expand_member_init (mem_initializer_id);
8858 if (member && !DECL_P (member))
8859 in_base_initializer = 1;
8860
8861 expression_list
8862 = cp_parser_parenthesized_expression_list (parser, false,
8863 /*cast_p=*/false,
8864 /*allow_expansion_p=*/true,
8865 /*non_constant_p=*/NULL);
8866 if (expression_list == error_mark_node)
8867 return error_mark_node;
8868 if (!expression_list)
8869 expression_list = void_type_node;
8870
8871 in_base_initializer = 0;
8872
8873 return member ? build_tree_list (member, expression_list) : error_mark_node;
8874 }
8875
8876 /* Parse a mem-initializer-id.
8877
8878 mem-initializer-id:
8879 :: [opt] nested-name-specifier [opt] class-name
8880 identifier
8881
8882 Returns a TYPE indicating the class to be initializer for the first
8883 production. Returns an IDENTIFIER_NODE indicating the data member
8884 to be initialized for the second production. */
8885
8886 static tree
8887 cp_parser_mem_initializer_id (cp_parser* parser)
8888 {
8889 bool global_scope_p;
8890 bool nested_name_specifier_p;
8891 bool template_p = false;
8892 tree id;
8893
8894 /* `typename' is not allowed in this context ([temp.res]). */
8895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8896 {
8897 error ("keyword %<typename%> not allowed in this context (a qualified "
8898 "member initializer is implicitly a type)");
8899 cp_lexer_consume_token (parser->lexer);
8900 }
8901 /* Look for the optional `::' operator. */
8902 global_scope_p
8903 = (cp_parser_global_scope_opt (parser,
8904 /*current_scope_valid_p=*/false)
8905 != NULL_TREE);
8906 /* Look for the optional nested-name-specifier. The simplest way to
8907 implement:
8908
8909 [temp.res]
8910
8911 The keyword `typename' is not permitted in a base-specifier or
8912 mem-initializer; in these contexts a qualified name that
8913 depends on a template-parameter is implicitly assumed to be a
8914 type name.
8915
8916 is to assume that we have seen the `typename' keyword at this
8917 point. */
8918 nested_name_specifier_p
8919 = (cp_parser_nested_name_specifier_opt (parser,
8920 /*typename_keyword_p=*/true,
8921 /*check_dependency_p=*/true,
8922 /*type_p=*/true,
8923 /*is_declaration=*/true)
8924 != NULL_TREE);
8925 if (nested_name_specifier_p)
8926 template_p = cp_parser_optional_template_keyword (parser);
8927 /* If there is a `::' operator or a nested-name-specifier, then we
8928 are definitely looking for a class-name. */
8929 if (global_scope_p || nested_name_specifier_p)
8930 return cp_parser_class_name (parser,
8931 /*typename_keyword_p=*/true,
8932 /*template_keyword_p=*/template_p,
8933 none_type,
8934 /*check_dependency_p=*/true,
8935 /*class_head_p=*/false,
8936 /*is_declaration=*/true);
8937 /* Otherwise, we could also be looking for an ordinary identifier. */
8938 cp_parser_parse_tentatively (parser);
8939 /* Try a class-name. */
8940 id = cp_parser_class_name (parser,
8941 /*typename_keyword_p=*/true,
8942 /*template_keyword_p=*/false,
8943 none_type,
8944 /*check_dependency_p=*/true,
8945 /*class_head_p=*/false,
8946 /*is_declaration=*/true);
8947 /* If we found one, we're done. */
8948 if (cp_parser_parse_definitely (parser))
8949 return id;
8950 /* Otherwise, look for an ordinary identifier. */
8951 return cp_parser_identifier (parser);
8952 }
8953
8954 /* Overloading [gram.over] */
8955
8956 /* Parse an operator-function-id.
8957
8958 operator-function-id:
8959 operator operator
8960
8961 Returns an IDENTIFIER_NODE for the operator which is a
8962 human-readable spelling of the identifier, e.g., `operator +'. */
8963
8964 static tree
8965 cp_parser_operator_function_id (cp_parser* parser)
8966 {
8967 /* Look for the `operator' keyword. */
8968 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8969 return error_mark_node;
8970 /* And then the name of the operator itself. */
8971 return cp_parser_operator (parser);
8972 }
8973
8974 /* Parse an operator.
8975
8976 operator:
8977 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8978 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8979 || ++ -- , ->* -> () []
8980
8981 GNU Extensions:
8982
8983 operator:
8984 <? >? <?= >?=
8985
8986 Returns an IDENTIFIER_NODE for the operator which is a
8987 human-readable spelling of the identifier, e.g., `operator +'. */
8988
8989 static tree
8990 cp_parser_operator (cp_parser* parser)
8991 {
8992 tree id = NULL_TREE;
8993 cp_token *token;
8994
8995 /* Peek at the next token. */
8996 token = cp_lexer_peek_token (parser->lexer);
8997 /* Figure out which operator we have. */
8998 switch (token->type)
8999 {
9000 case CPP_KEYWORD:
9001 {
9002 enum tree_code op;
9003
9004 /* The keyword should be either `new' or `delete'. */
9005 if (token->keyword == RID_NEW)
9006 op = NEW_EXPR;
9007 else if (token->keyword == RID_DELETE)
9008 op = DELETE_EXPR;
9009 else
9010 break;
9011
9012 /* Consume the `new' or `delete' token. */
9013 cp_lexer_consume_token (parser->lexer);
9014
9015 /* Peek at the next token. */
9016 token = cp_lexer_peek_token (parser->lexer);
9017 /* If it's a `[' token then this is the array variant of the
9018 operator. */
9019 if (token->type == CPP_OPEN_SQUARE)
9020 {
9021 /* Consume the `[' token. */
9022 cp_lexer_consume_token (parser->lexer);
9023 /* Look for the `]' token. */
9024 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9025 id = ansi_opname (op == NEW_EXPR
9026 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9027 }
9028 /* Otherwise, we have the non-array variant. */
9029 else
9030 id = ansi_opname (op);
9031
9032 return id;
9033 }
9034
9035 case CPP_PLUS:
9036 id = ansi_opname (PLUS_EXPR);
9037 break;
9038
9039 case CPP_MINUS:
9040 id = ansi_opname (MINUS_EXPR);
9041 break;
9042
9043 case CPP_MULT:
9044 id = ansi_opname (MULT_EXPR);
9045 break;
9046
9047 case CPP_DIV:
9048 id = ansi_opname (TRUNC_DIV_EXPR);
9049 break;
9050
9051 case CPP_MOD:
9052 id = ansi_opname (TRUNC_MOD_EXPR);
9053 break;
9054
9055 case CPP_XOR:
9056 id = ansi_opname (BIT_XOR_EXPR);
9057 break;
9058
9059 case CPP_AND:
9060 id = ansi_opname (BIT_AND_EXPR);
9061 break;
9062
9063 case CPP_OR:
9064 id = ansi_opname (BIT_IOR_EXPR);
9065 break;
9066
9067 case CPP_COMPL:
9068 id = ansi_opname (BIT_NOT_EXPR);
9069 break;
9070
9071 case CPP_NOT:
9072 id = ansi_opname (TRUTH_NOT_EXPR);
9073 break;
9074
9075 case CPP_EQ:
9076 id = ansi_assopname (NOP_EXPR);
9077 break;
9078
9079 case CPP_LESS:
9080 id = ansi_opname (LT_EXPR);
9081 break;
9082
9083 case CPP_GREATER:
9084 id = ansi_opname (GT_EXPR);
9085 break;
9086
9087 case CPP_PLUS_EQ:
9088 id = ansi_assopname (PLUS_EXPR);
9089 break;
9090
9091 case CPP_MINUS_EQ:
9092 id = ansi_assopname (MINUS_EXPR);
9093 break;
9094
9095 case CPP_MULT_EQ:
9096 id = ansi_assopname (MULT_EXPR);
9097 break;
9098
9099 case CPP_DIV_EQ:
9100 id = ansi_assopname (TRUNC_DIV_EXPR);
9101 break;
9102
9103 case CPP_MOD_EQ:
9104 id = ansi_assopname (TRUNC_MOD_EXPR);
9105 break;
9106
9107 case CPP_XOR_EQ:
9108 id = ansi_assopname (BIT_XOR_EXPR);
9109 break;
9110
9111 case CPP_AND_EQ:
9112 id = ansi_assopname (BIT_AND_EXPR);
9113 break;
9114
9115 case CPP_OR_EQ:
9116 id = ansi_assopname (BIT_IOR_EXPR);
9117 break;
9118
9119 case CPP_LSHIFT:
9120 id = ansi_opname (LSHIFT_EXPR);
9121 break;
9122
9123 case CPP_RSHIFT:
9124 id = ansi_opname (RSHIFT_EXPR);
9125 break;
9126
9127 case CPP_LSHIFT_EQ:
9128 id = ansi_assopname (LSHIFT_EXPR);
9129 break;
9130
9131 case CPP_RSHIFT_EQ:
9132 id = ansi_assopname (RSHIFT_EXPR);
9133 break;
9134
9135 case CPP_EQ_EQ:
9136 id = ansi_opname (EQ_EXPR);
9137 break;
9138
9139 case CPP_NOT_EQ:
9140 id = ansi_opname (NE_EXPR);
9141 break;
9142
9143 case CPP_LESS_EQ:
9144 id = ansi_opname (LE_EXPR);
9145 break;
9146
9147 case CPP_GREATER_EQ:
9148 id = ansi_opname (GE_EXPR);
9149 break;
9150
9151 case CPP_AND_AND:
9152 id = ansi_opname (TRUTH_ANDIF_EXPR);
9153 break;
9154
9155 case CPP_OR_OR:
9156 id = ansi_opname (TRUTH_ORIF_EXPR);
9157 break;
9158
9159 case CPP_PLUS_PLUS:
9160 id = ansi_opname (POSTINCREMENT_EXPR);
9161 break;
9162
9163 case CPP_MINUS_MINUS:
9164 id = ansi_opname (PREDECREMENT_EXPR);
9165 break;
9166
9167 case CPP_COMMA:
9168 id = ansi_opname (COMPOUND_EXPR);
9169 break;
9170
9171 case CPP_DEREF_STAR:
9172 id = ansi_opname (MEMBER_REF);
9173 break;
9174
9175 case CPP_DEREF:
9176 id = ansi_opname (COMPONENT_REF);
9177 break;
9178
9179 case CPP_OPEN_PAREN:
9180 /* Consume the `('. */
9181 cp_lexer_consume_token (parser->lexer);
9182 /* Look for the matching `)'. */
9183 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9184 return ansi_opname (CALL_EXPR);
9185
9186 case CPP_OPEN_SQUARE:
9187 /* Consume the `['. */
9188 cp_lexer_consume_token (parser->lexer);
9189 /* Look for the matching `]'. */
9190 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9191 return ansi_opname (ARRAY_REF);
9192
9193 default:
9194 /* Anything else is an error. */
9195 break;
9196 }
9197
9198 /* If we have selected an identifier, we need to consume the
9199 operator token. */
9200 if (id)
9201 cp_lexer_consume_token (parser->lexer);
9202 /* Otherwise, no valid operator name was present. */
9203 else
9204 {
9205 cp_parser_error (parser, "expected operator");
9206 id = error_mark_node;
9207 }
9208
9209 return id;
9210 }
9211
9212 /* Parse a template-declaration.
9213
9214 template-declaration:
9215 export [opt] template < template-parameter-list > declaration
9216
9217 If MEMBER_P is TRUE, this template-declaration occurs within a
9218 class-specifier.
9219
9220 The grammar rule given by the standard isn't correct. What
9221 is really meant is:
9222
9223 template-declaration:
9224 export [opt] template-parameter-list-seq
9225 decl-specifier-seq [opt] init-declarator [opt] ;
9226 export [opt] template-parameter-list-seq
9227 function-definition
9228
9229 template-parameter-list-seq:
9230 template-parameter-list-seq [opt]
9231 template < template-parameter-list > */
9232
9233 static void
9234 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9235 {
9236 /* Check for `export'. */
9237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9238 {
9239 /* Consume the `export' token. */
9240 cp_lexer_consume_token (parser->lexer);
9241 /* Warn that we do not support `export'. */
9242 warning (0, "keyword %<export%> not implemented, and will be ignored");
9243 }
9244
9245 cp_parser_template_declaration_after_export (parser, member_p);
9246 }
9247
9248 /* Parse a template-parameter-list.
9249
9250 template-parameter-list:
9251 template-parameter
9252 template-parameter-list , template-parameter
9253
9254 Returns a TREE_LIST. Each node represents a template parameter.
9255 The nodes are connected via their TREE_CHAINs. */
9256
9257 static tree
9258 cp_parser_template_parameter_list (cp_parser* parser)
9259 {
9260 tree parameter_list = NULL_TREE;
9261
9262 begin_template_parm_list ();
9263 while (true)
9264 {
9265 tree parameter;
9266 cp_token *token;
9267 bool is_non_type;
9268 bool is_parameter_pack;
9269
9270 /* Parse the template-parameter. */
9271 parameter = cp_parser_template_parameter (parser,
9272 &is_non_type,
9273 &is_parameter_pack);
9274 /* Add it to the list. */
9275 if (parameter != error_mark_node)
9276 parameter_list = process_template_parm (parameter_list,
9277 parameter,
9278 is_non_type,
9279 is_parameter_pack);
9280 else
9281 {
9282 tree err_parm = build_tree_list (parameter, parameter);
9283 TREE_VALUE (err_parm) = error_mark_node;
9284 parameter_list = chainon (parameter_list, err_parm);
9285 }
9286
9287 /* Peek at the next token. */
9288 token = cp_lexer_peek_token (parser->lexer);
9289 /* If it's not a `,', we're done. */
9290 if (token->type != CPP_COMMA)
9291 break;
9292 /* Otherwise, consume the `,' token. */
9293 cp_lexer_consume_token (parser->lexer);
9294 }
9295
9296 return end_template_parm_list (parameter_list);
9297 }
9298
9299 /* Parse a template-parameter.
9300
9301 template-parameter:
9302 type-parameter
9303 parameter-declaration
9304
9305 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9306 the parameter. The TREE_PURPOSE is the default value, if any.
9307 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9308 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9309 set to true iff this parameter is a parameter pack. */
9310
9311 static tree
9312 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9313 bool *is_parameter_pack)
9314 {
9315 cp_token *token;
9316 cp_parameter_declarator *parameter_declarator;
9317 cp_declarator *id_declarator;
9318 tree parm;
9319
9320 /* Assume it is a type parameter or a template parameter. */
9321 *is_non_type = false;
9322 /* Assume it not a parameter pack. */
9323 *is_parameter_pack = false;
9324 /* Peek at the next token. */
9325 token = cp_lexer_peek_token (parser->lexer);
9326 /* If it is `class' or `template', we have a type-parameter. */
9327 if (token->keyword == RID_TEMPLATE)
9328 return cp_parser_type_parameter (parser, is_parameter_pack);
9329 /* If it is `class' or `typename' we do not know yet whether it is a
9330 type parameter or a non-type parameter. Consider:
9331
9332 template <typename T, typename T::X X> ...
9333
9334 or:
9335
9336 template <class C, class D*> ...
9337
9338 Here, the first parameter is a type parameter, and the second is
9339 a non-type parameter. We can tell by looking at the token after
9340 the identifier -- if it is a `,', `=', or `>' then we have a type
9341 parameter. */
9342 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9343 {
9344 /* Peek at the token after `class' or `typename'. */
9345 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9346 /* If it's an ellipsis, we have a template type parameter
9347 pack. */
9348 if (token->type == CPP_ELLIPSIS)
9349 return cp_parser_type_parameter (parser, is_parameter_pack);
9350 /* If it's an identifier, skip it. */
9351 if (token->type == CPP_NAME)
9352 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9353 /* Now, see if the token looks like the end of a template
9354 parameter. */
9355 if (token->type == CPP_COMMA
9356 || token->type == CPP_EQ
9357 || token->type == CPP_GREATER)
9358 return cp_parser_type_parameter (parser, is_parameter_pack);
9359 }
9360
9361 /* Otherwise, it is a non-type parameter.
9362
9363 [temp.param]
9364
9365 When parsing a default template-argument for a non-type
9366 template-parameter, the first non-nested `>' is taken as the end
9367 of the template parameter-list rather than a greater-than
9368 operator. */
9369 *is_non_type = true;
9370 parameter_declarator
9371 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9372 /*parenthesized_p=*/NULL);
9373
9374 /* If the parameter declaration is marked as a parameter pack, set
9375 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9376 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9377 grokdeclarator. */
9378 if (parameter_declarator
9379 && parameter_declarator->declarator
9380 && parameter_declarator->declarator->parameter_pack_p)
9381 {
9382 *is_parameter_pack = true;
9383 parameter_declarator->declarator->parameter_pack_p = false;
9384 }
9385
9386 /* If the next token is an ellipsis, and we don't already have it
9387 marked as a parameter pack, then we have a parameter pack (that
9388 has no declarator). */
9389 if (!*is_parameter_pack
9390 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9391 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9392 {
9393 /* Consume the `...'. */
9394 cp_lexer_consume_token (parser->lexer);
9395 maybe_warn_variadic_templates ();
9396
9397 *is_parameter_pack = true;
9398
9399 /* Parameter packs cannot have default arguments. However, a
9400 user may try to do so, so we'll parse them and give an
9401 appropriate diagnostic here. */
9402 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9403 {
9404 /* Consume the `='. */
9405 cp_lexer_consume_token (parser->lexer);
9406
9407 /* Find the name of the parameter pack. */
9408 id_declarator = parameter_declarator->declarator;
9409 while (id_declarator && id_declarator->kind != cdk_id)
9410 id_declarator = id_declarator->declarator;
9411
9412 if (id_declarator && id_declarator->kind == cdk_id)
9413 error ("template parameter pack %qD cannot have a default argument",
9414 id_declarator->u.id.unqualified_name);
9415 else
9416 error ("template parameter pack cannot have a default argument");
9417
9418 /* Parse the default argument, but throw away the result. */
9419 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9420 }
9421 }
9422
9423 parm = grokdeclarator (parameter_declarator->declarator,
9424 &parameter_declarator->decl_specifiers,
9425 PARM, /*initialized=*/0,
9426 /*attrlist=*/NULL);
9427 if (parm == error_mark_node)
9428 return error_mark_node;
9429
9430 return build_tree_list (parameter_declarator->default_argument, parm);
9431 }
9432
9433 /* Parse a type-parameter.
9434
9435 type-parameter:
9436 class identifier [opt]
9437 class identifier [opt] = type-id
9438 typename identifier [opt]
9439 typename identifier [opt] = type-id
9440 template < template-parameter-list > class identifier [opt]
9441 template < template-parameter-list > class identifier [opt]
9442 = id-expression
9443
9444 GNU Extension (variadic templates):
9445
9446 type-parameter:
9447 class ... identifier [opt]
9448 typename ... identifier [opt]
9449
9450 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9451 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9452 the declaration of the parameter.
9453
9454 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9455
9456 static tree
9457 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9458 {
9459 cp_token *token;
9460 tree parameter;
9461
9462 /* Look for a keyword to tell us what kind of parameter this is. */
9463 token = cp_parser_require (parser, CPP_KEYWORD,
9464 "`class', `typename', or `template'");
9465 if (!token)
9466 return error_mark_node;
9467
9468 switch (token->keyword)
9469 {
9470 case RID_CLASS:
9471 case RID_TYPENAME:
9472 {
9473 tree identifier;
9474 tree default_argument;
9475
9476 /* If the next token is an ellipsis, we have a template
9477 argument pack. */
9478 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9479 {
9480 /* Consume the `...' token. */
9481 cp_lexer_consume_token (parser->lexer);
9482 maybe_warn_variadic_templates ();
9483
9484 *is_parameter_pack = true;
9485 }
9486
9487 /* If the next token is an identifier, then it names the
9488 parameter. */
9489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9490 identifier = cp_parser_identifier (parser);
9491 else
9492 identifier = NULL_TREE;
9493
9494 /* Create the parameter. */
9495 parameter = finish_template_type_parm (class_type_node, identifier);
9496
9497 /* If the next token is an `=', we have a default argument. */
9498 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9499 {
9500 /* Consume the `=' token. */
9501 cp_lexer_consume_token (parser->lexer);
9502 /* Parse the default-argument. */
9503 push_deferring_access_checks (dk_no_deferred);
9504 default_argument = cp_parser_type_id (parser);
9505
9506 /* Template parameter packs cannot have default
9507 arguments. */
9508 if (*is_parameter_pack)
9509 {
9510 if (identifier)
9511 error ("template parameter pack %qD cannot have a default argument",
9512 identifier);
9513 else
9514 error ("template parameter packs cannot have default arguments");
9515 default_argument = NULL_TREE;
9516 }
9517 pop_deferring_access_checks ();
9518 }
9519 else
9520 default_argument = NULL_TREE;
9521
9522 /* Create the combined representation of the parameter and the
9523 default argument. */
9524 parameter = build_tree_list (default_argument, parameter);
9525 }
9526 break;
9527
9528 case RID_TEMPLATE:
9529 {
9530 tree parameter_list;
9531 tree identifier;
9532 tree default_argument;
9533
9534 /* Look for the `<'. */
9535 cp_parser_require (parser, CPP_LESS, "`<'");
9536 /* Parse the template-parameter-list. */
9537 parameter_list = cp_parser_template_parameter_list (parser);
9538 /* Look for the `>'. */
9539 cp_parser_require (parser, CPP_GREATER, "`>'");
9540 /* Look for the `class' keyword. */
9541 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9542 /* If the next token is an ellipsis, we have a template
9543 argument pack. */
9544 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9545 {
9546 /* Consume the `...' token. */
9547 cp_lexer_consume_token (parser->lexer);
9548 maybe_warn_variadic_templates ();
9549
9550 *is_parameter_pack = true;
9551 }
9552 /* If the next token is an `=', then there is a
9553 default-argument. If the next token is a `>', we are at
9554 the end of the parameter-list. If the next token is a `,',
9555 then we are at the end of this parameter. */
9556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9557 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9558 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9559 {
9560 identifier = cp_parser_identifier (parser);
9561 /* Treat invalid names as if the parameter were nameless. */
9562 if (identifier == error_mark_node)
9563 identifier = NULL_TREE;
9564 }
9565 else
9566 identifier = NULL_TREE;
9567
9568 /* Create the template parameter. */
9569 parameter = finish_template_template_parm (class_type_node,
9570 identifier);
9571
9572 /* If the next token is an `=', then there is a
9573 default-argument. */
9574 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9575 {
9576 bool is_template;
9577
9578 /* Consume the `='. */
9579 cp_lexer_consume_token (parser->lexer);
9580 /* Parse the id-expression. */
9581 push_deferring_access_checks (dk_no_deferred);
9582 default_argument
9583 = cp_parser_id_expression (parser,
9584 /*template_keyword_p=*/false,
9585 /*check_dependency_p=*/true,
9586 /*template_p=*/&is_template,
9587 /*declarator_p=*/false,
9588 /*optional_p=*/false);
9589 if (TREE_CODE (default_argument) == TYPE_DECL)
9590 /* If the id-expression was a template-id that refers to
9591 a template-class, we already have the declaration here,
9592 so no further lookup is needed. */
9593 ;
9594 else
9595 /* Look up the name. */
9596 default_argument
9597 = cp_parser_lookup_name (parser, default_argument,
9598 none_type,
9599 /*is_template=*/is_template,
9600 /*is_namespace=*/false,
9601 /*check_dependency=*/true,
9602 /*ambiguous_decls=*/NULL);
9603 /* See if the default argument is valid. */
9604 default_argument
9605 = check_template_template_default_arg (default_argument);
9606
9607 /* Template parameter packs cannot have default
9608 arguments. */
9609 if (*is_parameter_pack)
9610 {
9611 if (identifier)
9612 error ("template parameter pack %qD cannot have a default argument",
9613 identifier);
9614 else
9615 error ("template parameter packs cannot have default arguments");
9616 default_argument = NULL_TREE;
9617 }
9618 pop_deferring_access_checks ();
9619 }
9620 else
9621 default_argument = NULL_TREE;
9622
9623 /* Create the combined representation of the parameter and the
9624 default argument. */
9625 parameter = build_tree_list (default_argument, parameter);
9626 }
9627 break;
9628
9629 default:
9630 gcc_unreachable ();
9631 break;
9632 }
9633
9634 return parameter;
9635 }
9636
9637 /* Parse a template-id.
9638
9639 template-id:
9640 template-name < template-argument-list [opt] >
9641
9642 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9643 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9644 returned. Otherwise, if the template-name names a function, or set
9645 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9646 names a class, returns a TYPE_DECL for the specialization.
9647
9648 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9649 uninstantiated templates. */
9650
9651 static tree
9652 cp_parser_template_id (cp_parser *parser,
9653 bool template_keyword_p,
9654 bool check_dependency_p,
9655 bool is_declaration)
9656 {
9657 int i;
9658 tree template;
9659 tree arguments;
9660 tree template_id;
9661 cp_token_position start_of_id = 0;
9662 deferred_access_check *chk;
9663 VEC (deferred_access_check,gc) *access_check;
9664 cp_token *next_token, *next_token_2;
9665 bool is_identifier;
9666
9667 /* If the next token corresponds to a template-id, there is no need
9668 to reparse it. */
9669 next_token = cp_lexer_peek_token (parser->lexer);
9670 if (next_token->type == CPP_TEMPLATE_ID)
9671 {
9672 struct tree_check *check_value;
9673
9674 /* Get the stored value. */
9675 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9676 /* Perform any access checks that were deferred. */
9677 access_check = check_value->checks;
9678 if (access_check)
9679 {
9680 for (i = 0 ;
9681 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9682 ++i)
9683 {
9684 perform_or_defer_access_check (chk->binfo,
9685 chk->decl,
9686 chk->diag_decl);
9687 }
9688 }
9689 /* Return the stored value. */
9690 return check_value->value;
9691 }
9692
9693 /* Avoid performing name lookup if there is no possibility of
9694 finding a template-id. */
9695 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9696 || (next_token->type == CPP_NAME
9697 && !cp_parser_nth_token_starts_template_argument_list_p
9698 (parser, 2)))
9699 {
9700 cp_parser_error (parser, "expected template-id");
9701 return error_mark_node;
9702 }
9703
9704 /* Remember where the template-id starts. */
9705 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9706 start_of_id = cp_lexer_token_position (parser->lexer, false);
9707
9708 push_deferring_access_checks (dk_deferred);
9709
9710 /* Parse the template-name. */
9711 is_identifier = false;
9712 template = cp_parser_template_name (parser, template_keyword_p,
9713 check_dependency_p,
9714 is_declaration,
9715 &is_identifier);
9716 if (template == error_mark_node || is_identifier)
9717 {
9718 pop_deferring_access_checks ();
9719 return template;
9720 }
9721
9722 /* If we find the sequence `[:' after a template-name, it's probably
9723 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9724 parse correctly the argument list. */
9725 next_token = cp_lexer_peek_token (parser->lexer);
9726 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9727 if (next_token->type == CPP_OPEN_SQUARE
9728 && next_token->flags & DIGRAPH
9729 && next_token_2->type == CPP_COLON
9730 && !(next_token_2->flags & PREV_WHITE))
9731 {
9732 cp_parser_parse_tentatively (parser);
9733 /* Change `:' into `::'. */
9734 next_token_2->type = CPP_SCOPE;
9735 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9736 CPP_LESS. */
9737 cp_lexer_consume_token (parser->lexer);
9738 /* Parse the arguments. */
9739 arguments = cp_parser_enclosed_template_argument_list (parser);
9740 if (!cp_parser_parse_definitely (parser))
9741 {
9742 /* If we couldn't parse an argument list, then we revert our changes
9743 and return simply an error. Maybe this is not a template-id
9744 after all. */
9745 next_token_2->type = CPP_COLON;
9746 cp_parser_error (parser, "expected %<<%>");
9747 pop_deferring_access_checks ();
9748 return error_mark_node;
9749 }
9750 /* Otherwise, emit an error about the invalid digraph, but continue
9751 parsing because we got our argument list. */
9752 pedwarn ("%<<::%> cannot begin a template-argument list");
9753 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9754 "between %<<%> and %<::%>");
9755 if (!flag_permissive)
9756 {
9757 static bool hint;
9758 if (!hint)
9759 {
9760 inform ("(if you use -fpermissive G++ will accept your code)");
9761 hint = true;
9762 }
9763 }
9764 }
9765 else
9766 {
9767 /* Look for the `<' that starts the template-argument-list. */
9768 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9769 {
9770 pop_deferring_access_checks ();
9771 return error_mark_node;
9772 }
9773 /* Parse the arguments. */
9774 arguments = cp_parser_enclosed_template_argument_list (parser);
9775 }
9776
9777 /* Build a representation of the specialization. */
9778 if (TREE_CODE (template) == IDENTIFIER_NODE)
9779 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9780 else if (DECL_CLASS_TEMPLATE_P (template)
9781 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9782 {
9783 bool entering_scope;
9784 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9785 template (rather than some instantiation thereof) only if
9786 is not nested within some other construct. For example, in
9787 "template <typename T> void f(T) { A<T>::", A<T> is just an
9788 instantiation of A. */
9789 entering_scope = (template_parm_scope_p ()
9790 && cp_lexer_next_token_is (parser->lexer,
9791 CPP_SCOPE));
9792 template_id
9793 = finish_template_type (template, arguments, entering_scope);
9794 }
9795 else
9796 {
9797 /* If it's not a class-template or a template-template, it should be
9798 a function-template. */
9799 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9800 || TREE_CODE (template) == OVERLOAD
9801 || BASELINK_P (template)));
9802
9803 template_id = lookup_template_function (template, arguments);
9804 }
9805
9806 /* If parsing tentatively, replace the sequence of tokens that makes
9807 up the template-id with a CPP_TEMPLATE_ID token. That way,
9808 should we re-parse the token stream, we will not have to repeat
9809 the effort required to do the parse, nor will we issue duplicate
9810 error messages about problems during instantiation of the
9811 template. */
9812 if (start_of_id)
9813 {
9814 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9815
9816 /* Reset the contents of the START_OF_ID token. */
9817 token->type = CPP_TEMPLATE_ID;
9818 /* Retrieve any deferred checks. Do not pop this access checks yet
9819 so the memory will not be reclaimed during token replacing below. */
9820 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9821 token->u.tree_check_value->value = template_id;
9822 token->u.tree_check_value->checks = get_deferred_access_checks ();
9823 token->keyword = RID_MAX;
9824
9825 /* Purge all subsequent tokens. */
9826 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9827
9828 /* ??? Can we actually assume that, if template_id ==
9829 error_mark_node, we will have issued a diagnostic to the
9830 user, as opposed to simply marking the tentative parse as
9831 failed? */
9832 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9833 error ("parse error in template argument list");
9834 }
9835
9836 pop_deferring_access_checks ();
9837 return template_id;
9838 }
9839
9840 /* Parse a template-name.
9841
9842 template-name:
9843 identifier
9844
9845 The standard should actually say:
9846
9847 template-name:
9848 identifier
9849 operator-function-id
9850
9851 A defect report has been filed about this issue.
9852
9853 A conversion-function-id cannot be a template name because they cannot
9854 be part of a template-id. In fact, looking at this code:
9855
9856 a.operator K<int>()
9857
9858 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9859 It is impossible to call a templated conversion-function-id with an
9860 explicit argument list, since the only allowed template parameter is
9861 the type to which it is converting.
9862
9863 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9864 `template' keyword, in a construction like:
9865
9866 T::template f<3>()
9867
9868 In that case `f' is taken to be a template-name, even though there
9869 is no way of knowing for sure.
9870
9871 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9872 name refers to a set of overloaded functions, at least one of which
9873 is a template, or an IDENTIFIER_NODE with the name of the template,
9874 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9875 names are looked up inside uninstantiated templates. */
9876
9877 static tree
9878 cp_parser_template_name (cp_parser* parser,
9879 bool template_keyword_p,
9880 bool check_dependency_p,
9881 bool is_declaration,
9882 bool *is_identifier)
9883 {
9884 tree identifier;
9885 tree decl;
9886 tree fns;
9887
9888 /* If the next token is `operator', then we have either an
9889 operator-function-id or a conversion-function-id. */
9890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9891 {
9892 /* We don't know whether we're looking at an
9893 operator-function-id or a conversion-function-id. */
9894 cp_parser_parse_tentatively (parser);
9895 /* Try an operator-function-id. */
9896 identifier = cp_parser_operator_function_id (parser);
9897 /* If that didn't work, try a conversion-function-id. */
9898 if (!cp_parser_parse_definitely (parser))
9899 {
9900 cp_parser_error (parser, "expected template-name");
9901 return error_mark_node;
9902 }
9903 }
9904 /* Look for the identifier. */
9905 else
9906 identifier = cp_parser_identifier (parser);
9907
9908 /* If we didn't find an identifier, we don't have a template-id. */
9909 if (identifier == error_mark_node)
9910 return error_mark_node;
9911
9912 /* If the name immediately followed the `template' keyword, then it
9913 is a template-name. However, if the next token is not `<', then
9914 we do not treat it as a template-name, since it is not being used
9915 as part of a template-id. This enables us to handle constructs
9916 like:
9917
9918 template <typename T> struct S { S(); };
9919 template <typename T> S<T>::S();
9920
9921 correctly. We would treat `S' as a template -- if it were `S<T>'
9922 -- but we do not if there is no `<'. */
9923
9924 if (processing_template_decl
9925 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9926 {
9927 /* In a declaration, in a dependent context, we pretend that the
9928 "template" keyword was present in order to improve error
9929 recovery. For example, given:
9930
9931 template <typename T> void f(T::X<int>);
9932
9933 we want to treat "X<int>" as a template-id. */
9934 if (is_declaration
9935 && !template_keyword_p
9936 && parser->scope && TYPE_P (parser->scope)
9937 && check_dependency_p
9938 && dependent_type_p (parser->scope)
9939 /* Do not do this for dtors (or ctors), since they never
9940 need the template keyword before their name. */
9941 && !constructor_name_p (identifier, parser->scope))
9942 {
9943 cp_token_position start = 0;
9944
9945 /* Explain what went wrong. */
9946 error ("non-template %qD used as template", identifier);
9947 inform ("use %<%T::template %D%> to indicate that it is a template",
9948 parser->scope, identifier);
9949 /* If parsing tentatively, find the location of the "<" token. */
9950 if (cp_parser_simulate_error (parser))
9951 start = cp_lexer_token_position (parser->lexer, true);
9952 /* Parse the template arguments so that we can issue error
9953 messages about them. */
9954 cp_lexer_consume_token (parser->lexer);
9955 cp_parser_enclosed_template_argument_list (parser);
9956 /* Skip tokens until we find a good place from which to
9957 continue parsing. */
9958 cp_parser_skip_to_closing_parenthesis (parser,
9959 /*recovering=*/true,
9960 /*or_comma=*/true,
9961 /*consume_paren=*/false);
9962 /* If parsing tentatively, permanently remove the
9963 template argument list. That will prevent duplicate
9964 error messages from being issued about the missing
9965 "template" keyword. */
9966 if (start)
9967 cp_lexer_purge_tokens_after (parser->lexer, start);
9968 if (is_identifier)
9969 *is_identifier = true;
9970 return identifier;
9971 }
9972
9973 /* If the "template" keyword is present, then there is generally
9974 no point in doing name-lookup, so we just return IDENTIFIER.
9975 But, if the qualifying scope is non-dependent then we can
9976 (and must) do name-lookup normally. */
9977 if (template_keyword_p
9978 && (!parser->scope
9979 || (TYPE_P (parser->scope)
9980 && dependent_type_p (parser->scope))))
9981 return identifier;
9982 }
9983
9984 /* Look up the name. */
9985 decl = cp_parser_lookup_name (parser, identifier,
9986 none_type,
9987 /*is_template=*/false,
9988 /*is_namespace=*/false,
9989 check_dependency_p,
9990 /*ambiguous_decls=*/NULL);
9991 decl = maybe_get_template_decl_from_type_decl (decl);
9992
9993 /* If DECL is a template, then the name was a template-name. */
9994 if (TREE_CODE (decl) == TEMPLATE_DECL)
9995 ;
9996 else
9997 {
9998 tree fn = NULL_TREE;
9999
10000 /* The standard does not explicitly indicate whether a name that
10001 names a set of overloaded declarations, some of which are
10002 templates, is a template-name. However, such a name should
10003 be a template-name; otherwise, there is no way to form a
10004 template-id for the overloaded templates. */
10005 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10006 if (TREE_CODE (fns) == OVERLOAD)
10007 for (fn = fns; fn; fn = OVL_NEXT (fn))
10008 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10009 break;
10010
10011 if (!fn)
10012 {
10013 /* The name does not name a template. */
10014 cp_parser_error (parser, "expected template-name");
10015 return error_mark_node;
10016 }
10017 }
10018
10019 /* If DECL is dependent, and refers to a function, then just return
10020 its name; we will look it up again during template instantiation. */
10021 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10022 {
10023 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10024 if (TYPE_P (scope) && dependent_type_p (scope))
10025 return identifier;
10026 }
10027
10028 return decl;
10029 }
10030
10031 /* Parse a template-argument-list.
10032
10033 template-argument-list:
10034 template-argument ... [opt]
10035 template-argument-list , template-argument ... [opt]
10036
10037 Returns a TREE_VEC containing the arguments. */
10038
10039 static tree
10040 cp_parser_template_argument_list (cp_parser* parser)
10041 {
10042 tree fixed_args[10];
10043 unsigned n_args = 0;
10044 unsigned alloced = 10;
10045 tree *arg_ary = fixed_args;
10046 tree vec;
10047 bool saved_in_template_argument_list_p;
10048 bool saved_ice_p;
10049 bool saved_non_ice_p;
10050
10051 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10052 parser->in_template_argument_list_p = true;
10053 /* Even if the template-id appears in an integral
10054 constant-expression, the contents of the argument list do
10055 not. */
10056 saved_ice_p = parser->integral_constant_expression_p;
10057 parser->integral_constant_expression_p = false;
10058 saved_non_ice_p = parser->non_integral_constant_expression_p;
10059 parser->non_integral_constant_expression_p = false;
10060 /* Parse the arguments. */
10061 do
10062 {
10063 tree argument;
10064
10065 if (n_args)
10066 /* Consume the comma. */
10067 cp_lexer_consume_token (parser->lexer);
10068
10069 /* Parse the template-argument. */
10070 argument = cp_parser_template_argument (parser);
10071
10072 /* If the next token is an ellipsis, we're expanding a template
10073 argument pack. */
10074 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10075 {
10076 /* Consume the `...' token. */
10077 cp_lexer_consume_token (parser->lexer);
10078
10079 /* Make the argument into a TYPE_PACK_EXPANSION or
10080 EXPR_PACK_EXPANSION. */
10081 argument = make_pack_expansion (argument);
10082 }
10083
10084 if (n_args == alloced)
10085 {
10086 alloced *= 2;
10087
10088 if (arg_ary == fixed_args)
10089 {
10090 arg_ary = XNEWVEC (tree, alloced);
10091 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10092 }
10093 else
10094 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10095 }
10096 arg_ary[n_args++] = argument;
10097 }
10098 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10099
10100 vec = make_tree_vec (n_args);
10101
10102 while (n_args--)
10103 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10104
10105 if (arg_ary != fixed_args)
10106 free (arg_ary);
10107 parser->non_integral_constant_expression_p = saved_non_ice_p;
10108 parser->integral_constant_expression_p = saved_ice_p;
10109 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10110 return vec;
10111 }
10112
10113 /* Parse a template-argument.
10114
10115 template-argument:
10116 assignment-expression
10117 type-id
10118 id-expression
10119
10120 The representation is that of an assignment-expression, type-id, or
10121 id-expression -- except that the qualified id-expression is
10122 evaluated, so that the value returned is either a DECL or an
10123 OVERLOAD.
10124
10125 Although the standard says "assignment-expression", it forbids
10126 throw-expressions or assignments in the template argument.
10127 Therefore, we use "conditional-expression" instead. */
10128
10129 static tree
10130 cp_parser_template_argument (cp_parser* parser)
10131 {
10132 tree argument;
10133 bool template_p;
10134 bool address_p;
10135 bool maybe_type_id = false;
10136 cp_token *token;
10137 cp_id_kind idk;
10138
10139 /* There's really no way to know what we're looking at, so we just
10140 try each alternative in order.
10141
10142 [temp.arg]
10143
10144 In a template-argument, an ambiguity between a type-id and an
10145 expression is resolved to a type-id, regardless of the form of
10146 the corresponding template-parameter.
10147
10148 Therefore, we try a type-id first. */
10149 cp_parser_parse_tentatively (parser);
10150 argument = cp_parser_type_id (parser);
10151 /* If there was no error parsing the type-id but the next token is a '>>',
10152 we probably found a typo for '> >'. But there are type-id which are
10153 also valid expressions. For instance:
10154
10155 struct X { int operator >> (int); };
10156 template <int V> struct Foo {};
10157 Foo<X () >> 5> r;
10158
10159 Here 'X()' is a valid type-id of a function type, but the user just
10160 wanted to write the expression "X() >> 5". Thus, we remember that we
10161 found a valid type-id, but we still try to parse the argument as an
10162 expression to see what happens. */
10163 if (!cp_parser_error_occurred (parser)
10164 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10165 {
10166 maybe_type_id = true;
10167 cp_parser_abort_tentative_parse (parser);
10168 }
10169 else
10170 {
10171 /* If the next token isn't a `,' or a `>', then this argument wasn't
10172 really finished. This means that the argument is not a valid
10173 type-id. */
10174 if (!cp_parser_next_token_ends_template_argument_p (parser))
10175 cp_parser_error (parser, "expected template-argument");
10176 /* If that worked, we're done. */
10177 if (cp_parser_parse_definitely (parser))
10178 return argument;
10179 }
10180 /* We're still not sure what the argument will be. */
10181 cp_parser_parse_tentatively (parser);
10182 /* Try a template. */
10183 argument = cp_parser_id_expression (parser,
10184 /*template_keyword_p=*/false,
10185 /*check_dependency_p=*/true,
10186 &template_p,
10187 /*declarator_p=*/false,
10188 /*optional_p=*/false);
10189 /* If the next token isn't a `,' or a `>', then this argument wasn't
10190 really finished. */
10191 if (!cp_parser_next_token_ends_template_argument_p (parser))
10192 cp_parser_error (parser, "expected template-argument");
10193 if (!cp_parser_error_occurred (parser))
10194 {
10195 /* Figure out what is being referred to. If the id-expression
10196 was for a class template specialization, then we will have a
10197 TYPE_DECL at this point. There is no need to do name lookup
10198 at this point in that case. */
10199 if (TREE_CODE (argument) != TYPE_DECL)
10200 argument = cp_parser_lookup_name (parser, argument,
10201 none_type,
10202 /*is_template=*/template_p,
10203 /*is_namespace=*/false,
10204 /*check_dependency=*/true,
10205 /*ambiguous_decls=*/NULL);
10206 if (TREE_CODE (argument) != TEMPLATE_DECL
10207 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10208 cp_parser_error (parser, "expected template-name");
10209 }
10210 if (cp_parser_parse_definitely (parser))
10211 return argument;
10212 /* It must be a non-type argument. There permitted cases are given
10213 in [temp.arg.nontype]:
10214
10215 -- an integral constant-expression of integral or enumeration
10216 type; or
10217
10218 -- the name of a non-type template-parameter; or
10219
10220 -- the name of an object or function with external linkage...
10221
10222 -- the address of an object or function with external linkage...
10223
10224 -- a pointer to member... */
10225 /* Look for a non-type template parameter. */
10226 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10227 {
10228 cp_parser_parse_tentatively (parser);
10229 argument = cp_parser_primary_expression (parser,
10230 /*adress_p=*/false,
10231 /*cast_p=*/false,
10232 /*template_arg_p=*/true,
10233 &idk);
10234 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10235 || !cp_parser_next_token_ends_template_argument_p (parser))
10236 cp_parser_simulate_error (parser);
10237 if (cp_parser_parse_definitely (parser))
10238 return argument;
10239 }
10240
10241 /* If the next token is "&", the argument must be the address of an
10242 object or function with external linkage. */
10243 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10244 if (address_p)
10245 cp_lexer_consume_token (parser->lexer);
10246 /* See if we might have an id-expression. */
10247 token = cp_lexer_peek_token (parser->lexer);
10248 if (token->type == CPP_NAME
10249 || token->keyword == RID_OPERATOR
10250 || token->type == CPP_SCOPE
10251 || token->type == CPP_TEMPLATE_ID
10252 || token->type == CPP_NESTED_NAME_SPECIFIER)
10253 {
10254 cp_parser_parse_tentatively (parser);
10255 argument = cp_parser_primary_expression (parser,
10256 address_p,
10257 /*cast_p=*/false,
10258 /*template_arg_p=*/true,
10259 &idk);
10260 if (cp_parser_error_occurred (parser)
10261 || !cp_parser_next_token_ends_template_argument_p (parser))
10262 cp_parser_abort_tentative_parse (parser);
10263 else
10264 {
10265 if (TREE_CODE (argument) == INDIRECT_REF)
10266 {
10267 gcc_assert (REFERENCE_REF_P (argument));
10268 argument = TREE_OPERAND (argument, 0);
10269 }
10270
10271 if (TREE_CODE (argument) == VAR_DECL)
10272 {
10273 /* A variable without external linkage might still be a
10274 valid constant-expression, so no error is issued here
10275 if the external-linkage check fails. */
10276 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10277 cp_parser_simulate_error (parser);
10278 }
10279 else if (is_overloaded_fn (argument))
10280 /* All overloaded functions are allowed; if the external
10281 linkage test does not pass, an error will be issued
10282 later. */
10283 ;
10284 else if (address_p
10285 && (TREE_CODE (argument) == OFFSET_REF
10286 || TREE_CODE (argument) == SCOPE_REF))
10287 /* A pointer-to-member. */
10288 ;
10289 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10290 ;
10291 else
10292 cp_parser_simulate_error (parser);
10293
10294 if (cp_parser_parse_definitely (parser))
10295 {
10296 if (address_p)
10297 argument = build_x_unary_op (ADDR_EXPR, argument);
10298 return argument;
10299 }
10300 }
10301 }
10302 /* If the argument started with "&", there are no other valid
10303 alternatives at this point. */
10304 if (address_p)
10305 {
10306 cp_parser_error (parser, "invalid non-type template argument");
10307 return error_mark_node;
10308 }
10309
10310 /* If the argument wasn't successfully parsed as a type-id followed
10311 by '>>', the argument can only be a constant expression now.
10312 Otherwise, we try parsing the constant-expression tentatively,
10313 because the argument could really be a type-id. */
10314 if (maybe_type_id)
10315 cp_parser_parse_tentatively (parser);
10316 argument = cp_parser_constant_expression (parser,
10317 /*allow_non_constant_p=*/false,
10318 /*non_constant_p=*/NULL);
10319 argument = fold_non_dependent_expr (argument);
10320 if (!maybe_type_id)
10321 return argument;
10322 if (!cp_parser_next_token_ends_template_argument_p (parser))
10323 cp_parser_error (parser, "expected template-argument");
10324 if (cp_parser_parse_definitely (parser))
10325 return argument;
10326 /* We did our best to parse the argument as a non type-id, but that
10327 was the only alternative that matched (albeit with a '>' after
10328 it). We can assume it's just a typo from the user, and a
10329 diagnostic will then be issued. */
10330 return cp_parser_type_id (parser);
10331 }
10332
10333 /* Parse an explicit-instantiation.
10334
10335 explicit-instantiation:
10336 template declaration
10337
10338 Although the standard says `declaration', what it really means is:
10339
10340 explicit-instantiation:
10341 template decl-specifier-seq [opt] declarator [opt] ;
10342
10343 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10344 supposed to be allowed. A defect report has been filed about this
10345 issue.
10346
10347 GNU Extension:
10348
10349 explicit-instantiation:
10350 storage-class-specifier template
10351 decl-specifier-seq [opt] declarator [opt] ;
10352 function-specifier template
10353 decl-specifier-seq [opt] declarator [opt] ; */
10354
10355 static void
10356 cp_parser_explicit_instantiation (cp_parser* parser)
10357 {
10358 int declares_class_or_enum;
10359 cp_decl_specifier_seq decl_specifiers;
10360 tree extension_specifier = NULL_TREE;
10361
10362 /* Look for an (optional) storage-class-specifier or
10363 function-specifier. */
10364 if (cp_parser_allow_gnu_extensions_p (parser))
10365 {
10366 extension_specifier
10367 = cp_parser_storage_class_specifier_opt (parser);
10368 if (!extension_specifier)
10369 extension_specifier
10370 = cp_parser_function_specifier_opt (parser,
10371 /*decl_specs=*/NULL);
10372 }
10373
10374 /* Look for the `template' keyword. */
10375 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10376 /* Let the front end know that we are processing an explicit
10377 instantiation. */
10378 begin_explicit_instantiation ();
10379 /* [temp.explicit] says that we are supposed to ignore access
10380 control while processing explicit instantiation directives. */
10381 push_deferring_access_checks (dk_no_check);
10382 /* Parse a decl-specifier-seq. */
10383 cp_parser_decl_specifier_seq (parser,
10384 CP_PARSER_FLAGS_OPTIONAL,
10385 &decl_specifiers,
10386 &declares_class_or_enum);
10387 /* If there was exactly one decl-specifier, and it declared a class,
10388 and there's no declarator, then we have an explicit type
10389 instantiation. */
10390 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10391 {
10392 tree type;
10393
10394 type = check_tag_decl (&decl_specifiers);
10395 /* Turn access control back on for names used during
10396 template instantiation. */
10397 pop_deferring_access_checks ();
10398 if (type)
10399 do_type_instantiation (type, extension_specifier,
10400 /*complain=*/tf_error);
10401 }
10402 else
10403 {
10404 cp_declarator *declarator;
10405 tree decl;
10406
10407 /* Parse the declarator. */
10408 declarator
10409 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10410 /*ctor_dtor_or_conv_p=*/NULL,
10411 /*parenthesized_p=*/NULL,
10412 /*member_p=*/false);
10413 if (declares_class_or_enum & 2)
10414 cp_parser_check_for_definition_in_return_type (declarator,
10415 decl_specifiers.type);
10416 if (declarator != cp_error_declarator)
10417 {
10418 decl = grokdeclarator (declarator, &decl_specifiers,
10419 NORMAL, 0, &decl_specifiers.attributes);
10420 /* Turn access control back on for names used during
10421 template instantiation. */
10422 pop_deferring_access_checks ();
10423 /* Do the explicit instantiation. */
10424 do_decl_instantiation (decl, extension_specifier);
10425 }
10426 else
10427 {
10428 pop_deferring_access_checks ();
10429 /* Skip the body of the explicit instantiation. */
10430 cp_parser_skip_to_end_of_statement (parser);
10431 }
10432 }
10433 /* We're done with the instantiation. */
10434 end_explicit_instantiation ();
10435
10436 cp_parser_consume_semicolon_at_end_of_statement (parser);
10437 }
10438
10439 /* Parse an explicit-specialization.
10440
10441 explicit-specialization:
10442 template < > declaration
10443
10444 Although the standard says `declaration', what it really means is:
10445
10446 explicit-specialization:
10447 template <> decl-specifier [opt] init-declarator [opt] ;
10448 template <> function-definition
10449 template <> explicit-specialization
10450 template <> template-declaration */
10451
10452 static void
10453 cp_parser_explicit_specialization (cp_parser* parser)
10454 {
10455 bool need_lang_pop;
10456 /* Look for the `template' keyword. */
10457 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10458 /* Look for the `<'. */
10459 cp_parser_require (parser, CPP_LESS, "`<'");
10460 /* Look for the `>'. */
10461 cp_parser_require (parser, CPP_GREATER, "`>'");
10462 /* We have processed another parameter list. */
10463 ++parser->num_template_parameter_lists;
10464 /* [temp]
10465
10466 A template ... explicit specialization ... shall not have C
10467 linkage. */
10468 if (current_lang_name == lang_name_c)
10469 {
10470 error ("template specialization with C linkage");
10471 /* Give it C++ linkage to avoid confusing other parts of the
10472 front end. */
10473 push_lang_context (lang_name_cplusplus);
10474 need_lang_pop = true;
10475 }
10476 else
10477 need_lang_pop = false;
10478 /* Let the front end know that we are beginning a specialization. */
10479 if (!begin_specialization ())
10480 {
10481 end_specialization ();
10482 cp_parser_skip_to_end_of_block_or_statement (parser);
10483 return;
10484 }
10485
10486 /* If the next keyword is `template', we need to figure out whether
10487 or not we're looking a template-declaration. */
10488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10489 {
10490 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10491 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10492 cp_parser_template_declaration_after_export (parser,
10493 /*member_p=*/false);
10494 else
10495 cp_parser_explicit_specialization (parser);
10496 }
10497 else
10498 /* Parse the dependent declaration. */
10499 cp_parser_single_declaration (parser,
10500 /*checks=*/NULL,
10501 /*member_p=*/false,
10502 /*explicit_specialization_p=*/true,
10503 /*friend_p=*/NULL);
10504 /* We're done with the specialization. */
10505 end_specialization ();
10506 /* For the erroneous case of a template with C linkage, we pushed an
10507 implicit C++ linkage scope; exit that scope now. */
10508 if (need_lang_pop)
10509 pop_lang_context ();
10510 /* We're done with this parameter list. */
10511 --parser->num_template_parameter_lists;
10512 }
10513
10514 /* Parse a type-specifier.
10515
10516 type-specifier:
10517 simple-type-specifier
10518 class-specifier
10519 enum-specifier
10520 elaborated-type-specifier
10521 cv-qualifier
10522
10523 GNU Extension:
10524
10525 type-specifier:
10526 __complex__
10527
10528 Returns a representation of the type-specifier. For a
10529 class-specifier, enum-specifier, or elaborated-type-specifier, a
10530 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10531
10532 The parser flags FLAGS is used to control type-specifier parsing.
10533
10534 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10535 in a decl-specifier-seq.
10536
10537 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10538 class-specifier, enum-specifier, or elaborated-type-specifier, then
10539 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10540 if a type is declared; 2 if it is defined. Otherwise, it is set to
10541 zero.
10542
10543 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10544 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10545 is set to FALSE. */
10546
10547 static tree
10548 cp_parser_type_specifier (cp_parser* parser,
10549 cp_parser_flags flags,
10550 cp_decl_specifier_seq *decl_specs,
10551 bool is_declaration,
10552 int* declares_class_or_enum,
10553 bool* is_cv_qualifier)
10554 {
10555 tree type_spec = NULL_TREE;
10556 cp_token *token;
10557 enum rid keyword;
10558 cp_decl_spec ds = ds_last;
10559
10560 /* Assume this type-specifier does not declare a new type. */
10561 if (declares_class_or_enum)
10562 *declares_class_or_enum = 0;
10563 /* And that it does not specify a cv-qualifier. */
10564 if (is_cv_qualifier)
10565 *is_cv_qualifier = false;
10566 /* Peek at the next token. */
10567 token = cp_lexer_peek_token (parser->lexer);
10568
10569 /* If we're looking at a keyword, we can use that to guide the
10570 production we choose. */
10571 keyword = token->keyword;
10572 switch (keyword)
10573 {
10574 case RID_ENUM:
10575 /* Look for the enum-specifier. */
10576 type_spec = cp_parser_enum_specifier (parser);
10577 /* If that worked, we're done. */
10578 if (type_spec)
10579 {
10580 if (declares_class_or_enum)
10581 *declares_class_or_enum = 2;
10582 if (decl_specs)
10583 cp_parser_set_decl_spec_type (decl_specs,
10584 type_spec,
10585 /*user_defined_p=*/true);
10586 return type_spec;
10587 }
10588 else
10589 goto elaborated_type_specifier;
10590
10591 /* Any of these indicate either a class-specifier, or an
10592 elaborated-type-specifier. */
10593 case RID_CLASS:
10594 case RID_STRUCT:
10595 case RID_UNION:
10596 /* Parse tentatively so that we can back up if we don't find a
10597 class-specifier. */
10598 cp_parser_parse_tentatively (parser);
10599 /* Look for the class-specifier. */
10600 type_spec = cp_parser_class_specifier (parser);
10601 /* If that worked, we're done. */
10602 if (cp_parser_parse_definitely (parser))
10603 {
10604 if (declares_class_or_enum)
10605 *declares_class_or_enum = 2;
10606 if (decl_specs)
10607 cp_parser_set_decl_spec_type (decl_specs,
10608 type_spec,
10609 /*user_defined_p=*/true);
10610 return type_spec;
10611 }
10612
10613 /* Fall through. */
10614 elaborated_type_specifier:
10615 /* We're declaring (not defining) a class or enum. */
10616 if (declares_class_or_enum)
10617 *declares_class_or_enum = 1;
10618
10619 /* Fall through. */
10620 case RID_TYPENAME:
10621 /* Look for an elaborated-type-specifier. */
10622 type_spec
10623 = (cp_parser_elaborated_type_specifier
10624 (parser,
10625 decl_specs && decl_specs->specs[(int) ds_friend],
10626 is_declaration));
10627 if (decl_specs)
10628 cp_parser_set_decl_spec_type (decl_specs,
10629 type_spec,
10630 /*user_defined_p=*/true);
10631 return type_spec;
10632
10633 case RID_CONST:
10634 ds = ds_const;
10635 if (is_cv_qualifier)
10636 *is_cv_qualifier = true;
10637 break;
10638
10639 case RID_VOLATILE:
10640 ds = ds_volatile;
10641 if (is_cv_qualifier)
10642 *is_cv_qualifier = true;
10643 break;
10644
10645 case RID_RESTRICT:
10646 ds = ds_restrict;
10647 if (is_cv_qualifier)
10648 *is_cv_qualifier = true;
10649 break;
10650
10651 case RID_COMPLEX:
10652 /* The `__complex__' keyword is a GNU extension. */
10653 ds = ds_complex;
10654 break;
10655
10656 default:
10657 break;
10658 }
10659
10660 /* Handle simple keywords. */
10661 if (ds != ds_last)
10662 {
10663 if (decl_specs)
10664 {
10665 ++decl_specs->specs[(int)ds];
10666 decl_specs->any_specifiers_p = true;
10667 }
10668 return cp_lexer_consume_token (parser->lexer)->u.value;
10669 }
10670
10671 /* If we do not already have a type-specifier, assume we are looking
10672 at a simple-type-specifier. */
10673 type_spec = cp_parser_simple_type_specifier (parser,
10674 decl_specs,
10675 flags);
10676
10677 /* If we didn't find a type-specifier, and a type-specifier was not
10678 optional in this context, issue an error message. */
10679 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10680 {
10681 cp_parser_error (parser, "expected type specifier");
10682 return error_mark_node;
10683 }
10684
10685 return type_spec;
10686 }
10687
10688 /* Parse a simple-type-specifier.
10689
10690 simple-type-specifier:
10691 :: [opt] nested-name-specifier [opt] type-name
10692 :: [opt] nested-name-specifier template template-id
10693 char
10694 wchar_t
10695 bool
10696 short
10697 int
10698 long
10699 signed
10700 unsigned
10701 float
10702 double
10703 void
10704
10705 C++0x Extension:
10706
10707 simple-type-specifier:
10708 decltype ( expression )
10709
10710 GNU Extension:
10711
10712 simple-type-specifier:
10713 __typeof__ unary-expression
10714 __typeof__ ( type-id )
10715
10716 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10717 appropriately updated. */
10718
10719 static tree
10720 cp_parser_simple_type_specifier (cp_parser* parser,
10721 cp_decl_specifier_seq *decl_specs,
10722 cp_parser_flags flags)
10723 {
10724 tree type = NULL_TREE;
10725 cp_token *token;
10726
10727 /* Peek at the next token. */
10728 token = cp_lexer_peek_token (parser->lexer);
10729
10730 /* If we're looking at a keyword, things are easy. */
10731 switch (token->keyword)
10732 {
10733 case RID_CHAR:
10734 if (decl_specs)
10735 decl_specs->explicit_char_p = true;
10736 type = char_type_node;
10737 break;
10738 case RID_WCHAR:
10739 type = wchar_type_node;
10740 break;
10741 case RID_BOOL:
10742 type = boolean_type_node;
10743 break;
10744 case RID_SHORT:
10745 if (decl_specs)
10746 ++decl_specs->specs[(int) ds_short];
10747 type = short_integer_type_node;
10748 break;
10749 case RID_INT:
10750 if (decl_specs)
10751 decl_specs->explicit_int_p = true;
10752 type = integer_type_node;
10753 break;
10754 case RID_LONG:
10755 if (decl_specs)
10756 ++decl_specs->specs[(int) ds_long];
10757 type = long_integer_type_node;
10758 break;
10759 case RID_SIGNED:
10760 if (decl_specs)
10761 ++decl_specs->specs[(int) ds_signed];
10762 type = integer_type_node;
10763 break;
10764 case RID_UNSIGNED:
10765 if (decl_specs)
10766 ++decl_specs->specs[(int) ds_unsigned];
10767 type = unsigned_type_node;
10768 break;
10769 case RID_FLOAT:
10770 type = float_type_node;
10771 break;
10772 case RID_DOUBLE:
10773 type = double_type_node;
10774 break;
10775 case RID_VOID:
10776 type = void_type_node;
10777 break;
10778
10779 case RID_DECLTYPE:
10780 /* Parse the `decltype' type. */
10781 type = cp_parser_decltype (parser);
10782
10783 if (decl_specs)
10784 cp_parser_set_decl_spec_type (decl_specs, type,
10785 /*user_defined_p=*/true);
10786
10787 return type;
10788
10789 case RID_TYPEOF:
10790 /* Consume the `typeof' token. */
10791 cp_lexer_consume_token (parser->lexer);
10792 /* Parse the operand to `typeof'. */
10793 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10794 /* If it is not already a TYPE, take its type. */
10795 if (!TYPE_P (type))
10796 type = finish_typeof (type);
10797
10798 if (decl_specs)
10799 cp_parser_set_decl_spec_type (decl_specs, type,
10800 /*user_defined_p=*/true);
10801
10802 return type;
10803
10804 default:
10805 break;
10806 }
10807
10808 /* If the type-specifier was for a built-in type, we're done. */
10809 if (type)
10810 {
10811 tree id;
10812
10813 /* Record the type. */
10814 if (decl_specs
10815 && (token->keyword != RID_SIGNED
10816 && token->keyword != RID_UNSIGNED
10817 && token->keyword != RID_SHORT
10818 && token->keyword != RID_LONG))
10819 cp_parser_set_decl_spec_type (decl_specs,
10820 type,
10821 /*user_defined=*/false);
10822 if (decl_specs)
10823 decl_specs->any_specifiers_p = true;
10824
10825 /* Consume the token. */
10826 id = cp_lexer_consume_token (parser->lexer)->u.value;
10827
10828 /* There is no valid C++ program where a non-template type is
10829 followed by a "<". That usually indicates that the user thought
10830 that the type was a template. */
10831 cp_parser_check_for_invalid_template_id (parser, type);
10832
10833 return TYPE_NAME (type);
10834 }
10835
10836 /* The type-specifier must be a user-defined type. */
10837 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10838 {
10839 bool qualified_p;
10840 bool global_p;
10841
10842 /* Don't gobble tokens or issue error messages if this is an
10843 optional type-specifier. */
10844 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10845 cp_parser_parse_tentatively (parser);
10846
10847 /* Look for the optional `::' operator. */
10848 global_p
10849 = (cp_parser_global_scope_opt (parser,
10850 /*current_scope_valid_p=*/false)
10851 != NULL_TREE);
10852 /* Look for the nested-name specifier. */
10853 qualified_p
10854 = (cp_parser_nested_name_specifier_opt (parser,
10855 /*typename_keyword_p=*/false,
10856 /*check_dependency_p=*/true,
10857 /*type_p=*/false,
10858 /*is_declaration=*/false)
10859 != NULL_TREE);
10860 /* If we have seen a nested-name-specifier, and the next token
10861 is `template', then we are using the template-id production. */
10862 if (parser->scope
10863 && cp_parser_optional_template_keyword (parser))
10864 {
10865 /* Look for the template-id. */
10866 type = cp_parser_template_id (parser,
10867 /*template_keyword_p=*/true,
10868 /*check_dependency_p=*/true,
10869 /*is_declaration=*/false);
10870 /* If the template-id did not name a type, we are out of
10871 luck. */
10872 if (TREE_CODE (type) != TYPE_DECL)
10873 {
10874 cp_parser_error (parser, "expected template-id for type");
10875 type = NULL_TREE;
10876 }
10877 }
10878 /* Otherwise, look for a type-name. */
10879 else
10880 type = cp_parser_type_name (parser);
10881 /* Keep track of all name-lookups performed in class scopes. */
10882 if (type
10883 && !global_p
10884 && !qualified_p
10885 && TREE_CODE (type) == TYPE_DECL
10886 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10887 maybe_note_name_used_in_class (DECL_NAME (type), type);
10888 /* If it didn't work out, we don't have a TYPE. */
10889 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10890 && !cp_parser_parse_definitely (parser))
10891 type = NULL_TREE;
10892 if (type && decl_specs)
10893 cp_parser_set_decl_spec_type (decl_specs, type,
10894 /*user_defined=*/true);
10895 }
10896
10897 /* If we didn't get a type-name, issue an error message. */
10898 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10899 {
10900 cp_parser_error (parser, "expected type-name");
10901 return error_mark_node;
10902 }
10903
10904 /* There is no valid C++ program where a non-template type is
10905 followed by a "<". That usually indicates that the user thought
10906 that the type was a template. */
10907 if (type && type != error_mark_node)
10908 {
10909 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10910 If it is, then the '<'...'>' enclose protocol names rather than
10911 template arguments, and so everything is fine. */
10912 if (c_dialect_objc ()
10913 && (objc_is_id (type) || objc_is_class_name (type)))
10914 {
10915 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10916 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10917
10918 /* Clobber the "unqualified" type previously entered into
10919 DECL_SPECS with the new, improved protocol-qualified version. */
10920 if (decl_specs)
10921 decl_specs->type = qual_type;
10922
10923 return qual_type;
10924 }
10925
10926 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10927 }
10928
10929 return type;
10930 }
10931
10932 /* Parse a type-name.
10933
10934 type-name:
10935 class-name
10936 enum-name
10937 typedef-name
10938
10939 enum-name:
10940 identifier
10941
10942 typedef-name:
10943 identifier
10944
10945 Returns a TYPE_DECL for the type. */
10946
10947 static tree
10948 cp_parser_type_name (cp_parser* parser)
10949 {
10950 tree type_decl;
10951
10952 /* We can't know yet whether it is a class-name or not. */
10953 cp_parser_parse_tentatively (parser);
10954 /* Try a class-name. */
10955 type_decl = cp_parser_class_name (parser,
10956 /*typename_keyword_p=*/false,
10957 /*template_keyword_p=*/false,
10958 none_type,
10959 /*check_dependency_p=*/true,
10960 /*class_head_p=*/false,
10961 /*is_declaration=*/false);
10962 /* If it's not a class-name, keep looking. */
10963 if (!cp_parser_parse_definitely (parser))
10964 {
10965 /* It must be a typedef-name or an enum-name. */
10966 return cp_parser_nonclass_name (parser);
10967 }
10968
10969 return type_decl;
10970 }
10971
10972 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
10973
10974 enum-name:
10975 identifier
10976
10977 typedef-name:
10978 identifier
10979
10980 Returns a TYPE_DECL for the type. */
10981
10982 static tree
10983 cp_parser_nonclass_name (cp_parser* parser)
10984 {
10985 tree type_decl;
10986 tree identifier;
10987
10988 identifier = cp_parser_identifier (parser);
10989 if (identifier == error_mark_node)
10990 return error_mark_node;
10991
10992 /* Look up the type-name. */
10993 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10994
10995 if (TREE_CODE (type_decl) != TYPE_DECL
10996 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10997 {
10998 /* See if this is an Objective-C type. */
10999 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11000 tree type = objc_get_protocol_qualified_type (identifier, protos);
11001 if (type)
11002 type_decl = TYPE_NAME (type);
11003 }
11004
11005 /* Issue an error if we did not find a type-name. */
11006 if (TREE_CODE (type_decl) != TYPE_DECL)
11007 {
11008 if (!cp_parser_simulate_error (parser))
11009 cp_parser_name_lookup_error (parser, identifier, type_decl,
11010 "is not a type");
11011 return error_mark_node;
11012 }
11013 /* Remember that the name was used in the definition of the
11014 current class so that we can check later to see if the
11015 meaning would have been different after the class was
11016 entirely defined. */
11017 else if (type_decl != error_mark_node
11018 && !parser->scope)
11019 maybe_note_name_used_in_class (identifier, type_decl);
11020
11021 return type_decl;
11022 }
11023
11024 /* Parse an elaborated-type-specifier. Note that the grammar given
11025 here incorporates the resolution to DR68.
11026
11027 elaborated-type-specifier:
11028 class-key :: [opt] nested-name-specifier [opt] identifier
11029 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11030 enum :: [opt] nested-name-specifier [opt] identifier
11031 typename :: [opt] nested-name-specifier identifier
11032 typename :: [opt] nested-name-specifier template [opt]
11033 template-id
11034
11035 GNU extension:
11036
11037 elaborated-type-specifier:
11038 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11039 class-key attributes :: [opt] nested-name-specifier [opt]
11040 template [opt] template-id
11041 enum attributes :: [opt] nested-name-specifier [opt] identifier
11042
11043 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11044 declared `friend'. If IS_DECLARATION is TRUE, then this
11045 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11046 something is being declared.
11047
11048 Returns the TYPE specified. */
11049
11050 static tree
11051 cp_parser_elaborated_type_specifier (cp_parser* parser,
11052 bool is_friend,
11053 bool is_declaration)
11054 {
11055 enum tag_types tag_type;
11056 tree identifier;
11057 tree type = NULL_TREE;
11058 tree attributes = NULL_TREE;
11059
11060 /* See if we're looking at the `enum' keyword. */
11061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11062 {
11063 /* Consume the `enum' token. */
11064 cp_lexer_consume_token (parser->lexer);
11065 /* Remember that it's an enumeration type. */
11066 tag_type = enum_type;
11067 /* Parse the attributes. */
11068 attributes = cp_parser_attributes_opt (parser);
11069 }
11070 /* Or, it might be `typename'. */
11071 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11072 RID_TYPENAME))
11073 {
11074 /* Consume the `typename' token. */
11075 cp_lexer_consume_token (parser->lexer);
11076 /* Remember that it's a `typename' type. */
11077 tag_type = typename_type;
11078 /* The `typename' keyword is only allowed in templates. */
11079 if (!processing_template_decl)
11080 pedwarn ("using %<typename%> outside of template");
11081 }
11082 /* Otherwise it must be a class-key. */
11083 else
11084 {
11085 tag_type = cp_parser_class_key (parser);
11086 if (tag_type == none_type)
11087 return error_mark_node;
11088 /* Parse the attributes. */
11089 attributes = cp_parser_attributes_opt (parser);
11090 }
11091
11092 /* Look for the `::' operator. */
11093 cp_parser_global_scope_opt (parser,
11094 /*current_scope_valid_p=*/false);
11095 /* Look for the nested-name-specifier. */
11096 if (tag_type == typename_type)
11097 {
11098 if (!cp_parser_nested_name_specifier (parser,
11099 /*typename_keyword_p=*/true,
11100 /*check_dependency_p=*/true,
11101 /*type_p=*/true,
11102 is_declaration))
11103 return error_mark_node;
11104 }
11105 else
11106 /* Even though `typename' is not present, the proposed resolution
11107 to Core Issue 180 says that in `class A<T>::B', `B' should be
11108 considered a type-name, even if `A<T>' is dependent. */
11109 cp_parser_nested_name_specifier_opt (parser,
11110 /*typename_keyword_p=*/true,
11111 /*check_dependency_p=*/true,
11112 /*type_p=*/true,
11113 is_declaration);
11114 /* For everything but enumeration types, consider a template-id.
11115 For an enumeration type, consider only a plain identifier. */
11116 if (tag_type != enum_type)
11117 {
11118 bool template_p = false;
11119 tree decl;
11120
11121 /* Allow the `template' keyword. */
11122 template_p = cp_parser_optional_template_keyword (parser);
11123 /* If we didn't see `template', we don't know if there's a
11124 template-id or not. */
11125 if (!template_p)
11126 cp_parser_parse_tentatively (parser);
11127 /* Parse the template-id. */
11128 decl = cp_parser_template_id (parser, template_p,
11129 /*check_dependency_p=*/true,
11130 is_declaration);
11131 /* If we didn't find a template-id, look for an ordinary
11132 identifier. */
11133 if (!template_p && !cp_parser_parse_definitely (parser))
11134 ;
11135 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11136 in effect, then we must assume that, upon instantiation, the
11137 template will correspond to a class. */
11138 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11139 && tag_type == typename_type)
11140 type = make_typename_type (parser->scope, decl,
11141 typename_type,
11142 /*complain=*/tf_error);
11143 else
11144 type = TREE_TYPE (decl);
11145 }
11146
11147 if (!type)
11148 {
11149 identifier = cp_parser_identifier (parser);
11150
11151 if (identifier == error_mark_node)
11152 {
11153 parser->scope = NULL_TREE;
11154 return error_mark_node;
11155 }
11156
11157 /* For a `typename', we needn't call xref_tag. */
11158 if (tag_type == typename_type
11159 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11160 return cp_parser_make_typename_type (parser, parser->scope,
11161 identifier);
11162 /* Look up a qualified name in the usual way. */
11163 if (parser->scope)
11164 {
11165 tree decl;
11166 tree ambiguous_decls;
11167
11168 decl = cp_parser_lookup_name (parser, identifier,
11169 tag_type,
11170 /*is_template=*/false,
11171 /*is_namespace=*/false,
11172 /*check_dependency=*/true,
11173 &ambiguous_decls);
11174
11175 /* If the lookup was ambiguous, an error will already have been
11176 issued. */
11177 if (ambiguous_decls)
11178 return error_mark_node;
11179
11180 /* If we are parsing friend declaration, DECL may be a
11181 TEMPLATE_DECL tree node here. However, we need to check
11182 whether this TEMPLATE_DECL results in valid code. Consider
11183 the following example:
11184
11185 namespace N {
11186 template <class T> class C {};
11187 }
11188 class X {
11189 template <class T> friend class N::C; // #1, valid code
11190 };
11191 template <class T> class Y {
11192 friend class N::C; // #2, invalid code
11193 };
11194
11195 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11196 name lookup of `N::C'. We see that friend declaration must
11197 be template for the code to be valid. Note that
11198 processing_template_decl does not work here since it is
11199 always 1 for the above two cases. */
11200
11201 decl = (cp_parser_maybe_treat_template_as_class
11202 (decl, /*tag_name_p=*/is_friend
11203 && parser->num_template_parameter_lists));
11204
11205 if (TREE_CODE (decl) != TYPE_DECL)
11206 {
11207 cp_parser_diagnose_invalid_type_name (parser,
11208 parser->scope,
11209 identifier);
11210 return error_mark_node;
11211 }
11212
11213 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11214 {
11215 bool allow_template = (parser->num_template_parameter_lists
11216 || DECL_SELF_REFERENCE_P (decl));
11217 type = check_elaborated_type_specifier (tag_type, decl,
11218 allow_template);
11219
11220 if (type == error_mark_node)
11221 return error_mark_node;
11222 }
11223
11224 /* Forward declarations of nested types, such as
11225
11226 class C1::C2;
11227 class C1::C2::C3;
11228
11229 are invalid unless all components preceding the final '::'
11230 are complete. If all enclosing types are complete, these
11231 declarations become merely pointless.
11232
11233 Invalid forward declarations of nested types are errors
11234 caught elsewhere in parsing. Those that are pointless arrive
11235 here. */
11236
11237 if (cp_parser_declares_only_class_p (parser)
11238 && !is_friend && !processing_explicit_instantiation)
11239 warning (0, "declaration %qD does not declare anything", decl);
11240
11241 type = TREE_TYPE (decl);
11242 }
11243 else
11244 {
11245 /* An elaborated-type-specifier sometimes introduces a new type and
11246 sometimes names an existing type. Normally, the rule is that it
11247 introduces a new type only if there is not an existing type of
11248 the same name already in scope. For example, given:
11249
11250 struct S {};
11251 void f() { struct S s; }
11252
11253 the `struct S' in the body of `f' is the same `struct S' as in
11254 the global scope; the existing definition is used. However, if
11255 there were no global declaration, this would introduce a new
11256 local class named `S'.
11257
11258 An exception to this rule applies to the following code:
11259
11260 namespace N { struct S; }
11261
11262 Here, the elaborated-type-specifier names a new type
11263 unconditionally; even if there is already an `S' in the
11264 containing scope this declaration names a new type.
11265 This exception only applies if the elaborated-type-specifier
11266 forms the complete declaration:
11267
11268 [class.name]
11269
11270 A declaration consisting solely of `class-key identifier ;' is
11271 either a redeclaration of the name in the current scope or a
11272 forward declaration of the identifier as a class name. It
11273 introduces the name into the current scope.
11274
11275 We are in this situation precisely when the next token is a `;'.
11276
11277 An exception to the exception is that a `friend' declaration does
11278 *not* name a new type; i.e., given:
11279
11280 struct S { friend struct T; };
11281
11282 `T' is not a new type in the scope of `S'.
11283
11284 Also, `new struct S' or `sizeof (struct S)' never results in the
11285 definition of a new type; a new type can only be declared in a
11286 declaration context. */
11287
11288 tag_scope ts;
11289 bool template_p;
11290
11291 if (is_friend)
11292 /* Friends have special name lookup rules. */
11293 ts = ts_within_enclosing_non_class;
11294 else if (is_declaration
11295 && cp_lexer_next_token_is (parser->lexer,
11296 CPP_SEMICOLON))
11297 /* This is a `class-key identifier ;' */
11298 ts = ts_current;
11299 else
11300 ts = ts_global;
11301
11302 template_p =
11303 (parser->num_template_parameter_lists
11304 && (cp_parser_next_token_starts_class_definition_p (parser)
11305 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11306 /* An unqualified name was used to reference this type, so
11307 there were no qualifying templates. */
11308 if (!cp_parser_check_template_parameters (parser,
11309 /*num_templates=*/0))
11310 return error_mark_node;
11311 type = xref_tag (tag_type, identifier, ts, template_p);
11312 }
11313 }
11314
11315 if (type == error_mark_node)
11316 return error_mark_node;
11317
11318 /* Allow attributes on forward declarations of classes. */
11319 if (attributes)
11320 {
11321 if (TREE_CODE (type) == TYPENAME_TYPE)
11322 warning (OPT_Wattributes,
11323 "attributes ignored on uninstantiated type");
11324 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11325 && ! processing_explicit_instantiation)
11326 warning (OPT_Wattributes,
11327 "attributes ignored on template instantiation");
11328 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11329 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11330 else
11331 warning (OPT_Wattributes,
11332 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11333 }
11334
11335 if (tag_type != enum_type)
11336 cp_parser_check_class_key (tag_type, type);
11337
11338 /* A "<" cannot follow an elaborated type specifier. If that
11339 happens, the user was probably trying to form a template-id. */
11340 cp_parser_check_for_invalid_template_id (parser, type);
11341
11342 return type;
11343 }
11344
11345 /* Parse an enum-specifier.
11346
11347 enum-specifier:
11348 enum identifier [opt] { enumerator-list [opt] }
11349
11350 GNU Extensions:
11351 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11352 attributes[opt]
11353
11354 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11355 if the token stream isn't an enum-specifier after all. */
11356
11357 static tree
11358 cp_parser_enum_specifier (cp_parser* parser)
11359 {
11360 tree identifier;
11361 tree type;
11362 tree attributes;
11363
11364 /* Parse tentatively so that we can back up if we don't find a
11365 enum-specifier. */
11366 cp_parser_parse_tentatively (parser);
11367
11368 /* Caller guarantees that the current token is 'enum', an identifier
11369 possibly follows, and the token after that is an opening brace.
11370 If we don't have an identifier, fabricate an anonymous name for
11371 the enumeration being defined. */
11372 cp_lexer_consume_token (parser->lexer);
11373
11374 attributes = cp_parser_attributes_opt (parser);
11375
11376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11377 identifier = cp_parser_identifier (parser);
11378 else
11379 identifier = make_anon_name ();
11380
11381 /* Look for the `{' but don't consume it yet. */
11382 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11383 cp_parser_simulate_error (parser);
11384
11385 if (!cp_parser_parse_definitely (parser))
11386 return NULL_TREE;
11387
11388 /* Issue an error message if type-definitions are forbidden here. */
11389 if (!cp_parser_check_type_definition (parser))
11390 type = error_mark_node;
11391 else
11392 /* Create the new type. We do this before consuming the opening
11393 brace so the enum will be recorded as being on the line of its
11394 tag (or the 'enum' keyword, if there is no tag). */
11395 type = start_enum (identifier);
11396
11397 /* Consume the opening brace. */
11398 cp_lexer_consume_token (parser->lexer);
11399
11400 if (type == error_mark_node)
11401 {
11402 cp_parser_skip_to_end_of_block_or_statement (parser);
11403 return error_mark_node;
11404 }
11405
11406 /* If the next token is not '}', then there are some enumerators. */
11407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11408 cp_parser_enumerator_list (parser, type);
11409
11410 /* Consume the final '}'. */
11411 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11412
11413 /* Look for trailing attributes to apply to this enumeration, and
11414 apply them if appropriate. */
11415 if (cp_parser_allow_gnu_extensions_p (parser))
11416 {
11417 tree trailing_attr = cp_parser_attributes_opt (parser);
11418 cplus_decl_attributes (&type,
11419 trailing_attr,
11420 (int) ATTR_FLAG_TYPE_IN_PLACE);
11421 }
11422
11423 /* Finish up the enumeration. */
11424 finish_enum (type);
11425
11426 return type;
11427 }
11428
11429 /* Parse an enumerator-list. The enumerators all have the indicated
11430 TYPE.
11431
11432 enumerator-list:
11433 enumerator-definition
11434 enumerator-list , enumerator-definition */
11435
11436 static void
11437 cp_parser_enumerator_list (cp_parser* parser, tree type)
11438 {
11439 while (true)
11440 {
11441 /* Parse an enumerator-definition. */
11442 cp_parser_enumerator_definition (parser, type);
11443
11444 /* If the next token is not a ',', we've reached the end of
11445 the list. */
11446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11447 break;
11448 /* Otherwise, consume the `,' and keep going. */
11449 cp_lexer_consume_token (parser->lexer);
11450 /* If the next token is a `}', there is a trailing comma. */
11451 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11452 {
11453 if (pedantic && !in_system_header)
11454 pedwarn ("comma at end of enumerator list");
11455 break;
11456 }
11457 }
11458 }
11459
11460 /* Parse an enumerator-definition. The enumerator has the indicated
11461 TYPE.
11462
11463 enumerator-definition:
11464 enumerator
11465 enumerator = constant-expression
11466
11467 enumerator:
11468 identifier */
11469
11470 static void
11471 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11472 {
11473 tree identifier;
11474 tree value;
11475
11476 /* Look for the identifier. */
11477 identifier = cp_parser_identifier (parser);
11478 if (identifier == error_mark_node)
11479 return;
11480
11481 /* If the next token is an '=', then there is an explicit value. */
11482 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11483 {
11484 /* Consume the `=' token. */
11485 cp_lexer_consume_token (parser->lexer);
11486 /* Parse the value. */
11487 value = cp_parser_constant_expression (parser,
11488 /*allow_non_constant_p=*/false,
11489 NULL);
11490 }
11491 else
11492 value = NULL_TREE;
11493
11494 /* Create the enumerator. */
11495 build_enumerator (identifier, value, type);
11496 }
11497
11498 /* Parse a namespace-name.
11499
11500 namespace-name:
11501 original-namespace-name
11502 namespace-alias
11503
11504 Returns the NAMESPACE_DECL for the namespace. */
11505
11506 static tree
11507 cp_parser_namespace_name (cp_parser* parser)
11508 {
11509 tree identifier;
11510 tree namespace_decl;
11511
11512 /* Get the name of the namespace. */
11513 identifier = cp_parser_identifier (parser);
11514 if (identifier == error_mark_node)
11515 return error_mark_node;
11516
11517 /* Look up the identifier in the currently active scope. Look only
11518 for namespaces, due to:
11519
11520 [basic.lookup.udir]
11521
11522 When looking up a namespace-name in a using-directive or alias
11523 definition, only namespace names are considered.
11524
11525 And:
11526
11527 [basic.lookup.qual]
11528
11529 During the lookup of a name preceding the :: scope resolution
11530 operator, object, function, and enumerator names are ignored.
11531
11532 (Note that cp_parser_class_or_namespace_name only calls this
11533 function if the token after the name is the scope resolution
11534 operator.) */
11535 namespace_decl = cp_parser_lookup_name (parser, identifier,
11536 none_type,
11537 /*is_template=*/false,
11538 /*is_namespace=*/true,
11539 /*check_dependency=*/true,
11540 /*ambiguous_decls=*/NULL);
11541 /* If it's not a namespace, issue an error. */
11542 if (namespace_decl == error_mark_node
11543 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11544 {
11545 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11546 error ("%qD is not a namespace-name", identifier);
11547 cp_parser_error (parser, "expected namespace-name");
11548 namespace_decl = error_mark_node;
11549 }
11550
11551 return namespace_decl;
11552 }
11553
11554 /* Parse a namespace-definition.
11555
11556 namespace-definition:
11557 named-namespace-definition
11558 unnamed-namespace-definition
11559
11560 named-namespace-definition:
11561 original-namespace-definition
11562 extension-namespace-definition
11563
11564 original-namespace-definition:
11565 namespace identifier { namespace-body }
11566
11567 extension-namespace-definition:
11568 namespace original-namespace-name { namespace-body }
11569
11570 unnamed-namespace-definition:
11571 namespace { namespace-body } */
11572
11573 static void
11574 cp_parser_namespace_definition (cp_parser* parser)
11575 {
11576 tree identifier, attribs;
11577 bool has_visibility;
11578 bool is_inline;
11579
11580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11581 {
11582 is_inline = true;
11583 cp_lexer_consume_token (parser->lexer);
11584 }
11585 else
11586 is_inline = false;
11587
11588 /* Look for the `namespace' keyword. */
11589 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11590
11591 /* Get the name of the namespace. We do not attempt to distinguish
11592 between an original-namespace-definition and an
11593 extension-namespace-definition at this point. The semantic
11594 analysis routines are responsible for that. */
11595 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11596 identifier = cp_parser_identifier (parser);
11597 else
11598 identifier = NULL_TREE;
11599
11600 /* Parse any specified attributes. */
11601 attribs = cp_parser_attributes_opt (parser);
11602
11603 /* Look for the `{' to start the namespace. */
11604 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11605 /* Start the namespace. */
11606 push_namespace (identifier);
11607
11608 /* "inline namespace" is equivalent to a stub namespace definition
11609 followed by a strong using directive. */
11610 if (is_inline)
11611 {
11612 tree namespace = current_namespace;
11613 /* Set up namespace association. */
11614 DECL_NAMESPACE_ASSOCIATIONS (namespace)
11615 = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11616 DECL_NAMESPACE_ASSOCIATIONS (namespace));
11617 /* Import the contents of the inline namespace. */
11618 pop_namespace ();
11619 do_using_directive (namespace);
11620 push_namespace (identifier);
11621 }
11622
11623 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11624
11625 /* Parse the body of the namespace. */
11626 cp_parser_namespace_body (parser);
11627
11628 #ifdef HANDLE_PRAGMA_VISIBILITY
11629 if (has_visibility)
11630 pop_visibility ();
11631 #endif
11632
11633 /* Finish the namespace. */
11634 pop_namespace ();
11635 /* Look for the final `}'. */
11636 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11637 }
11638
11639 /* Parse a namespace-body.
11640
11641 namespace-body:
11642 declaration-seq [opt] */
11643
11644 static void
11645 cp_parser_namespace_body (cp_parser* parser)
11646 {
11647 cp_parser_declaration_seq_opt (parser);
11648 }
11649
11650 /* Parse a namespace-alias-definition.
11651
11652 namespace-alias-definition:
11653 namespace identifier = qualified-namespace-specifier ; */
11654
11655 static void
11656 cp_parser_namespace_alias_definition (cp_parser* parser)
11657 {
11658 tree identifier;
11659 tree namespace_specifier;
11660
11661 /* Look for the `namespace' keyword. */
11662 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11663 /* Look for the identifier. */
11664 identifier = cp_parser_identifier (parser);
11665 if (identifier == error_mark_node)
11666 return;
11667 /* Look for the `=' token. */
11668 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11669 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11670 {
11671 error ("%<namespace%> definition is not allowed here");
11672 /* Skip the definition. */
11673 cp_lexer_consume_token (parser->lexer);
11674 if (cp_parser_skip_to_closing_brace (parser))
11675 cp_lexer_consume_token (parser->lexer);
11676 return;
11677 }
11678 cp_parser_require (parser, CPP_EQ, "`='");
11679 /* Look for the qualified-namespace-specifier. */
11680 namespace_specifier
11681 = cp_parser_qualified_namespace_specifier (parser);
11682 /* Look for the `;' token. */
11683 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11684
11685 /* Register the alias in the symbol table. */
11686 do_namespace_alias (identifier, namespace_specifier);
11687 }
11688
11689 /* Parse a qualified-namespace-specifier.
11690
11691 qualified-namespace-specifier:
11692 :: [opt] nested-name-specifier [opt] namespace-name
11693
11694 Returns a NAMESPACE_DECL corresponding to the specified
11695 namespace. */
11696
11697 static tree
11698 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11699 {
11700 /* Look for the optional `::'. */
11701 cp_parser_global_scope_opt (parser,
11702 /*current_scope_valid_p=*/false);
11703
11704 /* Look for the optional nested-name-specifier. */
11705 cp_parser_nested_name_specifier_opt (parser,
11706 /*typename_keyword_p=*/false,
11707 /*check_dependency_p=*/true,
11708 /*type_p=*/false,
11709 /*is_declaration=*/true);
11710
11711 return cp_parser_namespace_name (parser);
11712 }
11713
11714 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11715 access declaration.
11716
11717 using-declaration:
11718 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11719 using :: unqualified-id ;
11720
11721 access-declaration:
11722 qualified-id ;
11723
11724 */
11725
11726 static bool
11727 cp_parser_using_declaration (cp_parser* parser,
11728 bool access_declaration_p)
11729 {
11730 cp_token *token;
11731 bool typename_p = false;
11732 bool global_scope_p;
11733 tree decl;
11734 tree identifier;
11735 tree qscope;
11736
11737 if (access_declaration_p)
11738 cp_parser_parse_tentatively (parser);
11739 else
11740 {
11741 /* Look for the `using' keyword. */
11742 cp_parser_require_keyword (parser, RID_USING, "`using'");
11743
11744 /* Peek at the next token. */
11745 token = cp_lexer_peek_token (parser->lexer);
11746 /* See if it's `typename'. */
11747 if (token->keyword == RID_TYPENAME)
11748 {
11749 /* Remember that we've seen it. */
11750 typename_p = true;
11751 /* Consume the `typename' token. */
11752 cp_lexer_consume_token (parser->lexer);
11753 }
11754 }
11755
11756 /* Look for the optional global scope qualification. */
11757 global_scope_p
11758 = (cp_parser_global_scope_opt (parser,
11759 /*current_scope_valid_p=*/false)
11760 != NULL_TREE);
11761
11762 /* If we saw `typename', or didn't see `::', then there must be a
11763 nested-name-specifier present. */
11764 if (typename_p || !global_scope_p)
11765 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11766 /*check_dependency_p=*/true,
11767 /*type_p=*/false,
11768 /*is_declaration=*/true);
11769 /* Otherwise, we could be in either of the two productions. In that
11770 case, treat the nested-name-specifier as optional. */
11771 else
11772 qscope = cp_parser_nested_name_specifier_opt (parser,
11773 /*typename_keyword_p=*/false,
11774 /*check_dependency_p=*/true,
11775 /*type_p=*/false,
11776 /*is_declaration=*/true);
11777 if (!qscope)
11778 qscope = global_namespace;
11779
11780 if (access_declaration_p && cp_parser_error_occurred (parser))
11781 /* Something has already gone wrong; there's no need to parse
11782 further. Since an error has occurred, the return value of
11783 cp_parser_parse_definitely will be false, as required. */
11784 return cp_parser_parse_definitely (parser);
11785
11786 /* Parse the unqualified-id. */
11787 identifier = cp_parser_unqualified_id (parser,
11788 /*template_keyword_p=*/false,
11789 /*check_dependency_p=*/true,
11790 /*declarator_p=*/true,
11791 /*optional_p=*/false);
11792
11793 if (access_declaration_p)
11794 {
11795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11796 cp_parser_simulate_error (parser);
11797 if (!cp_parser_parse_definitely (parser))
11798 return false;
11799 }
11800
11801 /* The function we call to handle a using-declaration is different
11802 depending on what scope we are in. */
11803 if (qscope == error_mark_node || identifier == error_mark_node)
11804 ;
11805 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11806 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11807 /* [namespace.udecl]
11808
11809 A using declaration shall not name a template-id. */
11810 error ("a template-id may not appear in a using-declaration");
11811 else
11812 {
11813 if (at_class_scope_p ())
11814 {
11815 /* Create the USING_DECL. */
11816 decl = do_class_using_decl (parser->scope, identifier);
11817
11818 if (check_for_bare_parameter_packs (decl))
11819 return false;
11820 else
11821 /* Add it to the list of members in this class. */
11822 finish_member_declaration (decl);
11823 }
11824 else
11825 {
11826 decl = cp_parser_lookup_name_simple (parser, identifier);
11827 if (decl == error_mark_node)
11828 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11829 else if (check_for_bare_parameter_packs (decl))
11830 return false;
11831 else if (!at_namespace_scope_p ())
11832 do_local_using_decl (decl, qscope, identifier);
11833 else
11834 do_toplevel_using_decl (decl, qscope, identifier);
11835 }
11836 }
11837
11838 /* Look for the final `;'. */
11839 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11840
11841 return true;
11842 }
11843
11844 /* Parse a using-directive.
11845
11846 using-directive:
11847 using namespace :: [opt] nested-name-specifier [opt]
11848 namespace-name ; */
11849
11850 static void
11851 cp_parser_using_directive (cp_parser* parser)
11852 {
11853 tree namespace_decl;
11854 tree attribs;
11855
11856 /* Look for the `using' keyword. */
11857 cp_parser_require_keyword (parser, RID_USING, "`using'");
11858 /* And the `namespace' keyword. */
11859 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11860 /* Look for the optional `::' operator. */
11861 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11862 /* And the optional nested-name-specifier. */
11863 cp_parser_nested_name_specifier_opt (parser,
11864 /*typename_keyword_p=*/false,
11865 /*check_dependency_p=*/true,
11866 /*type_p=*/false,
11867 /*is_declaration=*/true);
11868 /* Get the namespace being used. */
11869 namespace_decl = cp_parser_namespace_name (parser);
11870 /* And any specified attributes. */
11871 attribs = cp_parser_attributes_opt (parser);
11872 /* Update the symbol table. */
11873 parse_using_directive (namespace_decl, attribs);
11874 /* Look for the final `;'. */
11875 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11876 }
11877
11878 /* Parse an asm-definition.
11879
11880 asm-definition:
11881 asm ( string-literal ) ;
11882
11883 GNU Extension:
11884
11885 asm-definition:
11886 asm volatile [opt] ( string-literal ) ;
11887 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11888 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11889 : asm-operand-list [opt] ) ;
11890 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11891 : asm-operand-list [opt]
11892 : asm-operand-list [opt] ) ; */
11893
11894 static void
11895 cp_parser_asm_definition (cp_parser* parser)
11896 {
11897 tree string;
11898 tree outputs = NULL_TREE;
11899 tree inputs = NULL_TREE;
11900 tree clobbers = NULL_TREE;
11901 tree asm_stmt;
11902 bool volatile_p = false;
11903 bool extended_p = false;
11904 bool invalid_inputs_p = false;
11905 bool invalid_outputs_p = false;
11906
11907 /* Look for the `asm' keyword. */
11908 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11909 /* See if the next token is `volatile'. */
11910 if (cp_parser_allow_gnu_extensions_p (parser)
11911 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11912 {
11913 /* Remember that we saw the `volatile' keyword. */
11914 volatile_p = true;
11915 /* Consume the token. */
11916 cp_lexer_consume_token (parser->lexer);
11917 }
11918 /* Look for the opening `('. */
11919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11920 return;
11921 /* Look for the string. */
11922 string = cp_parser_string_literal (parser, false, false);
11923 if (string == error_mark_node)
11924 {
11925 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11926 /*consume_paren=*/true);
11927 return;
11928 }
11929
11930 /* If we're allowing GNU extensions, check for the extended assembly
11931 syntax. Unfortunately, the `:' tokens need not be separated by
11932 a space in C, and so, for compatibility, we tolerate that here
11933 too. Doing that means that we have to treat the `::' operator as
11934 two `:' tokens. */
11935 if (cp_parser_allow_gnu_extensions_p (parser)
11936 && parser->in_function_body
11937 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11938 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11939 {
11940 bool inputs_p = false;
11941 bool clobbers_p = false;
11942
11943 /* The extended syntax was used. */
11944 extended_p = true;
11945
11946 /* Look for outputs. */
11947 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11948 {
11949 /* Consume the `:'. */
11950 cp_lexer_consume_token (parser->lexer);
11951 /* Parse the output-operands. */
11952 if (cp_lexer_next_token_is_not (parser->lexer,
11953 CPP_COLON)
11954 && cp_lexer_next_token_is_not (parser->lexer,
11955 CPP_SCOPE)
11956 && cp_lexer_next_token_is_not (parser->lexer,
11957 CPP_CLOSE_PAREN))
11958 outputs = cp_parser_asm_operand_list (parser);
11959
11960 if (outputs == error_mark_node)
11961 invalid_outputs_p = true;
11962 }
11963 /* If the next token is `::', there are no outputs, and the
11964 next token is the beginning of the inputs. */
11965 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11966 /* The inputs are coming next. */
11967 inputs_p = true;
11968
11969 /* Look for inputs. */
11970 if (inputs_p
11971 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11972 {
11973 /* Consume the `:' or `::'. */
11974 cp_lexer_consume_token (parser->lexer);
11975 /* Parse the output-operands. */
11976 if (cp_lexer_next_token_is_not (parser->lexer,
11977 CPP_COLON)
11978 && cp_lexer_next_token_is_not (parser->lexer,
11979 CPP_CLOSE_PAREN))
11980 inputs = cp_parser_asm_operand_list (parser);
11981
11982 if (inputs == error_mark_node)
11983 invalid_inputs_p = true;
11984 }
11985 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11986 /* The clobbers are coming next. */
11987 clobbers_p = true;
11988
11989 /* Look for clobbers. */
11990 if (clobbers_p
11991 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11992 {
11993 /* Consume the `:' or `::'. */
11994 cp_lexer_consume_token (parser->lexer);
11995 /* Parse the clobbers. */
11996 if (cp_lexer_next_token_is_not (parser->lexer,
11997 CPP_CLOSE_PAREN))
11998 clobbers = cp_parser_asm_clobber_list (parser);
11999 }
12000 }
12001 /* Look for the closing `)'. */
12002 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12003 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12004 /*consume_paren=*/true);
12005 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12006
12007 if (!invalid_inputs_p && !invalid_outputs_p)
12008 {
12009 /* Create the ASM_EXPR. */
12010 if (parser->in_function_body)
12011 {
12012 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12013 inputs, clobbers);
12014 /* If the extended syntax was not used, mark the ASM_EXPR. */
12015 if (!extended_p)
12016 {
12017 tree temp = asm_stmt;
12018 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12019 temp = TREE_OPERAND (temp, 0);
12020
12021 ASM_INPUT_P (temp) = 1;
12022 }
12023 }
12024 else
12025 cgraph_add_asm_node (string);
12026 }
12027 }
12028
12029 /* Declarators [gram.dcl.decl] */
12030
12031 /* Parse an init-declarator.
12032
12033 init-declarator:
12034 declarator initializer [opt]
12035
12036 GNU Extension:
12037
12038 init-declarator:
12039 declarator asm-specification [opt] attributes [opt] initializer [opt]
12040
12041 function-definition:
12042 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12043 function-body
12044 decl-specifier-seq [opt] declarator function-try-block
12045
12046 GNU Extension:
12047
12048 function-definition:
12049 __extension__ function-definition
12050
12051 The DECL_SPECIFIERS apply to this declarator. Returns a
12052 representation of the entity declared. If MEMBER_P is TRUE, then
12053 this declarator appears in a class scope. The new DECL created by
12054 this declarator is returned.
12055
12056 The CHECKS are access checks that should be performed once we know
12057 what entity is being declared (and, therefore, what classes have
12058 befriended it).
12059
12060 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12061 for a function-definition here as well. If the declarator is a
12062 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12063 be TRUE upon return. By that point, the function-definition will
12064 have been completely parsed.
12065
12066 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12067 is FALSE. */
12068
12069 static tree
12070 cp_parser_init_declarator (cp_parser* parser,
12071 cp_decl_specifier_seq *decl_specifiers,
12072 VEC (deferred_access_check,gc)* checks,
12073 bool function_definition_allowed_p,
12074 bool member_p,
12075 int declares_class_or_enum,
12076 bool* function_definition_p)
12077 {
12078 cp_token *token;
12079 cp_declarator *declarator;
12080 tree prefix_attributes;
12081 tree attributes;
12082 tree asm_specification;
12083 tree initializer;
12084 tree decl = NULL_TREE;
12085 tree scope;
12086 bool is_initialized;
12087 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12088 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12089 "(...)". */
12090 enum cpp_ttype initialization_kind;
12091 bool is_parenthesized_init = false;
12092 bool is_non_constant_init;
12093 int ctor_dtor_or_conv_p;
12094 bool friend_p;
12095 tree pushed_scope = NULL;
12096
12097 /* Gather the attributes that were provided with the
12098 decl-specifiers. */
12099 prefix_attributes = decl_specifiers->attributes;
12100
12101 /* Assume that this is not the declarator for a function
12102 definition. */
12103 if (function_definition_p)
12104 *function_definition_p = false;
12105
12106 /* Defer access checks while parsing the declarator; we cannot know
12107 what names are accessible until we know what is being
12108 declared. */
12109 resume_deferring_access_checks ();
12110
12111 /* Parse the declarator. */
12112 declarator
12113 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12114 &ctor_dtor_or_conv_p,
12115 /*parenthesized_p=*/NULL,
12116 /*member_p=*/false);
12117 /* Gather up the deferred checks. */
12118 stop_deferring_access_checks ();
12119
12120 /* If the DECLARATOR was erroneous, there's no need to go
12121 further. */
12122 if (declarator == cp_error_declarator)
12123 return error_mark_node;
12124
12125 /* Check that the number of template-parameter-lists is OK. */
12126 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12127 return error_mark_node;
12128
12129 if (declares_class_or_enum & 2)
12130 cp_parser_check_for_definition_in_return_type (declarator,
12131 decl_specifiers->type);
12132
12133 /* Figure out what scope the entity declared by the DECLARATOR is
12134 located in. `grokdeclarator' sometimes changes the scope, so
12135 we compute it now. */
12136 scope = get_scope_of_declarator (declarator);
12137
12138 /* If we're allowing GNU extensions, look for an asm-specification
12139 and attributes. */
12140 if (cp_parser_allow_gnu_extensions_p (parser))
12141 {
12142 /* Look for an asm-specification. */
12143 asm_specification = cp_parser_asm_specification_opt (parser);
12144 /* And attributes. */
12145 attributes = cp_parser_attributes_opt (parser);
12146 }
12147 else
12148 {
12149 asm_specification = NULL_TREE;
12150 attributes = NULL_TREE;
12151 }
12152
12153 /* Peek at the next token. */
12154 token = cp_lexer_peek_token (parser->lexer);
12155 /* Check to see if the token indicates the start of a
12156 function-definition. */
12157 if (cp_parser_token_starts_function_definition_p (token))
12158 {
12159 if (!function_definition_allowed_p)
12160 {
12161 /* If a function-definition should not appear here, issue an
12162 error message. */
12163 cp_parser_error (parser,
12164 "a function-definition is not allowed here");
12165 return error_mark_node;
12166 }
12167 else
12168 {
12169 /* Neither attributes nor an asm-specification are allowed
12170 on a function-definition. */
12171 if (asm_specification)
12172 error ("an asm-specification is not allowed on a function-definition");
12173 if (attributes)
12174 error ("attributes are not allowed on a function-definition");
12175 /* This is a function-definition. */
12176 *function_definition_p = true;
12177
12178 /* Parse the function definition. */
12179 if (member_p)
12180 decl = cp_parser_save_member_function_body (parser,
12181 decl_specifiers,
12182 declarator,
12183 prefix_attributes);
12184 else
12185 decl
12186 = (cp_parser_function_definition_from_specifiers_and_declarator
12187 (parser, decl_specifiers, prefix_attributes, declarator));
12188
12189 return decl;
12190 }
12191 }
12192
12193 /* [dcl.dcl]
12194
12195 Only in function declarations for constructors, destructors, and
12196 type conversions can the decl-specifier-seq be omitted.
12197
12198 We explicitly postpone this check past the point where we handle
12199 function-definitions because we tolerate function-definitions
12200 that are missing their return types in some modes. */
12201 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12202 {
12203 cp_parser_error (parser,
12204 "expected constructor, destructor, or type conversion");
12205 return error_mark_node;
12206 }
12207
12208 /* An `=' or an `(' indicates an initializer. */
12209 if (token->type == CPP_EQ
12210 || token->type == CPP_OPEN_PAREN)
12211 {
12212 is_initialized = true;
12213 initialization_kind = token->type;
12214 }
12215 else
12216 {
12217 /* If the init-declarator isn't initialized and isn't followed by a
12218 `,' or `;', it's not a valid init-declarator. */
12219 if (token->type != CPP_COMMA
12220 && token->type != CPP_SEMICOLON)
12221 {
12222 cp_parser_error (parser, "expected initializer");
12223 return error_mark_node;
12224 }
12225 is_initialized = false;
12226 initialization_kind = CPP_EOF;
12227 }
12228
12229 /* Because start_decl has side-effects, we should only call it if we
12230 know we're going ahead. By this point, we know that we cannot
12231 possibly be looking at any other construct. */
12232 cp_parser_commit_to_tentative_parse (parser);
12233
12234 /* If the decl specifiers were bad, issue an error now that we're
12235 sure this was intended to be a declarator. Then continue
12236 declaring the variable(s), as int, to try to cut down on further
12237 errors. */
12238 if (decl_specifiers->any_specifiers_p
12239 && decl_specifiers->type == error_mark_node)
12240 {
12241 cp_parser_error (parser, "invalid type in declaration");
12242 decl_specifiers->type = integer_type_node;
12243 }
12244
12245 /* Check to see whether or not this declaration is a friend. */
12246 friend_p = cp_parser_friend_p (decl_specifiers);
12247
12248 /* Enter the newly declared entry in the symbol table. If we're
12249 processing a declaration in a class-specifier, we wait until
12250 after processing the initializer. */
12251 if (!member_p)
12252 {
12253 if (parser->in_unbraced_linkage_specification_p)
12254 decl_specifiers->storage_class = sc_extern;
12255 decl = start_decl (declarator, decl_specifiers,
12256 is_initialized, attributes, prefix_attributes,
12257 &pushed_scope);
12258 }
12259 else if (scope)
12260 /* Enter the SCOPE. That way unqualified names appearing in the
12261 initializer will be looked up in SCOPE. */
12262 pushed_scope = push_scope (scope);
12263
12264 /* Perform deferred access control checks, now that we know in which
12265 SCOPE the declared entity resides. */
12266 if (!member_p && decl)
12267 {
12268 tree saved_current_function_decl = NULL_TREE;
12269
12270 /* If the entity being declared is a function, pretend that we
12271 are in its scope. If it is a `friend', it may have access to
12272 things that would not otherwise be accessible. */
12273 if (TREE_CODE (decl) == FUNCTION_DECL)
12274 {
12275 saved_current_function_decl = current_function_decl;
12276 current_function_decl = decl;
12277 }
12278
12279 /* Perform access checks for template parameters. */
12280 cp_parser_perform_template_parameter_access_checks (checks);
12281
12282 /* Perform the access control checks for the declarator and the
12283 the decl-specifiers. */
12284 perform_deferred_access_checks ();
12285
12286 /* Restore the saved value. */
12287 if (TREE_CODE (decl) == FUNCTION_DECL)
12288 current_function_decl = saved_current_function_decl;
12289 }
12290
12291 /* Parse the initializer. */
12292 initializer = NULL_TREE;
12293 is_parenthesized_init = false;
12294 is_non_constant_init = true;
12295 if (is_initialized)
12296 {
12297 if (function_declarator_p (declarator))
12298 {
12299 if (initialization_kind == CPP_EQ)
12300 initializer = cp_parser_pure_specifier (parser);
12301 else
12302 {
12303 /* If the declaration was erroneous, we don't really
12304 know what the user intended, so just silently
12305 consume the initializer. */
12306 if (decl != error_mark_node)
12307 error ("initializer provided for function");
12308 cp_parser_skip_to_closing_parenthesis (parser,
12309 /*recovering=*/true,
12310 /*or_comma=*/false,
12311 /*consume_paren=*/true);
12312 }
12313 }
12314 else
12315 initializer = cp_parser_initializer (parser,
12316 &is_parenthesized_init,
12317 &is_non_constant_init);
12318 }
12319
12320 /* The old parser allows attributes to appear after a parenthesized
12321 initializer. Mark Mitchell proposed removing this functionality
12322 on the GCC mailing lists on 2002-08-13. This parser accepts the
12323 attributes -- but ignores them. */
12324 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12325 if (cp_parser_attributes_opt (parser))
12326 warning (OPT_Wattributes,
12327 "attributes after parenthesized initializer ignored");
12328
12329 /* For an in-class declaration, use `grokfield' to create the
12330 declaration. */
12331 if (member_p)
12332 {
12333 if (pushed_scope)
12334 {
12335 pop_scope (pushed_scope);
12336 pushed_scope = false;
12337 }
12338 decl = grokfield (declarator, decl_specifiers,
12339 initializer, !is_non_constant_init,
12340 /*asmspec=*/NULL_TREE,
12341 prefix_attributes);
12342 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12343 cp_parser_save_default_args (parser, decl);
12344 }
12345
12346 /* Finish processing the declaration. But, skip friend
12347 declarations. */
12348 if (!friend_p && decl && decl != error_mark_node)
12349 {
12350 cp_finish_decl (decl,
12351 initializer, !is_non_constant_init,
12352 asm_specification,
12353 /* If the initializer is in parentheses, then this is
12354 a direct-initialization, which means that an
12355 `explicit' constructor is OK. Otherwise, an
12356 `explicit' constructor cannot be used. */
12357 ((is_parenthesized_init || !is_initialized)
12358 ? 0 : LOOKUP_ONLYCONVERTING));
12359 }
12360 else if ((cxx_dialect != cxx98) && friend_p
12361 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12362 /* Core issue #226 (C++0x only): A default template-argument
12363 shall not be specified in a friend class template
12364 declaration. */
12365 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12366 /*is_partial=*/0, /*is_friend_decl=*/1);
12367
12368 if (!friend_p && pushed_scope)
12369 pop_scope (pushed_scope);
12370
12371 return decl;
12372 }
12373
12374 /* Parse a declarator.
12375
12376 declarator:
12377 direct-declarator
12378 ptr-operator declarator
12379
12380 abstract-declarator:
12381 ptr-operator abstract-declarator [opt]
12382 direct-abstract-declarator
12383
12384 GNU Extensions:
12385
12386 declarator:
12387 attributes [opt] direct-declarator
12388 attributes [opt] ptr-operator declarator
12389
12390 abstract-declarator:
12391 attributes [opt] ptr-operator abstract-declarator [opt]
12392 attributes [opt] direct-abstract-declarator
12393
12394 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12395 detect constructor, destructor or conversion operators. It is set
12396 to -1 if the declarator is a name, and +1 if it is a
12397 function. Otherwise it is set to zero. Usually you just want to
12398 test for >0, but internally the negative value is used.
12399
12400 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12401 a decl-specifier-seq unless it declares a constructor, destructor,
12402 or conversion. It might seem that we could check this condition in
12403 semantic analysis, rather than parsing, but that makes it difficult
12404 to handle something like `f()'. We want to notice that there are
12405 no decl-specifiers, and therefore realize that this is an
12406 expression, not a declaration.)
12407
12408 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12409 the declarator is a direct-declarator of the form "(...)".
12410
12411 MEMBER_P is true iff this declarator is a member-declarator. */
12412
12413 static cp_declarator *
12414 cp_parser_declarator (cp_parser* parser,
12415 cp_parser_declarator_kind dcl_kind,
12416 int* ctor_dtor_or_conv_p,
12417 bool* parenthesized_p,
12418 bool member_p)
12419 {
12420 cp_token *token;
12421 cp_declarator *declarator;
12422 enum tree_code code;
12423 cp_cv_quals cv_quals;
12424 tree class_type;
12425 tree attributes = NULL_TREE;
12426
12427 /* Assume this is not a constructor, destructor, or type-conversion
12428 operator. */
12429 if (ctor_dtor_or_conv_p)
12430 *ctor_dtor_or_conv_p = 0;
12431
12432 if (cp_parser_allow_gnu_extensions_p (parser))
12433 attributes = cp_parser_attributes_opt (parser);
12434
12435 /* Peek at the next token. */
12436 token = cp_lexer_peek_token (parser->lexer);
12437
12438 /* Check for the ptr-operator production. */
12439 cp_parser_parse_tentatively (parser);
12440 /* Parse the ptr-operator. */
12441 code = cp_parser_ptr_operator (parser,
12442 &class_type,
12443 &cv_quals);
12444 /* If that worked, then we have a ptr-operator. */
12445 if (cp_parser_parse_definitely (parser))
12446 {
12447 /* If a ptr-operator was found, then this declarator was not
12448 parenthesized. */
12449 if (parenthesized_p)
12450 *parenthesized_p = true;
12451 /* The dependent declarator is optional if we are parsing an
12452 abstract-declarator. */
12453 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12454 cp_parser_parse_tentatively (parser);
12455
12456 /* Parse the dependent declarator. */
12457 declarator = cp_parser_declarator (parser, dcl_kind,
12458 /*ctor_dtor_or_conv_p=*/NULL,
12459 /*parenthesized_p=*/NULL,
12460 /*member_p=*/false);
12461
12462 /* If we are parsing an abstract-declarator, we must handle the
12463 case where the dependent declarator is absent. */
12464 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12465 && !cp_parser_parse_definitely (parser))
12466 declarator = NULL;
12467
12468 declarator = cp_parser_make_indirect_declarator
12469 (code, class_type, cv_quals, declarator);
12470 }
12471 /* Everything else is a direct-declarator. */
12472 else
12473 {
12474 if (parenthesized_p)
12475 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12476 CPP_OPEN_PAREN);
12477 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12478 ctor_dtor_or_conv_p,
12479 member_p);
12480 }
12481
12482 if (attributes && declarator && declarator != cp_error_declarator)
12483 declarator->attributes = attributes;
12484
12485 return declarator;
12486 }
12487
12488 /* Parse a direct-declarator or direct-abstract-declarator.
12489
12490 direct-declarator:
12491 declarator-id
12492 direct-declarator ( parameter-declaration-clause )
12493 cv-qualifier-seq [opt]
12494 exception-specification [opt]
12495 direct-declarator [ constant-expression [opt] ]
12496 ( declarator )
12497
12498 direct-abstract-declarator:
12499 direct-abstract-declarator [opt]
12500 ( parameter-declaration-clause )
12501 cv-qualifier-seq [opt]
12502 exception-specification [opt]
12503 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12504 ( abstract-declarator )
12505
12506 Returns a representation of the declarator. DCL_KIND is
12507 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12508 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12509 we are parsing a direct-declarator. It is
12510 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12511 of ambiguity we prefer an abstract declarator, as per
12512 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12513 cp_parser_declarator. */
12514
12515 static cp_declarator *
12516 cp_parser_direct_declarator (cp_parser* parser,
12517 cp_parser_declarator_kind dcl_kind,
12518 int* ctor_dtor_or_conv_p,
12519 bool member_p)
12520 {
12521 cp_token *token;
12522 cp_declarator *declarator = NULL;
12523 tree scope = NULL_TREE;
12524 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12525 bool saved_in_declarator_p = parser->in_declarator_p;
12526 bool first = true;
12527 tree pushed_scope = NULL_TREE;
12528
12529 while (true)
12530 {
12531 /* Peek at the next token. */
12532 token = cp_lexer_peek_token (parser->lexer);
12533 if (token->type == CPP_OPEN_PAREN)
12534 {
12535 /* This is either a parameter-declaration-clause, or a
12536 parenthesized declarator. When we know we are parsing a
12537 named declarator, it must be a parenthesized declarator
12538 if FIRST is true. For instance, `(int)' is a
12539 parameter-declaration-clause, with an omitted
12540 direct-abstract-declarator. But `((*))', is a
12541 parenthesized abstract declarator. Finally, when T is a
12542 template parameter `(T)' is a
12543 parameter-declaration-clause, and not a parenthesized
12544 named declarator.
12545
12546 We first try and parse a parameter-declaration-clause,
12547 and then try a nested declarator (if FIRST is true).
12548
12549 It is not an error for it not to be a
12550 parameter-declaration-clause, even when FIRST is
12551 false. Consider,
12552
12553 int i (int);
12554 int i (3);
12555
12556 The first is the declaration of a function while the
12557 second is a the definition of a variable, including its
12558 initializer.
12559
12560 Having seen only the parenthesis, we cannot know which of
12561 these two alternatives should be selected. Even more
12562 complex are examples like:
12563
12564 int i (int (a));
12565 int i (int (3));
12566
12567 The former is a function-declaration; the latter is a
12568 variable initialization.
12569
12570 Thus again, we try a parameter-declaration-clause, and if
12571 that fails, we back out and return. */
12572
12573 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12574 {
12575 cp_parameter_declarator *params;
12576 unsigned saved_num_template_parameter_lists;
12577
12578 /* In a member-declarator, the only valid interpretation
12579 of a parenthesis is the start of a
12580 parameter-declaration-clause. (It is invalid to
12581 initialize a static data member with a parenthesized
12582 initializer; only the "=" form of initialization is
12583 permitted.) */
12584 if (!member_p)
12585 cp_parser_parse_tentatively (parser);
12586
12587 /* Consume the `('. */
12588 cp_lexer_consume_token (parser->lexer);
12589 if (first)
12590 {
12591 /* If this is going to be an abstract declarator, we're
12592 in a declarator and we can't have default args. */
12593 parser->default_arg_ok_p = false;
12594 parser->in_declarator_p = true;
12595 }
12596
12597 /* Inside the function parameter list, surrounding
12598 template-parameter-lists do not apply. */
12599 saved_num_template_parameter_lists
12600 = parser->num_template_parameter_lists;
12601 parser->num_template_parameter_lists = 0;
12602
12603 /* Parse the parameter-declaration-clause. */
12604 params = cp_parser_parameter_declaration_clause (parser);
12605
12606 parser->num_template_parameter_lists
12607 = saved_num_template_parameter_lists;
12608
12609 /* If all went well, parse the cv-qualifier-seq and the
12610 exception-specification. */
12611 if (member_p || cp_parser_parse_definitely (parser))
12612 {
12613 cp_cv_quals cv_quals;
12614 tree exception_specification;
12615
12616 if (ctor_dtor_or_conv_p)
12617 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12618 first = false;
12619 /* Consume the `)'. */
12620 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12621
12622 /* Parse the cv-qualifier-seq. */
12623 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12624 /* And the exception-specification. */
12625 exception_specification
12626 = cp_parser_exception_specification_opt (parser);
12627
12628 /* Create the function-declarator. */
12629 declarator = make_call_declarator (declarator,
12630 params,
12631 cv_quals,
12632 exception_specification);
12633 /* Any subsequent parameter lists are to do with
12634 return type, so are not those of the declared
12635 function. */
12636 parser->default_arg_ok_p = false;
12637
12638 /* Repeat the main loop. */
12639 continue;
12640 }
12641 }
12642
12643 /* If this is the first, we can try a parenthesized
12644 declarator. */
12645 if (first)
12646 {
12647 bool saved_in_type_id_in_expr_p;
12648
12649 parser->default_arg_ok_p = saved_default_arg_ok_p;
12650 parser->in_declarator_p = saved_in_declarator_p;
12651
12652 /* Consume the `('. */
12653 cp_lexer_consume_token (parser->lexer);
12654 /* Parse the nested declarator. */
12655 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12656 parser->in_type_id_in_expr_p = true;
12657 declarator
12658 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12659 /*parenthesized_p=*/NULL,
12660 member_p);
12661 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12662 first = false;
12663 /* Expect a `)'. */
12664 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12665 declarator = cp_error_declarator;
12666 if (declarator == cp_error_declarator)
12667 break;
12668
12669 goto handle_declarator;
12670 }
12671 /* Otherwise, we must be done. */
12672 else
12673 break;
12674 }
12675 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12676 && token->type == CPP_OPEN_SQUARE)
12677 {
12678 /* Parse an array-declarator. */
12679 tree bounds;
12680
12681 if (ctor_dtor_or_conv_p)
12682 *ctor_dtor_or_conv_p = 0;
12683
12684 first = false;
12685 parser->default_arg_ok_p = false;
12686 parser->in_declarator_p = true;
12687 /* Consume the `['. */
12688 cp_lexer_consume_token (parser->lexer);
12689 /* Peek at the next token. */
12690 token = cp_lexer_peek_token (parser->lexer);
12691 /* If the next token is `]', then there is no
12692 constant-expression. */
12693 if (token->type != CPP_CLOSE_SQUARE)
12694 {
12695 bool non_constant_p;
12696
12697 bounds
12698 = cp_parser_constant_expression (parser,
12699 /*allow_non_constant=*/true,
12700 &non_constant_p);
12701 if (!non_constant_p)
12702 bounds = fold_non_dependent_expr (bounds);
12703 /* Normally, the array bound must be an integral constant
12704 expression. However, as an extension, we allow VLAs
12705 in function scopes. */
12706 else if (!parser->in_function_body)
12707 {
12708 error ("array bound is not an integer constant");
12709 bounds = error_mark_node;
12710 }
12711 }
12712 else
12713 bounds = NULL_TREE;
12714 /* Look for the closing `]'. */
12715 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12716 {
12717 declarator = cp_error_declarator;
12718 break;
12719 }
12720
12721 declarator = make_array_declarator (declarator, bounds);
12722 }
12723 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12724 {
12725 tree qualifying_scope;
12726 tree unqualified_name;
12727 special_function_kind sfk;
12728 bool abstract_ok;
12729 bool pack_expansion_p = false;
12730
12731 /* Parse a declarator-id */
12732 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12733 if (abstract_ok)
12734 {
12735 cp_parser_parse_tentatively (parser);
12736
12737 /* If we see an ellipsis, we should be looking at a
12738 parameter pack. */
12739 if (token->type == CPP_ELLIPSIS)
12740 {
12741 /* Consume the `...' */
12742 cp_lexer_consume_token (parser->lexer);
12743
12744 pack_expansion_p = true;
12745 }
12746 }
12747
12748 unqualified_name
12749 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12750 qualifying_scope = parser->scope;
12751 if (abstract_ok)
12752 {
12753 bool okay = false;
12754
12755 if (!unqualified_name && pack_expansion_p)
12756 {
12757 /* Check whether an error occurred. */
12758 okay = !cp_parser_error_occurred (parser);
12759
12760 /* We already consumed the ellipsis to mark a
12761 parameter pack, but we have no way to report it,
12762 so abort the tentative parse. We will be exiting
12763 immediately anyway. */
12764 cp_parser_abort_tentative_parse (parser);
12765 }
12766 else
12767 okay = cp_parser_parse_definitely (parser);
12768
12769 if (!okay)
12770 unqualified_name = error_mark_node;
12771 else if (unqualified_name
12772 && (qualifying_scope
12773 || (TREE_CODE (unqualified_name)
12774 != IDENTIFIER_NODE)))
12775 {
12776 cp_parser_error (parser, "expected unqualified-id");
12777 unqualified_name = error_mark_node;
12778 }
12779 }
12780
12781 if (!unqualified_name)
12782 return NULL;
12783 if (unqualified_name == error_mark_node)
12784 {
12785 declarator = cp_error_declarator;
12786 pack_expansion_p = false;
12787 declarator->parameter_pack_p = false;
12788 break;
12789 }
12790
12791 if (qualifying_scope && at_namespace_scope_p ()
12792 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12793 {
12794 /* In the declaration of a member of a template class
12795 outside of the class itself, the SCOPE will sometimes
12796 be a TYPENAME_TYPE. For example, given:
12797
12798 template <typename T>
12799 int S<T>::R::i = 3;
12800
12801 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12802 this context, we must resolve S<T>::R to an ordinary
12803 type, rather than a typename type.
12804
12805 The reason we normally avoid resolving TYPENAME_TYPEs
12806 is that a specialization of `S' might render
12807 `S<T>::R' not a type. However, if `S' is
12808 specialized, then this `i' will not be used, so there
12809 is no harm in resolving the types here. */
12810 tree type;
12811
12812 /* Resolve the TYPENAME_TYPE. */
12813 type = resolve_typename_type (qualifying_scope,
12814 /*only_current_p=*/false);
12815 /* If that failed, the declarator is invalid. */
12816 if (TREE_CODE (type) == TYPENAME_TYPE)
12817 error ("%<%T::%E%> is not a type",
12818 TYPE_CONTEXT (qualifying_scope),
12819 TYPE_IDENTIFIER (qualifying_scope));
12820 qualifying_scope = type;
12821 }
12822
12823 sfk = sfk_none;
12824
12825 if (unqualified_name)
12826 {
12827 tree class_type;
12828
12829 if (qualifying_scope
12830 && CLASS_TYPE_P (qualifying_scope))
12831 class_type = qualifying_scope;
12832 else
12833 class_type = current_class_type;
12834
12835 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12836 {
12837 tree name_type = TREE_TYPE (unqualified_name);
12838 if (class_type && same_type_p (name_type, class_type))
12839 {
12840 if (qualifying_scope
12841 && CLASSTYPE_USE_TEMPLATE (name_type))
12842 {
12843 error ("invalid use of constructor as a template");
12844 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12845 "name the constructor in a qualified name",
12846 class_type,
12847 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12848 class_type, name_type);
12849 declarator = cp_error_declarator;
12850 break;
12851 }
12852 else
12853 unqualified_name = constructor_name (class_type);
12854 }
12855 else
12856 {
12857 /* We do not attempt to print the declarator
12858 here because we do not have enough
12859 information about its original syntactic
12860 form. */
12861 cp_parser_error (parser, "invalid declarator");
12862 declarator = cp_error_declarator;
12863 break;
12864 }
12865 }
12866
12867 if (class_type)
12868 {
12869 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12870 sfk = sfk_destructor;
12871 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12872 sfk = sfk_conversion;
12873 else if (/* There's no way to declare a constructor
12874 for an anonymous type, even if the type
12875 got a name for linkage purposes. */
12876 !TYPE_WAS_ANONYMOUS (class_type)
12877 && constructor_name_p (unqualified_name,
12878 class_type))
12879 {
12880 unqualified_name = constructor_name (class_type);
12881 sfk = sfk_constructor;
12882 }
12883
12884 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12885 *ctor_dtor_or_conv_p = -1;
12886 }
12887 }
12888 declarator = make_id_declarator (qualifying_scope,
12889 unqualified_name,
12890 sfk);
12891 declarator->id_loc = token->location;
12892 declarator->parameter_pack_p = pack_expansion_p;
12893
12894 if (pack_expansion_p)
12895 maybe_warn_variadic_templates ();
12896
12897 handle_declarator:;
12898 scope = get_scope_of_declarator (declarator);
12899 if (scope)
12900 /* Any names that appear after the declarator-id for a
12901 member are looked up in the containing scope. */
12902 pushed_scope = push_scope (scope);
12903 parser->in_declarator_p = true;
12904 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12905 || (declarator && declarator->kind == cdk_id))
12906 /* Default args are only allowed on function
12907 declarations. */
12908 parser->default_arg_ok_p = saved_default_arg_ok_p;
12909 else
12910 parser->default_arg_ok_p = false;
12911
12912 first = false;
12913 }
12914 /* We're done. */
12915 else
12916 break;
12917 }
12918
12919 /* For an abstract declarator, we might wind up with nothing at this
12920 point. That's an error; the declarator is not optional. */
12921 if (!declarator)
12922 cp_parser_error (parser, "expected declarator");
12923
12924 /* If we entered a scope, we must exit it now. */
12925 if (pushed_scope)
12926 pop_scope (pushed_scope);
12927
12928 parser->default_arg_ok_p = saved_default_arg_ok_p;
12929 parser->in_declarator_p = saved_in_declarator_p;
12930
12931 return declarator;
12932 }
12933
12934 /* Parse a ptr-operator.
12935
12936 ptr-operator:
12937 * cv-qualifier-seq [opt]
12938 &
12939 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12940
12941 GNU Extension:
12942
12943 ptr-operator:
12944 & cv-qualifier-seq [opt]
12945
12946 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12947 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12948 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12949 filled in with the TYPE containing the member. *CV_QUALS is
12950 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12951 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12952 Note that the tree codes returned by this function have nothing
12953 to do with the types of trees that will be eventually be created
12954 to represent the pointer or reference type being parsed. They are
12955 just constants with suggestive names. */
12956 static enum tree_code
12957 cp_parser_ptr_operator (cp_parser* parser,
12958 tree* type,
12959 cp_cv_quals *cv_quals)
12960 {
12961 enum tree_code code = ERROR_MARK;
12962 cp_token *token;
12963
12964 /* Assume that it's not a pointer-to-member. */
12965 *type = NULL_TREE;
12966 /* And that there are no cv-qualifiers. */
12967 *cv_quals = TYPE_UNQUALIFIED;
12968
12969 /* Peek at the next token. */
12970 token = cp_lexer_peek_token (parser->lexer);
12971
12972 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12973 if (token->type == CPP_MULT)
12974 code = INDIRECT_REF;
12975 else if (token->type == CPP_AND)
12976 code = ADDR_EXPR;
12977 else if ((cxx_dialect != cxx98) &&
12978 token->type == CPP_AND_AND) /* C++0x only */
12979 code = NON_LVALUE_EXPR;
12980
12981 if (code != ERROR_MARK)
12982 {
12983 /* Consume the `*', `&' or `&&'. */
12984 cp_lexer_consume_token (parser->lexer);
12985
12986 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12987 `&', if we are allowing GNU extensions. (The only qualifier
12988 that can legally appear after `&' is `restrict', but that is
12989 enforced during semantic analysis. */
12990 if (code == INDIRECT_REF
12991 || cp_parser_allow_gnu_extensions_p (parser))
12992 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12993 }
12994 else
12995 {
12996 /* Try the pointer-to-member case. */
12997 cp_parser_parse_tentatively (parser);
12998 /* Look for the optional `::' operator. */
12999 cp_parser_global_scope_opt (parser,
13000 /*current_scope_valid_p=*/false);
13001 /* Look for the nested-name specifier. */
13002 cp_parser_nested_name_specifier (parser,
13003 /*typename_keyword_p=*/false,
13004 /*check_dependency_p=*/true,
13005 /*type_p=*/false,
13006 /*is_declaration=*/false);
13007 /* If we found it, and the next token is a `*', then we are
13008 indeed looking at a pointer-to-member operator. */
13009 if (!cp_parser_error_occurred (parser)
13010 && cp_parser_require (parser, CPP_MULT, "`*'"))
13011 {
13012 /* Indicate that the `*' operator was used. */
13013 code = INDIRECT_REF;
13014
13015 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13016 error ("%qD is a namespace", parser->scope);
13017 else
13018 {
13019 /* The type of which the member is a member is given by the
13020 current SCOPE. */
13021 *type = parser->scope;
13022 /* The next name will not be qualified. */
13023 parser->scope = NULL_TREE;
13024 parser->qualifying_scope = NULL_TREE;
13025 parser->object_scope = NULL_TREE;
13026 /* Look for the optional cv-qualifier-seq. */
13027 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13028 }
13029 }
13030 /* If that didn't work we don't have a ptr-operator. */
13031 if (!cp_parser_parse_definitely (parser))
13032 cp_parser_error (parser, "expected ptr-operator");
13033 }
13034
13035 return code;
13036 }
13037
13038 /* Parse an (optional) cv-qualifier-seq.
13039
13040 cv-qualifier-seq:
13041 cv-qualifier cv-qualifier-seq [opt]
13042
13043 cv-qualifier:
13044 const
13045 volatile
13046
13047 GNU Extension:
13048
13049 cv-qualifier:
13050 __restrict__
13051
13052 Returns a bitmask representing the cv-qualifiers. */
13053
13054 static cp_cv_quals
13055 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13056 {
13057 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13058
13059 while (true)
13060 {
13061 cp_token *token;
13062 cp_cv_quals cv_qualifier;
13063
13064 /* Peek at the next token. */
13065 token = cp_lexer_peek_token (parser->lexer);
13066 /* See if it's a cv-qualifier. */
13067 switch (token->keyword)
13068 {
13069 case RID_CONST:
13070 cv_qualifier = TYPE_QUAL_CONST;
13071 break;
13072
13073 case RID_VOLATILE:
13074 cv_qualifier = TYPE_QUAL_VOLATILE;
13075 break;
13076
13077 case RID_RESTRICT:
13078 cv_qualifier = TYPE_QUAL_RESTRICT;
13079 break;
13080
13081 default:
13082 cv_qualifier = TYPE_UNQUALIFIED;
13083 break;
13084 }
13085
13086 if (!cv_qualifier)
13087 break;
13088
13089 if (cv_quals & cv_qualifier)
13090 {
13091 error ("duplicate cv-qualifier");
13092 cp_lexer_purge_token (parser->lexer);
13093 }
13094 else
13095 {
13096 cp_lexer_consume_token (parser->lexer);
13097 cv_quals |= cv_qualifier;
13098 }
13099 }
13100
13101 return cv_quals;
13102 }
13103
13104 /* Parse a declarator-id.
13105
13106 declarator-id:
13107 id-expression
13108 :: [opt] nested-name-specifier [opt] type-name
13109
13110 In the `id-expression' case, the value returned is as for
13111 cp_parser_id_expression if the id-expression was an unqualified-id.
13112 If the id-expression was a qualified-id, then a SCOPE_REF is
13113 returned. The first operand is the scope (either a NAMESPACE_DECL
13114 or TREE_TYPE), but the second is still just a representation of an
13115 unqualified-id. */
13116
13117 static tree
13118 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13119 {
13120 tree id;
13121 /* The expression must be an id-expression. Assume that qualified
13122 names are the names of types so that:
13123
13124 template <class T>
13125 int S<T>::R::i = 3;
13126
13127 will work; we must treat `S<T>::R' as the name of a type.
13128 Similarly, assume that qualified names are templates, where
13129 required, so that:
13130
13131 template <class T>
13132 int S<T>::R<T>::i = 3;
13133
13134 will work, too. */
13135 id = cp_parser_id_expression (parser,
13136 /*template_keyword_p=*/false,
13137 /*check_dependency_p=*/false,
13138 /*template_p=*/NULL,
13139 /*declarator_p=*/true,
13140 optional_p);
13141 if (id && BASELINK_P (id))
13142 id = BASELINK_FUNCTIONS (id);
13143 return id;
13144 }
13145
13146 /* Parse a type-id.
13147
13148 type-id:
13149 type-specifier-seq abstract-declarator [opt]
13150
13151 Returns the TYPE specified. */
13152
13153 static tree
13154 cp_parser_type_id (cp_parser* parser)
13155 {
13156 cp_decl_specifier_seq type_specifier_seq;
13157 cp_declarator *abstract_declarator;
13158
13159 /* Parse the type-specifier-seq. */
13160 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13161 &type_specifier_seq);
13162 if (type_specifier_seq.type == error_mark_node)
13163 return error_mark_node;
13164
13165 /* There might or might not be an abstract declarator. */
13166 cp_parser_parse_tentatively (parser);
13167 /* Look for the declarator. */
13168 abstract_declarator
13169 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13170 /*parenthesized_p=*/NULL,
13171 /*member_p=*/false);
13172 /* Check to see if there really was a declarator. */
13173 if (!cp_parser_parse_definitely (parser))
13174 abstract_declarator = NULL;
13175
13176 return groktypename (&type_specifier_seq, abstract_declarator);
13177 }
13178
13179 /* Parse a type-specifier-seq.
13180
13181 type-specifier-seq:
13182 type-specifier type-specifier-seq [opt]
13183
13184 GNU extension:
13185
13186 type-specifier-seq:
13187 attributes type-specifier-seq [opt]
13188
13189 If IS_CONDITION is true, we are at the start of a "condition",
13190 e.g., we've just seen "if (".
13191
13192 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13193
13194 static void
13195 cp_parser_type_specifier_seq (cp_parser* parser,
13196 bool is_condition,
13197 cp_decl_specifier_seq *type_specifier_seq)
13198 {
13199 bool seen_type_specifier = false;
13200 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13201
13202 /* Clear the TYPE_SPECIFIER_SEQ. */
13203 clear_decl_specs (type_specifier_seq);
13204
13205 /* Parse the type-specifiers and attributes. */
13206 while (true)
13207 {
13208 tree type_specifier;
13209 bool is_cv_qualifier;
13210
13211 /* Check for attributes first. */
13212 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13213 {
13214 type_specifier_seq->attributes =
13215 chainon (type_specifier_seq->attributes,
13216 cp_parser_attributes_opt (parser));
13217 continue;
13218 }
13219
13220 /* Look for the type-specifier. */
13221 type_specifier = cp_parser_type_specifier (parser,
13222 flags,
13223 type_specifier_seq,
13224 /*is_declaration=*/false,
13225 NULL,
13226 &is_cv_qualifier);
13227 if (!type_specifier)
13228 {
13229 /* If the first type-specifier could not be found, this is not a
13230 type-specifier-seq at all. */
13231 if (!seen_type_specifier)
13232 {
13233 cp_parser_error (parser, "expected type-specifier");
13234 type_specifier_seq->type = error_mark_node;
13235 return;
13236 }
13237 /* If subsequent type-specifiers could not be found, the
13238 type-specifier-seq is complete. */
13239 break;
13240 }
13241
13242 seen_type_specifier = true;
13243 /* The standard says that a condition can be:
13244
13245 type-specifier-seq declarator = assignment-expression
13246
13247 However, given:
13248
13249 struct S {};
13250 if (int S = ...)
13251
13252 we should treat the "S" as a declarator, not as a
13253 type-specifier. The standard doesn't say that explicitly for
13254 type-specifier-seq, but it does say that for
13255 decl-specifier-seq in an ordinary declaration. Perhaps it
13256 would be clearer just to allow a decl-specifier-seq here, and
13257 then add a semantic restriction that if any decl-specifiers
13258 that are not type-specifiers appear, the program is invalid. */
13259 if (is_condition && !is_cv_qualifier)
13260 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13261 }
13262
13263 cp_parser_check_decl_spec (type_specifier_seq);
13264 }
13265
13266 /* Parse a parameter-declaration-clause.
13267
13268 parameter-declaration-clause:
13269 parameter-declaration-list [opt] ... [opt]
13270 parameter-declaration-list , ...
13271
13272 Returns a representation for the parameter declarations. A return
13273 value of NULL indicates a parameter-declaration-clause consisting
13274 only of an ellipsis. */
13275
13276 static cp_parameter_declarator *
13277 cp_parser_parameter_declaration_clause (cp_parser* parser)
13278 {
13279 cp_parameter_declarator *parameters;
13280 cp_token *token;
13281 bool ellipsis_p;
13282 bool is_error;
13283
13284 /* Peek at the next token. */
13285 token = cp_lexer_peek_token (parser->lexer);
13286 /* Check for trivial parameter-declaration-clauses. */
13287 if (token->type == CPP_ELLIPSIS)
13288 {
13289 /* Consume the `...' token. */
13290 cp_lexer_consume_token (parser->lexer);
13291 return NULL;
13292 }
13293 else if (token->type == CPP_CLOSE_PAREN)
13294 /* There are no parameters. */
13295 {
13296 #ifndef NO_IMPLICIT_EXTERN_C
13297 if (in_system_header && current_class_type == NULL
13298 && current_lang_name == lang_name_c)
13299 return NULL;
13300 else
13301 #endif
13302 return no_parameters;
13303 }
13304 /* Check for `(void)', too, which is a special case. */
13305 else if (token->keyword == RID_VOID
13306 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13307 == CPP_CLOSE_PAREN))
13308 {
13309 /* Consume the `void' token. */
13310 cp_lexer_consume_token (parser->lexer);
13311 /* There are no parameters. */
13312 return no_parameters;
13313 }
13314
13315 /* Parse the parameter-declaration-list. */
13316 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13317 /* If a parse error occurred while parsing the
13318 parameter-declaration-list, then the entire
13319 parameter-declaration-clause is erroneous. */
13320 if (is_error)
13321 return NULL;
13322
13323 /* Peek at the next token. */
13324 token = cp_lexer_peek_token (parser->lexer);
13325 /* If it's a `,', the clause should terminate with an ellipsis. */
13326 if (token->type == CPP_COMMA)
13327 {
13328 /* Consume the `,'. */
13329 cp_lexer_consume_token (parser->lexer);
13330 /* Expect an ellipsis. */
13331 ellipsis_p
13332 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13333 }
13334 /* It might also be `...' if the optional trailing `,' was
13335 omitted. */
13336 else if (token->type == CPP_ELLIPSIS)
13337 {
13338 /* Consume the `...' token. */
13339 cp_lexer_consume_token (parser->lexer);
13340 /* And remember that we saw it. */
13341 ellipsis_p = true;
13342 }
13343 else
13344 ellipsis_p = false;
13345
13346 /* Finish the parameter list. */
13347 if (parameters && ellipsis_p)
13348 parameters->ellipsis_p = true;
13349
13350 return parameters;
13351 }
13352
13353 /* Parse a parameter-declaration-list.
13354
13355 parameter-declaration-list:
13356 parameter-declaration
13357 parameter-declaration-list , parameter-declaration
13358
13359 Returns a representation of the parameter-declaration-list, as for
13360 cp_parser_parameter_declaration_clause. However, the
13361 `void_list_node' is never appended to the list. Upon return,
13362 *IS_ERROR will be true iff an error occurred. */
13363
13364 static cp_parameter_declarator *
13365 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13366 {
13367 cp_parameter_declarator *parameters = NULL;
13368 cp_parameter_declarator **tail = &parameters;
13369 bool saved_in_unbraced_linkage_specification_p;
13370
13371 /* Assume all will go well. */
13372 *is_error = false;
13373 /* The special considerations that apply to a function within an
13374 unbraced linkage specifications do not apply to the parameters
13375 to the function. */
13376 saved_in_unbraced_linkage_specification_p
13377 = parser->in_unbraced_linkage_specification_p;
13378 parser->in_unbraced_linkage_specification_p = false;
13379
13380 /* Look for more parameters. */
13381 while (true)
13382 {
13383 cp_parameter_declarator *parameter;
13384 bool parenthesized_p;
13385 /* Parse the parameter. */
13386 parameter
13387 = cp_parser_parameter_declaration (parser,
13388 /*template_parm_p=*/false,
13389 &parenthesized_p);
13390
13391 /* If a parse error occurred parsing the parameter declaration,
13392 then the entire parameter-declaration-list is erroneous. */
13393 if (!parameter)
13394 {
13395 *is_error = true;
13396 parameters = NULL;
13397 break;
13398 }
13399 /* Add the new parameter to the list. */
13400 *tail = parameter;
13401 tail = &parameter->next;
13402
13403 /* Peek at the next token. */
13404 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13405 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13406 /* These are for Objective-C++ */
13407 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13408 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13409 /* The parameter-declaration-list is complete. */
13410 break;
13411 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13412 {
13413 cp_token *token;
13414
13415 /* Peek at the next token. */
13416 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13417 /* If it's an ellipsis, then the list is complete. */
13418 if (token->type == CPP_ELLIPSIS)
13419 break;
13420 /* Otherwise, there must be more parameters. Consume the
13421 `,'. */
13422 cp_lexer_consume_token (parser->lexer);
13423 /* When parsing something like:
13424
13425 int i(float f, double d)
13426
13427 we can tell after seeing the declaration for "f" that we
13428 are not looking at an initialization of a variable "i",
13429 but rather at the declaration of a function "i".
13430
13431 Due to the fact that the parsing of template arguments
13432 (as specified to a template-id) requires backtracking we
13433 cannot use this technique when inside a template argument
13434 list. */
13435 if (!parser->in_template_argument_list_p
13436 && !parser->in_type_id_in_expr_p
13437 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13438 /* However, a parameter-declaration of the form
13439 "foat(f)" (which is a valid declaration of a
13440 parameter "f") can also be interpreted as an
13441 expression (the conversion of "f" to "float"). */
13442 && !parenthesized_p)
13443 cp_parser_commit_to_tentative_parse (parser);
13444 }
13445 else
13446 {
13447 cp_parser_error (parser, "expected %<,%> or %<...%>");
13448 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13449 cp_parser_skip_to_closing_parenthesis (parser,
13450 /*recovering=*/true,
13451 /*or_comma=*/false,
13452 /*consume_paren=*/false);
13453 break;
13454 }
13455 }
13456
13457 parser->in_unbraced_linkage_specification_p
13458 = saved_in_unbraced_linkage_specification_p;
13459
13460 return parameters;
13461 }
13462
13463 /* Parse a parameter declaration.
13464
13465 parameter-declaration:
13466 decl-specifier-seq ... [opt] declarator
13467 decl-specifier-seq declarator = assignment-expression
13468 decl-specifier-seq ... [opt] abstract-declarator [opt]
13469 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13470
13471 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13472 declares a template parameter. (In that case, a non-nested `>'
13473 token encountered during the parsing of the assignment-expression
13474 is not interpreted as a greater-than operator.)
13475
13476 Returns a representation of the parameter, or NULL if an error
13477 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13478 true iff the declarator is of the form "(p)". */
13479
13480 static cp_parameter_declarator *
13481 cp_parser_parameter_declaration (cp_parser *parser,
13482 bool template_parm_p,
13483 bool *parenthesized_p)
13484 {
13485 int declares_class_or_enum;
13486 bool greater_than_is_operator_p;
13487 cp_decl_specifier_seq decl_specifiers;
13488 cp_declarator *declarator;
13489 tree default_argument;
13490 cp_token *token;
13491 const char *saved_message;
13492
13493 /* In a template parameter, `>' is not an operator.
13494
13495 [temp.param]
13496
13497 When parsing a default template-argument for a non-type
13498 template-parameter, the first non-nested `>' is taken as the end
13499 of the template parameter-list rather than a greater-than
13500 operator. */
13501 greater_than_is_operator_p = !template_parm_p;
13502
13503 /* Type definitions may not appear in parameter types. */
13504 saved_message = parser->type_definition_forbidden_message;
13505 parser->type_definition_forbidden_message
13506 = "types may not be defined in parameter types";
13507
13508 /* Parse the declaration-specifiers. */
13509 cp_parser_decl_specifier_seq (parser,
13510 CP_PARSER_FLAGS_NONE,
13511 &decl_specifiers,
13512 &declares_class_or_enum);
13513 /* If an error occurred, there's no reason to attempt to parse the
13514 rest of the declaration. */
13515 if (cp_parser_error_occurred (parser))
13516 {
13517 parser->type_definition_forbidden_message = saved_message;
13518 return NULL;
13519 }
13520
13521 /* Peek at the next token. */
13522 token = cp_lexer_peek_token (parser->lexer);
13523
13524 /* If the next token is a `)', `,', `=', `>', or `...', then there
13525 is no declarator. However, when variadic templates are enabled,
13526 there may be a declarator following `...'. */
13527 if (token->type == CPP_CLOSE_PAREN
13528 || token->type == CPP_COMMA
13529 || token->type == CPP_EQ
13530 || token->type == CPP_GREATER)
13531 {
13532 declarator = NULL;
13533 if (parenthesized_p)
13534 *parenthesized_p = false;
13535 }
13536 /* Otherwise, there should be a declarator. */
13537 else
13538 {
13539 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13540 parser->default_arg_ok_p = false;
13541
13542 /* After seeing a decl-specifier-seq, if the next token is not a
13543 "(", there is no possibility that the code is a valid
13544 expression. Therefore, if parsing tentatively, we commit at
13545 this point. */
13546 if (!parser->in_template_argument_list_p
13547 /* In an expression context, having seen:
13548
13549 (int((char ...
13550
13551 we cannot be sure whether we are looking at a
13552 function-type (taking a "char" as a parameter) or a cast
13553 of some object of type "char" to "int". */
13554 && !parser->in_type_id_in_expr_p
13555 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13556 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13557 cp_parser_commit_to_tentative_parse (parser);
13558 /* Parse the declarator. */
13559 declarator = cp_parser_declarator (parser,
13560 CP_PARSER_DECLARATOR_EITHER,
13561 /*ctor_dtor_or_conv_p=*/NULL,
13562 parenthesized_p,
13563 /*member_p=*/false);
13564 parser->default_arg_ok_p = saved_default_arg_ok_p;
13565 /* After the declarator, allow more attributes. */
13566 decl_specifiers.attributes
13567 = chainon (decl_specifiers.attributes,
13568 cp_parser_attributes_opt (parser));
13569 }
13570
13571 /* If the next token is an ellipsis, and we have not seen a
13572 declarator name, and the type of the declarator contains parameter
13573 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13574 a parameter pack expansion expression. Otherwise, leave the
13575 ellipsis for a C-style variadic function. */
13576 token = cp_lexer_peek_token (parser->lexer);
13577 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13578 {
13579 tree type = decl_specifiers.type;
13580
13581 if (type && DECL_P (type))
13582 type = TREE_TYPE (type);
13583
13584 if (type
13585 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13586 && declarator_can_be_parameter_pack (declarator)
13587 && (!declarator || !declarator->parameter_pack_p)
13588 && uses_parameter_packs (type))
13589 {
13590 /* Consume the `...'. */
13591 cp_lexer_consume_token (parser->lexer);
13592 maybe_warn_variadic_templates ();
13593
13594 /* Build a pack expansion type */
13595 if (declarator)
13596 declarator->parameter_pack_p = true;
13597 else
13598 decl_specifiers.type = make_pack_expansion (type);
13599 }
13600 }
13601
13602 /* The restriction on defining new types applies only to the type
13603 of the parameter, not to the default argument. */
13604 parser->type_definition_forbidden_message = saved_message;
13605
13606 /* If the next token is `=', then process a default argument. */
13607 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13608 {
13609 /* Consume the `='. */
13610 cp_lexer_consume_token (parser->lexer);
13611
13612 /* If we are defining a class, then the tokens that make up the
13613 default argument must be saved and processed later. */
13614 if (!template_parm_p && at_class_scope_p ()
13615 && TYPE_BEING_DEFINED (current_class_type))
13616 {
13617 unsigned depth = 0;
13618 cp_token *first_token;
13619 cp_token *token;
13620
13621 /* Add tokens until we have processed the entire default
13622 argument. We add the range [first_token, token). */
13623 first_token = cp_lexer_peek_token (parser->lexer);
13624 while (true)
13625 {
13626 bool done = false;
13627
13628 /* Peek at the next token. */
13629 token = cp_lexer_peek_token (parser->lexer);
13630 /* What we do depends on what token we have. */
13631 switch (token->type)
13632 {
13633 /* In valid code, a default argument must be
13634 immediately followed by a `,' `)', or `...'. */
13635 case CPP_COMMA:
13636 case CPP_CLOSE_PAREN:
13637 case CPP_ELLIPSIS:
13638 /* If we run into a non-nested `;', `}', or `]',
13639 then the code is invalid -- but the default
13640 argument is certainly over. */
13641 case CPP_SEMICOLON:
13642 case CPP_CLOSE_BRACE:
13643 case CPP_CLOSE_SQUARE:
13644 if (depth == 0)
13645 done = true;
13646 /* Update DEPTH, if necessary. */
13647 else if (token->type == CPP_CLOSE_PAREN
13648 || token->type == CPP_CLOSE_BRACE
13649 || token->type == CPP_CLOSE_SQUARE)
13650 --depth;
13651 break;
13652
13653 case CPP_OPEN_PAREN:
13654 case CPP_OPEN_SQUARE:
13655 case CPP_OPEN_BRACE:
13656 ++depth;
13657 break;
13658
13659 case CPP_RSHIFT:
13660 if (cxx_dialect == cxx98)
13661 break;
13662 /* Fall through for C++0x, which treats the `>>'
13663 operator like two `>' tokens in certain
13664 cases. */
13665
13666 case CPP_GREATER:
13667 /* If we see a non-nested `>', and `>' is not an
13668 operator, then it marks the end of the default
13669 argument. */
13670 if (!depth && !greater_than_is_operator_p)
13671 done = true;
13672 break;
13673
13674 /* If we run out of tokens, issue an error message. */
13675 case CPP_EOF:
13676 case CPP_PRAGMA_EOL:
13677 error ("file ends in default argument");
13678 done = true;
13679 break;
13680
13681 case CPP_NAME:
13682 case CPP_SCOPE:
13683 /* In these cases, we should look for template-ids.
13684 For example, if the default argument is
13685 `X<int, double>()', we need to do name lookup to
13686 figure out whether or not `X' is a template; if
13687 so, the `,' does not end the default argument.
13688
13689 That is not yet done. */
13690 break;
13691
13692 default:
13693 break;
13694 }
13695
13696 /* If we've reached the end, stop. */
13697 if (done)
13698 break;
13699
13700 /* Add the token to the token block. */
13701 token = cp_lexer_consume_token (parser->lexer);
13702 }
13703
13704 /* Create a DEFAULT_ARG to represent the unparsed default
13705 argument. */
13706 default_argument = make_node (DEFAULT_ARG);
13707 DEFARG_TOKENS (default_argument)
13708 = cp_token_cache_new (first_token, token);
13709 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13710 }
13711 /* Outside of a class definition, we can just parse the
13712 assignment-expression. */
13713 else
13714 default_argument
13715 = cp_parser_default_argument (parser, template_parm_p);
13716
13717 if (!parser->default_arg_ok_p)
13718 {
13719 if (!flag_pedantic_errors)
13720 warning (0, "deprecated use of default argument for parameter of non-function");
13721 else
13722 {
13723 error ("default arguments are only permitted for function parameters");
13724 default_argument = NULL_TREE;
13725 }
13726 }
13727 else if ((declarator && declarator->parameter_pack_p)
13728 || (decl_specifiers.type
13729 && PACK_EXPANSION_P (decl_specifiers.type)))
13730 {
13731 const char* kind = template_parm_p? "template " : "";
13732
13733 /* Find the name of the parameter pack. */
13734 cp_declarator *id_declarator = declarator;
13735 while (id_declarator && id_declarator->kind != cdk_id)
13736 id_declarator = id_declarator->declarator;
13737
13738 if (id_declarator && id_declarator->kind == cdk_id)
13739 error ("%sparameter pack %qD cannot have a default argument",
13740 kind, id_declarator->u.id.unqualified_name);
13741 else
13742 error ("%sparameter pack cannot have a default argument",
13743 kind);
13744
13745 default_argument = NULL_TREE;
13746 }
13747 }
13748 else
13749 default_argument = NULL_TREE;
13750
13751 return make_parameter_declarator (&decl_specifiers,
13752 declarator,
13753 default_argument);
13754 }
13755
13756 /* Parse a default argument and return it.
13757
13758 TEMPLATE_PARM_P is true if this is a default argument for a
13759 non-type template parameter. */
13760 static tree
13761 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13762 {
13763 tree default_argument = NULL_TREE;
13764 bool saved_greater_than_is_operator_p;
13765 bool saved_local_variables_forbidden_p;
13766
13767 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13768 set correctly. */
13769 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13770 parser->greater_than_is_operator_p = !template_parm_p;
13771 /* Local variable names (and the `this' keyword) may not
13772 appear in a default argument. */
13773 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13774 parser->local_variables_forbidden_p = true;
13775 /* The default argument expression may cause implicitly
13776 defined member functions to be synthesized, which will
13777 result in garbage collection. We must treat this
13778 situation as if we were within the body of function so as
13779 to avoid collecting live data on the stack. */
13780 ++function_depth;
13781 /* Parse the assignment-expression. */
13782 if (template_parm_p)
13783 push_deferring_access_checks (dk_no_deferred);
13784 default_argument
13785 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13786 if (template_parm_p)
13787 pop_deferring_access_checks ();
13788 /* Restore saved state. */
13789 --function_depth;
13790 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13791 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13792
13793 return default_argument;
13794 }
13795
13796 /* Parse a function-body.
13797
13798 function-body:
13799 compound_statement */
13800
13801 static void
13802 cp_parser_function_body (cp_parser *parser)
13803 {
13804 cp_parser_compound_statement (parser, NULL, false);
13805 }
13806
13807 /* Parse a ctor-initializer-opt followed by a function-body. Return
13808 true if a ctor-initializer was present. */
13809
13810 static bool
13811 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13812 {
13813 tree body;
13814 bool ctor_initializer_p;
13815
13816 /* Begin the function body. */
13817 body = begin_function_body ();
13818 /* Parse the optional ctor-initializer. */
13819 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13820 /* Parse the function-body. */
13821 cp_parser_function_body (parser);
13822 /* Finish the function body. */
13823 finish_function_body (body);
13824
13825 return ctor_initializer_p;
13826 }
13827
13828 /* Parse an initializer.
13829
13830 initializer:
13831 = initializer-clause
13832 ( expression-list )
13833
13834 Returns an expression representing the initializer. If no
13835 initializer is present, NULL_TREE is returned.
13836
13837 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13838 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13839 set to FALSE if there is no initializer present. If there is an
13840 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13841 is set to true; otherwise it is set to false. */
13842
13843 static tree
13844 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13845 bool* non_constant_p)
13846 {
13847 cp_token *token;
13848 tree init;
13849
13850 /* Peek at the next token. */
13851 token = cp_lexer_peek_token (parser->lexer);
13852
13853 /* Let our caller know whether or not this initializer was
13854 parenthesized. */
13855 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13856 /* Assume that the initializer is constant. */
13857 *non_constant_p = false;
13858
13859 if (token->type == CPP_EQ)
13860 {
13861 /* Consume the `='. */
13862 cp_lexer_consume_token (parser->lexer);
13863 /* Parse the initializer-clause. */
13864 init = cp_parser_initializer_clause (parser, non_constant_p);
13865 }
13866 else if (token->type == CPP_OPEN_PAREN)
13867 init = cp_parser_parenthesized_expression_list (parser, false,
13868 /*cast_p=*/false,
13869 /*allow_expansion_p=*/true,
13870 non_constant_p);
13871 else
13872 {
13873 /* Anything else is an error. */
13874 cp_parser_error (parser, "expected initializer");
13875 init = error_mark_node;
13876 }
13877
13878 return init;
13879 }
13880
13881 /* Parse an initializer-clause.
13882
13883 initializer-clause:
13884 assignment-expression
13885 { initializer-list , [opt] }
13886 { }
13887
13888 Returns an expression representing the initializer.
13889
13890 If the `assignment-expression' production is used the value
13891 returned is simply a representation for the expression.
13892
13893 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13894 the elements of the initializer-list (or NULL, if the last
13895 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13896 NULL_TREE. There is no way to detect whether or not the optional
13897 trailing `,' was provided. NON_CONSTANT_P is as for
13898 cp_parser_initializer. */
13899
13900 static tree
13901 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13902 {
13903 tree initializer;
13904
13905 /* Assume the expression is constant. */
13906 *non_constant_p = false;
13907
13908 /* If it is not a `{', then we are looking at an
13909 assignment-expression. */
13910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13911 {
13912 initializer
13913 = cp_parser_constant_expression (parser,
13914 /*allow_non_constant_p=*/true,
13915 non_constant_p);
13916 if (!*non_constant_p)
13917 initializer = fold_non_dependent_expr (initializer);
13918 }
13919 else
13920 {
13921 /* Consume the `{' token. */
13922 cp_lexer_consume_token (parser->lexer);
13923 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13924 initializer = make_node (CONSTRUCTOR);
13925 /* If it's not a `}', then there is a non-trivial initializer. */
13926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13927 {
13928 /* Parse the initializer list. */
13929 CONSTRUCTOR_ELTS (initializer)
13930 = cp_parser_initializer_list (parser, non_constant_p);
13931 /* A trailing `,' token is allowed. */
13932 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13933 cp_lexer_consume_token (parser->lexer);
13934 }
13935 /* Now, there should be a trailing `}'. */
13936 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13937 }
13938
13939 return initializer;
13940 }
13941
13942 /* Parse an initializer-list.
13943
13944 initializer-list:
13945 initializer-clause ... [opt]
13946 initializer-list , initializer-clause ... [opt]
13947
13948 GNU Extension:
13949
13950 initializer-list:
13951 identifier : initializer-clause
13952 initializer-list, identifier : initializer-clause
13953
13954 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13955 for the initializer. If the INDEX of the elt is non-NULL, it is the
13956 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13957 as for cp_parser_initializer. */
13958
13959 static VEC(constructor_elt,gc) *
13960 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13961 {
13962 VEC(constructor_elt,gc) *v = NULL;
13963
13964 /* Assume all of the expressions are constant. */
13965 *non_constant_p = false;
13966
13967 /* Parse the rest of the list. */
13968 while (true)
13969 {
13970 cp_token *token;
13971 tree identifier;
13972 tree initializer;
13973 bool clause_non_constant_p;
13974
13975 /* If the next token is an identifier and the following one is a
13976 colon, we are looking at the GNU designated-initializer
13977 syntax. */
13978 if (cp_parser_allow_gnu_extensions_p (parser)
13979 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13980 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13981 {
13982 /* Warn the user that they are using an extension. */
13983 if (pedantic)
13984 pedwarn ("ISO C++ does not allow designated initializers");
13985 /* Consume the identifier. */
13986 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13987 /* Consume the `:'. */
13988 cp_lexer_consume_token (parser->lexer);
13989 }
13990 else
13991 identifier = NULL_TREE;
13992
13993 /* Parse the initializer. */
13994 initializer = cp_parser_initializer_clause (parser,
13995 &clause_non_constant_p);
13996 /* If any clause is non-constant, so is the entire initializer. */
13997 if (clause_non_constant_p)
13998 *non_constant_p = true;
13999
14000 /* If we have an ellipsis, this is an initializer pack
14001 expansion. */
14002 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14003 {
14004 /* Consume the `...'. */
14005 cp_lexer_consume_token (parser->lexer);
14006
14007 /* Turn the initializer into an initializer expansion. */
14008 initializer = make_pack_expansion (initializer);
14009 }
14010
14011 /* Add it to the vector. */
14012 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14013
14014 /* If the next token is not a comma, we have reached the end of
14015 the list. */
14016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14017 break;
14018
14019 /* Peek at the next token. */
14020 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14021 /* If the next token is a `}', then we're still done. An
14022 initializer-clause can have a trailing `,' after the
14023 initializer-list and before the closing `}'. */
14024 if (token->type == CPP_CLOSE_BRACE)
14025 break;
14026
14027 /* Consume the `,' token. */
14028 cp_lexer_consume_token (parser->lexer);
14029 }
14030
14031 return v;
14032 }
14033
14034 /* Classes [gram.class] */
14035
14036 /* Parse a class-name.
14037
14038 class-name:
14039 identifier
14040 template-id
14041
14042 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14043 to indicate that names looked up in dependent types should be
14044 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14045 keyword has been used to indicate that the name that appears next
14046 is a template. TAG_TYPE indicates the explicit tag given before
14047 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14048 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14049 is the class being defined in a class-head.
14050
14051 Returns the TYPE_DECL representing the class. */
14052
14053 static tree
14054 cp_parser_class_name (cp_parser *parser,
14055 bool typename_keyword_p,
14056 bool template_keyword_p,
14057 enum tag_types tag_type,
14058 bool check_dependency_p,
14059 bool class_head_p,
14060 bool is_declaration)
14061 {
14062 tree decl;
14063 tree scope;
14064 bool typename_p;
14065 cp_token *token;
14066
14067 /* All class-names start with an identifier. */
14068 token = cp_lexer_peek_token (parser->lexer);
14069 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14070 {
14071 cp_parser_error (parser, "expected class-name");
14072 return error_mark_node;
14073 }
14074
14075 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14076 to a template-id, so we save it here. */
14077 scope = parser->scope;
14078 if (scope == error_mark_node)
14079 return error_mark_node;
14080
14081 /* Any name names a type if we're following the `typename' keyword
14082 in a qualified name where the enclosing scope is type-dependent. */
14083 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14084 && dependent_type_p (scope));
14085 /* Handle the common case (an identifier, but not a template-id)
14086 efficiently. */
14087 if (token->type == CPP_NAME
14088 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14089 {
14090 cp_token *identifier_token;
14091 tree identifier;
14092 bool ambiguous_p;
14093
14094 /* Look for the identifier. */
14095 identifier_token = cp_lexer_peek_token (parser->lexer);
14096 ambiguous_p = identifier_token->ambiguous_p;
14097 identifier = cp_parser_identifier (parser);
14098 /* If the next token isn't an identifier, we are certainly not
14099 looking at a class-name. */
14100 if (identifier == error_mark_node)
14101 decl = error_mark_node;
14102 /* If we know this is a type-name, there's no need to look it
14103 up. */
14104 else if (typename_p)
14105 decl = identifier;
14106 else
14107 {
14108 tree ambiguous_decls;
14109 /* If we already know that this lookup is ambiguous, then
14110 we've already issued an error message; there's no reason
14111 to check again. */
14112 if (ambiguous_p)
14113 {
14114 cp_parser_simulate_error (parser);
14115 return error_mark_node;
14116 }
14117 /* If the next token is a `::', then the name must be a type
14118 name.
14119
14120 [basic.lookup.qual]
14121
14122 During the lookup for a name preceding the :: scope
14123 resolution operator, object, function, and enumerator
14124 names are ignored. */
14125 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14126 tag_type = typename_type;
14127 /* Look up the name. */
14128 decl = cp_parser_lookup_name (parser, identifier,
14129 tag_type,
14130 /*is_template=*/false,
14131 /*is_namespace=*/false,
14132 check_dependency_p,
14133 &ambiguous_decls);
14134 if (ambiguous_decls)
14135 {
14136 error ("reference to %qD is ambiguous", identifier);
14137 print_candidates (ambiguous_decls);
14138 if (cp_parser_parsing_tentatively (parser))
14139 {
14140 identifier_token->ambiguous_p = true;
14141 cp_parser_simulate_error (parser);
14142 }
14143 return error_mark_node;
14144 }
14145 }
14146 }
14147 else
14148 {
14149 /* Try a template-id. */
14150 decl = cp_parser_template_id (parser, template_keyword_p,
14151 check_dependency_p,
14152 is_declaration);
14153 if (decl == error_mark_node)
14154 return error_mark_node;
14155 }
14156
14157 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14158
14159 /* If this is a typename, create a TYPENAME_TYPE. */
14160 if (typename_p && decl != error_mark_node)
14161 {
14162 decl = make_typename_type (scope, decl, typename_type,
14163 /*complain=*/tf_error);
14164 if (decl != error_mark_node)
14165 decl = TYPE_NAME (decl);
14166 }
14167
14168 /* Check to see that it is really the name of a class. */
14169 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14170 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14171 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14172 /* Situations like this:
14173
14174 template <typename T> struct A {
14175 typename T::template X<int>::I i;
14176 };
14177
14178 are problematic. Is `T::template X<int>' a class-name? The
14179 standard does not seem to be definitive, but there is no other
14180 valid interpretation of the following `::'. Therefore, those
14181 names are considered class-names. */
14182 {
14183 decl = make_typename_type (scope, decl, tag_type, tf_error);
14184 if (decl != error_mark_node)
14185 decl = TYPE_NAME (decl);
14186 }
14187 else if (TREE_CODE (decl) != TYPE_DECL
14188 || TREE_TYPE (decl) == error_mark_node
14189 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14190 decl = error_mark_node;
14191
14192 if (decl == error_mark_node)
14193 cp_parser_error (parser, "expected class-name");
14194
14195 return decl;
14196 }
14197
14198 /* Parse a class-specifier.
14199
14200 class-specifier:
14201 class-head { member-specification [opt] }
14202
14203 Returns the TREE_TYPE representing the class. */
14204
14205 static tree
14206 cp_parser_class_specifier (cp_parser* parser)
14207 {
14208 cp_token *token;
14209 tree type;
14210 tree attributes = NULL_TREE;
14211 int has_trailing_semicolon;
14212 bool nested_name_specifier_p;
14213 unsigned saved_num_template_parameter_lists;
14214 bool saved_in_function_body;
14215 tree old_scope = NULL_TREE;
14216 tree scope = NULL_TREE;
14217 tree bases;
14218
14219 push_deferring_access_checks (dk_no_deferred);
14220
14221 /* Parse the class-head. */
14222 type = cp_parser_class_head (parser,
14223 &nested_name_specifier_p,
14224 &attributes,
14225 &bases);
14226 /* If the class-head was a semantic disaster, skip the entire body
14227 of the class. */
14228 if (!type)
14229 {
14230 cp_parser_skip_to_end_of_block_or_statement (parser);
14231 pop_deferring_access_checks ();
14232 return error_mark_node;
14233 }
14234
14235 /* Look for the `{'. */
14236 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14237 {
14238 pop_deferring_access_checks ();
14239 return error_mark_node;
14240 }
14241
14242 /* Process the base classes. If they're invalid, skip the
14243 entire class body. */
14244 if (!xref_basetypes (type, bases))
14245 {
14246 /* Consuming the closing brace yields better error messages
14247 later on. */
14248 if (cp_parser_skip_to_closing_brace (parser))
14249 cp_lexer_consume_token (parser->lexer);
14250 pop_deferring_access_checks ();
14251 return error_mark_node;
14252 }
14253
14254 /* Issue an error message if type-definitions are forbidden here. */
14255 cp_parser_check_type_definition (parser);
14256 /* Remember that we are defining one more class. */
14257 ++parser->num_classes_being_defined;
14258 /* Inside the class, surrounding template-parameter-lists do not
14259 apply. */
14260 saved_num_template_parameter_lists
14261 = parser->num_template_parameter_lists;
14262 parser->num_template_parameter_lists = 0;
14263 /* We are not in a function body. */
14264 saved_in_function_body = parser->in_function_body;
14265 parser->in_function_body = false;
14266
14267 /* Start the class. */
14268 if (nested_name_specifier_p)
14269 {
14270 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14271 old_scope = push_inner_scope (scope);
14272 }
14273 type = begin_class_definition (type, attributes);
14274
14275 if (type == error_mark_node)
14276 /* If the type is erroneous, skip the entire body of the class. */
14277 cp_parser_skip_to_closing_brace (parser);
14278 else
14279 /* Parse the member-specification. */
14280 cp_parser_member_specification_opt (parser);
14281
14282 /* Look for the trailing `}'. */
14283 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14284 /* We get better error messages by noticing a common problem: a
14285 missing trailing `;'. */
14286 token = cp_lexer_peek_token (parser->lexer);
14287 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14288 /* Look for trailing attributes to apply to this class. */
14289 if (cp_parser_allow_gnu_extensions_p (parser))
14290 attributes = cp_parser_attributes_opt (parser);
14291 if (type != error_mark_node)
14292 type = finish_struct (type, attributes);
14293 if (nested_name_specifier_p)
14294 pop_inner_scope (old_scope, scope);
14295 /* If this class is not itself within the scope of another class,
14296 then we need to parse the bodies of all of the queued function
14297 definitions. Note that the queued functions defined in a class
14298 are not always processed immediately following the
14299 class-specifier for that class. Consider:
14300
14301 struct A {
14302 struct B { void f() { sizeof (A); } };
14303 };
14304
14305 If `f' were processed before the processing of `A' were
14306 completed, there would be no way to compute the size of `A'.
14307 Note that the nesting we are interested in here is lexical --
14308 not the semantic nesting given by TYPE_CONTEXT. In particular,
14309 for:
14310
14311 struct A { struct B; };
14312 struct A::B { void f() { } };
14313
14314 there is no need to delay the parsing of `A::B::f'. */
14315 if (--parser->num_classes_being_defined == 0)
14316 {
14317 tree queue_entry;
14318 tree fn;
14319 tree class_type = NULL_TREE;
14320 tree pushed_scope = NULL_TREE;
14321
14322 /* In a first pass, parse default arguments to the functions.
14323 Then, in a second pass, parse the bodies of the functions.
14324 This two-phased approach handles cases like:
14325
14326 struct S {
14327 void f() { g(); }
14328 void g(int i = 3);
14329 };
14330
14331 */
14332 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14333 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14334 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14335 TREE_PURPOSE (parser->unparsed_functions_queues)
14336 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14337 {
14338 fn = TREE_VALUE (queue_entry);
14339 /* If there are default arguments that have not yet been processed,
14340 take care of them now. */
14341 if (class_type != TREE_PURPOSE (queue_entry))
14342 {
14343 if (pushed_scope)
14344 pop_scope (pushed_scope);
14345 class_type = TREE_PURPOSE (queue_entry);
14346 pushed_scope = push_scope (class_type);
14347 }
14348 /* Make sure that any template parameters are in scope. */
14349 maybe_begin_member_template_processing (fn);
14350 /* Parse the default argument expressions. */
14351 cp_parser_late_parsing_default_args (parser, fn);
14352 /* Remove any template parameters from the symbol table. */
14353 maybe_end_member_template_processing ();
14354 }
14355 if (pushed_scope)
14356 pop_scope (pushed_scope);
14357 /* Now parse the body of the functions. */
14358 for (TREE_VALUE (parser->unparsed_functions_queues)
14359 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14360 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14361 TREE_VALUE (parser->unparsed_functions_queues)
14362 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14363 {
14364 /* Figure out which function we need to process. */
14365 fn = TREE_VALUE (queue_entry);
14366 /* Parse the function. */
14367 cp_parser_late_parsing_for_member (parser, fn);
14368 }
14369 }
14370
14371 /* Put back any saved access checks. */
14372 pop_deferring_access_checks ();
14373
14374 /* Restore saved state. */
14375 parser->in_function_body = saved_in_function_body;
14376 parser->num_template_parameter_lists
14377 = saved_num_template_parameter_lists;
14378
14379 return type;
14380 }
14381
14382 /* Parse a class-head.
14383
14384 class-head:
14385 class-key identifier [opt] base-clause [opt]
14386 class-key nested-name-specifier identifier base-clause [opt]
14387 class-key nested-name-specifier [opt] template-id
14388 base-clause [opt]
14389
14390 GNU Extensions:
14391 class-key attributes identifier [opt] base-clause [opt]
14392 class-key attributes nested-name-specifier identifier base-clause [opt]
14393 class-key attributes nested-name-specifier [opt] template-id
14394 base-clause [opt]
14395
14396 Upon return BASES is initialized to the list of base classes (or
14397 NULL, if there are none) in the same form returned by
14398 cp_parser_base_clause.
14399
14400 Returns the TYPE of the indicated class. Sets
14401 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14402 involving a nested-name-specifier was used, and FALSE otherwise.
14403
14404 Returns error_mark_node if this is not a class-head.
14405
14406 Returns NULL_TREE if the class-head is syntactically valid, but
14407 semantically invalid in a way that means we should skip the entire
14408 body of the class. */
14409
14410 static tree
14411 cp_parser_class_head (cp_parser* parser,
14412 bool* nested_name_specifier_p,
14413 tree *attributes_p,
14414 tree *bases)
14415 {
14416 tree nested_name_specifier;
14417 enum tag_types class_key;
14418 tree id = NULL_TREE;
14419 tree type = NULL_TREE;
14420 tree attributes;
14421 bool template_id_p = false;
14422 bool qualified_p = false;
14423 bool invalid_nested_name_p = false;
14424 bool invalid_explicit_specialization_p = false;
14425 tree pushed_scope = NULL_TREE;
14426 unsigned num_templates;
14427
14428 /* Assume no nested-name-specifier will be present. */
14429 *nested_name_specifier_p = false;
14430 /* Assume no template parameter lists will be used in defining the
14431 type. */
14432 num_templates = 0;
14433
14434 *bases = NULL_TREE;
14435
14436 /* Look for the class-key. */
14437 class_key = cp_parser_class_key (parser);
14438 if (class_key == none_type)
14439 return error_mark_node;
14440
14441 /* Parse the attributes. */
14442 attributes = cp_parser_attributes_opt (parser);
14443
14444 /* If the next token is `::', that is invalid -- but sometimes
14445 people do try to write:
14446
14447 struct ::S {};
14448
14449 Handle this gracefully by accepting the extra qualifier, and then
14450 issuing an error about it later if this really is a
14451 class-head. If it turns out just to be an elaborated type
14452 specifier, remain silent. */
14453 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14454 qualified_p = true;
14455
14456 push_deferring_access_checks (dk_no_check);
14457
14458 /* Determine the name of the class. Begin by looking for an
14459 optional nested-name-specifier. */
14460 nested_name_specifier
14461 = cp_parser_nested_name_specifier_opt (parser,
14462 /*typename_keyword_p=*/false,
14463 /*check_dependency_p=*/false,
14464 /*type_p=*/false,
14465 /*is_declaration=*/false);
14466 /* If there was a nested-name-specifier, then there *must* be an
14467 identifier. */
14468 if (nested_name_specifier)
14469 {
14470 /* Although the grammar says `identifier', it really means
14471 `class-name' or `template-name'. You are only allowed to
14472 define a class that has already been declared with this
14473 syntax.
14474
14475 The proposed resolution for Core Issue 180 says that wherever
14476 you see `class T::X' you should treat `X' as a type-name.
14477
14478 It is OK to define an inaccessible class; for example:
14479
14480 class A { class B; };
14481 class A::B {};
14482
14483 We do not know if we will see a class-name, or a
14484 template-name. We look for a class-name first, in case the
14485 class-name is a template-id; if we looked for the
14486 template-name first we would stop after the template-name. */
14487 cp_parser_parse_tentatively (parser);
14488 type = cp_parser_class_name (parser,
14489 /*typename_keyword_p=*/false,
14490 /*template_keyword_p=*/false,
14491 class_type,
14492 /*check_dependency_p=*/false,
14493 /*class_head_p=*/true,
14494 /*is_declaration=*/false);
14495 /* If that didn't work, ignore the nested-name-specifier. */
14496 if (!cp_parser_parse_definitely (parser))
14497 {
14498 invalid_nested_name_p = true;
14499 id = cp_parser_identifier (parser);
14500 if (id == error_mark_node)
14501 id = NULL_TREE;
14502 }
14503 /* If we could not find a corresponding TYPE, treat this
14504 declaration like an unqualified declaration. */
14505 if (type == error_mark_node)
14506 nested_name_specifier = NULL_TREE;
14507 /* Otherwise, count the number of templates used in TYPE and its
14508 containing scopes. */
14509 else
14510 {
14511 tree scope;
14512
14513 for (scope = TREE_TYPE (type);
14514 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14515 scope = (TYPE_P (scope)
14516 ? TYPE_CONTEXT (scope)
14517 : DECL_CONTEXT (scope)))
14518 if (TYPE_P (scope)
14519 && CLASS_TYPE_P (scope)
14520 && CLASSTYPE_TEMPLATE_INFO (scope)
14521 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14522 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14523 ++num_templates;
14524 }
14525 }
14526 /* Otherwise, the identifier is optional. */
14527 else
14528 {
14529 /* We don't know whether what comes next is a template-id,
14530 an identifier, or nothing at all. */
14531 cp_parser_parse_tentatively (parser);
14532 /* Check for a template-id. */
14533 id = cp_parser_template_id (parser,
14534 /*template_keyword_p=*/false,
14535 /*check_dependency_p=*/true,
14536 /*is_declaration=*/true);
14537 /* If that didn't work, it could still be an identifier. */
14538 if (!cp_parser_parse_definitely (parser))
14539 {
14540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14541 id = cp_parser_identifier (parser);
14542 else
14543 id = NULL_TREE;
14544 }
14545 else
14546 {
14547 template_id_p = true;
14548 ++num_templates;
14549 }
14550 }
14551
14552 pop_deferring_access_checks ();
14553
14554 if (id)
14555 cp_parser_check_for_invalid_template_id (parser, id);
14556
14557 /* If it's not a `:' or a `{' then we can't really be looking at a
14558 class-head, since a class-head only appears as part of a
14559 class-specifier. We have to detect this situation before calling
14560 xref_tag, since that has irreversible side-effects. */
14561 if (!cp_parser_next_token_starts_class_definition_p (parser))
14562 {
14563 cp_parser_error (parser, "expected %<{%> or %<:%>");
14564 return error_mark_node;
14565 }
14566
14567 /* At this point, we're going ahead with the class-specifier, even
14568 if some other problem occurs. */
14569 cp_parser_commit_to_tentative_parse (parser);
14570 /* Issue the error about the overly-qualified name now. */
14571 if (qualified_p)
14572 cp_parser_error (parser,
14573 "global qualification of class name is invalid");
14574 else if (invalid_nested_name_p)
14575 cp_parser_error (parser,
14576 "qualified name does not name a class");
14577 else if (nested_name_specifier)
14578 {
14579 tree scope;
14580
14581 /* Reject typedef-names in class heads. */
14582 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14583 {
14584 error ("invalid class name in declaration of %qD", type);
14585 type = NULL_TREE;
14586 goto done;
14587 }
14588
14589 /* Figure out in what scope the declaration is being placed. */
14590 scope = current_scope ();
14591 /* If that scope does not contain the scope in which the
14592 class was originally declared, the program is invalid. */
14593 if (scope && !is_ancestor (scope, nested_name_specifier))
14594 {
14595 if (at_namespace_scope_p ())
14596 error ("declaration of %qD in namespace %qD which does not "
14597 "enclose %qD", type, scope, nested_name_specifier);
14598 else
14599 error ("declaration of %qD in %qD which does not enclose %qD",
14600 type, scope, nested_name_specifier);
14601 type = NULL_TREE;
14602 goto done;
14603 }
14604 /* [dcl.meaning]
14605
14606 A declarator-id shall not be qualified exception of the
14607 definition of a ... nested class outside of its class
14608 ... [or] a the definition or explicit instantiation of a
14609 class member of a namespace outside of its namespace. */
14610 if (scope == nested_name_specifier)
14611 {
14612 pedwarn ("extra qualification ignored");
14613 nested_name_specifier = NULL_TREE;
14614 num_templates = 0;
14615 }
14616 }
14617 /* An explicit-specialization must be preceded by "template <>". If
14618 it is not, try to recover gracefully. */
14619 if (at_namespace_scope_p ()
14620 && parser->num_template_parameter_lists == 0
14621 && template_id_p)
14622 {
14623 error ("an explicit specialization must be preceded by %<template <>%>");
14624 invalid_explicit_specialization_p = true;
14625 /* Take the same action that would have been taken by
14626 cp_parser_explicit_specialization. */
14627 ++parser->num_template_parameter_lists;
14628 begin_specialization ();
14629 }
14630 /* There must be no "return" statements between this point and the
14631 end of this function; set "type "to the correct return value and
14632 use "goto done;" to return. */
14633 /* Make sure that the right number of template parameters were
14634 present. */
14635 if (!cp_parser_check_template_parameters (parser, num_templates))
14636 {
14637 /* If something went wrong, there is no point in even trying to
14638 process the class-definition. */
14639 type = NULL_TREE;
14640 goto done;
14641 }
14642
14643 /* Look up the type. */
14644 if (template_id_p)
14645 {
14646 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14647 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14648 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14649 {
14650 error ("function template %qD redeclared as a class template", id);
14651 type = error_mark_node;
14652 }
14653 else
14654 {
14655 type = TREE_TYPE (id);
14656 type = maybe_process_partial_specialization (type);
14657 }
14658 if (nested_name_specifier)
14659 pushed_scope = push_scope (nested_name_specifier);
14660 }
14661 else if (nested_name_specifier)
14662 {
14663 tree class_type;
14664
14665 /* Given:
14666
14667 template <typename T> struct S { struct T };
14668 template <typename T> struct S<T>::T { };
14669
14670 we will get a TYPENAME_TYPE when processing the definition of
14671 `S::T'. We need to resolve it to the actual type before we
14672 try to define it. */
14673 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14674 {
14675 class_type = resolve_typename_type (TREE_TYPE (type),
14676 /*only_current_p=*/false);
14677 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14678 type = TYPE_NAME (class_type);
14679 else
14680 {
14681 cp_parser_error (parser, "could not resolve typename type");
14682 type = error_mark_node;
14683 }
14684 }
14685
14686 maybe_process_partial_specialization (TREE_TYPE (type));
14687 class_type = current_class_type;
14688 /* Enter the scope indicated by the nested-name-specifier. */
14689 pushed_scope = push_scope (nested_name_specifier);
14690 /* Get the canonical version of this type. */
14691 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14692 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14693 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14694 {
14695 type = push_template_decl (type);
14696 if (type == error_mark_node)
14697 {
14698 type = NULL_TREE;
14699 goto done;
14700 }
14701 }
14702
14703 type = TREE_TYPE (type);
14704 *nested_name_specifier_p = true;
14705 }
14706 else /* The name is not a nested name. */
14707 {
14708 /* If the class was unnamed, create a dummy name. */
14709 if (!id)
14710 id = make_anon_name ();
14711 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14712 parser->num_template_parameter_lists);
14713 }
14714
14715 /* Indicate whether this class was declared as a `class' or as a
14716 `struct'. */
14717 if (TREE_CODE (type) == RECORD_TYPE)
14718 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14719 cp_parser_check_class_key (class_key, type);
14720
14721 /* If this type was already complete, and we see another definition,
14722 that's an error. */
14723 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14724 {
14725 error ("redefinition of %q#T", type);
14726 error ("previous definition of %q+#T", type);
14727 type = NULL_TREE;
14728 goto done;
14729 }
14730 else if (type == error_mark_node)
14731 type = NULL_TREE;
14732
14733 /* We will have entered the scope containing the class; the names of
14734 base classes should be looked up in that context. For example:
14735
14736 struct A { struct B {}; struct C; };
14737 struct A::C : B {};
14738
14739 is valid. */
14740
14741 /* Get the list of base-classes, if there is one. */
14742 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14743 *bases = cp_parser_base_clause (parser);
14744
14745 done:
14746 /* Leave the scope given by the nested-name-specifier. We will
14747 enter the class scope itself while processing the members. */
14748 if (pushed_scope)
14749 pop_scope (pushed_scope);
14750
14751 if (invalid_explicit_specialization_p)
14752 {
14753 end_specialization ();
14754 --parser->num_template_parameter_lists;
14755 }
14756 *attributes_p = attributes;
14757 return type;
14758 }
14759
14760 /* Parse a class-key.
14761
14762 class-key:
14763 class
14764 struct
14765 union
14766
14767 Returns the kind of class-key specified, or none_type to indicate
14768 error. */
14769
14770 static enum tag_types
14771 cp_parser_class_key (cp_parser* parser)
14772 {
14773 cp_token *token;
14774 enum tag_types tag_type;
14775
14776 /* Look for the class-key. */
14777 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14778 if (!token)
14779 return none_type;
14780
14781 /* Check to see if the TOKEN is a class-key. */
14782 tag_type = cp_parser_token_is_class_key (token);
14783 if (!tag_type)
14784 cp_parser_error (parser, "expected class-key");
14785 return tag_type;
14786 }
14787
14788 /* Parse an (optional) member-specification.
14789
14790 member-specification:
14791 member-declaration member-specification [opt]
14792 access-specifier : member-specification [opt] */
14793
14794 static void
14795 cp_parser_member_specification_opt (cp_parser* parser)
14796 {
14797 while (true)
14798 {
14799 cp_token *token;
14800 enum rid keyword;
14801
14802 /* Peek at the next token. */
14803 token = cp_lexer_peek_token (parser->lexer);
14804 /* If it's a `}', or EOF then we've seen all the members. */
14805 if (token->type == CPP_CLOSE_BRACE
14806 || token->type == CPP_EOF
14807 || token->type == CPP_PRAGMA_EOL)
14808 break;
14809
14810 /* See if this token is a keyword. */
14811 keyword = token->keyword;
14812 switch (keyword)
14813 {
14814 case RID_PUBLIC:
14815 case RID_PROTECTED:
14816 case RID_PRIVATE:
14817 /* Consume the access-specifier. */
14818 cp_lexer_consume_token (parser->lexer);
14819 /* Remember which access-specifier is active. */
14820 current_access_specifier = token->u.value;
14821 /* Look for the `:'. */
14822 cp_parser_require (parser, CPP_COLON, "`:'");
14823 break;
14824
14825 default:
14826 /* Accept #pragmas at class scope. */
14827 if (token->type == CPP_PRAGMA)
14828 {
14829 cp_parser_pragma (parser, pragma_external);
14830 break;
14831 }
14832
14833 /* Otherwise, the next construction must be a
14834 member-declaration. */
14835 cp_parser_member_declaration (parser);
14836 }
14837 }
14838 }
14839
14840 /* Parse a member-declaration.
14841
14842 member-declaration:
14843 decl-specifier-seq [opt] member-declarator-list [opt] ;
14844 function-definition ; [opt]
14845 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14846 using-declaration
14847 template-declaration
14848
14849 member-declarator-list:
14850 member-declarator
14851 member-declarator-list , member-declarator
14852
14853 member-declarator:
14854 declarator pure-specifier [opt]
14855 declarator constant-initializer [opt]
14856 identifier [opt] : constant-expression
14857
14858 GNU Extensions:
14859
14860 member-declaration:
14861 __extension__ member-declaration
14862
14863 member-declarator:
14864 declarator attributes [opt] pure-specifier [opt]
14865 declarator attributes [opt] constant-initializer [opt]
14866 identifier [opt] attributes [opt] : constant-expression
14867
14868 C++0x Extensions:
14869
14870 member-declaration:
14871 static_assert-declaration */
14872
14873 static void
14874 cp_parser_member_declaration (cp_parser* parser)
14875 {
14876 cp_decl_specifier_seq decl_specifiers;
14877 tree prefix_attributes;
14878 tree decl;
14879 int declares_class_or_enum;
14880 bool friend_p;
14881 cp_token *token;
14882 int saved_pedantic;
14883
14884 /* Check for the `__extension__' keyword. */
14885 if (cp_parser_extension_opt (parser, &saved_pedantic))
14886 {
14887 /* Recurse. */
14888 cp_parser_member_declaration (parser);
14889 /* Restore the old value of the PEDANTIC flag. */
14890 pedantic = saved_pedantic;
14891
14892 return;
14893 }
14894
14895 /* Check for a template-declaration. */
14896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14897 {
14898 /* An explicit specialization here is an error condition, and we
14899 expect the specialization handler to detect and report this. */
14900 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14901 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14902 cp_parser_explicit_specialization (parser);
14903 else
14904 cp_parser_template_declaration (parser, /*member_p=*/true);
14905
14906 return;
14907 }
14908
14909 /* Check for a using-declaration. */
14910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14911 {
14912 /* Parse the using-declaration. */
14913 cp_parser_using_declaration (parser,
14914 /*access_declaration_p=*/false);
14915 return;
14916 }
14917
14918 /* Check for @defs. */
14919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14920 {
14921 tree ivar, member;
14922 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14923 ivar = ivar_chains;
14924 while (ivar)
14925 {
14926 member = ivar;
14927 ivar = TREE_CHAIN (member);
14928 TREE_CHAIN (member) = NULL_TREE;
14929 finish_member_declaration (member);
14930 }
14931 return;
14932 }
14933
14934 /* If the next token is `static_assert' we have a static assertion. */
14935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14936 {
14937 cp_parser_static_assert (parser, /*member_p=*/true);
14938 return;
14939 }
14940
14941 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14942 return;
14943
14944 /* Parse the decl-specifier-seq. */
14945 cp_parser_decl_specifier_seq (parser,
14946 CP_PARSER_FLAGS_OPTIONAL,
14947 &decl_specifiers,
14948 &declares_class_or_enum);
14949 prefix_attributes = decl_specifiers.attributes;
14950 decl_specifiers.attributes = NULL_TREE;
14951 /* Check for an invalid type-name. */
14952 if (!decl_specifiers.type
14953 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14954 return;
14955 /* If there is no declarator, then the decl-specifier-seq should
14956 specify a type. */
14957 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14958 {
14959 /* If there was no decl-specifier-seq, and the next token is a
14960 `;', then we have something like:
14961
14962 struct S { ; };
14963
14964 [class.mem]
14965
14966 Each member-declaration shall declare at least one member
14967 name of the class. */
14968 if (!decl_specifiers.any_specifiers_p)
14969 {
14970 cp_token *token = cp_lexer_peek_token (parser->lexer);
14971 if (pedantic && !token->in_system_header)
14972 pedwarn ("%Hextra %<;%>", &token->location);
14973 }
14974 else
14975 {
14976 tree type;
14977
14978 /* See if this declaration is a friend. */
14979 friend_p = cp_parser_friend_p (&decl_specifiers);
14980 /* If there were decl-specifiers, check to see if there was
14981 a class-declaration. */
14982 type = check_tag_decl (&decl_specifiers);
14983 /* Nested classes have already been added to the class, but
14984 a `friend' needs to be explicitly registered. */
14985 if (friend_p)
14986 {
14987 /* If the `friend' keyword was present, the friend must
14988 be introduced with a class-key. */
14989 if (!declares_class_or_enum)
14990 error ("a class-key must be used when declaring a friend");
14991 /* In this case:
14992
14993 template <typename T> struct A {
14994 friend struct A<T>::B;
14995 };
14996
14997 A<T>::B will be represented by a TYPENAME_TYPE, and
14998 therefore not recognized by check_tag_decl. */
14999 if (!type
15000 && decl_specifiers.type
15001 && TYPE_P (decl_specifiers.type))
15002 type = decl_specifiers.type;
15003 if (!type || !TYPE_P (type))
15004 error ("friend declaration does not name a class or "
15005 "function");
15006 else
15007 make_friend_class (current_class_type, type,
15008 /*complain=*/true);
15009 }
15010 /* If there is no TYPE, an error message will already have
15011 been issued. */
15012 else if (!type || type == error_mark_node)
15013 ;
15014 /* An anonymous aggregate has to be handled specially; such
15015 a declaration really declares a data member (with a
15016 particular type), as opposed to a nested class. */
15017 else if (ANON_AGGR_TYPE_P (type))
15018 {
15019 /* Remove constructors and such from TYPE, now that we
15020 know it is an anonymous aggregate. */
15021 fixup_anonymous_aggr (type);
15022 /* And make the corresponding data member. */
15023 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15024 /* Add it to the class. */
15025 finish_member_declaration (decl);
15026 }
15027 else
15028 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15029 }
15030 }
15031 else
15032 {
15033 /* See if these declarations will be friends. */
15034 friend_p = cp_parser_friend_p (&decl_specifiers);
15035
15036 /* Keep going until we hit the `;' at the end of the
15037 declaration. */
15038 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15039 {
15040 tree attributes = NULL_TREE;
15041 tree first_attribute;
15042
15043 /* Peek at the next token. */
15044 token = cp_lexer_peek_token (parser->lexer);
15045
15046 /* Check for a bitfield declaration. */
15047 if (token->type == CPP_COLON
15048 || (token->type == CPP_NAME
15049 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15050 == CPP_COLON))
15051 {
15052 tree identifier;
15053 tree width;
15054
15055 /* Get the name of the bitfield. Note that we cannot just
15056 check TOKEN here because it may have been invalidated by
15057 the call to cp_lexer_peek_nth_token above. */
15058 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15059 identifier = cp_parser_identifier (parser);
15060 else
15061 identifier = NULL_TREE;
15062
15063 /* Consume the `:' token. */
15064 cp_lexer_consume_token (parser->lexer);
15065 /* Get the width of the bitfield. */
15066 width
15067 = cp_parser_constant_expression (parser,
15068 /*allow_non_constant=*/false,
15069 NULL);
15070
15071 /* Look for attributes that apply to the bitfield. */
15072 attributes = cp_parser_attributes_opt (parser);
15073 /* Remember which attributes are prefix attributes and
15074 which are not. */
15075 first_attribute = attributes;
15076 /* Combine the attributes. */
15077 attributes = chainon (prefix_attributes, attributes);
15078
15079 /* Create the bitfield declaration. */
15080 decl = grokbitfield (identifier
15081 ? make_id_declarator (NULL_TREE,
15082 identifier,
15083 sfk_none)
15084 : NULL,
15085 &decl_specifiers,
15086 width);
15087 /* Apply the attributes. */
15088 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15089 }
15090 else
15091 {
15092 cp_declarator *declarator;
15093 tree initializer;
15094 tree asm_specification;
15095 int ctor_dtor_or_conv_p;
15096
15097 /* Parse the declarator. */
15098 declarator
15099 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15100 &ctor_dtor_or_conv_p,
15101 /*parenthesized_p=*/NULL,
15102 /*member_p=*/true);
15103
15104 /* If something went wrong parsing the declarator, make sure
15105 that we at least consume some tokens. */
15106 if (declarator == cp_error_declarator)
15107 {
15108 /* Skip to the end of the statement. */
15109 cp_parser_skip_to_end_of_statement (parser);
15110 /* If the next token is not a semicolon, that is
15111 probably because we just skipped over the body of
15112 a function. So, we consume a semicolon if
15113 present, but do not issue an error message if it
15114 is not present. */
15115 if (cp_lexer_next_token_is (parser->lexer,
15116 CPP_SEMICOLON))
15117 cp_lexer_consume_token (parser->lexer);
15118 return;
15119 }
15120
15121 if (declares_class_or_enum & 2)
15122 cp_parser_check_for_definition_in_return_type
15123 (declarator, decl_specifiers.type);
15124
15125 /* Look for an asm-specification. */
15126 asm_specification = cp_parser_asm_specification_opt (parser);
15127 /* Look for attributes that apply to the declaration. */
15128 attributes = cp_parser_attributes_opt (parser);
15129 /* Remember which attributes are prefix attributes and
15130 which are not. */
15131 first_attribute = attributes;
15132 /* Combine the attributes. */
15133 attributes = chainon (prefix_attributes, attributes);
15134
15135 /* If it's an `=', then we have a constant-initializer or a
15136 pure-specifier. It is not correct to parse the
15137 initializer before registering the member declaration
15138 since the member declaration should be in scope while
15139 its initializer is processed. However, the rest of the
15140 front end does not yet provide an interface that allows
15141 us to handle this correctly. */
15142 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15143 {
15144 /* In [class.mem]:
15145
15146 A pure-specifier shall be used only in the declaration of
15147 a virtual function.
15148
15149 A member-declarator can contain a constant-initializer
15150 only if it declares a static member of integral or
15151 enumeration type.
15152
15153 Therefore, if the DECLARATOR is for a function, we look
15154 for a pure-specifier; otherwise, we look for a
15155 constant-initializer. When we call `grokfield', it will
15156 perform more stringent semantics checks. */
15157 if (function_declarator_p (declarator))
15158 initializer = cp_parser_pure_specifier (parser);
15159 else
15160 /* Parse the initializer. */
15161 initializer = cp_parser_constant_initializer (parser);
15162 }
15163 /* Otherwise, there is no initializer. */
15164 else
15165 initializer = NULL_TREE;
15166
15167 /* See if we are probably looking at a function
15168 definition. We are certainly not looking at a
15169 member-declarator. Calling `grokfield' has
15170 side-effects, so we must not do it unless we are sure
15171 that we are looking at a member-declarator. */
15172 if (cp_parser_token_starts_function_definition_p
15173 (cp_lexer_peek_token (parser->lexer)))
15174 {
15175 /* The grammar does not allow a pure-specifier to be
15176 used when a member function is defined. (It is
15177 possible that this fact is an oversight in the
15178 standard, since a pure function may be defined
15179 outside of the class-specifier. */
15180 if (initializer)
15181 error ("pure-specifier on function-definition");
15182 decl = cp_parser_save_member_function_body (parser,
15183 &decl_specifiers,
15184 declarator,
15185 attributes);
15186 /* If the member was not a friend, declare it here. */
15187 if (!friend_p)
15188 finish_member_declaration (decl);
15189 /* Peek at the next token. */
15190 token = cp_lexer_peek_token (parser->lexer);
15191 /* If the next token is a semicolon, consume it. */
15192 if (token->type == CPP_SEMICOLON)
15193 cp_lexer_consume_token (parser->lexer);
15194 return;
15195 }
15196 else
15197 /* Create the declaration. */
15198 decl = grokfield (declarator, &decl_specifiers,
15199 initializer, /*init_const_expr_p=*/true,
15200 asm_specification,
15201 attributes);
15202 }
15203
15204 /* Reset PREFIX_ATTRIBUTES. */
15205 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15206 attributes = TREE_CHAIN (attributes);
15207 if (attributes)
15208 TREE_CHAIN (attributes) = NULL_TREE;
15209
15210 /* If there is any qualification still in effect, clear it
15211 now; we will be starting fresh with the next declarator. */
15212 parser->scope = NULL_TREE;
15213 parser->qualifying_scope = NULL_TREE;
15214 parser->object_scope = NULL_TREE;
15215 /* If it's a `,', then there are more declarators. */
15216 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15217 cp_lexer_consume_token (parser->lexer);
15218 /* If the next token isn't a `;', then we have a parse error. */
15219 else if (cp_lexer_next_token_is_not (parser->lexer,
15220 CPP_SEMICOLON))
15221 {
15222 cp_parser_error (parser, "expected %<;%>");
15223 /* Skip tokens until we find a `;'. */
15224 cp_parser_skip_to_end_of_statement (parser);
15225
15226 break;
15227 }
15228
15229 if (decl)
15230 {
15231 /* Add DECL to the list of members. */
15232 if (!friend_p)
15233 finish_member_declaration (decl);
15234
15235 if (TREE_CODE (decl) == FUNCTION_DECL)
15236 cp_parser_save_default_args (parser, decl);
15237 }
15238 }
15239 }
15240
15241 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15242 }
15243
15244 /* Parse a pure-specifier.
15245
15246 pure-specifier:
15247 = 0
15248
15249 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15250 Otherwise, ERROR_MARK_NODE is returned. */
15251
15252 static tree
15253 cp_parser_pure_specifier (cp_parser* parser)
15254 {
15255 cp_token *token;
15256
15257 /* Look for the `=' token. */
15258 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15259 return error_mark_node;
15260 /* Look for the `0' token. */
15261 token = cp_lexer_consume_token (parser->lexer);
15262 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15263 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15264 {
15265 cp_parser_error (parser,
15266 "invalid pure specifier (only `= 0' is allowed)");
15267 cp_parser_skip_to_end_of_statement (parser);
15268 return error_mark_node;
15269 }
15270 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15271 {
15272 error ("templates may not be %<virtual%>");
15273 return error_mark_node;
15274 }
15275
15276 return integer_zero_node;
15277 }
15278
15279 /* Parse a constant-initializer.
15280
15281 constant-initializer:
15282 = constant-expression
15283
15284 Returns a representation of the constant-expression. */
15285
15286 static tree
15287 cp_parser_constant_initializer (cp_parser* parser)
15288 {
15289 /* Look for the `=' token. */
15290 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15291 return error_mark_node;
15292
15293 /* It is invalid to write:
15294
15295 struct S { static const int i = { 7 }; };
15296
15297 */
15298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15299 {
15300 cp_parser_error (parser,
15301 "a brace-enclosed initializer is not allowed here");
15302 /* Consume the opening brace. */
15303 cp_lexer_consume_token (parser->lexer);
15304 /* Skip the initializer. */
15305 cp_parser_skip_to_closing_brace (parser);
15306 /* Look for the trailing `}'. */
15307 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15308
15309 return error_mark_node;
15310 }
15311
15312 return cp_parser_constant_expression (parser,
15313 /*allow_non_constant=*/false,
15314 NULL);
15315 }
15316
15317 /* Derived classes [gram.class.derived] */
15318
15319 /* Parse a base-clause.
15320
15321 base-clause:
15322 : base-specifier-list
15323
15324 base-specifier-list:
15325 base-specifier ... [opt]
15326 base-specifier-list , base-specifier ... [opt]
15327
15328 Returns a TREE_LIST representing the base-classes, in the order in
15329 which they were declared. The representation of each node is as
15330 described by cp_parser_base_specifier.
15331
15332 In the case that no bases are specified, this function will return
15333 NULL_TREE, not ERROR_MARK_NODE. */
15334
15335 static tree
15336 cp_parser_base_clause (cp_parser* parser)
15337 {
15338 tree bases = NULL_TREE;
15339
15340 /* Look for the `:' that begins the list. */
15341 cp_parser_require (parser, CPP_COLON, "`:'");
15342
15343 /* Scan the base-specifier-list. */
15344 while (true)
15345 {
15346 cp_token *token;
15347 tree base;
15348 bool pack_expansion_p = false;
15349
15350 /* Look for the base-specifier. */
15351 base = cp_parser_base_specifier (parser);
15352 /* Look for the (optional) ellipsis. */
15353 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15354 {
15355 /* Consume the `...'. */
15356 cp_lexer_consume_token (parser->lexer);
15357
15358 pack_expansion_p = true;
15359 }
15360
15361 /* Add BASE to the front of the list. */
15362 if (base != error_mark_node)
15363 {
15364 if (pack_expansion_p)
15365 /* Make this a pack expansion type. */
15366 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15367
15368
15369 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15370 {
15371 TREE_CHAIN (base) = bases;
15372 bases = base;
15373 }
15374 }
15375 /* Peek at the next token. */
15376 token = cp_lexer_peek_token (parser->lexer);
15377 /* If it's not a comma, then the list is complete. */
15378 if (token->type != CPP_COMMA)
15379 break;
15380 /* Consume the `,'. */
15381 cp_lexer_consume_token (parser->lexer);
15382 }
15383
15384 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15385 base class had a qualified name. However, the next name that
15386 appears is certainly not qualified. */
15387 parser->scope = NULL_TREE;
15388 parser->qualifying_scope = NULL_TREE;
15389 parser->object_scope = NULL_TREE;
15390
15391 return nreverse (bases);
15392 }
15393
15394 /* Parse a base-specifier.
15395
15396 base-specifier:
15397 :: [opt] nested-name-specifier [opt] class-name
15398 virtual access-specifier [opt] :: [opt] nested-name-specifier
15399 [opt] class-name
15400 access-specifier virtual [opt] :: [opt] nested-name-specifier
15401 [opt] class-name
15402
15403 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15404 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15405 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15406 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15407
15408 static tree
15409 cp_parser_base_specifier (cp_parser* parser)
15410 {
15411 cp_token *token;
15412 bool done = false;
15413 bool virtual_p = false;
15414 bool duplicate_virtual_error_issued_p = false;
15415 bool duplicate_access_error_issued_p = false;
15416 bool class_scope_p, template_p;
15417 tree access = access_default_node;
15418 tree type;
15419
15420 /* Process the optional `virtual' and `access-specifier'. */
15421 while (!done)
15422 {
15423 /* Peek at the next token. */
15424 token = cp_lexer_peek_token (parser->lexer);
15425 /* Process `virtual'. */
15426 switch (token->keyword)
15427 {
15428 case RID_VIRTUAL:
15429 /* If `virtual' appears more than once, issue an error. */
15430 if (virtual_p && !duplicate_virtual_error_issued_p)
15431 {
15432 cp_parser_error (parser,
15433 "%<virtual%> specified more than once in base-specified");
15434 duplicate_virtual_error_issued_p = true;
15435 }
15436
15437 virtual_p = true;
15438
15439 /* Consume the `virtual' token. */
15440 cp_lexer_consume_token (parser->lexer);
15441
15442 break;
15443
15444 case RID_PUBLIC:
15445 case RID_PROTECTED:
15446 case RID_PRIVATE:
15447 /* If more than one access specifier appears, issue an
15448 error. */
15449 if (access != access_default_node
15450 && !duplicate_access_error_issued_p)
15451 {
15452 cp_parser_error (parser,
15453 "more than one access specifier in base-specified");
15454 duplicate_access_error_issued_p = true;
15455 }
15456
15457 access = ridpointers[(int) token->keyword];
15458
15459 /* Consume the access-specifier. */
15460 cp_lexer_consume_token (parser->lexer);
15461
15462 break;
15463
15464 default:
15465 done = true;
15466 break;
15467 }
15468 }
15469 /* It is not uncommon to see programs mechanically, erroneously, use
15470 the 'typename' keyword to denote (dependent) qualified types
15471 as base classes. */
15472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15473 {
15474 if (!processing_template_decl)
15475 error ("keyword %<typename%> not allowed outside of templates");
15476 else
15477 error ("keyword %<typename%> not allowed in this context "
15478 "(the base class is implicitly a type)");
15479 cp_lexer_consume_token (parser->lexer);
15480 }
15481
15482 /* Look for the optional `::' operator. */
15483 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15484 /* Look for the nested-name-specifier. The simplest way to
15485 implement:
15486
15487 [temp.res]
15488
15489 The keyword `typename' is not permitted in a base-specifier or
15490 mem-initializer; in these contexts a qualified name that
15491 depends on a template-parameter is implicitly assumed to be a
15492 type name.
15493
15494 is to pretend that we have seen the `typename' keyword at this
15495 point. */
15496 cp_parser_nested_name_specifier_opt (parser,
15497 /*typename_keyword_p=*/true,
15498 /*check_dependency_p=*/true,
15499 typename_type,
15500 /*is_declaration=*/true);
15501 /* If the base class is given by a qualified name, assume that names
15502 we see are type names or templates, as appropriate. */
15503 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15504 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15505
15506 /* Finally, look for the class-name. */
15507 type = cp_parser_class_name (parser,
15508 class_scope_p,
15509 template_p,
15510 typename_type,
15511 /*check_dependency_p=*/true,
15512 /*class_head_p=*/false,
15513 /*is_declaration=*/true);
15514
15515 if (type == error_mark_node)
15516 return error_mark_node;
15517
15518 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15519 }
15520
15521 /* Exception handling [gram.exception] */
15522
15523 /* Parse an (optional) exception-specification.
15524
15525 exception-specification:
15526 throw ( type-id-list [opt] )
15527
15528 Returns a TREE_LIST representing the exception-specification. The
15529 TREE_VALUE of each node is a type. */
15530
15531 static tree
15532 cp_parser_exception_specification_opt (cp_parser* parser)
15533 {
15534 cp_token *token;
15535 tree type_id_list;
15536
15537 /* Peek at the next token. */
15538 token = cp_lexer_peek_token (parser->lexer);
15539 /* If it's not `throw', then there's no exception-specification. */
15540 if (!cp_parser_is_keyword (token, RID_THROW))
15541 return NULL_TREE;
15542
15543 /* Consume the `throw'. */
15544 cp_lexer_consume_token (parser->lexer);
15545
15546 /* Look for the `('. */
15547 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15548
15549 /* Peek at the next token. */
15550 token = cp_lexer_peek_token (parser->lexer);
15551 /* If it's not a `)', then there is a type-id-list. */
15552 if (token->type != CPP_CLOSE_PAREN)
15553 {
15554 const char *saved_message;
15555
15556 /* Types may not be defined in an exception-specification. */
15557 saved_message = parser->type_definition_forbidden_message;
15558 parser->type_definition_forbidden_message
15559 = "types may not be defined in an exception-specification";
15560 /* Parse the type-id-list. */
15561 type_id_list = cp_parser_type_id_list (parser);
15562 /* Restore the saved message. */
15563 parser->type_definition_forbidden_message = saved_message;
15564 }
15565 else
15566 type_id_list = empty_except_spec;
15567
15568 /* Look for the `)'. */
15569 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15570
15571 return type_id_list;
15572 }
15573
15574 /* Parse an (optional) type-id-list.
15575
15576 type-id-list:
15577 type-id ... [opt]
15578 type-id-list , type-id ... [opt]
15579
15580 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15581 in the order that the types were presented. */
15582
15583 static tree
15584 cp_parser_type_id_list (cp_parser* parser)
15585 {
15586 tree types = NULL_TREE;
15587
15588 while (true)
15589 {
15590 cp_token *token;
15591 tree type;
15592
15593 /* Get the next type-id. */
15594 type = cp_parser_type_id (parser);
15595 /* Parse the optional ellipsis. */
15596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15597 {
15598 /* Consume the `...'. */
15599 cp_lexer_consume_token (parser->lexer);
15600
15601 /* Turn the type into a pack expansion expression. */
15602 type = make_pack_expansion (type);
15603 }
15604 /* Add it to the list. */
15605 types = add_exception_specifier (types, type, /*complain=*/1);
15606 /* Peek at the next token. */
15607 token = cp_lexer_peek_token (parser->lexer);
15608 /* If it is not a `,', we are done. */
15609 if (token->type != CPP_COMMA)
15610 break;
15611 /* Consume the `,'. */
15612 cp_lexer_consume_token (parser->lexer);
15613 }
15614
15615 return nreverse (types);
15616 }
15617
15618 /* Parse a try-block.
15619
15620 try-block:
15621 try compound-statement handler-seq */
15622
15623 static tree
15624 cp_parser_try_block (cp_parser* parser)
15625 {
15626 tree try_block;
15627
15628 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15629 try_block = begin_try_block ();
15630 cp_parser_compound_statement (parser, NULL, true);
15631 finish_try_block (try_block);
15632 cp_parser_handler_seq (parser);
15633 finish_handler_sequence (try_block);
15634
15635 return try_block;
15636 }
15637
15638 /* Parse a function-try-block.
15639
15640 function-try-block:
15641 try ctor-initializer [opt] function-body handler-seq */
15642
15643 static bool
15644 cp_parser_function_try_block (cp_parser* parser)
15645 {
15646 tree compound_stmt;
15647 tree try_block;
15648 bool ctor_initializer_p;
15649
15650 /* Look for the `try' keyword. */
15651 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15652 return false;
15653 /* Let the rest of the front end know where we are. */
15654 try_block = begin_function_try_block (&compound_stmt);
15655 /* Parse the function-body. */
15656 ctor_initializer_p
15657 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15658 /* We're done with the `try' part. */
15659 finish_function_try_block (try_block);
15660 /* Parse the handlers. */
15661 cp_parser_handler_seq (parser);
15662 /* We're done with the handlers. */
15663 finish_function_handler_sequence (try_block, compound_stmt);
15664
15665 return ctor_initializer_p;
15666 }
15667
15668 /* Parse a handler-seq.
15669
15670 handler-seq:
15671 handler handler-seq [opt] */
15672
15673 static void
15674 cp_parser_handler_seq (cp_parser* parser)
15675 {
15676 while (true)
15677 {
15678 cp_token *token;
15679
15680 /* Parse the handler. */
15681 cp_parser_handler (parser);
15682 /* Peek at the next token. */
15683 token = cp_lexer_peek_token (parser->lexer);
15684 /* If it's not `catch' then there are no more handlers. */
15685 if (!cp_parser_is_keyword (token, RID_CATCH))
15686 break;
15687 }
15688 }
15689
15690 /* Parse a handler.
15691
15692 handler:
15693 catch ( exception-declaration ) compound-statement */
15694
15695 static void
15696 cp_parser_handler (cp_parser* parser)
15697 {
15698 tree handler;
15699 tree declaration;
15700
15701 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15702 handler = begin_handler ();
15703 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15704 declaration = cp_parser_exception_declaration (parser);
15705 finish_handler_parms (declaration, handler);
15706 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15707 cp_parser_compound_statement (parser, NULL, false);
15708 finish_handler (handler);
15709 }
15710
15711 /* Parse an exception-declaration.
15712
15713 exception-declaration:
15714 type-specifier-seq declarator
15715 type-specifier-seq abstract-declarator
15716 type-specifier-seq
15717 ...
15718
15719 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15720 ellipsis variant is used. */
15721
15722 static tree
15723 cp_parser_exception_declaration (cp_parser* parser)
15724 {
15725 cp_decl_specifier_seq type_specifiers;
15726 cp_declarator *declarator;
15727 const char *saved_message;
15728
15729 /* If it's an ellipsis, it's easy to handle. */
15730 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15731 {
15732 /* Consume the `...' token. */
15733 cp_lexer_consume_token (parser->lexer);
15734 return NULL_TREE;
15735 }
15736
15737 /* Types may not be defined in exception-declarations. */
15738 saved_message = parser->type_definition_forbidden_message;
15739 parser->type_definition_forbidden_message
15740 = "types may not be defined in exception-declarations";
15741
15742 /* Parse the type-specifier-seq. */
15743 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15744 &type_specifiers);
15745 /* If it's a `)', then there is no declarator. */
15746 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15747 declarator = NULL;
15748 else
15749 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15750 /*ctor_dtor_or_conv_p=*/NULL,
15751 /*parenthesized_p=*/NULL,
15752 /*member_p=*/false);
15753
15754 /* Restore the saved message. */
15755 parser->type_definition_forbidden_message = saved_message;
15756
15757 if (!type_specifiers.any_specifiers_p)
15758 return error_mark_node;
15759
15760 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15761 }
15762
15763 /* Parse a throw-expression.
15764
15765 throw-expression:
15766 throw assignment-expression [opt]
15767
15768 Returns a THROW_EXPR representing the throw-expression. */
15769
15770 static tree
15771 cp_parser_throw_expression (cp_parser* parser)
15772 {
15773 tree expression;
15774 cp_token* token;
15775
15776 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15777 token = cp_lexer_peek_token (parser->lexer);
15778 /* Figure out whether or not there is an assignment-expression
15779 following the "throw" keyword. */
15780 if (token->type == CPP_COMMA
15781 || token->type == CPP_SEMICOLON
15782 || token->type == CPP_CLOSE_PAREN
15783 || token->type == CPP_CLOSE_SQUARE
15784 || token->type == CPP_CLOSE_BRACE
15785 || token->type == CPP_COLON)
15786 expression = NULL_TREE;
15787 else
15788 expression = cp_parser_assignment_expression (parser,
15789 /*cast_p=*/false);
15790
15791 return build_throw (expression);
15792 }
15793
15794 /* GNU Extensions */
15795
15796 /* Parse an (optional) asm-specification.
15797
15798 asm-specification:
15799 asm ( string-literal )
15800
15801 If the asm-specification is present, returns a STRING_CST
15802 corresponding to the string-literal. Otherwise, returns
15803 NULL_TREE. */
15804
15805 static tree
15806 cp_parser_asm_specification_opt (cp_parser* parser)
15807 {
15808 cp_token *token;
15809 tree asm_specification;
15810
15811 /* Peek at the next token. */
15812 token = cp_lexer_peek_token (parser->lexer);
15813 /* If the next token isn't the `asm' keyword, then there's no
15814 asm-specification. */
15815 if (!cp_parser_is_keyword (token, RID_ASM))
15816 return NULL_TREE;
15817
15818 /* Consume the `asm' token. */
15819 cp_lexer_consume_token (parser->lexer);
15820 /* Look for the `('. */
15821 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15822
15823 /* Look for the string-literal. */
15824 asm_specification = cp_parser_string_literal (parser, false, false);
15825
15826 /* Look for the `)'. */
15827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15828
15829 return asm_specification;
15830 }
15831
15832 /* Parse an asm-operand-list.
15833
15834 asm-operand-list:
15835 asm-operand
15836 asm-operand-list , asm-operand
15837
15838 asm-operand:
15839 string-literal ( expression )
15840 [ string-literal ] string-literal ( expression )
15841
15842 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15843 each node is the expression. The TREE_PURPOSE is itself a
15844 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15845 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15846 is a STRING_CST for the string literal before the parenthesis. Returns
15847 ERROR_MARK_NODE if any of the operands are invalid. */
15848
15849 static tree
15850 cp_parser_asm_operand_list (cp_parser* parser)
15851 {
15852 tree asm_operands = NULL_TREE;
15853 bool invalid_operands = false;
15854
15855 while (true)
15856 {
15857 tree string_literal;
15858 tree expression;
15859 tree name;
15860
15861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15862 {
15863 /* Consume the `[' token. */
15864 cp_lexer_consume_token (parser->lexer);
15865 /* Read the operand name. */
15866 name = cp_parser_identifier (parser);
15867 if (name != error_mark_node)
15868 name = build_string (IDENTIFIER_LENGTH (name),
15869 IDENTIFIER_POINTER (name));
15870 /* Look for the closing `]'. */
15871 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15872 }
15873 else
15874 name = NULL_TREE;
15875 /* Look for the string-literal. */
15876 string_literal = cp_parser_string_literal (parser, false, false);
15877
15878 /* Look for the `('. */
15879 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15880 /* Parse the expression. */
15881 expression = cp_parser_expression (parser, /*cast_p=*/false);
15882 /* Look for the `)'. */
15883 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15884
15885 if (name == error_mark_node
15886 || string_literal == error_mark_node
15887 || expression == error_mark_node)
15888 invalid_operands = true;
15889
15890 /* Add this operand to the list. */
15891 asm_operands = tree_cons (build_tree_list (name, string_literal),
15892 expression,
15893 asm_operands);
15894 /* If the next token is not a `,', there are no more
15895 operands. */
15896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15897 break;
15898 /* Consume the `,'. */
15899 cp_lexer_consume_token (parser->lexer);
15900 }
15901
15902 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15903 }
15904
15905 /* Parse an asm-clobber-list.
15906
15907 asm-clobber-list:
15908 string-literal
15909 asm-clobber-list , string-literal
15910
15911 Returns a TREE_LIST, indicating the clobbers in the order that they
15912 appeared. The TREE_VALUE of each node is a STRING_CST. */
15913
15914 static tree
15915 cp_parser_asm_clobber_list (cp_parser* parser)
15916 {
15917 tree clobbers = NULL_TREE;
15918
15919 while (true)
15920 {
15921 tree string_literal;
15922
15923 /* Look for the string literal. */
15924 string_literal = cp_parser_string_literal (parser, false, false);
15925 /* Add it to the list. */
15926 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15927 /* If the next token is not a `,', then the list is
15928 complete. */
15929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15930 break;
15931 /* Consume the `,' token. */
15932 cp_lexer_consume_token (parser->lexer);
15933 }
15934
15935 return clobbers;
15936 }
15937
15938 /* Parse an (optional) series of attributes.
15939
15940 attributes:
15941 attributes attribute
15942
15943 attribute:
15944 __attribute__ (( attribute-list [opt] ))
15945
15946 The return value is as for cp_parser_attribute_list. */
15947
15948 static tree
15949 cp_parser_attributes_opt (cp_parser* parser)
15950 {
15951 tree attributes = NULL_TREE;
15952
15953 while (true)
15954 {
15955 cp_token *token;
15956 tree attribute_list;
15957
15958 /* Peek at the next token. */
15959 token = cp_lexer_peek_token (parser->lexer);
15960 /* If it's not `__attribute__', then we're done. */
15961 if (token->keyword != RID_ATTRIBUTE)
15962 break;
15963
15964 /* Consume the `__attribute__' keyword. */
15965 cp_lexer_consume_token (parser->lexer);
15966 /* Look for the two `(' tokens. */
15967 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15968 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15969
15970 /* Peek at the next token. */
15971 token = cp_lexer_peek_token (parser->lexer);
15972 if (token->type != CPP_CLOSE_PAREN)
15973 /* Parse the attribute-list. */
15974 attribute_list = cp_parser_attribute_list (parser);
15975 else
15976 /* If the next token is a `)', then there is no attribute
15977 list. */
15978 attribute_list = NULL;
15979
15980 /* Look for the two `)' tokens. */
15981 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15982 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15983
15984 /* Add these new attributes to the list. */
15985 attributes = chainon (attributes, attribute_list);
15986 }
15987
15988 return attributes;
15989 }
15990
15991 /* Parse an attribute-list.
15992
15993 attribute-list:
15994 attribute
15995 attribute-list , attribute
15996
15997 attribute:
15998 identifier
15999 identifier ( identifier )
16000 identifier ( identifier , expression-list )
16001 identifier ( expression-list )
16002
16003 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16004 to an attribute. The TREE_PURPOSE of each node is the identifier
16005 indicating which attribute is in use. The TREE_VALUE represents
16006 the arguments, if any. */
16007
16008 static tree
16009 cp_parser_attribute_list (cp_parser* parser)
16010 {
16011 tree attribute_list = NULL_TREE;
16012 bool save_translate_strings_p = parser->translate_strings_p;
16013
16014 parser->translate_strings_p = false;
16015 while (true)
16016 {
16017 cp_token *token;
16018 tree identifier;
16019 tree attribute;
16020
16021 /* Look for the identifier. We also allow keywords here; for
16022 example `__attribute__ ((const))' is legal. */
16023 token = cp_lexer_peek_token (parser->lexer);
16024 if (token->type == CPP_NAME
16025 || token->type == CPP_KEYWORD)
16026 {
16027 tree arguments = NULL_TREE;
16028
16029 /* Consume the token. */
16030 token = cp_lexer_consume_token (parser->lexer);
16031
16032 /* Save away the identifier that indicates which attribute
16033 this is. */
16034 identifier = token->u.value;
16035 attribute = build_tree_list (identifier, NULL_TREE);
16036
16037 /* Peek at the next token. */
16038 token = cp_lexer_peek_token (parser->lexer);
16039 /* If it's an `(', then parse the attribute arguments. */
16040 if (token->type == CPP_OPEN_PAREN)
16041 {
16042 arguments = cp_parser_parenthesized_expression_list
16043 (parser, true, /*cast_p=*/false,
16044 /*allow_expansion_p=*/false,
16045 /*non_constant_p=*/NULL);
16046 /* Save the arguments away. */
16047 TREE_VALUE (attribute) = arguments;
16048 }
16049
16050 if (arguments != error_mark_node)
16051 {
16052 /* Add this attribute to the list. */
16053 TREE_CHAIN (attribute) = attribute_list;
16054 attribute_list = attribute;
16055 }
16056
16057 token = cp_lexer_peek_token (parser->lexer);
16058 }
16059 /* Now, look for more attributes. If the next token isn't a
16060 `,', we're done. */
16061 if (token->type != CPP_COMMA)
16062 break;
16063
16064 /* Consume the comma and keep going. */
16065 cp_lexer_consume_token (parser->lexer);
16066 }
16067 parser->translate_strings_p = save_translate_strings_p;
16068
16069 /* We built up the list in reverse order. */
16070 return nreverse (attribute_list);
16071 }
16072
16073 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16074 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16075 current value of the PEDANTIC flag, regardless of whether or not
16076 the `__extension__' keyword is present. The caller is responsible
16077 for restoring the value of the PEDANTIC flag. */
16078
16079 static bool
16080 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16081 {
16082 /* Save the old value of the PEDANTIC flag. */
16083 *saved_pedantic = pedantic;
16084
16085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16086 {
16087 /* Consume the `__extension__' token. */
16088 cp_lexer_consume_token (parser->lexer);
16089 /* We're not being pedantic while the `__extension__' keyword is
16090 in effect. */
16091 pedantic = 0;
16092
16093 return true;
16094 }
16095
16096 return false;
16097 }
16098
16099 /* Parse a label declaration.
16100
16101 label-declaration:
16102 __label__ label-declarator-seq ;
16103
16104 label-declarator-seq:
16105 identifier , label-declarator-seq
16106 identifier */
16107
16108 static void
16109 cp_parser_label_declaration (cp_parser* parser)
16110 {
16111 /* Look for the `__label__' keyword. */
16112 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16113
16114 while (true)
16115 {
16116 tree identifier;
16117
16118 /* Look for an identifier. */
16119 identifier = cp_parser_identifier (parser);
16120 /* If we failed, stop. */
16121 if (identifier == error_mark_node)
16122 break;
16123 /* Declare it as a label. */
16124 finish_label_decl (identifier);
16125 /* If the next token is a `;', stop. */
16126 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16127 break;
16128 /* Look for the `,' separating the label declarations. */
16129 cp_parser_require (parser, CPP_COMMA, "`,'");
16130 }
16131
16132 /* Look for the final `;'. */
16133 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16134 }
16135
16136 /* Support Functions */
16137
16138 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16139 NAME should have one of the representations used for an
16140 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16141 is returned. If PARSER->SCOPE is a dependent type, then a
16142 SCOPE_REF is returned.
16143
16144 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16145 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16146 was formed. Abstractly, such entities should not be passed to this
16147 function, because they do not need to be looked up, but it is
16148 simpler to check for this special case here, rather than at the
16149 call-sites.
16150
16151 In cases not explicitly covered above, this function returns a
16152 DECL, OVERLOAD, or baselink representing the result of the lookup.
16153 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16154 is returned.
16155
16156 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16157 (e.g., "struct") that was used. In that case bindings that do not
16158 refer to types are ignored.
16159
16160 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16161 ignored.
16162
16163 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16164 are ignored.
16165
16166 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16167 types.
16168
16169 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16170 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16171 NULL_TREE otherwise. */
16172
16173 static tree
16174 cp_parser_lookup_name (cp_parser *parser, tree name,
16175 enum tag_types tag_type,
16176 bool is_template,
16177 bool is_namespace,
16178 bool check_dependency,
16179 tree *ambiguous_decls)
16180 {
16181 int flags = 0;
16182 tree decl;
16183 tree object_type = parser->context->object_type;
16184
16185 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16186 flags |= LOOKUP_COMPLAIN;
16187
16188 /* Assume that the lookup will be unambiguous. */
16189 if (ambiguous_decls)
16190 *ambiguous_decls = NULL_TREE;
16191
16192 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16193 no longer valid. Note that if we are parsing tentatively, and
16194 the parse fails, OBJECT_TYPE will be automatically restored. */
16195 parser->context->object_type = NULL_TREE;
16196
16197 if (name == error_mark_node)
16198 return error_mark_node;
16199
16200 /* A template-id has already been resolved; there is no lookup to
16201 do. */
16202 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16203 return name;
16204 if (BASELINK_P (name))
16205 {
16206 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16207 == TEMPLATE_ID_EXPR);
16208 return name;
16209 }
16210
16211 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16212 it should already have been checked to make sure that the name
16213 used matches the type being destroyed. */
16214 if (TREE_CODE (name) == BIT_NOT_EXPR)
16215 {
16216 tree type;
16217
16218 /* Figure out to which type this destructor applies. */
16219 if (parser->scope)
16220 type = parser->scope;
16221 else if (object_type)
16222 type = object_type;
16223 else
16224 type = current_class_type;
16225 /* If that's not a class type, there is no destructor. */
16226 if (!type || !CLASS_TYPE_P (type))
16227 return error_mark_node;
16228 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16229 lazily_declare_fn (sfk_destructor, type);
16230 if (!CLASSTYPE_DESTRUCTORS (type))
16231 return error_mark_node;
16232 /* If it was a class type, return the destructor. */
16233 return CLASSTYPE_DESTRUCTORS (type);
16234 }
16235
16236 /* By this point, the NAME should be an ordinary identifier. If
16237 the id-expression was a qualified name, the qualifying scope is
16238 stored in PARSER->SCOPE at this point. */
16239 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16240
16241 /* Perform the lookup. */
16242 if (parser->scope)
16243 {
16244 bool dependent_p;
16245
16246 if (parser->scope == error_mark_node)
16247 return error_mark_node;
16248
16249 /* If the SCOPE is dependent, the lookup must be deferred until
16250 the template is instantiated -- unless we are explicitly
16251 looking up names in uninstantiated templates. Even then, we
16252 cannot look up the name if the scope is not a class type; it
16253 might, for example, be a template type parameter. */
16254 dependent_p = (TYPE_P (parser->scope)
16255 && !(parser->in_declarator_p
16256 && currently_open_class (parser->scope))
16257 && dependent_type_p (parser->scope));
16258 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16259 && dependent_p)
16260 {
16261 if (tag_type)
16262 {
16263 tree type;
16264
16265 /* The resolution to Core Issue 180 says that `struct
16266 A::B' should be considered a type-name, even if `A'
16267 is dependent. */
16268 type = make_typename_type (parser->scope, name, tag_type,
16269 /*complain=*/tf_error);
16270 decl = TYPE_NAME (type);
16271 }
16272 else if (is_template
16273 && (cp_parser_next_token_ends_template_argument_p (parser)
16274 || cp_lexer_next_token_is (parser->lexer,
16275 CPP_CLOSE_PAREN)))
16276 decl = make_unbound_class_template (parser->scope,
16277 name, NULL_TREE,
16278 /*complain=*/tf_error);
16279 else
16280 decl = build_qualified_name (/*type=*/NULL_TREE,
16281 parser->scope, name,
16282 is_template);
16283 }
16284 else
16285 {
16286 tree pushed_scope = NULL_TREE;
16287
16288 /* If PARSER->SCOPE is a dependent type, then it must be a
16289 class type, and we must not be checking dependencies;
16290 otherwise, we would have processed this lookup above. So
16291 that PARSER->SCOPE is not considered a dependent base by
16292 lookup_member, we must enter the scope here. */
16293 if (dependent_p)
16294 pushed_scope = push_scope (parser->scope);
16295 /* If the PARSER->SCOPE is a template specialization, it
16296 may be instantiated during name lookup. In that case,
16297 errors may be issued. Even if we rollback the current
16298 tentative parse, those errors are valid. */
16299 decl = lookup_qualified_name (parser->scope, name,
16300 tag_type != none_type,
16301 /*complain=*/true);
16302 if (pushed_scope)
16303 pop_scope (pushed_scope);
16304 }
16305 parser->qualifying_scope = parser->scope;
16306 parser->object_scope = NULL_TREE;
16307 }
16308 else if (object_type)
16309 {
16310 tree object_decl = NULL_TREE;
16311 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16312 OBJECT_TYPE is not a class. */
16313 if (CLASS_TYPE_P (object_type))
16314 /* If the OBJECT_TYPE is a template specialization, it may
16315 be instantiated during name lookup. In that case, errors
16316 may be issued. Even if we rollback the current tentative
16317 parse, those errors are valid. */
16318 object_decl = lookup_member (object_type,
16319 name,
16320 /*protect=*/0,
16321 tag_type != none_type);
16322 /* Look it up in the enclosing context, too. */
16323 decl = lookup_name_real (name, tag_type != none_type,
16324 /*nonclass=*/0,
16325 /*block_p=*/true, is_namespace, flags);
16326 parser->object_scope = object_type;
16327 parser->qualifying_scope = NULL_TREE;
16328 if (object_decl)
16329 decl = object_decl;
16330 }
16331 else
16332 {
16333 decl = lookup_name_real (name, tag_type != none_type,
16334 /*nonclass=*/0,
16335 /*block_p=*/true, is_namespace, flags);
16336 parser->qualifying_scope = NULL_TREE;
16337 parser->object_scope = NULL_TREE;
16338 }
16339
16340 /* If the lookup failed, let our caller know. */
16341 if (!decl || decl == error_mark_node)
16342 return error_mark_node;
16343
16344 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16345 if (TREE_CODE (decl) == TREE_LIST)
16346 {
16347 if (ambiguous_decls)
16348 *ambiguous_decls = decl;
16349 /* The error message we have to print is too complicated for
16350 cp_parser_error, so we incorporate its actions directly. */
16351 if (!cp_parser_simulate_error (parser))
16352 {
16353 error ("reference to %qD is ambiguous", name);
16354 print_candidates (decl);
16355 }
16356 return error_mark_node;
16357 }
16358
16359 gcc_assert (DECL_P (decl)
16360 || TREE_CODE (decl) == OVERLOAD
16361 || TREE_CODE (decl) == SCOPE_REF
16362 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16363 || BASELINK_P (decl));
16364
16365 /* If we have resolved the name of a member declaration, check to
16366 see if the declaration is accessible. When the name resolves to
16367 set of overloaded functions, accessibility is checked when
16368 overload resolution is done.
16369
16370 During an explicit instantiation, access is not checked at all,
16371 as per [temp.explicit]. */
16372 if (DECL_P (decl))
16373 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16374
16375 return decl;
16376 }
16377
16378 /* Like cp_parser_lookup_name, but for use in the typical case where
16379 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16380 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16381
16382 static tree
16383 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16384 {
16385 return cp_parser_lookup_name (parser, name,
16386 none_type,
16387 /*is_template=*/false,
16388 /*is_namespace=*/false,
16389 /*check_dependency=*/true,
16390 /*ambiguous_decls=*/NULL);
16391 }
16392
16393 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16394 the current context, return the TYPE_DECL. If TAG_NAME_P is
16395 true, the DECL indicates the class being defined in a class-head,
16396 or declared in an elaborated-type-specifier.
16397
16398 Otherwise, return DECL. */
16399
16400 static tree
16401 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16402 {
16403 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16404 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16405
16406 struct A {
16407 template <typename T> struct B;
16408 };
16409
16410 template <typename T> struct A::B {};
16411
16412 Similarly, in an elaborated-type-specifier:
16413
16414 namespace N { struct X{}; }
16415
16416 struct A {
16417 template <typename T> friend struct N::X;
16418 };
16419
16420 However, if the DECL refers to a class type, and we are in
16421 the scope of the class, then the name lookup automatically
16422 finds the TYPE_DECL created by build_self_reference rather
16423 than a TEMPLATE_DECL. For example, in:
16424
16425 template <class T> struct S {
16426 S s;
16427 };
16428
16429 there is no need to handle such case. */
16430
16431 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16432 return DECL_TEMPLATE_RESULT (decl);
16433
16434 return decl;
16435 }
16436
16437 /* If too many, or too few, template-parameter lists apply to the
16438 declarator, issue an error message. Returns TRUE if all went well,
16439 and FALSE otherwise. */
16440
16441 static bool
16442 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16443 cp_declarator *declarator)
16444 {
16445 unsigned num_templates;
16446
16447 /* We haven't seen any classes that involve template parameters yet. */
16448 num_templates = 0;
16449
16450 switch (declarator->kind)
16451 {
16452 case cdk_id:
16453 if (declarator->u.id.qualifying_scope)
16454 {
16455 tree scope;
16456 tree member;
16457
16458 scope = declarator->u.id.qualifying_scope;
16459 member = declarator->u.id.unqualified_name;
16460
16461 while (scope && CLASS_TYPE_P (scope))
16462 {
16463 /* You're supposed to have one `template <...>'
16464 for every template class, but you don't need one
16465 for a full specialization. For example:
16466
16467 template <class T> struct S{};
16468 template <> struct S<int> { void f(); };
16469 void S<int>::f () {}
16470
16471 is correct; there shouldn't be a `template <>' for
16472 the definition of `S<int>::f'. */
16473 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16474 /* If SCOPE does not have template information of any
16475 kind, then it is not a template, nor is it nested
16476 within a template. */
16477 break;
16478 if (explicit_class_specialization_p (scope))
16479 break;
16480 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16481 ++num_templates;
16482
16483 scope = TYPE_CONTEXT (scope);
16484 }
16485 }
16486 else if (TREE_CODE (declarator->u.id.unqualified_name)
16487 == TEMPLATE_ID_EXPR)
16488 /* If the DECLARATOR has the form `X<y>' then it uses one
16489 additional level of template parameters. */
16490 ++num_templates;
16491
16492 return cp_parser_check_template_parameters (parser,
16493 num_templates);
16494
16495 case cdk_function:
16496 case cdk_array:
16497 case cdk_pointer:
16498 case cdk_reference:
16499 case cdk_ptrmem:
16500 return (cp_parser_check_declarator_template_parameters
16501 (parser, declarator->declarator));
16502
16503 case cdk_error:
16504 return true;
16505
16506 default:
16507 gcc_unreachable ();
16508 }
16509 return false;
16510 }
16511
16512 /* NUM_TEMPLATES were used in the current declaration. If that is
16513 invalid, return FALSE and issue an error messages. Otherwise,
16514 return TRUE. */
16515
16516 static bool
16517 cp_parser_check_template_parameters (cp_parser* parser,
16518 unsigned num_templates)
16519 {
16520 /* If there are more template classes than parameter lists, we have
16521 something like:
16522
16523 template <class T> void S<T>::R<T>::f (); */
16524 if (parser->num_template_parameter_lists < num_templates)
16525 {
16526 error ("too few template-parameter-lists");
16527 return false;
16528 }
16529 /* If there are the same number of template classes and parameter
16530 lists, that's OK. */
16531 if (parser->num_template_parameter_lists == num_templates)
16532 return true;
16533 /* If there are more, but only one more, then we are referring to a
16534 member template. That's OK too. */
16535 if (parser->num_template_parameter_lists == num_templates + 1)
16536 return true;
16537 /* Otherwise, there are too many template parameter lists. We have
16538 something like:
16539
16540 template <class T> template <class U> void S::f(); */
16541 error ("too many template-parameter-lists");
16542 return false;
16543 }
16544
16545 /* Parse an optional `::' token indicating that the following name is
16546 from the global namespace. If so, PARSER->SCOPE is set to the
16547 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16548 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16549 Returns the new value of PARSER->SCOPE, if the `::' token is
16550 present, and NULL_TREE otherwise. */
16551
16552 static tree
16553 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16554 {
16555 cp_token *token;
16556
16557 /* Peek at the next token. */
16558 token = cp_lexer_peek_token (parser->lexer);
16559 /* If we're looking at a `::' token then we're starting from the
16560 global namespace, not our current location. */
16561 if (token->type == CPP_SCOPE)
16562 {
16563 /* Consume the `::' token. */
16564 cp_lexer_consume_token (parser->lexer);
16565 /* Set the SCOPE so that we know where to start the lookup. */
16566 parser->scope = global_namespace;
16567 parser->qualifying_scope = global_namespace;
16568 parser->object_scope = NULL_TREE;
16569
16570 return parser->scope;
16571 }
16572 else if (!current_scope_valid_p)
16573 {
16574 parser->scope = NULL_TREE;
16575 parser->qualifying_scope = NULL_TREE;
16576 parser->object_scope = NULL_TREE;
16577 }
16578
16579 return NULL_TREE;
16580 }
16581
16582 /* Returns TRUE if the upcoming token sequence is the start of a
16583 constructor declarator. If FRIEND_P is true, the declarator is
16584 preceded by the `friend' specifier. */
16585
16586 static bool
16587 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16588 {
16589 bool constructor_p;
16590 tree type_decl = NULL_TREE;
16591 bool nested_name_p;
16592 cp_token *next_token;
16593
16594 /* The common case is that this is not a constructor declarator, so
16595 try to avoid doing lots of work if at all possible. It's not
16596 valid declare a constructor at function scope. */
16597 if (parser->in_function_body)
16598 return false;
16599 /* And only certain tokens can begin a constructor declarator. */
16600 next_token = cp_lexer_peek_token (parser->lexer);
16601 if (next_token->type != CPP_NAME
16602 && next_token->type != CPP_SCOPE
16603 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16604 && next_token->type != CPP_TEMPLATE_ID)
16605 return false;
16606
16607 /* Parse tentatively; we are going to roll back all of the tokens
16608 consumed here. */
16609 cp_parser_parse_tentatively (parser);
16610 /* Assume that we are looking at a constructor declarator. */
16611 constructor_p = true;
16612
16613 /* Look for the optional `::' operator. */
16614 cp_parser_global_scope_opt (parser,
16615 /*current_scope_valid_p=*/false);
16616 /* Look for the nested-name-specifier. */
16617 nested_name_p
16618 = (cp_parser_nested_name_specifier_opt (parser,
16619 /*typename_keyword_p=*/false,
16620 /*check_dependency_p=*/false,
16621 /*type_p=*/false,
16622 /*is_declaration=*/false)
16623 != NULL_TREE);
16624 /* Outside of a class-specifier, there must be a
16625 nested-name-specifier. */
16626 if (!nested_name_p &&
16627 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16628 || friend_p))
16629 constructor_p = false;
16630 /* If we still think that this might be a constructor-declarator,
16631 look for a class-name. */
16632 if (constructor_p)
16633 {
16634 /* If we have:
16635
16636 template <typename T> struct S { S(); };
16637 template <typename T> S<T>::S ();
16638
16639 we must recognize that the nested `S' names a class.
16640 Similarly, for:
16641
16642 template <typename T> S<T>::S<T> ();
16643
16644 we must recognize that the nested `S' names a template. */
16645 type_decl = cp_parser_class_name (parser,
16646 /*typename_keyword_p=*/false,
16647 /*template_keyword_p=*/false,
16648 none_type,
16649 /*check_dependency_p=*/false,
16650 /*class_head_p=*/false,
16651 /*is_declaration=*/false);
16652 /* If there was no class-name, then this is not a constructor. */
16653 constructor_p = !cp_parser_error_occurred (parser);
16654 }
16655
16656 /* If we're still considering a constructor, we have to see a `(',
16657 to begin the parameter-declaration-clause, followed by either a
16658 `)', an `...', or a decl-specifier. We need to check for a
16659 type-specifier to avoid being fooled into thinking that:
16660
16661 S::S (f) (int);
16662
16663 is a constructor. (It is actually a function named `f' that
16664 takes one parameter (of type `int') and returns a value of type
16665 `S::S'. */
16666 if (constructor_p
16667 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16668 {
16669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16670 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16671 /* A parameter declaration begins with a decl-specifier,
16672 which is either the "attribute" keyword, a storage class
16673 specifier, or (usually) a type-specifier. */
16674 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16675 {
16676 tree type;
16677 tree pushed_scope = NULL_TREE;
16678 unsigned saved_num_template_parameter_lists;
16679
16680 /* Names appearing in the type-specifier should be looked up
16681 in the scope of the class. */
16682 if (current_class_type)
16683 type = NULL_TREE;
16684 else
16685 {
16686 type = TREE_TYPE (type_decl);
16687 if (TREE_CODE (type) == TYPENAME_TYPE)
16688 {
16689 type = resolve_typename_type (type,
16690 /*only_current_p=*/false);
16691 if (TREE_CODE (type) == TYPENAME_TYPE)
16692 {
16693 cp_parser_abort_tentative_parse (parser);
16694 return false;
16695 }
16696 }
16697 pushed_scope = push_scope (type);
16698 }
16699
16700 /* Inside the constructor parameter list, surrounding
16701 template-parameter-lists do not apply. */
16702 saved_num_template_parameter_lists
16703 = parser->num_template_parameter_lists;
16704 parser->num_template_parameter_lists = 0;
16705
16706 /* Look for the type-specifier. */
16707 cp_parser_type_specifier (parser,
16708 CP_PARSER_FLAGS_NONE,
16709 /*decl_specs=*/NULL,
16710 /*is_declarator=*/true,
16711 /*declares_class_or_enum=*/NULL,
16712 /*is_cv_qualifier=*/NULL);
16713
16714 parser->num_template_parameter_lists
16715 = saved_num_template_parameter_lists;
16716
16717 /* Leave the scope of the class. */
16718 if (pushed_scope)
16719 pop_scope (pushed_scope);
16720
16721 constructor_p = !cp_parser_error_occurred (parser);
16722 }
16723 }
16724 else
16725 constructor_p = false;
16726 /* We did not really want to consume any tokens. */
16727 cp_parser_abort_tentative_parse (parser);
16728
16729 return constructor_p;
16730 }
16731
16732 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16733 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16734 they must be performed once we are in the scope of the function.
16735
16736 Returns the function defined. */
16737
16738 static tree
16739 cp_parser_function_definition_from_specifiers_and_declarator
16740 (cp_parser* parser,
16741 cp_decl_specifier_seq *decl_specifiers,
16742 tree attributes,
16743 const cp_declarator *declarator)
16744 {
16745 tree fn;
16746 bool success_p;
16747
16748 /* Begin the function-definition. */
16749 success_p = start_function (decl_specifiers, declarator, attributes);
16750
16751 /* The things we're about to see are not directly qualified by any
16752 template headers we've seen thus far. */
16753 reset_specialization ();
16754
16755 /* If there were names looked up in the decl-specifier-seq that we
16756 did not check, check them now. We must wait until we are in the
16757 scope of the function to perform the checks, since the function
16758 might be a friend. */
16759 perform_deferred_access_checks ();
16760
16761 if (!success_p)
16762 {
16763 /* Skip the entire function. */
16764 cp_parser_skip_to_end_of_block_or_statement (parser);
16765 fn = error_mark_node;
16766 }
16767 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16768 {
16769 /* Seen already, skip it. An error message has already been output. */
16770 cp_parser_skip_to_end_of_block_or_statement (parser);
16771 fn = current_function_decl;
16772 current_function_decl = NULL_TREE;
16773 /* If this is a function from a class, pop the nested class. */
16774 if (current_class_name)
16775 pop_nested_class ();
16776 }
16777 else
16778 fn = cp_parser_function_definition_after_declarator (parser,
16779 /*inline_p=*/false);
16780
16781 return fn;
16782 }
16783
16784 /* Parse the part of a function-definition that follows the
16785 declarator. INLINE_P is TRUE iff this function is an inline
16786 function defined with a class-specifier.
16787
16788 Returns the function defined. */
16789
16790 static tree
16791 cp_parser_function_definition_after_declarator (cp_parser* parser,
16792 bool inline_p)
16793 {
16794 tree fn;
16795 bool ctor_initializer_p = false;
16796 bool saved_in_unbraced_linkage_specification_p;
16797 bool saved_in_function_body;
16798 unsigned saved_num_template_parameter_lists;
16799
16800 saved_in_function_body = parser->in_function_body;
16801 parser->in_function_body = true;
16802 /* If the next token is `return', then the code may be trying to
16803 make use of the "named return value" extension that G++ used to
16804 support. */
16805 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16806 {
16807 /* Consume the `return' keyword. */
16808 cp_lexer_consume_token (parser->lexer);
16809 /* Look for the identifier that indicates what value is to be
16810 returned. */
16811 cp_parser_identifier (parser);
16812 /* Issue an error message. */
16813 error ("named return values are no longer supported");
16814 /* Skip tokens until we reach the start of the function body. */
16815 while (true)
16816 {
16817 cp_token *token = cp_lexer_peek_token (parser->lexer);
16818 if (token->type == CPP_OPEN_BRACE
16819 || token->type == CPP_EOF
16820 || token->type == CPP_PRAGMA_EOL)
16821 break;
16822 cp_lexer_consume_token (parser->lexer);
16823 }
16824 }
16825 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16826 anything declared inside `f'. */
16827 saved_in_unbraced_linkage_specification_p
16828 = parser->in_unbraced_linkage_specification_p;
16829 parser->in_unbraced_linkage_specification_p = false;
16830 /* Inside the function, surrounding template-parameter-lists do not
16831 apply. */
16832 saved_num_template_parameter_lists
16833 = parser->num_template_parameter_lists;
16834 parser->num_template_parameter_lists = 0;
16835 /* If the next token is `try', then we are looking at a
16836 function-try-block. */
16837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16838 ctor_initializer_p = cp_parser_function_try_block (parser);
16839 /* A function-try-block includes the function-body, so we only do
16840 this next part if we're not processing a function-try-block. */
16841 else
16842 ctor_initializer_p
16843 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16844
16845 /* Finish the function. */
16846 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16847 (inline_p ? 2 : 0));
16848 /* Generate code for it, if necessary. */
16849 expand_or_defer_fn (fn);
16850 /* Restore the saved values. */
16851 parser->in_unbraced_linkage_specification_p
16852 = saved_in_unbraced_linkage_specification_p;
16853 parser->num_template_parameter_lists
16854 = saved_num_template_parameter_lists;
16855 parser->in_function_body = saved_in_function_body;
16856
16857 return fn;
16858 }
16859
16860 /* Parse a template-declaration, assuming that the `export' (and
16861 `extern') keywords, if present, has already been scanned. MEMBER_P
16862 is as for cp_parser_template_declaration. */
16863
16864 static void
16865 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16866 {
16867 tree decl = NULL_TREE;
16868 VEC (deferred_access_check,gc) *checks;
16869 tree parameter_list;
16870 bool friend_p = false;
16871 bool need_lang_pop;
16872
16873 /* Look for the `template' keyword. */
16874 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16875 return;
16876
16877 /* And the `<'. */
16878 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16879 return;
16880 if (at_class_scope_p () && current_function_decl)
16881 {
16882 /* 14.5.2.2 [temp.mem]
16883
16884 A local class shall not have member templates. */
16885 error ("invalid declaration of member template in local class");
16886 cp_parser_skip_to_end_of_block_or_statement (parser);
16887 return;
16888 }
16889 /* [temp]
16890
16891 A template ... shall not have C linkage. */
16892 if (current_lang_name == lang_name_c)
16893 {
16894 error ("template with C linkage");
16895 /* Give it C++ linkage to avoid confusing other parts of the
16896 front end. */
16897 push_lang_context (lang_name_cplusplus);
16898 need_lang_pop = true;
16899 }
16900 else
16901 need_lang_pop = false;
16902
16903 /* We cannot perform access checks on the template parameter
16904 declarations until we know what is being declared, just as we
16905 cannot check the decl-specifier list. */
16906 push_deferring_access_checks (dk_deferred);
16907
16908 /* If the next token is `>', then we have an invalid
16909 specialization. Rather than complain about an invalid template
16910 parameter, issue an error message here. */
16911 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16912 {
16913 cp_parser_error (parser, "invalid explicit specialization");
16914 begin_specialization ();
16915 parameter_list = NULL_TREE;
16916 }
16917 else
16918 /* Parse the template parameters. */
16919 parameter_list = cp_parser_template_parameter_list (parser);
16920
16921 /* Get the deferred access checks from the parameter list. These
16922 will be checked once we know what is being declared, as for a
16923 member template the checks must be performed in the scope of the
16924 class containing the member. */
16925 checks = get_deferred_access_checks ();
16926
16927 /* Look for the `>'. */
16928 cp_parser_skip_to_end_of_template_parameter_list (parser);
16929 /* We just processed one more parameter list. */
16930 ++parser->num_template_parameter_lists;
16931 /* If the next token is `template', there are more template
16932 parameters. */
16933 if (cp_lexer_next_token_is_keyword (parser->lexer,
16934 RID_TEMPLATE))
16935 cp_parser_template_declaration_after_export (parser, member_p);
16936 else
16937 {
16938 /* There are no access checks when parsing a template, as we do not
16939 know if a specialization will be a friend. */
16940 push_deferring_access_checks (dk_no_check);
16941 decl = cp_parser_single_declaration (parser,
16942 checks,
16943 member_p,
16944 /*explicit_specialization_p=*/false,
16945 &friend_p);
16946 pop_deferring_access_checks ();
16947
16948 /* If this is a member template declaration, let the front
16949 end know. */
16950 if (member_p && !friend_p && decl)
16951 {
16952 if (TREE_CODE (decl) == TYPE_DECL)
16953 cp_parser_check_access_in_redeclaration (decl);
16954
16955 decl = finish_member_template_decl (decl);
16956 }
16957 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16958 make_friend_class (current_class_type, TREE_TYPE (decl),
16959 /*complain=*/true);
16960 }
16961 /* We are done with the current parameter list. */
16962 --parser->num_template_parameter_lists;
16963
16964 pop_deferring_access_checks ();
16965
16966 /* Finish up. */
16967 finish_template_decl (parameter_list);
16968
16969 /* Register member declarations. */
16970 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16971 finish_member_declaration (decl);
16972 /* For the erroneous case of a template with C linkage, we pushed an
16973 implicit C++ linkage scope; exit that scope now. */
16974 if (need_lang_pop)
16975 pop_lang_context ();
16976 /* If DECL is a function template, we must return to parse it later.
16977 (Even though there is no definition, there might be default
16978 arguments that need handling.) */
16979 if (member_p && decl
16980 && (TREE_CODE (decl) == FUNCTION_DECL
16981 || DECL_FUNCTION_TEMPLATE_P (decl)))
16982 TREE_VALUE (parser->unparsed_functions_queues)
16983 = tree_cons (NULL_TREE, decl,
16984 TREE_VALUE (parser->unparsed_functions_queues));
16985 }
16986
16987 /* Perform the deferred access checks from a template-parameter-list.
16988 CHECKS is a TREE_LIST of access checks, as returned by
16989 get_deferred_access_checks. */
16990
16991 static void
16992 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16993 {
16994 ++processing_template_parmlist;
16995 perform_access_checks (checks);
16996 --processing_template_parmlist;
16997 }
16998
16999 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17000 `function-definition' sequence. MEMBER_P is true, this declaration
17001 appears in a class scope.
17002
17003 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17004 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17005
17006 static tree
17007 cp_parser_single_declaration (cp_parser* parser,
17008 VEC (deferred_access_check,gc)* checks,
17009 bool member_p,
17010 bool explicit_specialization_p,
17011 bool* friend_p)
17012 {
17013 int declares_class_or_enum;
17014 tree decl = NULL_TREE;
17015 cp_decl_specifier_seq decl_specifiers;
17016 bool function_definition_p = false;
17017
17018 /* This function is only used when processing a template
17019 declaration. */
17020 gcc_assert (innermost_scope_kind () == sk_template_parms
17021 || innermost_scope_kind () == sk_template_spec);
17022
17023 /* Defer access checks until we know what is being declared. */
17024 push_deferring_access_checks (dk_deferred);
17025
17026 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17027 alternative. */
17028 cp_parser_decl_specifier_seq (parser,
17029 CP_PARSER_FLAGS_OPTIONAL,
17030 &decl_specifiers,
17031 &declares_class_or_enum);
17032 if (friend_p)
17033 *friend_p = cp_parser_friend_p (&decl_specifiers);
17034
17035 /* There are no template typedefs. */
17036 if (decl_specifiers.specs[(int) ds_typedef])
17037 {
17038 error ("template declaration of %qs", "typedef");
17039 decl = error_mark_node;
17040 }
17041
17042 /* Gather up the access checks that occurred the
17043 decl-specifier-seq. */
17044 stop_deferring_access_checks ();
17045
17046 /* Check for the declaration of a template class. */
17047 if (declares_class_or_enum)
17048 {
17049 if (cp_parser_declares_only_class_p (parser))
17050 {
17051 decl = shadow_tag (&decl_specifiers);
17052
17053 /* In this case:
17054
17055 struct C {
17056 friend template <typename T> struct A<T>::B;
17057 };
17058
17059 A<T>::B will be represented by a TYPENAME_TYPE, and
17060 therefore not recognized by shadow_tag. */
17061 if (friend_p && *friend_p
17062 && !decl
17063 && decl_specifiers.type
17064 && TYPE_P (decl_specifiers.type))
17065 decl = decl_specifiers.type;
17066
17067 if (decl && decl != error_mark_node)
17068 decl = TYPE_NAME (decl);
17069 else
17070 decl = error_mark_node;
17071
17072 /* Perform access checks for template parameters. */
17073 cp_parser_perform_template_parameter_access_checks (checks);
17074 }
17075 }
17076 /* If it's not a template class, try for a template function. If
17077 the next token is a `;', then this declaration does not declare
17078 anything. But, if there were errors in the decl-specifiers, then
17079 the error might well have come from an attempted class-specifier.
17080 In that case, there's no need to warn about a missing declarator. */
17081 if (!decl
17082 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17083 || decl_specifiers.type != error_mark_node))
17084 {
17085 decl = cp_parser_init_declarator (parser,
17086 &decl_specifiers,
17087 checks,
17088 /*function_definition_allowed_p=*/true,
17089 member_p,
17090 declares_class_or_enum,
17091 &function_definition_p);
17092
17093 /* 7.1.1-1 [dcl.stc]
17094
17095 A storage-class-specifier shall not be specified in an explicit
17096 specialization... */
17097 if (decl
17098 && explicit_specialization_p
17099 && decl_specifiers.storage_class != sc_none)
17100 {
17101 error ("explicit template specialization cannot have a storage class");
17102 decl = error_mark_node;
17103 }
17104 }
17105
17106 pop_deferring_access_checks ();
17107
17108 /* Clear any current qualification; whatever comes next is the start
17109 of something new. */
17110 parser->scope = NULL_TREE;
17111 parser->qualifying_scope = NULL_TREE;
17112 parser->object_scope = NULL_TREE;
17113 /* Look for a trailing `;' after the declaration. */
17114 if (!function_definition_p
17115 && (decl == error_mark_node
17116 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17117 cp_parser_skip_to_end_of_block_or_statement (parser);
17118
17119 return decl;
17120 }
17121
17122 /* Parse a cast-expression that is not the operand of a unary "&". */
17123
17124 static tree
17125 cp_parser_simple_cast_expression (cp_parser *parser)
17126 {
17127 return cp_parser_cast_expression (parser, /*address_p=*/false,
17128 /*cast_p=*/false);
17129 }
17130
17131 /* Parse a functional cast to TYPE. Returns an expression
17132 representing the cast. */
17133
17134 static tree
17135 cp_parser_functional_cast (cp_parser* parser, tree type)
17136 {
17137 tree expression_list;
17138 tree cast;
17139
17140 expression_list
17141 = cp_parser_parenthesized_expression_list (parser, false,
17142 /*cast_p=*/true,
17143 /*allow_expansion_p=*/true,
17144 /*non_constant_p=*/NULL);
17145
17146 cast = build_functional_cast (type, expression_list);
17147 /* [expr.const]/1: In an integral constant expression "only type
17148 conversions to integral or enumeration type can be used". */
17149 if (TREE_CODE (type) == TYPE_DECL)
17150 type = TREE_TYPE (type);
17151 if (cast != error_mark_node
17152 && !cast_valid_in_integral_constant_expression_p (type)
17153 && (cp_parser_non_integral_constant_expression
17154 (parser, "a call to a constructor")))
17155 return error_mark_node;
17156 return cast;
17157 }
17158
17159 /* Save the tokens that make up the body of a member function defined
17160 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17161 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17162 specifiers applied to the declaration. Returns the FUNCTION_DECL
17163 for the member function. */
17164
17165 static tree
17166 cp_parser_save_member_function_body (cp_parser* parser,
17167 cp_decl_specifier_seq *decl_specifiers,
17168 cp_declarator *declarator,
17169 tree attributes)
17170 {
17171 cp_token *first;
17172 cp_token *last;
17173 tree fn;
17174
17175 /* Create the function-declaration. */
17176 fn = start_method (decl_specifiers, declarator, attributes);
17177 /* If something went badly wrong, bail out now. */
17178 if (fn == error_mark_node)
17179 {
17180 /* If there's a function-body, skip it. */
17181 if (cp_parser_token_starts_function_definition_p
17182 (cp_lexer_peek_token (parser->lexer)))
17183 cp_parser_skip_to_end_of_block_or_statement (parser);
17184 return error_mark_node;
17185 }
17186
17187 /* Remember it, if there default args to post process. */
17188 cp_parser_save_default_args (parser, fn);
17189
17190 /* Save away the tokens that make up the body of the
17191 function. */
17192 first = parser->lexer->next_token;
17193 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17194 /* Handle function try blocks. */
17195 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17196 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17197 last = parser->lexer->next_token;
17198
17199 /* Save away the inline definition; we will process it when the
17200 class is complete. */
17201 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17202 DECL_PENDING_INLINE_P (fn) = 1;
17203
17204 /* We need to know that this was defined in the class, so that
17205 friend templates are handled correctly. */
17206 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17207
17208 /* We're done with the inline definition. */
17209 finish_method (fn);
17210
17211 /* Add FN to the queue of functions to be parsed later. */
17212 TREE_VALUE (parser->unparsed_functions_queues)
17213 = tree_cons (NULL_TREE, fn,
17214 TREE_VALUE (parser->unparsed_functions_queues));
17215
17216 return fn;
17217 }
17218
17219 /* Parse a template-argument-list, as well as the trailing ">" (but
17220 not the opening ">"). See cp_parser_template_argument_list for the
17221 return value. */
17222
17223 static tree
17224 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17225 {
17226 tree arguments;
17227 tree saved_scope;
17228 tree saved_qualifying_scope;
17229 tree saved_object_scope;
17230 bool saved_greater_than_is_operator_p;
17231 bool saved_skip_evaluation;
17232
17233 /* [temp.names]
17234
17235 When parsing a template-id, the first non-nested `>' is taken as
17236 the end of the template-argument-list rather than a greater-than
17237 operator. */
17238 saved_greater_than_is_operator_p
17239 = parser->greater_than_is_operator_p;
17240 parser->greater_than_is_operator_p = false;
17241 /* Parsing the argument list may modify SCOPE, so we save it
17242 here. */
17243 saved_scope = parser->scope;
17244 saved_qualifying_scope = parser->qualifying_scope;
17245 saved_object_scope = parser->object_scope;
17246 /* We need to evaluate the template arguments, even though this
17247 template-id may be nested within a "sizeof". */
17248 saved_skip_evaluation = skip_evaluation;
17249 skip_evaluation = false;
17250 /* Parse the template-argument-list itself. */
17251 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17252 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17253 arguments = NULL_TREE;
17254 else
17255 arguments = cp_parser_template_argument_list (parser);
17256 /* Look for the `>' that ends the template-argument-list. If we find
17257 a '>>' instead, it's probably just a typo. */
17258 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17259 {
17260 if (cxx_dialect != cxx98)
17261 {
17262 /* In C++0x, a `>>' in a template argument list or cast
17263 expression is considered to be two separate `>'
17264 tokens. So, change the current token to a `>', but don't
17265 consume it: it will be consumed later when the outer
17266 template argument list (or cast expression) is parsed.
17267 Note that this replacement of `>' for `>>' is necessary
17268 even if we are parsing tentatively: in the tentative
17269 case, after calling
17270 cp_parser_enclosed_template_argument_list we will always
17271 throw away all of the template arguments and the first
17272 closing `>', either because the template argument list
17273 was erroneous or because we are replacing those tokens
17274 with a CPP_TEMPLATE_ID token. The second `>' (which will
17275 not have been thrown away) is needed either to close an
17276 outer template argument list or to complete a new-style
17277 cast. */
17278 cp_token *token = cp_lexer_peek_token (parser->lexer);
17279 token->type = CPP_GREATER;
17280 }
17281 else if (!saved_greater_than_is_operator_p)
17282 {
17283 /* If we're in a nested template argument list, the '>>' has
17284 to be a typo for '> >'. We emit the error message, but we
17285 continue parsing and we push a '>' as next token, so that
17286 the argument list will be parsed correctly. Note that the
17287 global source location is still on the token before the
17288 '>>', so we need to say explicitly where we want it. */
17289 cp_token *token = cp_lexer_peek_token (parser->lexer);
17290 error ("%H%<>>%> should be %<> >%> "
17291 "within a nested template argument list",
17292 &token->location);
17293
17294 token->type = CPP_GREATER;
17295 }
17296 else
17297 {
17298 /* If this is not a nested template argument list, the '>>'
17299 is a typo for '>'. Emit an error message and continue.
17300 Same deal about the token location, but here we can get it
17301 right by consuming the '>>' before issuing the diagnostic. */
17302 cp_lexer_consume_token (parser->lexer);
17303 error ("spurious %<>>%>, use %<>%> to terminate "
17304 "a template argument list");
17305 }
17306 }
17307 else
17308 cp_parser_skip_to_end_of_template_parameter_list (parser);
17309 /* The `>' token might be a greater-than operator again now. */
17310 parser->greater_than_is_operator_p
17311 = saved_greater_than_is_operator_p;
17312 /* Restore the SAVED_SCOPE. */
17313 parser->scope = saved_scope;
17314 parser->qualifying_scope = saved_qualifying_scope;
17315 parser->object_scope = saved_object_scope;
17316 skip_evaluation = saved_skip_evaluation;
17317
17318 return arguments;
17319 }
17320
17321 /* MEMBER_FUNCTION is a member function, or a friend. If default
17322 arguments, or the body of the function have not yet been parsed,
17323 parse them now. */
17324
17325 static void
17326 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17327 {
17328 /* If this member is a template, get the underlying
17329 FUNCTION_DECL. */
17330 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17331 member_function = DECL_TEMPLATE_RESULT (member_function);
17332
17333 /* There should not be any class definitions in progress at this
17334 point; the bodies of members are only parsed outside of all class
17335 definitions. */
17336 gcc_assert (parser->num_classes_being_defined == 0);
17337 /* While we're parsing the member functions we might encounter more
17338 classes. We want to handle them right away, but we don't want
17339 them getting mixed up with functions that are currently in the
17340 queue. */
17341 parser->unparsed_functions_queues
17342 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17343
17344 /* Make sure that any template parameters are in scope. */
17345 maybe_begin_member_template_processing (member_function);
17346
17347 /* If the body of the function has not yet been parsed, parse it
17348 now. */
17349 if (DECL_PENDING_INLINE_P (member_function))
17350 {
17351 tree function_scope;
17352 cp_token_cache *tokens;
17353
17354 /* The function is no longer pending; we are processing it. */
17355 tokens = DECL_PENDING_INLINE_INFO (member_function);
17356 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17357 DECL_PENDING_INLINE_P (member_function) = 0;
17358
17359 /* If this is a local class, enter the scope of the containing
17360 function. */
17361 function_scope = current_function_decl;
17362 if (function_scope)
17363 push_function_context_to (function_scope);
17364
17365
17366 /* Push the body of the function onto the lexer stack. */
17367 cp_parser_push_lexer_for_tokens (parser, tokens);
17368
17369 /* Let the front end know that we going to be defining this
17370 function. */
17371 start_preparsed_function (member_function, NULL_TREE,
17372 SF_PRE_PARSED | SF_INCLASS_INLINE);
17373
17374 /* Don't do access checking if it is a templated function. */
17375 if (processing_template_decl)
17376 push_deferring_access_checks (dk_no_check);
17377
17378 /* Now, parse the body of the function. */
17379 cp_parser_function_definition_after_declarator (parser,
17380 /*inline_p=*/true);
17381
17382 if (processing_template_decl)
17383 pop_deferring_access_checks ();
17384
17385 /* Leave the scope of the containing function. */
17386 if (function_scope)
17387 pop_function_context_from (function_scope);
17388 cp_parser_pop_lexer (parser);
17389 }
17390
17391 /* Remove any template parameters from the symbol table. */
17392 maybe_end_member_template_processing ();
17393
17394 /* Restore the queue. */
17395 parser->unparsed_functions_queues
17396 = TREE_CHAIN (parser->unparsed_functions_queues);
17397 }
17398
17399 /* If DECL contains any default args, remember it on the unparsed
17400 functions queue. */
17401
17402 static void
17403 cp_parser_save_default_args (cp_parser* parser, tree decl)
17404 {
17405 tree probe;
17406
17407 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17408 probe;
17409 probe = TREE_CHAIN (probe))
17410 if (TREE_PURPOSE (probe))
17411 {
17412 TREE_PURPOSE (parser->unparsed_functions_queues)
17413 = tree_cons (current_class_type, decl,
17414 TREE_PURPOSE (parser->unparsed_functions_queues));
17415 break;
17416 }
17417 }
17418
17419 /* FN is a FUNCTION_DECL which may contains a parameter with an
17420 unparsed DEFAULT_ARG. Parse the default args now. This function
17421 assumes that the current scope is the scope in which the default
17422 argument should be processed. */
17423
17424 static void
17425 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17426 {
17427 bool saved_local_variables_forbidden_p;
17428 tree parm;
17429
17430 /* While we're parsing the default args, we might (due to the
17431 statement expression extension) encounter more classes. We want
17432 to handle them right away, but we don't want them getting mixed
17433 up with default args that are currently in the queue. */
17434 parser->unparsed_functions_queues
17435 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17436
17437 /* Local variable names (and the `this' keyword) may not appear
17438 in a default argument. */
17439 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17440 parser->local_variables_forbidden_p = true;
17441
17442 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17443 parm;
17444 parm = TREE_CHAIN (parm))
17445 {
17446 cp_token_cache *tokens;
17447 tree default_arg = TREE_PURPOSE (parm);
17448 tree parsed_arg;
17449 VEC(tree,gc) *insts;
17450 tree copy;
17451 unsigned ix;
17452
17453 if (!default_arg)
17454 continue;
17455
17456 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17457 /* This can happen for a friend declaration for a function
17458 already declared with default arguments. */
17459 continue;
17460
17461 /* Push the saved tokens for the default argument onto the parser's
17462 lexer stack. */
17463 tokens = DEFARG_TOKENS (default_arg);
17464 cp_parser_push_lexer_for_tokens (parser, tokens);
17465
17466 /* Parse the assignment-expression. */
17467 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17468
17469 if (!processing_template_decl)
17470 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17471
17472 TREE_PURPOSE (parm) = parsed_arg;
17473
17474 /* Update any instantiations we've already created. */
17475 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17476 VEC_iterate (tree, insts, ix, copy); ix++)
17477 TREE_PURPOSE (copy) = parsed_arg;
17478
17479 /* If the token stream has not been completely used up, then
17480 there was extra junk after the end of the default
17481 argument. */
17482 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17483 cp_parser_error (parser, "expected %<,%>");
17484
17485 /* Revert to the main lexer. */
17486 cp_parser_pop_lexer (parser);
17487 }
17488
17489 /* Make sure no default arg is missing. */
17490 check_default_args (fn);
17491
17492 /* Restore the state of local_variables_forbidden_p. */
17493 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17494
17495 /* Restore the queue. */
17496 parser->unparsed_functions_queues
17497 = TREE_CHAIN (parser->unparsed_functions_queues);
17498 }
17499
17500 /* Parse the operand of `sizeof' (or a similar operator). Returns
17501 either a TYPE or an expression, depending on the form of the
17502 input. The KEYWORD indicates which kind of expression we have
17503 encountered. */
17504
17505 static tree
17506 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17507 {
17508 static const char *format;
17509 tree expr = NULL_TREE;
17510 const char *saved_message;
17511 char *tmp;
17512 bool saved_integral_constant_expression_p;
17513 bool saved_non_integral_constant_expression_p;
17514 bool pack_expansion_p = false;
17515
17516 /* Initialize FORMAT the first time we get here. */
17517 if (!format)
17518 format = "types may not be defined in '%s' expressions";
17519
17520 /* Types cannot be defined in a `sizeof' expression. Save away the
17521 old message. */
17522 saved_message = parser->type_definition_forbidden_message;
17523 /* And create the new one. */
17524 parser->type_definition_forbidden_message = tmp
17525 = XNEWVEC (char, strlen (format)
17526 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17527 + 1 /* `\0' */);
17528 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17529
17530 /* The restrictions on constant-expressions do not apply inside
17531 sizeof expressions. */
17532 saved_integral_constant_expression_p
17533 = parser->integral_constant_expression_p;
17534 saved_non_integral_constant_expression_p
17535 = parser->non_integral_constant_expression_p;
17536 parser->integral_constant_expression_p = false;
17537
17538 /* If it's a `...', then we are computing the length of a parameter
17539 pack. */
17540 if (keyword == RID_SIZEOF
17541 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17542 {
17543 /* Consume the `...'. */
17544 cp_lexer_consume_token (parser->lexer);
17545 maybe_warn_variadic_templates ();
17546
17547 /* Note that this is an expansion. */
17548 pack_expansion_p = true;
17549 }
17550
17551 /* Do not actually evaluate the expression. */
17552 ++skip_evaluation;
17553 /* If it's a `(', then we might be looking at the type-id
17554 construction. */
17555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17556 {
17557 tree type;
17558 bool saved_in_type_id_in_expr_p;
17559
17560 /* We can't be sure yet whether we're looking at a type-id or an
17561 expression. */
17562 cp_parser_parse_tentatively (parser);
17563 /* Consume the `('. */
17564 cp_lexer_consume_token (parser->lexer);
17565 /* Parse the type-id. */
17566 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17567 parser->in_type_id_in_expr_p = true;
17568 type = cp_parser_type_id (parser);
17569 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17570 /* Now, look for the trailing `)'. */
17571 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17572 /* If all went well, then we're done. */
17573 if (cp_parser_parse_definitely (parser))
17574 {
17575 cp_decl_specifier_seq decl_specs;
17576
17577 /* Build a trivial decl-specifier-seq. */
17578 clear_decl_specs (&decl_specs);
17579 decl_specs.type = type;
17580
17581 /* Call grokdeclarator to figure out what type this is. */
17582 expr = grokdeclarator (NULL,
17583 &decl_specs,
17584 TYPENAME,
17585 /*initialized=*/0,
17586 /*attrlist=*/NULL);
17587 }
17588 }
17589
17590 /* If the type-id production did not work out, then we must be
17591 looking at the unary-expression production. */
17592 if (!expr)
17593 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17594 /*cast_p=*/false);
17595
17596 if (pack_expansion_p)
17597 /* Build a pack expansion. */
17598 expr = make_pack_expansion (expr);
17599
17600 /* Go back to evaluating expressions. */
17601 --skip_evaluation;
17602
17603 /* Free the message we created. */
17604 free (tmp);
17605 /* And restore the old one. */
17606 parser->type_definition_forbidden_message = saved_message;
17607 parser->integral_constant_expression_p
17608 = saved_integral_constant_expression_p;
17609 parser->non_integral_constant_expression_p
17610 = saved_non_integral_constant_expression_p;
17611
17612 return expr;
17613 }
17614
17615 /* If the current declaration has no declarator, return true. */
17616
17617 static bool
17618 cp_parser_declares_only_class_p (cp_parser *parser)
17619 {
17620 /* If the next token is a `;' or a `,' then there is no
17621 declarator. */
17622 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17623 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17624 }
17625
17626 /* Update the DECL_SPECS to reflect the storage class indicated by
17627 KEYWORD. */
17628
17629 static void
17630 cp_parser_set_storage_class (cp_parser *parser,
17631 cp_decl_specifier_seq *decl_specs,
17632 enum rid keyword)
17633 {
17634 cp_storage_class storage_class;
17635
17636 if (parser->in_unbraced_linkage_specification_p)
17637 {
17638 error ("invalid use of %qD in linkage specification",
17639 ridpointers[keyword]);
17640 return;
17641 }
17642 else if (decl_specs->storage_class != sc_none)
17643 {
17644 decl_specs->conflicting_specifiers_p = true;
17645 return;
17646 }
17647
17648 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17649 && decl_specs->specs[(int) ds_thread])
17650 {
17651 error ("%<__thread%> before %qD", ridpointers[keyword]);
17652 decl_specs->specs[(int) ds_thread] = 0;
17653 }
17654
17655 switch (keyword)
17656 {
17657 case RID_AUTO:
17658 storage_class = sc_auto;
17659 break;
17660 case RID_REGISTER:
17661 storage_class = sc_register;
17662 break;
17663 case RID_STATIC:
17664 storage_class = sc_static;
17665 break;
17666 case RID_EXTERN:
17667 storage_class = sc_extern;
17668 break;
17669 case RID_MUTABLE:
17670 storage_class = sc_mutable;
17671 break;
17672 default:
17673 gcc_unreachable ();
17674 }
17675 decl_specs->storage_class = storage_class;
17676
17677 /* A storage class specifier cannot be applied alongside a typedef
17678 specifier. If there is a typedef specifier present then set
17679 conflicting_specifiers_p which will trigger an error later
17680 on in grokdeclarator. */
17681 if (decl_specs->specs[(int)ds_typedef])
17682 decl_specs->conflicting_specifiers_p = true;
17683 }
17684
17685 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17686 is true, the type is a user-defined type; otherwise it is a
17687 built-in type specified by a keyword. */
17688
17689 static void
17690 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17691 tree type_spec,
17692 bool user_defined_p)
17693 {
17694 decl_specs->any_specifiers_p = true;
17695
17696 /* If the user tries to redeclare bool or wchar_t (with, for
17697 example, in "typedef int wchar_t;") we remember that this is what
17698 happened. In system headers, we ignore these declarations so
17699 that G++ can work with system headers that are not C++-safe. */
17700 if (decl_specs->specs[(int) ds_typedef]
17701 && !user_defined_p
17702 && (type_spec == boolean_type_node
17703 || type_spec == wchar_type_node)
17704 && (decl_specs->type
17705 || decl_specs->specs[(int) ds_long]
17706 || decl_specs->specs[(int) ds_short]
17707 || decl_specs->specs[(int) ds_unsigned]
17708 || decl_specs->specs[(int) ds_signed]))
17709 {
17710 decl_specs->redefined_builtin_type = type_spec;
17711 if (!decl_specs->type)
17712 {
17713 decl_specs->type = type_spec;
17714 decl_specs->user_defined_type_p = false;
17715 }
17716 }
17717 else if (decl_specs->type)
17718 decl_specs->multiple_types_p = true;
17719 else
17720 {
17721 decl_specs->type = type_spec;
17722 decl_specs->user_defined_type_p = user_defined_p;
17723 decl_specs->redefined_builtin_type = NULL_TREE;
17724 }
17725 }
17726
17727 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17728 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17729
17730 static bool
17731 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17732 {
17733 return decl_specifiers->specs[(int) ds_friend] != 0;
17734 }
17735
17736 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17737 issue an error message indicating that TOKEN_DESC was expected.
17738
17739 Returns the token consumed, if the token had the appropriate type.
17740 Otherwise, returns NULL. */
17741
17742 static cp_token *
17743 cp_parser_require (cp_parser* parser,
17744 enum cpp_ttype type,
17745 const char* token_desc)
17746 {
17747 if (cp_lexer_next_token_is (parser->lexer, type))
17748 return cp_lexer_consume_token (parser->lexer);
17749 else
17750 {
17751 /* Output the MESSAGE -- unless we're parsing tentatively. */
17752 if (!cp_parser_simulate_error (parser))
17753 {
17754 char *message = concat ("expected ", token_desc, NULL);
17755 cp_parser_error (parser, message);
17756 free (message);
17757 }
17758 return NULL;
17759 }
17760 }
17761
17762 /* An error message is produced if the next token is not '>'.
17763 All further tokens are skipped until the desired token is
17764 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17765
17766 static void
17767 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17768 {
17769 /* Current level of '< ... >'. */
17770 unsigned level = 0;
17771 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17772 unsigned nesting_depth = 0;
17773
17774 /* Are we ready, yet? If not, issue error message. */
17775 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17776 return;
17777
17778 /* Skip tokens until the desired token is found. */
17779 while (true)
17780 {
17781 /* Peek at the next token. */
17782 switch (cp_lexer_peek_token (parser->lexer)->type)
17783 {
17784 case CPP_LESS:
17785 if (!nesting_depth)
17786 ++level;
17787 break;
17788
17789 case CPP_RSHIFT:
17790 if (cxx_dialect == cxx98)
17791 /* C++0x views the `>>' operator as two `>' tokens, but
17792 C++98 does not. */
17793 break;
17794 else if (!nesting_depth && level-- == 0)
17795 {
17796 /* We've hit a `>>' where the first `>' closes the
17797 template argument list, and the second `>' is
17798 spurious. Just consume the `>>' and stop; we've
17799 already produced at least one error. */
17800 cp_lexer_consume_token (parser->lexer);
17801 return;
17802 }
17803 /* Fall through for C++0x, so we handle the second `>' in
17804 the `>>'. */
17805
17806 case CPP_GREATER:
17807 if (!nesting_depth && level-- == 0)
17808 {
17809 /* We've reached the token we want, consume it and stop. */
17810 cp_lexer_consume_token (parser->lexer);
17811 return;
17812 }
17813 break;
17814
17815 case CPP_OPEN_PAREN:
17816 case CPP_OPEN_SQUARE:
17817 ++nesting_depth;
17818 break;
17819
17820 case CPP_CLOSE_PAREN:
17821 case CPP_CLOSE_SQUARE:
17822 if (nesting_depth-- == 0)
17823 return;
17824 break;
17825
17826 case CPP_EOF:
17827 case CPP_PRAGMA_EOL:
17828 case CPP_SEMICOLON:
17829 case CPP_OPEN_BRACE:
17830 case CPP_CLOSE_BRACE:
17831 /* The '>' was probably forgotten, don't look further. */
17832 return;
17833
17834 default:
17835 break;
17836 }
17837
17838 /* Consume this token. */
17839 cp_lexer_consume_token (parser->lexer);
17840 }
17841 }
17842
17843 /* If the next token is the indicated keyword, consume it. Otherwise,
17844 issue an error message indicating that TOKEN_DESC was expected.
17845
17846 Returns the token consumed, if the token had the appropriate type.
17847 Otherwise, returns NULL. */
17848
17849 static cp_token *
17850 cp_parser_require_keyword (cp_parser* parser,
17851 enum rid keyword,
17852 const char* token_desc)
17853 {
17854 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17855
17856 if (token && token->keyword != keyword)
17857 {
17858 dyn_string_t error_msg;
17859
17860 /* Format the error message. */
17861 error_msg = dyn_string_new (0);
17862 dyn_string_append_cstr (error_msg, "expected ");
17863 dyn_string_append_cstr (error_msg, token_desc);
17864 cp_parser_error (parser, error_msg->s);
17865 dyn_string_delete (error_msg);
17866 return NULL;
17867 }
17868
17869 return token;
17870 }
17871
17872 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17873 function-definition. */
17874
17875 static bool
17876 cp_parser_token_starts_function_definition_p (cp_token* token)
17877 {
17878 return (/* An ordinary function-body begins with an `{'. */
17879 token->type == CPP_OPEN_BRACE
17880 /* A ctor-initializer begins with a `:'. */
17881 || token->type == CPP_COLON
17882 /* A function-try-block begins with `try'. */
17883 || token->keyword == RID_TRY
17884 /* The named return value extension begins with `return'. */
17885 || token->keyword == RID_RETURN);
17886 }
17887
17888 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17889 definition. */
17890
17891 static bool
17892 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17893 {
17894 cp_token *token;
17895
17896 token = cp_lexer_peek_token (parser->lexer);
17897 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17898 }
17899
17900 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17901 C++0x) ending a template-argument. */
17902
17903 static bool
17904 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17905 {
17906 cp_token *token;
17907
17908 token = cp_lexer_peek_token (parser->lexer);
17909 return (token->type == CPP_COMMA
17910 || token->type == CPP_GREATER
17911 || token->type == CPP_ELLIPSIS
17912 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17913 }
17914
17915 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17916 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17917
17918 static bool
17919 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17920 size_t n)
17921 {
17922 cp_token *token;
17923
17924 token = cp_lexer_peek_nth_token (parser->lexer, n);
17925 if (token->type == CPP_LESS)
17926 return true;
17927 /* Check for the sequence `<::' in the original code. It would be lexed as
17928 `[:', where `[' is a digraph, and there is no whitespace before
17929 `:'. */
17930 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17931 {
17932 cp_token *token2;
17933 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17934 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17935 return true;
17936 }
17937 return false;
17938 }
17939
17940 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17941 or none_type otherwise. */
17942
17943 static enum tag_types
17944 cp_parser_token_is_class_key (cp_token* token)
17945 {
17946 switch (token->keyword)
17947 {
17948 case RID_CLASS:
17949 return class_type;
17950 case RID_STRUCT:
17951 return record_type;
17952 case RID_UNION:
17953 return union_type;
17954
17955 default:
17956 return none_type;
17957 }
17958 }
17959
17960 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17961
17962 static void
17963 cp_parser_check_class_key (enum tag_types class_key, tree type)
17964 {
17965 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17966 pedwarn ("%qs tag used in naming %q#T",
17967 class_key == union_type ? "union"
17968 : class_key == record_type ? "struct" : "class",
17969 type);
17970 }
17971
17972 /* Issue an error message if DECL is redeclared with different
17973 access than its original declaration [class.access.spec/3].
17974 This applies to nested classes and nested class templates.
17975 [class.mem/1]. */
17976
17977 static void
17978 cp_parser_check_access_in_redeclaration (tree decl)
17979 {
17980 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17981 return;
17982
17983 if ((TREE_PRIVATE (decl)
17984 != (current_access_specifier == access_private_node))
17985 || (TREE_PROTECTED (decl)
17986 != (current_access_specifier == access_protected_node)))
17987 error ("%qD redeclared with different access", decl);
17988 }
17989
17990 /* Look for the `template' keyword, as a syntactic disambiguator.
17991 Return TRUE iff it is present, in which case it will be
17992 consumed. */
17993
17994 static bool
17995 cp_parser_optional_template_keyword (cp_parser *parser)
17996 {
17997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17998 {
17999 /* The `template' keyword can only be used within templates;
18000 outside templates the parser can always figure out what is a
18001 template and what is not. */
18002 if (!processing_template_decl)
18003 {
18004 error ("%<template%> (as a disambiguator) is only allowed "
18005 "within templates");
18006 /* If this part of the token stream is rescanned, the same
18007 error message would be generated. So, we purge the token
18008 from the stream. */
18009 cp_lexer_purge_token (parser->lexer);
18010 return false;
18011 }
18012 else
18013 {
18014 /* Consume the `template' keyword. */
18015 cp_lexer_consume_token (parser->lexer);
18016 return true;
18017 }
18018 }
18019
18020 return false;
18021 }
18022
18023 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18024 set PARSER->SCOPE, and perform other related actions. */
18025
18026 static void
18027 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18028 {
18029 int i;
18030 struct tree_check *check_value;
18031 deferred_access_check *chk;
18032 VEC (deferred_access_check,gc) *checks;
18033
18034 /* Get the stored value. */
18035 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18036 /* Perform any access checks that were deferred. */
18037 checks = check_value->checks;
18038 if (checks)
18039 {
18040 for (i = 0 ;
18041 VEC_iterate (deferred_access_check, checks, i, chk) ;
18042 ++i)
18043 {
18044 perform_or_defer_access_check (chk->binfo,
18045 chk->decl,
18046 chk->diag_decl);
18047 }
18048 }
18049 /* Set the scope from the stored value. */
18050 parser->scope = check_value->value;
18051 parser->qualifying_scope = check_value->qualifying_scope;
18052 parser->object_scope = NULL_TREE;
18053 }
18054
18055 /* Consume tokens up through a non-nested END token. */
18056
18057 static void
18058 cp_parser_cache_group (cp_parser *parser,
18059 enum cpp_ttype end,
18060 unsigned depth)
18061 {
18062 while (true)
18063 {
18064 cp_token *token;
18065
18066 /* Abort a parenthesized expression if we encounter a brace. */
18067 if ((end == CPP_CLOSE_PAREN || depth == 0)
18068 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18069 return;
18070 /* If we've reached the end of the file, stop. */
18071 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18072 || (end != CPP_PRAGMA_EOL
18073 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18074 return;
18075 /* Consume the next token. */
18076 token = cp_lexer_consume_token (parser->lexer);
18077 /* See if it starts a new group. */
18078 if (token->type == CPP_OPEN_BRACE)
18079 {
18080 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18081 if (depth == 0)
18082 return;
18083 }
18084 else if (token->type == CPP_OPEN_PAREN)
18085 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18086 else if (token->type == CPP_PRAGMA)
18087 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18088 else if (token->type == end)
18089 return;
18090 }
18091 }
18092
18093 /* Begin parsing tentatively. We always save tokens while parsing
18094 tentatively so that if the tentative parsing fails we can restore the
18095 tokens. */
18096
18097 static void
18098 cp_parser_parse_tentatively (cp_parser* parser)
18099 {
18100 /* Enter a new parsing context. */
18101 parser->context = cp_parser_context_new (parser->context);
18102 /* Begin saving tokens. */
18103 cp_lexer_save_tokens (parser->lexer);
18104 /* In order to avoid repetitive access control error messages,
18105 access checks are queued up until we are no longer parsing
18106 tentatively. */
18107 push_deferring_access_checks (dk_deferred);
18108 }
18109
18110 /* Commit to the currently active tentative parse. */
18111
18112 static void
18113 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18114 {
18115 cp_parser_context *context;
18116 cp_lexer *lexer;
18117
18118 /* Mark all of the levels as committed. */
18119 lexer = parser->lexer;
18120 for (context = parser->context; context->next; context = context->next)
18121 {
18122 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18123 break;
18124 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18125 while (!cp_lexer_saving_tokens (lexer))
18126 lexer = lexer->next;
18127 cp_lexer_commit_tokens (lexer);
18128 }
18129 }
18130
18131 /* Abort the currently active tentative parse. All consumed tokens
18132 will be rolled back, and no diagnostics will be issued. */
18133
18134 static void
18135 cp_parser_abort_tentative_parse (cp_parser* parser)
18136 {
18137 cp_parser_simulate_error (parser);
18138 /* Now, pretend that we want to see if the construct was
18139 successfully parsed. */
18140 cp_parser_parse_definitely (parser);
18141 }
18142
18143 /* Stop parsing tentatively. If a parse error has occurred, restore the
18144 token stream. Otherwise, commit to the tokens we have consumed.
18145 Returns true if no error occurred; false otherwise. */
18146
18147 static bool
18148 cp_parser_parse_definitely (cp_parser* parser)
18149 {
18150 bool error_occurred;
18151 cp_parser_context *context;
18152
18153 /* Remember whether or not an error occurred, since we are about to
18154 destroy that information. */
18155 error_occurred = cp_parser_error_occurred (parser);
18156 /* Remove the topmost context from the stack. */
18157 context = parser->context;
18158 parser->context = context->next;
18159 /* If no parse errors occurred, commit to the tentative parse. */
18160 if (!error_occurred)
18161 {
18162 /* Commit to the tokens read tentatively, unless that was
18163 already done. */
18164 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18165 cp_lexer_commit_tokens (parser->lexer);
18166
18167 pop_to_parent_deferring_access_checks ();
18168 }
18169 /* Otherwise, if errors occurred, roll back our state so that things
18170 are just as they were before we began the tentative parse. */
18171 else
18172 {
18173 cp_lexer_rollback_tokens (parser->lexer);
18174 pop_deferring_access_checks ();
18175 }
18176 /* Add the context to the front of the free list. */
18177 context->next = cp_parser_context_free_list;
18178 cp_parser_context_free_list = context;
18179
18180 return !error_occurred;
18181 }
18182
18183 /* Returns true if we are parsing tentatively and are not committed to
18184 this tentative parse. */
18185
18186 static bool
18187 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18188 {
18189 return (cp_parser_parsing_tentatively (parser)
18190 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18191 }
18192
18193 /* Returns nonzero iff an error has occurred during the most recent
18194 tentative parse. */
18195
18196 static bool
18197 cp_parser_error_occurred (cp_parser* parser)
18198 {
18199 return (cp_parser_parsing_tentatively (parser)
18200 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18201 }
18202
18203 /* Returns nonzero if GNU extensions are allowed. */
18204
18205 static bool
18206 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18207 {
18208 return parser->allow_gnu_extensions_p;
18209 }
18210 \f
18211 /* Objective-C++ Productions */
18212
18213
18214 /* Parse an Objective-C expression, which feeds into a primary-expression
18215 above.
18216
18217 objc-expression:
18218 objc-message-expression
18219 objc-string-literal
18220 objc-encode-expression
18221 objc-protocol-expression
18222 objc-selector-expression
18223
18224 Returns a tree representation of the expression. */
18225
18226 static tree
18227 cp_parser_objc_expression (cp_parser* parser)
18228 {
18229 /* Try to figure out what kind of declaration is present. */
18230 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18231
18232 switch (kwd->type)
18233 {
18234 case CPP_OPEN_SQUARE:
18235 return cp_parser_objc_message_expression (parser);
18236
18237 case CPP_OBJC_STRING:
18238 kwd = cp_lexer_consume_token (parser->lexer);
18239 return objc_build_string_object (kwd->u.value);
18240
18241 case CPP_KEYWORD:
18242 switch (kwd->keyword)
18243 {
18244 case RID_AT_ENCODE:
18245 return cp_parser_objc_encode_expression (parser);
18246
18247 case RID_AT_PROTOCOL:
18248 return cp_parser_objc_protocol_expression (parser);
18249
18250 case RID_AT_SELECTOR:
18251 return cp_parser_objc_selector_expression (parser);
18252
18253 default:
18254 break;
18255 }
18256 default:
18257 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18258 cp_parser_skip_to_end_of_block_or_statement (parser);
18259 }
18260
18261 return error_mark_node;
18262 }
18263
18264 /* Parse an Objective-C message expression.
18265
18266 objc-message-expression:
18267 [ objc-message-receiver objc-message-args ]
18268
18269 Returns a representation of an Objective-C message. */
18270
18271 static tree
18272 cp_parser_objc_message_expression (cp_parser* parser)
18273 {
18274 tree receiver, messageargs;
18275
18276 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18277 receiver = cp_parser_objc_message_receiver (parser);
18278 messageargs = cp_parser_objc_message_args (parser);
18279 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18280
18281 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18282 }
18283
18284 /* Parse an objc-message-receiver.
18285
18286 objc-message-receiver:
18287 expression
18288 simple-type-specifier
18289
18290 Returns a representation of the type or expression. */
18291
18292 static tree
18293 cp_parser_objc_message_receiver (cp_parser* parser)
18294 {
18295 tree rcv;
18296
18297 /* An Objective-C message receiver may be either (1) a type
18298 or (2) an expression. */
18299 cp_parser_parse_tentatively (parser);
18300 rcv = cp_parser_expression (parser, false);
18301
18302 if (cp_parser_parse_definitely (parser))
18303 return rcv;
18304
18305 rcv = cp_parser_simple_type_specifier (parser,
18306 /*decl_specs=*/NULL,
18307 CP_PARSER_FLAGS_NONE);
18308
18309 return objc_get_class_reference (rcv);
18310 }
18311
18312 /* Parse the arguments and selectors comprising an Objective-C message.
18313
18314 objc-message-args:
18315 objc-selector
18316 objc-selector-args
18317 objc-selector-args , objc-comma-args
18318
18319 objc-selector-args:
18320 objc-selector [opt] : assignment-expression
18321 objc-selector-args objc-selector [opt] : assignment-expression
18322
18323 objc-comma-args:
18324 assignment-expression
18325 objc-comma-args , assignment-expression
18326
18327 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18328 selector arguments and TREE_VALUE containing a list of comma
18329 arguments. */
18330
18331 static tree
18332 cp_parser_objc_message_args (cp_parser* parser)
18333 {
18334 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18335 bool maybe_unary_selector_p = true;
18336 cp_token *token = cp_lexer_peek_token (parser->lexer);
18337
18338 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18339 {
18340 tree selector = NULL_TREE, arg;
18341
18342 if (token->type != CPP_COLON)
18343 selector = cp_parser_objc_selector (parser);
18344
18345 /* Detect if we have a unary selector. */
18346 if (maybe_unary_selector_p
18347 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18348 return build_tree_list (selector, NULL_TREE);
18349
18350 maybe_unary_selector_p = false;
18351 cp_parser_require (parser, CPP_COLON, "`:'");
18352 arg = cp_parser_assignment_expression (parser, false);
18353
18354 sel_args
18355 = chainon (sel_args,
18356 build_tree_list (selector, arg));
18357
18358 token = cp_lexer_peek_token (parser->lexer);
18359 }
18360
18361 /* Handle non-selector arguments, if any. */
18362 while (token->type == CPP_COMMA)
18363 {
18364 tree arg;
18365
18366 cp_lexer_consume_token (parser->lexer);
18367 arg = cp_parser_assignment_expression (parser, false);
18368
18369 addl_args
18370 = chainon (addl_args,
18371 build_tree_list (NULL_TREE, arg));
18372
18373 token = cp_lexer_peek_token (parser->lexer);
18374 }
18375
18376 return build_tree_list (sel_args, addl_args);
18377 }
18378
18379 /* Parse an Objective-C encode expression.
18380
18381 objc-encode-expression:
18382 @encode objc-typename
18383
18384 Returns an encoded representation of the type argument. */
18385
18386 static tree
18387 cp_parser_objc_encode_expression (cp_parser* parser)
18388 {
18389 tree type;
18390
18391 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18392 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18393 type = complete_type (cp_parser_type_id (parser));
18394 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18395
18396 if (!type)
18397 {
18398 error ("%<@encode%> must specify a type as an argument");
18399 return error_mark_node;
18400 }
18401
18402 return objc_build_encode_expr (type);
18403 }
18404
18405 /* Parse an Objective-C @defs expression. */
18406
18407 static tree
18408 cp_parser_objc_defs_expression (cp_parser *parser)
18409 {
18410 tree name;
18411
18412 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18413 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18414 name = cp_parser_identifier (parser);
18415 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18416
18417 return objc_get_class_ivars (name);
18418 }
18419
18420 /* Parse an Objective-C protocol expression.
18421
18422 objc-protocol-expression:
18423 @protocol ( identifier )
18424
18425 Returns a representation of the protocol expression. */
18426
18427 static tree
18428 cp_parser_objc_protocol_expression (cp_parser* parser)
18429 {
18430 tree proto;
18431
18432 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18433 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18434 proto = cp_parser_identifier (parser);
18435 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18436
18437 return objc_build_protocol_expr (proto);
18438 }
18439
18440 /* Parse an Objective-C selector expression.
18441
18442 objc-selector-expression:
18443 @selector ( objc-method-signature )
18444
18445 objc-method-signature:
18446 objc-selector
18447 objc-selector-seq
18448
18449 objc-selector-seq:
18450 objc-selector :
18451 objc-selector-seq objc-selector :
18452
18453 Returns a representation of the method selector. */
18454
18455 static tree
18456 cp_parser_objc_selector_expression (cp_parser* parser)
18457 {
18458 tree sel_seq = NULL_TREE;
18459 bool maybe_unary_selector_p = true;
18460 cp_token *token;
18461
18462 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18463 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18464 token = cp_lexer_peek_token (parser->lexer);
18465
18466 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18467 || token->type == CPP_SCOPE)
18468 {
18469 tree selector = NULL_TREE;
18470
18471 if (token->type != CPP_COLON
18472 || token->type == CPP_SCOPE)
18473 selector = cp_parser_objc_selector (parser);
18474
18475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18476 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18477 {
18478 /* Detect if we have a unary selector. */
18479 if (maybe_unary_selector_p)
18480 {
18481 sel_seq = selector;
18482 goto finish_selector;
18483 }
18484 else
18485 {
18486 cp_parser_error (parser, "expected %<:%>");
18487 }
18488 }
18489 maybe_unary_selector_p = false;
18490 token = cp_lexer_consume_token (parser->lexer);
18491
18492 if (token->type == CPP_SCOPE)
18493 {
18494 sel_seq
18495 = chainon (sel_seq,
18496 build_tree_list (selector, NULL_TREE));
18497 sel_seq
18498 = chainon (sel_seq,
18499 build_tree_list (NULL_TREE, NULL_TREE));
18500 }
18501 else
18502 sel_seq
18503 = chainon (sel_seq,
18504 build_tree_list (selector, NULL_TREE));
18505
18506 token = cp_lexer_peek_token (parser->lexer);
18507 }
18508
18509 finish_selector:
18510 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18511
18512 return objc_build_selector_expr (sel_seq);
18513 }
18514
18515 /* Parse a list of identifiers.
18516
18517 objc-identifier-list:
18518 identifier
18519 objc-identifier-list , identifier
18520
18521 Returns a TREE_LIST of identifier nodes. */
18522
18523 static tree
18524 cp_parser_objc_identifier_list (cp_parser* parser)
18525 {
18526 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18527 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18528
18529 while (sep->type == CPP_COMMA)
18530 {
18531 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18532 list = chainon (list,
18533 build_tree_list (NULL_TREE,
18534 cp_parser_identifier (parser)));
18535 sep = cp_lexer_peek_token (parser->lexer);
18536 }
18537
18538 return list;
18539 }
18540
18541 /* Parse an Objective-C alias declaration.
18542
18543 objc-alias-declaration:
18544 @compatibility_alias identifier identifier ;
18545
18546 This function registers the alias mapping with the Objective-C front end.
18547 It returns nothing. */
18548
18549 static void
18550 cp_parser_objc_alias_declaration (cp_parser* parser)
18551 {
18552 tree alias, orig;
18553
18554 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18555 alias = cp_parser_identifier (parser);
18556 orig = cp_parser_identifier (parser);
18557 objc_declare_alias (alias, orig);
18558 cp_parser_consume_semicolon_at_end_of_statement (parser);
18559 }
18560
18561 /* Parse an Objective-C class forward-declaration.
18562
18563 objc-class-declaration:
18564 @class objc-identifier-list ;
18565
18566 The function registers the forward declarations with the Objective-C
18567 front end. It returns nothing. */
18568
18569 static void
18570 cp_parser_objc_class_declaration (cp_parser* parser)
18571 {
18572 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18573 objc_declare_class (cp_parser_objc_identifier_list (parser));
18574 cp_parser_consume_semicolon_at_end_of_statement (parser);
18575 }
18576
18577 /* Parse a list of Objective-C protocol references.
18578
18579 objc-protocol-refs-opt:
18580 objc-protocol-refs [opt]
18581
18582 objc-protocol-refs:
18583 < objc-identifier-list >
18584
18585 Returns a TREE_LIST of identifiers, if any. */
18586
18587 static tree
18588 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18589 {
18590 tree protorefs = NULL_TREE;
18591
18592 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18593 {
18594 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18595 protorefs = cp_parser_objc_identifier_list (parser);
18596 cp_parser_require (parser, CPP_GREATER, "`>'");
18597 }
18598
18599 return protorefs;
18600 }
18601
18602 /* Parse a Objective-C visibility specification. */
18603
18604 static void
18605 cp_parser_objc_visibility_spec (cp_parser* parser)
18606 {
18607 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18608
18609 switch (vis->keyword)
18610 {
18611 case RID_AT_PRIVATE:
18612 objc_set_visibility (2);
18613 break;
18614 case RID_AT_PROTECTED:
18615 objc_set_visibility (0);
18616 break;
18617 case RID_AT_PUBLIC:
18618 objc_set_visibility (1);
18619 break;
18620 default:
18621 return;
18622 }
18623
18624 /* Eat '@private'/'@protected'/'@public'. */
18625 cp_lexer_consume_token (parser->lexer);
18626 }
18627
18628 /* Parse an Objective-C method type. */
18629
18630 static void
18631 cp_parser_objc_method_type (cp_parser* parser)
18632 {
18633 objc_set_method_type
18634 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18635 ? PLUS_EXPR
18636 : MINUS_EXPR);
18637 }
18638
18639 /* Parse an Objective-C protocol qualifier. */
18640
18641 static tree
18642 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18643 {
18644 tree quals = NULL_TREE, node;
18645 cp_token *token = cp_lexer_peek_token (parser->lexer);
18646
18647 node = token->u.value;
18648
18649 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18650 && (node == ridpointers [(int) RID_IN]
18651 || node == ridpointers [(int) RID_OUT]
18652 || node == ridpointers [(int) RID_INOUT]
18653 || node == ridpointers [(int) RID_BYCOPY]
18654 || node == ridpointers [(int) RID_BYREF]
18655 || node == ridpointers [(int) RID_ONEWAY]))
18656 {
18657 quals = tree_cons (NULL_TREE, node, quals);
18658 cp_lexer_consume_token (parser->lexer);
18659 token = cp_lexer_peek_token (parser->lexer);
18660 node = token->u.value;
18661 }
18662
18663 return quals;
18664 }
18665
18666 /* Parse an Objective-C typename. */
18667
18668 static tree
18669 cp_parser_objc_typename (cp_parser* parser)
18670 {
18671 tree typename = NULL_TREE;
18672
18673 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18674 {
18675 tree proto_quals, cp_type = NULL_TREE;
18676
18677 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18678 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18679
18680 /* An ObjC type name may consist of just protocol qualifiers, in which
18681 case the type shall default to 'id'. */
18682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18683 cp_type = cp_parser_type_id (parser);
18684
18685 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18686 typename = build_tree_list (proto_quals, cp_type);
18687 }
18688
18689 return typename;
18690 }
18691
18692 /* Check to see if TYPE refers to an Objective-C selector name. */
18693
18694 static bool
18695 cp_parser_objc_selector_p (enum cpp_ttype type)
18696 {
18697 return (type == CPP_NAME || type == CPP_KEYWORD
18698 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18699 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18700 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18701 || type == CPP_XOR || type == CPP_XOR_EQ);
18702 }
18703
18704 /* Parse an Objective-C selector. */
18705
18706 static tree
18707 cp_parser_objc_selector (cp_parser* parser)
18708 {
18709 cp_token *token = cp_lexer_consume_token (parser->lexer);
18710
18711 if (!cp_parser_objc_selector_p (token->type))
18712 {
18713 error ("invalid Objective-C++ selector name");
18714 return error_mark_node;
18715 }
18716
18717 /* C++ operator names are allowed to appear in ObjC selectors. */
18718 switch (token->type)
18719 {
18720 case CPP_AND_AND: return get_identifier ("and");
18721 case CPP_AND_EQ: return get_identifier ("and_eq");
18722 case CPP_AND: return get_identifier ("bitand");
18723 case CPP_OR: return get_identifier ("bitor");
18724 case CPP_COMPL: return get_identifier ("compl");
18725 case CPP_NOT: return get_identifier ("not");
18726 case CPP_NOT_EQ: return get_identifier ("not_eq");
18727 case CPP_OR_OR: return get_identifier ("or");
18728 case CPP_OR_EQ: return get_identifier ("or_eq");
18729 case CPP_XOR: return get_identifier ("xor");
18730 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18731 default: return token->u.value;
18732 }
18733 }
18734
18735 /* Parse an Objective-C params list. */
18736
18737 static tree
18738 cp_parser_objc_method_keyword_params (cp_parser* parser)
18739 {
18740 tree params = NULL_TREE;
18741 bool maybe_unary_selector_p = true;
18742 cp_token *token = cp_lexer_peek_token (parser->lexer);
18743
18744 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18745 {
18746 tree selector = NULL_TREE, typename, identifier;
18747
18748 if (token->type != CPP_COLON)
18749 selector = cp_parser_objc_selector (parser);
18750
18751 /* Detect if we have a unary selector. */
18752 if (maybe_unary_selector_p
18753 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18754 return selector;
18755
18756 maybe_unary_selector_p = false;
18757 cp_parser_require (parser, CPP_COLON, "`:'");
18758 typename = cp_parser_objc_typename (parser);
18759 identifier = cp_parser_identifier (parser);
18760
18761 params
18762 = chainon (params,
18763 objc_build_keyword_decl (selector,
18764 typename,
18765 identifier));
18766
18767 token = cp_lexer_peek_token (parser->lexer);
18768 }
18769
18770 return params;
18771 }
18772
18773 /* Parse the non-keyword Objective-C params. */
18774
18775 static tree
18776 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18777 {
18778 tree params = make_node (TREE_LIST);
18779 cp_token *token = cp_lexer_peek_token (parser->lexer);
18780 *ellipsisp = false; /* Initially, assume no ellipsis. */
18781
18782 while (token->type == CPP_COMMA)
18783 {
18784 cp_parameter_declarator *parmdecl;
18785 tree parm;
18786
18787 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18788 token = cp_lexer_peek_token (parser->lexer);
18789
18790 if (token->type == CPP_ELLIPSIS)
18791 {
18792 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18793 *ellipsisp = true;
18794 break;
18795 }
18796
18797 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18798 parm = grokdeclarator (parmdecl->declarator,
18799 &parmdecl->decl_specifiers,
18800 PARM, /*initialized=*/0,
18801 /*attrlist=*/NULL);
18802
18803 chainon (params, build_tree_list (NULL_TREE, parm));
18804 token = cp_lexer_peek_token (parser->lexer);
18805 }
18806
18807 return params;
18808 }
18809
18810 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18811
18812 static void
18813 cp_parser_objc_interstitial_code (cp_parser* parser)
18814 {
18815 cp_token *token = cp_lexer_peek_token (parser->lexer);
18816
18817 /* If the next token is `extern' and the following token is a string
18818 literal, then we have a linkage specification. */
18819 if (token->keyword == RID_EXTERN
18820 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18821 cp_parser_linkage_specification (parser);
18822 /* Handle #pragma, if any. */
18823 else if (token->type == CPP_PRAGMA)
18824 cp_parser_pragma (parser, pragma_external);
18825 /* Allow stray semicolons. */
18826 else if (token->type == CPP_SEMICOLON)
18827 cp_lexer_consume_token (parser->lexer);
18828 /* Finally, try to parse a block-declaration, or a function-definition. */
18829 else
18830 cp_parser_block_declaration (parser, /*statement_p=*/false);
18831 }
18832
18833 /* Parse a method signature. */
18834
18835 static tree
18836 cp_parser_objc_method_signature (cp_parser* parser)
18837 {
18838 tree rettype, kwdparms, optparms;
18839 bool ellipsis = false;
18840
18841 cp_parser_objc_method_type (parser);
18842 rettype = cp_parser_objc_typename (parser);
18843 kwdparms = cp_parser_objc_method_keyword_params (parser);
18844 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18845
18846 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18847 }
18848
18849 /* Pars an Objective-C method prototype list. */
18850
18851 static void
18852 cp_parser_objc_method_prototype_list (cp_parser* parser)
18853 {
18854 cp_token *token = cp_lexer_peek_token (parser->lexer);
18855
18856 while (token->keyword != RID_AT_END)
18857 {
18858 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18859 {
18860 objc_add_method_declaration
18861 (cp_parser_objc_method_signature (parser));
18862 cp_parser_consume_semicolon_at_end_of_statement (parser);
18863 }
18864 else
18865 /* Allow for interspersed non-ObjC++ code. */
18866 cp_parser_objc_interstitial_code (parser);
18867
18868 token = cp_lexer_peek_token (parser->lexer);
18869 }
18870
18871 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18872 objc_finish_interface ();
18873 }
18874
18875 /* Parse an Objective-C method definition list. */
18876
18877 static void
18878 cp_parser_objc_method_definition_list (cp_parser* parser)
18879 {
18880 cp_token *token = cp_lexer_peek_token (parser->lexer);
18881
18882 while (token->keyword != RID_AT_END)
18883 {
18884 tree meth;
18885
18886 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18887 {
18888 push_deferring_access_checks (dk_deferred);
18889 objc_start_method_definition
18890 (cp_parser_objc_method_signature (parser));
18891
18892 /* For historical reasons, we accept an optional semicolon. */
18893 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18894 cp_lexer_consume_token (parser->lexer);
18895
18896 perform_deferred_access_checks ();
18897 stop_deferring_access_checks ();
18898 meth = cp_parser_function_definition_after_declarator (parser,
18899 false);
18900 pop_deferring_access_checks ();
18901 objc_finish_method_definition (meth);
18902 }
18903 else
18904 /* Allow for interspersed non-ObjC++ code. */
18905 cp_parser_objc_interstitial_code (parser);
18906
18907 token = cp_lexer_peek_token (parser->lexer);
18908 }
18909
18910 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18911 objc_finish_implementation ();
18912 }
18913
18914 /* Parse Objective-C ivars. */
18915
18916 static void
18917 cp_parser_objc_class_ivars (cp_parser* parser)
18918 {
18919 cp_token *token = cp_lexer_peek_token (parser->lexer);
18920
18921 if (token->type != CPP_OPEN_BRACE)
18922 return; /* No ivars specified. */
18923
18924 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18925 token = cp_lexer_peek_token (parser->lexer);
18926
18927 while (token->type != CPP_CLOSE_BRACE)
18928 {
18929 cp_decl_specifier_seq declspecs;
18930 int decl_class_or_enum_p;
18931 tree prefix_attributes;
18932
18933 cp_parser_objc_visibility_spec (parser);
18934
18935 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18936 break;
18937
18938 cp_parser_decl_specifier_seq (parser,
18939 CP_PARSER_FLAGS_OPTIONAL,
18940 &declspecs,
18941 &decl_class_or_enum_p);
18942 prefix_attributes = declspecs.attributes;
18943 declspecs.attributes = NULL_TREE;
18944
18945 /* Keep going until we hit the `;' at the end of the
18946 declaration. */
18947 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18948 {
18949 tree width = NULL_TREE, attributes, first_attribute, decl;
18950 cp_declarator *declarator = NULL;
18951 int ctor_dtor_or_conv_p;
18952
18953 /* Check for a (possibly unnamed) bitfield declaration. */
18954 token = cp_lexer_peek_token (parser->lexer);
18955 if (token->type == CPP_COLON)
18956 goto eat_colon;
18957
18958 if (token->type == CPP_NAME
18959 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18960 == CPP_COLON))
18961 {
18962 /* Get the name of the bitfield. */
18963 declarator = make_id_declarator (NULL_TREE,
18964 cp_parser_identifier (parser),
18965 sfk_none);
18966
18967 eat_colon:
18968 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18969 /* Get the width of the bitfield. */
18970 width
18971 = cp_parser_constant_expression (parser,
18972 /*allow_non_constant=*/false,
18973 NULL);
18974 }
18975 else
18976 {
18977 /* Parse the declarator. */
18978 declarator
18979 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18980 &ctor_dtor_or_conv_p,
18981 /*parenthesized_p=*/NULL,
18982 /*member_p=*/false);
18983 }
18984
18985 /* Look for attributes that apply to the ivar. */
18986 attributes = cp_parser_attributes_opt (parser);
18987 /* Remember which attributes are prefix attributes and
18988 which are not. */
18989 first_attribute = attributes;
18990 /* Combine the attributes. */
18991 attributes = chainon (prefix_attributes, attributes);
18992
18993 if (width)
18994 {
18995 /* Create the bitfield declaration. */
18996 decl = grokbitfield (declarator, &declspecs, width);
18997 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18998 }
18999 else
19000 decl = grokfield (declarator, &declspecs,
19001 NULL_TREE, /*init_const_expr_p=*/false,
19002 NULL_TREE, attributes);
19003
19004 /* Add the instance variable. */
19005 objc_add_instance_variable (decl);
19006
19007 /* Reset PREFIX_ATTRIBUTES. */
19008 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19009 attributes = TREE_CHAIN (attributes);
19010 if (attributes)
19011 TREE_CHAIN (attributes) = NULL_TREE;
19012
19013 token = cp_lexer_peek_token (parser->lexer);
19014
19015 if (token->type == CPP_COMMA)
19016 {
19017 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19018 continue;
19019 }
19020 break;
19021 }
19022
19023 cp_parser_consume_semicolon_at_end_of_statement (parser);
19024 token = cp_lexer_peek_token (parser->lexer);
19025 }
19026
19027 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19028 /* For historical reasons, we accept an optional semicolon. */
19029 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19030 cp_lexer_consume_token (parser->lexer);
19031 }
19032
19033 /* Parse an Objective-C protocol declaration. */
19034
19035 static void
19036 cp_parser_objc_protocol_declaration (cp_parser* parser)
19037 {
19038 tree proto, protorefs;
19039 cp_token *tok;
19040
19041 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19043 {
19044 error ("identifier expected after %<@protocol%>");
19045 goto finish;
19046 }
19047
19048 /* See if we have a forward declaration or a definition. */
19049 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19050
19051 /* Try a forward declaration first. */
19052 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19053 {
19054 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19055 finish:
19056 cp_parser_consume_semicolon_at_end_of_statement (parser);
19057 }
19058
19059 /* Ok, we got a full-fledged definition (or at least should). */
19060 else
19061 {
19062 proto = cp_parser_identifier (parser);
19063 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19064 objc_start_protocol (proto, protorefs);
19065 cp_parser_objc_method_prototype_list (parser);
19066 }
19067 }
19068
19069 /* Parse an Objective-C superclass or category. */
19070
19071 static void
19072 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19073 tree *categ)
19074 {
19075 cp_token *next = cp_lexer_peek_token (parser->lexer);
19076
19077 *super = *categ = NULL_TREE;
19078 if (next->type == CPP_COLON)
19079 {
19080 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19081 *super = cp_parser_identifier (parser);
19082 }
19083 else if (next->type == CPP_OPEN_PAREN)
19084 {
19085 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19086 *categ = cp_parser_identifier (parser);
19087 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19088 }
19089 }
19090
19091 /* Parse an Objective-C class interface. */
19092
19093 static void
19094 cp_parser_objc_class_interface (cp_parser* parser)
19095 {
19096 tree name, super, categ, protos;
19097
19098 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19099 name = cp_parser_identifier (parser);
19100 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19101 protos = cp_parser_objc_protocol_refs_opt (parser);
19102
19103 /* We have either a class or a category on our hands. */
19104 if (categ)
19105 objc_start_category_interface (name, categ, protos);
19106 else
19107 {
19108 objc_start_class_interface (name, super, protos);
19109 /* Handle instance variable declarations, if any. */
19110 cp_parser_objc_class_ivars (parser);
19111 objc_continue_interface ();
19112 }
19113
19114 cp_parser_objc_method_prototype_list (parser);
19115 }
19116
19117 /* Parse an Objective-C class implementation. */
19118
19119 static void
19120 cp_parser_objc_class_implementation (cp_parser* parser)
19121 {
19122 tree name, super, categ;
19123
19124 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19125 name = cp_parser_identifier (parser);
19126 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19127
19128 /* We have either a class or a category on our hands. */
19129 if (categ)
19130 objc_start_category_implementation (name, categ);
19131 else
19132 {
19133 objc_start_class_implementation (name, super);
19134 /* Handle instance variable declarations, if any. */
19135 cp_parser_objc_class_ivars (parser);
19136 objc_continue_implementation ();
19137 }
19138
19139 cp_parser_objc_method_definition_list (parser);
19140 }
19141
19142 /* Consume the @end token and finish off the implementation. */
19143
19144 static void
19145 cp_parser_objc_end_implementation (cp_parser* parser)
19146 {
19147 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19148 objc_finish_implementation ();
19149 }
19150
19151 /* Parse an Objective-C declaration. */
19152
19153 static void
19154 cp_parser_objc_declaration (cp_parser* parser)
19155 {
19156 /* Try to figure out what kind of declaration is present. */
19157 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19158
19159 switch (kwd->keyword)
19160 {
19161 case RID_AT_ALIAS:
19162 cp_parser_objc_alias_declaration (parser);
19163 break;
19164 case RID_AT_CLASS:
19165 cp_parser_objc_class_declaration (parser);
19166 break;
19167 case RID_AT_PROTOCOL:
19168 cp_parser_objc_protocol_declaration (parser);
19169 break;
19170 case RID_AT_INTERFACE:
19171 cp_parser_objc_class_interface (parser);
19172 break;
19173 case RID_AT_IMPLEMENTATION:
19174 cp_parser_objc_class_implementation (parser);
19175 break;
19176 case RID_AT_END:
19177 cp_parser_objc_end_implementation (parser);
19178 break;
19179 default:
19180 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19181 cp_parser_skip_to_end_of_block_or_statement (parser);
19182 }
19183 }
19184
19185 /* Parse an Objective-C try-catch-finally statement.
19186
19187 objc-try-catch-finally-stmt:
19188 @try compound-statement objc-catch-clause-seq [opt]
19189 objc-finally-clause [opt]
19190
19191 objc-catch-clause-seq:
19192 objc-catch-clause objc-catch-clause-seq [opt]
19193
19194 objc-catch-clause:
19195 @catch ( exception-declaration ) compound-statement
19196
19197 objc-finally-clause
19198 @finally compound-statement
19199
19200 Returns NULL_TREE. */
19201
19202 static tree
19203 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19204 location_t location;
19205 tree stmt;
19206
19207 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19208 location = cp_lexer_peek_token (parser->lexer)->location;
19209 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19210 node, lest it get absorbed into the surrounding block. */
19211 stmt = push_stmt_list ();
19212 cp_parser_compound_statement (parser, NULL, false);
19213 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19214
19215 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19216 {
19217 cp_parameter_declarator *parmdecl;
19218 tree parm;
19219
19220 cp_lexer_consume_token (parser->lexer);
19221 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19222 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19223 parm = grokdeclarator (parmdecl->declarator,
19224 &parmdecl->decl_specifiers,
19225 PARM, /*initialized=*/0,
19226 /*attrlist=*/NULL);
19227 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19228 objc_begin_catch_clause (parm);
19229 cp_parser_compound_statement (parser, NULL, false);
19230 objc_finish_catch_clause ();
19231 }
19232
19233 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19234 {
19235 cp_lexer_consume_token (parser->lexer);
19236 location = cp_lexer_peek_token (parser->lexer)->location;
19237 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19238 node, lest it get absorbed into the surrounding block. */
19239 stmt = push_stmt_list ();
19240 cp_parser_compound_statement (parser, NULL, false);
19241 objc_build_finally_clause (location, pop_stmt_list (stmt));
19242 }
19243
19244 return objc_finish_try_stmt ();
19245 }
19246
19247 /* Parse an Objective-C synchronized statement.
19248
19249 objc-synchronized-stmt:
19250 @synchronized ( expression ) compound-statement
19251
19252 Returns NULL_TREE. */
19253
19254 static tree
19255 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19256 location_t location;
19257 tree lock, stmt;
19258
19259 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19260
19261 location = cp_lexer_peek_token (parser->lexer)->location;
19262 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19263 lock = cp_parser_expression (parser, false);
19264 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19265
19266 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19267 node, lest it get absorbed into the surrounding block. */
19268 stmt = push_stmt_list ();
19269 cp_parser_compound_statement (parser, NULL, false);
19270
19271 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19272 }
19273
19274 /* Parse an Objective-C throw statement.
19275
19276 objc-throw-stmt:
19277 @throw assignment-expression [opt] ;
19278
19279 Returns a constructed '@throw' statement. */
19280
19281 static tree
19282 cp_parser_objc_throw_statement (cp_parser *parser) {
19283 tree expr = NULL_TREE;
19284
19285 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19286
19287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19288 expr = cp_parser_assignment_expression (parser, false);
19289
19290 cp_parser_consume_semicolon_at_end_of_statement (parser);
19291
19292 return objc_build_throw_stmt (expr);
19293 }
19294
19295 /* Parse an Objective-C statement. */
19296
19297 static tree
19298 cp_parser_objc_statement (cp_parser * parser) {
19299 /* Try to figure out what kind of declaration is present. */
19300 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19301
19302 switch (kwd->keyword)
19303 {
19304 case RID_AT_TRY:
19305 return cp_parser_objc_try_catch_finally_statement (parser);
19306 case RID_AT_SYNCHRONIZED:
19307 return cp_parser_objc_synchronized_statement (parser);
19308 case RID_AT_THROW:
19309 return cp_parser_objc_throw_statement (parser);
19310 default:
19311 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19312 cp_parser_skip_to_end_of_block_or_statement (parser);
19313 }
19314
19315 return error_mark_node;
19316 }
19317 \f
19318 /* OpenMP 2.5 parsing routines. */
19319
19320 /* Returns name of the next clause.
19321 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19322 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19323 returned and the token is consumed. */
19324
19325 static pragma_omp_clause
19326 cp_parser_omp_clause_name (cp_parser *parser)
19327 {
19328 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19329
19330 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19331 result = PRAGMA_OMP_CLAUSE_IF;
19332 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19333 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19334 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19335 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19336 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19337 {
19338 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19339 const char *p = IDENTIFIER_POINTER (id);
19340
19341 switch (p[0])
19342 {
19343 case 'c':
19344 if (!strcmp ("copyin", p))
19345 result = PRAGMA_OMP_CLAUSE_COPYIN;
19346 else if (!strcmp ("copyprivate", p))
19347 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19348 break;
19349 case 'f':
19350 if (!strcmp ("firstprivate", p))
19351 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19352 break;
19353 case 'l':
19354 if (!strcmp ("lastprivate", p))
19355 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19356 break;
19357 case 'n':
19358 if (!strcmp ("nowait", p))
19359 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19360 else if (!strcmp ("num_threads", p))
19361 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19362 break;
19363 case 'o':
19364 if (!strcmp ("ordered", p))
19365 result = PRAGMA_OMP_CLAUSE_ORDERED;
19366 break;
19367 case 'r':
19368 if (!strcmp ("reduction", p))
19369 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19370 break;
19371 case 's':
19372 if (!strcmp ("schedule", p))
19373 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19374 else if (!strcmp ("shared", p))
19375 result = PRAGMA_OMP_CLAUSE_SHARED;
19376 break;
19377 }
19378 }
19379
19380 if (result != PRAGMA_OMP_CLAUSE_NONE)
19381 cp_lexer_consume_token (parser->lexer);
19382
19383 return result;
19384 }
19385
19386 /* Validate that a clause of the given type does not already exist. */
19387
19388 static void
19389 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19390 {
19391 tree c;
19392
19393 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19394 if (OMP_CLAUSE_CODE (c) == code)
19395 {
19396 error ("too many %qs clauses", name);
19397 break;
19398 }
19399 }
19400
19401 /* OpenMP 2.5:
19402 variable-list:
19403 identifier
19404 variable-list , identifier
19405
19406 In addition, we match a closing parenthesis. An opening parenthesis
19407 will have been consumed by the caller.
19408
19409 If KIND is nonzero, create the appropriate node and install the decl
19410 in OMP_CLAUSE_DECL and add the node to the head of the list.
19411
19412 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19413 return the list created. */
19414
19415 static tree
19416 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19417 tree list)
19418 {
19419 while (1)
19420 {
19421 tree name, decl;
19422
19423 name = cp_parser_id_expression (parser, /*template_p=*/false,
19424 /*check_dependency_p=*/true,
19425 /*template_p=*/NULL,
19426 /*declarator_p=*/false,
19427 /*optional_p=*/false);
19428 if (name == error_mark_node)
19429 goto skip_comma;
19430
19431 decl = cp_parser_lookup_name_simple (parser, name);
19432 if (decl == error_mark_node)
19433 cp_parser_name_lookup_error (parser, name, decl, NULL);
19434 else if (kind != 0)
19435 {
19436 tree u = build_omp_clause (kind);
19437 OMP_CLAUSE_DECL (u) = decl;
19438 OMP_CLAUSE_CHAIN (u) = list;
19439 list = u;
19440 }
19441 else
19442 list = tree_cons (decl, NULL_TREE, list);
19443
19444 get_comma:
19445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19446 break;
19447 cp_lexer_consume_token (parser->lexer);
19448 }
19449
19450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19451 {
19452 int ending;
19453
19454 /* Try to resync to an unnested comma. Copied from
19455 cp_parser_parenthesized_expression_list. */
19456 skip_comma:
19457 ending = cp_parser_skip_to_closing_parenthesis (parser,
19458 /*recovering=*/true,
19459 /*or_comma=*/true,
19460 /*consume_paren=*/true);
19461 if (ending < 0)
19462 goto get_comma;
19463 }
19464
19465 return list;
19466 }
19467
19468 /* Similarly, but expect leading and trailing parenthesis. This is a very
19469 common case for omp clauses. */
19470
19471 static tree
19472 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19473 {
19474 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19475 return cp_parser_omp_var_list_no_open (parser, kind, list);
19476 return list;
19477 }
19478
19479 /* OpenMP 2.5:
19480 default ( shared | none ) */
19481
19482 static tree
19483 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19484 {
19485 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19486 tree c;
19487
19488 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19489 return list;
19490 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19491 {
19492 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19493 const char *p = IDENTIFIER_POINTER (id);
19494
19495 switch (p[0])
19496 {
19497 case 'n':
19498 if (strcmp ("none", p) != 0)
19499 goto invalid_kind;
19500 kind = OMP_CLAUSE_DEFAULT_NONE;
19501 break;
19502
19503 case 's':
19504 if (strcmp ("shared", p) != 0)
19505 goto invalid_kind;
19506 kind = OMP_CLAUSE_DEFAULT_SHARED;
19507 break;
19508
19509 default:
19510 goto invalid_kind;
19511 }
19512
19513 cp_lexer_consume_token (parser->lexer);
19514 }
19515 else
19516 {
19517 invalid_kind:
19518 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19519 }
19520
19521 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19523 /*or_comma=*/false,
19524 /*consume_paren=*/true);
19525
19526 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19527 return list;
19528
19529 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19530 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19531 OMP_CLAUSE_CHAIN (c) = list;
19532 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19533
19534 return c;
19535 }
19536
19537 /* OpenMP 2.5:
19538 if ( expression ) */
19539
19540 static tree
19541 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19542 {
19543 tree t, c;
19544
19545 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19546 return list;
19547
19548 t = cp_parser_condition (parser);
19549
19550 if (t == error_mark_node
19551 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19552 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19553 /*or_comma=*/false,
19554 /*consume_paren=*/true);
19555
19556 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19557
19558 c = build_omp_clause (OMP_CLAUSE_IF);
19559 OMP_CLAUSE_IF_EXPR (c) = t;
19560 OMP_CLAUSE_CHAIN (c) = list;
19561
19562 return c;
19563 }
19564
19565 /* OpenMP 2.5:
19566 nowait */
19567
19568 static tree
19569 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19570 {
19571 tree c;
19572
19573 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19574
19575 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19576 OMP_CLAUSE_CHAIN (c) = list;
19577 return c;
19578 }
19579
19580 /* OpenMP 2.5:
19581 num_threads ( expression ) */
19582
19583 static tree
19584 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19585 {
19586 tree t, c;
19587
19588 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19589 return list;
19590
19591 t = cp_parser_expression (parser, false);
19592
19593 if (t == error_mark_node
19594 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19595 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19596 /*or_comma=*/false,
19597 /*consume_paren=*/true);
19598
19599 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19600
19601 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19602 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19603 OMP_CLAUSE_CHAIN (c) = list;
19604
19605 return c;
19606 }
19607
19608 /* OpenMP 2.5:
19609 ordered */
19610
19611 static tree
19612 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19613 {
19614 tree c;
19615
19616 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19617
19618 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19619 OMP_CLAUSE_CHAIN (c) = list;
19620 return c;
19621 }
19622
19623 /* OpenMP 2.5:
19624 reduction ( reduction-operator : variable-list )
19625
19626 reduction-operator:
19627 One of: + * - & ^ | && || */
19628
19629 static tree
19630 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19631 {
19632 enum tree_code code;
19633 tree nlist, c;
19634
19635 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19636 return list;
19637
19638 switch (cp_lexer_peek_token (parser->lexer)->type)
19639 {
19640 case CPP_PLUS:
19641 code = PLUS_EXPR;
19642 break;
19643 case CPP_MULT:
19644 code = MULT_EXPR;
19645 break;
19646 case CPP_MINUS:
19647 code = MINUS_EXPR;
19648 break;
19649 case CPP_AND:
19650 code = BIT_AND_EXPR;
19651 break;
19652 case CPP_XOR:
19653 code = BIT_XOR_EXPR;
19654 break;
19655 case CPP_OR:
19656 code = BIT_IOR_EXPR;
19657 break;
19658 case CPP_AND_AND:
19659 code = TRUTH_ANDIF_EXPR;
19660 break;
19661 case CPP_OR_OR:
19662 code = TRUTH_ORIF_EXPR;
19663 break;
19664 default:
19665 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19666 resync_fail:
19667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19668 /*or_comma=*/false,
19669 /*consume_paren=*/true);
19670 return list;
19671 }
19672 cp_lexer_consume_token (parser->lexer);
19673
19674 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19675 goto resync_fail;
19676
19677 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19678 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19679 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19680
19681 return nlist;
19682 }
19683
19684 /* OpenMP 2.5:
19685 schedule ( schedule-kind )
19686 schedule ( schedule-kind , expression )
19687
19688 schedule-kind:
19689 static | dynamic | guided | runtime */
19690
19691 static tree
19692 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19693 {
19694 tree c, t;
19695
19696 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19697 return list;
19698
19699 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19700
19701 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19702 {
19703 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19704 const char *p = IDENTIFIER_POINTER (id);
19705
19706 switch (p[0])
19707 {
19708 case 'd':
19709 if (strcmp ("dynamic", p) != 0)
19710 goto invalid_kind;
19711 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19712 break;
19713
19714 case 'g':
19715 if (strcmp ("guided", p) != 0)
19716 goto invalid_kind;
19717 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19718 break;
19719
19720 case 'r':
19721 if (strcmp ("runtime", p) != 0)
19722 goto invalid_kind;
19723 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19724 break;
19725
19726 default:
19727 goto invalid_kind;
19728 }
19729 }
19730 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19731 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19732 else
19733 goto invalid_kind;
19734 cp_lexer_consume_token (parser->lexer);
19735
19736 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19737 {
19738 cp_lexer_consume_token (parser->lexer);
19739
19740 t = cp_parser_assignment_expression (parser, false);
19741
19742 if (t == error_mark_node)
19743 goto resync_fail;
19744 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19745 error ("schedule %<runtime%> does not take "
19746 "a %<chunk_size%> parameter");
19747 else
19748 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19749
19750 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19751 goto resync_fail;
19752 }
19753 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19754 goto resync_fail;
19755
19756 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19757 OMP_CLAUSE_CHAIN (c) = list;
19758 return c;
19759
19760 invalid_kind:
19761 cp_parser_error (parser, "invalid schedule kind");
19762 resync_fail:
19763 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19764 /*or_comma=*/false,
19765 /*consume_paren=*/true);
19766 return list;
19767 }
19768
19769 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19770 is a bitmask in MASK. Return the list of clauses found; the result
19771 of clause default goes in *pdefault. */
19772
19773 static tree
19774 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19775 const char *where, cp_token *pragma_tok)
19776 {
19777 tree clauses = NULL;
19778 bool first = true;
19779
19780 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19781 {
19782 pragma_omp_clause c_kind;
19783 const char *c_name;
19784 tree prev = clauses;
19785
19786 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19787 cp_lexer_consume_token (parser->lexer);
19788
19789 c_kind = cp_parser_omp_clause_name (parser);
19790 first = false;
19791
19792 switch (c_kind)
19793 {
19794 case PRAGMA_OMP_CLAUSE_COPYIN:
19795 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19796 c_name = "copyin";
19797 break;
19798 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19800 clauses);
19801 c_name = "copyprivate";
19802 break;
19803 case PRAGMA_OMP_CLAUSE_DEFAULT:
19804 clauses = cp_parser_omp_clause_default (parser, clauses);
19805 c_name = "default";
19806 break;
19807 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19808 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19809 clauses);
19810 c_name = "firstprivate";
19811 break;
19812 case PRAGMA_OMP_CLAUSE_IF:
19813 clauses = cp_parser_omp_clause_if (parser, clauses);
19814 c_name = "if";
19815 break;
19816 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19817 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19818 clauses);
19819 c_name = "lastprivate";
19820 break;
19821 case PRAGMA_OMP_CLAUSE_NOWAIT:
19822 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19823 c_name = "nowait";
19824 break;
19825 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19826 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19827 c_name = "num_threads";
19828 break;
19829 case PRAGMA_OMP_CLAUSE_ORDERED:
19830 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19831 c_name = "ordered";
19832 break;
19833 case PRAGMA_OMP_CLAUSE_PRIVATE:
19834 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19835 clauses);
19836 c_name = "private";
19837 break;
19838 case PRAGMA_OMP_CLAUSE_REDUCTION:
19839 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19840 c_name = "reduction";
19841 break;
19842 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19843 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19844 c_name = "schedule";
19845 break;
19846 case PRAGMA_OMP_CLAUSE_SHARED:
19847 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19848 clauses);
19849 c_name = "shared";
19850 break;
19851 default:
19852 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19853 goto saw_error;
19854 }
19855
19856 if (((mask >> c_kind) & 1) == 0)
19857 {
19858 /* Remove the invalid clause(s) from the list to avoid
19859 confusing the rest of the compiler. */
19860 clauses = prev;
19861 error ("%qs is not valid for %qs", c_name, where);
19862 }
19863 }
19864 saw_error:
19865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19866 return finish_omp_clauses (clauses);
19867 }
19868
19869 /* OpenMP 2.5:
19870 structured-block:
19871 statement
19872
19873 In practice, we're also interested in adding the statement to an
19874 outer node. So it is convenient if we work around the fact that
19875 cp_parser_statement calls add_stmt. */
19876
19877 static unsigned
19878 cp_parser_begin_omp_structured_block (cp_parser *parser)
19879 {
19880 unsigned save = parser->in_statement;
19881
19882 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19883 This preserves the "not within loop or switch" style error messages
19884 for nonsense cases like
19885 void foo() {
19886 #pragma omp single
19887 break;
19888 }
19889 */
19890 if (parser->in_statement)
19891 parser->in_statement = IN_OMP_BLOCK;
19892
19893 return save;
19894 }
19895
19896 static void
19897 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19898 {
19899 parser->in_statement = save;
19900 }
19901
19902 static tree
19903 cp_parser_omp_structured_block (cp_parser *parser)
19904 {
19905 tree stmt = begin_omp_structured_block ();
19906 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19907
19908 cp_parser_statement (parser, NULL_TREE, false, NULL);
19909
19910 cp_parser_end_omp_structured_block (parser, save);
19911 return finish_omp_structured_block (stmt);
19912 }
19913
19914 /* OpenMP 2.5:
19915 # pragma omp atomic new-line
19916 expression-stmt
19917
19918 expression-stmt:
19919 x binop= expr | x++ | ++x | x-- | --x
19920 binop:
19921 +, *, -, /, &, ^, |, <<, >>
19922
19923 where x is an lvalue expression with scalar type. */
19924
19925 static void
19926 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19927 {
19928 tree lhs, rhs;
19929 enum tree_code code;
19930
19931 cp_parser_require_pragma_eol (parser, pragma_tok);
19932
19933 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19934 /*cast_p=*/false);
19935 switch (TREE_CODE (lhs))
19936 {
19937 case ERROR_MARK:
19938 goto saw_error;
19939
19940 case PREINCREMENT_EXPR:
19941 case POSTINCREMENT_EXPR:
19942 lhs = TREE_OPERAND (lhs, 0);
19943 code = PLUS_EXPR;
19944 rhs = integer_one_node;
19945 break;
19946
19947 case PREDECREMENT_EXPR:
19948 case POSTDECREMENT_EXPR:
19949 lhs = TREE_OPERAND (lhs, 0);
19950 code = MINUS_EXPR;
19951 rhs = integer_one_node;
19952 break;
19953
19954 default:
19955 switch (cp_lexer_peek_token (parser->lexer)->type)
19956 {
19957 case CPP_MULT_EQ:
19958 code = MULT_EXPR;
19959 break;
19960 case CPP_DIV_EQ:
19961 code = TRUNC_DIV_EXPR;
19962 break;
19963 case CPP_PLUS_EQ:
19964 code = PLUS_EXPR;
19965 break;
19966 case CPP_MINUS_EQ:
19967 code = MINUS_EXPR;
19968 break;
19969 case CPP_LSHIFT_EQ:
19970 code = LSHIFT_EXPR;
19971 break;
19972 case CPP_RSHIFT_EQ:
19973 code = RSHIFT_EXPR;
19974 break;
19975 case CPP_AND_EQ:
19976 code = BIT_AND_EXPR;
19977 break;
19978 case CPP_OR_EQ:
19979 code = BIT_IOR_EXPR;
19980 break;
19981 case CPP_XOR_EQ:
19982 code = BIT_XOR_EXPR;
19983 break;
19984 default:
19985 cp_parser_error (parser,
19986 "invalid operator for %<#pragma omp atomic%>");
19987 goto saw_error;
19988 }
19989 cp_lexer_consume_token (parser->lexer);
19990
19991 rhs = cp_parser_expression (parser, false);
19992 if (rhs == error_mark_node)
19993 goto saw_error;
19994 break;
19995 }
19996 finish_omp_atomic (code, lhs, rhs);
19997 cp_parser_consume_semicolon_at_end_of_statement (parser);
19998 return;
19999
20000 saw_error:
20001 cp_parser_skip_to_end_of_block_or_statement (parser);
20002 }
20003
20004
20005 /* OpenMP 2.5:
20006 # pragma omp barrier new-line */
20007
20008 static void
20009 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20010 {
20011 cp_parser_require_pragma_eol (parser, pragma_tok);
20012 finish_omp_barrier ();
20013 }
20014
20015 /* OpenMP 2.5:
20016 # pragma omp critical [(name)] new-line
20017 structured-block */
20018
20019 static tree
20020 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20021 {
20022 tree stmt, name = NULL;
20023
20024 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20025 {
20026 cp_lexer_consume_token (parser->lexer);
20027
20028 name = cp_parser_identifier (parser);
20029
20030 if (name == error_mark_node
20031 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20033 /*or_comma=*/false,
20034 /*consume_paren=*/true);
20035 if (name == error_mark_node)
20036 name = NULL;
20037 }
20038 cp_parser_require_pragma_eol (parser, pragma_tok);
20039
20040 stmt = cp_parser_omp_structured_block (parser);
20041 return c_finish_omp_critical (stmt, name);
20042 }
20043
20044 /* OpenMP 2.5:
20045 # pragma omp flush flush-vars[opt] new-line
20046
20047 flush-vars:
20048 ( variable-list ) */
20049
20050 static void
20051 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20052 {
20053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20054 (void) cp_parser_omp_var_list (parser, 0, NULL);
20055 cp_parser_require_pragma_eol (parser, pragma_tok);
20056
20057 finish_omp_flush ();
20058 }
20059
20060 /* Parse the restricted form of the for statment allowed by OpenMP. */
20061
20062 static tree
20063 cp_parser_omp_for_loop (cp_parser *parser)
20064 {
20065 tree init, cond, incr, body, decl, pre_body;
20066 location_t loc;
20067
20068 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20069 {
20070 cp_parser_error (parser, "for statement expected");
20071 return NULL;
20072 }
20073 loc = cp_lexer_consume_token (parser->lexer)->location;
20074 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20075 return NULL;
20076
20077 init = decl = NULL;
20078 pre_body = push_stmt_list ();
20079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20080 {
20081 cp_decl_specifier_seq type_specifiers;
20082
20083 /* First, try to parse as an initialized declaration. See
20084 cp_parser_condition, from whence the bulk of this is copied. */
20085
20086 cp_parser_parse_tentatively (parser);
20087 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20088 &type_specifiers);
20089 if (!cp_parser_error_occurred (parser))
20090 {
20091 tree asm_specification, attributes;
20092 cp_declarator *declarator;
20093
20094 declarator = cp_parser_declarator (parser,
20095 CP_PARSER_DECLARATOR_NAMED,
20096 /*ctor_dtor_or_conv_p=*/NULL,
20097 /*parenthesized_p=*/NULL,
20098 /*member_p=*/false);
20099 attributes = cp_parser_attributes_opt (parser);
20100 asm_specification = cp_parser_asm_specification_opt (parser);
20101
20102 cp_parser_require (parser, CPP_EQ, "`='");
20103 if (cp_parser_parse_definitely (parser))
20104 {
20105 tree pushed_scope;
20106
20107 decl = start_decl (declarator, &type_specifiers,
20108 /*initialized_p=*/false, attributes,
20109 /*prefix_attributes=*/NULL_TREE,
20110 &pushed_scope);
20111
20112 init = cp_parser_assignment_expression (parser, false);
20113
20114 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20115 init = error_mark_node;
20116 else
20117 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20118 asm_specification, LOOKUP_ONLYCONVERTING);
20119
20120 if (pushed_scope)
20121 pop_scope (pushed_scope);
20122 }
20123 }
20124 else
20125 cp_parser_abort_tentative_parse (parser);
20126
20127 /* If parsing as an initialized declaration failed, try again as
20128 a simple expression. */
20129 if (decl == NULL)
20130 init = cp_parser_expression (parser, false);
20131 }
20132 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20133 pre_body = pop_stmt_list (pre_body);
20134
20135 cond = NULL;
20136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20137 cond = cp_parser_condition (parser);
20138 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20139
20140 incr = NULL;
20141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20142 incr = cp_parser_expression (parser, false);
20143
20144 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20145 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20146 /*or_comma=*/false,
20147 /*consume_paren=*/true);
20148
20149 /* Note that we saved the original contents of this flag when we entered
20150 the structured block, and so we don't need to re-save it here. */
20151 parser->in_statement = IN_OMP_FOR;
20152
20153 /* Note that the grammar doesn't call for a structured block here,
20154 though the loop as a whole is a structured block. */
20155 body = push_stmt_list ();
20156 cp_parser_statement (parser, NULL_TREE, false, NULL);
20157 body = pop_stmt_list (body);
20158
20159 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20160 }
20161
20162 /* OpenMP 2.5:
20163 #pragma omp for for-clause[optseq] new-line
20164 for-loop */
20165
20166 #define OMP_FOR_CLAUSE_MASK \
20167 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20168 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20169 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20170 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20171 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20172 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20173 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20174
20175 static tree
20176 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20177 {
20178 tree clauses, sb, ret;
20179 unsigned int save;
20180
20181 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20182 "#pragma omp for", pragma_tok);
20183
20184 sb = begin_omp_structured_block ();
20185 save = cp_parser_begin_omp_structured_block (parser);
20186
20187 ret = cp_parser_omp_for_loop (parser);
20188 if (ret)
20189 OMP_FOR_CLAUSES (ret) = clauses;
20190
20191 cp_parser_end_omp_structured_block (parser, save);
20192 add_stmt (finish_omp_structured_block (sb));
20193
20194 return ret;
20195 }
20196
20197 /* OpenMP 2.5:
20198 # pragma omp master new-line
20199 structured-block */
20200
20201 static tree
20202 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20203 {
20204 cp_parser_require_pragma_eol (parser, pragma_tok);
20205 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20206 }
20207
20208 /* OpenMP 2.5:
20209 # pragma omp ordered new-line
20210 structured-block */
20211
20212 static tree
20213 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20214 {
20215 cp_parser_require_pragma_eol (parser, pragma_tok);
20216 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20217 }
20218
20219 /* OpenMP 2.5:
20220
20221 section-scope:
20222 { section-sequence }
20223
20224 section-sequence:
20225 section-directive[opt] structured-block
20226 section-sequence section-directive structured-block */
20227
20228 static tree
20229 cp_parser_omp_sections_scope (cp_parser *parser)
20230 {
20231 tree stmt, substmt;
20232 bool error_suppress = false;
20233 cp_token *tok;
20234
20235 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20236 return NULL_TREE;
20237
20238 stmt = push_stmt_list ();
20239
20240 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20241 {
20242 unsigned save;
20243
20244 substmt = begin_omp_structured_block ();
20245 save = cp_parser_begin_omp_structured_block (parser);
20246
20247 while (1)
20248 {
20249 cp_parser_statement (parser, NULL_TREE, false, NULL);
20250
20251 tok = cp_lexer_peek_token (parser->lexer);
20252 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20253 break;
20254 if (tok->type == CPP_CLOSE_BRACE)
20255 break;
20256 if (tok->type == CPP_EOF)
20257 break;
20258 }
20259
20260 cp_parser_end_omp_structured_block (parser, save);
20261 substmt = finish_omp_structured_block (substmt);
20262 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20263 add_stmt (substmt);
20264 }
20265
20266 while (1)
20267 {
20268 tok = cp_lexer_peek_token (parser->lexer);
20269 if (tok->type == CPP_CLOSE_BRACE)
20270 break;
20271 if (tok->type == CPP_EOF)
20272 break;
20273
20274 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20275 {
20276 cp_lexer_consume_token (parser->lexer);
20277 cp_parser_require_pragma_eol (parser, tok);
20278 error_suppress = false;
20279 }
20280 else if (!error_suppress)
20281 {
20282 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20283 error_suppress = true;
20284 }
20285
20286 substmt = cp_parser_omp_structured_block (parser);
20287 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20288 add_stmt (substmt);
20289 }
20290 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20291
20292 substmt = pop_stmt_list (stmt);
20293
20294 stmt = make_node (OMP_SECTIONS);
20295 TREE_TYPE (stmt) = void_type_node;
20296 OMP_SECTIONS_BODY (stmt) = substmt;
20297
20298 add_stmt (stmt);
20299 return stmt;
20300 }
20301
20302 /* OpenMP 2.5:
20303 # pragma omp sections sections-clause[optseq] newline
20304 sections-scope */
20305
20306 #define OMP_SECTIONS_CLAUSE_MASK \
20307 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20308 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20309 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20310 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20311 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20312
20313 static tree
20314 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20315 {
20316 tree clauses, ret;
20317
20318 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20319 "#pragma omp sections", pragma_tok);
20320
20321 ret = cp_parser_omp_sections_scope (parser);
20322 if (ret)
20323 OMP_SECTIONS_CLAUSES (ret) = clauses;
20324
20325 return ret;
20326 }
20327
20328 /* OpenMP 2.5:
20329 # pragma parallel parallel-clause new-line
20330 # pragma parallel for parallel-for-clause new-line
20331 # pragma parallel sections parallel-sections-clause new-line */
20332
20333 #define OMP_PARALLEL_CLAUSE_MASK \
20334 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20335 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20336 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20337 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20338 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20339 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20340 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20341 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20342
20343 static tree
20344 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20345 {
20346 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20347 const char *p_name = "#pragma omp parallel";
20348 tree stmt, clauses, par_clause, ws_clause, block;
20349 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20350 unsigned int save;
20351
20352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20353 {
20354 cp_lexer_consume_token (parser->lexer);
20355 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20356 p_name = "#pragma omp parallel for";
20357 mask |= OMP_FOR_CLAUSE_MASK;
20358 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20359 }
20360 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20361 {
20362 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20363 const char *p = IDENTIFIER_POINTER (id);
20364 if (strcmp (p, "sections") == 0)
20365 {
20366 cp_lexer_consume_token (parser->lexer);
20367 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20368 p_name = "#pragma omp parallel sections";
20369 mask |= OMP_SECTIONS_CLAUSE_MASK;
20370 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20371 }
20372 }
20373
20374 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20375 block = begin_omp_parallel ();
20376 save = cp_parser_begin_omp_structured_block (parser);
20377
20378 switch (p_kind)
20379 {
20380 case PRAGMA_OMP_PARALLEL:
20381 cp_parser_statement (parser, NULL_TREE, false, NULL);
20382 par_clause = clauses;
20383 break;
20384
20385 case PRAGMA_OMP_PARALLEL_FOR:
20386 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20387 stmt = cp_parser_omp_for_loop (parser);
20388 if (stmt)
20389 OMP_FOR_CLAUSES (stmt) = ws_clause;
20390 break;
20391
20392 case PRAGMA_OMP_PARALLEL_SECTIONS:
20393 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20394 stmt = cp_parser_omp_sections_scope (parser);
20395 if (stmt)
20396 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20397 break;
20398
20399 default:
20400 gcc_unreachable ();
20401 }
20402
20403 cp_parser_end_omp_structured_block (parser, save);
20404 stmt = finish_omp_parallel (par_clause, block);
20405 if (p_kind != PRAGMA_OMP_PARALLEL)
20406 OMP_PARALLEL_COMBINED (stmt) = 1;
20407 return stmt;
20408 }
20409
20410 /* OpenMP 2.5:
20411 # pragma omp single single-clause[optseq] new-line
20412 structured-block */
20413
20414 #define OMP_SINGLE_CLAUSE_MASK \
20415 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20416 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20417 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20418 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20419
20420 static tree
20421 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20422 {
20423 tree stmt = make_node (OMP_SINGLE);
20424 TREE_TYPE (stmt) = void_type_node;
20425
20426 OMP_SINGLE_CLAUSES (stmt)
20427 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20428 "#pragma omp single", pragma_tok);
20429 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20430
20431 return add_stmt (stmt);
20432 }
20433
20434 /* OpenMP 2.5:
20435 # pragma omp threadprivate (variable-list) */
20436
20437 static void
20438 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20439 {
20440 tree vars;
20441
20442 vars = cp_parser_omp_var_list (parser, 0, NULL);
20443 cp_parser_require_pragma_eol (parser, pragma_tok);
20444
20445 finish_omp_threadprivate (vars);
20446 }
20447
20448 /* Main entry point to OpenMP statement pragmas. */
20449
20450 static void
20451 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20452 {
20453 tree stmt;
20454
20455 switch (pragma_tok->pragma_kind)
20456 {
20457 case PRAGMA_OMP_ATOMIC:
20458 cp_parser_omp_atomic (parser, pragma_tok);
20459 return;
20460 case PRAGMA_OMP_CRITICAL:
20461 stmt = cp_parser_omp_critical (parser, pragma_tok);
20462 break;
20463 case PRAGMA_OMP_FOR:
20464 stmt = cp_parser_omp_for (parser, pragma_tok);
20465 break;
20466 case PRAGMA_OMP_MASTER:
20467 stmt = cp_parser_omp_master (parser, pragma_tok);
20468 break;
20469 case PRAGMA_OMP_ORDERED:
20470 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20471 break;
20472 case PRAGMA_OMP_PARALLEL:
20473 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20474 break;
20475 case PRAGMA_OMP_SECTIONS:
20476 stmt = cp_parser_omp_sections (parser, pragma_tok);
20477 break;
20478 case PRAGMA_OMP_SINGLE:
20479 stmt = cp_parser_omp_single (parser, pragma_tok);
20480 break;
20481 default:
20482 gcc_unreachable ();
20483 }
20484
20485 if (stmt)
20486 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20487 }
20488 \f
20489 /* The parser. */
20490
20491 static GTY (()) cp_parser *the_parser;
20492
20493 \f
20494 /* Special handling for the first token or line in the file. The first
20495 thing in the file might be #pragma GCC pch_preprocess, which loads a
20496 PCH file, which is a GC collection point. So we need to handle this
20497 first pragma without benefit of an existing lexer structure.
20498
20499 Always returns one token to the caller in *FIRST_TOKEN. This is
20500 either the true first token of the file, or the first token after
20501 the initial pragma. */
20502
20503 static void
20504 cp_parser_initial_pragma (cp_token *first_token)
20505 {
20506 tree name = NULL;
20507
20508 cp_lexer_get_preprocessor_token (NULL, first_token);
20509 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20510 return;
20511
20512 cp_lexer_get_preprocessor_token (NULL, first_token);
20513 if (first_token->type == CPP_STRING)
20514 {
20515 name = first_token->u.value;
20516
20517 cp_lexer_get_preprocessor_token (NULL, first_token);
20518 if (first_token->type != CPP_PRAGMA_EOL)
20519 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20520 }
20521 else
20522 error ("expected string literal");
20523
20524 /* Skip to the end of the pragma. */
20525 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20526 cp_lexer_get_preprocessor_token (NULL, first_token);
20527
20528 /* Now actually load the PCH file. */
20529 if (name)
20530 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20531
20532 /* Read one more token to return to our caller. We have to do this
20533 after reading the PCH file in, since its pointers have to be
20534 live. */
20535 cp_lexer_get_preprocessor_token (NULL, first_token);
20536 }
20537
20538 /* Normal parsing of a pragma token. Here we can (and must) use the
20539 regular lexer. */
20540
20541 static bool
20542 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20543 {
20544 cp_token *pragma_tok;
20545 unsigned int id;
20546
20547 pragma_tok = cp_lexer_consume_token (parser->lexer);
20548 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20549 parser->lexer->in_pragma = true;
20550
20551 id = pragma_tok->pragma_kind;
20552 switch (id)
20553 {
20554 case PRAGMA_GCC_PCH_PREPROCESS:
20555 error ("%<#pragma GCC pch_preprocess%> must be first");
20556 break;
20557
20558 case PRAGMA_OMP_BARRIER:
20559 switch (context)
20560 {
20561 case pragma_compound:
20562 cp_parser_omp_barrier (parser, pragma_tok);
20563 return false;
20564 case pragma_stmt:
20565 error ("%<#pragma omp barrier%> may only be "
20566 "used in compound statements");
20567 break;
20568 default:
20569 goto bad_stmt;
20570 }
20571 break;
20572
20573 case PRAGMA_OMP_FLUSH:
20574 switch (context)
20575 {
20576 case pragma_compound:
20577 cp_parser_omp_flush (parser, pragma_tok);
20578 return false;
20579 case pragma_stmt:
20580 error ("%<#pragma omp flush%> may only be "
20581 "used in compound statements");
20582 break;
20583 default:
20584 goto bad_stmt;
20585 }
20586 break;
20587
20588 case PRAGMA_OMP_THREADPRIVATE:
20589 cp_parser_omp_threadprivate (parser, pragma_tok);
20590 return false;
20591
20592 case PRAGMA_OMP_ATOMIC:
20593 case PRAGMA_OMP_CRITICAL:
20594 case PRAGMA_OMP_FOR:
20595 case PRAGMA_OMP_MASTER:
20596 case PRAGMA_OMP_ORDERED:
20597 case PRAGMA_OMP_PARALLEL:
20598 case PRAGMA_OMP_SECTIONS:
20599 case PRAGMA_OMP_SINGLE:
20600 if (context == pragma_external)
20601 goto bad_stmt;
20602 cp_parser_omp_construct (parser, pragma_tok);
20603 return true;
20604
20605 case PRAGMA_OMP_SECTION:
20606 error ("%<#pragma omp section%> may only be used in "
20607 "%<#pragma omp sections%> construct");
20608 break;
20609
20610 default:
20611 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20612 c_invoke_pragma_handler (id);
20613 break;
20614
20615 bad_stmt:
20616 cp_parser_error (parser, "expected declaration specifiers");
20617 break;
20618 }
20619
20620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20621 return false;
20622 }
20623
20624 /* The interface the pragma parsers have to the lexer. */
20625
20626 enum cpp_ttype
20627 pragma_lex (tree *value)
20628 {
20629 cp_token *tok;
20630 enum cpp_ttype ret;
20631
20632 tok = cp_lexer_peek_token (the_parser->lexer);
20633
20634 ret = tok->type;
20635 *value = tok->u.value;
20636
20637 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20638 ret = CPP_EOF;
20639 else if (ret == CPP_STRING)
20640 *value = cp_parser_string_literal (the_parser, false, false);
20641 else
20642 {
20643 cp_lexer_consume_token (the_parser->lexer);
20644 if (ret == CPP_KEYWORD)
20645 ret = CPP_NAME;
20646 }
20647
20648 return ret;
20649 }
20650
20651 \f
20652 /* External interface. */
20653
20654 /* Parse one entire translation unit. */
20655
20656 void
20657 c_parse_file (void)
20658 {
20659 bool error_occurred;
20660 static bool already_called = false;
20661
20662 if (already_called)
20663 {
20664 sorry ("inter-module optimizations not implemented for C++");
20665 return;
20666 }
20667 already_called = true;
20668
20669 the_parser = cp_parser_new ();
20670 push_deferring_access_checks (flag_access_control
20671 ? dk_no_deferred : dk_no_check);
20672 error_occurred = cp_parser_translation_unit (the_parser);
20673 the_parser = NULL;
20674 }
20675
20676 #include "gt-cp-parser.h"