]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
re PR c++/34267 (ICE applying __decltype to name of template class)
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 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 input file stack index at which this token was found. */
83 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84 /* The value associated with this token, if any. */
85 union cp_token_value {
86 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
87 struct tree_check* GTY((tag ("1"))) tree_check_value;
88 /* Use for all other tokens. */
89 tree GTY((tag ("0"))) value;
90 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91 /* The location at which this token was found. */
92 location_t location;
93 } cp_token;
94
95 /* We use a stack of token pointer for saving token sets. */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
99
100 static cp_token eof_token =
101 {
102 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
103 #if USE_MAPPED_LOCATION
104 0
105 #else
106 {0, 0}
107 #endif
108 };
109
110 /* The cp_lexer structure represents the C++ lexer. It is responsible
111 for managing the token stream from the preprocessor and supplying
112 it to the parser. Tokens are never added to the cp_lexer after
113 it is created. */
114
115 typedef struct cp_lexer GTY (())
116 {
117 /* The memory allocated for the buffer. NULL if this lexer does not
118 own the token buffer. */
119 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
120 /* If the lexer owns the buffer, this is the number of tokens in the
121 buffer. */
122 size_t buffer_length;
123
124 /* A pointer just past the last available token. The tokens
125 in this lexer are [buffer, last_token). */
126 cp_token_position GTY ((skip)) last_token;
127
128 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
129 no more available tokens. */
130 cp_token_position GTY ((skip)) next_token;
131
132 /* A stack indicating positions at which cp_lexer_save_tokens was
133 called. The top entry is the most recent position at which we
134 began saving tokens. If the stack is non-empty, we are saving
135 tokens. */
136 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
137
138 /* The next lexer in a linked list of lexers. */
139 struct cp_lexer *next;
140
141 /* True if we should output debugging information. */
142 bool debugging_p;
143
144 /* True if we're in the context of parsing a pragma, and should not
145 increment past the end-of-line marker. */
146 bool in_pragma;
147 } cp_lexer;
148
149 /* cp_token_cache is a range of tokens. There is no need to represent
150 allocate heap memory for it, since tokens are never removed from the
151 lexer's array. There is also no need for the GC to walk through
152 a cp_token_cache, since everything in here is referenced through
153 a lexer. */
154
155 typedef struct cp_token_cache GTY(())
156 {
157 /* The beginning of the token range. */
158 cp_token * GTY((skip)) first;
159
160 /* Points immediately after the last token in the range. */
161 cp_token * GTY ((skip)) last;
162 } cp_token_cache;
163
164 /* Prototypes. */
165
166 static cp_lexer *cp_lexer_new_main
167 (void);
168 static cp_lexer *cp_lexer_new_from_tokens
169 (cp_token_cache *tokens);
170 static void cp_lexer_destroy
171 (cp_lexer *);
172 static int cp_lexer_saving_tokens
173 (const cp_lexer *);
174 static cp_token_position cp_lexer_token_position
175 (cp_lexer *, bool);
176 static cp_token *cp_lexer_token_at
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_get_preprocessor_token
179 (cp_lexer *, cp_token *);
180 static inline cp_token *cp_lexer_peek_token
181 (cp_lexer *);
182 static cp_token *cp_lexer_peek_nth_token
183 (cp_lexer *, size_t);
184 static inline bool cp_lexer_next_token_is
185 (cp_lexer *, enum cpp_ttype);
186 static bool cp_lexer_next_token_is_not
187 (cp_lexer *, enum cpp_ttype);
188 static bool cp_lexer_next_token_is_keyword
189 (cp_lexer *, enum rid);
190 static cp_token *cp_lexer_consume_token
191 (cp_lexer *);
192 static void cp_lexer_purge_token
193 (cp_lexer *);
194 static void cp_lexer_purge_tokens_after
195 (cp_lexer *, cp_token_position);
196 static void cp_lexer_save_tokens
197 (cp_lexer *);
198 static void cp_lexer_commit_tokens
199 (cp_lexer *);
200 static void cp_lexer_rollback_tokens
201 (cp_lexer *);
202 #ifdef ENABLE_CHECKING
203 static void cp_lexer_print_token
204 (FILE *, cp_token *);
205 static inline bool cp_lexer_debugging_p
206 (cp_lexer *);
207 static void cp_lexer_start_debugging
208 (cp_lexer *) ATTRIBUTE_UNUSED;
209 static void cp_lexer_stop_debugging
210 (cp_lexer *) ATTRIBUTE_UNUSED;
211 #else
212 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
213 about passing NULL to functions that require non-NULL arguments
214 (fputs, fprintf). It will never be used, so all we need is a value
215 of the right type that's guaranteed not to be NULL. */
216 #define cp_lexer_debug_stream stdout
217 #define cp_lexer_print_token(str, tok) (void) 0
218 #define cp_lexer_debugging_p(lexer) 0
219 #endif /* ENABLE_CHECKING */
220
221 static cp_token_cache *cp_token_cache_new
222 (cp_token *, cp_token *);
223
224 static void cp_parser_initial_pragma
225 (cp_token *);
226
227 /* Manifest constants. */
228 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
229 #define CP_SAVED_TOKEN_STACK 5
230
231 /* A token type for keywords, as opposed to ordinary identifiers. */
232 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
233
234 /* A token type for template-ids. If a template-id is processed while
235 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
236 the value of the CPP_TEMPLATE_ID is whatever was returned by
237 cp_parser_template_id. */
238 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
239
240 /* A token type for nested-name-specifiers. If a
241 nested-name-specifier is processed while parsing tentatively, it is
242 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
243 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
244 cp_parser_nested_name_specifier_opt. */
245 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
246
247 /* A token type for tokens that are not tokens at all; these are used
248 to represent slots in the array where there used to be a token
249 that has now been deleted. */
250 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
251
252 /* The number of token types, including C++-specific ones. */
253 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
254
255 /* Variables. */
256
257 #ifdef ENABLE_CHECKING
258 /* The stream to which debugging output should be written. */
259 static FILE *cp_lexer_debug_stream;
260 #endif /* ENABLE_CHECKING */
261
262 /* Create a new main C++ lexer, the lexer that gets tokens from the
263 preprocessor. */
264
265 static cp_lexer *
266 cp_lexer_new_main (void)
267 {
268 cp_token first_token;
269 cp_lexer *lexer;
270 cp_token *pos;
271 size_t alloc;
272 size_t space;
273 cp_token *buffer;
274
275 /* It's possible that parsing the first pragma will load a PCH file,
276 which is a GC collection point. So we have to do that before
277 allocating any memory. */
278 cp_parser_initial_pragma (&first_token);
279
280 c_common_no_more_pch ();
281
282 /* Allocate the memory. */
283 lexer = GGC_CNEW (cp_lexer);
284
285 #ifdef ENABLE_CHECKING
286 /* Initially we are not debugging. */
287 lexer->debugging_p = false;
288 #endif /* ENABLE_CHECKING */
289 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
290 CP_SAVED_TOKEN_STACK);
291
292 /* Create the buffer. */
293 alloc = CP_LEXER_BUFFER_SIZE;
294 buffer = GGC_NEWVEC (cp_token, alloc);
295
296 /* Put the first token in the buffer. */
297 space = alloc;
298 pos = buffer;
299 *pos = first_token;
300
301 /* Get the remaining tokens from the preprocessor. */
302 while (pos->type != CPP_EOF)
303 {
304 pos++;
305 if (!--space)
306 {
307 space = alloc;
308 alloc *= 2;
309 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
310 pos = buffer + space;
311 }
312 cp_lexer_get_preprocessor_token (lexer, pos);
313 }
314 lexer->buffer = buffer;
315 lexer->buffer_length = alloc - space;
316 lexer->last_token = pos;
317 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
318
319 /* Subsequent preprocessor diagnostics should use compiler
320 diagnostic functions to get the compiler source location. */
321 cpp_get_options (parse_in)->client_diagnostic = true;
322 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
323
324 gcc_assert (lexer->next_token->type != CPP_PURGED);
325 return lexer;
326 }
327
328 /* Create a new lexer whose token stream is primed with the tokens in
329 CACHE. When these tokens are exhausted, no new tokens will be read. */
330
331 static cp_lexer *
332 cp_lexer_new_from_tokens (cp_token_cache *cache)
333 {
334 cp_token *first = cache->first;
335 cp_token *last = cache->last;
336 cp_lexer *lexer = GGC_CNEW (cp_lexer);
337
338 /* We do not own the buffer. */
339 lexer->buffer = NULL;
340 lexer->buffer_length = 0;
341 lexer->next_token = first == last ? &eof_token : first;
342 lexer->last_token = last;
343
344 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
345 CP_SAVED_TOKEN_STACK);
346
347 #ifdef ENABLE_CHECKING
348 /* Initially we are not debugging. */
349 lexer->debugging_p = false;
350 #endif
351
352 gcc_assert (lexer->next_token->type != CPP_PURGED);
353 return lexer;
354 }
355
356 /* Frees all resources associated with LEXER. */
357
358 static void
359 cp_lexer_destroy (cp_lexer *lexer)
360 {
361 if (lexer->buffer)
362 ggc_free (lexer->buffer);
363 VEC_free (cp_token_position, heap, lexer->saved_tokens);
364 ggc_free (lexer);
365 }
366
367 /* Returns nonzero if debugging information should be output. */
368
369 #ifdef ENABLE_CHECKING
370
371 static inline bool
372 cp_lexer_debugging_p (cp_lexer *lexer)
373 {
374 return lexer->debugging_p;
375 }
376
377 #endif /* ENABLE_CHECKING */
378
379 static inline cp_token_position
380 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
381 {
382 gcc_assert (!previous_p || lexer->next_token != &eof_token);
383
384 return lexer->next_token - previous_p;
385 }
386
387 static inline cp_token *
388 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
389 {
390 return pos;
391 }
392
393 /* nonzero if we are presently saving tokens. */
394
395 static inline int
396 cp_lexer_saving_tokens (const cp_lexer* lexer)
397 {
398 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
399 }
400
401 /* Store the next token from the preprocessor in *TOKEN. Return true
402 if we reach EOF. If LEXER is NULL, assume we are handling an
403 initial #pragma pch_preprocess, and thus want the lexer to return
404 processed strings. */
405
406 static void
407 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
408 {
409 static int is_extern_c = 0;
410
411 /* Get a new token from the preprocessor. */
412 token->type
413 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
414 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
415 token->input_file_stack_index = input_file_stack_tick;
416 token->keyword = RID_MAX;
417 token->pragma_kind = PRAGMA_NONE;
418 token->in_system_header = in_system_header;
419
420 /* On some systems, some header files are surrounded by an
421 implicit extern "C" block. Set a flag in the token if it
422 comes from such a header. */
423 is_extern_c += pending_lang_change;
424 pending_lang_change = 0;
425 token->implicit_extern_c = is_extern_c > 0;
426
427 /* Check to see if this token is a keyword. */
428 if (token->type == CPP_NAME)
429 {
430 if (C_IS_RESERVED_WORD (token->u.value))
431 {
432 /* Mark this token as a keyword. */
433 token->type = CPP_KEYWORD;
434 /* Record which keyword. */
435 token->keyword = C_RID_CODE (token->u.value);
436 /* Update the value. Some keywords are mapped to particular
437 entities, rather than simply having the value of the
438 corresponding IDENTIFIER_NODE. For example, `__const' is
439 mapped to `const'. */
440 token->u.value = ridpointers[token->keyword];
441 }
442 else
443 {
444 if (warn_cxx0x_compat
445 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
446 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
447 {
448 /* Warn about the C++0x keyword (but still treat it as
449 an identifier). */
450 warning (OPT_Wc__0x_compat,
451 "identifier %<%s%> will become a keyword in C++0x",
452 IDENTIFIER_POINTER (token->u.value));
453
454 /* Clear out the C_RID_CODE so we don't warn about this
455 particular identifier-turned-keyword again. */
456 C_RID_CODE (token->u.value) = RID_MAX;
457 }
458
459 token->ambiguous_p = false;
460 token->keyword = RID_MAX;
461 }
462 }
463 /* Handle Objective-C++ keywords. */
464 else if (token->type == CPP_AT_NAME)
465 {
466 token->type = CPP_KEYWORD;
467 switch (C_RID_CODE (token->u.value))
468 {
469 /* Map 'class' to '@class', 'private' to '@private', etc. */
470 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
471 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
472 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
473 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
474 case RID_THROW: token->keyword = RID_AT_THROW; break;
475 case RID_TRY: token->keyword = RID_AT_TRY; break;
476 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
477 default: token->keyword = C_RID_CODE (token->u.value);
478 }
479 }
480 else if (token->type == CPP_PRAGMA)
481 {
482 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
483 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
484 token->u.value = NULL_TREE;
485 }
486 }
487
488 /* Update the globals input_location and in_system_header and the
489 input file stack from TOKEN. */
490 static inline void
491 cp_lexer_set_source_position_from_token (cp_token *token)
492 {
493 if (token->type != CPP_EOF)
494 {
495 input_location = token->location;
496 in_system_header = token->in_system_header;
497 restore_input_file_stack (token->input_file_stack_index);
498 }
499 }
500
501 /* Return a pointer to the next token in the token stream, but do not
502 consume it. */
503
504 static inline cp_token *
505 cp_lexer_peek_token (cp_lexer *lexer)
506 {
507 if (cp_lexer_debugging_p (lexer))
508 {
509 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
510 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
511 putc ('\n', cp_lexer_debug_stream);
512 }
513 return lexer->next_token;
514 }
515
516 /* Return true if the next token has the indicated TYPE. */
517
518 static inline bool
519 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
520 {
521 return cp_lexer_peek_token (lexer)->type == type;
522 }
523
524 /* Return true if the next token does not have the indicated TYPE. */
525
526 static inline bool
527 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
528 {
529 return !cp_lexer_next_token_is (lexer, type);
530 }
531
532 /* Return true if the next token is the indicated KEYWORD. */
533
534 static inline bool
535 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
536 {
537 return cp_lexer_peek_token (lexer)->keyword == keyword;
538 }
539
540 /* Return true if the next token is a keyword for a decl-specifier. */
541
542 static bool
543 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
544 {
545 cp_token *token;
546
547 token = cp_lexer_peek_token (lexer);
548 switch (token->keyword)
549 {
550 /* Storage classes. */
551 case RID_AUTO:
552 case RID_REGISTER:
553 case RID_STATIC:
554 case RID_EXTERN:
555 case RID_MUTABLE:
556 case RID_THREAD:
557 /* Elaborated type specifiers. */
558 case RID_ENUM:
559 case RID_CLASS:
560 case RID_STRUCT:
561 case RID_UNION:
562 case RID_TYPENAME:
563 /* Simple type specifiers. */
564 case RID_CHAR:
565 case RID_WCHAR:
566 case RID_BOOL:
567 case RID_SHORT:
568 case RID_INT:
569 case RID_LONG:
570 case RID_SIGNED:
571 case RID_UNSIGNED:
572 case RID_FLOAT:
573 case RID_DOUBLE:
574 case RID_VOID:
575 /* GNU extensions. */
576 case RID_ATTRIBUTE:
577 case RID_TYPEOF:
578 /* C++0x extensions. */
579 case RID_DECLTYPE:
580 return true;
581
582 default:
583 return false;
584 }
585 }
586
587 /* Return a pointer to the Nth token in the token stream. If N is 1,
588 then this is precisely equivalent to cp_lexer_peek_token (except
589 that it is not inline). One would like to disallow that case, but
590 there is one case (cp_parser_nth_token_starts_template_id) where
591 the caller passes a variable for N and it might be 1. */
592
593 static cp_token *
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
595 {
596 cp_token *token;
597
598 /* N is 1-based, not zero-based. */
599 gcc_assert (n > 0);
600
601 if (cp_lexer_debugging_p (lexer))
602 fprintf (cp_lexer_debug_stream,
603 "cp_lexer: peeking ahead %ld at token: ", (long)n);
604
605 --n;
606 token = lexer->next_token;
607 gcc_assert (!n || token != &eof_token);
608 while (n != 0)
609 {
610 ++token;
611 if (token == lexer->last_token)
612 {
613 token = &eof_token;
614 break;
615 }
616
617 if (token->type != CPP_PURGED)
618 --n;
619 }
620
621 if (cp_lexer_debugging_p (lexer))
622 {
623 cp_lexer_print_token (cp_lexer_debug_stream, token);
624 putc ('\n', cp_lexer_debug_stream);
625 }
626
627 return token;
628 }
629
630 /* Return the next token, and advance the lexer's next_token pointer
631 to point to the next non-purged token. */
632
633 static cp_token *
634 cp_lexer_consume_token (cp_lexer* lexer)
635 {
636 cp_token *token = lexer->next_token;
637
638 gcc_assert (token != &eof_token);
639 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
640
641 do
642 {
643 lexer->next_token++;
644 if (lexer->next_token == lexer->last_token)
645 {
646 lexer->next_token = &eof_token;
647 break;
648 }
649
650 }
651 while (lexer->next_token->type == CPP_PURGED);
652
653 cp_lexer_set_source_position_from_token (token);
654
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
657 {
658 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659 cp_lexer_print_token (cp_lexer_debug_stream, token);
660 putc ('\n', cp_lexer_debug_stream);
661 }
662
663 return token;
664 }
665
666 /* Permanently remove the next token from the token stream, and
667 advance the next_token pointer to refer to the next non-purged
668 token. */
669
670 static void
671 cp_lexer_purge_token (cp_lexer *lexer)
672 {
673 cp_token *tok = lexer->next_token;
674
675 gcc_assert (tok != &eof_token);
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->u.value = NULL_TREE;
679 tok->keyword = RID_MAX;
680
681 do
682 {
683 tok++;
684 if (tok == lexer->last_token)
685 {
686 tok = &eof_token;
687 break;
688 }
689 }
690 while (tok->type == CPP_PURGED);
691 lexer->next_token = tok;
692 }
693
694 /* Permanently remove all tokens after TOK, up to, but not
695 including, the token that will be returned next by
696 cp_lexer_peek_token. */
697
698 static void
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 {
701 cp_token *peek = lexer->next_token;
702
703 if (peek == &eof_token)
704 peek = lexer->last_token;
705
706 gcc_assert (tok < peek);
707
708 for ( tok += 1; tok != peek; tok += 1)
709 {
710 tok->type = CPP_PURGED;
711 tok->location = UNKNOWN_LOCATION;
712 tok->u.value = NULL_TREE;
713 tok->keyword = RID_MAX;
714 }
715 }
716
717 /* Begin saving tokens. All tokens consumed after this point will be
718 preserved. */
719
720 static void
721 cp_lexer_save_tokens (cp_lexer* lexer)
722 {
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726
727 VEC_safe_push (cp_token_position, heap,
728 lexer->saved_tokens, lexer->next_token);
729 }
730
731 /* Commit to the portion of the token stream most recently saved. */
732
733 static void
734 cp_lexer_commit_tokens (cp_lexer* lexer)
735 {
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739
740 VEC_pop (cp_token_position, lexer->saved_tokens);
741 }
742
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744 to the token stream. Stop saving tokens. */
745
746 static void
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 {
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
751 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752
753 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 }
755
756 /* Print a representation of the TOKEN on the STREAM. */
757
758 #ifdef ENABLE_CHECKING
759
760 static void
761 cp_lexer_print_token (FILE * stream, cp_token *token)
762 {
763 /* We don't use cpp_type2name here because the parser defines
764 a few tokens of its own. */
765 static const char *const token_names[] = {
766 /* cpplib-defined token types */
767 #define OP(e, s) #e,
768 #define TK(e, s) #e,
769 TTYPE_TABLE
770 #undef OP
771 #undef TK
772 /* C++ parser token types - see "Manifest constants", above. */
773 "KEYWORD",
774 "TEMPLATE_ID",
775 "NESTED_NAME_SPECIFIER",
776 "PURGED"
777 };
778
779 /* If we have a name for the token, print it out. Otherwise, we
780 simply give the numeric code. */
781 gcc_assert (token->type < ARRAY_SIZE(token_names));
782 fputs (token_names[token->type], stream);
783
784 /* For some tokens, print the associated data. */
785 switch (token->type)
786 {
787 case CPP_KEYWORD:
788 /* Some keywords have a value that is not an IDENTIFIER_NODE.
789 For example, `struct' is mapped to an INTEGER_CST. */
790 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 break;
792 /* else fall through */
793 case CPP_NAME:
794 fputs (IDENTIFIER_POINTER (token->u.value), stream);
795 break;
796
797 case CPP_STRING:
798 case CPP_WSTRING:
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800 break;
801
802 default:
803 break;
804 }
805 }
806
807 /* Start emitting debugging information. */
808
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
811 {
812 lexer->debugging_p = true;
813 }
814
815 /* Stop emitting debugging information. */
816
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
819 {
820 lexer->debugging_p = false;
821 }
822
823 #endif /* ENABLE_CHECKING */
824
825 /* Create a new cp_token_cache, representing a range of tokens. */
826
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
829 {
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
832 cache->last = last;
833 return cache;
834 }
835
836 \f
837 /* Decl-specifiers. */
838
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
840
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
843 {
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 }
846
847 /* Declarators. */
848
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
853
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *, bool);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
866
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
869
870 /* The obstack on which declarators and related data structures are
871 allocated. */
872 static struct obstack declarator_obstack;
873
874 /* Alloc BYTES from the declarator memory pool. */
875
876 static inline void *
877 alloc_declarator (size_t bytes)
878 {
879 return obstack_alloc (&declarator_obstack, bytes);
880 }
881
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
884
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
887 {
888 cp_declarator *declarator;
889
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
895
896 return declarator;
897 }
898
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
903 is, if any. */
904
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
908 {
909 cp_declarator *declarator;
910
911 /* It is valid to write:
912
913 class C { void f(); };
914 typedef C D;
915 void D::f();
916
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
920 well. */
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
932
933 return declarator;
934 }
935
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
939
940 cp_declarator *
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 {
943 cp_declarator *declarator;
944
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
949 if (target)
950 {
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
953 }
954 else
955 declarator->parameter_pack_p = false;
956
957 return declarator;
958 }
959
960 /* Like make_pointer_declarator -- but for references. */
961
962 cp_declarator *
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
964 bool rvalue_ref)
965 {
966 cp_declarator *declarator;
967
968 declarator = make_declarator (cdk_reference);
969 declarator->declarator = target;
970 declarator->u.reference.qualifiers = cv_qualifiers;
971 declarator->u.reference.rvalue_ref = rvalue_ref;
972 if (target)
973 {
974 declarator->parameter_pack_p = target->parameter_pack_p;
975 target->parameter_pack_p = false;
976 }
977 else
978 declarator->parameter_pack_p = false;
979
980 return declarator;
981 }
982
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984 member of CLASS_TYPE. */
985
986 cp_declarator *
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988 cp_declarator *pointee)
989 {
990 cp_declarator *declarator;
991
992 declarator = make_declarator (cdk_ptrmem);
993 declarator->declarator = pointee;
994 declarator->u.pointer.qualifiers = cv_qualifiers;
995 declarator->u.pointer.class_type = class_type;
996
997 if (pointee)
998 {
999 declarator->parameter_pack_p = pointee->parameter_pack_p;
1000 pointee->parameter_pack_p = false;
1001 }
1002 else
1003 declarator->parameter_pack_p = false;
1004
1005 return declarator;
1006 }
1007
1008 /* Make a declarator for the function given by TARGET, with the
1009 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1010 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1011 indicates what exceptions can be thrown. */
1012
1013 cp_declarator *
1014 make_call_declarator (cp_declarator *target,
1015 cp_parameter_declarator *parms,
1016 cp_cv_quals cv_qualifiers,
1017 tree exception_specification)
1018 {
1019 cp_declarator *declarator;
1020
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1026 if (target)
1027 {
1028 declarator->parameter_pack_p = target->parameter_pack_p;
1029 target->parameter_pack_p = false;
1030 }
1031 else
1032 declarator->parameter_pack_p = false;
1033
1034 return declarator;
1035 }
1036
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1039
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1042 {
1043 cp_declarator *declarator;
1044
1045 declarator = make_declarator (cdk_array);
1046 declarator->declarator = element;
1047 declarator->u.array.bounds = bounds;
1048 if (element)
1049 {
1050 declarator->parameter_pack_p = element->parameter_pack_p;
1051 element->parameter_pack_p = false;
1052 }
1053 else
1054 declarator->parameter_pack_p = false;
1055
1056 return declarator;
1057 }
1058
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1061 static bool
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 {
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1068 bool found = false;
1069 while (declarator && !found)
1070 {
1071 switch ((int)declarator->kind)
1072 {
1073 case cdk_id:
1074 case cdk_error:
1075 case cdk_array:
1076 case cdk_ptrmem:
1077 found = true;
1078 break;
1079
1080 default:
1081 declarator = declarator->declarator;
1082 break;
1083 }
1084 }
1085
1086 return !found;
1087 }
1088
1089 cp_parameter_declarator *no_parameters;
1090
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1093
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096 cp_declarator *declarator,
1097 tree default_argument)
1098 {
1099 cp_parameter_declarator *parameter;
1100
1101 parameter = ((cp_parameter_declarator *)
1102 alloc_declarator (sizeof (cp_parameter_declarator)));
1103 parameter->next = NULL;
1104 if (decl_specifiers)
1105 parameter->decl_specifiers = *decl_specifiers;
1106 else
1107 clear_decl_specs (&parameter->decl_specifiers);
1108 parameter->declarator = declarator;
1109 parameter->default_argument = default_argument;
1110 parameter->ellipsis_p = false;
1111
1112 return parameter;
1113 }
1114
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1116
1117 static bool
1118 function_declarator_p (const cp_declarator *declarator)
1119 {
1120 while (declarator)
1121 {
1122 if (declarator->kind == cdk_function
1123 && declarator->declarator->kind == cdk_id)
1124 return true;
1125 if (declarator->kind == cdk_id
1126 || declarator->kind == cdk_error)
1127 return false;
1128 declarator = declarator->declarator;
1129 }
1130 return false;
1131 }
1132
1133 /* The parser. */
1134
1135 /* Overview
1136 --------
1137
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1145
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1148 code processed.
1149
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1158
1159 Methodology
1160 -----------
1161
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1178
1179 Future Improvements
1180 -------------------
1181
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1186 case. */
1187
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1190
1191 typedef enum cp_parser_flags
1192 {
1193 /* No flags. */
1194 CP_PARSER_FLAGS_NONE = 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198 /* When parsing a type-specifier, do not allow user-defined types. */
1199 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1200 } cp_parser_flags;
1201
1202 /* The different kinds of declarators we want to parse. */
1203
1204 typedef enum cp_parser_declarator_kind
1205 {
1206 /* We want an abstract declarator. */
1207 CP_PARSER_DECLARATOR_ABSTRACT,
1208 /* We want a named declarator. */
1209 CP_PARSER_DECLARATOR_NAMED,
1210 /* We don't mind, but the name must be an unqualified-id. */
1211 CP_PARSER_DECLARATOR_EITHER
1212 } cp_parser_declarator_kind;
1213
1214 /* The precedence values used to parse binary expressions. The minimum value
1215 of PREC must be 1, because zero is reserved to quickly discriminate
1216 binary operators from other tokens. */
1217
1218 enum cp_parser_prec
1219 {
1220 PREC_NOT_OPERATOR,
1221 PREC_LOGICAL_OR_EXPRESSION,
1222 PREC_LOGICAL_AND_EXPRESSION,
1223 PREC_INCLUSIVE_OR_EXPRESSION,
1224 PREC_EXCLUSIVE_OR_EXPRESSION,
1225 PREC_AND_EXPRESSION,
1226 PREC_EQUALITY_EXPRESSION,
1227 PREC_RELATIONAL_EXPRESSION,
1228 PREC_SHIFT_EXPRESSION,
1229 PREC_ADDITIVE_EXPRESSION,
1230 PREC_MULTIPLICATIVE_EXPRESSION,
1231 PREC_PM_EXPRESSION,
1232 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1233 };
1234
1235 /* A mapping from a token type to a corresponding tree node type, with a
1236 precedence value. */
1237
1238 typedef struct cp_parser_binary_operations_map_node
1239 {
1240 /* The token type. */
1241 enum cpp_ttype token_type;
1242 /* The corresponding tree code. */
1243 enum tree_code tree_type;
1244 /* The precedence of this operator. */
1245 enum cp_parser_prec prec;
1246 } cp_parser_binary_operations_map_node;
1247
1248 /* The status of a tentative parse. */
1249
1250 typedef enum cp_parser_status_kind
1251 {
1252 /* No errors have occurred. */
1253 CP_PARSER_STATUS_KIND_NO_ERROR,
1254 /* An error has occurred. */
1255 CP_PARSER_STATUS_KIND_ERROR,
1256 /* We are committed to this tentative parse, whether or not an error
1257 has occurred. */
1258 CP_PARSER_STATUS_KIND_COMMITTED
1259 } cp_parser_status_kind;
1260
1261 typedef struct cp_parser_expression_stack_entry
1262 {
1263 /* Left hand side of the binary operation we are currently
1264 parsing. */
1265 tree lhs;
1266 /* Original tree code for left hand side, if it was a binary
1267 expression itself (used for -Wparentheses). */
1268 enum tree_code lhs_type;
1269 /* Tree code for the binary operation we are parsing. */
1270 enum tree_code tree_type;
1271 /* Precedence of the binary operation we are parsing. */
1272 int prec;
1273 } cp_parser_expression_stack_entry;
1274
1275 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1276 entries because precedence levels on the stack are monotonically
1277 increasing. */
1278 typedef struct cp_parser_expression_stack_entry
1279 cp_parser_expression_stack[NUM_PREC_VALUES];
1280
1281 /* Context that is saved and restored when parsing tentatively. */
1282 typedef struct cp_parser_context GTY (())
1283 {
1284 /* If this is a tentative parsing context, the status of the
1285 tentative parse. */
1286 enum cp_parser_status_kind status;
1287 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1288 that are looked up in this context must be looked up both in the
1289 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1290 the context of the containing expression. */
1291 tree object_type;
1292
1293 /* The next parsing context in the stack. */
1294 struct cp_parser_context *next;
1295 } cp_parser_context;
1296
1297 /* Prototypes. */
1298
1299 /* Constructors and destructors. */
1300
1301 static cp_parser_context *cp_parser_context_new
1302 (cp_parser_context *);
1303
1304 /* Class variables. */
1305
1306 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1307
1308 /* The operator-precedence table used by cp_parser_binary_expression.
1309 Transformed into an associative array (binops_by_token) by
1310 cp_parser_new. */
1311
1312 static const cp_parser_binary_operations_map_node binops[] = {
1313 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1314 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1315
1316 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319
1320 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322
1323 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325
1326 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330
1331 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1332 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1333
1334 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1335
1336 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1337
1338 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1339
1340 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1341
1342 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1343 };
1344
1345 /* The same as binops, but initialized by cp_parser_new so that
1346 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1347 for speed. */
1348 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1349
1350 /* Constructors and destructors. */
1351
1352 /* Construct a new context. The context below this one on the stack
1353 is given by NEXT. */
1354
1355 static cp_parser_context *
1356 cp_parser_context_new (cp_parser_context* next)
1357 {
1358 cp_parser_context *context;
1359
1360 /* Allocate the storage. */
1361 if (cp_parser_context_free_list != NULL)
1362 {
1363 /* Pull the first entry from the free list. */
1364 context = cp_parser_context_free_list;
1365 cp_parser_context_free_list = context->next;
1366 memset (context, 0, sizeof (*context));
1367 }
1368 else
1369 context = GGC_CNEW (cp_parser_context);
1370
1371 /* No errors have occurred yet in this context. */
1372 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1373 /* If this is not the bottomost context, copy information that we
1374 need from the previous context. */
1375 if (next)
1376 {
1377 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1378 expression, then we are parsing one in this context, too. */
1379 context->object_type = next->object_type;
1380 /* Thread the stack. */
1381 context->next = next;
1382 }
1383
1384 return context;
1385 }
1386
1387 /* The cp_parser structure represents the C++ parser. */
1388
1389 typedef struct cp_parser GTY(())
1390 {
1391 /* The lexer from which we are obtaining tokens. */
1392 cp_lexer *lexer;
1393
1394 /* The scope in which names should be looked up. If NULL_TREE, then
1395 we look up names in the scope that is currently open in the
1396 source program. If non-NULL, this is either a TYPE or
1397 NAMESPACE_DECL for the scope in which we should look. It can
1398 also be ERROR_MARK, when we've parsed a bogus scope.
1399
1400 This value is not cleared automatically after a name is looked
1401 up, so we must be careful to clear it before starting a new look
1402 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1403 will look up `Z' in the scope of `X', rather than the current
1404 scope.) Unfortunately, it is difficult to tell when name lookup
1405 is complete, because we sometimes peek at a token, look it up,
1406 and then decide not to consume it. */
1407 tree scope;
1408
1409 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1410 last lookup took place. OBJECT_SCOPE is used if an expression
1411 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1412 respectively. QUALIFYING_SCOPE is used for an expression of the
1413 form "X::Y"; it refers to X. */
1414 tree object_scope;
1415 tree qualifying_scope;
1416
1417 /* A stack of parsing contexts. All but the bottom entry on the
1418 stack will be tentative contexts.
1419
1420 We parse tentatively in order to determine which construct is in
1421 use in some situations. For example, in order to determine
1422 whether a statement is an expression-statement or a
1423 declaration-statement we parse it tentatively as a
1424 declaration-statement. If that fails, we then reparse the same
1425 token stream as an expression-statement. */
1426 cp_parser_context *context;
1427
1428 /* True if we are parsing GNU C++. If this flag is not set, then
1429 GNU extensions are not recognized. */
1430 bool allow_gnu_extensions_p;
1431
1432 /* TRUE if the `>' token should be interpreted as the greater-than
1433 operator. FALSE if it is the end of a template-id or
1434 template-parameter-list. In C++0x mode, this flag also applies to
1435 `>>' tokens, which are viewed as two consecutive `>' tokens when
1436 this flag is FALSE. */
1437 bool greater_than_is_operator_p;
1438
1439 /* TRUE if default arguments are allowed within a parameter list
1440 that starts at this point. FALSE if only a gnu extension makes
1441 them permissible. */
1442 bool default_arg_ok_p;
1443
1444 /* TRUE if we are parsing an integral constant-expression. See
1445 [expr.const] for a precise definition. */
1446 bool integral_constant_expression_p;
1447
1448 /* TRUE if we are parsing an integral constant-expression -- but a
1449 non-constant expression should be permitted as well. This flag
1450 is used when parsing an array bound so that GNU variable-length
1451 arrays are tolerated. */
1452 bool allow_non_integral_constant_expression_p;
1453
1454 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1455 been seen that makes the expression non-constant. */
1456 bool non_integral_constant_expression_p;
1457
1458 /* TRUE if local variable names and `this' are forbidden in the
1459 current context. */
1460 bool local_variables_forbidden_p;
1461
1462 /* TRUE if the declaration we are parsing is part of a
1463 linkage-specification of the form `extern string-literal
1464 declaration'. */
1465 bool in_unbraced_linkage_specification_p;
1466
1467 /* TRUE if we are presently parsing a declarator, after the
1468 direct-declarator. */
1469 bool in_declarator_p;
1470
1471 /* TRUE if we are presently parsing a template-argument-list. */
1472 bool in_template_argument_list_p;
1473
1474 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1475 to IN_OMP_BLOCK if parsing OpenMP structured block and
1476 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1477 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1478 iteration-statement, OpenMP block or loop within that switch. */
1479 #define IN_SWITCH_STMT 1
1480 #define IN_ITERATION_STMT 2
1481 #define IN_OMP_BLOCK 4
1482 #define IN_OMP_FOR 8
1483 #define IN_IF_STMT 16
1484 unsigned char in_statement;
1485
1486 /* TRUE if we are presently parsing the body of a switch statement.
1487 Note that this doesn't quite overlap with in_statement above.
1488 The difference relates to giving the right sets of error messages:
1489 "case not in switch" vs "break statement used with OpenMP...". */
1490 bool in_switch_statement_p;
1491
1492 /* TRUE if we are parsing a type-id in an expression context. In
1493 such a situation, both "type (expr)" and "type (type)" are valid
1494 alternatives. */
1495 bool in_type_id_in_expr_p;
1496
1497 /* TRUE if we are currently in a header file where declarations are
1498 implicitly extern "C". */
1499 bool implicit_extern_c;
1500
1501 /* TRUE if strings in expressions should be translated to the execution
1502 character set. */
1503 bool translate_strings_p;
1504
1505 /* TRUE if we are presently parsing the body of a function, but not
1506 a local class. */
1507 bool in_function_body;
1508
1509 /* If non-NULL, then we are parsing a construct where new type
1510 definitions are not permitted. The string stored here will be
1511 issued as an error message if a type is defined. */
1512 const char *type_definition_forbidden_message;
1513
1514 /* A list of lists. The outer list is a stack, used for member
1515 functions of local classes. At each level there are two sub-list,
1516 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1517 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1518 TREE_VALUE's. The functions are chained in reverse declaration
1519 order.
1520
1521 The TREE_PURPOSE sublist contains those functions with default
1522 arguments that need post processing, and the TREE_VALUE sublist
1523 contains those functions with definitions that need post
1524 processing.
1525
1526 These lists can only be processed once the outermost class being
1527 defined is complete. */
1528 tree unparsed_functions_queues;
1529
1530 /* The number of classes whose definitions are currently in
1531 progress. */
1532 unsigned num_classes_being_defined;
1533
1534 /* The number of template parameter lists that apply directly to the
1535 current declaration. */
1536 unsigned num_template_parameter_lists;
1537 } cp_parser;
1538
1539 /* Prototypes. */
1540
1541 /* Constructors and destructors. */
1542
1543 static cp_parser *cp_parser_new
1544 (void);
1545
1546 /* Routines to parse various constructs.
1547
1548 Those that return `tree' will return the error_mark_node (rather
1549 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1550 Sometimes, they will return an ordinary node if error-recovery was
1551 attempted, even though a parse error occurred. So, to check
1552 whether or not a parse error occurred, you should always use
1553 cp_parser_error_occurred. If the construct is optional (indicated
1554 either by an `_opt' in the name of the function that does the
1555 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1556 the construct is not present. */
1557
1558 /* Lexical conventions [gram.lex] */
1559
1560 static tree cp_parser_identifier
1561 (cp_parser *);
1562 static tree cp_parser_string_literal
1563 (cp_parser *, bool, bool);
1564
1565 /* Basic concepts [gram.basic] */
1566
1567 static bool cp_parser_translation_unit
1568 (cp_parser *);
1569
1570 /* Expressions [gram.expr] */
1571
1572 static tree cp_parser_primary_expression
1573 (cp_parser *, bool, bool, bool, cp_id_kind *);
1574 static tree cp_parser_id_expression
1575 (cp_parser *, bool, bool, bool *, bool, bool);
1576 static tree cp_parser_unqualified_id
1577 (cp_parser *, bool, bool, bool, bool);
1578 static tree cp_parser_nested_name_specifier_opt
1579 (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_class_or_namespace_name
1583 (cp_parser *, bool, bool, bool, bool, bool);
1584 static tree cp_parser_postfix_expression
1585 (cp_parser *, bool, bool, bool);
1586 static tree cp_parser_postfix_open_square_expression
1587 (cp_parser *, tree, bool);
1588 static tree cp_parser_postfix_dot_deref_expression
1589 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1590 static tree cp_parser_parenthesized_expression_list
1591 (cp_parser *, bool, bool, bool, bool *);
1592 static void cp_parser_pseudo_destructor_name
1593 (cp_parser *, tree *, tree *);
1594 static tree cp_parser_unary_expression
1595 (cp_parser *, bool, bool);
1596 static enum tree_code cp_parser_unary_operator
1597 (cp_token *);
1598 static tree cp_parser_new_expression
1599 (cp_parser *);
1600 static tree cp_parser_new_placement
1601 (cp_parser *);
1602 static tree cp_parser_new_type_id
1603 (cp_parser *, tree *);
1604 static cp_declarator *cp_parser_new_declarator_opt
1605 (cp_parser *);
1606 static cp_declarator *cp_parser_direct_new_declarator
1607 (cp_parser *);
1608 static tree cp_parser_new_initializer
1609 (cp_parser *);
1610 static tree cp_parser_delete_expression
1611 (cp_parser *);
1612 static tree cp_parser_cast_expression
1613 (cp_parser *, bool, bool);
1614 static tree cp_parser_binary_expression
1615 (cp_parser *, bool);
1616 static tree cp_parser_question_colon_clause
1617 (cp_parser *, tree);
1618 static tree cp_parser_assignment_expression
1619 (cp_parser *, bool);
1620 static enum tree_code cp_parser_assignment_operator_opt
1621 (cp_parser *);
1622 static tree cp_parser_expression
1623 (cp_parser *, bool);
1624 static tree cp_parser_constant_expression
1625 (cp_parser *, bool, bool *);
1626 static tree cp_parser_builtin_offsetof
1627 (cp_parser *);
1628
1629 /* Statements [gram.stmt.stmt] */
1630
1631 static void cp_parser_statement
1632 (cp_parser *, tree, bool, bool *);
1633 static void cp_parser_label_for_labeled_statement
1634 (cp_parser *);
1635 static tree cp_parser_expression_statement
1636 (cp_parser *, tree);
1637 static tree cp_parser_compound_statement
1638 (cp_parser *, tree, bool);
1639 static void cp_parser_statement_seq_opt
1640 (cp_parser *, tree);
1641 static tree cp_parser_selection_statement
1642 (cp_parser *, bool *);
1643 static tree cp_parser_condition
1644 (cp_parser *);
1645 static tree cp_parser_iteration_statement
1646 (cp_parser *);
1647 static void cp_parser_for_init_statement
1648 (cp_parser *);
1649 static tree cp_parser_jump_statement
1650 (cp_parser *);
1651 static void cp_parser_declaration_statement
1652 (cp_parser *);
1653
1654 static tree cp_parser_implicitly_scoped_statement
1655 (cp_parser *, bool *);
1656 static void cp_parser_already_scoped_statement
1657 (cp_parser *);
1658
1659 /* Declarations [gram.dcl.dcl] */
1660
1661 static void cp_parser_declaration_seq_opt
1662 (cp_parser *);
1663 static void cp_parser_declaration
1664 (cp_parser *);
1665 static void cp_parser_block_declaration
1666 (cp_parser *, bool);
1667 static void cp_parser_simple_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_decl_specifier_seq
1670 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1671 static tree cp_parser_storage_class_specifier_opt
1672 (cp_parser *);
1673 static tree cp_parser_function_specifier_opt
1674 (cp_parser *, cp_decl_specifier_seq *);
1675 static tree cp_parser_type_specifier
1676 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1677 int *, bool *);
1678 static tree cp_parser_simple_type_specifier
1679 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1680 static tree cp_parser_type_name
1681 (cp_parser *);
1682 static tree cp_parser_elaborated_type_specifier
1683 (cp_parser *, bool, bool);
1684 static tree cp_parser_enum_specifier
1685 (cp_parser *);
1686 static void cp_parser_enumerator_list
1687 (cp_parser *, tree);
1688 static void cp_parser_enumerator_definition
1689 (cp_parser *, tree);
1690 static tree cp_parser_namespace_name
1691 (cp_parser *);
1692 static void cp_parser_namespace_definition
1693 (cp_parser *);
1694 static void cp_parser_namespace_body
1695 (cp_parser *);
1696 static tree cp_parser_qualified_namespace_specifier
1697 (cp_parser *);
1698 static void cp_parser_namespace_alias_definition
1699 (cp_parser *);
1700 static bool cp_parser_using_declaration
1701 (cp_parser *, bool);
1702 static void cp_parser_using_directive
1703 (cp_parser *);
1704 static void cp_parser_asm_definition
1705 (cp_parser *);
1706 static void cp_parser_linkage_specification
1707 (cp_parser *);
1708 static void cp_parser_static_assert
1709 (cp_parser *, bool);
1710 static tree cp_parser_decltype
1711 (cp_parser *);
1712
1713 /* Declarators [gram.dcl.decl] */
1714
1715 static tree cp_parser_init_declarator
1716 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1717 static cp_declarator *cp_parser_declarator
1718 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1719 static cp_declarator *cp_parser_direct_declarator
1720 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1721 static enum tree_code cp_parser_ptr_operator
1722 (cp_parser *, tree *, cp_cv_quals *);
1723 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1724 (cp_parser *);
1725 static tree cp_parser_declarator_id
1726 (cp_parser *, bool);
1727 static tree cp_parser_type_id
1728 (cp_parser *);
1729 static void cp_parser_type_specifier_seq
1730 (cp_parser *, bool, cp_decl_specifier_seq *);
1731 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1732 (cp_parser *);
1733 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1734 (cp_parser *, bool *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration
1736 (cp_parser *, bool, bool *);
1737 static void cp_parser_function_body
1738 (cp_parser *);
1739 static tree cp_parser_initializer
1740 (cp_parser *, bool *, bool *);
1741 static tree cp_parser_initializer_clause
1742 (cp_parser *, bool *);
1743 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1744 (cp_parser *, bool *);
1745
1746 static bool cp_parser_ctor_initializer_opt_and_function_body
1747 (cp_parser *);
1748
1749 /* Classes [gram.class] */
1750
1751 static tree cp_parser_class_name
1752 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1753 static tree cp_parser_class_specifier
1754 (cp_parser *);
1755 static tree cp_parser_class_head
1756 (cp_parser *, bool *, tree *, tree *);
1757 static enum tag_types cp_parser_class_key
1758 (cp_parser *);
1759 static void cp_parser_member_specification_opt
1760 (cp_parser *);
1761 static void cp_parser_member_declaration
1762 (cp_parser *);
1763 static tree cp_parser_pure_specifier
1764 (cp_parser *);
1765 static tree cp_parser_constant_initializer
1766 (cp_parser *);
1767
1768 /* Derived classes [gram.class.derived] */
1769
1770 static tree cp_parser_base_clause
1771 (cp_parser *);
1772 static tree cp_parser_base_specifier
1773 (cp_parser *);
1774
1775 /* Special member functions [gram.special] */
1776
1777 static tree cp_parser_conversion_function_id
1778 (cp_parser *);
1779 static tree cp_parser_conversion_type_id
1780 (cp_parser *);
1781 static cp_declarator *cp_parser_conversion_declarator_opt
1782 (cp_parser *);
1783 static bool cp_parser_ctor_initializer_opt
1784 (cp_parser *);
1785 static void cp_parser_mem_initializer_list
1786 (cp_parser *);
1787 static tree cp_parser_mem_initializer
1788 (cp_parser *);
1789 static tree cp_parser_mem_initializer_id
1790 (cp_parser *);
1791
1792 /* Overloading [gram.over] */
1793
1794 static tree cp_parser_operator_function_id
1795 (cp_parser *);
1796 static tree cp_parser_operator
1797 (cp_parser *);
1798
1799 /* Templates [gram.temp] */
1800
1801 static void cp_parser_template_declaration
1802 (cp_parser *, bool);
1803 static tree cp_parser_template_parameter_list
1804 (cp_parser *);
1805 static tree cp_parser_template_parameter
1806 (cp_parser *, bool *, bool *);
1807 static tree cp_parser_type_parameter
1808 (cp_parser *, bool *);
1809 static tree cp_parser_template_id
1810 (cp_parser *, bool, bool, bool);
1811 static tree cp_parser_template_name
1812 (cp_parser *, bool, bool, bool, bool *);
1813 static tree cp_parser_template_argument_list
1814 (cp_parser *);
1815 static tree cp_parser_template_argument
1816 (cp_parser *);
1817 static void cp_parser_explicit_instantiation
1818 (cp_parser *);
1819 static void cp_parser_explicit_specialization
1820 (cp_parser *);
1821
1822 /* Exception handling [gram.exception] */
1823
1824 static tree cp_parser_try_block
1825 (cp_parser *);
1826 static bool cp_parser_function_try_block
1827 (cp_parser *);
1828 static void cp_parser_handler_seq
1829 (cp_parser *);
1830 static void cp_parser_handler
1831 (cp_parser *);
1832 static tree cp_parser_exception_declaration
1833 (cp_parser *);
1834 static tree cp_parser_throw_expression
1835 (cp_parser *);
1836 static tree cp_parser_exception_specification_opt
1837 (cp_parser *);
1838 static tree cp_parser_type_id_list
1839 (cp_parser *);
1840
1841 /* GNU Extensions */
1842
1843 static tree cp_parser_asm_specification_opt
1844 (cp_parser *);
1845 static tree cp_parser_asm_operand_list
1846 (cp_parser *);
1847 static tree cp_parser_asm_clobber_list
1848 (cp_parser *);
1849 static tree cp_parser_attributes_opt
1850 (cp_parser *);
1851 static tree cp_parser_attribute_list
1852 (cp_parser *);
1853 static bool cp_parser_extension_opt
1854 (cp_parser *, int *);
1855 static void cp_parser_label_declaration
1856 (cp_parser *);
1857
1858 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1859 static bool cp_parser_pragma
1860 (cp_parser *, enum pragma_context);
1861
1862 /* Objective-C++ Productions */
1863
1864 static tree cp_parser_objc_message_receiver
1865 (cp_parser *);
1866 static tree cp_parser_objc_message_args
1867 (cp_parser *);
1868 static tree cp_parser_objc_message_expression
1869 (cp_parser *);
1870 static tree cp_parser_objc_encode_expression
1871 (cp_parser *);
1872 static tree cp_parser_objc_defs_expression
1873 (cp_parser *);
1874 static tree cp_parser_objc_protocol_expression
1875 (cp_parser *);
1876 static tree cp_parser_objc_selector_expression
1877 (cp_parser *);
1878 static tree cp_parser_objc_expression
1879 (cp_parser *);
1880 static bool cp_parser_objc_selector_p
1881 (enum cpp_ttype);
1882 static tree cp_parser_objc_selector
1883 (cp_parser *);
1884 static tree cp_parser_objc_protocol_refs_opt
1885 (cp_parser *);
1886 static void cp_parser_objc_declaration
1887 (cp_parser *);
1888 static tree cp_parser_objc_statement
1889 (cp_parser *);
1890
1891 /* Utility Routines */
1892
1893 static tree cp_parser_lookup_name
1894 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1895 static tree cp_parser_lookup_name_simple
1896 (cp_parser *, tree);
1897 static tree cp_parser_maybe_treat_template_as_class
1898 (tree, bool);
1899 static bool cp_parser_check_declarator_template_parameters
1900 (cp_parser *, cp_declarator *);
1901 static bool cp_parser_check_template_parameters
1902 (cp_parser *, unsigned);
1903 static tree cp_parser_simple_cast_expression
1904 (cp_parser *);
1905 static tree cp_parser_global_scope_opt
1906 (cp_parser *, bool);
1907 static bool cp_parser_constructor_declarator_p
1908 (cp_parser *, bool);
1909 static tree cp_parser_function_definition_from_specifiers_and_declarator
1910 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1911 static tree cp_parser_function_definition_after_declarator
1912 (cp_parser *, bool);
1913 static void cp_parser_template_declaration_after_export
1914 (cp_parser *, bool);
1915 static void cp_parser_perform_template_parameter_access_checks
1916 (VEC (deferred_access_check,gc)*);
1917 static tree cp_parser_single_declaration
1918 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1919 static tree cp_parser_functional_cast
1920 (cp_parser *, tree);
1921 static tree cp_parser_save_member_function_body
1922 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1923 static tree cp_parser_enclosed_template_argument_list
1924 (cp_parser *);
1925 static void cp_parser_save_default_args
1926 (cp_parser *, tree);
1927 static void cp_parser_late_parsing_for_member
1928 (cp_parser *, tree);
1929 static void cp_parser_late_parsing_default_args
1930 (cp_parser *, tree);
1931 static tree cp_parser_sizeof_operand
1932 (cp_parser *, enum rid);
1933 static tree cp_parser_trait_expr
1934 (cp_parser *, enum rid);
1935 static bool cp_parser_declares_only_class_p
1936 (cp_parser *);
1937 static void cp_parser_set_storage_class
1938 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1939 static void cp_parser_set_decl_spec_type
1940 (cp_decl_specifier_seq *, tree, bool);
1941 static bool cp_parser_friend_p
1942 (const cp_decl_specifier_seq *);
1943 static cp_token *cp_parser_require
1944 (cp_parser *, enum cpp_ttype, const char *);
1945 static cp_token *cp_parser_require_keyword
1946 (cp_parser *, enum rid, const char *);
1947 static bool cp_parser_token_starts_function_definition_p
1948 (cp_token *);
1949 static bool cp_parser_next_token_starts_class_definition_p
1950 (cp_parser *);
1951 static bool cp_parser_next_token_ends_template_argument_p
1952 (cp_parser *);
1953 static bool cp_parser_nth_token_starts_template_argument_list_p
1954 (cp_parser *, size_t);
1955 static enum tag_types cp_parser_token_is_class_key
1956 (cp_token *);
1957 static void cp_parser_check_class_key
1958 (enum tag_types, tree type);
1959 static void cp_parser_check_access_in_redeclaration
1960 (tree type);
1961 static bool cp_parser_optional_template_keyword
1962 (cp_parser *);
1963 static void cp_parser_pre_parsed_nested_name_specifier
1964 (cp_parser *);
1965 static void cp_parser_cache_group
1966 (cp_parser *, enum cpp_ttype, unsigned);
1967 static void cp_parser_parse_tentatively
1968 (cp_parser *);
1969 static void cp_parser_commit_to_tentative_parse
1970 (cp_parser *);
1971 static void cp_parser_abort_tentative_parse
1972 (cp_parser *);
1973 static bool cp_parser_parse_definitely
1974 (cp_parser *);
1975 static inline bool cp_parser_parsing_tentatively
1976 (cp_parser *);
1977 static bool cp_parser_uncommitted_to_tentative_parse_p
1978 (cp_parser *);
1979 static void cp_parser_error
1980 (cp_parser *, const char *);
1981 static void cp_parser_name_lookup_error
1982 (cp_parser *, tree, tree, const char *);
1983 static bool cp_parser_simulate_error
1984 (cp_parser *);
1985 static bool cp_parser_check_type_definition
1986 (cp_parser *);
1987 static void cp_parser_check_for_definition_in_return_type
1988 (cp_declarator *, tree);
1989 static void cp_parser_check_for_invalid_template_id
1990 (cp_parser *, tree);
1991 static bool cp_parser_non_integral_constant_expression
1992 (cp_parser *, const char *);
1993 static void cp_parser_diagnose_invalid_type_name
1994 (cp_parser *, tree, tree);
1995 static bool cp_parser_parse_and_diagnose_invalid_type_name
1996 (cp_parser *);
1997 static int cp_parser_skip_to_closing_parenthesis
1998 (cp_parser *, bool, bool, bool);
1999 static void cp_parser_skip_to_end_of_statement
2000 (cp_parser *);
2001 static void cp_parser_consume_semicolon_at_end_of_statement
2002 (cp_parser *);
2003 static void cp_parser_skip_to_end_of_block_or_statement
2004 (cp_parser *);
2005 static bool cp_parser_skip_to_closing_brace
2006 (cp_parser *);
2007 static void cp_parser_skip_to_end_of_template_parameter_list
2008 (cp_parser *);
2009 static void cp_parser_skip_to_pragma_eol
2010 (cp_parser*, cp_token *);
2011 static bool cp_parser_error_occurred
2012 (cp_parser *);
2013 static bool cp_parser_allow_gnu_extensions_p
2014 (cp_parser *);
2015 static bool cp_parser_is_string_literal
2016 (cp_token *);
2017 static bool cp_parser_is_keyword
2018 (cp_token *, enum rid);
2019 static tree cp_parser_make_typename_type
2020 (cp_parser *, tree, tree);
2021 static cp_declarator * cp_parser_make_indirect_declarator
2022 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2023
2024 /* Returns nonzero if we are parsing tentatively. */
2025
2026 static inline bool
2027 cp_parser_parsing_tentatively (cp_parser* parser)
2028 {
2029 return parser->context->next != NULL;
2030 }
2031
2032 /* Returns nonzero if TOKEN is a string literal. */
2033
2034 static bool
2035 cp_parser_is_string_literal (cp_token* token)
2036 {
2037 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2038 }
2039
2040 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2041
2042 static bool
2043 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2044 {
2045 return token->keyword == keyword;
2046 }
2047
2048 /* If not parsing tentatively, issue a diagnostic of the form
2049 FILE:LINE: MESSAGE before TOKEN
2050 where TOKEN is the next token in the input stream. MESSAGE
2051 (specified by the caller) is usually of the form "expected
2052 OTHER-TOKEN". */
2053
2054 static void
2055 cp_parser_error (cp_parser* parser, const char* message)
2056 {
2057 if (!cp_parser_simulate_error (parser))
2058 {
2059 cp_token *token = cp_lexer_peek_token (parser->lexer);
2060 /* This diagnostic makes more sense if it is tagged to the line
2061 of the token we just peeked at. */
2062 cp_lexer_set_source_position_from_token (token);
2063
2064 if (token->type == CPP_PRAGMA)
2065 {
2066 error ("%<#pragma%> is not allowed here");
2067 cp_parser_skip_to_pragma_eol (parser, token);
2068 return;
2069 }
2070
2071 c_parse_error (message,
2072 /* Because c_parser_error does not understand
2073 CPP_KEYWORD, keywords are treated like
2074 identifiers. */
2075 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2076 token->u.value);
2077 }
2078 }
2079
2080 /* Issue an error about name-lookup failing. NAME is the
2081 IDENTIFIER_NODE DECL is the result of
2082 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2083 the thing that we hoped to find. */
2084
2085 static void
2086 cp_parser_name_lookup_error (cp_parser* parser,
2087 tree name,
2088 tree decl,
2089 const char* desired)
2090 {
2091 /* If name lookup completely failed, tell the user that NAME was not
2092 declared. */
2093 if (decl == error_mark_node)
2094 {
2095 if (parser->scope && parser->scope != global_namespace)
2096 error ("%<%E::%E%> has not been declared",
2097 parser->scope, name);
2098 else if (parser->scope == global_namespace)
2099 error ("%<::%E%> has not been declared", name);
2100 else if (parser->object_scope
2101 && !CLASS_TYPE_P (parser->object_scope))
2102 error ("request for member %qE in non-class type %qT",
2103 name, parser->object_scope);
2104 else if (parser->object_scope)
2105 error ("%<%T::%E%> has not been declared",
2106 parser->object_scope, name);
2107 else
2108 error ("%qE has not been declared", name);
2109 }
2110 else if (parser->scope && parser->scope != global_namespace)
2111 error ("%<%E::%E%> %s", parser->scope, name, desired);
2112 else if (parser->scope == global_namespace)
2113 error ("%<::%E%> %s", name, desired);
2114 else
2115 error ("%qE %s", name, desired);
2116 }
2117
2118 /* If we are parsing tentatively, remember that an error has occurred
2119 during this tentative parse. Returns true if the error was
2120 simulated; false if a message should be issued by the caller. */
2121
2122 static bool
2123 cp_parser_simulate_error (cp_parser* parser)
2124 {
2125 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2126 {
2127 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2128 return true;
2129 }
2130 return false;
2131 }
2132
2133 /* Check for repeated decl-specifiers. */
2134
2135 static void
2136 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2137 {
2138 cp_decl_spec ds;
2139
2140 for (ds = ds_first; ds != ds_last; ++ds)
2141 {
2142 unsigned count = decl_specs->specs[(int)ds];
2143 if (count < 2)
2144 continue;
2145 /* The "long" specifier is a special case because of "long long". */
2146 if (ds == ds_long)
2147 {
2148 if (count > 2)
2149 error ("%<long long long%> is too long for GCC");
2150 else if (pedantic && !in_system_header && warn_long_long)
2151 pedwarn ("ISO C++ does not support %<long long%>");
2152 }
2153 else if (count > 1)
2154 {
2155 static const char *const decl_spec_names[] = {
2156 "signed",
2157 "unsigned",
2158 "short",
2159 "long",
2160 "const",
2161 "volatile",
2162 "restrict",
2163 "inline",
2164 "virtual",
2165 "explicit",
2166 "friend",
2167 "typedef",
2168 "__complex",
2169 "__thread"
2170 };
2171 error ("duplicate %qs", decl_spec_names[(int)ds]);
2172 }
2173 }
2174 }
2175
2176 /* This function is called when a type is defined. If type
2177 definitions are forbidden at this point, an error message is
2178 issued. */
2179
2180 static bool
2181 cp_parser_check_type_definition (cp_parser* parser)
2182 {
2183 /* If types are forbidden here, issue a message. */
2184 if (parser->type_definition_forbidden_message)
2185 {
2186 /* Use `%s' to print the string in case there are any escape
2187 characters in the message. */
2188 error ("%s", parser->type_definition_forbidden_message);
2189 return false;
2190 }
2191 return true;
2192 }
2193
2194 /* This function is called when the DECLARATOR is processed. The TYPE
2195 was a type defined in the decl-specifiers. If it is invalid to
2196 define a type in the decl-specifiers for DECLARATOR, an error is
2197 issued. */
2198
2199 static void
2200 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2201 tree type)
2202 {
2203 /* [dcl.fct] forbids type definitions in return types.
2204 Unfortunately, it's not easy to know whether or not we are
2205 processing a return type until after the fact. */
2206 while (declarator
2207 && (declarator->kind == cdk_pointer
2208 || declarator->kind == cdk_reference
2209 || declarator->kind == cdk_ptrmem))
2210 declarator = declarator->declarator;
2211 if (declarator
2212 && declarator->kind == cdk_function)
2213 {
2214 error ("new types may not be defined in a return type");
2215 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2216 type);
2217 }
2218 }
2219
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221 "<" in any valid C++ program. If the next token is indeed "<",
2222 issue a message warning the user about what appears to be an
2223 invalid attempt to form a template-id. */
2224
2225 static void
2226 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2227 tree type)
2228 {
2229 cp_token_position start = 0;
2230
2231 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2232 {
2233 if (TYPE_P (type))
2234 error ("%qT is not a template", type);
2235 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2236 error ("%qE is not a template", type);
2237 else
2238 error ("invalid template-id");
2239 /* Remember the location of the invalid "<". */
2240 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2241 start = cp_lexer_token_position (parser->lexer, true);
2242 /* Consume the "<". */
2243 cp_lexer_consume_token (parser->lexer);
2244 /* Parse the template arguments. */
2245 cp_parser_enclosed_template_argument_list (parser);
2246 /* Permanently remove the invalid template arguments so that
2247 this error message is not issued again. */
2248 if (start)
2249 cp_lexer_purge_tokens_after (parser->lexer, start);
2250 }
2251 }
2252
2253 /* If parsing an integral constant-expression, issue an error message
2254 about the fact that THING appeared and return true. Otherwise,
2255 return false. In either case, set
2256 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2257
2258 static bool
2259 cp_parser_non_integral_constant_expression (cp_parser *parser,
2260 const char *thing)
2261 {
2262 parser->non_integral_constant_expression_p = true;
2263 if (parser->integral_constant_expression_p)
2264 {
2265 if (!parser->allow_non_integral_constant_expression_p)
2266 {
2267 error ("%s cannot appear in a constant-expression", thing);
2268 return true;
2269 }
2270 }
2271 return false;
2272 }
2273
2274 /* Emit a diagnostic for an invalid type name. SCOPE is the
2275 qualifying scope (or NULL, if none) for ID. This function commits
2276 to the current active tentative parse, if any. (Otherwise, the
2277 problematic construct might be encountered again later, resulting
2278 in duplicate error messages.) */
2279
2280 static void
2281 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2282 {
2283 tree decl, old_scope;
2284 /* Try to lookup the identifier. */
2285 old_scope = parser->scope;
2286 parser->scope = scope;
2287 decl = cp_parser_lookup_name_simple (parser, id);
2288 parser->scope = old_scope;
2289 /* If the lookup found a template-name, it means that the user forgot
2290 to specify an argument list. Emit a useful error message. */
2291 if (TREE_CODE (decl) == TEMPLATE_DECL)
2292 error ("invalid use of template-name %qE without an argument list", decl);
2293 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2294 error ("invalid use of destructor %qD as a type", id);
2295 else if (TREE_CODE (decl) == TYPE_DECL)
2296 /* Something like 'unsigned A a;' */
2297 error ("invalid combination of multiple type-specifiers");
2298 else if (!parser->scope)
2299 {
2300 /* Issue an error message. */
2301 error ("%qE does not name a type", id);
2302 /* If we're in a template class, it's possible that the user was
2303 referring to a type from a base class. For example:
2304
2305 template <typename T> struct A { typedef T X; };
2306 template <typename T> struct B : public A<T> { X x; };
2307
2308 The user should have said "typename A<T>::X". */
2309 if (processing_template_decl && current_class_type
2310 && TYPE_BINFO (current_class_type))
2311 {
2312 tree b;
2313
2314 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2315 b;
2316 b = TREE_CHAIN (b))
2317 {
2318 tree base_type = BINFO_TYPE (b);
2319 if (CLASS_TYPE_P (base_type)
2320 && dependent_type_p (base_type))
2321 {
2322 tree field;
2323 /* Go from a particular instantiation of the
2324 template (which will have an empty TYPE_FIELDs),
2325 to the main version. */
2326 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2327 for (field = TYPE_FIELDS (base_type);
2328 field;
2329 field = TREE_CHAIN (field))
2330 if (TREE_CODE (field) == TYPE_DECL
2331 && DECL_NAME (field) == id)
2332 {
2333 inform ("(perhaps %<typename %T::%E%> was intended)",
2334 BINFO_TYPE (b), id);
2335 break;
2336 }
2337 if (field)
2338 break;
2339 }
2340 }
2341 }
2342 }
2343 /* Here we diagnose qualified-ids where the scope is actually correct,
2344 but the identifier does not resolve to a valid type name. */
2345 else if (parser->scope != error_mark_node)
2346 {
2347 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2348 error ("%qE in namespace %qE does not name a type",
2349 id, parser->scope);
2350 else if (TYPE_P (parser->scope))
2351 error ("%qE in class %qT does not name a type", id, parser->scope);
2352 else
2353 gcc_unreachable ();
2354 }
2355 cp_parser_commit_to_tentative_parse (parser);
2356 }
2357
2358 /* Check for a common situation where a type-name should be present,
2359 but is not, and issue a sensible error message. Returns true if an
2360 invalid type-name was detected.
2361
2362 The situation handled by this function are variable declarations of the
2363 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364 Usually, `ID' should name a type, but if we got here it means that it
2365 does not. We try to emit the best possible error message depending on
2366 how exactly the id-expression looks like. */
2367
2368 static bool
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2370 {
2371 tree id;
2372
2373 cp_parser_parse_tentatively (parser);
2374 id = cp_parser_id_expression (parser,
2375 /*template_keyword_p=*/false,
2376 /*check_dependency_p=*/true,
2377 /*template_p=*/NULL,
2378 /*declarator_p=*/true,
2379 /*optional_p=*/false);
2380 /* After the id-expression, there should be a plain identifier,
2381 otherwise this is not a simple variable declaration. Also, if
2382 the scope is dependent, we cannot do much. */
2383 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2384 || (parser->scope && TYPE_P (parser->scope)
2385 && dependent_type_p (parser->scope))
2386 || TREE_CODE (id) == TYPE_DECL)
2387 {
2388 cp_parser_abort_tentative_parse (parser);
2389 return false;
2390 }
2391 if (!cp_parser_parse_definitely (parser))
2392 return false;
2393
2394 /* Emit a diagnostic for the invalid type. */
2395 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2396 /* Skip to the end of the declaration; there's no point in
2397 trying to process it. */
2398 cp_parser_skip_to_end_of_block_or_statement (parser);
2399 return true;
2400 }
2401
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2404 are doing error recovery. Returns -1 if OR_COMMA is true and we
2405 found an unnested comma. */
2406
2407 static int
2408 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2409 bool recovering,
2410 bool or_comma,
2411 bool consume_paren)
2412 {
2413 unsigned paren_depth = 0;
2414 unsigned brace_depth = 0;
2415
2416 if (recovering && !or_comma
2417 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2418 return 0;
2419
2420 while (true)
2421 {
2422 cp_token * token = cp_lexer_peek_token (parser->lexer);
2423
2424 switch (token->type)
2425 {
2426 case CPP_EOF:
2427 case CPP_PRAGMA_EOL:
2428 /* If we've run out of tokens, then there is no closing `)'. */
2429 return 0;
2430
2431 case CPP_SEMICOLON:
2432 /* This matches the processing in skip_to_end_of_statement. */
2433 if (!brace_depth)
2434 return 0;
2435 break;
2436
2437 case CPP_OPEN_BRACE:
2438 ++brace_depth;
2439 break;
2440 case CPP_CLOSE_BRACE:
2441 if (!brace_depth--)
2442 return 0;
2443 break;
2444
2445 case CPP_COMMA:
2446 if (recovering && or_comma && !brace_depth && !paren_depth)
2447 return -1;
2448 break;
2449
2450 case CPP_OPEN_PAREN:
2451 if (!brace_depth)
2452 ++paren_depth;
2453 break;
2454
2455 case CPP_CLOSE_PAREN:
2456 if (!brace_depth && !paren_depth--)
2457 {
2458 if (consume_paren)
2459 cp_lexer_consume_token (parser->lexer);
2460 return 1;
2461 }
2462 break;
2463
2464 default:
2465 break;
2466 }
2467
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser->lexer);
2470 }
2471 }
2472
2473 /* Consume tokens until we reach the end of the current statement.
2474 Normally, that will be just before consuming a `;'. However, if a
2475 non-nested `}' comes first, then we stop before consuming that. */
2476
2477 static void
2478 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2479 {
2480 unsigned nesting_depth = 0;
2481
2482 while (true)
2483 {
2484 cp_token *token = cp_lexer_peek_token (parser->lexer);
2485
2486 switch (token->type)
2487 {
2488 case CPP_EOF:
2489 case CPP_PRAGMA_EOL:
2490 /* If we've run out of tokens, stop. */
2491 return;
2492
2493 case CPP_SEMICOLON:
2494 /* If the next token is a `;', we have reached the end of the
2495 statement. */
2496 if (!nesting_depth)
2497 return;
2498 break;
2499
2500 case CPP_CLOSE_BRACE:
2501 /* If this is a non-nested '}', stop before consuming it.
2502 That way, when confronted with something like:
2503
2504 { 3 + }
2505
2506 we stop before consuming the closing '}', even though we
2507 have not yet reached a `;'. */
2508 if (nesting_depth == 0)
2509 return;
2510
2511 /* If it is the closing '}' for a block that we have
2512 scanned, stop -- but only after consuming the token.
2513 That way given:
2514
2515 void f g () { ... }
2516 typedef int I;
2517
2518 we will stop after the body of the erroneously declared
2519 function, but before consuming the following `typedef'
2520 declaration. */
2521 if (--nesting_depth == 0)
2522 {
2523 cp_lexer_consume_token (parser->lexer);
2524 return;
2525 }
2526
2527 case CPP_OPEN_BRACE:
2528 ++nesting_depth;
2529 break;
2530
2531 default:
2532 break;
2533 }
2534
2535 /* Consume the token. */
2536 cp_lexer_consume_token (parser->lexer);
2537 }
2538 }
2539
2540 /* This function is called at the end of a statement or declaration.
2541 If the next token is a semicolon, it is consumed; otherwise, error
2542 recovery is attempted. */
2543
2544 static void
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2546 {
2547 /* Look for the trailing `;'. */
2548 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2549 {
2550 /* If there is additional (erroneous) input, skip to the end of
2551 the statement. */
2552 cp_parser_skip_to_end_of_statement (parser);
2553 /* If the next token is now a `;', consume it. */
2554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2555 cp_lexer_consume_token (parser->lexer);
2556 }
2557 }
2558
2559 /* Skip tokens until we have consumed an entire block, or until we
2560 have consumed a non-nested `;'. */
2561
2562 static void
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2564 {
2565 int nesting_depth = 0;
2566
2567 while (nesting_depth >= 0)
2568 {
2569 cp_token *token = cp_lexer_peek_token (parser->lexer);
2570
2571 switch (token->type)
2572 {
2573 case CPP_EOF:
2574 case CPP_PRAGMA_EOL:
2575 /* If we've run out of tokens, stop. */
2576 return;
2577
2578 case CPP_SEMICOLON:
2579 /* Stop if this is an unnested ';'. */
2580 if (!nesting_depth)
2581 nesting_depth = -1;
2582 break;
2583
2584 case CPP_CLOSE_BRACE:
2585 /* Stop if this is an unnested '}', or closes the outermost
2586 nesting level. */
2587 nesting_depth--;
2588 if (!nesting_depth)
2589 nesting_depth = -1;
2590 break;
2591
2592 case CPP_OPEN_BRACE:
2593 /* Nest. */
2594 nesting_depth++;
2595 break;
2596
2597 default:
2598 break;
2599 }
2600
2601 /* Consume the token. */
2602 cp_lexer_consume_token (parser->lexer);
2603 }
2604 }
2605
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607 token, or there are no more tokens. Return true in the first case,
2608 false otherwise. */
2609
2610 static bool
2611 cp_parser_skip_to_closing_brace (cp_parser *parser)
2612 {
2613 unsigned nesting_depth = 0;
2614
2615 while (true)
2616 {
2617 cp_token *token = cp_lexer_peek_token (parser->lexer);
2618
2619 switch (token->type)
2620 {
2621 case CPP_EOF:
2622 case CPP_PRAGMA_EOL:
2623 /* If we've run out of tokens, stop. */
2624 return false;
2625
2626 case CPP_CLOSE_BRACE:
2627 /* If the next token is a non-nested `}', then we have reached
2628 the end of the current block. */
2629 if (nesting_depth-- == 0)
2630 return true;
2631 break;
2632
2633 case CPP_OPEN_BRACE:
2634 /* If it the next token is a `{', then we are entering a new
2635 block. Consume the entire block. */
2636 ++nesting_depth;
2637 break;
2638
2639 default:
2640 break;
2641 }
2642
2643 /* Consume the token. */
2644 cp_lexer_consume_token (parser->lexer);
2645 }
2646 }
2647
2648 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2649 parameter is the PRAGMA token, allowing us to purge the entire pragma
2650 sequence. */
2651
2652 static void
2653 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2654 {
2655 cp_token *token;
2656
2657 parser->lexer->in_pragma = false;
2658
2659 do
2660 token = cp_lexer_consume_token (parser->lexer);
2661 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2662
2663 /* Ensure that the pragma is not parsed again. */
2664 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2665 }
2666
2667 /* Require pragma end of line, resyncing with it as necessary. The
2668 arguments are as for cp_parser_skip_to_pragma_eol. */
2669
2670 static void
2671 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2672 {
2673 parser->lexer->in_pragma = false;
2674 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2675 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2676 }
2677
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679 an unresolved identifier node, we can provide a superior diagnostic
2680 using cp_parser_diagnose_invalid_type_name. */
2681
2682 static tree
2683 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2684 {
2685 tree result;
2686 if (TREE_CODE (id) == IDENTIFIER_NODE)
2687 {
2688 result = make_typename_type (scope, id, typename_type,
2689 /*complain=*/tf_none);
2690 if (result == error_mark_node)
2691 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2692 return result;
2693 }
2694 return make_typename_type (scope, id, typename_type, tf_error);
2695 }
2696
2697 /* This is a wrapper around the
2698 make_{pointer,ptrmem,reference}_declarator functions that decides
2699 which one to call based on the CODE and CLASS_TYPE arguments. The
2700 CODE argument should be one of the values returned by
2701 cp_parser_ptr_operator. */
2702 static cp_declarator *
2703 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2704 cp_cv_quals cv_qualifiers,
2705 cp_declarator *target)
2706 {
2707 if (code == ERROR_MARK)
2708 return cp_error_declarator;
2709
2710 if (code == INDIRECT_REF)
2711 if (class_type == NULL_TREE)
2712 return make_pointer_declarator (cv_qualifiers, target);
2713 else
2714 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2715 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2716 return make_reference_declarator (cv_qualifiers, target, false);
2717 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2718 return make_reference_declarator (cv_qualifiers, target, true);
2719 gcc_unreachable ();
2720 }
2721
2722 /* Create a new C++ parser. */
2723
2724 static cp_parser *
2725 cp_parser_new (void)
2726 {
2727 cp_parser *parser;
2728 cp_lexer *lexer;
2729 unsigned i;
2730
2731 /* cp_lexer_new_main is called before calling ggc_alloc because
2732 cp_lexer_new_main might load a PCH file. */
2733 lexer = cp_lexer_new_main ();
2734
2735 /* Initialize the binops_by_token so that we can get the tree
2736 directly from the token. */
2737 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2738 binops_by_token[binops[i].token_type] = binops[i];
2739
2740 parser = GGC_CNEW (cp_parser);
2741 parser->lexer = lexer;
2742 parser->context = cp_parser_context_new (NULL);
2743
2744 /* For now, we always accept GNU extensions. */
2745 parser->allow_gnu_extensions_p = 1;
2746
2747 /* The `>' token is a greater-than operator, not the end of a
2748 template-id. */
2749 parser->greater_than_is_operator_p = true;
2750
2751 parser->default_arg_ok_p = true;
2752
2753 /* We are not parsing a constant-expression. */
2754 parser->integral_constant_expression_p = false;
2755 parser->allow_non_integral_constant_expression_p = false;
2756 parser->non_integral_constant_expression_p = false;
2757
2758 /* Local variable names are not forbidden. */
2759 parser->local_variables_forbidden_p = false;
2760
2761 /* We are not processing an `extern "C"' declaration. */
2762 parser->in_unbraced_linkage_specification_p = false;
2763
2764 /* We are not processing a declarator. */
2765 parser->in_declarator_p = false;
2766
2767 /* We are not processing a template-argument-list. */
2768 parser->in_template_argument_list_p = false;
2769
2770 /* We are not in an iteration statement. */
2771 parser->in_statement = 0;
2772
2773 /* We are not in a switch statement. */
2774 parser->in_switch_statement_p = false;
2775
2776 /* We are not parsing a type-id inside an expression. */
2777 parser->in_type_id_in_expr_p = false;
2778
2779 /* Declarations aren't implicitly extern "C". */
2780 parser->implicit_extern_c = false;
2781
2782 /* String literals should be translated to the execution character set. */
2783 parser->translate_strings_p = true;
2784
2785 /* We are not parsing a function body. */
2786 parser->in_function_body = false;
2787
2788 /* The unparsed function queue is empty. */
2789 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2790
2791 /* There are no classes being defined. */
2792 parser->num_classes_being_defined = 0;
2793
2794 /* No template parameters apply. */
2795 parser->num_template_parameter_lists = 0;
2796
2797 return parser;
2798 }
2799
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801 and push it onto the parser's lexer stack. This is used for delayed
2802 parsing of in-class method bodies and default arguments, and should
2803 not be confused with tentative parsing. */
2804 static void
2805 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2806 {
2807 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2808 lexer->next = parser->lexer;
2809 parser->lexer = lexer;
2810
2811 /* Move the current source position to that of the first token in the
2812 new lexer. */
2813 cp_lexer_set_source_position_from_token (lexer->next_token);
2814 }
2815
2816 /* Pop the top lexer off the parser stack. This is never used for the
2817 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2818 static void
2819 cp_parser_pop_lexer (cp_parser *parser)
2820 {
2821 cp_lexer *lexer = parser->lexer;
2822 parser->lexer = lexer->next;
2823 cp_lexer_destroy (lexer);
2824
2825 /* Put the current source position back where it was before this
2826 lexer was pushed. */
2827 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2828 }
2829
2830 /* Lexical conventions [gram.lex] */
2831
2832 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2833 identifier. */
2834
2835 static tree
2836 cp_parser_identifier (cp_parser* parser)
2837 {
2838 cp_token *token;
2839
2840 /* Look for the identifier. */
2841 token = cp_parser_require (parser, CPP_NAME, "identifier");
2842 /* Return the value. */
2843 return token ? token->u.value : error_mark_node;
2844 }
2845
2846 /* Parse a sequence of adjacent string constants. Returns a
2847 TREE_STRING representing the combined, nul-terminated string
2848 constant. If TRANSLATE is true, translate the string to the
2849 execution character set. If WIDE_OK is true, a wide string is
2850 invalid here.
2851
2852 C++98 [lex.string] says that if a narrow string literal token is
2853 adjacent to a wide string literal token, the behavior is undefined.
2854 However, C99 6.4.5p4 says that this results in a wide string literal.
2855 We follow C99 here, for consistency with the C front end.
2856
2857 This code is largely lifted from lex_string() in c-lex.c.
2858
2859 FUTURE: ObjC++ will need to handle @-strings here. */
2860 static tree
2861 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2862 {
2863 tree value;
2864 bool wide = false;
2865 size_t count;
2866 struct obstack str_ob;
2867 cpp_string str, istr, *strs;
2868 cp_token *tok;
2869
2870 tok = cp_lexer_peek_token (parser->lexer);
2871 if (!cp_parser_is_string_literal (tok))
2872 {
2873 cp_parser_error (parser, "expected string-literal");
2874 return error_mark_node;
2875 }
2876
2877 /* Try to avoid the overhead of creating and destroying an obstack
2878 for the common case of just one string. */
2879 if (!cp_parser_is_string_literal
2880 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2881 {
2882 cp_lexer_consume_token (parser->lexer);
2883
2884 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2885 str.len = TREE_STRING_LENGTH (tok->u.value);
2886 count = 1;
2887 if (tok->type == CPP_WSTRING)
2888 wide = true;
2889
2890 strs = &str;
2891 }
2892 else
2893 {
2894 gcc_obstack_init (&str_ob);
2895 count = 0;
2896
2897 do
2898 {
2899 cp_lexer_consume_token (parser->lexer);
2900 count++;
2901 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2902 str.len = TREE_STRING_LENGTH (tok->u.value);
2903 if (tok->type == CPP_WSTRING)
2904 wide = true;
2905
2906 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2907
2908 tok = cp_lexer_peek_token (parser->lexer);
2909 }
2910 while (cp_parser_is_string_literal (tok));
2911
2912 strs = (cpp_string *) obstack_finish (&str_ob);
2913 }
2914
2915 if (wide && !wide_ok)
2916 {
2917 cp_parser_error (parser, "a wide string is invalid in this context");
2918 wide = false;
2919 }
2920
2921 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2922 (parse_in, strs, count, &istr, wide))
2923 {
2924 value = build_string (istr.len, (const char *)istr.text);
2925 free (CONST_CAST (unsigned char *, istr.text));
2926
2927 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2928 value = fix_string_type (value);
2929 }
2930 else
2931 /* cpp_interpret_string has issued an error. */
2932 value = error_mark_node;
2933
2934 if (count > 1)
2935 obstack_free (&str_ob, 0);
2936
2937 return value;
2938 }
2939
2940
2941 /* Basic concepts [gram.basic] */
2942
2943 /* Parse a translation-unit.
2944
2945 translation-unit:
2946 declaration-seq [opt]
2947
2948 Returns TRUE if all went well. */
2949
2950 static bool
2951 cp_parser_translation_unit (cp_parser* parser)
2952 {
2953 /* The address of the first non-permanent object on the declarator
2954 obstack. */
2955 static void *declarator_obstack_base;
2956
2957 bool success;
2958
2959 /* Create the declarator obstack, if necessary. */
2960 if (!cp_error_declarator)
2961 {
2962 gcc_obstack_init (&declarator_obstack);
2963 /* Create the error declarator. */
2964 cp_error_declarator = make_declarator (cdk_error);
2965 /* Create the empty parameter list. */
2966 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2967 /* Remember where the base of the declarator obstack lies. */
2968 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2969 }
2970
2971 cp_parser_declaration_seq_opt (parser);
2972
2973 /* If there are no tokens left then all went well. */
2974 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2975 {
2976 /* Get rid of the token array; we don't need it any more. */
2977 cp_lexer_destroy (parser->lexer);
2978 parser->lexer = NULL;
2979
2980 /* This file might have been a context that's implicitly extern
2981 "C". If so, pop the lang context. (Only relevant for PCH.) */
2982 if (parser->implicit_extern_c)
2983 {
2984 pop_lang_context ();
2985 parser->implicit_extern_c = false;
2986 }
2987
2988 /* Finish up. */
2989 finish_translation_unit ();
2990
2991 success = true;
2992 }
2993 else
2994 {
2995 cp_parser_error (parser, "expected declaration");
2996 success = false;
2997 }
2998
2999 /* Make sure the declarator obstack was fully cleaned up. */
3000 gcc_assert (obstack_next_free (&declarator_obstack)
3001 == declarator_obstack_base);
3002
3003 /* All went well. */
3004 return success;
3005 }
3006
3007 /* Expressions [gram.expr] */
3008
3009 /* Parse a primary-expression.
3010
3011 primary-expression:
3012 literal
3013 this
3014 ( expression )
3015 id-expression
3016
3017 GNU Extensions:
3018
3019 primary-expression:
3020 ( compound-statement )
3021 __builtin_va_arg ( assignment-expression , type-id )
3022 __builtin_offsetof ( type-id , offsetof-expression )
3023
3024 C++ Extensions:
3025 __has_nothrow_assign ( type-id )
3026 __has_nothrow_constructor ( type-id )
3027 __has_nothrow_copy ( type-id )
3028 __has_trivial_assign ( type-id )
3029 __has_trivial_constructor ( type-id )
3030 __has_trivial_copy ( type-id )
3031 __has_trivial_destructor ( type-id )
3032 __has_virtual_destructor ( type-id )
3033 __is_abstract ( type-id )
3034 __is_base_of ( type-id , type-id )
3035 __is_class ( type-id )
3036 __is_convertible_to ( type-id , type-id )
3037 __is_empty ( type-id )
3038 __is_enum ( type-id )
3039 __is_pod ( type-id )
3040 __is_polymorphic ( type-id )
3041 __is_union ( type-id )
3042
3043 Objective-C++ Extension:
3044
3045 primary-expression:
3046 objc-expression
3047
3048 literal:
3049 __null
3050
3051 ADDRESS_P is true iff this expression was immediately preceded by
3052 "&" and therefore might denote a pointer-to-member. CAST_P is true
3053 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3054 true iff this expression is a template argument.
3055
3056 Returns a representation of the expression. Upon return, *IDK
3057 indicates what kind of id-expression (if any) was present. */
3058
3059 static tree
3060 cp_parser_primary_expression (cp_parser *parser,
3061 bool address_p,
3062 bool cast_p,
3063 bool template_arg_p,
3064 cp_id_kind *idk)
3065 {
3066 cp_token *token;
3067
3068 /* Assume the primary expression is not an id-expression. */
3069 *idk = CP_ID_KIND_NONE;
3070
3071 /* Peek at the next token. */
3072 token = cp_lexer_peek_token (parser->lexer);
3073 switch (token->type)
3074 {
3075 /* literal:
3076 integer-literal
3077 character-literal
3078 floating-literal
3079 string-literal
3080 boolean-literal */
3081 case CPP_CHAR:
3082 case CPP_WCHAR:
3083 case CPP_NUMBER:
3084 token = cp_lexer_consume_token (parser->lexer);
3085 /* Floating-point literals are only allowed in an integral
3086 constant expression if they are cast to an integral or
3087 enumeration type. */
3088 if (TREE_CODE (token->u.value) == REAL_CST
3089 && parser->integral_constant_expression_p
3090 && pedantic)
3091 {
3092 /* CAST_P will be set even in invalid code like "int(2.7 +
3093 ...)". Therefore, we have to check that the next token
3094 is sure to end the cast. */
3095 if (cast_p)
3096 {
3097 cp_token *next_token;
3098
3099 next_token = cp_lexer_peek_token (parser->lexer);
3100 if (/* The comma at the end of an
3101 enumerator-definition. */
3102 next_token->type != CPP_COMMA
3103 /* The curly brace at the end of an enum-specifier. */
3104 && next_token->type != CPP_CLOSE_BRACE
3105 /* The end of a statement. */
3106 && next_token->type != CPP_SEMICOLON
3107 /* The end of the cast-expression. */
3108 && next_token->type != CPP_CLOSE_PAREN
3109 /* The end of an array bound. */
3110 && next_token->type != CPP_CLOSE_SQUARE
3111 /* The closing ">" in a template-argument-list. */
3112 && (next_token->type != CPP_GREATER
3113 || parser->greater_than_is_operator_p)
3114 /* C++0x only: A ">>" treated like two ">" tokens,
3115 in a template-argument-list. */
3116 && (next_token->type != CPP_RSHIFT
3117 || (cxx_dialect == cxx98)
3118 || parser->greater_than_is_operator_p))
3119 cast_p = false;
3120 }
3121
3122 /* If we are within a cast, then the constraint that the
3123 cast is to an integral or enumeration type will be
3124 checked at that point. If we are not within a cast, then
3125 this code is invalid. */
3126 if (!cast_p)
3127 cp_parser_non_integral_constant_expression
3128 (parser, "floating-point literal");
3129 }
3130 return token->u.value;
3131
3132 case CPP_STRING:
3133 case CPP_WSTRING:
3134 /* ??? Should wide strings be allowed when parser->translate_strings_p
3135 is false (i.e. in attributes)? If not, we can kill the third
3136 argument to cp_parser_string_literal. */
3137 return cp_parser_string_literal (parser,
3138 parser->translate_strings_p,
3139 true);
3140
3141 case CPP_OPEN_PAREN:
3142 {
3143 tree expr;
3144 bool saved_greater_than_is_operator_p;
3145
3146 /* Consume the `('. */
3147 cp_lexer_consume_token (parser->lexer);
3148 /* Within a parenthesized expression, a `>' token is always
3149 the greater-than operator. */
3150 saved_greater_than_is_operator_p
3151 = parser->greater_than_is_operator_p;
3152 parser->greater_than_is_operator_p = true;
3153 /* If we see `( { ' then we are looking at the beginning of
3154 a GNU statement-expression. */
3155 if (cp_parser_allow_gnu_extensions_p (parser)
3156 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3157 {
3158 /* Statement-expressions are not allowed by the standard. */
3159 if (pedantic)
3160 pedwarn ("ISO C++ forbids braced-groups within expressions");
3161
3162 /* And they're not allowed outside of a function-body; you
3163 cannot, for example, write:
3164
3165 int i = ({ int j = 3; j + 1; });
3166
3167 at class or namespace scope. */
3168 if (!parser->in_function_body
3169 || parser->in_template_argument_list_p)
3170 {
3171 error ("statement-expressions are not allowed outside "
3172 "functions nor in template-argument lists");
3173 cp_parser_skip_to_end_of_block_or_statement (parser);
3174 expr = error_mark_node;
3175 }
3176 else
3177 {
3178 /* Start the statement-expression. */
3179 expr = begin_stmt_expr ();
3180 /* Parse the compound-statement. */
3181 cp_parser_compound_statement (parser, expr, false);
3182 /* Finish up. */
3183 expr = finish_stmt_expr (expr, false);
3184 }
3185 }
3186 else
3187 {
3188 /* Parse the parenthesized expression. */
3189 expr = cp_parser_expression (parser, cast_p);
3190 /* Let the front end know that this expression was
3191 enclosed in parentheses. This matters in case, for
3192 example, the expression is of the form `A::B', since
3193 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3194 not. */
3195 finish_parenthesized_expr (expr);
3196 }
3197 /* The `>' token might be the end of a template-id or
3198 template-parameter-list now. */
3199 parser->greater_than_is_operator_p
3200 = saved_greater_than_is_operator_p;
3201 /* Consume the `)'. */
3202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3203 cp_parser_skip_to_end_of_statement (parser);
3204
3205 return expr;
3206 }
3207
3208 case CPP_KEYWORD:
3209 switch (token->keyword)
3210 {
3211 /* These two are the boolean literals. */
3212 case RID_TRUE:
3213 cp_lexer_consume_token (parser->lexer);
3214 return boolean_true_node;
3215 case RID_FALSE:
3216 cp_lexer_consume_token (parser->lexer);
3217 return boolean_false_node;
3218
3219 /* The `__null' literal. */
3220 case RID_NULL:
3221 cp_lexer_consume_token (parser->lexer);
3222 return null_node;
3223
3224 /* Recognize the `this' keyword. */
3225 case RID_THIS:
3226 cp_lexer_consume_token (parser->lexer);
3227 if (parser->local_variables_forbidden_p)
3228 {
3229 error ("%<this%> may not be used in this context");
3230 return error_mark_node;
3231 }
3232 /* Pointers cannot appear in constant-expressions. */
3233 if (cp_parser_non_integral_constant_expression (parser,
3234 "`this'"))
3235 return error_mark_node;
3236 return finish_this_expr ();
3237
3238 /* The `operator' keyword can be the beginning of an
3239 id-expression. */
3240 case RID_OPERATOR:
3241 goto id_expression;
3242
3243 case RID_FUNCTION_NAME:
3244 case RID_PRETTY_FUNCTION_NAME:
3245 case RID_C99_FUNCTION_NAME:
3246 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247 __func__ are the names of variables -- but they are
3248 treated specially. Therefore, they are handled here,
3249 rather than relying on the generic id-expression logic
3250 below. Grammatically, these names are id-expressions.
3251
3252 Consume the token. */
3253 token = cp_lexer_consume_token (parser->lexer);
3254 /* Look up the name. */
3255 return finish_fname (token->u.value);
3256
3257 case RID_VA_ARG:
3258 {
3259 tree expression;
3260 tree type;
3261
3262 /* The `__builtin_va_arg' construct is used to handle
3263 `va_arg'. Consume the `__builtin_va_arg' token. */
3264 cp_lexer_consume_token (parser->lexer);
3265 /* Look for the opening `('. */
3266 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3267 /* Now, parse the assignment-expression. */
3268 expression = cp_parser_assignment_expression (parser,
3269 /*cast_p=*/false);
3270 /* Look for the `,'. */
3271 cp_parser_require (parser, CPP_COMMA, "`,'");
3272 /* Parse the type-id. */
3273 type = cp_parser_type_id (parser);
3274 /* Look for the closing `)'. */
3275 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3276 /* Using `va_arg' in a constant-expression is not
3277 allowed. */
3278 if (cp_parser_non_integral_constant_expression (parser,
3279 "`va_arg'"))
3280 return error_mark_node;
3281 return build_x_va_arg (expression, type);
3282 }
3283
3284 case RID_OFFSETOF:
3285 return cp_parser_builtin_offsetof (parser);
3286
3287 case RID_HAS_NOTHROW_ASSIGN:
3288 case RID_HAS_NOTHROW_CONSTRUCTOR:
3289 case RID_HAS_NOTHROW_COPY:
3290 case RID_HAS_TRIVIAL_ASSIGN:
3291 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3292 case RID_HAS_TRIVIAL_COPY:
3293 case RID_HAS_TRIVIAL_DESTRUCTOR:
3294 case RID_HAS_VIRTUAL_DESTRUCTOR:
3295 case RID_IS_ABSTRACT:
3296 case RID_IS_BASE_OF:
3297 case RID_IS_CLASS:
3298 case RID_IS_CONVERTIBLE_TO:
3299 case RID_IS_EMPTY:
3300 case RID_IS_ENUM:
3301 case RID_IS_POD:
3302 case RID_IS_POLYMORPHIC:
3303 case RID_IS_UNION:
3304 return cp_parser_trait_expr (parser, token->keyword);
3305
3306 /* Objective-C++ expressions. */
3307 case RID_AT_ENCODE:
3308 case RID_AT_PROTOCOL:
3309 case RID_AT_SELECTOR:
3310 return cp_parser_objc_expression (parser);
3311
3312 default:
3313 cp_parser_error (parser, "expected primary-expression");
3314 return error_mark_node;
3315 }
3316
3317 /* An id-expression can start with either an identifier, a
3318 `::' as the beginning of a qualified-id, or the "operator"
3319 keyword. */
3320 case CPP_NAME:
3321 case CPP_SCOPE:
3322 case CPP_TEMPLATE_ID:
3323 case CPP_NESTED_NAME_SPECIFIER:
3324 {
3325 tree id_expression;
3326 tree decl;
3327 const char *error_msg;
3328 bool template_p;
3329 bool done;
3330
3331 id_expression:
3332 /* Parse the id-expression. */
3333 id_expression
3334 = cp_parser_id_expression (parser,
3335 /*template_keyword_p=*/false,
3336 /*check_dependency_p=*/true,
3337 &template_p,
3338 /*declarator_p=*/false,
3339 /*optional_p=*/false);
3340 if (id_expression == error_mark_node)
3341 return error_mark_node;
3342 token = cp_lexer_peek_token (parser->lexer);
3343 done = (token->type != CPP_OPEN_SQUARE
3344 && token->type != CPP_OPEN_PAREN
3345 && token->type != CPP_DOT
3346 && token->type != CPP_DEREF
3347 && token->type != CPP_PLUS_PLUS
3348 && token->type != CPP_MINUS_MINUS);
3349 /* If we have a template-id, then no further lookup is
3350 required. If the template-id was for a template-class, we
3351 will sometimes have a TYPE_DECL at this point. */
3352 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3353 || TREE_CODE (id_expression) == TYPE_DECL)
3354 decl = id_expression;
3355 /* Look up the name. */
3356 else
3357 {
3358 tree ambiguous_decls;
3359
3360 decl = cp_parser_lookup_name (parser, id_expression,
3361 none_type,
3362 template_p,
3363 /*is_namespace=*/false,
3364 /*check_dependency=*/true,
3365 &ambiguous_decls);
3366 /* If the lookup was ambiguous, an error will already have
3367 been issued. */
3368 if (ambiguous_decls)
3369 return error_mark_node;
3370
3371 /* In Objective-C++, an instance variable (ivar) may be preferred
3372 to whatever cp_parser_lookup_name() found. */
3373 decl = objc_lookup_ivar (decl, id_expression);
3374
3375 /* If name lookup gives us a SCOPE_REF, then the
3376 qualifying scope was dependent. */
3377 if (TREE_CODE (decl) == SCOPE_REF)
3378 {
3379 /* At this point, we do not know if DECL is a valid
3380 integral constant expression. We assume that it is
3381 in fact such an expression, so that code like:
3382
3383 template <int N> struct A {
3384 int a[B<N>::i];
3385 };
3386
3387 is accepted. At template-instantiation time, we
3388 will check that B<N>::i is actually a constant. */
3389 return decl;
3390 }
3391 /* Check to see if DECL is a local variable in a context
3392 where that is forbidden. */
3393 if (parser->local_variables_forbidden_p
3394 && local_variable_p (decl))
3395 {
3396 /* It might be that we only found DECL because we are
3397 trying to be generous with pre-ISO scoping rules.
3398 For example, consider:
3399
3400 int i;
3401 void g() {
3402 for (int i = 0; i < 10; ++i) {}
3403 extern void f(int j = i);
3404 }
3405
3406 Here, name look up will originally find the out
3407 of scope `i'. We need to issue a warning message,
3408 but then use the global `i'. */
3409 decl = check_for_out_of_scope_variable (decl);
3410 if (local_variable_p (decl))
3411 {
3412 error ("local variable %qD may not appear in this context",
3413 decl);
3414 return error_mark_node;
3415 }
3416 }
3417 }
3418
3419 decl = (finish_id_expression
3420 (id_expression, decl, parser->scope,
3421 idk,
3422 parser->integral_constant_expression_p,
3423 parser->allow_non_integral_constant_expression_p,
3424 &parser->non_integral_constant_expression_p,
3425 template_p, done, address_p,
3426 template_arg_p,
3427 &error_msg));
3428 if (error_msg)
3429 cp_parser_error (parser, error_msg);
3430 return decl;
3431 }
3432
3433 /* Anything else is an error. */
3434 default:
3435 /* ...unless we have an Objective-C++ message or string literal,
3436 that is. */
3437 if (c_dialect_objc ()
3438 && (token->type == CPP_OPEN_SQUARE
3439 || token->type == CPP_OBJC_STRING))
3440 return cp_parser_objc_expression (parser);
3441
3442 cp_parser_error (parser, "expected primary-expression");
3443 return error_mark_node;
3444 }
3445 }
3446
3447 /* Parse an id-expression.
3448
3449 id-expression:
3450 unqualified-id
3451 qualified-id
3452
3453 qualified-id:
3454 :: [opt] nested-name-specifier template [opt] unqualified-id
3455 :: identifier
3456 :: operator-function-id
3457 :: template-id
3458
3459 Return a representation of the unqualified portion of the
3460 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3461 a `::' or nested-name-specifier.
3462
3463 Often, if the id-expression was a qualified-id, the caller will
3464 want to make a SCOPE_REF to represent the qualified-id. This
3465 function does not do this in order to avoid wastefully creating
3466 SCOPE_REFs when they are not required.
3467
3468 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3469 `template' keyword.
3470
3471 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472 uninstantiated templates.
3473
3474 If *TEMPLATE_P is non-NULL, it is set to true iff the
3475 `template' keyword is used to explicitly indicate that the entity
3476 named is a template.
3477
3478 If DECLARATOR_P is true, the id-expression is appearing as part of
3479 a declarator, rather than as part of an expression. */
3480
3481 static tree
3482 cp_parser_id_expression (cp_parser *parser,
3483 bool template_keyword_p,
3484 bool check_dependency_p,
3485 bool *template_p,
3486 bool declarator_p,
3487 bool optional_p)
3488 {
3489 bool global_scope_p;
3490 bool nested_name_specifier_p;
3491
3492 /* Assume the `template' keyword was not used. */
3493 if (template_p)
3494 *template_p = template_keyword_p;
3495
3496 /* Look for the optional `::' operator. */
3497 global_scope_p
3498 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3499 != NULL_TREE);
3500 /* Look for the optional nested-name-specifier. */
3501 nested_name_specifier_p
3502 = (cp_parser_nested_name_specifier_opt (parser,
3503 /*typename_keyword_p=*/false,
3504 check_dependency_p,
3505 /*type_p=*/false,
3506 declarator_p)
3507 != NULL_TREE);
3508 /* If there is a nested-name-specifier, then we are looking at
3509 the first qualified-id production. */
3510 if (nested_name_specifier_p)
3511 {
3512 tree saved_scope;
3513 tree saved_object_scope;
3514 tree saved_qualifying_scope;
3515 tree unqualified_id;
3516 bool is_template;
3517
3518 /* See if the next token is the `template' keyword. */
3519 if (!template_p)
3520 template_p = &is_template;
3521 *template_p = cp_parser_optional_template_keyword (parser);
3522 /* Name lookup we do during the processing of the
3523 unqualified-id might obliterate SCOPE. */
3524 saved_scope = parser->scope;
3525 saved_object_scope = parser->object_scope;
3526 saved_qualifying_scope = parser->qualifying_scope;
3527 /* Process the final unqualified-id. */
3528 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3529 check_dependency_p,
3530 declarator_p,
3531 /*optional_p=*/false);
3532 /* Restore the SAVED_SCOPE for our caller. */
3533 parser->scope = saved_scope;
3534 parser->object_scope = saved_object_scope;
3535 parser->qualifying_scope = saved_qualifying_scope;
3536
3537 return unqualified_id;
3538 }
3539 /* Otherwise, if we are in global scope, then we are looking at one
3540 of the other qualified-id productions. */
3541 else if (global_scope_p)
3542 {
3543 cp_token *token;
3544 tree id;
3545
3546 /* Peek at the next token. */
3547 token = cp_lexer_peek_token (parser->lexer);
3548
3549 /* If it's an identifier, and the next token is not a "<", then
3550 we can avoid the template-id case. This is an optimization
3551 for this common case. */
3552 if (token->type == CPP_NAME
3553 && !cp_parser_nth_token_starts_template_argument_list_p
3554 (parser, 2))
3555 return cp_parser_identifier (parser);
3556
3557 cp_parser_parse_tentatively (parser);
3558 /* Try a template-id. */
3559 id = cp_parser_template_id (parser,
3560 /*template_keyword_p=*/false,
3561 /*check_dependency_p=*/true,
3562 declarator_p);
3563 /* If that worked, we're done. */
3564 if (cp_parser_parse_definitely (parser))
3565 return id;
3566
3567 /* Peek at the next token. (Changes in the token buffer may
3568 have invalidated the pointer obtained above.) */
3569 token = cp_lexer_peek_token (parser->lexer);
3570
3571 switch (token->type)
3572 {
3573 case CPP_NAME:
3574 return cp_parser_identifier (parser);
3575
3576 case CPP_KEYWORD:
3577 if (token->keyword == RID_OPERATOR)
3578 return cp_parser_operator_function_id (parser);
3579 /* Fall through. */
3580
3581 default:
3582 cp_parser_error (parser, "expected id-expression");
3583 return error_mark_node;
3584 }
3585 }
3586 else
3587 return cp_parser_unqualified_id (parser, template_keyword_p,
3588 /*check_dependency_p=*/true,
3589 declarator_p,
3590 optional_p);
3591 }
3592
3593 /* Parse an unqualified-id.
3594
3595 unqualified-id:
3596 identifier
3597 operator-function-id
3598 conversion-function-id
3599 ~ class-name
3600 template-id
3601
3602 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603 keyword, in a construct like `A::template ...'.
3604
3605 Returns a representation of unqualified-id. For the `identifier'
3606 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3607 production a BIT_NOT_EXPR is returned; the operand of the
3608 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3609 other productions, see the documentation accompanying the
3610 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3611 names are looked up in uninstantiated templates. If DECLARATOR_P
3612 is true, the unqualified-id is appearing as part of a declarator,
3613 rather than as part of an expression. */
3614
3615 static tree
3616 cp_parser_unqualified_id (cp_parser* parser,
3617 bool template_keyword_p,
3618 bool check_dependency_p,
3619 bool declarator_p,
3620 bool optional_p)
3621 {
3622 cp_token *token;
3623
3624 /* Peek at the next token. */
3625 token = cp_lexer_peek_token (parser->lexer);
3626
3627 switch (token->type)
3628 {
3629 case CPP_NAME:
3630 {
3631 tree id;
3632
3633 /* We don't know yet whether or not this will be a
3634 template-id. */
3635 cp_parser_parse_tentatively (parser);
3636 /* Try a template-id. */
3637 id = cp_parser_template_id (parser, template_keyword_p,
3638 check_dependency_p,
3639 declarator_p);
3640 /* If it worked, we're done. */
3641 if (cp_parser_parse_definitely (parser))
3642 return id;
3643 /* Otherwise, it's an ordinary identifier. */
3644 return cp_parser_identifier (parser);
3645 }
3646
3647 case CPP_TEMPLATE_ID:
3648 return cp_parser_template_id (parser, template_keyword_p,
3649 check_dependency_p,
3650 declarator_p);
3651
3652 case CPP_COMPL:
3653 {
3654 tree type_decl;
3655 tree qualifying_scope;
3656 tree object_scope;
3657 tree scope;
3658 bool done;
3659
3660 /* Consume the `~' token. */
3661 cp_lexer_consume_token (parser->lexer);
3662 /* Parse the class-name. The standard, as written, seems to
3663 say that:
3664
3665 template <typename T> struct S { ~S (); };
3666 template <typename T> S<T>::~S() {}
3667
3668 is invalid, since `~' must be followed by a class-name, but
3669 `S<T>' is dependent, and so not known to be a class.
3670 That's not right; we need to look in uninstantiated
3671 templates. A further complication arises from:
3672
3673 template <typename T> void f(T t) {
3674 t.T::~T();
3675 }
3676
3677 Here, it is not possible to look up `T' in the scope of `T'
3678 itself. We must look in both the current scope, and the
3679 scope of the containing complete expression.
3680
3681 Yet another issue is:
3682
3683 struct S {
3684 int S;
3685 ~S();
3686 };
3687
3688 S::~S() {}
3689
3690 The standard does not seem to say that the `S' in `~S'
3691 should refer to the type `S' and not the data member
3692 `S::S'. */
3693
3694 /* DR 244 says that we look up the name after the "~" in the
3695 same scope as we looked up the qualifying name. That idea
3696 isn't fully worked out; it's more complicated than that. */
3697 scope = parser->scope;
3698 object_scope = parser->object_scope;
3699 qualifying_scope = parser->qualifying_scope;
3700
3701 /* Check for invalid scopes. */
3702 if (scope == error_mark_node)
3703 {
3704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3705 cp_lexer_consume_token (parser->lexer);
3706 return error_mark_node;
3707 }
3708 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3709 {
3710 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3711 error ("scope %qT before %<~%> is not a class-name", scope);
3712 cp_parser_simulate_error (parser);
3713 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3714 cp_lexer_consume_token (parser->lexer);
3715 return error_mark_node;
3716 }
3717 gcc_assert (!scope || TYPE_P (scope));
3718
3719 /* If the name is of the form "X::~X" it's OK. */
3720 token = cp_lexer_peek_token (parser->lexer);
3721 if (scope
3722 && token->type == CPP_NAME
3723 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3724 == CPP_OPEN_PAREN)
3725 && constructor_name_p (token->u.value, scope))
3726 {
3727 cp_lexer_consume_token (parser->lexer);
3728 return build_nt (BIT_NOT_EXPR, scope);
3729 }
3730
3731 /* If there was an explicit qualification (S::~T), first look
3732 in the scope given by the qualification (i.e., S). */
3733 done = false;
3734 type_decl = NULL_TREE;
3735 if (scope)
3736 {
3737 cp_parser_parse_tentatively (parser);
3738 type_decl = cp_parser_class_name (parser,
3739 /*typename_keyword_p=*/false,
3740 /*template_keyword_p=*/false,
3741 none_type,
3742 /*check_dependency=*/false,
3743 /*class_head_p=*/false,
3744 declarator_p);
3745 if (cp_parser_parse_definitely (parser))
3746 done = true;
3747 }
3748 /* In "N::S::~S", look in "N" as well. */
3749 if (!done && scope && qualifying_scope)
3750 {
3751 cp_parser_parse_tentatively (parser);
3752 parser->scope = qualifying_scope;
3753 parser->object_scope = NULL_TREE;
3754 parser->qualifying_scope = NULL_TREE;
3755 type_decl
3756 = cp_parser_class_name (parser,
3757 /*typename_keyword_p=*/false,
3758 /*template_keyword_p=*/false,
3759 none_type,
3760 /*check_dependency=*/false,
3761 /*class_head_p=*/false,
3762 declarator_p);
3763 if (cp_parser_parse_definitely (parser))
3764 done = true;
3765 }
3766 /* In "p->S::~T", look in the scope given by "*p" as well. */
3767 else if (!done && object_scope)
3768 {
3769 cp_parser_parse_tentatively (parser);
3770 parser->scope = object_scope;
3771 parser->object_scope = NULL_TREE;
3772 parser->qualifying_scope = NULL_TREE;
3773 type_decl
3774 = cp_parser_class_name (parser,
3775 /*typename_keyword_p=*/false,
3776 /*template_keyword_p=*/false,
3777 none_type,
3778 /*check_dependency=*/false,
3779 /*class_head_p=*/false,
3780 declarator_p);
3781 if (cp_parser_parse_definitely (parser))
3782 done = true;
3783 }
3784 /* Look in the surrounding context. */
3785 if (!done)
3786 {
3787 parser->scope = NULL_TREE;
3788 parser->object_scope = NULL_TREE;
3789 parser->qualifying_scope = NULL_TREE;
3790 type_decl
3791 = cp_parser_class_name (parser,
3792 /*typename_keyword_p=*/false,
3793 /*template_keyword_p=*/false,
3794 none_type,
3795 /*check_dependency=*/false,
3796 /*class_head_p=*/false,
3797 declarator_p);
3798 }
3799 /* If an error occurred, assume that the name of the
3800 destructor is the same as the name of the qualifying
3801 class. That allows us to keep parsing after running
3802 into ill-formed destructor names. */
3803 if (type_decl == error_mark_node && scope)
3804 return build_nt (BIT_NOT_EXPR, scope);
3805 else if (type_decl == error_mark_node)
3806 return error_mark_node;
3807
3808 /* Check that destructor name and scope match. */
3809 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3810 {
3811 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3812 error ("declaration of %<~%T%> as member of %qT",
3813 type_decl, scope);
3814 cp_parser_simulate_error (parser);
3815 return error_mark_node;
3816 }
3817
3818 /* [class.dtor]
3819
3820 A typedef-name that names a class shall not be used as the
3821 identifier in the declarator for a destructor declaration. */
3822 if (declarator_p
3823 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3824 && !DECL_SELF_REFERENCE_P (type_decl)
3825 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3826 error ("typedef-name %qD used as destructor declarator",
3827 type_decl);
3828
3829 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3830 }
3831
3832 case CPP_KEYWORD:
3833 if (token->keyword == RID_OPERATOR)
3834 {
3835 tree id;
3836
3837 /* This could be a template-id, so we try that first. */
3838 cp_parser_parse_tentatively (parser);
3839 /* Try a template-id. */
3840 id = cp_parser_template_id (parser, template_keyword_p,
3841 /*check_dependency_p=*/true,
3842 declarator_p);
3843 /* If that worked, we're done. */
3844 if (cp_parser_parse_definitely (parser))
3845 return id;
3846 /* We still don't know whether we're looking at an
3847 operator-function-id or a conversion-function-id. */
3848 cp_parser_parse_tentatively (parser);
3849 /* Try an operator-function-id. */
3850 id = cp_parser_operator_function_id (parser);
3851 /* If that didn't work, try a conversion-function-id. */
3852 if (!cp_parser_parse_definitely (parser))
3853 id = cp_parser_conversion_function_id (parser);
3854
3855 return id;
3856 }
3857 /* Fall through. */
3858
3859 default:
3860 if (optional_p)
3861 return NULL_TREE;
3862 cp_parser_error (parser, "expected unqualified-id");
3863 return error_mark_node;
3864 }
3865 }
3866
3867 /* Parse an (optional) nested-name-specifier.
3868
3869 nested-name-specifier:
3870 class-or-namespace-name :: nested-name-specifier [opt]
3871 class-or-namespace-name :: template nested-name-specifier [opt]
3872
3873 PARSER->SCOPE should be set appropriately before this function is
3874 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3876 in name lookups.
3877
3878 Sets PARSER->SCOPE to the class (TYPE) or namespace
3879 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880 it unchanged if there is no nested-name-specifier. Returns the new
3881 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3882
3883 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884 part of a declaration and/or decl-specifier. */
3885
3886 static tree
3887 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3888 bool typename_keyword_p,
3889 bool check_dependency_p,
3890 bool type_p,
3891 bool is_declaration)
3892 {
3893 bool success = false;
3894 cp_token_position start = 0;
3895 cp_token *token;
3896
3897 /* Remember where the nested-name-specifier starts. */
3898 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3899 {
3900 start = cp_lexer_token_position (parser->lexer, false);
3901 push_deferring_access_checks (dk_deferred);
3902 }
3903
3904 while (true)
3905 {
3906 tree new_scope;
3907 tree old_scope;
3908 tree saved_qualifying_scope;
3909 bool template_keyword_p;
3910
3911 /* Spot cases that cannot be the beginning of a
3912 nested-name-specifier. */
3913 token = cp_lexer_peek_token (parser->lexer);
3914
3915 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916 the already parsed nested-name-specifier. */
3917 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3918 {
3919 /* Grab the nested-name-specifier and continue the loop. */
3920 cp_parser_pre_parsed_nested_name_specifier (parser);
3921 /* If we originally encountered this nested-name-specifier
3922 with IS_DECLARATION set to false, we will not have
3923 resolved TYPENAME_TYPEs, so we must do so here. */
3924 if (is_declaration
3925 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3926 {
3927 new_scope = resolve_typename_type (parser->scope,
3928 /*only_current_p=*/false);
3929 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3930 parser->scope = new_scope;
3931 }
3932 success = true;
3933 continue;
3934 }
3935
3936 /* Spot cases that cannot be the beginning of a
3937 nested-name-specifier. On the second and subsequent times
3938 through the loop, we look for the `template' keyword. */
3939 if (success && token->keyword == RID_TEMPLATE)
3940 ;
3941 /* A template-id can start a nested-name-specifier. */
3942 else if (token->type == CPP_TEMPLATE_ID)
3943 ;
3944 else
3945 {
3946 /* If the next token is not an identifier, then it is
3947 definitely not a class-or-namespace-name. */
3948 if (token->type != CPP_NAME)
3949 break;
3950 /* If the following token is neither a `<' (to begin a
3951 template-id), nor a `::', then we are not looking at a
3952 nested-name-specifier. */
3953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3954 if (token->type != CPP_SCOPE
3955 && !cp_parser_nth_token_starts_template_argument_list_p
3956 (parser, 2))
3957 break;
3958 }
3959
3960 /* The nested-name-specifier is optional, so we parse
3961 tentatively. */
3962 cp_parser_parse_tentatively (parser);
3963
3964 /* Look for the optional `template' keyword, if this isn't the
3965 first time through the loop. */
3966 if (success)
3967 template_keyword_p = cp_parser_optional_template_keyword (parser);
3968 else
3969 template_keyword_p = false;
3970
3971 /* Save the old scope since the name lookup we are about to do
3972 might destroy it. */
3973 old_scope = parser->scope;
3974 saved_qualifying_scope = parser->qualifying_scope;
3975 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976 look up names in "X<T>::I" in order to determine that "Y" is
3977 a template. So, if we have a typename at this point, we make
3978 an effort to look through it. */
3979 if (is_declaration
3980 && !typename_keyword_p
3981 && parser->scope
3982 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3983 parser->scope = resolve_typename_type (parser->scope,
3984 /*only_current_p=*/false);
3985 /* Parse the qualifying entity. */
3986 new_scope
3987 = cp_parser_class_or_namespace_name (parser,
3988 typename_keyword_p,
3989 template_keyword_p,
3990 check_dependency_p,
3991 type_p,
3992 is_declaration);
3993 /* Look for the `::' token. */
3994 cp_parser_require (parser, CPP_SCOPE, "`::'");
3995
3996 /* If we found what we wanted, we keep going; otherwise, we're
3997 done. */
3998 if (!cp_parser_parse_definitely (parser))
3999 {
4000 bool error_p = false;
4001
4002 /* Restore the OLD_SCOPE since it was valid before the
4003 failed attempt at finding the last
4004 class-or-namespace-name. */
4005 parser->scope = old_scope;
4006 parser->qualifying_scope = saved_qualifying_scope;
4007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4008 break;
4009 /* If the next token is an identifier, and the one after
4010 that is a `::', then any valid interpretation would have
4011 found a class-or-namespace-name. */
4012 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4013 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4014 == CPP_SCOPE)
4015 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4016 != CPP_COMPL))
4017 {
4018 token = cp_lexer_consume_token (parser->lexer);
4019 if (!error_p)
4020 {
4021 if (!token->ambiguous_p)
4022 {
4023 tree decl;
4024 tree ambiguous_decls;
4025
4026 decl = cp_parser_lookup_name (parser, token->u.value,
4027 none_type,
4028 /*is_template=*/false,
4029 /*is_namespace=*/false,
4030 /*check_dependency=*/true,
4031 &ambiguous_decls);
4032 if (TREE_CODE (decl) == TEMPLATE_DECL)
4033 error ("%qD used without template parameters", decl);
4034 else if (ambiguous_decls)
4035 {
4036 error ("reference to %qD is ambiguous",
4037 token->u.value);
4038 print_candidates (ambiguous_decls);
4039 decl = error_mark_node;
4040 }
4041 else
4042 cp_parser_name_lookup_error
4043 (parser, token->u.value, decl,
4044 "is not a class or namespace");
4045 }
4046 parser->scope = error_mark_node;
4047 error_p = true;
4048 /* Treat this as a successful nested-name-specifier
4049 due to:
4050
4051 [basic.lookup.qual]
4052
4053 If the name found is not a class-name (clause
4054 _class_) or namespace-name (_namespace.def_), the
4055 program is ill-formed. */
4056 success = true;
4057 }
4058 cp_lexer_consume_token (parser->lexer);
4059 }
4060 break;
4061 }
4062 /* We've found one valid nested-name-specifier. */
4063 success = true;
4064 /* Name lookup always gives us a DECL. */
4065 if (TREE_CODE (new_scope) == TYPE_DECL)
4066 new_scope = TREE_TYPE (new_scope);
4067 /* Uses of "template" must be followed by actual templates. */
4068 if (template_keyword_p
4069 && !(CLASS_TYPE_P (new_scope)
4070 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4071 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4072 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4073 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4074 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4075 == TEMPLATE_ID_EXPR)))
4076 pedwarn (TYPE_P (new_scope)
4077 ? "%qT is not a template"
4078 : "%qD is not a template",
4079 new_scope);
4080 /* If it is a class scope, try to complete it; we are about to
4081 be looking up names inside the class. */
4082 if (TYPE_P (new_scope)
4083 /* Since checking types for dependency can be expensive,
4084 avoid doing it if the type is already complete. */
4085 && !COMPLETE_TYPE_P (new_scope)
4086 /* Do not try to complete dependent types. */
4087 && !dependent_type_p (new_scope))
4088 {
4089 new_scope = complete_type (new_scope);
4090 /* If it is a typedef to current class, use the current
4091 class instead, as the typedef won't have any names inside
4092 it yet. */
4093 if (!COMPLETE_TYPE_P (new_scope)
4094 && currently_open_class (new_scope))
4095 new_scope = TYPE_MAIN_VARIANT (new_scope);
4096 }
4097 /* Make sure we look in the right scope the next time through
4098 the loop. */
4099 parser->scope = new_scope;
4100 }
4101
4102 /* If parsing tentatively, replace the sequence of tokens that makes
4103 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4104 token. That way, should we re-parse the token stream, we will
4105 not have to repeat the effort required to do the parse, nor will
4106 we issue duplicate error messages. */
4107 if (success && start)
4108 {
4109 cp_token *token;
4110
4111 token = cp_lexer_token_at (parser->lexer, start);
4112 /* Reset the contents of the START token. */
4113 token->type = CPP_NESTED_NAME_SPECIFIER;
4114 /* Retrieve any deferred checks. Do not pop this access checks yet
4115 so the memory will not be reclaimed during token replacing below. */
4116 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4117 token->u.tree_check_value->value = parser->scope;
4118 token->u.tree_check_value->checks = get_deferred_access_checks ();
4119 token->u.tree_check_value->qualifying_scope =
4120 parser->qualifying_scope;
4121 token->keyword = RID_MAX;
4122
4123 /* Purge all subsequent tokens. */
4124 cp_lexer_purge_tokens_after (parser->lexer, start);
4125 }
4126
4127 if (start)
4128 pop_to_parent_deferring_access_checks ();
4129
4130 return success ? parser->scope : NULL_TREE;
4131 }
4132
4133 /* Parse a nested-name-specifier. See
4134 cp_parser_nested_name_specifier_opt for details. This function
4135 behaves identically, except that it will an issue an error if no
4136 nested-name-specifier is present. */
4137
4138 static tree
4139 cp_parser_nested_name_specifier (cp_parser *parser,
4140 bool typename_keyword_p,
4141 bool check_dependency_p,
4142 bool type_p,
4143 bool is_declaration)
4144 {
4145 tree scope;
4146
4147 /* Look for the nested-name-specifier. */
4148 scope = cp_parser_nested_name_specifier_opt (parser,
4149 typename_keyword_p,
4150 check_dependency_p,
4151 type_p,
4152 is_declaration);
4153 /* If it was not present, issue an error message. */
4154 if (!scope)
4155 {
4156 cp_parser_error (parser, "expected nested-name-specifier");
4157 parser->scope = NULL_TREE;
4158 }
4159
4160 return scope;
4161 }
4162
4163 /* Parse a class-or-namespace-name.
4164
4165 class-or-namespace-name:
4166 class-name
4167 namespace-name
4168
4169 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4170 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4171 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4172 TYPE_P is TRUE iff the next name should be taken as a class-name,
4173 even the same name is declared to be another entity in the same
4174 scope.
4175
4176 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4177 specified by the class-or-namespace-name. If neither is found the
4178 ERROR_MARK_NODE is returned. */
4179
4180 static tree
4181 cp_parser_class_or_namespace_name (cp_parser *parser,
4182 bool typename_keyword_p,
4183 bool template_keyword_p,
4184 bool check_dependency_p,
4185 bool type_p,
4186 bool is_declaration)
4187 {
4188 tree saved_scope;
4189 tree saved_qualifying_scope;
4190 tree saved_object_scope;
4191 tree scope;
4192 bool only_class_p;
4193
4194 /* Before we try to parse the class-name, we must save away the
4195 current PARSER->SCOPE since cp_parser_class_name will destroy
4196 it. */
4197 saved_scope = parser->scope;
4198 saved_qualifying_scope = parser->qualifying_scope;
4199 saved_object_scope = parser->object_scope;
4200 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4201 there is no need to look for a namespace-name. */
4202 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4203 if (!only_class_p)
4204 cp_parser_parse_tentatively (parser);
4205 scope = cp_parser_class_name (parser,
4206 typename_keyword_p,
4207 template_keyword_p,
4208 type_p ? class_type : none_type,
4209 check_dependency_p,
4210 /*class_head_p=*/false,
4211 is_declaration);
4212 /* If that didn't work, try for a namespace-name. */
4213 if (!only_class_p && !cp_parser_parse_definitely (parser))
4214 {
4215 /* Restore the saved scope. */
4216 parser->scope = saved_scope;
4217 parser->qualifying_scope = saved_qualifying_scope;
4218 parser->object_scope = saved_object_scope;
4219 /* If we are not looking at an identifier followed by the scope
4220 resolution operator, then this is not part of a
4221 nested-name-specifier. (Note that this function is only used
4222 to parse the components of a nested-name-specifier.) */
4223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4224 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4225 return error_mark_node;
4226 scope = cp_parser_namespace_name (parser);
4227 }
4228
4229 return scope;
4230 }
4231
4232 /* Parse a postfix-expression.
4233
4234 postfix-expression:
4235 primary-expression
4236 postfix-expression [ expression ]
4237 postfix-expression ( expression-list [opt] )
4238 simple-type-specifier ( expression-list [opt] )
4239 typename :: [opt] nested-name-specifier identifier
4240 ( expression-list [opt] )
4241 typename :: [opt] nested-name-specifier template [opt] template-id
4242 ( expression-list [opt] )
4243 postfix-expression . template [opt] id-expression
4244 postfix-expression -> template [opt] id-expression
4245 postfix-expression . pseudo-destructor-name
4246 postfix-expression -> pseudo-destructor-name
4247 postfix-expression ++
4248 postfix-expression --
4249 dynamic_cast < type-id > ( expression )
4250 static_cast < type-id > ( expression )
4251 reinterpret_cast < type-id > ( expression )
4252 const_cast < type-id > ( expression )
4253 typeid ( expression )
4254 typeid ( type-id )
4255
4256 GNU Extension:
4257
4258 postfix-expression:
4259 ( type-id ) { initializer-list , [opt] }
4260
4261 This extension is a GNU version of the C99 compound-literal
4262 construct. (The C99 grammar uses `type-name' instead of `type-id',
4263 but they are essentially the same concept.)
4264
4265 If ADDRESS_P is true, the postfix expression is the operand of the
4266 `&' operator. CAST_P is true if this expression is the target of a
4267 cast.
4268
4269 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4270 class member access expressions [expr.ref].
4271
4272 Returns a representation of the expression. */
4273
4274 static tree
4275 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4276 bool member_access_only_p)
4277 {
4278 cp_token *token;
4279 enum rid keyword;
4280 cp_id_kind idk = CP_ID_KIND_NONE;
4281 tree postfix_expression = NULL_TREE;
4282 bool is_member_access = false;
4283
4284 /* Peek at the next token. */
4285 token = cp_lexer_peek_token (parser->lexer);
4286 /* Some of the productions are determined by keywords. */
4287 keyword = token->keyword;
4288 switch (keyword)
4289 {
4290 case RID_DYNCAST:
4291 case RID_STATCAST:
4292 case RID_REINTCAST:
4293 case RID_CONSTCAST:
4294 {
4295 tree type;
4296 tree expression;
4297 const char *saved_message;
4298
4299 /* All of these can be handled in the same way from the point
4300 of view of parsing. Begin by consuming the token
4301 identifying the cast. */
4302 cp_lexer_consume_token (parser->lexer);
4303
4304 /* New types cannot be defined in the cast. */
4305 saved_message = parser->type_definition_forbidden_message;
4306 parser->type_definition_forbidden_message
4307 = "types may not be defined in casts";
4308
4309 /* Look for the opening `<'. */
4310 cp_parser_require (parser, CPP_LESS, "`<'");
4311 /* Parse the type to which we are casting. */
4312 type = cp_parser_type_id (parser);
4313 /* Look for the closing `>'. */
4314 cp_parser_require (parser, CPP_GREATER, "`>'");
4315 /* Restore the old message. */
4316 parser->type_definition_forbidden_message = saved_message;
4317
4318 /* And the expression which is being cast. */
4319 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4320 expression = cp_parser_expression (parser, /*cast_p=*/true);
4321 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4322
4323 /* Only type conversions to integral or enumeration types
4324 can be used in constant-expressions. */
4325 if (!cast_valid_in_integral_constant_expression_p (type)
4326 && (cp_parser_non_integral_constant_expression
4327 (parser,
4328 "a cast to a type other than an integral or "
4329 "enumeration type")))
4330 return error_mark_node;
4331
4332 switch (keyword)
4333 {
4334 case RID_DYNCAST:
4335 postfix_expression
4336 = build_dynamic_cast (type, expression);
4337 break;
4338 case RID_STATCAST:
4339 postfix_expression
4340 = build_static_cast (type, expression);
4341 break;
4342 case RID_REINTCAST:
4343 postfix_expression
4344 = build_reinterpret_cast (type, expression);
4345 break;
4346 case RID_CONSTCAST:
4347 postfix_expression
4348 = build_const_cast (type, expression);
4349 break;
4350 default:
4351 gcc_unreachable ();
4352 }
4353 }
4354 break;
4355
4356 case RID_TYPEID:
4357 {
4358 tree type;
4359 const char *saved_message;
4360 bool saved_in_type_id_in_expr_p;
4361
4362 /* Consume the `typeid' token. */
4363 cp_lexer_consume_token (parser->lexer);
4364 /* Look for the `(' token. */
4365 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4366 /* Types cannot be defined in a `typeid' expression. */
4367 saved_message = parser->type_definition_forbidden_message;
4368 parser->type_definition_forbidden_message
4369 = "types may not be defined in a `typeid\' expression";
4370 /* We can't be sure yet whether we're looking at a type-id or an
4371 expression. */
4372 cp_parser_parse_tentatively (parser);
4373 /* Try a type-id first. */
4374 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4375 parser->in_type_id_in_expr_p = true;
4376 type = cp_parser_type_id (parser);
4377 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4378 /* Look for the `)' token. Otherwise, we can't be sure that
4379 we're not looking at an expression: consider `typeid (int
4380 (3))', for example. */
4381 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4382 /* If all went well, simply lookup the type-id. */
4383 if (cp_parser_parse_definitely (parser))
4384 postfix_expression = get_typeid (type);
4385 /* Otherwise, fall back to the expression variant. */
4386 else
4387 {
4388 tree expression;
4389
4390 /* Look for an expression. */
4391 expression = cp_parser_expression (parser, /*cast_p=*/false);
4392 /* Compute its typeid. */
4393 postfix_expression = build_typeid (expression);
4394 /* Look for the `)' token. */
4395 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4396 }
4397 /* Restore the saved message. */
4398 parser->type_definition_forbidden_message = saved_message;
4399 /* `typeid' may not appear in an integral constant expression. */
4400 if (cp_parser_non_integral_constant_expression(parser,
4401 "`typeid' operator"))
4402 return error_mark_node;
4403 }
4404 break;
4405
4406 case RID_TYPENAME:
4407 {
4408 tree type;
4409 /* The syntax permitted here is the same permitted for an
4410 elaborated-type-specifier. */
4411 type = cp_parser_elaborated_type_specifier (parser,
4412 /*is_friend=*/false,
4413 /*is_declaration=*/false);
4414 postfix_expression = cp_parser_functional_cast (parser, type);
4415 }
4416 break;
4417
4418 default:
4419 {
4420 tree type;
4421
4422 /* If the next thing is a simple-type-specifier, we may be
4423 looking at a functional cast. We could also be looking at
4424 an id-expression. So, we try the functional cast, and if
4425 that doesn't work we fall back to the primary-expression. */
4426 cp_parser_parse_tentatively (parser);
4427 /* Look for the simple-type-specifier. */
4428 type = cp_parser_simple_type_specifier (parser,
4429 /*decl_specs=*/NULL,
4430 CP_PARSER_FLAGS_NONE);
4431 /* Parse the cast itself. */
4432 if (!cp_parser_error_occurred (parser))
4433 postfix_expression
4434 = cp_parser_functional_cast (parser, type);
4435 /* If that worked, we're done. */
4436 if (cp_parser_parse_definitely (parser))
4437 break;
4438
4439 /* If the functional-cast didn't work out, try a
4440 compound-literal. */
4441 if (cp_parser_allow_gnu_extensions_p (parser)
4442 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4443 {
4444 VEC(constructor_elt,gc) *initializer_list = NULL;
4445 bool saved_in_type_id_in_expr_p;
4446
4447 cp_parser_parse_tentatively (parser);
4448 /* Consume the `('. */
4449 cp_lexer_consume_token (parser->lexer);
4450 /* Parse the type. */
4451 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4452 parser->in_type_id_in_expr_p = true;
4453 type = cp_parser_type_id (parser);
4454 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4455 /* Look for the `)'. */
4456 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4457 /* Look for the `{'. */
4458 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4459 /* If things aren't going well, there's no need to
4460 keep going. */
4461 if (!cp_parser_error_occurred (parser))
4462 {
4463 bool non_constant_p;
4464 /* Parse the initializer-list. */
4465 initializer_list
4466 = cp_parser_initializer_list (parser, &non_constant_p);
4467 /* Allow a trailing `,'. */
4468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4469 cp_lexer_consume_token (parser->lexer);
4470 /* Look for the final `}'. */
4471 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4472 }
4473 /* If that worked, we're definitely looking at a
4474 compound-literal expression. */
4475 if (cp_parser_parse_definitely (parser))
4476 {
4477 /* Warn the user that a compound literal is not
4478 allowed in standard C++. */
4479 if (pedantic)
4480 pedwarn ("ISO C++ forbids compound-literals");
4481 /* For simplicity, we disallow compound literals in
4482 constant-expressions. We could
4483 allow compound literals of integer type, whose
4484 initializer was a constant, in constant
4485 expressions. Permitting that usage, as a further
4486 extension, would not change the meaning of any
4487 currently accepted programs. (Of course, as
4488 compound literals are not part of ISO C++, the
4489 standard has nothing to say.) */
4490 if (cp_parser_non_integral_constant_expression
4491 (parser, "non-constant compound literals"))
4492 {
4493 postfix_expression = error_mark_node;
4494 break;
4495 }
4496 /* Form the representation of the compound-literal. */
4497 postfix_expression
4498 = finish_compound_literal (type, initializer_list);
4499 break;
4500 }
4501 }
4502
4503 /* It must be a primary-expression. */
4504 postfix_expression
4505 = cp_parser_primary_expression (parser, address_p, cast_p,
4506 /*template_arg_p=*/false,
4507 &idk);
4508 }
4509 break;
4510 }
4511
4512 /* Keep looping until the postfix-expression is complete. */
4513 while (true)
4514 {
4515 if (idk == CP_ID_KIND_UNQUALIFIED
4516 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4517 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4518 /* It is not a Koenig lookup function call. */
4519 postfix_expression
4520 = unqualified_name_lookup_error (postfix_expression);
4521
4522 /* Peek at the next token. */
4523 token = cp_lexer_peek_token (parser->lexer);
4524
4525 switch (token->type)
4526 {
4527 case CPP_OPEN_SQUARE:
4528 postfix_expression
4529 = cp_parser_postfix_open_square_expression (parser,
4530 postfix_expression,
4531 false);
4532 idk = CP_ID_KIND_NONE;
4533 is_member_access = false;
4534 break;
4535
4536 case CPP_OPEN_PAREN:
4537 /* postfix-expression ( expression-list [opt] ) */
4538 {
4539 bool koenig_p;
4540 bool is_builtin_constant_p;
4541 bool saved_integral_constant_expression_p = false;
4542 bool saved_non_integral_constant_expression_p = false;
4543 tree args;
4544
4545 is_member_access = false;
4546
4547 is_builtin_constant_p
4548 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4549 if (is_builtin_constant_p)
4550 {
4551 /* The whole point of __builtin_constant_p is to allow
4552 non-constant expressions to appear as arguments. */
4553 saved_integral_constant_expression_p
4554 = parser->integral_constant_expression_p;
4555 saved_non_integral_constant_expression_p
4556 = parser->non_integral_constant_expression_p;
4557 parser->integral_constant_expression_p = false;
4558 }
4559 args = (cp_parser_parenthesized_expression_list
4560 (parser, /*is_attribute_list=*/false,
4561 /*cast_p=*/false, /*allow_expansion_p=*/true,
4562 /*non_constant_p=*/NULL));
4563 if (is_builtin_constant_p)
4564 {
4565 parser->integral_constant_expression_p
4566 = saved_integral_constant_expression_p;
4567 parser->non_integral_constant_expression_p
4568 = saved_non_integral_constant_expression_p;
4569 }
4570
4571 if (args == error_mark_node)
4572 {
4573 postfix_expression = error_mark_node;
4574 break;
4575 }
4576
4577 /* Function calls are not permitted in
4578 constant-expressions. */
4579 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4580 && cp_parser_non_integral_constant_expression (parser,
4581 "a function call"))
4582 {
4583 postfix_expression = error_mark_node;
4584 break;
4585 }
4586
4587 koenig_p = false;
4588 if (idk == CP_ID_KIND_UNQUALIFIED)
4589 {
4590 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4591 {
4592 if (args)
4593 {
4594 koenig_p = true;
4595 postfix_expression
4596 = perform_koenig_lookup (postfix_expression, args);
4597 }
4598 else
4599 postfix_expression
4600 = unqualified_fn_lookup_error (postfix_expression);
4601 }
4602 /* We do not perform argument-dependent lookup if
4603 normal lookup finds a non-function, in accordance
4604 with the expected resolution of DR 218. */
4605 else if (args && is_overloaded_fn (postfix_expression))
4606 {
4607 tree fn = get_first_fn (postfix_expression);
4608
4609 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4610 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4611
4612 /* Only do argument dependent lookup if regular
4613 lookup does not find a set of member functions.
4614 [basic.lookup.koenig]/2a */
4615 if (!DECL_FUNCTION_MEMBER_P (fn))
4616 {
4617 koenig_p = true;
4618 postfix_expression
4619 = perform_koenig_lookup (postfix_expression, args);
4620 }
4621 }
4622 }
4623
4624 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4625 {
4626 tree instance = TREE_OPERAND (postfix_expression, 0);
4627 tree fn = TREE_OPERAND (postfix_expression, 1);
4628
4629 if (processing_template_decl
4630 && (type_dependent_expression_p (instance)
4631 || (!BASELINK_P (fn)
4632 && TREE_CODE (fn) != FIELD_DECL)
4633 || type_dependent_expression_p (fn)
4634 || any_type_dependent_arguments_p (args)))
4635 {
4636 postfix_expression
4637 = build_nt_call_list (postfix_expression, args);
4638 break;
4639 }
4640
4641 if (BASELINK_P (fn))
4642 postfix_expression
4643 = (build_new_method_call
4644 (instance, fn, args, NULL_TREE,
4645 (idk == CP_ID_KIND_QUALIFIED
4646 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4647 /*fn_p=*/NULL));
4648 else
4649 postfix_expression
4650 = finish_call_expr (postfix_expression, args,
4651 /*disallow_virtual=*/false,
4652 /*koenig_p=*/false);
4653 }
4654 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4655 || TREE_CODE (postfix_expression) == MEMBER_REF
4656 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4657 postfix_expression = (build_offset_ref_call_from_tree
4658 (postfix_expression, args));
4659 else if (idk == CP_ID_KIND_QUALIFIED)
4660 /* A call to a static class member, or a namespace-scope
4661 function. */
4662 postfix_expression
4663 = finish_call_expr (postfix_expression, args,
4664 /*disallow_virtual=*/true,
4665 koenig_p);
4666 else
4667 /* All other function calls. */
4668 postfix_expression
4669 = finish_call_expr (postfix_expression, args,
4670 /*disallow_virtual=*/false,
4671 koenig_p);
4672
4673 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4674 idk = CP_ID_KIND_NONE;
4675 }
4676 break;
4677
4678 case CPP_DOT:
4679 case CPP_DEREF:
4680 /* postfix-expression . template [opt] id-expression
4681 postfix-expression . pseudo-destructor-name
4682 postfix-expression -> template [opt] id-expression
4683 postfix-expression -> pseudo-destructor-name */
4684
4685 /* Consume the `.' or `->' operator. */
4686 cp_lexer_consume_token (parser->lexer);
4687
4688 postfix_expression
4689 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4690 postfix_expression,
4691 false, &idk);
4692
4693 is_member_access = true;
4694 break;
4695
4696 case CPP_PLUS_PLUS:
4697 /* postfix-expression ++ */
4698 /* Consume the `++' token. */
4699 cp_lexer_consume_token (parser->lexer);
4700 /* Generate a representation for the complete expression. */
4701 postfix_expression
4702 = finish_increment_expr (postfix_expression,
4703 POSTINCREMENT_EXPR);
4704 /* Increments may not appear in constant-expressions. */
4705 if (cp_parser_non_integral_constant_expression (parser,
4706 "an increment"))
4707 postfix_expression = error_mark_node;
4708 idk = CP_ID_KIND_NONE;
4709 is_member_access = false;
4710 break;
4711
4712 case CPP_MINUS_MINUS:
4713 /* postfix-expression -- */
4714 /* Consume the `--' token. */
4715 cp_lexer_consume_token (parser->lexer);
4716 /* Generate a representation for the complete expression. */
4717 postfix_expression
4718 = finish_increment_expr (postfix_expression,
4719 POSTDECREMENT_EXPR);
4720 /* Decrements may not appear in constant-expressions. */
4721 if (cp_parser_non_integral_constant_expression (parser,
4722 "a decrement"))
4723 postfix_expression = error_mark_node;
4724 idk = CP_ID_KIND_NONE;
4725 is_member_access = false;
4726 break;
4727
4728 default:
4729 if (member_access_only_p)
4730 return is_member_access? postfix_expression : error_mark_node;
4731 else
4732 return postfix_expression;
4733 }
4734 }
4735
4736 /* We should never get here. */
4737 gcc_unreachable ();
4738 return error_mark_node;
4739 }
4740
4741 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4742 by cp_parser_builtin_offsetof. We're looking for
4743
4744 postfix-expression [ expression ]
4745
4746 FOR_OFFSETOF is set if we're being called in that context, which
4747 changes how we deal with integer constant expressions. */
4748
4749 static tree
4750 cp_parser_postfix_open_square_expression (cp_parser *parser,
4751 tree postfix_expression,
4752 bool for_offsetof)
4753 {
4754 tree index;
4755
4756 /* Consume the `[' token. */
4757 cp_lexer_consume_token (parser->lexer);
4758
4759 /* Parse the index expression. */
4760 /* ??? For offsetof, there is a question of what to allow here. If
4761 offsetof is not being used in an integral constant expression context,
4762 then we *could* get the right answer by computing the value at runtime.
4763 If we are in an integral constant expression context, then we might
4764 could accept any constant expression; hard to say without analysis.
4765 Rather than open the barn door too wide right away, allow only integer
4766 constant expressions here. */
4767 if (for_offsetof)
4768 index = cp_parser_constant_expression (parser, false, NULL);
4769 else
4770 index = cp_parser_expression (parser, /*cast_p=*/false);
4771
4772 /* Look for the closing `]'. */
4773 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4774
4775 /* Build the ARRAY_REF. */
4776 postfix_expression = grok_array_decl (postfix_expression, index);
4777
4778 /* When not doing offsetof, array references are not permitted in
4779 constant-expressions. */
4780 if (!for_offsetof
4781 && (cp_parser_non_integral_constant_expression
4782 (parser, "an array reference")))
4783 postfix_expression = error_mark_node;
4784
4785 return postfix_expression;
4786 }
4787
4788 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4789 by cp_parser_builtin_offsetof. We're looking for
4790
4791 postfix-expression . template [opt] id-expression
4792 postfix-expression . pseudo-destructor-name
4793 postfix-expression -> template [opt] id-expression
4794 postfix-expression -> pseudo-destructor-name
4795
4796 FOR_OFFSETOF is set if we're being called in that context. That sorta
4797 limits what of the above we'll actually accept, but nevermind.
4798 TOKEN_TYPE is the "." or "->" token, which will already have been
4799 removed from the stream. */
4800
4801 static tree
4802 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4803 enum cpp_ttype token_type,
4804 tree postfix_expression,
4805 bool for_offsetof, cp_id_kind *idk)
4806 {
4807 tree name;
4808 bool dependent_p;
4809 bool pseudo_destructor_p;
4810 tree scope = NULL_TREE;
4811
4812 /* If this is a `->' operator, dereference the pointer. */
4813 if (token_type == CPP_DEREF)
4814 postfix_expression = build_x_arrow (postfix_expression);
4815 /* Check to see whether or not the expression is type-dependent. */
4816 dependent_p = type_dependent_expression_p (postfix_expression);
4817 /* The identifier following the `->' or `.' is not qualified. */
4818 parser->scope = NULL_TREE;
4819 parser->qualifying_scope = NULL_TREE;
4820 parser->object_scope = NULL_TREE;
4821 *idk = CP_ID_KIND_NONE;
4822 /* Enter the scope corresponding to the type of the object
4823 given by the POSTFIX_EXPRESSION. */
4824 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4825 {
4826 scope = TREE_TYPE (postfix_expression);
4827 /* According to the standard, no expression should ever have
4828 reference type. Unfortunately, we do not currently match
4829 the standard in this respect in that our internal representation
4830 of an expression may have reference type even when the standard
4831 says it does not. Therefore, we have to manually obtain the
4832 underlying type here. */
4833 scope = non_reference (scope);
4834 /* The type of the POSTFIX_EXPRESSION must be complete. */
4835 if (scope == unknown_type_node)
4836 {
4837 error ("%qE does not have class type", postfix_expression);
4838 scope = NULL_TREE;
4839 }
4840 else
4841 scope = complete_type_or_else (scope, NULL_TREE);
4842 /* Let the name lookup machinery know that we are processing a
4843 class member access expression. */
4844 parser->context->object_type = scope;
4845 /* If something went wrong, we want to be able to discern that case,
4846 as opposed to the case where there was no SCOPE due to the type
4847 of expression being dependent. */
4848 if (!scope)
4849 scope = error_mark_node;
4850 /* If the SCOPE was erroneous, make the various semantic analysis
4851 functions exit quickly -- and without issuing additional error
4852 messages. */
4853 if (scope == error_mark_node)
4854 postfix_expression = error_mark_node;
4855 }
4856
4857 /* Assume this expression is not a pseudo-destructor access. */
4858 pseudo_destructor_p = false;
4859
4860 /* If the SCOPE is a scalar type, then, if this is a valid program,
4861 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4862 is type dependent, it can be pseudo-destructor-name or something else.
4863 Try to parse it as pseudo-destructor-name first. */
4864 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4865 {
4866 tree s;
4867 tree type;
4868
4869 cp_parser_parse_tentatively (parser);
4870 /* Parse the pseudo-destructor-name. */
4871 s = NULL_TREE;
4872 cp_parser_pseudo_destructor_name (parser, &s, &type);
4873 if (dependent_p
4874 && (cp_parser_error_occurred (parser)
4875 || TREE_CODE (type) != TYPE_DECL
4876 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4877 cp_parser_abort_tentative_parse (parser);
4878 else if (cp_parser_parse_definitely (parser))
4879 {
4880 pseudo_destructor_p = true;
4881 postfix_expression
4882 = finish_pseudo_destructor_expr (postfix_expression,
4883 s, TREE_TYPE (type));
4884 }
4885 }
4886
4887 if (!pseudo_destructor_p)
4888 {
4889 /* If the SCOPE is not a scalar type, we are looking at an
4890 ordinary class member access expression, rather than a
4891 pseudo-destructor-name. */
4892 bool template_p;
4893 /* Parse the id-expression. */
4894 name = (cp_parser_id_expression
4895 (parser,
4896 cp_parser_optional_template_keyword (parser),
4897 /*check_dependency_p=*/true,
4898 &template_p,
4899 /*declarator_p=*/false,
4900 /*optional_p=*/false));
4901 /* In general, build a SCOPE_REF if the member name is qualified.
4902 However, if the name was not dependent and has already been
4903 resolved; there is no need to build the SCOPE_REF. For example;
4904
4905 struct X { void f(); };
4906 template <typename T> void f(T* t) { t->X::f(); }
4907
4908 Even though "t" is dependent, "X::f" is not and has been resolved
4909 to a BASELINK; there is no need to include scope information. */
4910
4911 /* But we do need to remember that there was an explicit scope for
4912 virtual function calls. */
4913 if (parser->scope)
4914 *idk = CP_ID_KIND_QUALIFIED;
4915
4916 /* If the name is a template-id that names a type, we will get a
4917 TYPE_DECL here. That is invalid code. */
4918 if (TREE_CODE (name) == TYPE_DECL)
4919 {
4920 error ("invalid use of %qD", name);
4921 postfix_expression = error_mark_node;
4922 }
4923 else
4924 {
4925 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4926 {
4927 name = build_qualified_name (/*type=*/NULL_TREE,
4928 parser->scope,
4929 name,
4930 template_p);
4931 parser->scope = NULL_TREE;
4932 parser->qualifying_scope = NULL_TREE;
4933 parser->object_scope = NULL_TREE;
4934 }
4935 if (scope && name && BASELINK_P (name))
4936 adjust_result_of_qualified_name_lookup
4937 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4938 postfix_expression
4939 = finish_class_member_access_expr (postfix_expression, name,
4940 template_p);
4941 }
4942 }
4943
4944 /* We no longer need to look up names in the scope of the object on
4945 the left-hand side of the `.' or `->' operator. */
4946 parser->context->object_type = NULL_TREE;
4947
4948 /* Outside of offsetof, these operators may not appear in
4949 constant-expressions. */
4950 if (!for_offsetof
4951 && (cp_parser_non_integral_constant_expression
4952 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4953 postfix_expression = error_mark_node;
4954
4955 return postfix_expression;
4956 }
4957
4958 /* Parse a parenthesized expression-list.
4959
4960 expression-list:
4961 assignment-expression
4962 expression-list, assignment-expression
4963
4964 attribute-list:
4965 expression-list
4966 identifier
4967 identifier, expression-list
4968
4969 CAST_P is true if this expression is the target of a cast.
4970
4971 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4972 argument pack.
4973
4974 Returns a TREE_LIST. The TREE_VALUE of each node is a
4975 representation of an assignment-expression. Note that a TREE_LIST
4976 is returned even if there is only a single expression in the list.
4977 error_mark_node is returned if the ( and or ) are
4978 missing. NULL_TREE is returned on no expressions. The parentheses
4979 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4980 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4981 indicates whether or not all of the expressions in the list were
4982 constant. */
4983
4984 static tree
4985 cp_parser_parenthesized_expression_list (cp_parser* parser,
4986 bool is_attribute_list,
4987 bool cast_p,
4988 bool allow_expansion_p,
4989 bool *non_constant_p)
4990 {
4991 tree expression_list = NULL_TREE;
4992 bool fold_expr_p = is_attribute_list;
4993 tree identifier = NULL_TREE;
4994 bool saved_greater_than_is_operator_p;
4995
4996 /* Assume all the expressions will be constant. */
4997 if (non_constant_p)
4998 *non_constant_p = false;
4999
5000 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5001 return error_mark_node;
5002
5003 /* Within a parenthesized expression, a `>' token is always
5004 the greater-than operator. */
5005 saved_greater_than_is_operator_p
5006 = parser->greater_than_is_operator_p;
5007 parser->greater_than_is_operator_p = true;
5008
5009 /* Consume expressions until there are no more. */
5010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5011 while (true)
5012 {
5013 tree expr;
5014
5015 /* At the beginning of attribute lists, check to see if the
5016 next token is an identifier. */
5017 if (is_attribute_list
5018 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5019 {
5020 cp_token *token;
5021
5022 /* Consume the identifier. */
5023 token = cp_lexer_consume_token (parser->lexer);
5024 /* Save the identifier. */
5025 identifier = token->u.value;
5026 }
5027 else
5028 {
5029 /* Parse the next assignment-expression. */
5030 if (non_constant_p)
5031 {
5032 bool expr_non_constant_p;
5033 expr = (cp_parser_constant_expression
5034 (parser, /*allow_non_constant_p=*/true,
5035 &expr_non_constant_p));
5036 if (expr_non_constant_p)
5037 *non_constant_p = true;
5038 }
5039 else
5040 expr = cp_parser_assignment_expression (parser, cast_p);
5041
5042 if (fold_expr_p)
5043 expr = fold_non_dependent_expr (expr);
5044
5045 /* If we have an ellipsis, then this is an expression
5046 expansion. */
5047 if (allow_expansion_p
5048 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5049 {
5050 /* Consume the `...'. */
5051 cp_lexer_consume_token (parser->lexer);
5052
5053 /* Build the argument pack. */
5054 expr = make_pack_expansion (expr);
5055 }
5056
5057 /* Add it to the list. We add error_mark_node
5058 expressions to the list, so that we can still tell if
5059 the correct form for a parenthesized expression-list
5060 is found. That gives better errors. */
5061 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5062
5063 if (expr == error_mark_node)
5064 goto skip_comma;
5065 }
5066
5067 /* After the first item, attribute lists look the same as
5068 expression lists. */
5069 is_attribute_list = false;
5070
5071 get_comma:;
5072 /* If the next token isn't a `,', then we are done. */
5073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5074 break;
5075
5076 /* Otherwise, consume the `,' and keep going. */
5077 cp_lexer_consume_token (parser->lexer);
5078 }
5079
5080 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5081 {
5082 int ending;
5083
5084 skip_comma:;
5085 /* We try and resync to an unnested comma, as that will give the
5086 user better diagnostics. */
5087 ending = cp_parser_skip_to_closing_parenthesis (parser,
5088 /*recovering=*/true,
5089 /*or_comma=*/true,
5090 /*consume_paren=*/true);
5091 if (ending < 0)
5092 goto get_comma;
5093 if (!ending)
5094 {
5095 parser->greater_than_is_operator_p
5096 = saved_greater_than_is_operator_p;
5097 return error_mark_node;
5098 }
5099 }
5100
5101 parser->greater_than_is_operator_p
5102 = saved_greater_than_is_operator_p;
5103
5104 /* We built up the list in reverse order so we must reverse it now. */
5105 expression_list = nreverse (expression_list);
5106 if (identifier)
5107 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5108
5109 return expression_list;
5110 }
5111
5112 /* Parse a pseudo-destructor-name.
5113
5114 pseudo-destructor-name:
5115 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5116 :: [opt] nested-name-specifier template template-id :: ~ type-name
5117 :: [opt] nested-name-specifier [opt] ~ type-name
5118
5119 If either of the first two productions is used, sets *SCOPE to the
5120 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5121 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5122 or ERROR_MARK_NODE if the parse fails. */
5123
5124 static void
5125 cp_parser_pseudo_destructor_name (cp_parser* parser,
5126 tree* scope,
5127 tree* type)
5128 {
5129 bool nested_name_specifier_p;
5130
5131 /* Assume that things will not work out. */
5132 *type = error_mark_node;
5133
5134 /* Look for the optional `::' operator. */
5135 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5136 /* Look for the optional nested-name-specifier. */
5137 nested_name_specifier_p
5138 = (cp_parser_nested_name_specifier_opt (parser,
5139 /*typename_keyword_p=*/false,
5140 /*check_dependency_p=*/true,
5141 /*type_p=*/false,
5142 /*is_declaration=*/true)
5143 != NULL_TREE);
5144 /* Now, if we saw a nested-name-specifier, we might be doing the
5145 second production. */
5146 if (nested_name_specifier_p
5147 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5148 {
5149 /* Consume the `template' keyword. */
5150 cp_lexer_consume_token (parser->lexer);
5151 /* Parse the template-id. */
5152 cp_parser_template_id (parser,
5153 /*template_keyword_p=*/true,
5154 /*check_dependency_p=*/false,
5155 /*is_declaration=*/true);
5156 /* Look for the `::' token. */
5157 cp_parser_require (parser, CPP_SCOPE, "`::'");
5158 }
5159 /* If the next token is not a `~', then there might be some
5160 additional qualification. */
5161 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5162 {
5163 /* Look for the type-name. */
5164 *scope = TREE_TYPE (cp_parser_type_name (parser));
5165
5166 if (*scope == error_mark_node)
5167 return;
5168
5169 /* If we don't have ::~, then something has gone wrong. Since
5170 the only caller of this function is looking for something
5171 after `.' or `->' after a scalar type, most likely the
5172 program is trying to get a member of a non-aggregate
5173 type. */
5174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5175 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5176 {
5177 cp_parser_error (parser, "request for member of non-aggregate type");
5178 return;
5179 }
5180
5181 /* Look for the `::' token. */
5182 cp_parser_require (parser, CPP_SCOPE, "`::'");
5183 }
5184 else
5185 *scope = NULL_TREE;
5186
5187 /* Look for the `~'. */
5188 cp_parser_require (parser, CPP_COMPL, "`~'");
5189 /* Look for the type-name again. We are not responsible for
5190 checking that it matches the first type-name. */
5191 *type = cp_parser_type_name (parser);
5192 }
5193
5194 /* Parse a unary-expression.
5195
5196 unary-expression:
5197 postfix-expression
5198 ++ cast-expression
5199 -- cast-expression
5200 unary-operator cast-expression
5201 sizeof unary-expression
5202 sizeof ( type-id )
5203 new-expression
5204 delete-expression
5205
5206 GNU Extensions:
5207
5208 unary-expression:
5209 __extension__ cast-expression
5210 __alignof__ unary-expression
5211 __alignof__ ( type-id )
5212 __real__ cast-expression
5213 __imag__ cast-expression
5214 && identifier
5215
5216 ADDRESS_P is true iff the unary-expression is appearing as the
5217 operand of the `&' operator. CAST_P is true if this expression is
5218 the target of a cast.
5219
5220 Returns a representation of the expression. */
5221
5222 static tree
5223 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5224 {
5225 cp_token *token;
5226 enum tree_code unary_operator;
5227
5228 /* Peek at the next token. */
5229 token = cp_lexer_peek_token (parser->lexer);
5230 /* Some keywords give away the kind of expression. */
5231 if (token->type == CPP_KEYWORD)
5232 {
5233 enum rid keyword = token->keyword;
5234
5235 switch (keyword)
5236 {
5237 case RID_ALIGNOF:
5238 case RID_SIZEOF:
5239 {
5240 tree operand;
5241 enum tree_code op;
5242
5243 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5244 /* Consume the token. */
5245 cp_lexer_consume_token (parser->lexer);
5246 /* Parse the operand. */
5247 operand = cp_parser_sizeof_operand (parser, keyword);
5248
5249 if (TYPE_P (operand))
5250 return cxx_sizeof_or_alignof_type (operand, op, true);
5251 else
5252 return cxx_sizeof_or_alignof_expr (operand, op);
5253 }
5254
5255 case RID_NEW:
5256 return cp_parser_new_expression (parser);
5257
5258 case RID_DELETE:
5259 return cp_parser_delete_expression (parser);
5260
5261 case RID_EXTENSION:
5262 {
5263 /* The saved value of the PEDANTIC flag. */
5264 int saved_pedantic;
5265 tree expr;
5266
5267 /* Save away the PEDANTIC flag. */
5268 cp_parser_extension_opt (parser, &saved_pedantic);
5269 /* Parse the cast-expression. */
5270 expr = cp_parser_simple_cast_expression (parser);
5271 /* Restore the PEDANTIC flag. */
5272 pedantic = saved_pedantic;
5273
5274 return expr;
5275 }
5276
5277 case RID_REALPART:
5278 case RID_IMAGPART:
5279 {
5280 tree expression;
5281
5282 /* Consume the `__real__' or `__imag__' token. */
5283 cp_lexer_consume_token (parser->lexer);
5284 /* Parse the cast-expression. */
5285 expression = cp_parser_simple_cast_expression (parser);
5286 /* Create the complete representation. */
5287 return build_x_unary_op ((keyword == RID_REALPART
5288 ? REALPART_EXPR : IMAGPART_EXPR),
5289 expression);
5290 }
5291 break;
5292
5293 default:
5294 break;
5295 }
5296 }
5297
5298 /* Look for the `:: new' and `:: delete', which also signal the
5299 beginning of a new-expression, or delete-expression,
5300 respectively. If the next token is `::', then it might be one of
5301 these. */
5302 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5303 {
5304 enum rid keyword;
5305
5306 /* See if the token after the `::' is one of the keywords in
5307 which we're interested. */
5308 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5309 /* If it's `new', we have a new-expression. */
5310 if (keyword == RID_NEW)
5311 return cp_parser_new_expression (parser);
5312 /* Similarly, for `delete'. */
5313 else if (keyword == RID_DELETE)
5314 return cp_parser_delete_expression (parser);
5315 }
5316
5317 /* Look for a unary operator. */
5318 unary_operator = cp_parser_unary_operator (token);
5319 /* The `++' and `--' operators can be handled similarly, even though
5320 they are not technically unary-operators in the grammar. */
5321 if (unary_operator == ERROR_MARK)
5322 {
5323 if (token->type == CPP_PLUS_PLUS)
5324 unary_operator = PREINCREMENT_EXPR;
5325 else if (token->type == CPP_MINUS_MINUS)
5326 unary_operator = PREDECREMENT_EXPR;
5327 /* Handle the GNU address-of-label extension. */
5328 else if (cp_parser_allow_gnu_extensions_p (parser)
5329 && token->type == CPP_AND_AND)
5330 {
5331 tree identifier;
5332 tree expression;
5333
5334 /* Consume the '&&' token. */
5335 cp_lexer_consume_token (parser->lexer);
5336 /* Look for the identifier. */
5337 identifier = cp_parser_identifier (parser);
5338 /* Create an expression representing the address. */
5339 expression = finish_label_address_expr (identifier);
5340 if (cp_parser_non_integral_constant_expression (parser,
5341 "the address of a label"))
5342 expression = error_mark_node;
5343 return expression;
5344 }
5345 }
5346 if (unary_operator != ERROR_MARK)
5347 {
5348 tree cast_expression;
5349 tree expression = error_mark_node;
5350 const char *non_constant_p = NULL;
5351
5352 /* Consume the operator token. */
5353 token = cp_lexer_consume_token (parser->lexer);
5354 /* Parse the cast-expression. */
5355 cast_expression
5356 = cp_parser_cast_expression (parser,
5357 unary_operator == ADDR_EXPR,
5358 /*cast_p=*/false);
5359 /* Now, build an appropriate representation. */
5360 switch (unary_operator)
5361 {
5362 case INDIRECT_REF:
5363 non_constant_p = "`*'";
5364 expression = build_x_indirect_ref (cast_expression, "unary *");
5365 break;
5366
5367 case ADDR_EXPR:
5368 non_constant_p = "`&'";
5369 /* Fall through. */
5370 case BIT_NOT_EXPR:
5371 expression = build_x_unary_op (unary_operator, cast_expression);
5372 break;
5373
5374 case PREINCREMENT_EXPR:
5375 case PREDECREMENT_EXPR:
5376 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5377 ? "`++'" : "`--'");
5378 /* Fall through. */
5379 case UNARY_PLUS_EXPR:
5380 case NEGATE_EXPR:
5381 case TRUTH_NOT_EXPR:
5382 expression = finish_unary_op_expr (unary_operator, cast_expression);
5383 break;
5384
5385 default:
5386 gcc_unreachable ();
5387 }
5388
5389 if (non_constant_p
5390 && cp_parser_non_integral_constant_expression (parser,
5391 non_constant_p))
5392 expression = error_mark_node;
5393
5394 return expression;
5395 }
5396
5397 return cp_parser_postfix_expression (parser, address_p, cast_p,
5398 /*member_access_only_p=*/false);
5399 }
5400
5401 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5402 unary-operator, the corresponding tree code is returned. */
5403
5404 static enum tree_code
5405 cp_parser_unary_operator (cp_token* token)
5406 {
5407 switch (token->type)
5408 {
5409 case CPP_MULT:
5410 return INDIRECT_REF;
5411
5412 case CPP_AND:
5413 return ADDR_EXPR;
5414
5415 case CPP_PLUS:
5416 return UNARY_PLUS_EXPR;
5417
5418 case CPP_MINUS:
5419 return NEGATE_EXPR;
5420
5421 case CPP_NOT:
5422 return TRUTH_NOT_EXPR;
5423
5424 case CPP_COMPL:
5425 return BIT_NOT_EXPR;
5426
5427 default:
5428 return ERROR_MARK;
5429 }
5430 }
5431
5432 /* Parse a new-expression.
5433
5434 new-expression:
5435 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5436 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5437
5438 Returns a representation of the expression. */
5439
5440 static tree
5441 cp_parser_new_expression (cp_parser* parser)
5442 {
5443 bool global_scope_p;
5444 tree placement;
5445 tree type;
5446 tree initializer;
5447 tree nelts;
5448
5449 /* Look for the optional `::' operator. */
5450 global_scope_p
5451 = (cp_parser_global_scope_opt (parser,
5452 /*current_scope_valid_p=*/false)
5453 != NULL_TREE);
5454 /* Look for the `new' operator. */
5455 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5456 /* There's no easy way to tell a new-placement from the
5457 `( type-id )' construct. */
5458 cp_parser_parse_tentatively (parser);
5459 /* Look for a new-placement. */
5460 placement = cp_parser_new_placement (parser);
5461 /* If that didn't work out, there's no new-placement. */
5462 if (!cp_parser_parse_definitely (parser))
5463 placement = NULL_TREE;
5464
5465 /* If the next token is a `(', then we have a parenthesized
5466 type-id. */
5467 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5468 {
5469 /* Consume the `('. */
5470 cp_lexer_consume_token (parser->lexer);
5471 /* Parse the type-id. */
5472 type = cp_parser_type_id (parser);
5473 /* Look for the closing `)'. */
5474 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5475 /* There should not be a direct-new-declarator in this production,
5476 but GCC used to allowed this, so we check and emit a sensible error
5477 message for this case. */
5478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5479 {
5480 error ("array bound forbidden after parenthesized type-id");
5481 inform ("try removing the parentheses around the type-id");
5482 cp_parser_direct_new_declarator (parser);
5483 }
5484 nelts = NULL_TREE;
5485 }
5486 /* Otherwise, there must be a new-type-id. */
5487 else
5488 type = cp_parser_new_type_id (parser, &nelts);
5489
5490 /* If the next token is a `(', then we have a new-initializer. */
5491 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5492 initializer = cp_parser_new_initializer (parser);
5493 else
5494 initializer = NULL_TREE;
5495
5496 /* A new-expression may not appear in an integral constant
5497 expression. */
5498 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5499 return error_mark_node;
5500
5501 /* Create a representation of the new-expression. */
5502 return build_new (placement, type, nelts, initializer, global_scope_p);
5503 }
5504
5505 /* Parse a new-placement.
5506
5507 new-placement:
5508 ( expression-list )
5509
5510 Returns the same representation as for an expression-list. */
5511
5512 static tree
5513 cp_parser_new_placement (cp_parser* parser)
5514 {
5515 tree expression_list;
5516
5517 /* Parse the expression-list. */
5518 expression_list = (cp_parser_parenthesized_expression_list
5519 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5520 /*non_constant_p=*/NULL));
5521
5522 return expression_list;
5523 }
5524
5525 /* Parse a new-type-id.
5526
5527 new-type-id:
5528 type-specifier-seq new-declarator [opt]
5529
5530 Returns the TYPE allocated. If the new-type-id indicates an array
5531 type, *NELTS is set to the number of elements in the last array
5532 bound; the TYPE will not include the last array bound. */
5533
5534 static tree
5535 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5536 {
5537 cp_decl_specifier_seq type_specifier_seq;
5538 cp_declarator *new_declarator;
5539 cp_declarator *declarator;
5540 cp_declarator *outer_declarator;
5541 const char *saved_message;
5542 tree type;
5543
5544 /* The type-specifier sequence must not contain type definitions.
5545 (It cannot contain declarations of new types either, but if they
5546 are not definitions we will catch that because they are not
5547 complete.) */
5548 saved_message = parser->type_definition_forbidden_message;
5549 parser->type_definition_forbidden_message
5550 = "types may not be defined in a new-type-id";
5551 /* Parse the type-specifier-seq. */
5552 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5553 &type_specifier_seq);
5554 /* Restore the old message. */
5555 parser->type_definition_forbidden_message = saved_message;
5556 /* Parse the new-declarator. */
5557 new_declarator = cp_parser_new_declarator_opt (parser);
5558
5559 /* Determine the number of elements in the last array dimension, if
5560 any. */
5561 *nelts = NULL_TREE;
5562 /* Skip down to the last array dimension. */
5563 declarator = new_declarator;
5564 outer_declarator = NULL;
5565 while (declarator && (declarator->kind == cdk_pointer
5566 || declarator->kind == cdk_ptrmem))
5567 {
5568 outer_declarator = declarator;
5569 declarator = declarator->declarator;
5570 }
5571 while (declarator
5572 && declarator->kind == cdk_array
5573 && declarator->declarator
5574 && declarator->declarator->kind == cdk_array)
5575 {
5576 outer_declarator = declarator;
5577 declarator = declarator->declarator;
5578 }
5579
5580 if (declarator && declarator->kind == cdk_array)
5581 {
5582 *nelts = declarator->u.array.bounds;
5583 if (*nelts == error_mark_node)
5584 *nelts = integer_one_node;
5585
5586 if (outer_declarator)
5587 outer_declarator->declarator = declarator->declarator;
5588 else
5589 new_declarator = NULL;
5590 }
5591
5592 type = groktypename (&type_specifier_seq, new_declarator);
5593 return type;
5594 }
5595
5596 /* Parse an (optional) new-declarator.
5597
5598 new-declarator:
5599 ptr-operator new-declarator [opt]
5600 direct-new-declarator
5601
5602 Returns the declarator. */
5603
5604 static cp_declarator *
5605 cp_parser_new_declarator_opt (cp_parser* parser)
5606 {
5607 enum tree_code code;
5608 tree type;
5609 cp_cv_quals cv_quals;
5610
5611 /* We don't know if there's a ptr-operator next, or not. */
5612 cp_parser_parse_tentatively (parser);
5613 /* Look for a ptr-operator. */
5614 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5615 /* If that worked, look for more new-declarators. */
5616 if (cp_parser_parse_definitely (parser))
5617 {
5618 cp_declarator *declarator;
5619
5620 /* Parse another optional declarator. */
5621 declarator = cp_parser_new_declarator_opt (parser);
5622
5623 return cp_parser_make_indirect_declarator
5624 (code, type, cv_quals, declarator);
5625 }
5626
5627 /* If the next token is a `[', there is a direct-new-declarator. */
5628 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5629 return cp_parser_direct_new_declarator (parser);
5630
5631 return NULL;
5632 }
5633
5634 /* Parse a direct-new-declarator.
5635
5636 direct-new-declarator:
5637 [ expression ]
5638 direct-new-declarator [constant-expression]
5639
5640 */
5641
5642 static cp_declarator *
5643 cp_parser_direct_new_declarator (cp_parser* parser)
5644 {
5645 cp_declarator *declarator = NULL;
5646
5647 while (true)
5648 {
5649 tree expression;
5650
5651 /* Look for the opening `['. */
5652 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5653 /* The first expression is not required to be constant. */
5654 if (!declarator)
5655 {
5656 expression = cp_parser_expression (parser, /*cast_p=*/false);
5657 /* The standard requires that the expression have integral
5658 type. DR 74 adds enumeration types. We believe that the
5659 real intent is that these expressions be handled like the
5660 expression in a `switch' condition, which also allows
5661 classes with a single conversion to integral or
5662 enumeration type. */
5663 if (!processing_template_decl)
5664 {
5665 expression
5666 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5667 expression,
5668 /*complain=*/true);
5669 if (!expression)
5670 {
5671 error ("expression in new-declarator must have integral "
5672 "or enumeration type");
5673 expression = error_mark_node;
5674 }
5675 }
5676 }
5677 /* But all the other expressions must be. */
5678 else
5679 expression
5680 = cp_parser_constant_expression (parser,
5681 /*allow_non_constant=*/false,
5682 NULL);
5683 /* Look for the closing `]'. */
5684 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5685
5686 /* Add this bound to the declarator. */
5687 declarator = make_array_declarator (declarator, expression);
5688
5689 /* If the next token is not a `[', then there are no more
5690 bounds. */
5691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5692 break;
5693 }
5694
5695 return declarator;
5696 }
5697
5698 /* Parse a new-initializer.
5699
5700 new-initializer:
5701 ( expression-list [opt] )
5702
5703 Returns a representation of the expression-list. If there is no
5704 expression-list, VOID_ZERO_NODE is returned. */
5705
5706 static tree
5707 cp_parser_new_initializer (cp_parser* parser)
5708 {
5709 tree expression_list;
5710
5711 expression_list = (cp_parser_parenthesized_expression_list
5712 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5713 /*non_constant_p=*/NULL));
5714 if (!expression_list)
5715 expression_list = void_zero_node;
5716
5717 return expression_list;
5718 }
5719
5720 /* Parse a delete-expression.
5721
5722 delete-expression:
5723 :: [opt] delete cast-expression
5724 :: [opt] delete [ ] cast-expression
5725
5726 Returns a representation of the expression. */
5727
5728 static tree
5729 cp_parser_delete_expression (cp_parser* parser)
5730 {
5731 bool global_scope_p;
5732 bool array_p;
5733 tree expression;
5734
5735 /* Look for the optional `::' operator. */
5736 global_scope_p
5737 = (cp_parser_global_scope_opt (parser,
5738 /*current_scope_valid_p=*/false)
5739 != NULL_TREE);
5740 /* Look for the `delete' keyword. */
5741 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5742 /* See if the array syntax is in use. */
5743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5744 {
5745 /* Consume the `[' token. */
5746 cp_lexer_consume_token (parser->lexer);
5747 /* Look for the `]' token. */
5748 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5749 /* Remember that this is the `[]' construct. */
5750 array_p = true;
5751 }
5752 else
5753 array_p = false;
5754
5755 /* Parse the cast-expression. */
5756 expression = cp_parser_simple_cast_expression (parser);
5757
5758 /* A delete-expression may not appear in an integral constant
5759 expression. */
5760 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5761 return error_mark_node;
5762
5763 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5764 }
5765
5766 /* Parse a cast-expression.
5767
5768 cast-expression:
5769 unary-expression
5770 ( type-id ) cast-expression
5771
5772 ADDRESS_P is true iff the unary-expression is appearing as the
5773 operand of the `&' operator. CAST_P is true if this expression is
5774 the target of a cast.
5775
5776 Returns a representation of the expression. */
5777
5778 static tree
5779 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5780 {
5781 /* If it's a `(', then we might be looking at a cast. */
5782 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5783 {
5784 tree type = NULL_TREE;
5785 tree expr = NULL_TREE;
5786 bool compound_literal_p;
5787 const char *saved_message;
5788
5789 /* There's no way to know yet whether or not this is a cast.
5790 For example, `(int (3))' is a unary-expression, while `(int)
5791 3' is a cast. So, we resort to parsing tentatively. */
5792 cp_parser_parse_tentatively (parser);
5793 /* Types may not be defined in a cast. */
5794 saved_message = parser->type_definition_forbidden_message;
5795 parser->type_definition_forbidden_message
5796 = "types may not be defined in casts";
5797 /* Consume the `('. */
5798 cp_lexer_consume_token (parser->lexer);
5799 /* A very tricky bit is that `(struct S) { 3 }' is a
5800 compound-literal (which we permit in C++ as an extension).
5801 But, that construct is not a cast-expression -- it is a
5802 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5803 is legal; if the compound-literal were a cast-expression,
5804 you'd need an extra set of parentheses.) But, if we parse
5805 the type-id, and it happens to be a class-specifier, then we
5806 will commit to the parse at that point, because we cannot
5807 undo the action that is done when creating a new class. So,
5808 then we cannot back up and do a postfix-expression.
5809
5810 Therefore, we scan ahead to the closing `)', and check to see
5811 if the token after the `)' is a `{'. If so, we are not
5812 looking at a cast-expression.
5813
5814 Save tokens so that we can put them back. */
5815 cp_lexer_save_tokens (parser->lexer);
5816 /* Skip tokens until the next token is a closing parenthesis.
5817 If we find the closing `)', and the next token is a `{', then
5818 we are looking at a compound-literal. */
5819 compound_literal_p
5820 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5821 /*consume_paren=*/true)
5822 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5823 /* Roll back the tokens we skipped. */
5824 cp_lexer_rollback_tokens (parser->lexer);
5825 /* If we were looking at a compound-literal, simulate an error
5826 so that the call to cp_parser_parse_definitely below will
5827 fail. */
5828 if (compound_literal_p)
5829 cp_parser_simulate_error (parser);
5830 else
5831 {
5832 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5833 parser->in_type_id_in_expr_p = true;
5834 /* Look for the type-id. */
5835 type = cp_parser_type_id (parser);
5836 /* Look for the closing `)'. */
5837 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5838 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5839 }
5840
5841 /* Restore the saved message. */
5842 parser->type_definition_forbidden_message = saved_message;
5843
5844 /* If ok so far, parse the dependent expression. We cannot be
5845 sure it is a cast. Consider `(T ())'. It is a parenthesized
5846 ctor of T, but looks like a cast to function returning T
5847 without a dependent expression. */
5848 if (!cp_parser_error_occurred (parser))
5849 expr = cp_parser_cast_expression (parser,
5850 /*address_p=*/false,
5851 /*cast_p=*/true);
5852
5853 if (cp_parser_parse_definitely (parser))
5854 {
5855 /* Warn about old-style casts, if so requested. */
5856 if (warn_old_style_cast
5857 && !in_system_header
5858 && !VOID_TYPE_P (type)
5859 && current_lang_name != lang_name_c)
5860 warning (OPT_Wold_style_cast, "use of old-style cast");
5861
5862 /* Only type conversions to integral or enumeration types
5863 can be used in constant-expressions. */
5864 if (!cast_valid_in_integral_constant_expression_p (type)
5865 && (cp_parser_non_integral_constant_expression
5866 (parser,
5867 "a cast to a type other than an integral or "
5868 "enumeration type")))
5869 return error_mark_node;
5870
5871 /* Perform the cast. */
5872 expr = build_c_cast (type, expr);
5873 return expr;
5874 }
5875 }
5876
5877 /* If we get here, then it's not a cast, so it must be a
5878 unary-expression. */
5879 return cp_parser_unary_expression (parser, address_p, cast_p);
5880 }
5881
5882 /* Parse a binary expression of the general form:
5883
5884 pm-expression:
5885 cast-expression
5886 pm-expression .* cast-expression
5887 pm-expression ->* cast-expression
5888
5889 multiplicative-expression:
5890 pm-expression
5891 multiplicative-expression * pm-expression
5892 multiplicative-expression / pm-expression
5893 multiplicative-expression % pm-expression
5894
5895 additive-expression:
5896 multiplicative-expression
5897 additive-expression + multiplicative-expression
5898 additive-expression - multiplicative-expression
5899
5900 shift-expression:
5901 additive-expression
5902 shift-expression << additive-expression
5903 shift-expression >> additive-expression
5904
5905 relational-expression:
5906 shift-expression
5907 relational-expression < shift-expression
5908 relational-expression > shift-expression
5909 relational-expression <= shift-expression
5910 relational-expression >= shift-expression
5911
5912 GNU Extension:
5913
5914 relational-expression:
5915 relational-expression <? shift-expression
5916 relational-expression >? shift-expression
5917
5918 equality-expression:
5919 relational-expression
5920 equality-expression == relational-expression
5921 equality-expression != relational-expression
5922
5923 and-expression:
5924 equality-expression
5925 and-expression & equality-expression
5926
5927 exclusive-or-expression:
5928 and-expression
5929 exclusive-or-expression ^ and-expression
5930
5931 inclusive-or-expression:
5932 exclusive-or-expression
5933 inclusive-or-expression | exclusive-or-expression
5934
5935 logical-and-expression:
5936 inclusive-or-expression
5937 logical-and-expression && inclusive-or-expression
5938
5939 logical-or-expression:
5940 logical-and-expression
5941 logical-or-expression || logical-and-expression
5942
5943 All these are implemented with a single function like:
5944
5945 binary-expression:
5946 simple-cast-expression
5947 binary-expression <token> binary-expression
5948
5949 CAST_P is true if this expression is the target of a cast.
5950
5951 The binops_by_token map is used to get the tree codes for each <token> type.
5952 binary-expressions are associated according to a precedence table. */
5953
5954 #define TOKEN_PRECEDENCE(token) \
5955 (((token->type == CPP_GREATER \
5956 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5957 && !parser->greater_than_is_operator_p) \
5958 ? PREC_NOT_OPERATOR \
5959 : binops_by_token[token->type].prec)
5960
5961 static tree
5962 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5963 {
5964 cp_parser_expression_stack stack;
5965 cp_parser_expression_stack_entry *sp = &stack[0];
5966 tree lhs, rhs;
5967 cp_token *token;
5968 enum tree_code tree_type, lhs_type, rhs_type;
5969 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5970 bool overloaded_p;
5971
5972 /* Parse the first expression. */
5973 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5974 lhs_type = ERROR_MARK;
5975
5976 for (;;)
5977 {
5978 /* Get an operator token. */
5979 token = cp_lexer_peek_token (parser->lexer);
5980
5981 if (warn_cxx0x_compat
5982 && token->type == CPP_RSHIFT
5983 && !parser->greater_than_is_operator_p)
5984 {
5985 warning (OPT_Wc__0x_compat,
5986 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5987 &token->location);
5988 warning (OPT_Wc__0x_compat,
5989 "suggest parentheses around %<>>%> expression");
5990 }
5991
5992 new_prec = TOKEN_PRECEDENCE (token);
5993
5994 /* Popping an entry off the stack means we completed a subexpression:
5995 - either we found a token which is not an operator (`>' where it is not
5996 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5997 will happen repeatedly;
5998 - or, we found an operator which has lower priority. This is the case
5999 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6000 parsing `3 * 4'. */
6001 if (new_prec <= prec)
6002 {
6003 if (sp == stack)
6004 break;
6005 else
6006 goto pop;
6007 }
6008
6009 get_rhs:
6010 tree_type = binops_by_token[token->type].tree_type;
6011
6012 /* We used the operator token. */
6013 cp_lexer_consume_token (parser->lexer);
6014
6015 /* Extract another operand. It may be the RHS of this expression
6016 or the LHS of a new, higher priority expression. */
6017 rhs = cp_parser_simple_cast_expression (parser);
6018 rhs_type = ERROR_MARK;
6019
6020 /* Get another operator token. Look up its precedence to avoid
6021 building a useless (immediately popped) stack entry for common
6022 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6023 token = cp_lexer_peek_token (parser->lexer);
6024 lookahead_prec = TOKEN_PRECEDENCE (token);
6025 if (lookahead_prec > new_prec)
6026 {
6027 /* ... and prepare to parse the RHS of the new, higher priority
6028 expression. Since precedence levels on the stack are
6029 monotonically increasing, we do not have to care about
6030 stack overflows. */
6031 sp->prec = prec;
6032 sp->tree_type = tree_type;
6033 sp->lhs = lhs;
6034 sp->lhs_type = lhs_type;
6035 sp++;
6036 lhs = rhs;
6037 lhs_type = rhs_type;
6038 prec = new_prec;
6039 new_prec = lookahead_prec;
6040 goto get_rhs;
6041
6042 pop:
6043 /* If the stack is not empty, we have parsed into LHS the right side
6044 (`4' in the example above) of an expression we had suspended.
6045 We can use the information on the stack to recover the LHS (`3')
6046 from the stack together with the tree code (`MULT_EXPR'), and
6047 the precedence of the higher level subexpression
6048 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6049 which will be used to actually build the additive expression. */
6050 --sp;
6051 prec = sp->prec;
6052 tree_type = sp->tree_type;
6053 rhs = lhs;
6054 rhs_type = lhs_type;
6055 lhs = sp->lhs;
6056 lhs_type = sp->lhs_type;
6057 }
6058
6059 overloaded_p = false;
6060 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6061 &overloaded_p);
6062 lhs_type = tree_type;
6063
6064 /* If the binary operator required the use of an overloaded operator,
6065 then this expression cannot be an integral constant-expression.
6066 An overloaded operator can be used even if both operands are
6067 otherwise permissible in an integral constant-expression if at
6068 least one of the operands is of enumeration type. */
6069
6070 if (overloaded_p
6071 && (cp_parser_non_integral_constant_expression
6072 (parser, "calls to overloaded operators")))
6073 return error_mark_node;
6074 }
6075
6076 return lhs;
6077 }
6078
6079
6080 /* Parse the `? expression : assignment-expression' part of a
6081 conditional-expression. The LOGICAL_OR_EXPR is the
6082 logical-or-expression that started the conditional-expression.
6083 Returns a representation of the entire conditional-expression.
6084
6085 This routine is used by cp_parser_assignment_expression.
6086
6087 ? expression : assignment-expression
6088
6089 GNU Extensions:
6090
6091 ? : assignment-expression */
6092
6093 static tree
6094 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6095 {
6096 tree expr;
6097 tree assignment_expr;
6098
6099 /* Consume the `?' token. */
6100 cp_lexer_consume_token (parser->lexer);
6101 if (cp_parser_allow_gnu_extensions_p (parser)
6102 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6103 /* Implicit true clause. */
6104 expr = NULL_TREE;
6105 else
6106 /* Parse the expression. */
6107 expr = cp_parser_expression (parser, /*cast_p=*/false);
6108
6109 /* The next token should be a `:'. */
6110 cp_parser_require (parser, CPP_COLON, "`:'");
6111 /* Parse the assignment-expression. */
6112 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6113
6114 /* Build the conditional-expression. */
6115 return build_x_conditional_expr (logical_or_expr,
6116 expr,
6117 assignment_expr);
6118 }
6119
6120 /* Parse an assignment-expression.
6121
6122 assignment-expression:
6123 conditional-expression
6124 logical-or-expression assignment-operator assignment_expression
6125 throw-expression
6126
6127 CAST_P is true if this expression is the target of a cast.
6128
6129 Returns a representation for the expression. */
6130
6131 static tree
6132 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6133 {
6134 tree expr;
6135
6136 /* If the next token is the `throw' keyword, then we're looking at
6137 a throw-expression. */
6138 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6139 expr = cp_parser_throw_expression (parser);
6140 /* Otherwise, it must be that we are looking at a
6141 logical-or-expression. */
6142 else
6143 {
6144 /* Parse the binary expressions (logical-or-expression). */
6145 expr = cp_parser_binary_expression (parser, cast_p);
6146 /* If the next token is a `?' then we're actually looking at a
6147 conditional-expression. */
6148 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6149 return cp_parser_question_colon_clause (parser, expr);
6150 else
6151 {
6152 enum tree_code assignment_operator;
6153
6154 /* If it's an assignment-operator, we're using the second
6155 production. */
6156 assignment_operator
6157 = cp_parser_assignment_operator_opt (parser);
6158 if (assignment_operator != ERROR_MARK)
6159 {
6160 tree rhs;
6161
6162 /* Parse the right-hand side of the assignment. */
6163 rhs = cp_parser_assignment_expression (parser, cast_p);
6164 /* An assignment may not appear in a
6165 constant-expression. */
6166 if (cp_parser_non_integral_constant_expression (parser,
6167 "an assignment"))
6168 return error_mark_node;
6169 /* Build the assignment expression. */
6170 expr = build_x_modify_expr (expr,
6171 assignment_operator,
6172 rhs);
6173 }
6174 }
6175 }
6176
6177 return expr;
6178 }
6179
6180 /* Parse an (optional) assignment-operator.
6181
6182 assignment-operator: one of
6183 = *= /= %= += -= >>= <<= &= ^= |=
6184
6185 GNU Extension:
6186
6187 assignment-operator: one of
6188 <?= >?=
6189
6190 If the next token is an assignment operator, the corresponding tree
6191 code is returned, and the token is consumed. For example, for
6192 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6193 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6194 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6195 operator, ERROR_MARK is returned. */
6196
6197 static enum tree_code
6198 cp_parser_assignment_operator_opt (cp_parser* parser)
6199 {
6200 enum tree_code op;
6201 cp_token *token;
6202
6203 /* Peek at the next toen. */
6204 token = cp_lexer_peek_token (parser->lexer);
6205
6206 switch (token->type)
6207 {
6208 case CPP_EQ:
6209 op = NOP_EXPR;
6210 break;
6211
6212 case CPP_MULT_EQ:
6213 op = MULT_EXPR;
6214 break;
6215
6216 case CPP_DIV_EQ:
6217 op = TRUNC_DIV_EXPR;
6218 break;
6219
6220 case CPP_MOD_EQ:
6221 op = TRUNC_MOD_EXPR;
6222 break;
6223
6224 case CPP_PLUS_EQ:
6225 op = PLUS_EXPR;
6226 break;
6227
6228 case CPP_MINUS_EQ:
6229 op = MINUS_EXPR;
6230 break;
6231
6232 case CPP_RSHIFT_EQ:
6233 op = RSHIFT_EXPR;
6234 break;
6235
6236 case CPP_LSHIFT_EQ:
6237 op = LSHIFT_EXPR;
6238 break;
6239
6240 case CPP_AND_EQ:
6241 op = BIT_AND_EXPR;
6242 break;
6243
6244 case CPP_XOR_EQ:
6245 op = BIT_XOR_EXPR;
6246 break;
6247
6248 case CPP_OR_EQ:
6249 op = BIT_IOR_EXPR;
6250 break;
6251
6252 default:
6253 /* Nothing else is an assignment operator. */
6254 op = ERROR_MARK;
6255 }
6256
6257 /* If it was an assignment operator, consume it. */
6258 if (op != ERROR_MARK)
6259 cp_lexer_consume_token (parser->lexer);
6260
6261 return op;
6262 }
6263
6264 /* Parse an expression.
6265
6266 expression:
6267 assignment-expression
6268 expression , assignment-expression
6269
6270 CAST_P is true if this expression is the target of a cast.
6271
6272 Returns a representation of the expression. */
6273
6274 static tree
6275 cp_parser_expression (cp_parser* parser, bool cast_p)
6276 {
6277 tree expression = NULL_TREE;
6278
6279 while (true)
6280 {
6281 tree assignment_expression;
6282
6283 /* Parse the next assignment-expression. */
6284 assignment_expression
6285 = cp_parser_assignment_expression (parser, cast_p);
6286 /* If this is the first assignment-expression, we can just
6287 save it away. */
6288 if (!expression)
6289 expression = assignment_expression;
6290 else
6291 expression = build_x_compound_expr (expression,
6292 assignment_expression);
6293 /* If the next token is not a comma, then we are done with the
6294 expression. */
6295 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6296 break;
6297 /* Consume the `,'. */
6298 cp_lexer_consume_token (parser->lexer);
6299 /* A comma operator cannot appear in a constant-expression. */
6300 if (cp_parser_non_integral_constant_expression (parser,
6301 "a comma operator"))
6302 expression = error_mark_node;
6303 }
6304
6305 return expression;
6306 }
6307
6308 /* Parse a constant-expression.
6309
6310 constant-expression:
6311 conditional-expression
6312
6313 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6314 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6315 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6316 is false, NON_CONSTANT_P should be NULL. */
6317
6318 static tree
6319 cp_parser_constant_expression (cp_parser* parser,
6320 bool allow_non_constant_p,
6321 bool *non_constant_p)
6322 {
6323 bool saved_integral_constant_expression_p;
6324 bool saved_allow_non_integral_constant_expression_p;
6325 bool saved_non_integral_constant_expression_p;
6326 tree expression;
6327
6328 /* It might seem that we could simply parse the
6329 conditional-expression, and then check to see if it were
6330 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6331 one that the compiler can figure out is constant, possibly after
6332 doing some simplifications or optimizations. The standard has a
6333 precise definition of constant-expression, and we must honor
6334 that, even though it is somewhat more restrictive.
6335
6336 For example:
6337
6338 int i[(2, 3)];
6339
6340 is not a legal declaration, because `(2, 3)' is not a
6341 constant-expression. The `,' operator is forbidden in a
6342 constant-expression. However, GCC's constant-folding machinery
6343 will fold this operation to an INTEGER_CST for `3'. */
6344
6345 /* Save the old settings. */
6346 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6347 saved_allow_non_integral_constant_expression_p
6348 = parser->allow_non_integral_constant_expression_p;
6349 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6350 /* We are now parsing a constant-expression. */
6351 parser->integral_constant_expression_p = true;
6352 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6353 parser->non_integral_constant_expression_p = false;
6354 /* Although the grammar says "conditional-expression", we parse an
6355 "assignment-expression", which also permits "throw-expression"
6356 and the use of assignment operators. In the case that
6357 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6358 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6359 actually essential that we look for an assignment-expression.
6360 For example, cp_parser_initializer_clauses uses this function to
6361 determine whether a particular assignment-expression is in fact
6362 constant. */
6363 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6364 /* Restore the old settings. */
6365 parser->integral_constant_expression_p
6366 = saved_integral_constant_expression_p;
6367 parser->allow_non_integral_constant_expression_p
6368 = saved_allow_non_integral_constant_expression_p;
6369 if (allow_non_constant_p)
6370 *non_constant_p = parser->non_integral_constant_expression_p;
6371 else if (parser->non_integral_constant_expression_p)
6372 expression = error_mark_node;
6373 parser->non_integral_constant_expression_p
6374 = saved_non_integral_constant_expression_p;
6375
6376 return expression;
6377 }
6378
6379 /* Parse __builtin_offsetof.
6380
6381 offsetof-expression:
6382 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6383
6384 offsetof-member-designator:
6385 id-expression
6386 | offsetof-member-designator "." id-expression
6387 | offsetof-member-designator "[" expression "]" */
6388
6389 static tree
6390 cp_parser_builtin_offsetof (cp_parser *parser)
6391 {
6392 int save_ice_p, save_non_ice_p;
6393 tree type, expr;
6394 cp_id_kind dummy;
6395
6396 /* We're about to accept non-integral-constant things, but will
6397 definitely yield an integral constant expression. Save and
6398 restore these values around our local parsing. */
6399 save_ice_p = parser->integral_constant_expression_p;
6400 save_non_ice_p = parser->non_integral_constant_expression_p;
6401
6402 /* Consume the "__builtin_offsetof" token. */
6403 cp_lexer_consume_token (parser->lexer);
6404 /* Consume the opening `('. */
6405 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6406 /* Parse the type-id. */
6407 type = cp_parser_type_id (parser);
6408 /* Look for the `,'. */
6409 cp_parser_require (parser, CPP_COMMA, "`,'");
6410
6411 /* Build the (type *)null that begins the traditional offsetof macro. */
6412 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6413
6414 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6415 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6416 true, &dummy);
6417 while (true)
6418 {
6419 cp_token *token = cp_lexer_peek_token (parser->lexer);
6420 switch (token->type)
6421 {
6422 case CPP_OPEN_SQUARE:
6423 /* offsetof-member-designator "[" expression "]" */
6424 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6425 break;
6426
6427 case CPP_DOT:
6428 /* offsetof-member-designator "." identifier */
6429 cp_lexer_consume_token (parser->lexer);
6430 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6431 true, &dummy);
6432 break;
6433
6434 case CPP_CLOSE_PAREN:
6435 /* Consume the ")" token. */
6436 cp_lexer_consume_token (parser->lexer);
6437 goto success;
6438
6439 default:
6440 /* Error. We know the following require will fail, but
6441 that gives the proper error message. */
6442 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6443 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6444 expr = error_mark_node;
6445 goto failure;
6446 }
6447 }
6448
6449 success:
6450 /* If we're processing a template, we can't finish the semantics yet.
6451 Otherwise we can fold the entire expression now. */
6452 if (processing_template_decl)
6453 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6454 else
6455 expr = finish_offsetof (expr);
6456
6457 failure:
6458 parser->integral_constant_expression_p = save_ice_p;
6459 parser->non_integral_constant_expression_p = save_non_ice_p;
6460
6461 return expr;
6462 }
6463
6464 /* Parse a trait expression. */
6465
6466 static tree
6467 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6468 {
6469 cp_trait_kind kind;
6470 tree type1, type2 = NULL_TREE;
6471 bool binary = false;
6472 cp_decl_specifier_seq decl_specs;
6473
6474 switch (keyword)
6475 {
6476 case RID_HAS_NOTHROW_ASSIGN:
6477 kind = CPTK_HAS_NOTHROW_ASSIGN;
6478 break;
6479 case RID_HAS_NOTHROW_CONSTRUCTOR:
6480 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6481 break;
6482 case RID_HAS_NOTHROW_COPY:
6483 kind = CPTK_HAS_NOTHROW_COPY;
6484 break;
6485 case RID_HAS_TRIVIAL_ASSIGN:
6486 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6487 break;
6488 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6489 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6490 break;
6491 case RID_HAS_TRIVIAL_COPY:
6492 kind = CPTK_HAS_TRIVIAL_COPY;
6493 break;
6494 case RID_HAS_TRIVIAL_DESTRUCTOR:
6495 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6496 break;
6497 case RID_HAS_VIRTUAL_DESTRUCTOR:
6498 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6499 break;
6500 case RID_IS_ABSTRACT:
6501 kind = CPTK_IS_ABSTRACT;
6502 break;
6503 case RID_IS_BASE_OF:
6504 kind = CPTK_IS_BASE_OF;
6505 binary = true;
6506 break;
6507 case RID_IS_CLASS:
6508 kind = CPTK_IS_CLASS;
6509 break;
6510 case RID_IS_CONVERTIBLE_TO:
6511 kind = CPTK_IS_CONVERTIBLE_TO;
6512 binary = true;
6513 break;
6514 case RID_IS_EMPTY:
6515 kind = CPTK_IS_EMPTY;
6516 break;
6517 case RID_IS_ENUM:
6518 kind = CPTK_IS_ENUM;
6519 break;
6520 case RID_IS_POD:
6521 kind = CPTK_IS_POD;
6522 break;
6523 case RID_IS_POLYMORPHIC:
6524 kind = CPTK_IS_POLYMORPHIC;
6525 break;
6526 case RID_IS_UNION:
6527 kind = CPTK_IS_UNION;
6528 break;
6529 default:
6530 gcc_unreachable ();
6531 }
6532
6533 /* Consume the token. */
6534 cp_lexer_consume_token (parser->lexer);
6535
6536 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6537
6538 type1 = cp_parser_type_id (parser);
6539
6540 if (type1 == error_mark_node)
6541 return error_mark_node;
6542
6543 /* Build a trivial decl-specifier-seq. */
6544 clear_decl_specs (&decl_specs);
6545 decl_specs.type = type1;
6546
6547 /* Call grokdeclarator to figure out what type this is. */
6548 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6549 /*initialized=*/0, /*attrlist=*/NULL);
6550
6551 if (binary)
6552 {
6553 cp_parser_require (parser, CPP_COMMA, "`,'");
6554
6555 type2 = cp_parser_type_id (parser);
6556
6557 if (type2 == error_mark_node)
6558 return error_mark_node;
6559
6560 /* Build a trivial decl-specifier-seq. */
6561 clear_decl_specs (&decl_specs);
6562 decl_specs.type = type2;
6563
6564 /* Call grokdeclarator to figure out what type this is. */
6565 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6566 /*initialized=*/0, /*attrlist=*/NULL);
6567 }
6568
6569 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6570
6571 /* Complete the trait expression, which may mean either processing
6572 the trait expr now or saving it for template instantiation. */
6573 return finish_trait_expr (kind, type1, type2);
6574 }
6575
6576 /* Statements [gram.stmt.stmt] */
6577
6578 /* Parse a statement.
6579
6580 statement:
6581 labeled-statement
6582 expression-statement
6583 compound-statement
6584 selection-statement
6585 iteration-statement
6586 jump-statement
6587 declaration-statement
6588 try-block
6589
6590 IN_COMPOUND is true when the statement is nested inside a
6591 cp_parser_compound_statement; this matters for certain pragmas.
6592
6593 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6594 is a (possibly labeled) if statement which is not enclosed in braces
6595 and has an else clause. This is used to implement -Wparentheses. */
6596
6597 static void
6598 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6599 bool in_compound, bool *if_p)
6600 {
6601 tree statement;
6602 cp_token *token;
6603 location_t statement_location;
6604
6605 restart:
6606 if (if_p != NULL)
6607 *if_p = false;
6608 /* There is no statement yet. */
6609 statement = NULL_TREE;
6610 /* Peek at the next token. */
6611 token = cp_lexer_peek_token (parser->lexer);
6612 /* Remember the location of the first token in the statement. */
6613 statement_location = token->location;
6614 /* If this is a keyword, then that will often determine what kind of
6615 statement we have. */
6616 if (token->type == CPP_KEYWORD)
6617 {
6618 enum rid keyword = token->keyword;
6619
6620 switch (keyword)
6621 {
6622 case RID_CASE:
6623 case RID_DEFAULT:
6624 /* Looks like a labeled-statement with a case label.
6625 Parse the label, and then use tail recursion to parse
6626 the statement. */
6627 cp_parser_label_for_labeled_statement (parser);
6628 goto restart;
6629
6630 case RID_IF:
6631 case RID_SWITCH:
6632 statement = cp_parser_selection_statement (parser, if_p);
6633 break;
6634
6635 case RID_WHILE:
6636 case RID_DO:
6637 case RID_FOR:
6638 statement = cp_parser_iteration_statement (parser);
6639 break;
6640
6641 case RID_BREAK:
6642 case RID_CONTINUE:
6643 case RID_RETURN:
6644 case RID_GOTO:
6645 statement = cp_parser_jump_statement (parser);
6646 break;
6647
6648 /* Objective-C++ exception-handling constructs. */
6649 case RID_AT_TRY:
6650 case RID_AT_CATCH:
6651 case RID_AT_FINALLY:
6652 case RID_AT_SYNCHRONIZED:
6653 case RID_AT_THROW:
6654 statement = cp_parser_objc_statement (parser);
6655 break;
6656
6657 case RID_TRY:
6658 statement = cp_parser_try_block (parser);
6659 break;
6660
6661 case RID_NAMESPACE:
6662 /* This must be a namespace alias definition. */
6663 cp_parser_declaration_statement (parser);
6664 return;
6665
6666 default:
6667 /* It might be a keyword like `int' that can start a
6668 declaration-statement. */
6669 break;
6670 }
6671 }
6672 else if (token->type == CPP_NAME)
6673 {
6674 /* If the next token is a `:', then we are looking at a
6675 labeled-statement. */
6676 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6677 if (token->type == CPP_COLON)
6678 {
6679 /* Looks like a labeled-statement with an ordinary label.
6680 Parse the label, and then use tail recursion to parse
6681 the statement. */
6682 cp_parser_label_for_labeled_statement (parser);
6683 goto restart;
6684 }
6685 }
6686 /* Anything that starts with a `{' must be a compound-statement. */
6687 else if (token->type == CPP_OPEN_BRACE)
6688 statement = cp_parser_compound_statement (parser, NULL, false);
6689 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6690 a statement all its own. */
6691 else if (token->type == CPP_PRAGMA)
6692 {
6693 /* Only certain OpenMP pragmas are attached to statements, and thus
6694 are considered statements themselves. All others are not. In
6695 the context of a compound, accept the pragma as a "statement" and
6696 return so that we can check for a close brace. Otherwise we
6697 require a real statement and must go back and read one. */
6698 if (in_compound)
6699 cp_parser_pragma (parser, pragma_compound);
6700 else if (!cp_parser_pragma (parser, pragma_stmt))
6701 goto restart;
6702 return;
6703 }
6704 else if (token->type == CPP_EOF)
6705 {
6706 cp_parser_error (parser, "expected statement");
6707 return;
6708 }
6709
6710 /* Everything else must be a declaration-statement or an
6711 expression-statement. Try for the declaration-statement
6712 first, unless we are looking at a `;', in which case we know that
6713 we have an expression-statement. */
6714 if (!statement)
6715 {
6716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6717 {
6718 cp_parser_parse_tentatively (parser);
6719 /* Try to parse the declaration-statement. */
6720 cp_parser_declaration_statement (parser);
6721 /* If that worked, we're done. */
6722 if (cp_parser_parse_definitely (parser))
6723 return;
6724 }
6725 /* Look for an expression-statement instead. */
6726 statement = cp_parser_expression_statement (parser, in_statement_expr);
6727 }
6728
6729 /* Set the line number for the statement. */
6730 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6731 SET_EXPR_LOCATION (statement, statement_location);
6732 }
6733
6734 /* Parse the label for a labeled-statement, i.e.
6735
6736 identifier :
6737 case constant-expression :
6738 default :
6739
6740 GNU Extension:
6741 case constant-expression ... constant-expression : statement
6742
6743 When a label is parsed without errors, the label is added to the
6744 parse tree by the finish_* functions, so this function doesn't
6745 have to return the label. */
6746
6747 static void
6748 cp_parser_label_for_labeled_statement (cp_parser* parser)
6749 {
6750 cp_token *token;
6751
6752 /* The next token should be an identifier. */
6753 token = cp_lexer_peek_token (parser->lexer);
6754 if (token->type != CPP_NAME
6755 && token->type != CPP_KEYWORD)
6756 {
6757 cp_parser_error (parser, "expected labeled-statement");
6758 return;
6759 }
6760
6761 switch (token->keyword)
6762 {
6763 case RID_CASE:
6764 {
6765 tree expr, expr_hi;
6766 cp_token *ellipsis;
6767
6768 /* Consume the `case' token. */
6769 cp_lexer_consume_token (parser->lexer);
6770 /* Parse the constant-expression. */
6771 expr = cp_parser_constant_expression (parser,
6772 /*allow_non_constant_p=*/false,
6773 NULL);
6774
6775 ellipsis = cp_lexer_peek_token (parser->lexer);
6776 if (ellipsis->type == CPP_ELLIPSIS)
6777 {
6778 /* Consume the `...' token. */
6779 cp_lexer_consume_token (parser->lexer);
6780 expr_hi =
6781 cp_parser_constant_expression (parser,
6782 /*allow_non_constant_p=*/false,
6783 NULL);
6784 /* We don't need to emit warnings here, as the common code
6785 will do this for us. */
6786 }
6787 else
6788 expr_hi = NULL_TREE;
6789
6790 if (parser->in_switch_statement_p)
6791 finish_case_label (expr, expr_hi);
6792 else
6793 error ("case label %qE not within a switch statement", expr);
6794 }
6795 break;
6796
6797 case RID_DEFAULT:
6798 /* Consume the `default' token. */
6799 cp_lexer_consume_token (parser->lexer);
6800
6801 if (parser->in_switch_statement_p)
6802 finish_case_label (NULL_TREE, NULL_TREE);
6803 else
6804 error ("case label not within a switch statement");
6805 break;
6806
6807 default:
6808 /* Anything else must be an ordinary label. */
6809 finish_label_stmt (cp_parser_identifier (parser));
6810 break;
6811 }
6812
6813 /* Require the `:' token. */
6814 cp_parser_require (parser, CPP_COLON, "`:'");
6815 }
6816
6817 /* Parse an expression-statement.
6818
6819 expression-statement:
6820 expression [opt] ;
6821
6822 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6823 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6824 indicates whether this expression-statement is part of an
6825 expression statement. */
6826
6827 static tree
6828 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6829 {
6830 tree statement = NULL_TREE;
6831
6832 /* If the next token is a ';', then there is no expression
6833 statement. */
6834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6835 statement = cp_parser_expression (parser, /*cast_p=*/false);
6836
6837 /* Consume the final `;'. */
6838 cp_parser_consume_semicolon_at_end_of_statement (parser);
6839
6840 if (in_statement_expr
6841 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6842 /* This is the final expression statement of a statement
6843 expression. */
6844 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6845 else if (statement)
6846 statement = finish_expr_stmt (statement);
6847 else
6848 finish_stmt ();
6849
6850 return statement;
6851 }
6852
6853 /* Parse a compound-statement.
6854
6855 compound-statement:
6856 { statement-seq [opt] }
6857
6858 GNU extension:
6859
6860 compound-statement:
6861 { label-declaration-seq [opt] statement-seq [opt] }
6862
6863 label-declaration-seq:
6864 label-declaration
6865 label-declaration-seq label-declaration
6866
6867 Returns a tree representing the statement. */
6868
6869 static tree
6870 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6871 bool in_try)
6872 {
6873 tree compound_stmt;
6874
6875 /* Consume the `{'. */
6876 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6877 return error_mark_node;
6878 /* Begin the compound-statement. */
6879 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6880 /* If the next keyword is `__label__' we have a label declaration. */
6881 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6882 cp_parser_label_declaration (parser);
6883 /* Parse an (optional) statement-seq. */
6884 cp_parser_statement_seq_opt (parser, in_statement_expr);
6885 /* Finish the compound-statement. */
6886 finish_compound_stmt (compound_stmt);
6887 /* Consume the `}'. */
6888 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6889
6890 return compound_stmt;
6891 }
6892
6893 /* Parse an (optional) statement-seq.
6894
6895 statement-seq:
6896 statement
6897 statement-seq [opt] statement */
6898
6899 static void
6900 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6901 {
6902 /* Scan statements until there aren't any more. */
6903 while (true)
6904 {
6905 cp_token *token = cp_lexer_peek_token (parser->lexer);
6906
6907 /* If we're looking at a `}', then we've run out of statements. */
6908 if (token->type == CPP_CLOSE_BRACE
6909 || token->type == CPP_EOF
6910 || token->type == CPP_PRAGMA_EOL)
6911 break;
6912
6913 /* If we are in a compound statement and find 'else' then
6914 something went wrong. */
6915 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6916 {
6917 if (parser->in_statement & IN_IF_STMT)
6918 break;
6919 else
6920 {
6921 token = cp_lexer_consume_token (parser->lexer);
6922 error ("%<else%> without a previous %<if%>");
6923 }
6924 }
6925
6926 /* Parse the statement. */
6927 cp_parser_statement (parser, in_statement_expr, true, NULL);
6928 }
6929 }
6930
6931 /* Parse a selection-statement.
6932
6933 selection-statement:
6934 if ( condition ) statement
6935 if ( condition ) statement else statement
6936 switch ( condition ) statement
6937
6938 Returns the new IF_STMT or SWITCH_STMT.
6939
6940 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6941 is a (possibly labeled) if statement which is not enclosed in
6942 braces and has an else clause. This is used to implement
6943 -Wparentheses. */
6944
6945 static tree
6946 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6947 {
6948 cp_token *token;
6949 enum rid keyword;
6950
6951 if (if_p != NULL)
6952 *if_p = false;
6953
6954 /* Peek at the next token. */
6955 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6956
6957 /* See what kind of keyword it is. */
6958 keyword = token->keyword;
6959 switch (keyword)
6960 {
6961 case RID_IF:
6962 case RID_SWITCH:
6963 {
6964 tree statement;
6965 tree condition;
6966
6967 /* Look for the `('. */
6968 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6969 {
6970 cp_parser_skip_to_end_of_statement (parser);
6971 return error_mark_node;
6972 }
6973
6974 /* Begin the selection-statement. */
6975 if (keyword == RID_IF)
6976 statement = begin_if_stmt ();
6977 else
6978 statement = begin_switch_stmt ();
6979
6980 /* Parse the condition. */
6981 condition = cp_parser_condition (parser);
6982 /* Look for the `)'. */
6983 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6984 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6985 /*consume_paren=*/true);
6986
6987 if (keyword == RID_IF)
6988 {
6989 bool nested_if;
6990 unsigned char in_statement;
6991
6992 /* Add the condition. */
6993 finish_if_stmt_cond (condition, statement);
6994
6995 /* Parse the then-clause. */
6996 in_statement = parser->in_statement;
6997 parser->in_statement |= IN_IF_STMT;
6998 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6999 parser->in_statement = in_statement;
7000
7001 finish_then_clause (statement);
7002
7003 /* If the next token is `else', parse the else-clause. */
7004 if (cp_lexer_next_token_is_keyword (parser->lexer,
7005 RID_ELSE))
7006 {
7007 /* Consume the `else' keyword. */
7008 cp_lexer_consume_token (parser->lexer);
7009 begin_else_clause (statement);
7010 /* Parse the else-clause. */
7011 cp_parser_implicitly_scoped_statement (parser, NULL);
7012 finish_else_clause (statement);
7013
7014 /* If we are currently parsing a then-clause, then
7015 IF_P will not be NULL. We set it to true to
7016 indicate that this if statement has an else clause.
7017 This may trigger the Wparentheses warning below
7018 when we get back up to the parent if statement. */
7019 if (if_p != NULL)
7020 *if_p = true;
7021 }
7022 else
7023 {
7024 /* This if statement does not have an else clause. If
7025 NESTED_IF is true, then the then-clause is an if
7026 statement which does have an else clause. We warn
7027 about the potential ambiguity. */
7028 if (nested_if)
7029 warning (OPT_Wparentheses,
7030 ("%Hsuggest explicit braces "
7031 "to avoid ambiguous %<else%>"),
7032 EXPR_LOCUS (statement));
7033 }
7034
7035 /* Now we're all done with the if-statement. */
7036 finish_if_stmt (statement);
7037 }
7038 else
7039 {
7040 bool in_switch_statement_p;
7041 unsigned char in_statement;
7042
7043 /* Add the condition. */
7044 finish_switch_cond (condition, statement);
7045
7046 /* Parse the body of the switch-statement. */
7047 in_switch_statement_p = parser->in_switch_statement_p;
7048 in_statement = parser->in_statement;
7049 parser->in_switch_statement_p = true;
7050 parser->in_statement |= IN_SWITCH_STMT;
7051 cp_parser_implicitly_scoped_statement (parser, NULL);
7052 parser->in_switch_statement_p = in_switch_statement_p;
7053 parser->in_statement = in_statement;
7054
7055 /* Now we're all done with the switch-statement. */
7056 finish_switch_stmt (statement);
7057 }
7058
7059 return statement;
7060 }
7061 break;
7062
7063 default:
7064 cp_parser_error (parser, "expected selection-statement");
7065 return error_mark_node;
7066 }
7067 }
7068
7069 /* Parse a condition.
7070
7071 condition:
7072 expression
7073 type-specifier-seq declarator = assignment-expression
7074
7075 GNU Extension:
7076
7077 condition:
7078 type-specifier-seq declarator asm-specification [opt]
7079 attributes [opt] = assignment-expression
7080
7081 Returns the expression that should be tested. */
7082
7083 static tree
7084 cp_parser_condition (cp_parser* parser)
7085 {
7086 cp_decl_specifier_seq type_specifiers;
7087 const char *saved_message;
7088
7089 /* Try the declaration first. */
7090 cp_parser_parse_tentatively (parser);
7091 /* New types are not allowed in the type-specifier-seq for a
7092 condition. */
7093 saved_message = parser->type_definition_forbidden_message;
7094 parser->type_definition_forbidden_message
7095 = "types may not be defined in conditions";
7096 /* Parse the type-specifier-seq. */
7097 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7098 &type_specifiers);
7099 /* Restore the saved message. */
7100 parser->type_definition_forbidden_message = saved_message;
7101 /* If all is well, we might be looking at a declaration. */
7102 if (!cp_parser_error_occurred (parser))
7103 {
7104 tree decl;
7105 tree asm_specification;
7106 tree attributes;
7107 cp_declarator *declarator;
7108 tree initializer = NULL_TREE;
7109
7110 /* Parse the declarator. */
7111 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7112 /*ctor_dtor_or_conv_p=*/NULL,
7113 /*parenthesized_p=*/NULL,
7114 /*member_p=*/false);
7115 /* Parse the attributes. */
7116 attributes = cp_parser_attributes_opt (parser);
7117 /* Parse the asm-specification. */
7118 asm_specification = cp_parser_asm_specification_opt (parser);
7119 /* If the next token is not an `=', then we might still be
7120 looking at an expression. For example:
7121
7122 if (A(a).x)
7123
7124 looks like a decl-specifier-seq and a declarator -- but then
7125 there is no `=', so this is an expression. */
7126 cp_parser_require (parser, CPP_EQ, "`='");
7127 /* If we did see an `=', then we are looking at a declaration
7128 for sure. */
7129 if (cp_parser_parse_definitely (parser))
7130 {
7131 tree pushed_scope;
7132 bool non_constant_p;
7133
7134 /* Create the declaration. */
7135 decl = start_decl (declarator, &type_specifiers,
7136 /*initialized_p=*/true,
7137 attributes, /*prefix_attributes=*/NULL_TREE,
7138 &pushed_scope);
7139 /* Parse the assignment-expression. */
7140 initializer
7141 = cp_parser_constant_expression (parser,
7142 /*allow_non_constant_p=*/true,
7143 &non_constant_p);
7144 if (!non_constant_p)
7145 initializer = fold_non_dependent_expr (initializer);
7146
7147 /* Process the initializer. */
7148 cp_finish_decl (decl,
7149 initializer, !non_constant_p,
7150 asm_specification,
7151 LOOKUP_ONLYCONVERTING);
7152
7153 if (pushed_scope)
7154 pop_scope (pushed_scope);
7155
7156 return convert_from_reference (decl);
7157 }
7158 }
7159 /* If we didn't even get past the declarator successfully, we are
7160 definitely not looking at a declaration. */
7161 else
7162 cp_parser_abort_tentative_parse (parser);
7163
7164 /* Otherwise, we are looking at an expression. */
7165 return cp_parser_expression (parser, /*cast_p=*/false);
7166 }
7167
7168 /* We check for a ) immediately followed by ; with no whitespacing
7169 between. This is used to issue a warning for:
7170
7171 while (...);
7172
7173 and:
7174
7175 for (...);
7176
7177 as the semicolon is probably extraneous.
7178
7179 On parse errors, the next token might not be a ), so do nothing in
7180 that case. */
7181
7182 static void
7183 check_empty_body (cp_parser* parser, const char* type)
7184 {
7185 cp_token *token;
7186 cp_token *close_paren;
7187 expanded_location close_loc;
7188 expanded_location semi_loc;
7189
7190 close_paren = cp_lexer_peek_token (parser->lexer);
7191 if (close_paren->type != CPP_CLOSE_PAREN)
7192 return;
7193
7194 close_loc = expand_location (close_paren->location);
7195 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7196
7197 if (token->type != CPP_SEMICOLON
7198 || (token->flags & PREV_WHITE))
7199 return;
7200
7201 semi_loc = expand_location (token->location);
7202 if (close_loc.line == semi_loc.line
7203 #ifdef USE_MAPPED_LOCATION
7204 && close_loc.column+1 == semi_loc.column
7205 #endif
7206 )
7207 warning (OPT_Wempty_body,
7208 "suggest a space before %<;%> or explicit braces around empty "
7209 "body in %<%s%> statement",
7210 type);
7211 }
7212
7213 /* Parse an iteration-statement.
7214
7215 iteration-statement:
7216 while ( condition ) statement
7217 do statement while ( expression ) ;
7218 for ( for-init-statement condition [opt] ; expression [opt] )
7219 statement
7220
7221 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7222
7223 static tree
7224 cp_parser_iteration_statement (cp_parser* parser)
7225 {
7226 cp_token *token;
7227 enum rid keyword;
7228 tree statement;
7229 unsigned char in_statement;
7230
7231 /* Peek at the next token. */
7232 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7233 if (!token)
7234 return error_mark_node;
7235
7236 /* Remember whether or not we are already within an iteration
7237 statement. */
7238 in_statement = parser->in_statement;
7239
7240 /* See what kind of keyword it is. */
7241 keyword = token->keyword;
7242 switch (keyword)
7243 {
7244 case RID_WHILE:
7245 {
7246 tree condition;
7247
7248 /* Begin the while-statement. */
7249 statement = begin_while_stmt ();
7250 /* Look for the `('. */
7251 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7252 /* Parse the condition. */
7253 condition = cp_parser_condition (parser);
7254 finish_while_stmt_cond (condition, statement);
7255 check_empty_body (parser, "while");
7256 /* Look for the `)'. */
7257 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7258 /* Parse the dependent statement. */
7259 parser->in_statement = IN_ITERATION_STMT;
7260 cp_parser_already_scoped_statement (parser);
7261 parser->in_statement = in_statement;
7262 /* We're done with the while-statement. */
7263 finish_while_stmt (statement);
7264 }
7265 break;
7266
7267 case RID_DO:
7268 {
7269 tree expression;
7270
7271 /* Begin the do-statement. */
7272 statement = begin_do_stmt ();
7273 /* Parse the body of the do-statement. */
7274 parser->in_statement = IN_ITERATION_STMT;
7275 cp_parser_implicitly_scoped_statement (parser, NULL);
7276 parser->in_statement = in_statement;
7277 finish_do_body (statement);
7278 /* Look for the `while' keyword. */
7279 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7280 /* Look for the `('. */
7281 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7282 /* Parse the expression. */
7283 expression = cp_parser_expression (parser, /*cast_p=*/false);
7284 /* We're done with the do-statement. */
7285 finish_do_stmt (expression, statement);
7286 /* Look for the `)'. */
7287 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7288 /* Look for the `;'. */
7289 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7290 }
7291 break;
7292
7293 case RID_FOR:
7294 {
7295 tree condition = NULL_TREE;
7296 tree expression = NULL_TREE;
7297
7298 /* Begin the for-statement. */
7299 statement = begin_for_stmt ();
7300 /* Look for the `('. */
7301 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7302 /* Parse the initialization. */
7303 cp_parser_for_init_statement (parser);
7304 finish_for_init_stmt (statement);
7305
7306 /* If there's a condition, process it. */
7307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7308 condition = cp_parser_condition (parser);
7309 finish_for_cond (condition, statement);
7310 /* Look for the `;'. */
7311 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7312
7313 /* If there's an expression, process it. */
7314 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7315 expression = cp_parser_expression (parser, /*cast_p=*/false);
7316 finish_for_expr (expression, statement);
7317 check_empty_body (parser, "for");
7318 /* Look for the `)'. */
7319 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7320
7321 /* Parse the body of the for-statement. */
7322 parser->in_statement = IN_ITERATION_STMT;
7323 cp_parser_already_scoped_statement (parser);
7324 parser->in_statement = in_statement;
7325
7326 /* We're done with the for-statement. */
7327 finish_for_stmt (statement);
7328 }
7329 break;
7330
7331 default:
7332 cp_parser_error (parser, "expected iteration-statement");
7333 statement = error_mark_node;
7334 break;
7335 }
7336
7337 return statement;
7338 }
7339
7340 /* Parse a for-init-statement.
7341
7342 for-init-statement:
7343 expression-statement
7344 simple-declaration */
7345
7346 static void
7347 cp_parser_for_init_statement (cp_parser* parser)
7348 {
7349 /* If the next token is a `;', then we have an empty
7350 expression-statement. Grammatically, this is also a
7351 simple-declaration, but an invalid one, because it does not
7352 declare anything. Therefore, if we did not handle this case
7353 specially, we would issue an error message about an invalid
7354 declaration. */
7355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7356 {
7357 /* We're going to speculatively look for a declaration, falling back
7358 to an expression, if necessary. */
7359 cp_parser_parse_tentatively (parser);
7360 /* Parse the declaration. */
7361 cp_parser_simple_declaration (parser,
7362 /*function_definition_allowed_p=*/false);
7363 /* If the tentative parse failed, then we shall need to look for an
7364 expression-statement. */
7365 if (cp_parser_parse_definitely (parser))
7366 return;
7367 }
7368
7369 cp_parser_expression_statement (parser, false);
7370 }
7371
7372 /* Parse a jump-statement.
7373
7374 jump-statement:
7375 break ;
7376 continue ;
7377 return expression [opt] ;
7378 goto identifier ;
7379
7380 GNU extension:
7381
7382 jump-statement:
7383 goto * expression ;
7384
7385 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7386
7387 static tree
7388 cp_parser_jump_statement (cp_parser* parser)
7389 {
7390 tree statement = error_mark_node;
7391 cp_token *token;
7392 enum rid keyword;
7393 unsigned char in_statement;
7394
7395 /* Peek at the next token. */
7396 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7397 if (!token)
7398 return error_mark_node;
7399
7400 /* See what kind of keyword it is. */
7401 keyword = token->keyword;
7402 switch (keyword)
7403 {
7404 case RID_BREAK:
7405 in_statement = parser->in_statement & ~IN_IF_STMT;
7406 switch (in_statement)
7407 {
7408 case 0:
7409 error ("break statement not within loop or switch");
7410 break;
7411 default:
7412 gcc_assert ((in_statement & IN_SWITCH_STMT)
7413 || in_statement == IN_ITERATION_STMT);
7414 statement = finish_break_stmt ();
7415 break;
7416 case IN_OMP_BLOCK:
7417 error ("invalid exit from OpenMP structured block");
7418 break;
7419 case IN_OMP_FOR:
7420 error ("break statement used with OpenMP for loop");
7421 break;
7422 }
7423 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7424 break;
7425
7426 case RID_CONTINUE:
7427 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7428 {
7429 case 0:
7430 error ("continue statement not within a loop");
7431 break;
7432 case IN_ITERATION_STMT:
7433 case IN_OMP_FOR:
7434 statement = finish_continue_stmt ();
7435 break;
7436 case IN_OMP_BLOCK:
7437 error ("invalid exit from OpenMP structured block");
7438 break;
7439 default:
7440 gcc_unreachable ();
7441 }
7442 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7443 break;
7444
7445 case RID_RETURN:
7446 {
7447 tree expr;
7448
7449 /* If the next token is a `;', then there is no
7450 expression. */
7451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7452 expr = cp_parser_expression (parser, /*cast_p=*/false);
7453 else
7454 expr = NULL_TREE;
7455 /* Build the return-statement. */
7456 statement = finish_return_stmt (expr);
7457 /* Look for the final `;'. */
7458 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7459 }
7460 break;
7461
7462 case RID_GOTO:
7463 /* Create the goto-statement. */
7464 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7465 {
7466 /* Issue a warning about this use of a GNU extension. */
7467 if (pedantic)
7468 pedwarn ("ISO C++ forbids computed gotos");
7469 /* Consume the '*' token. */
7470 cp_lexer_consume_token (parser->lexer);
7471 /* Parse the dependent expression. */
7472 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7473 }
7474 else
7475 finish_goto_stmt (cp_parser_identifier (parser));
7476 /* Look for the final `;'. */
7477 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7478 break;
7479
7480 default:
7481 cp_parser_error (parser, "expected jump-statement");
7482 break;
7483 }
7484
7485 return statement;
7486 }
7487
7488 /* Parse a declaration-statement.
7489
7490 declaration-statement:
7491 block-declaration */
7492
7493 static void
7494 cp_parser_declaration_statement (cp_parser* parser)
7495 {
7496 void *p;
7497
7498 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7499 p = obstack_alloc (&declarator_obstack, 0);
7500
7501 /* Parse the block-declaration. */
7502 cp_parser_block_declaration (parser, /*statement_p=*/true);
7503
7504 /* Free any declarators allocated. */
7505 obstack_free (&declarator_obstack, p);
7506
7507 /* Finish off the statement. */
7508 finish_stmt ();
7509 }
7510
7511 /* Some dependent statements (like `if (cond) statement'), are
7512 implicitly in their own scope. In other words, if the statement is
7513 a single statement (as opposed to a compound-statement), it is
7514 none-the-less treated as if it were enclosed in braces. Any
7515 declarations appearing in the dependent statement are out of scope
7516 after control passes that point. This function parses a statement,
7517 but ensures that is in its own scope, even if it is not a
7518 compound-statement.
7519
7520 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7521 is a (possibly labeled) if statement which is not enclosed in
7522 braces and has an else clause. This is used to implement
7523 -Wparentheses.
7524
7525 Returns the new statement. */
7526
7527 static tree
7528 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7529 {
7530 tree statement;
7531
7532 if (if_p != NULL)
7533 *if_p = false;
7534
7535 /* Mark if () ; with a special NOP_EXPR. */
7536 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7537 {
7538 cp_lexer_consume_token (parser->lexer);
7539 statement = add_stmt (build_empty_stmt ());
7540 }
7541 /* if a compound is opened, we simply parse the statement directly. */
7542 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7543 statement = cp_parser_compound_statement (parser, NULL, false);
7544 /* If the token is not a `{', then we must take special action. */
7545 else
7546 {
7547 /* Create a compound-statement. */
7548 statement = begin_compound_stmt (0);
7549 /* Parse the dependent-statement. */
7550 cp_parser_statement (parser, NULL_TREE, false, if_p);
7551 /* Finish the dummy compound-statement. */
7552 finish_compound_stmt (statement);
7553 }
7554
7555 /* Return the statement. */
7556 return statement;
7557 }
7558
7559 /* For some dependent statements (like `while (cond) statement'), we
7560 have already created a scope. Therefore, even if the dependent
7561 statement is a compound-statement, we do not want to create another
7562 scope. */
7563
7564 static void
7565 cp_parser_already_scoped_statement (cp_parser* parser)
7566 {
7567 /* If the token is a `{', then we must take special action. */
7568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7569 cp_parser_statement (parser, NULL_TREE, false, NULL);
7570 else
7571 {
7572 /* Avoid calling cp_parser_compound_statement, so that we
7573 don't create a new scope. Do everything else by hand. */
7574 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7575 cp_parser_statement_seq_opt (parser, NULL_TREE);
7576 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7577 }
7578 }
7579
7580 /* Declarations [gram.dcl.dcl] */
7581
7582 /* Parse an optional declaration-sequence.
7583
7584 declaration-seq:
7585 declaration
7586 declaration-seq declaration */
7587
7588 static void
7589 cp_parser_declaration_seq_opt (cp_parser* parser)
7590 {
7591 while (true)
7592 {
7593 cp_token *token;
7594
7595 token = cp_lexer_peek_token (parser->lexer);
7596
7597 if (token->type == CPP_CLOSE_BRACE
7598 || token->type == CPP_EOF
7599 || token->type == CPP_PRAGMA_EOL)
7600 break;
7601
7602 if (token->type == CPP_SEMICOLON)
7603 {
7604 /* A declaration consisting of a single semicolon is
7605 invalid. Allow it unless we're being pedantic. */
7606 cp_lexer_consume_token (parser->lexer);
7607 if (pedantic && !in_system_header)
7608 pedwarn ("extra %<;%>");
7609 continue;
7610 }
7611
7612 /* If we're entering or exiting a region that's implicitly
7613 extern "C", modify the lang context appropriately. */
7614 if (!parser->implicit_extern_c && token->implicit_extern_c)
7615 {
7616 push_lang_context (lang_name_c);
7617 parser->implicit_extern_c = true;
7618 }
7619 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7620 {
7621 pop_lang_context ();
7622 parser->implicit_extern_c = false;
7623 }
7624
7625 if (token->type == CPP_PRAGMA)
7626 {
7627 /* A top-level declaration can consist solely of a #pragma.
7628 A nested declaration cannot, so this is done here and not
7629 in cp_parser_declaration. (A #pragma at block scope is
7630 handled in cp_parser_statement.) */
7631 cp_parser_pragma (parser, pragma_external);
7632 continue;
7633 }
7634
7635 /* Parse the declaration itself. */
7636 cp_parser_declaration (parser);
7637 }
7638 }
7639
7640 /* Parse a declaration.
7641
7642 declaration:
7643 block-declaration
7644 function-definition
7645 template-declaration
7646 explicit-instantiation
7647 explicit-specialization
7648 linkage-specification
7649 namespace-definition
7650
7651 GNU extension:
7652
7653 declaration:
7654 __extension__ declaration */
7655
7656 static void
7657 cp_parser_declaration (cp_parser* parser)
7658 {
7659 cp_token token1;
7660 cp_token token2;
7661 int saved_pedantic;
7662 void *p;
7663
7664 /* Check for the `__extension__' keyword. */
7665 if (cp_parser_extension_opt (parser, &saved_pedantic))
7666 {
7667 /* Parse the qualified declaration. */
7668 cp_parser_declaration (parser);
7669 /* Restore the PEDANTIC flag. */
7670 pedantic = saved_pedantic;
7671
7672 return;
7673 }
7674
7675 /* Try to figure out what kind of declaration is present. */
7676 token1 = *cp_lexer_peek_token (parser->lexer);
7677
7678 if (token1.type != CPP_EOF)
7679 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7680 else
7681 {
7682 token2.type = CPP_EOF;
7683 token2.keyword = RID_MAX;
7684 }
7685
7686 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7687 p = obstack_alloc (&declarator_obstack, 0);
7688
7689 /* If the next token is `extern' and the following token is a string
7690 literal, then we have a linkage specification. */
7691 if (token1.keyword == RID_EXTERN
7692 && cp_parser_is_string_literal (&token2))
7693 cp_parser_linkage_specification (parser);
7694 /* If the next token is `template', then we have either a template
7695 declaration, an explicit instantiation, or an explicit
7696 specialization. */
7697 else if (token1.keyword == RID_TEMPLATE)
7698 {
7699 /* `template <>' indicates a template specialization. */
7700 if (token2.type == CPP_LESS
7701 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7702 cp_parser_explicit_specialization (parser);
7703 /* `template <' indicates a template declaration. */
7704 else if (token2.type == CPP_LESS)
7705 cp_parser_template_declaration (parser, /*member_p=*/false);
7706 /* Anything else must be an explicit instantiation. */
7707 else
7708 cp_parser_explicit_instantiation (parser);
7709 }
7710 /* If the next token is `export', then we have a template
7711 declaration. */
7712 else if (token1.keyword == RID_EXPORT)
7713 cp_parser_template_declaration (parser, /*member_p=*/false);
7714 /* If the next token is `extern', 'static' or 'inline' and the one
7715 after that is `template', we have a GNU extended explicit
7716 instantiation directive. */
7717 else if (cp_parser_allow_gnu_extensions_p (parser)
7718 && (token1.keyword == RID_EXTERN
7719 || token1.keyword == RID_STATIC
7720 || token1.keyword == RID_INLINE)
7721 && token2.keyword == RID_TEMPLATE)
7722 cp_parser_explicit_instantiation (parser);
7723 /* If the next token is `namespace', check for a named or unnamed
7724 namespace definition. */
7725 else if (token1.keyword == RID_NAMESPACE
7726 && (/* A named namespace definition. */
7727 (token2.type == CPP_NAME
7728 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7729 != CPP_EQ))
7730 /* An unnamed namespace definition. */
7731 || token2.type == CPP_OPEN_BRACE
7732 || token2.keyword == RID_ATTRIBUTE))
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 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8606 /*consume_paren=*/true);
8607
8608 return finish_decltype_type (expr, id_expression_or_member_access_p);
8609 }
8610
8611 /* Special member functions [gram.special] */
8612
8613 /* Parse a conversion-function-id.
8614
8615 conversion-function-id:
8616 operator conversion-type-id
8617
8618 Returns an IDENTIFIER_NODE representing the operator. */
8619
8620 static tree
8621 cp_parser_conversion_function_id (cp_parser* parser)
8622 {
8623 tree type;
8624 tree saved_scope;
8625 tree saved_qualifying_scope;
8626 tree saved_object_scope;
8627 tree pushed_scope = NULL_TREE;
8628
8629 /* Look for the `operator' token. */
8630 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8631 return error_mark_node;
8632 /* When we parse the conversion-type-id, the current scope will be
8633 reset. However, we need that information in able to look up the
8634 conversion function later, so we save it here. */
8635 saved_scope = parser->scope;
8636 saved_qualifying_scope = parser->qualifying_scope;
8637 saved_object_scope = parser->object_scope;
8638 /* We must enter the scope of the class so that the names of
8639 entities declared within the class are available in the
8640 conversion-type-id. For example, consider:
8641
8642 struct S {
8643 typedef int I;
8644 operator I();
8645 };
8646
8647 S::operator I() { ... }
8648
8649 In order to see that `I' is a type-name in the definition, we
8650 must be in the scope of `S'. */
8651 if (saved_scope)
8652 pushed_scope = push_scope (saved_scope);
8653 /* Parse the conversion-type-id. */
8654 type = cp_parser_conversion_type_id (parser);
8655 /* Leave the scope of the class, if any. */
8656 if (pushed_scope)
8657 pop_scope (pushed_scope);
8658 /* Restore the saved scope. */
8659 parser->scope = saved_scope;
8660 parser->qualifying_scope = saved_qualifying_scope;
8661 parser->object_scope = saved_object_scope;
8662 /* If the TYPE is invalid, indicate failure. */
8663 if (type == error_mark_node)
8664 return error_mark_node;
8665 return mangle_conv_op_name_for_type (type);
8666 }
8667
8668 /* Parse a conversion-type-id:
8669
8670 conversion-type-id:
8671 type-specifier-seq conversion-declarator [opt]
8672
8673 Returns the TYPE specified. */
8674
8675 static tree
8676 cp_parser_conversion_type_id (cp_parser* parser)
8677 {
8678 tree attributes;
8679 cp_decl_specifier_seq type_specifiers;
8680 cp_declarator *declarator;
8681 tree type_specified;
8682
8683 /* Parse the attributes. */
8684 attributes = cp_parser_attributes_opt (parser);
8685 /* Parse the type-specifiers. */
8686 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8687 &type_specifiers);
8688 /* If that didn't work, stop. */
8689 if (type_specifiers.type == error_mark_node)
8690 return error_mark_node;
8691 /* Parse the conversion-declarator. */
8692 declarator = cp_parser_conversion_declarator_opt (parser);
8693
8694 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8695 /*initialized=*/0, &attributes);
8696 if (attributes)
8697 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8698 return type_specified;
8699 }
8700
8701 /* Parse an (optional) conversion-declarator.
8702
8703 conversion-declarator:
8704 ptr-operator conversion-declarator [opt]
8705
8706 */
8707
8708 static cp_declarator *
8709 cp_parser_conversion_declarator_opt (cp_parser* parser)
8710 {
8711 enum tree_code code;
8712 tree class_type;
8713 cp_cv_quals cv_quals;
8714
8715 /* We don't know if there's a ptr-operator next, or not. */
8716 cp_parser_parse_tentatively (parser);
8717 /* Try the ptr-operator. */
8718 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8719 /* If it worked, look for more conversion-declarators. */
8720 if (cp_parser_parse_definitely (parser))
8721 {
8722 cp_declarator *declarator;
8723
8724 /* Parse another optional declarator. */
8725 declarator = cp_parser_conversion_declarator_opt (parser);
8726
8727 return cp_parser_make_indirect_declarator
8728 (code, class_type, cv_quals, declarator);
8729 }
8730
8731 return NULL;
8732 }
8733
8734 /* Parse an (optional) ctor-initializer.
8735
8736 ctor-initializer:
8737 : mem-initializer-list
8738
8739 Returns TRUE iff the ctor-initializer was actually present. */
8740
8741 static bool
8742 cp_parser_ctor_initializer_opt (cp_parser* parser)
8743 {
8744 /* If the next token is not a `:', then there is no
8745 ctor-initializer. */
8746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8747 {
8748 /* Do default initialization of any bases and members. */
8749 if (DECL_CONSTRUCTOR_P (current_function_decl))
8750 finish_mem_initializers (NULL_TREE);
8751
8752 return false;
8753 }
8754
8755 /* Consume the `:' token. */
8756 cp_lexer_consume_token (parser->lexer);
8757 /* And the mem-initializer-list. */
8758 cp_parser_mem_initializer_list (parser);
8759
8760 return true;
8761 }
8762
8763 /* Parse a mem-initializer-list.
8764
8765 mem-initializer-list:
8766 mem-initializer ... [opt]
8767 mem-initializer ... [opt] , mem-initializer-list */
8768
8769 static void
8770 cp_parser_mem_initializer_list (cp_parser* parser)
8771 {
8772 tree mem_initializer_list = NULL_TREE;
8773
8774 /* Let the semantic analysis code know that we are starting the
8775 mem-initializer-list. */
8776 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8777 error ("only constructors take base initializers");
8778
8779 /* Loop through the list. */
8780 while (true)
8781 {
8782 tree mem_initializer;
8783
8784 /* Parse the mem-initializer. */
8785 mem_initializer = cp_parser_mem_initializer (parser);
8786 /* If the next token is a `...', we're expanding member initializers. */
8787 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8788 {
8789 /* Consume the `...'. */
8790 cp_lexer_consume_token (parser->lexer);
8791
8792 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8793 can be expanded but members cannot. */
8794 if (mem_initializer != error_mark_node
8795 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8796 {
8797 error ("cannot expand initializer for member %<%D%>",
8798 TREE_PURPOSE (mem_initializer));
8799 mem_initializer = error_mark_node;
8800 }
8801
8802 /* Construct the pack expansion type. */
8803 if (mem_initializer != error_mark_node)
8804 mem_initializer = make_pack_expansion (mem_initializer);
8805 }
8806 /* Add it to the list, unless it was erroneous. */
8807 if (mem_initializer != error_mark_node)
8808 {
8809 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8810 mem_initializer_list = mem_initializer;
8811 }
8812 /* If the next token is not a `,', we're done. */
8813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8814 break;
8815 /* Consume the `,' token. */
8816 cp_lexer_consume_token (parser->lexer);
8817 }
8818
8819 /* Perform semantic analysis. */
8820 if (DECL_CONSTRUCTOR_P (current_function_decl))
8821 finish_mem_initializers (mem_initializer_list);
8822 }
8823
8824 /* Parse a mem-initializer.
8825
8826 mem-initializer:
8827 mem-initializer-id ( expression-list [opt] )
8828
8829 GNU extension:
8830
8831 mem-initializer:
8832 ( expression-list [opt] )
8833
8834 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8835 class) or FIELD_DECL (for a non-static data member) to initialize;
8836 the TREE_VALUE is the expression-list. An empty initialization
8837 list is represented by void_list_node. */
8838
8839 static tree
8840 cp_parser_mem_initializer (cp_parser* parser)
8841 {
8842 tree mem_initializer_id;
8843 tree expression_list;
8844 tree member;
8845
8846 /* Find out what is being initialized. */
8847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8848 {
8849 pedwarn ("anachronistic old-style base class initializer");
8850 mem_initializer_id = NULL_TREE;
8851 }
8852 else
8853 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8854 member = expand_member_init (mem_initializer_id);
8855 if (member && !DECL_P (member))
8856 in_base_initializer = 1;
8857
8858 expression_list
8859 = cp_parser_parenthesized_expression_list (parser, false,
8860 /*cast_p=*/false,
8861 /*allow_expansion_p=*/true,
8862 /*non_constant_p=*/NULL);
8863 if (expression_list == error_mark_node)
8864 return error_mark_node;
8865 if (!expression_list)
8866 expression_list = void_type_node;
8867
8868 in_base_initializer = 0;
8869
8870 return member ? build_tree_list (member, expression_list) : error_mark_node;
8871 }
8872
8873 /* Parse a mem-initializer-id.
8874
8875 mem-initializer-id:
8876 :: [opt] nested-name-specifier [opt] class-name
8877 identifier
8878
8879 Returns a TYPE indicating the class to be initializer for the first
8880 production. Returns an IDENTIFIER_NODE indicating the data member
8881 to be initialized for the second production. */
8882
8883 static tree
8884 cp_parser_mem_initializer_id (cp_parser* parser)
8885 {
8886 bool global_scope_p;
8887 bool nested_name_specifier_p;
8888 bool template_p = false;
8889 tree id;
8890
8891 /* `typename' is not allowed in this context ([temp.res]). */
8892 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8893 {
8894 error ("keyword %<typename%> not allowed in this context (a qualified "
8895 "member initializer is implicitly a type)");
8896 cp_lexer_consume_token (parser->lexer);
8897 }
8898 /* Look for the optional `::' operator. */
8899 global_scope_p
8900 = (cp_parser_global_scope_opt (parser,
8901 /*current_scope_valid_p=*/false)
8902 != NULL_TREE);
8903 /* Look for the optional nested-name-specifier. The simplest way to
8904 implement:
8905
8906 [temp.res]
8907
8908 The keyword `typename' is not permitted in a base-specifier or
8909 mem-initializer; in these contexts a qualified name that
8910 depends on a template-parameter is implicitly assumed to be a
8911 type name.
8912
8913 is to assume that we have seen the `typename' keyword at this
8914 point. */
8915 nested_name_specifier_p
8916 = (cp_parser_nested_name_specifier_opt (parser,
8917 /*typename_keyword_p=*/true,
8918 /*check_dependency_p=*/true,
8919 /*type_p=*/true,
8920 /*is_declaration=*/true)
8921 != NULL_TREE);
8922 if (nested_name_specifier_p)
8923 template_p = cp_parser_optional_template_keyword (parser);
8924 /* If there is a `::' operator or a nested-name-specifier, then we
8925 are definitely looking for a class-name. */
8926 if (global_scope_p || nested_name_specifier_p)
8927 return cp_parser_class_name (parser,
8928 /*typename_keyword_p=*/true,
8929 /*template_keyword_p=*/template_p,
8930 none_type,
8931 /*check_dependency_p=*/true,
8932 /*class_head_p=*/false,
8933 /*is_declaration=*/true);
8934 /* Otherwise, we could also be looking for an ordinary identifier. */
8935 cp_parser_parse_tentatively (parser);
8936 /* Try a class-name. */
8937 id = cp_parser_class_name (parser,
8938 /*typename_keyword_p=*/true,
8939 /*template_keyword_p=*/false,
8940 none_type,
8941 /*check_dependency_p=*/true,
8942 /*class_head_p=*/false,
8943 /*is_declaration=*/true);
8944 /* If we found one, we're done. */
8945 if (cp_parser_parse_definitely (parser))
8946 return id;
8947 /* Otherwise, look for an ordinary identifier. */
8948 return cp_parser_identifier (parser);
8949 }
8950
8951 /* Overloading [gram.over] */
8952
8953 /* Parse an operator-function-id.
8954
8955 operator-function-id:
8956 operator operator
8957
8958 Returns an IDENTIFIER_NODE for the operator which is a
8959 human-readable spelling of the identifier, e.g., `operator +'. */
8960
8961 static tree
8962 cp_parser_operator_function_id (cp_parser* parser)
8963 {
8964 /* Look for the `operator' keyword. */
8965 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8966 return error_mark_node;
8967 /* And then the name of the operator itself. */
8968 return cp_parser_operator (parser);
8969 }
8970
8971 /* Parse an operator.
8972
8973 operator:
8974 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8975 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8976 || ++ -- , ->* -> () []
8977
8978 GNU Extensions:
8979
8980 operator:
8981 <? >? <?= >?=
8982
8983 Returns an IDENTIFIER_NODE for the operator which is a
8984 human-readable spelling of the identifier, e.g., `operator +'. */
8985
8986 static tree
8987 cp_parser_operator (cp_parser* parser)
8988 {
8989 tree id = NULL_TREE;
8990 cp_token *token;
8991
8992 /* Peek at the next token. */
8993 token = cp_lexer_peek_token (parser->lexer);
8994 /* Figure out which operator we have. */
8995 switch (token->type)
8996 {
8997 case CPP_KEYWORD:
8998 {
8999 enum tree_code op;
9000
9001 /* The keyword should be either `new' or `delete'. */
9002 if (token->keyword == RID_NEW)
9003 op = NEW_EXPR;
9004 else if (token->keyword == RID_DELETE)
9005 op = DELETE_EXPR;
9006 else
9007 break;
9008
9009 /* Consume the `new' or `delete' token. */
9010 cp_lexer_consume_token (parser->lexer);
9011
9012 /* Peek at the next token. */
9013 token = cp_lexer_peek_token (parser->lexer);
9014 /* If it's a `[' token then this is the array variant of the
9015 operator. */
9016 if (token->type == CPP_OPEN_SQUARE)
9017 {
9018 /* Consume the `[' token. */
9019 cp_lexer_consume_token (parser->lexer);
9020 /* Look for the `]' token. */
9021 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9022 id = ansi_opname (op == NEW_EXPR
9023 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9024 }
9025 /* Otherwise, we have the non-array variant. */
9026 else
9027 id = ansi_opname (op);
9028
9029 return id;
9030 }
9031
9032 case CPP_PLUS:
9033 id = ansi_opname (PLUS_EXPR);
9034 break;
9035
9036 case CPP_MINUS:
9037 id = ansi_opname (MINUS_EXPR);
9038 break;
9039
9040 case CPP_MULT:
9041 id = ansi_opname (MULT_EXPR);
9042 break;
9043
9044 case CPP_DIV:
9045 id = ansi_opname (TRUNC_DIV_EXPR);
9046 break;
9047
9048 case CPP_MOD:
9049 id = ansi_opname (TRUNC_MOD_EXPR);
9050 break;
9051
9052 case CPP_XOR:
9053 id = ansi_opname (BIT_XOR_EXPR);
9054 break;
9055
9056 case CPP_AND:
9057 id = ansi_opname (BIT_AND_EXPR);
9058 break;
9059
9060 case CPP_OR:
9061 id = ansi_opname (BIT_IOR_EXPR);
9062 break;
9063
9064 case CPP_COMPL:
9065 id = ansi_opname (BIT_NOT_EXPR);
9066 break;
9067
9068 case CPP_NOT:
9069 id = ansi_opname (TRUTH_NOT_EXPR);
9070 break;
9071
9072 case CPP_EQ:
9073 id = ansi_assopname (NOP_EXPR);
9074 break;
9075
9076 case CPP_LESS:
9077 id = ansi_opname (LT_EXPR);
9078 break;
9079
9080 case CPP_GREATER:
9081 id = ansi_opname (GT_EXPR);
9082 break;
9083
9084 case CPP_PLUS_EQ:
9085 id = ansi_assopname (PLUS_EXPR);
9086 break;
9087
9088 case CPP_MINUS_EQ:
9089 id = ansi_assopname (MINUS_EXPR);
9090 break;
9091
9092 case CPP_MULT_EQ:
9093 id = ansi_assopname (MULT_EXPR);
9094 break;
9095
9096 case CPP_DIV_EQ:
9097 id = ansi_assopname (TRUNC_DIV_EXPR);
9098 break;
9099
9100 case CPP_MOD_EQ:
9101 id = ansi_assopname (TRUNC_MOD_EXPR);
9102 break;
9103
9104 case CPP_XOR_EQ:
9105 id = ansi_assopname (BIT_XOR_EXPR);
9106 break;
9107
9108 case CPP_AND_EQ:
9109 id = ansi_assopname (BIT_AND_EXPR);
9110 break;
9111
9112 case CPP_OR_EQ:
9113 id = ansi_assopname (BIT_IOR_EXPR);
9114 break;
9115
9116 case CPP_LSHIFT:
9117 id = ansi_opname (LSHIFT_EXPR);
9118 break;
9119
9120 case CPP_RSHIFT:
9121 id = ansi_opname (RSHIFT_EXPR);
9122 break;
9123
9124 case CPP_LSHIFT_EQ:
9125 id = ansi_assopname (LSHIFT_EXPR);
9126 break;
9127
9128 case CPP_RSHIFT_EQ:
9129 id = ansi_assopname (RSHIFT_EXPR);
9130 break;
9131
9132 case CPP_EQ_EQ:
9133 id = ansi_opname (EQ_EXPR);
9134 break;
9135
9136 case CPP_NOT_EQ:
9137 id = ansi_opname (NE_EXPR);
9138 break;
9139
9140 case CPP_LESS_EQ:
9141 id = ansi_opname (LE_EXPR);
9142 break;
9143
9144 case CPP_GREATER_EQ:
9145 id = ansi_opname (GE_EXPR);
9146 break;
9147
9148 case CPP_AND_AND:
9149 id = ansi_opname (TRUTH_ANDIF_EXPR);
9150 break;
9151
9152 case CPP_OR_OR:
9153 id = ansi_opname (TRUTH_ORIF_EXPR);
9154 break;
9155
9156 case CPP_PLUS_PLUS:
9157 id = ansi_opname (POSTINCREMENT_EXPR);
9158 break;
9159
9160 case CPP_MINUS_MINUS:
9161 id = ansi_opname (PREDECREMENT_EXPR);
9162 break;
9163
9164 case CPP_COMMA:
9165 id = ansi_opname (COMPOUND_EXPR);
9166 break;
9167
9168 case CPP_DEREF_STAR:
9169 id = ansi_opname (MEMBER_REF);
9170 break;
9171
9172 case CPP_DEREF:
9173 id = ansi_opname (COMPONENT_REF);
9174 break;
9175
9176 case CPP_OPEN_PAREN:
9177 /* Consume the `('. */
9178 cp_lexer_consume_token (parser->lexer);
9179 /* Look for the matching `)'. */
9180 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9181 return ansi_opname (CALL_EXPR);
9182
9183 case CPP_OPEN_SQUARE:
9184 /* Consume the `['. */
9185 cp_lexer_consume_token (parser->lexer);
9186 /* Look for the matching `]'. */
9187 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9188 return ansi_opname (ARRAY_REF);
9189
9190 default:
9191 /* Anything else is an error. */
9192 break;
9193 }
9194
9195 /* If we have selected an identifier, we need to consume the
9196 operator token. */
9197 if (id)
9198 cp_lexer_consume_token (parser->lexer);
9199 /* Otherwise, no valid operator name was present. */
9200 else
9201 {
9202 cp_parser_error (parser, "expected operator");
9203 id = error_mark_node;
9204 }
9205
9206 return id;
9207 }
9208
9209 /* Parse a template-declaration.
9210
9211 template-declaration:
9212 export [opt] template < template-parameter-list > declaration
9213
9214 If MEMBER_P is TRUE, this template-declaration occurs within a
9215 class-specifier.
9216
9217 The grammar rule given by the standard isn't correct. What
9218 is really meant is:
9219
9220 template-declaration:
9221 export [opt] template-parameter-list-seq
9222 decl-specifier-seq [opt] init-declarator [opt] ;
9223 export [opt] template-parameter-list-seq
9224 function-definition
9225
9226 template-parameter-list-seq:
9227 template-parameter-list-seq [opt]
9228 template < template-parameter-list > */
9229
9230 static void
9231 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9232 {
9233 /* Check for `export'. */
9234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9235 {
9236 /* Consume the `export' token. */
9237 cp_lexer_consume_token (parser->lexer);
9238 /* Warn that we do not support `export'. */
9239 warning (0, "keyword %<export%> not implemented, and will be ignored");
9240 }
9241
9242 cp_parser_template_declaration_after_export (parser, member_p);
9243 }
9244
9245 /* Parse a template-parameter-list.
9246
9247 template-parameter-list:
9248 template-parameter
9249 template-parameter-list , template-parameter
9250
9251 Returns a TREE_LIST. Each node represents a template parameter.
9252 The nodes are connected via their TREE_CHAINs. */
9253
9254 static tree
9255 cp_parser_template_parameter_list (cp_parser* parser)
9256 {
9257 tree parameter_list = NULL_TREE;
9258
9259 begin_template_parm_list ();
9260 while (true)
9261 {
9262 tree parameter;
9263 cp_token *token;
9264 bool is_non_type;
9265 bool is_parameter_pack;
9266
9267 /* Parse the template-parameter. */
9268 parameter = cp_parser_template_parameter (parser,
9269 &is_non_type,
9270 &is_parameter_pack);
9271 /* Add it to the list. */
9272 if (parameter != error_mark_node)
9273 parameter_list = process_template_parm (parameter_list,
9274 parameter,
9275 is_non_type,
9276 is_parameter_pack);
9277 else
9278 {
9279 tree err_parm = build_tree_list (parameter, parameter);
9280 TREE_VALUE (err_parm) = error_mark_node;
9281 parameter_list = chainon (parameter_list, err_parm);
9282 }
9283
9284 /* Peek at the next token. */
9285 token = cp_lexer_peek_token (parser->lexer);
9286 /* If it's not a `,', we're done. */
9287 if (token->type != CPP_COMMA)
9288 break;
9289 /* Otherwise, consume the `,' token. */
9290 cp_lexer_consume_token (parser->lexer);
9291 }
9292
9293 return end_template_parm_list (parameter_list);
9294 }
9295
9296 /* Parse a template-parameter.
9297
9298 template-parameter:
9299 type-parameter
9300 parameter-declaration
9301
9302 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9303 the parameter. The TREE_PURPOSE is the default value, if any.
9304 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9305 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9306 set to true iff this parameter is a parameter pack. */
9307
9308 static tree
9309 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9310 bool *is_parameter_pack)
9311 {
9312 cp_token *token;
9313 cp_parameter_declarator *parameter_declarator;
9314 tree parm;
9315
9316 /* Assume it is a type parameter or a template parameter. */
9317 *is_non_type = false;
9318 /* Assume it not a parameter pack. */
9319 *is_parameter_pack = false;
9320 /* Peek at the next token. */
9321 token = cp_lexer_peek_token (parser->lexer);
9322 /* If it is `class' or `template', we have a type-parameter. */
9323 if (token->keyword == RID_TEMPLATE)
9324 return cp_parser_type_parameter (parser, is_parameter_pack);
9325 /* If it is `class' or `typename' we do not know yet whether it is a
9326 type parameter or a non-type parameter. Consider:
9327
9328 template <typename T, typename T::X X> ...
9329
9330 or:
9331
9332 template <class C, class D*> ...
9333
9334 Here, the first parameter is a type parameter, and the second is
9335 a non-type parameter. We can tell by looking at the token after
9336 the identifier -- if it is a `,', `=', or `>' then we have a type
9337 parameter. */
9338 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9339 {
9340 /* Peek at the token after `class' or `typename'. */
9341 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9342 /* If it's an ellipsis, we have a template type parameter
9343 pack. */
9344 if (token->type == CPP_ELLIPSIS)
9345 return cp_parser_type_parameter (parser, is_parameter_pack);
9346 /* If it's an identifier, skip it. */
9347 if (token->type == CPP_NAME)
9348 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9349 /* Now, see if the token looks like the end of a template
9350 parameter. */
9351 if (token->type == CPP_COMMA
9352 || token->type == CPP_EQ
9353 || token->type == CPP_GREATER)
9354 return cp_parser_type_parameter (parser, is_parameter_pack);
9355 }
9356
9357 /* Otherwise, it is a non-type parameter.
9358
9359 [temp.param]
9360
9361 When parsing a default template-argument for a non-type
9362 template-parameter, the first non-nested `>' is taken as the end
9363 of the template parameter-list rather than a greater-than
9364 operator. */
9365 *is_non_type = true;
9366 parameter_declarator
9367 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9368 /*parenthesized_p=*/NULL);
9369
9370 /* If the parameter declaration is marked as a parameter pack, set
9371 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9372 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9373 grokdeclarator. */
9374 if (parameter_declarator
9375 && parameter_declarator->declarator
9376 && parameter_declarator->declarator->parameter_pack_p)
9377 {
9378 *is_parameter_pack = true;
9379 parameter_declarator->declarator->parameter_pack_p = false;
9380 }
9381
9382 /* If the next token is an ellipsis, and we don't already have it
9383 marked as a parameter pack, then we have a parameter pack (that
9384 has no declarator); */
9385 if (!*is_parameter_pack
9386 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9387 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9388 {
9389 /* Consume the `...'. */
9390 cp_lexer_consume_token (parser->lexer);
9391 maybe_warn_variadic_templates ();
9392
9393 *is_parameter_pack = true;
9394 }
9395
9396 parm = grokdeclarator (parameter_declarator->declarator,
9397 &parameter_declarator->decl_specifiers,
9398 PARM, /*initialized=*/0,
9399 /*attrlist=*/NULL);
9400 if (parm == error_mark_node)
9401 return error_mark_node;
9402
9403 return build_tree_list (parameter_declarator->default_argument, parm);
9404 }
9405
9406 /* Parse a type-parameter.
9407
9408 type-parameter:
9409 class identifier [opt]
9410 class identifier [opt] = type-id
9411 typename identifier [opt]
9412 typename identifier [opt] = type-id
9413 template < template-parameter-list > class identifier [opt]
9414 template < template-parameter-list > class identifier [opt]
9415 = id-expression
9416
9417 GNU Extension (variadic templates):
9418
9419 type-parameter:
9420 class ... identifier [opt]
9421 typename ... identifier [opt]
9422
9423 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9424 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9425 the declaration of the parameter.
9426
9427 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9428
9429 static tree
9430 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9431 {
9432 cp_token *token;
9433 tree parameter;
9434
9435 /* Look for a keyword to tell us what kind of parameter this is. */
9436 token = cp_parser_require (parser, CPP_KEYWORD,
9437 "`class', `typename', or `template'");
9438 if (!token)
9439 return error_mark_node;
9440
9441 switch (token->keyword)
9442 {
9443 case RID_CLASS:
9444 case RID_TYPENAME:
9445 {
9446 tree identifier;
9447 tree default_argument;
9448
9449 /* If the next token is an ellipsis, we have a template
9450 argument pack. */
9451 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9452 {
9453 /* Consume the `...' token. */
9454 cp_lexer_consume_token (parser->lexer);
9455 maybe_warn_variadic_templates ();
9456
9457 *is_parameter_pack = true;
9458 }
9459
9460 /* If the next token is an identifier, then it names the
9461 parameter. */
9462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9463 identifier = cp_parser_identifier (parser);
9464 else
9465 identifier = NULL_TREE;
9466
9467 /* Create the parameter. */
9468 parameter = finish_template_type_parm (class_type_node, identifier);
9469
9470 /* If the next token is an `=', we have a default argument. */
9471 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9472 {
9473 /* Consume the `=' token. */
9474 cp_lexer_consume_token (parser->lexer);
9475 /* Parse the default-argument. */
9476 push_deferring_access_checks (dk_no_deferred);
9477 default_argument = cp_parser_type_id (parser);
9478
9479 /* Template parameter packs cannot have default
9480 arguments. */
9481 if (*is_parameter_pack)
9482 {
9483 if (identifier)
9484 error ("template parameter pack %qD cannot have a default argument",
9485 identifier);
9486 else
9487 error ("template parameter packs cannot have default arguments");
9488 default_argument = NULL_TREE;
9489 }
9490 pop_deferring_access_checks ();
9491 }
9492 else
9493 default_argument = NULL_TREE;
9494
9495 /* Create the combined representation of the parameter and the
9496 default argument. */
9497 parameter = build_tree_list (default_argument, parameter);
9498 }
9499 break;
9500
9501 case RID_TEMPLATE:
9502 {
9503 tree parameter_list;
9504 tree identifier;
9505 tree default_argument;
9506
9507 /* Look for the `<'. */
9508 cp_parser_require (parser, CPP_LESS, "`<'");
9509 /* Parse the template-parameter-list. */
9510 parameter_list = cp_parser_template_parameter_list (parser);
9511 /* Look for the `>'. */
9512 cp_parser_require (parser, CPP_GREATER, "`>'");
9513 /* Look for the `class' keyword. */
9514 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9515 /* If the next token is an ellipsis, we have a template
9516 argument pack. */
9517 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9518 {
9519 /* Consume the `...' token. */
9520 cp_lexer_consume_token (parser->lexer);
9521 maybe_warn_variadic_templates ();
9522
9523 *is_parameter_pack = true;
9524 }
9525 /* If the next token is an `=', then there is a
9526 default-argument. If the next token is a `>', we are at
9527 the end of the parameter-list. If the next token is a `,',
9528 then we are at the end of this parameter. */
9529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9530 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9531 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9532 {
9533 identifier = cp_parser_identifier (parser);
9534 /* Treat invalid names as if the parameter were nameless. */
9535 if (identifier == error_mark_node)
9536 identifier = NULL_TREE;
9537 }
9538 else
9539 identifier = NULL_TREE;
9540
9541 /* Create the template parameter. */
9542 parameter = finish_template_template_parm (class_type_node,
9543 identifier);
9544
9545 /* If the next token is an `=', then there is a
9546 default-argument. */
9547 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9548 {
9549 bool is_template;
9550
9551 /* Consume the `='. */
9552 cp_lexer_consume_token (parser->lexer);
9553 /* Parse the id-expression. */
9554 push_deferring_access_checks (dk_no_deferred);
9555 default_argument
9556 = cp_parser_id_expression (parser,
9557 /*template_keyword_p=*/false,
9558 /*check_dependency_p=*/true,
9559 /*template_p=*/&is_template,
9560 /*declarator_p=*/false,
9561 /*optional_p=*/false);
9562 if (TREE_CODE (default_argument) == TYPE_DECL)
9563 /* If the id-expression was a template-id that refers to
9564 a template-class, we already have the declaration here,
9565 so no further lookup is needed. */
9566 ;
9567 else
9568 /* Look up the name. */
9569 default_argument
9570 = cp_parser_lookup_name (parser, default_argument,
9571 none_type,
9572 /*is_template=*/is_template,
9573 /*is_namespace=*/false,
9574 /*check_dependency=*/true,
9575 /*ambiguous_decls=*/NULL);
9576 /* See if the default argument is valid. */
9577 default_argument
9578 = check_template_template_default_arg (default_argument);
9579
9580 /* Template parameter packs cannot have default
9581 arguments. */
9582 if (*is_parameter_pack)
9583 {
9584 if (identifier)
9585 error ("template parameter pack %qD cannot have a default argument",
9586 identifier);
9587 else
9588 error ("template parameter packs cannot have default arguments");
9589 default_argument = NULL_TREE;
9590 }
9591 pop_deferring_access_checks ();
9592 }
9593 else
9594 default_argument = NULL_TREE;
9595
9596 /* Create the combined representation of the parameter and the
9597 default argument. */
9598 parameter = build_tree_list (default_argument, parameter);
9599 }
9600 break;
9601
9602 default:
9603 gcc_unreachable ();
9604 break;
9605 }
9606
9607 return parameter;
9608 }
9609
9610 /* Parse a template-id.
9611
9612 template-id:
9613 template-name < template-argument-list [opt] >
9614
9615 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9616 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9617 returned. Otherwise, if the template-name names a function, or set
9618 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9619 names a class, returns a TYPE_DECL for the specialization.
9620
9621 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9622 uninstantiated templates. */
9623
9624 static tree
9625 cp_parser_template_id (cp_parser *parser,
9626 bool template_keyword_p,
9627 bool check_dependency_p,
9628 bool is_declaration)
9629 {
9630 int i;
9631 tree template;
9632 tree arguments;
9633 tree template_id;
9634 cp_token_position start_of_id = 0;
9635 deferred_access_check *chk;
9636 VEC (deferred_access_check,gc) *access_check;
9637 cp_token *next_token, *next_token_2;
9638 bool is_identifier;
9639
9640 /* If the next token corresponds to a template-id, there is no need
9641 to reparse it. */
9642 next_token = cp_lexer_peek_token (parser->lexer);
9643 if (next_token->type == CPP_TEMPLATE_ID)
9644 {
9645 struct tree_check *check_value;
9646
9647 /* Get the stored value. */
9648 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9649 /* Perform any access checks that were deferred. */
9650 access_check = check_value->checks;
9651 if (access_check)
9652 {
9653 for (i = 0 ;
9654 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9655 ++i)
9656 {
9657 perform_or_defer_access_check (chk->binfo,
9658 chk->decl,
9659 chk->diag_decl);
9660 }
9661 }
9662 /* Return the stored value. */
9663 return check_value->value;
9664 }
9665
9666 /* Avoid performing name lookup if there is no possibility of
9667 finding a template-id. */
9668 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9669 || (next_token->type == CPP_NAME
9670 && !cp_parser_nth_token_starts_template_argument_list_p
9671 (parser, 2)))
9672 {
9673 cp_parser_error (parser, "expected template-id");
9674 return error_mark_node;
9675 }
9676
9677 /* Remember where the template-id starts. */
9678 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9679 start_of_id = cp_lexer_token_position (parser->lexer, false);
9680
9681 push_deferring_access_checks (dk_deferred);
9682
9683 /* Parse the template-name. */
9684 is_identifier = false;
9685 template = cp_parser_template_name (parser, template_keyword_p,
9686 check_dependency_p,
9687 is_declaration,
9688 &is_identifier);
9689 if (template == error_mark_node || is_identifier)
9690 {
9691 pop_deferring_access_checks ();
9692 return template;
9693 }
9694
9695 /* If we find the sequence `[:' after a template-name, it's probably
9696 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9697 parse correctly the argument list. */
9698 next_token = cp_lexer_peek_token (parser->lexer);
9699 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9700 if (next_token->type == CPP_OPEN_SQUARE
9701 && next_token->flags & DIGRAPH
9702 && next_token_2->type == CPP_COLON
9703 && !(next_token_2->flags & PREV_WHITE))
9704 {
9705 cp_parser_parse_tentatively (parser);
9706 /* Change `:' into `::'. */
9707 next_token_2->type = CPP_SCOPE;
9708 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9709 CPP_LESS. */
9710 cp_lexer_consume_token (parser->lexer);
9711 /* Parse the arguments. */
9712 arguments = cp_parser_enclosed_template_argument_list (parser);
9713 if (!cp_parser_parse_definitely (parser))
9714 {
9715 /* If we couldn't parse an argument list, then we revert our changes
9716 and return simply an error. Maybe this is not a template-id
9717 after all. */
9718 next_token_2->type = CPP_COLON;
9719 cp_parser_error (parser, "expected %<<%>");
9720 pop_deferring_access_checks ();
9721 return error_mark_node;
9722 }
9723 /* Otherwise, emit an error about the invalid digraph, but continue
9724 parsing because we got our argument list. */
9725 pedwarn ("%<<::%> cannot begin a template-argument list");
9726 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9727 "between %<<%> and %<::%>");
9728 if (!flag_permissive)
9729 {
9730 static bool hint;
9731 if (!hint)
9732 {
9733 inform ("(if you use -fpermissive G++ will accept your code)");
9734 hint = true;
9735 }
9736 }
9737 }
9738 else
9739 {
9740 /* Look for the `<' that starts the template-argument-list. */
9741 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9742 {
9743 pop_deferring_access_checks ();
9744 return error_mark_node;
9745 }
9746 /* Parse the arguments. */
9747 arguments = cp_parser_enclosed_template_argument_list (parser);
9748 }
9749
9750 /* Build a representation of the specialization. */
9751 if (TREE_CODE (template) == IDENTIFIER_NODE)
9752 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9753 else if (DECL_CLASS_TEMPLATE_P (template)
9754 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9755 {
9756 bool entering_scope;
9757 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9758 template (rather than some instantiation thereof) only if
9759 is not nested within some other construct. For example, in
9760 "template <typename T> void f(T) { A<T>::", A<T> is just an
9761 instantiation of A. */
9762 entering_scope = (template_parm_scope_p ()
9763 && cp_lexer_next_token_is (parser->lexer,
9764 CPP_SCOPE));
9765 template_id
9766 = finish_template_type (template, arguments, entering_scope);
9767 }
9768 else
9769 {
9770 /* If it's not a class-template or a template-template, it should be
9771 a function-template. */
9772 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9773 || TREE_CODE (template) == OVERLOAD
9774 || BASELINK_P (template)));
9775
9776 template_id = lookup_template_function (template, arguments);
9777 }
9778
9779 /* If parsing tentatively, replace the sequence of tokens that makes
9780 up the template-id with a CPP_TEMPLATE_ID token. That way,
9781 should we re-parse the token stream, we will not have to repeat
9782 the effort required to do the parse, nor will we issue duplicate
9783 error messages about problems during instantiation of the
9784 template. */
9785 if (start_of_id)
9786 {
9787 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9788
9789 /* Reset the contents of the START_OF_ID token. */
9790 token->type = CPP_TEMPLATE_ID;
9791 /* Retrieve any deferred checks. Do not pop this access checks yet
9792 so the memory will not be reclaimed during token replacing below. */
9793 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9794 token->u.tree_check_value->value = template_id;
9795 token->u.tree_check_value->checks = get_deferred_access_checks ();
9796 token->keyword = RID_MAX;
9797
9798 /* Purge all subsequent tokens. */
9799 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9800
9801 /* ??? Can we actually assume that, if template_id ==
9802 error_mark_node, we will have issued a diagnostic to the
9803 user, as opposed to simply marking the tentative parse as
9804 failed? */
9805 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9806 error ("parse error in template argument list");
9807 }
9808
9809 pop_deferring_access_checks ();
9810 return template_id;
9811 }
9812
9813 /* Parse a template-name.
9814
9815 template-name:
9816 identifier
9817
9818 The standard should actually say:
9819
9820 template-name:
9821 identifier
9822 operator-function-id
9823
9824 A defect report has been filed about this issue.
9825
9826 A conversion-function-id cannot be a template name because they cannot
9827 be part of a template-id. In fact, looking at this code:
9828
9829 a.operator K<int>()
9830
9831 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9832 It is impossible to call a templated conversion-function-id with an
9833 explicit argument list, since the only allowed template parameter is
9834 the type to which it is converting.
9835
9836 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9837 `template' keyword, in a construction like:
9838
9839 T::template f<3>()
9840
9841 In that case `f' is taken to be a template-name, even though there
9842 is no way of knowing for sure.
9843
9844 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9845 name refers to a set of overloaded functions, at least one of which
9846 is a template, or an IDENTIFIER_NODE with the name of the template,
9847 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9848 names are looked up inside uninstantiated templates. */
9849
9850 static tree
9851 cp_parser_template_name (cp_parser* parser,
9852 bool template_keyword_p,
9853 bool check_dependency_p,
9854 bool is_declaration,
9855 bool *is_identifier)
9856 {
9857 tree identifier;
9858 tree decl;
9859 tree fns;
9860
9861 /* If the next token is `operator', then we have either an
9862 operator-function-id or a conversion-function-id. */
9863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9864 {
9865 /* We don't know whether we're looking at an
9866 operator-function-id or a conversion-function-id. */
9867 cp_parser_parse_tentatively (parser);
9868 /* Try an operator-function-id. */
9869 identifier = cp_parser_operator_function_id (parser);
9870 /* If that didn't work, try a conversion-function-id. */
9871 if (!cp_parser_parse_definitely (parser))
9872 {
9873 cp_parser_error (parser, "expected template-name");
9874 return error_mark_node;
9875 }
9876 }
9877 /* Look for the identifier. */
9878 else
9879 identifier = cp_parser_identifier (parser);
9880
9881 /* If we didn't find an identifier, we don't have a template-id. */
9882 if (identifier == error_mark_node)
9883 return error_mark_node;
9884
9885 /* If the name immediately followed the `template' keyword, then it
9886 is a template-name. However, if the next token is not `<', then
9887 we do not treat it as a template-name, since it is not being used
9888 as part of a template-id. This enables us to handle constructs
9889 like:
9890
9891 template <typename T> struct S { S(); };
9892 template <typename T> S<T>::S();
9893
9894 correctly. We would treat `S' as a template -- if it were `S<T>'
9895 -- but we do not if there is no `<'. */
9896
9897 if (processing_template_decl
9898 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9899 {
9900 /* In a declaration, in a dependent context, we pretend that the
9901 "template" keyword was present in order to improve error
9902 recovery. For example, given:
9903
9904 template <typename T> void f(T::X<int>);
9905
9906 we want to treat "X<int>" as a template-id. */
9907 if (is_declaration
9908 && !template_keyword_p
9909 && parser->scope && TYPE_P (parser->scope)
9910 && check_dependency_p
9911 && dependent_type_p (parser->scope)
9912 /* Do not do this for dtors (or ctors), since they never
9913 need the template keyword before their name. */
9914 && !constructor_name_p (identifier, parser->scope))
9915 {
9916 cp_token_position start = 0;
9917
9918 /* Explain what went wrong. */
9919 error ("non-template %qD used as template", identifier);
9920 inform ("use %<%T::template %D%> to indicate that it is a template",
9921 parser->scope, identifier);
9922 /* If parsing tentatively, find the location of the "<" token. */
9923 if (cp_parser_simulate_error (parser))
9924 start = cp_lexer_token_position (parser->lexer, true);
9925 /* Parse the template arguments so that we can issue error
9926 messages about them. */
9927 cp_lexer_consume_token (parser->lexer);
9928 cp_parser_enclosed_template_argument_list (parser);
9929 /* Skip tokens until we find a good place from which to
9930 continue parsing. */
9931 cp_parser_skip_to_closing_parenthesis (parser,
9932 /*recovering=*/true,
9933 /*or_comma=*/true,
9934 /*consume_paren=*/false);
9935 /* If parsing tentatively, permanently remove the
9936 template argument list. That will prevent duplicate
9937 error messages from being issued about the missing
9938 "template" keyword. */
9939 if (start)
9940 cp_lexer_purge_tokens_after (parser->lexer, start);
9941 if (is_identifier)
9942 *is_identifier = true;
9943 return identifier;
9944 }
9945
9946 /* If the "template" keyword is present, then there is generally
9947 no point in doing name-lookup, so we just return IDENTIFIER.
9948 But, if the qualifying scope is non-dependent then we can
9949 (and must) do name-lookup normally. */
9950 if (template_keyword_p
9951 && (!parser->scope
9952 || (TYPE_P (parser->scope)
9953 && dependent_type_p (parser->scope))))
9954 return identifier;
9955 }
9956
9957 /* Look up the name. */
9958 decl = cp_parser_lookup_name (parser, identifier,
9959 none_type,
9960 /*is_template=*/false,
9961 /*is_namespace=*/false,
9962 check_dependency_p,
9963 /*ambiguous_decls=*/NULL);
9964 decl = maybe_get_template_decl_from_type_decl (decl);
9965
9966 /* If DECL is a template, then the name was a template-name. */
9967 if (TREE_CODE (decl) == TEMPLATE_DECL)
9968 ;
9969 else
9970 {
9971 tree fn = NULL_TREE;
9972
9973 /* The standard does not explicitly indicate whether a name that
9974 names a set of overloaded declarations, some of which are
9975 templates, is a template-name. However, such a name should
9976 be a template-name; otherwise, there is no way to form a
9977 template-id for the overloaded templates. */
9978 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9979 if (TREE_CODE (fns) == OVERLOAD)
9980 for (fn = fns; fn; fn = OVL_NEXT (fn))
9981 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9982 break;
9983
9984 if (!fn)
9985 {
9986 /* The name does not name a template. */
9987 cp_parser_error (parser, "expected template-name");
9988 return error_mark_node;
9989 }
9990 }
9991
9992 /* If DECL is dependent, and refers to a function, then just return
9993 its name; we will look it up again during template instantiation. */
9994 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9995 {
9996 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9997 if (TYPE_P (scope) && dependent_type_p (scope))
9998 return identifier;
9999 }
10000
10001 return decl;
10002 }
10003
10004 /* Parse a template-argument-list.
10005
10006 template-argument-list:
10007 template-argument ... [opt]
10008 template-argument-list , template-argument ... [opt]
10009
10010 Returns a TREE_VEC containing the arguments. */
10011
10012 static tree
10013 cp_parser_template_argument_list (cp_parser* parser)
10014 {
10015 tree fixed_args[10];
10016 unsigned n_args = 0;
10017 unsigned alloced = 10;
10018 tree *arg_ary = fixed_args;
10019 tree vec;
10020 bool saved_in_template_argument_list_p;
10021 bool saved_ice_p;
10022 bool saved_non_ice_p;
10023
10024 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10025 parser->in_template_argument_list_p = true;
10026 /* Even if the template-id appears in an integral
10027 constant-expression, the contents of the argument list do
10028 not. */
10029 saved_ice_p = parser->integral_constant_expression_p;
10030 parser->integral_constant_expression_p = false;
10031 saved_non_ice_p = parser->non_integral_constant_expression_p;
10032 parser->non_integral_constant_expression_p = false;
10033 /* Parse the arguments. */
10034 do
10035 {
10036 tree argument;
10037
10038 if (n_args)
10039 /* Consume the comma. */
10040 cp_lexer_consume_token (parser->lexer);
10041
10042 /* Parse the template-argument. */
10043 argument = cp_parser_template_argument (parser);
10044
10045 /* If the next token is an ellipsis, we're expanding a template
10046 argument pack. */
10047 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10048 {
10049 /* Consume the `...' token. */
10050 cp_lexer_consume_token (parser->lexer);
10051
10052 /* Make the argument into a TYPE_PACK_EXPANSION or
10053 EXPR_PACK_EXPANSION. */
10054 argument = make_pack_expansion (argument);
10055 }
10056
10057 if (n_args == alloced)
10058 {
10059 alloced *= 2;
10060
10061 if (arg_ary == fixed_args)
10062 {
10063 arg_ary = XNEWVEC (tree, alloced);
10064 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10065 }
10066 else
10067 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10068 }
10069 arg_ary[n_args++] = argument;
10070 }
10071 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10072
10073 vec = make_tree_vec (n_args);
10074
10075 while (n_args--)
10076 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10077
10078 if (arg_ary != fixed_args)
10079 free (arg_ary);
10080 parser->non_integral_constant_expression_p = saved_non_ice_p;
10081 parser->integral_constant_expression_p = saved_ice_p;
10082 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10083 return vec;
10084 }
10085
10086 /* Parse a template-argument.
10087
10088 template-argument:
10089 assignment-expression
10090 type-id
10091 id-expression
10092
10093 The representation is that of an assignment-expression, type-id, or
10094 id-expression -- except that the qualified id-expression is
10095 evaluated, so that the value returned is either a DECL or an
10096 OVERLOAD.
10097
10098 Although the standard says "assignment-expression", it forbids
10099 throw-expressions or assignments in the template argument.
10100 Therefore, we use "conditional-expression" instead. */
10101
10102 static tree
10103 cp_parser_template_argument (cp_parser* parser)
10104 {
10105 tree argument;
10106 bool template_p;
10107 bool address_p;
10108 bool maybe_type_id = false;
10109 cp_token *token;
10110 cp_id_kind idk;
10111
10112 /* There's really no way to know what we're looking at, so we just
10113 try each alternative in order.
10114
10115 [temp.arg]
10116
10117 In a template-argument, an ambiguity between a type-id and an
10118 expression is resolved to a type-id, regardless of the form of
10119 the corresponding template-parameter.
10120
10121 Therefore, we try a type-id first. */
10122 cp_parser_parse_tentatively (parser);
10123 argument = cp_parser_type_id (parser);
10124 /* If there was no error parsing the type-id but the next token is a '>>',
10125 we probably found a typo for '> >'. But there are type-id which are
10126 also valid expressions. For instance:
10127
10128 struct X { int operator >> (int); };
10129 template <int V> struct Foo {};
10130 Foo<X () >> 5> r;
10131
10132 Here 'X()' is a valid type-id of a function type, but the user just
10133 wanted to write the expression "X() >> 5". Thus, we remember that we
10134 found a valid type-id, but we still try to parse the argument as an
10135 expression to see what happens. */
10136 if (!cp_parser_error_occurred (parser)
10137 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10138 {
10139 maybe_type_id = true;
10140 cp_parser_abort_tentative_parse (parser);
10141 }
10142 else
10143 {
10144 /* If the next token isn't a `,' or a `>', then this argument wasn't
10145 really finished. This means that the argument is not a valid
10146 type-id. */
10147 if (!cp_parser_next_token_ends_template_argument_p (parser))
10148 cp_parser_error (parser, "expected template-argument");
10149 /* If that worked, we're done. */
10150 if (cp_parser_parse_definitely (parser))
10151 return argument;
10152 }
10153 /* We're still not sure what the argument will be. */
10154 cp_parser_parse_tentatively (parser);
10155 /* Try a template. */
10156 argument = cp_parser_id_expression (parser,
10157 /*template_keyword_p=*/false,
10158 /*check_dependency_p=*/true,
10159 &template_p,
10160 /*declarator_p=*/false,
10161 /*optional_p=*/false);
10162 /* If the next token isn't a `,' or a `>', then this argument wasn't
10163 really finished. */
10164 if (!cp_parser_next_token_ends_template_argument_p (parser))
10165 cp_parser_error (parser, "expected template-argument");
10166 if (!cp_parser_error_occurred (parser))
10167 {
10168 /* Figure out what is being referred to. If the id-expression
10169 was for a class template specialization, then we will have a
10170 TYPE_DECL at this point. There is no need to do name lookup
10171 at this point in that case. */
10172 if (TREE_CODE (argument) != TYPE_DECL)
10173 argument = cp_parser_lookup_name (parser, argument,
10174 none_type,
10175 /*is_template=*/template_p,
10176 /*is_namespace=*/false,
10177 /*check_dependency=*/true,
10178 /*ambiguous_decls=*/NULL);
10179 if (TREE_CODE (argument) != TEMPLATE_DECL
10180 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10181 cp_parser_error (parser, "expected template-name");
10182 }
10183 if (cp_parser_parse_definitely (parser))
10184 return argument;
10185 /* It must be a non-type argument. There permitted cases are given
10186 in [temp.arg.nontype]:
10187
10188 -- an integral constant-expression of integral or enumeration
10189 type; or
10190
10191 -- the name of a non-type template-parameter; or
10192
10193 -- the name of an object or function with external linkage...
10194
10195 -- the address of an object or function with external linkage...
10196
10197 -- a pointer to member... */
10198 /* Look for a non-type template parameter. */
10199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10200 {
10201 cp_parser_parse_tentatively (parser);
10202 argument = cp_parser_primary_expression (parser,
10203 /*adress_p=*/false,
10204 /*cast_p=*/false,
10205 /*template_arg_p=*/true,
10206 &idk);
10207 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10208 || !cp_parser_next_token_ends_template_argument_p (parser))
10209 cp_parser_simulate_error (parser);
10210 if (cp_parser_parse_definitely (parser))
10211 return argument;
10212 }
10213
10214 /* If the next token is "&", the argument must be the address of an
10215 object or function with external linkage. */
10216 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10217 if (address_p)
10218 cp_lexer_consume_token (parser->lexer);
10219 /* See if we might have an id-expression. */
10220 token = cp_lexer_peek_token (parser->lexer);
10221 if (token->type == CPP_NAME
10222 || token->keyword == RID_OPERATOR
10223 || token->type == CPP_SCOPE
10224 || token->type == CPP_TEMPLATE_ID
10225 || token->type == CPP_NESTED_NAME_SPECIFIER)
10226 {
10227 cp_parser_parse_tentatively (parser);
10228 argument = cp_parser_primary_expression (parser,
10229 address_p,
10230 /*cast_p=*/false,
10231 /*template_arg_p=*/true,
10232 &idk);
10233 if (cp_parser_error_occurred (parser)
10234 || !cp_parser_next_token_ends_template_argument_p (parser))
10235 cp_parser_abort_tentative_parse (parser);
10236 else
10237 {
10238 if (TREE_CODE (argument) == INDIRECT_REF)
10239 {
10240 gcc_assert (REFERENCE_REF_P (argument));
10241 argument = TREE_OPERAND (argument, 0);
10242 }
10243
10244 if (TREE_CODE (argument) == VAR_DECL)
10245 {
10246 /* A variable without external linkage might still be a
10247 valid constant-expression, so no error is issued here
10248 if the external-linkage check fails. */
10249 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10250 cp_parser_simulate_error (parser);
10251 }
10252 else if (is_overloaded_fn (argument))
10253 /* All overloaded functions are allowed; if the external
10254 linkage test does not pass, an error will be issued
10255 later. */
10256 ;
10257 else if (address_p
10258 && (TREE_CODE (argument) == OFFSET_REF
10259 || TREE_CODE (argument) == SCOPE_REF))
10260 /* A pointer-to-member. */
10261 ;
10262 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10263 ;
10264 else
10265 cp_parser_simulate_error (parser);
10266
10267 if (cp_parser_parse_definitely (parser))
10268 {
10269 if (address_p)
10270 argument = build_x_unary_op (ADDR_EXPR, argument);
10271 return argument;
10272 }
10273 }
10274 }
10275 /* If the argument started with "&", there are no other valid
10276 alternatives at this point. */
10277 if (address_p)
10278 {
10279 cp_parser_error (parser, "invalid non-type template argument");
10280 return error_mark_node;
10281 }
10282
10283 /* If the argument wasn't successfully parsed as a type-id followed
10284 by '>>', the argument can only be a constant expression now.
10285 Otherwise, we try parsing the constant-expression tentatively,
10286 because the argument could really be a type-id. */
10287 if (maybe_type_id)
10288 cp_parser_parse_tentatively (parser);
10289 argument = cp_parser_constant_expression (parser,
10290 /*allow_non_constant_p=*/false,
10291 /*non_constant_p=*/NULL);
10292 argument = fold_non_dependent_expr (argument);
10293 if (!maybe_type_id)
10294 return argument;
10295 if (!cp_parser_next_token_ends_template_argument_p (parser))
10296 cp_parser_error (parser, "expected template-argument");
10297 if (cp_parser_parse_definitely (parser))
10298 return argument;
10299 /* We did our best to parse the argument as a non type-id, but that
10300 was the only alternative that matched (albeit with a '>' after
10301 it). We can assume it's just a typo from the user, and a
10302 diagnostic will then be issued. */
10303 return cp_parser_type_id (parser);
10304 }
10305
10306 /* Parse an explicit-instantiation.
10307
10308 explicit-instantiation:
10309 template declaration
10310
10311 Although the standard says `declaration', what it really means is:
10312
10313 explicit-instantiation:
10314 template decl-specifier-seq [opt] declarator [opt] ;
10315
10316 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10317 supposed to be allowed. A defect report has been filed about this
10318 issue.
10319
10320 GNU Extension:
10321
10322 explicit-instantiation:
10323 storage-class-specifier template
10324 decl-specifier-seq [opt] declarator [opt] ;
10325 function-specifier template
10326 decl-specifier-seq [opt] declarator [opt] ; */
10327
10328 static void
10329 cp_parser_explicit_instantiation (cp_parser* parser)
10330 {
10331 int declares_class_or_enum;
10332 cp_decl_specifier_seq decl_specifiers;
10333 tree extension_specifier = NULL_TREE;
10334
10335 /* Look for an (optional) storage-class-specifier or
10336 function-specifier. */
10337 if (cp_parser_allow_gnu_extensions_p (parser))
10338 {
10339 extension_specifier
10340 = cp_parser_storage_class_specifier_opt (parser);
10341 if (!extension_specifier)
10342 extension_specifier
10343 = cp_parser_function_specifier_opt (parser,
10344 /*decl_specs=*/NULL);
10345 }
10346
10347 /* Look for the `template' keyword. */
10348 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10349 /* Let the front end know that we are processing an explicit
10350 instantiation. */
10351 begin_explicit_instantiation ();
10352 /* [temp.explicit] says that we are supposed to ignore access
10353 control while processing explicit instantiation directives. */
10354 push_deferring_access_checks (dk_no_check);
10355 /* Parse a decl-specifier-seq. */
10356 cp_parser_decl_specifier_seq (parser,
10357 CP_PARSER_FLAGS_OPTIONAL,
10358 &decl_specifiers,
10359 &declares_class_or_enum);
10360 /* If there was exactly one decl-specifier, and it declared a class,
10361 and there's no declarator, then we have an explicit type
10362 instantiation. */
10363 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10364 {
10365 tree type;
10366
10367 type = check_tag_decl (&decl_specifiers);
10368 /* Turn access control back on for names used during
10369 template instantiation. */
10370 pop_deferring_access_checks ();
10371 if (type)
10372 do_type_instantiation (type, extension_specifier,
10373 /*complain=*/tf_error);
10374 }
10375 else
10376 {
10377 cp_declarator *declarator;
10378 tree decl;
10379
10380 /* Parse the declarator. */
10381 declarator
10382 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10383 /*ctor_dtor_or_conv_p=*/NULL,
10384 /*parenthesized_p=*/NULL,
10385 /*member_p=*/false);
10386 if (declares_class_or_enum & 2)
10387 cp_parser_check_for_definition_in_return_type (declarator,
10388 decl_specifiers.type);
10389 if (declarator != cp_error_declarator)
10390 {
10391 decl = grokdeclarator (declarator, &decl_specifiers,
10392 NORMAL, 0, &decl_specifiers.attributes);
10393 /* Turn access control back on for names used during
10394 template instantiation. */
10395 pop_deferring_access_checks ();
10396 /* Do the explicit instantiation. */
10397 do_decl_instantiation (decl, extension_specifier);
10398 }
10399 else
10400 {
10401 pop_deferring_access_checks ();
10402 /* Skip the body of the explicit instantiation. */
10403 cp_parser_skip_to_end_of_statement (parser);
10404 }
10405 }
10406 /* We're done with the instantiation. */
10407 end_explicit_instantiation ();
10408
10409 cp_parser_consume_semicolon_at_end_of_statement (parser);
10410 }
10411
10412 /* Parse an explicit-specialization.
10413
10414 explicit-specialization:
10415 template < > declaration
10416
10417 Although the standard says `declaration', what it really means is:
10418
10419 explicit-specialization:
10420 template <> decl-specifier [opt] init-declarator [opt] ;
10421 template <> function-definition
10422 template <> explicit-specialization
10423 template <> template-declaration */
10424
10425 static void
10426 cp_parser_explicit_specialization (cp_parser* parser)
10427 {
10428 bool need_lang_pop;
10429 /* Look for the `template' keyword. */
10430 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10431 /* Look for the `<'. */
10432 cp_parser_require (parser, CPP_LESS, "`<'");
10433 /* Look for the `>'. */
10434 cp_parser_require (parser, CPP_GREATER, "`>'");
10435 /* We have processed another parameter list. */
10436 ++parser->num_template_parameter_lists;
10437 /* [temp]
10438
10439 A template ... explicit specialization ... shall not have C
10440 linkage. */
10441 if (current_lang_name == lang_name_c)
10442 {
10443 error ("template specialization with C linkage");
10444 /* Give it C++ linkage to avoid confusing other parts of the
10445 front end. */
10446 push_lang_context (lang_name_cplusplus);
10447 need_lang_pop = true;
10448 }
10449 else
10450 need_lang_pop = false;
10451 /* Let the front end know that we are beginning a specialization. */
10452 if (!begin_specialization ())
10453 {
10454 end_specialization ();
10455 cp_parser_skip_to_end_of_block_or_statement (parser);
10456 return;
10457 }
10458
10459 /* If the next keyword is `template', we need to figure out whether
10460 or not we're looking a template-declaration. */
10461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10462 {
10463 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10464 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10465 cp_parser_template_declaration_after_export (parser,
10466 /*member_p=*/false);
10467 else
10468 cp_parser_explicit_specialization (parser);
10469 }
10470 else
10471 /* Parse the dependent declaration. */
10472 cp_parser_single_declaration (parser,
10473 /*checks=*/NULL,
10474 /*member_p=*/false,
10475 /*explicit_specialization_p=*/true,
10476 /*friend_p=*/NULL);
10477 /* We're done with the specialization. */
10478 end_specialization ();
10479 /* For the erroneous case of a template with C linkage, we pushed an
10480 implicit C++ linkage scope; exit that scope now. */
10481 if (need_lang_pop)
10482 pop_lang_context ();
10483 /* We're done with this parameter list. */
10484 --parser->num_template_parameter_lists;
10485 }
10486
10487 /* Parse a type-specifier.
10488
10489 type-specifier:
10490 simple-type-specifier
10491 class-specifier
10492 enum-specifier
10493 elaborated-type-specifier
10494 cv-qualifier
10495
10496 GNU Extension:
10497
10498 type-specifier:
10499 __complex__
10500
10501 Returns a representation of the type-specifier. For a
10502 class-specifier, enum-specifier, or elaborated-type-specifier, a
10503 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10504
10505 The parser flags FLAGS is used to control type-specifier parsing.
10506
10507 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10508 in a decl-specifier-seq.
10509
10510 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10511 class-specifier, enum-specifier, or elaborated-type-specifier, then
10512 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10513 if a type is declared; 2 if it is defined. Otherwise, it is set to
10514 zero.
10515
10516 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10517 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10518 is set to FALSE. */
10519
10520 static tree
10521 cp_parser_type_specifier (cp_parser* parser,
10522 cp_parser_flags flags,
10523 cp_decl_specifier_seq *decl_specs,
10524 bool is_declaration,
10525 int* declares_class_or_enum,
10526 bool* is_cv_qualifier)
10527 {
10528 tree type_spec = NULL_TREE;
10529 cp_token *token;
10530 enum rid keyword;
10531 cp_decl_spec ds = ds_last;
10532
10533 /* Assume this type-specifier does not declare a new type. */
10534 if (declares_class_or_enum)
10535 *declares_class_or_enum = 0;
10536 /* And that it does not specify a cv-qualifier. */
10537 if (is_cv_qualifier)
10538 *is_cv_qualifier = false;
10539 /* Peek at the next token. */
10540 token = cp_lexer_peek_token (parser->lexer);
10541
10542 /* If we're looking at a keyword, we can use that to guide the
10543 production we choose. */
10544 keyword = token->keyword;
10545 switch (keyword)
10546 {
10547 case RID_ENUM:
10548 /* Look for the enum-specifier. */
10549 type_spec = cp_parser_enum_specifier (parser);
10550 /* If that worked, we're done. */
10551 if (type_spec)
10552 {
10553 if (declares_class_or_enum)
10554 *declares_class_or_enum = 2;
10555 if (decl_specs)
10556 cp_parser_set_decl_spec_type (decl_specs,
10557 type_spec,
10558 /*user_defined_p=*/true);
10559 return type_spec;
10560 }
10561 else
10562 goto elaborated_type_specifier;
10563
10564 /* Any of these indicate either a class-specifier, or an
10565 elaborated-type-specifier. */
10566 case RID_CLASS:
10567 case RID_STRUCT:
10568 case RID_UNION:
10569 /* Parse tentatively so that we can back up if we don't find a
10570 class-specifier. */
10571 cp_parser_parse_tentatively (parser);
10572 /* Look for the class-specifier. */
10573 type_spec = cp_parser_class_specifier (parser);
10574 /* If that worked, we're done. */
10575 if (cp_parser_parse_definitely (parser))
10576 {
10577 if (declares_class_or_enum)
10578 *declares_class_or_enum = 2;
10579 if (decl_specs)
10580 cp_parser_set_decl_spec_type (decl_specs,
10581 type_spec,
10582 /*user_defined_p=*/true);
10583 return type_spec;
10584 }
10585
10586 /* Fall through. */
10587 elaborated_type_specifier:
10588 /* We're declaring (not defining) a class or enum. */
10589 if (declares_class_or_enum)
10590 *declares_class_or_enum = 1;
10591
10592 /* Fall through. */
10593 case RID_TYPENAME:
10594 /* Look for an elaborated-type-specifier. */
10595 type_spec
10596 = (cp_parser_elaborated_type_specifier
10597 (parser,
10598 decl_specs && decl_specs->specs[(int) ds_friend],
10599 is_declaration));
10600 if (decl_specs)
10601 cp_parser_set_decl_spec_type (decl_specs,
10602 type_spec,
10603 /*user_defined_p=*/true);
10604 return type_spec;
10605
10606 case RID_CONST:
10607 ds = ds_const;
10608 if (is_cv_qualifier)
10609 *is_cv_qualifier = true;
10610 break;
10611
10612 case RID_VOLATILE:
10613 ds = ds_volatile;
10614 if (is_cv_qualifier)
10615 *is_cv_qualifier = true;
10616 break;
10617
10618 case RID_RESTRICT:
10619 ds = ds_restrict;
10620 if (is_cv_qualifier)
10621 *is_cv_qualifier = true;
10622 break;
10623
10624 case RID_COMPLEX:
10625 /* The `__complex__' keyword is a GNU extension. */
10626 ds = ds_complex;
10627 break;
10628
10629 default:
10630 break;
10631 }
10632
10633 /* Handle simple keywords. */
10634 if (ds != ds_last)
10635 {
10636 if (decl_specs)
10637 {
10638 ++decl_specs->specs[(int)ds];
10639 decl_specs->any_specifiers_p = true;
10640 }
10641 return cp_lexer_consume_token (parser->lexer)->u.value;
10642 }
10643
10644 /* If we do not already have a type-specifier, assume we are looking
10645 at a simple-type-specifier. */
10646 type_spec = cp_parser_simple_type_specifier (parser,
10647 decl_specs,
10648 flags);
10649
10650 /* If we didn't find a type-specifier, and a type-specifier was not
10651 optional in this context, issue an error message. */
10652 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10653 {
10654 cp_parser_error (parser, "expected type specifier");
10655 return error_mark_node;
10656 }
10657
10658 return type_spec;
10659 }
10660
10661 /* Parse a simple-type-specifier.
10662
10663 simple-type-specifier:
10664 :: [opt] nested-name-specifier [opt] type-name
10665 :: [opt] nested-name-specifier template template-id
10666 char
10667 wchar_t
10668 bool
10669 short
10670 int
10671 long
10672 signed
10673 unsigned
10674 float
10675 double
10676 void
10677
10678 C++0x Extension:
10679
10680 simple-type-specifier:
10681 decltype ( expression )
10682
10683 GNU Extension:
10684
10685 simple-type-specifier:
10686 __typeof__ unary-expression
10687 __typeof__ ( type-id )
10688
10689 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10690 appropriately updated. */
10691
10692 static tree
10693 cp_parser_simple_type_specifier (cp_parser* parser,
10694 cp_decl_specifier_seq *decl_specs,
10695 cp_parser_flags flags)
10696 {
10697 tree type = NULL_TREE;
10698 cp_token *token;
10699
10700 /* Peek at the next token. */
10701 token = cp_lexer_peek_token (parser->lexer);
10702
10703 /* If we're looking at a keyword, things are easy. */
10704 switch (token->keyword)
10705 {
10706 case RID_CHAR:
10707 if (decl_specs)
10708 decl_specs->explicit_char_p = true;
10709 type = char_type_node;
10710 break;
10711 case RID_WCHAR:
10712 type = wchar_type_node;
10713 break;
10714 case RID_BOOL:
10715 type = boolean_type_node;
10716 break;
10717 case RID_SHORT:
10718 if (decl_specs)
10719 ++decl_specs->specs[(int) ds_short];
10720 type = short_integer_type_node;
10721 break;
10722 case RID_INT:
10723 if (decl_specs)
10724 decl_specs->explicit_int_p = true;
10725 type = integer_type_node;
10726 break;
10727 case RID_LONG:
10728 if (decl_specs)
10729 ++decl_specs->specs[(int) ds_long];
10730 type = long_integer_type_node;
10731 break;
10732 case RID_SIGNED:
10733 if (decl_specs)
10734 ++decl_specs->specs[(int) ds_signed];
10735 type = integer_type_node;
10736 break;
10737 case RID_UNSIGNED:
10738 if (decl_specs)
10739 ++decl_specs->specs[(int) ds_unsigned];
10740 type = unsigned_type_node;
10741 break;
10742 case RID_FLOAT:
10743 type = float_type_node;
10744 break;
10745 case RID_DOUBLE:
10746 type = double_type_node;
10747 break;
10748 case RID_VOID:
10749 type = void_type_node;
10750 break;
10751
10752 case RID_DECLTYPE:
10753 /* Parse the `decltype' type. */
10754 type = cp_parser_decltype (parser);
10755
10756 if (decl_specs)
10757 cp_parser_set_decl_spec_type (decl_specs, type,
10758 /*user_defined_p=*/true);
10759
10760 return type;
10761
10762 case RID_TYPEOF:
10763 /* Consume the `typeof' token. */
10764 cp_lexer_consume_token (parser->lexer);
10765 /* Parse the operand to `typeof'. */
10766 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10767 /* If it is not already a TYPE, take its type. */
10768 if (!TYPE_P (type))
10769 type = finish_typeof (type);
10770
10771 if (decl_specs)
10772 cp_parser_set_decl_spec_type (decl_specs, type,
10773 /*user_defined_p=*/true);
10774
10775 return type;
10776
10777 default:
10778 break;
10779 }
10780
10781 /* If the type-specifier was for a built-in type, we're done. */
10782 if (type)
10783 {
10784 tree id;
10785
10786 /* Record the type. */
10787 if (decl_specs
10788 && (token->keyword != RID_SIGNED
10789 && token->keyword != RID_UNSIGNED
10790 && token->keyword != RID_SHORT
10791 && token->keyword != RID_LONG))
10792 cp_parser_set_decl_spec_type (decl_specs,
10793 type,
10794 /*user_defined=*/false);
10795 if (decl_specs)
10796 decl_specs->any_specifiers_p = true;
10797
10798 /* Consume the token. */
10799 id = cp_lexer_consume_token (parser->lexer)->u.value;
10800
10801 /* There is no valid C++ program where a non-template type is
10802 followed by a "<". That usually indicates that the user thought
10803 that the type was a template. */
10804 cp_parser_check_for_invalid_template_id (parser, type);
10805
10806 return TYPE_NAME (type);
10807 }
10808
10809 /* The type-specifier must be a user-defined type. */
10810 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10811 {
10812 bool qualified_p;
10813 bool global_p;
10814
10815 /* Don't gobble tokens or issue error messages if this is an
10816 optional type-specifier. */
10817 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10818 cp_parser_parse_tentatively (parser);
10819
10820 /* Look for the optional `::' operator. */
10821 global_p
10822 = (cp_parser_global_scope_opt (parser,
10823 /*current_scope_valid_p=*/false)
10824 != NULL_TREE);
10825 /* Look for the nested-name specifier. */
10826 qualified_p
10827 = (cp_parser_nested_name_specifier_opt (parser,
10828 /*typename_keyword_p=*/false,
10829 /*check_dependency_p=*/true,
10830 /*type_p=*/false,
10831 /*is_declaration=*/false)
10832 != NULL_TREE);
10833 /* If we have seen a nested-name-specifier, and the next token
10834 is `template', then we are using the template-id production. */
10835 if (parser->scope
10836 && cp_parser_optional_template_keyword (parser))
10837 {
10838 /* Look for the template-id. */
10839 type = cp_parser_template_id (parser,
10840 /*template_keyword_p=*/true,
10841 /*check_dependency_p=*/true,
10842 /*is_declaration=*/false);
10843 /* If the template-id did not name a type, we are out of
10844 luck. */
10845 if (TREE_CODE (type) != TYPE_DECL)
10846 {
10847 cp_parser_error (parser, "expected template-id for type");
10848 type = NULL_TREE;
10849 }
10850 }
10851 /* Otherwise, look for a type-name. */
10852 else
10853 type = cp_parser_type_name (parser);
10854 /* Keep track of all name-lookups performed in class scopes. */
10855 if (type
10856 && !global_p
10857 && !qualified_p
10858 && TREE_CODE (type) == TYPE_DECL
10859 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10860 maybe_note_name_used_in_class (DECL_NAME (type), type);
10861 /* If it didn't work out, we don't have a TYPE. */
10862 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10863 && !cp_parser_parse_definitely (parser))
10864 type = NULL_TREE;
10865 if (type && decl_specs)
10866 cp_parser_set_decl_spec_type (decl_specs, type,
10867 /*user_defined=*/true);
10868 }
10869
10870 /* If we didn't get a type-name, issue an error message. */
10871 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10872 {
10873 cp_parser_error (parser, "expected type-name");
10874 return error_mark_node;
10875 }
10876
10877 /* There is no valid C++ program where a non-template type is
10878 followed by a "<". That usually indicates that the user thought
10879 that the type was a template. */
10880 if (type && type != error_mark_node)
10881 {
10882 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10883 If it is, then the '<'...'>' enclose protocol names rather than
10884 template arguments, and so everything is fine. */
10885 if (c_dialect_objc ()
10886 && (objc_is_id (type) || objc_is_class_name (type)))
10887 {
10888 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10889 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10890
10891 /* Clobber the "unqualified" type previously entered into
10892 DECL_SPECS with the new, improved protocol-qualified version. */
10893 if (decl_specs)
10894 decl_specs->type = qual_type;
10895
10896 return qual_type;
10897 }
10898
10899 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10900 }
10901
10902 return type;
10903 }
10904
10905 /* Parse a type-name.
10906
10907 type-name:
10908 class-name
10909 enum-name
10910 typedef-name
10911
10912 enum-name:
10913 identifier
10914
10915 typedef-name:
10916 identifier
10917
10918 Returns a TYPE_DECL for the type. */
10919
10920 static tree
10921 cp_parser_type_name (cp_parser* parser)
10922 {
10923 tree type_decl;
10924 tree identifier;
10925
10926 /* We can't know yet whether it is a class-name or not. */
10927 cp_parser_parse_tentatively (parser);
10928 /* Try a class-name. */
10929 type_decl = cp_parser_class_name (parser,
10930 /*typename_keyword_p=*/false,
10931 /*template_keyword_p=*/false,
10932 none_type,
10933 /*check_dependency_p=*/true,
10934 /*class_head_p=*/false,
10935 /*is_declaration=*/false);
10936 /* If it's not a class-name, keep looking. */
10937 if (!cp_parser_parse_definitely (parser))
10938 {
10939 /* It must be a typedef-name or an enum-name. */
10940 identifier = cp_parser_identifier (parser);
10941 if (identifier == error_mark_node)
10942 return error_mark_node;
10943
10944 /* Look up the type-name. */
10945 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10946
10947 if (TREE_CODE (type_decl) != TYPE_DECL
10948 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10949 {
10950 /* See if this is an Objective-C type. */
10951 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10952 tree type = objc_get_protocol_qualified_type (identifier, protos);
10953 if (type)
10954 type_decl = TYPE_NAME (type);
10955 }
10956
10957 /* Issue an error if we did not find a type-name. */
10958 if (TREE_CODE (type_decl) != TYPE_DECL)
10959 {
10960 if (!cp_parser_simulate_error (parser))
10961 cp_parser_name_lookup_error (parser, identifier, type_decl,
10962 "is not a type");
10963 type_decl = error_mark_node;
10964 }
10965 /* Remember that the name was used in the definition of the
10966 current class so that we can check later to see if the
10967 meaning would have been different after the class was
10968 entirely defined. */
10969 else if (type_decl != error_mark_node
10970 && !parser->scope)
10971 maybe_note_name_used_in_class (identifier, type_decl);
10972 }
10973
10974 return type_decl;
10975 }
10976
10977
10978 /* Parse an elaborated-type-specifier. Note that the grammar given
10979 here incorporates the resolution to DR68.
10980
10981 elaborated-type-specifier:
10982 class-key :: [opt] nested-name-specifier [opt] identifier
10983 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10984 enum :: [opt] nested-name-specifier [opt] identifier
10985 typename :: [opt] nested-name-specifier identifier
10986 typename :: [opt] nested-name-specifier template [opt]
10987 template-id
10988
10989 GNU extension:
10990
10991 elaborated-type-specifier:
10992 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10993 class-key attributes :: [opt] nested-name-specifier [opt]
10994 template [opt] template-id
10995 enum attributes :: [opt] nested-name-specifier [opt] identifier
10996
10997 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10998 declared `friend'. If IS_DECLARATION is TRUE, then this
10999 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11000 something is being declared.
11001
11002 Returns the TYPE specified. */
11003
11004 static tree
11005 cp_parser_elaborated_type_specifier (cp_parser* parser,
11006 bool is_friend,
11007 bool is_declaration)
11008 {
11009 enum tag_types tag_type;
11010 tree identifier;
11011 tree type = NULL_TREE;
11012 tree attributes = NULL_TREE;
11013
11014 /* See if we're looking at the `enum' keyword. */
11015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11016 {
11017 /* Consume the `enum' token. */
11018 cp_lexer_consume_token (parser->lexer);
11019 /* Remember that it's an enumeration type. */
11020 tag_type = enum_type;
11021 /* Parse the attributes. */
11022 attributes = cp_parser_attributes_opt (parser);
11023 }
11024 /* Or, it might be `typename'. */
11025 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11026 RID_TYPENAME))
11027 {
11028 /* Consume the `typename' token. */
11029 cp_lexer_consume_token (parser->lexer);
11030 /* Remember that it's a `typename' type. */
11031 tag_type = typename_type;
11032 /* The `typename' keyword is only allowed in templates. */
11033 if (!processing_template_decl)
11034 pedwarn ("using %<typename%> outside of template");
11035 }
11036 /* Otherwise it must be a class-key. */
11037 else
11038 {
11039 tag_type = cp_parser_class_key (parser);
11040 if (tag_type == none_type)
11041 return error_mark_node;
11042 /* Parse the attributes. */
11043 attributes = cp_parser_attributes_opt (parser);
11044 }
11045
11046 /* Look for the `::' operator. */
11047 cp_parser_global_scope_opt (parser,
11048 /*current_scope_valid_p=*/false);
11049 /* Look for the nested-name-specifier. */
11050 if (tag_type == typename_type)
11051 {
11052 if (!cp_parser_nested_name_specifier (parser,
11053 /*typename_keyword_p=*/true,
11054 /*check_dependency_p=*/true,
11055 /*type_p=*/true,
11056 is_declaration))
11057 return error_mark_node;
11058 }
11059 else
11060 /* Even though `typename' is not present, the proposed resolution
11061 to Core Issue 180 says that in `class A<T>::B', `B' should be
11062 considered a type-name, even if `A<T>' is dependent. */
11063 cp_parser_nested_name_specifier_opt (parser,
11064 /*typename_keyword_p=*/true,
11065 /*check_dependency_p=*/true,
11066 /*type_p=*/true,
11067 is_declaration);
11068 /* For everything but enumeration types, consider a template-id.
11069 For an enumeration type, consider only a plain identifier. */
11070 if (tag_type != enum_type)
11071 {
11072 bool template_p = false;
11073 tree decl;
11074
11075 /* Allow the `template' keyword. */
11076 template_p = cp_parser_optional_template_keyword (parser);
11077 /* If we didn't see `template', we don't know if there's a
11078 template-id or not. */
11079 if (!template_p)
11080 cp_parser_parse_tentatively (parser);
11081 /* Parse the template-id. */
11082 decl = cp_parser_template_id (parser, template_p,
11083 /*check_dependency_p=*/true,
11084 is_declaration);
11085 /* If we didn't find a template-id, look for an ordinary
11086 identifier. */
11087 if (!template_p && !cp_parser_parse_definitely (parser))
11088 ;
11089 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11090 in effect, then we must assume that, upon instantiation, the
11091 template will correspond to a class. */
11092 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11093 && tag_type == typename_type)
11094 type = make_typename_type (parser->scope, decl,
11095 typename_type,
11096 /*complain=*/tf_error);
11097 else
11098 type = TREE_TYPE (decl);
11099 }
11100
11101 if (!type)
11102 {
11103 identifier = cp_parser_identifier (parser);
11104
11105 if (identifier == error_mark_node)
11106 {
11107 parser->scope = NULL_TREE;
11108 return error_mark_node;
11109 }
11110
11111 /* For a `typename', we needn't call xref_tag. */
11112 if (tag_type == typename_type
11113 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11114 return cp_parser_make_typename_type (parser, parser->scope,
11115 identifier);
11116 /* Look up a qualified name in the usual way. */
11117 if (parser->scope)
11118 {
11119 tree decl;
11120 tree ambiguous_decls;
11121
11122 decl = cp_parser_lookup_name (parser, identifier,
11123 tag_type,
11124 /*is_template=*/false,
11125 /*is_namespace=*/false,
11126 /*check_dependency=*/true,
11127 &ambiguous_decls);
11128
11129 /* If the lookup was ambiguous, an error will already have been
11130 issued. */
11131 if (ambiguous_decls)
11132 return error_mark_node;
11133
11134 /* If we are parsing friend declaration, DECL may be a
11135 TEMPLATE_DECL tree node here. However, we need to check
11136 whether this TEMPLATE_DECL results in valid code. Consider
11137 the following example:
11138
11139 namespace N {
11140 template <class T> class C {};
11141 }
11142 class X {
11143 template <class T> friend class N::C; // #1, valid code
11144 };
11145 template <class T> class Y {
11146 friend class N::C; // #2, invalid code
11147 };
11148
11149 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11150 name lookup of `N::C'. We see that friend declaration must
11151 be template for the code to be valid. Note that
11152 processing_template_decl does not work here since it is
11153 always 1 for the above two cases. */
11154
11155 decl = (cp_parser_maybe_treat_template_as_class
11156 (decl, /*tag_name_p=*/is_friend
11157 && parser->num_template_parameter_lists));
11158
11159 if (TREE_CODE (decl) != TYPE_DECL)
11160 {
11161 cp_parser_diagnose_invalid_type_name (parser,
11162 parser->scope,
11163 identifier);
11164 return error_mark_node;
11165 }
11166
11167 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11168 {
11169 bool allow_template = (parser->num_template_parameter_lists
11170 || DECL_SELF_REFERENCE_P (decl));
11171 type = check_elaborated_type_specifier (tag_type, decl,
11172 allow_template);
11173
11174 if (type == error_mark_node)
11175 return error_mark_node;
11176 }
11177
11178 /* Forward declarations of nested types, such as
11179
11180 class C1::C2;
11181 class C1::C2::C3;
11182
11183 are invalid unless all components preceding the final '::'
11184 are complete. If all enclosing types are complete, these
11185 declarations become merely pointless.
11186
11187 Invalid forward declarations of nested types are errors
11188 caught elsewhere in parsing. Those that are pointless arrive
11189 here. */
11190
11191 if (cp_parser_declares_only_class_p (parser)
11192 && !is_friend && !processing_explicit_instantiation)
11193 warning (0, "declaration %qD does not declare anything", decl);
11194
11195 type = TREE_TYPE (decl);
11196 }
11197 else
11198 {
11199 /* An elaborated-type-specifier sometimes introduces a new type and
11200 sometimes names an existing type. Normally, the rule is that it
11201 introduces a new type only if there is not an existing type of
11202 the same name already in scope. For example, given:
11203
11204 struct S {};
11205 void f() { struct S s; }
11206
11207 the `struct S' in the body of `f' is the same `struct S' as in
11208 the global scope; the existing definition is used. However, if
11209 there were no global declaration, this would introduce a new
11210 local class named `S'.
11211
11212 An exception to this rule applies to the following code:
11213
11214 namespace N { struct S; }
11215
11216 Here, the elaborated-type-specifier names a new type
11217 unconditionally; even if there is already an `S' in the
11218 containing scope this declaration names a new type.
11219 This exception only applies if the elaborated-type-specifier
11220 forms the complete declaration:
11221
11222 [class.name]
11223
11224 A declaration consisting solely of `class-key identifier ;' is
11225 either a redeclaration of the name in the current scope or a
11226 forward declaration of the identifier as a class name. It
11227 introduces the name into the current scope.
11228
11229 We are in this situation precisely when the next token is a `;'.
11230
11231 An exception to the exception is that a `friend' declaration does
11232 *not* name a new type; i.e., given:
11233
11234 struct S { friend struct T; };
11235
11236 `T' is not a new type in the scope of `S'.
11237
11238 Also, `new struct S' or `sizeof (struct S)' never results in the
11239 definition of a new type; a new type can only be declared in a
11240 declaration context. */
11241
11242 tag_scope ts;
11243 bool template_p;
11244
11245 if (is_friend)
11246 /* Friends have special name lookup rules. */
11247 ts = ts_within_enclosing_non_class;
11248 else if (is_declaration
11249 && cp_lexer_next_token_is (parser->lexer,
11250 CPP_SEMICOLON))
11251 /* This is a `class-key identifier ;' */
11252 ts = ts_current;
11253 else
11254 ts = ts_global;
11255
11256 template_p =
11257 (parser->num_template_parameter_lists
11258 && (cp_parser_next_token_starts_class_definition_p (parser)
11259 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11260 /* An unqualified name was used to reference this type, so
11261 there were no qualifying templates. */
11262 if (!cp_parser_check_template_parameters (parser,
11263 /*num_templates=*/0))
11264 return error_mark_node;
11265 type = xref_tag (tag_type, identifier, ts, template_p);
11266 }
11267 }
11268
11269 if (type == error_mark_node)
11270 return error_mark_node;
11271
11272 /* Allow attributes on forward declarations of classes. */
11273 if (attributes)
11274 {
11275 if (TREE_CODE (type) == TYPENAME_TYPE)
11276 warning (OPT_Wattributes,
11277 "attributes ignored on uninstantiated type");
11278 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11279 && ! processing_explicit_instantiation)
11280 warning (OPT_Wattributes,
11281 "attributes ignored on template instantiation");
11282 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11283 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11284 else
11285 warning (OPT_Wattributes,
11286 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11287 }
11288
11289 if (tag_type != enum_type)
11290 cp_parser_check_class_key (tag_type, type);
11291
11292 /* A "<" cannot follow an elaborated type specifier. If that
11293 happens, the user was probably trying to form a template-id. */
11294 cp_parser_check_for_invalid_template_id (parser, type);
11295
11296 return type;
11297 }
11298
11299 /* Parse an enum-specifier.
11300
11301 enum-specifier:
11302 enum identifier [opt] { enumerator-list [opt] }
11303
11304 GNU Extensions:
11305 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11306 attributes[opt]
11307
11308 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11309 if the token stream isn't an enum-specifier after all. */
11310
11311 static tree
11312 cp_parser_enum_specifier (cp_parser* parser)
11313 {
11314 tree identifier;
11315 tree type;
11316 tree attributes;
11317
11318 /* Parse tentatively so that we can back up if we don't find a
11319 enum-specifier. */
11320 cp_parser_parse_tentatively (parser);
11321
11322 /* Caller guarantees that the current token is 'enum', an identifier
11323 possibly follows, and the token after that is an opening brace.
11324 If we don't have an identifier, fabricate an anonymous name for
11325 the enumeration being defined. */
11326 cp_lexer_consume_token (parser->lexer);
11327
11328 attributes = cp_parser_attributes_opt (parser);
11329
11330 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11331 identifier = cp_parser_identifier (parser);
11332 else
11333 identifier = make_anon_name ();
11334
11335 /* Look for the `{' but don't consume it yet. */
11336 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11337 cp_parser_simulate_error (parser);
11338
11339 if (!cp_parser_parse_definitely (parser))
11340 return NULL_TREE;
11341
11342 /* Issue an error message if type-definitions are forbidden here. */
11343 if (!cp_parser_check_type_definition (parser))
11344 type = error_mark_node;
11345 else
11346 /* Create the new type. We do this before consuming the opening
11347 brace so the enum will be recorded as being on the line of its
11348 tag (or the 'enum' keyword, if there is no tag). */
11349 type = start_enum (identifier);
11350
11351 /* Consume the opening brace. */
11352 cp_lexer_consume_token (parser->lexer);
11353
11354 if (type == error_mark_node)
11355 {
11356 cp_parser_skip_to_end_of_block_or_statement (parser);
11357 return error_mark_node;
11358 }
11359
11360 /* If the next token is not '}', then there are some enumerators. */
11361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11362 cp_parser_enumerator_list (parser, type);
11363
11364 /* Consume the final '}'. */
11365 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11366
11367 /* Look for trailing attributes to apply to this enumeration, and
11368 apply them if appropriate. */
11369 if (cp_parser_allow_gnu_extensions_p (parser))
11370 {
11371 tree trailing_attr = cp_parser_attributes_opt (parser);
11372 cplus_decl_attributes (&type,
11373 trailing_attr,
11374 (int) ATTR_FLAG_TYPE_IN_PLACE);
11375 }
11376
11377 /* Finish up the enumeration. */
11378 finish_enum (type);
11379
11380 return type;
11381 }
11382
11383 /* Parse an enumerator-list. The enumerators all have the indicated
11384 TYPE.
11385
11386 enumerator-list:
11387 enumerator-definition
11388 enumerator-list , enumerator-definition */
11389
11390 static void
11391 cp_parser_enumerator_list (cp_parser* parser, tree type)
11392 {
11393 while (true)
11394 {
11395 /* Parse an enumerator-definition. */
11396 cp_parser_enumerator_definition (parser, type);
11397
11398 /* If the next token is not a ',', we've reached the end of
11399 the list. */
11400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11401 break;
11402 /* Otherwise, consume the `,' and keep going. */
11403 cp_lexer_consume_token (parser->lexer);
11404 /* If the next token is a `}', there is a trailing comma. */
11405 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11406 {
11407 if (pedantic && !in_system_header)
11408 pedwarn ("comma at end of enumerator list");
11409 break;
11410 }
11411 }
11412 }
11413
11414 /* Parse an enumerator-definition. The enumerator has the indicated
11415 TYPE.
11416
11417 enumerator-definition:
11418 enumerator
11419 enumerator = constant-expression
11420
11421 enumerator:
11422 identifier */
11423
11424 static void
11425 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11426 {
11427 tree identifier;
11428 tree value;
11429
11430 /* Look for the identifier. */
11431 identifier = cp_parser_identifier (parser);
11432 if (identifier == error_mark_node)
11433 return;
11434
11435 /* If the next token is an '=', then there is an explicit value. */
11436 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11437 {
11438 /* Consume the `=' token. */
11439 cp_lexer_consume_token (parser->lexer);
11440 /* Parse the value. */
11441 value = cp_parser_constant_expression (parser,
11442 /*allow_non_constant_p=*/false,
11443 NULL);
11444 }
11445 else
11446 value = NULL_TREE;
11447
11448 /* Create the enumerator. */
11449 build_enumerator (identifier, value, type);
11450 }
11451
11452 /* Parse a namespace-name.
11453
11454 namespace-name:
11455 original-namespace-name
11456 namespace-alias
11457
11458 Returns the NAMESPACE_DECL for the namespace. */
11459
11460 static tree
11461 cp_parser_namespace_name (cp_parser* parser)
11462 {
11463 tree identifier;
11464 tree namespace_decl;
11465
11466 /* Get the name of the namespace. */
11467 identifier = cp_parser_identifier (parser);
11468 if (identifier == error_mark_node)
11469 return error_mark_node;
11470
11471 /* Look up the identifier in the currently active scope. Look only
11472 for namespaces, due to:
11473
11474 [basic.lookup.udir]
11475
11476 When looking up a namespace-name in a using-directive or alias
11477 definition, only namespace names are considered.
11478
11479 And:
11480
11481 [basic.lookup.qual]
11482
11483 During the lookup of a name preceding the :: scope resolution
11484 operator, object, function, and enumerator names are ignored.
11485
11486 (Note that cp_parser_class_or_namespace_name only calls this
11487 function if the token after the name is the scope resolution
11488 operator.) */
11489 namespace_decl = cp_parser_lookup_name (parser, identifier,
11490 none_type,
11491 /*is_template=*/false,
11492 /*is_namespace=*/true,
11493 /*check_dependency=*/true,
11494 /*ambiguous_decls=*/NULL);
11495 /* If it's not a namespace, issue an error. */
11496 if (namespace_decl == error_mark_node
11497 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11498 {
11499 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11500 error ("%qD is not a namespace-name", identifier);
11501 cp_parser_error (parser, "expected namespace-name");
11502 namespace_decl = error_mark_node;
11503 }
11504
11505 return namespace_decl;
11506 }
11507
11508 /* Parse a namespace-definition.
11509
11510 namespace-definition:
11511 named-namespace-definition
11512 unnamed-namespace-definition
11513
11514 named-namespace-definition:
11515 original-namespace-definition
11516 extension-namespace-definition
11517
11518 original-namespace-definition:
11519 namespace identifier { namespace-body }
11520
11521 extension-namespace-definition:
11522 namespace original-namespace-name { namespace-body }
11523
11524 unnamed-namespace-definition:
11525 namespace { namespace-body } */
11526
11527 static void
11528 cp_parser_namespace_definition (cp_parser* parser)
11529 {
11530 tree identifier, attribs;
11531 bool has_visibility;
11532
11533 /* Look for the `namespace' keyword. */
11534 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11535
11536 /* Get the name of the namespace. We do not attempt to distinguish
11537 between an original-namespace-definition and an
11538 extension-namespace-definition at this point. The semantic
11539 analysis routines are responsible for that. */
11540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11541 identifier = cp_parser_identifier (parser);
11542 else
11543 identifier = NULL_TREE;
11544
11545 /* Parse any specified attributes. */
11546 attribs = cp_parser_attributes_opt (parser);
11547
11548 /* Look for the `{' to start the namespace. */
11549 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11550 /* Start the namespace. */
11551 push_namespace (identifier);
11552
11553 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11554
11555 /* Parse the body of the namespace. */
11556 cp_parser_namespace_body (parser);
11557
11558 #ifdef HANDLE_PRAGMA_VISIBILITY
11559 if (has_visibility)
11560 pop_visibility ();
11561 #endif
11562
11563 /* Finish the namespace. */
11564 pop_namespace ();
11565 /* Look for the final `}'. */
11566 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11567 }
11568
11569 /* Parse a namespace-body.
11570
11571 namespace-body:
11572 declaration-seq [opt] */
11573
11574 static void
11575 cp_parser_namespace_body (cp_parser* parser)
11576 {
11577 cp_parser_declaration_seq_opt (parser);
11578 }
11579
11580 /* Parse a namespace-alias-definition.
11581
11582 namespace-alias-definition:
11583 namespace identifier = qualified-namespace-specifier ; */
11584
11585 static void
11586 cp_parser_namespace_alias_definition (cp_parser* parser)
11587 {
11588 tree identifier;
11589 tree namespace_specifier;
11590
11591 /* Look for the `namespace' keyword. */
11592 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11593 /* Look for the identifier. */
11594 identifier = cp_parser_identifier (parser);
11595 if (identifier == error_mark_node)
11596 return;
11597 /* Look for the `=' token. */
11598 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11599 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11600 {
11601 error ("%<namespace%> definition is not allowed here");
11602 /* Skip the definition. */
11603 cp_lexer_consume_token (parser->lexer);
11604 if (cp_parser_skip_to_closing_brace (parser))
11605 cp_lexer_consume_token (parser->lexer);
11606 return;
11607 }
11608 cp_parser_require (parser, CPP_EQ, "`='");
11609 /* Look for the qualified-namespace-specifier. */
11610 namespace_specifier
11611 = cp_parser_qualified_namespace_specifier (parser);
11612 /* Look for the `;' token. */
11613 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11614
11615 /* Register the alias in the symbol table. */
11616 do_namespace_alias (identifier, namespace_specifier);
11617 }
11618
11619 /* Parse a qualified-namespace-specifier.
11620
11621 qualified-namespace-specifier:
11622 :: [opt] nested-name-specifier [opt] namespace-name
11623
11624 Returns a NAMESPACE_DECL corresponding to the specified
11625 namespace. */
11626
11627 static tree
11628 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11629 {
11630 /* Look for the optional `::'. */
11631 cp_parser_global_scope_opt (parser,
11632 /*current_scope_valid_p=*/false);
11633
11634 /* Look for the optional nested-name-specifier. */
11635 cp_parser_nested_name_specifier_opt (parser,
11636 /*typename_keyword_p=*/false,
11637 /*check_dependency_p=*/true,
11638 /*type_p=*/false,
11639 /*is_declaration=*/true);
11640
11641 return cp_parser_namespace_name (parser);
11642 }
11643
11644 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11645 access declaration.
11646
11647 using-declaration:
11648 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11649 using :: unqualified-id ;
11650
11651 access-declaration:
11652 qualified-id ;
11653
11654 */
11655
11656 static bool
11657 cp_parser_using_declaration (cp_parser* parser,
11658 bool access_declaration_p)
11659 {
11660 cp_token *token;
11661 bool typename_p = false;
11662 bool global_scope_p;
11663 tree decl;
11664 tree identifier;
11665 tree qscope;
11666
11667 if (access_declaration_p)
11668 cp_parser_parse_tentatively (parser);
11669 else
11670 {
11671 /* Look for the `using' keyword. */
11672 cp_parser_require_keyword (parser, RID_USING, "`using'");
11673
11674 /* Peek at the next token. */
11675 token = cp_lexer_peek_token (parser->lexer);
11676 /* See if it's `typename'. */
11677 if (token->keyword == RID_TYPENAME)
11678 {
11679 /* Remember that we've seen it. */
11680 typename_p = true;
11681 /* Consume the `typename' token. */
11682 cp_lexer_consume_token (parser->lexer);
11683 }
11684 }
11685
11686 /* Look for the optional global scope qualification. */
11687 global_scope_p
11688 = (cp_parser_global_scope_opt (parser,
11689 /*current_scope_valid_p=*/false)
11690 != NULL_TREE);
11691
11692 /* If we saw `typename', or didn't see `::', then there must be a
11693 nested-name-specifier present. */
11694 if (typename_p || !global_scope_p)
11695 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11696 /*check_dependency_p=*/true,
11697 /*type_p=*/false,
11698 /*is_declaration=*/true);
11699 /* Otherwise, we could be in either of the two productions. In that
11700 case, treat the nested-name-specifier as optional. */
11701 else
11702 qscope = cp_parser_nested_name_specifier_opt (parser,
11703 /*typename_keyword_p=*/false,
11704 /*check_dependency_p=*/true,
11705 /*type_p=*/false,
11706 /*is_declaration=*/true);
11707 if (!qscope)
11708 qscope = global_namespace;
11709
11710 if (access_declaration_p && cp_parser_error_occurred (parser))
11711 /* Something has already gone wrong; there's no need to parse
11712 further. Since an error has occurred, the return value of
11713 cp_parser_parse_definitely will be false, as required. */
11714 return cp_parser_parse_definitely (parser);
11715
11716 /* Parse the unqualified-id. */
11717 identifier = cp_parser_unqualified_id (parser,
11718 /*template_keyword_p=*/false,
11719 /*check_dependency_p=*/true,
11720 /*declarator_p=*/true,
11721 /*optional_p=*/false);
11722
11723 if (access_declaration_p)
11724 {
11725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11726 cp_parser_simulate_error (parser);
11727 if (!cp_parser_parse_definitely (parser))
11728 return false;
11729 }
11730
11731 /* The function we call to handle a using-declaration is different
11732 depending on what scope we are in. */
11733 if (qscope == error_mark_node || identifier == error_mark_node)
11734 ;
11735 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11736 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11737 /* [namespace.udecl]
11738
11739 A using declaration shall not name a template-id. */
11740 error ("a template-id may not appear in a using-declaration");
11741 else
11742 {
11743 if (at_class_scope_p ())
11744 {
11745 /* Create the USING_DECL. */
11746 decl = do_class_using_decl (parser->scope, identifier);
11747 /* Add it to the list of members in this class. */
11748 finish_member_declaration (decl);
11749 }
11750 else
11751 {
11752 decl = cp_parser_lookup_name_simple (parser, identifier);
11753 if (decl == error_mark_node)
11754 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11755 else if (!at_namespace_scope_p ())
11756 do_local_using_decl (decl, qscope, identifier);
11757 else
11758 do_toplevel_using_decl (decl, qscope, identifier);
11759 }
11760 }
11761
11762 /* Look for the final `;'. */
11763 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11764
11765 return true;
11766 }
11767
11768 /* Parse a using-directive.
11769
11770 using-directive:
11771 using namespace :: [opt] nested-name-specifier [opt]
11772 namespace-name ; */
11773
11774 static void
11775 cp_parser_using_directive (cp_parser* parser)
11776 {
11777 tree namespace_decl;
11778 tree attribs;
11779
11780 /* Look for the `using' keyword. */
11781 cp_parser_require_keyword (parser, RID_USING, "`using'");
11782 /* And the `namespace' keyword. */
11783 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11784 /* Look for the optional `::' operator. */
11785 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11786 /* And the optional nested-name-specifier. */
11787 cp_parser_nested_name_specifier_opt (parser,
11788 /*typename_keyword_p=*/false,
11789 /*check_dependency_p=*/true,
11790 /*type_p=*/false,
11791 /*is_declaration=*/true);
11792 /* Get the namespace being used. */
11793 namespace_decl = cp_parser_namespace_name (parser);
11794 /* And any specified attributes. */
11795 attribs = cp_parser_attributes_opt (parser);
11796 /* Update the symbol table. */
11797 parse_using_directive (namespace_decl, attribs);
11798 /* Look for the final `;'. */
11799 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11800 }
11801
11802 /* Parse an asm-definition.
11803
11804 asm-definition:
11805 asm ( string-literal ) ;
11806
11807 GNU Extension:
11808
11809 asm-definition:
11810 asm volatile [opt] ( string-literal ) ;
11811 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11812 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11813 : asm-operand-list [opt] ) ;
11814 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11815 : asm-operand-list [opt]
11816 : asm-operand-list [opt] ) ; */
11817
11818 static void
11819 cp_parser_asm_definition (cp_parser* parser)
11820 {
11821 tree string;
11822 tree outputs = NULL_TREE;
11823 tree inputs = NULL_TREE;
11824 tree clobbers = NULL_TREE;
11825 tree asm_stmt;
11826 bool volatile_p = false;
11827 bool extended_p = false;
11828 bool invalid_inputs_p = false;
11829 bool invalid_outputs_p = false;
11830
11831 /* Look for the `asm' keyword. */
11832 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11833 /* See if the next token is `volatile'. */
11834 if (cp_parser_allow_gnu_extensions_p (parser)
11835 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11836 {
11837 /* Remember that we saw the `volatile' keyword. */
11838 volatile_p = true;
11839 /* Consume the token. */
11840 cp_lexer_consume_token (parser->lexer);
11841 }
11842 /* Look for the opening `('. */
11843 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11844 return;
11845 /* Look for the string. */
11846 string = cp_parser_string_literal (parser, false, false);
11847 if (string == error_mark_node)
11848 {
11849 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11850 /*consume_paren=*/true);
11851 return;
11852 }
11853
11854 /* If we're allowing GNU extensions, check for the extended assembly
11855 syntax. Unfortunately, the `:' tokens need not be separated by
11856 a space in C, and so, for compatibility, we tolerate that here
11857 too. Doing that means that we have to treat the `::' operator as
11858 two `:' tokens. */
11859 if (cp_parser_allow_gnu_extensions_p (parser)
11860 && parser->in_function_body
11861 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11862 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11863 {
11864 bool inputs_p = false;
11865 bool clobbers_p = false;
11866
11867 /* The extended syntax was used. */
11868 extended_p = true;
11869
11870 /* Look for outputs. */
11871 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11872 {
11873 /* Consume the `:'. */
11874 cp_lexer_consume_token (parser->lexer);
11875 /* Parse the output-operands. */
11876 if (cp_lexer_next_token_is_not (parser->lexer,
11877 CPP_COLON)
11878 && cp_lexer_next_token_is_not (parser->lexer,
11879 CPP_SCOPE)
11880 && cp_lexer_next_token_is_not (parser->lexer,
11881 CPP_CLOSE_PAREN))
11882 outputs = cp_parser_asm_operand_list (parser);
11883
11884 if (outputs == error_mark_node)
11885 invalid_outputs_p = true;
11886 }
11887 /* If the next token is `::', there are no outputs, and the
11888 next token is the beginning of the inputs. */
11889 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11890 /* The inputs are coming next. */
11891 inputs_p = true;
11892
11893 /* Look for inputs. */
11894 if (inputs_p
11895 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11896 {
11897 /* Consume the `:' or `::'. */
11898 cp_lexer_consume_token (parser->lexer);
11899 /* Parse the output-operands. */
11900 if (cp_lexer_next_token_is_not (parser->lexer,
11901 CPP_COLON)
11902 && cp_lexer_next_token_is_not (parser->lexer,
11903 CPP_CLOSE_PAREN))
11904 inputs = cp_parser_asm_operand_list (parser);
11905
11906 if (inputs == error_mark_node)
11907 invalid_inputs_p = true;
11908 }
11909 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11910 /* The clobbers are coming next. */
11911 clobbers_p = true;
11912
11913 /* Look for clobbers. */
11914 if (clobbers_p
11915 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11916 {
11917 /* Consume the `:' or `::'. */
11918 cp_lexer_consume_token (parser->lexer);
11919 /* Parse the clobbers. */
11920 if (cp_lexer_next_token_is_not (parser->lexer,
11921 CPP_CLOSE_PAREN))
11922 clobbers = cp_parser_asm_clobber_list (parser);
11923 }
11924 }
11925 /* Look for the closing `)'. */
11926 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11927 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11928 /*consume_paren=*/true);
11929 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11930
11931 if (!invalid_inputs_p && !invalid_outputs_p)
11932 {
11933 /* Create the ASM_EXPR. */
11934 if (parser->in_function_body)
11935 {
11936 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11937 inputs, clobbers);
11938 /* If the extended syntax was not used, mark the ASM_EXPR. */
11939 if (!extended_p)
11940 {
11941 tree temp = asm_stmt;
11942 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11943 temp = TREE_OPERAND (temp, 0);
11944
11945 ASM_INPUT_P (temp) = 1;
11946 }
11947 }
11948 else
11949 cgraph_add_asm_node (string);
11950 }
11951 }
11952
11953 /* Declarators [gram.dcl.decl] */
11954
11955 /* Parse an init-declarator.
11956
11957 init-declarator:
11958 declarator initializer [opt]
11959
11960 GNU Extension:
11961
11962 init-declarator:
11963 declarator asm-specification [opt] attributes [opt] initializer [opt]
11964
11965 function-definition:
11966 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11967 function-body
11968 decl-specifier-seq [opt] declarator function-try-block
11969
11970 GNU Extension:
11971
11972 function-definition:
11973 __extension__ function-definition
11974
11975 The DECL_SPECIFIERS apply to this declarator. Returns a
11976 representation of the entity declared. If MEMBER_P is TRUE, then
11977 this declarator appears in a class scope. The new DECL created by
11978 this declarator is returned.
11979
11980 The CHECKS are access checks that should be performed once we know
11981 what entity is being declared (and, therefore, what classes have
11982 befriended it).
11983
11984 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11985 for a function-definition here as well. If the declarator is a
11986 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11987 be TRUE upon return. By that point, the function-definition will
11988 have been completely parsed.
11989
11990 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11991 is FALSE. */
11992
11993 static tree
11994 cp_parser_init_declarator (cp_parser* parser,
11995 cp_decl_specifier_seq *decl_specifiers,
11996 VEC (deferred_access_check,gc)* checks,
11997 bool function_definition_allowed_p,
11998 bool member_p,
11999 int declares_class_or_enum,
12000 bool* function_definition_p)
12001 {
12002 cp_token *token;
12003 cp_declarator *declarator;
12004 tree prefix_attributes;
12005 tree attributes;
12006 tree asm_specification;
12007 tree initializer;
12008 tree decl = NULL_TREE;
12009 tree scope;
12010 bool is_initialized;
12011 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12012 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12013 "(...)". */
12014 enum cpp_ttype initialization_kind;
12015 bool is_parenthesized_init = false;
12016 bool is_non_constant_init;
12017 int ctor_dtor_or_conv_p;
12018 bool friend_p;
12019 tree pushed_scope = NULL;
12020
12021 /* Gather the attributes that were provided with the
12022 decl-specifiers. */
12023 prefix_attributes = decl_specifiers->attributes;
12024
12025 /* Assume that this is not the declarator for a function
12026 definition. */
12027 if (function_definition_p)
12028 *function_definition_p = false;
12029
12030 /* Defer access checks while parsing the declarator; we cannot know
12031 what names are accessible until we know what is being
12032 declared. */
12033 resume_deferring_access_checks ();
12034
12035 /* Parse the declarator. */
12036 declarator
12037 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12038 &ctor_dtor_or_conv_p,
12039 /*parenthesized_p=*/NULL,
12040 /*member_p=*/false);
12041 /* Gather up the deferred checks. */
12042 stop_deferring_access_checks ();
12043
12044 /* If the DECLARATOR was erroneous, there's no need to go
12045 further. */
12046 if (declarator == cp_error_declarator)
12047 return error_mark_node;
12048
12049 /* Check that the number of template-parameter-lists is OK. */
12050 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12051 return error_mark_node;
12052
12053 if (declares_class_or_enum & 2)
12054 cp_parser_check_for_definition_in_return_type (declarator,
12055 decl_specifiers->type);
12056
12057 /* Figure out what scope the entity declared by the DECLARATOR is
12058 located in. `grokdeclarator' sometimes changes the scope, so
12059 we compute it now. */
12060 scope = get_scope_of_declarator (declarator);
12061
12062 /* If we're allowing GNU extensions, look for an asm-specification
12063 and attributes. */
12064 if (cp_parser_allow_gnu_extensions_p (parser))
12065 {
12066 /* Look for an asm-specification. */
12067 asm_specification = cp_parser_asm_specification_opt (parser);
12068 /* And attributes. */
12069 attributes = cp_parser_attributes_opt (parser);
12070 }
12071 else
12072 {
12073 asm_specification = NULL_TREE;
12074 attributes = NULL_TREE;
12075 }
12076
12077 /* Peek at the next token. */
12078 token = cp_lexer_peek_token (parser->lexer);
12079 /* Check to see if the token indicates the start of a
12080 function-definition. */
12081 if (cp_parser_token_starts_function_definition_p (token))
12082 {
12083 if (!function_definition_allowed_p)
12084 {
12085 /* If a function-definition should not appear here, issue an
12086 error message. */
12087 cp_parser_error (parser,
12088 "a function-definition is not allowed here");
12089 return error_mark_node;
12090 }
12091 else
12092 {
12093 /* Neither attributes nor an asm-specification are allowed
12094 on a function-definition. */
12095 if (asm_specification)
12096 error ("an asm-specification is not allowed on a function-definition");
12097 if (attributes)
12098 error ("attributes are not allowed on a function-definition");
12099 /* This is a function-definition. */
12100 *function_definition_p = true;
12101
12102 /* Parse the function definition. */
12103 if (member_p)
12104 decl = cp_parser_save_member_function_body (parser,
12105 decl_specifiers,
12106 declarator,
12107 prefix_attributes);
12108 else
12109 decl
12110 = (cp_parser_function_definition_from_specifiers_and_declarator
12111 (parser, decl_specifiers, prefix_attributes, declarator));
12112
12113 return decl;
12114 }
12115 }
12116
12117 /* [dcl.dcl]
12118
12119 Only in function declarations for constructors, destructors, and
12120 type conversions can the decl-specifier-seq be omitted.
12121
12122 We explicitly postpone this check past the point where we handle
12123 function-definitions because we tolerate function-definitions
12124 that are missing their return types in some modes. */
12125 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12126 {
12127 cp_parser_error (parser,
12128 "expected constructor, destructor, or type conversion");
12129 return error_mark_node;
12130 }
12131
12132 /* An `=' or an `(' indicates an initializer. */
12133 if (token->type == CPP_EQ
12134 || token->type == CPP_OPEN_PAREN)
12135 {
12136 is_initialized = true;
12137 initialization_kind = token->type;
12138 }
12139 else
12140 {
12141 /* If the init-declarator isn't initialized and isn't followed by a
12142 `,' or `;', it's not a valid init-declarator. */
12143 if (token->type != CPP_COMMA
12144 && token->type != CPP_SEMICOLON)
12145 {
12146 cp_parser_error (parser, "expected initializer");
12147 return error_mark_node;
12148 }
12149 is_initialized = false;
12150 initialization_kind = CPP_EOF;
12151 }
12152
12153 /* Because start_decl has side-effects, we should only call it if we
12154 know we're going ahead. By this point, we know that we cannot
12155 possibly be looking at any other construct. */
12156 cp_parser_commit_to_tentative_parse (parser);
12157
12158 /* If the decl specifiers were bad, issue an error now that we're
12159 sure this was intended to be a declarator. Then continue
12160 declaring the variable(s), as int, to try to cut down on further
12161 errors. */
12162 if (decl_specifiers->any_specifiers_p
12163 && decl_specifiers->type == error_mark_node)
12164 {
12165 cp_parser_error (parser, "invalid type in declaration");
12166 decl_specifiers->type = integer_type_node;
12167 }
12168
12169 /* Check to see whether or not this declaration is a friend. */
12170 friend_p = cp_parser_friend_p (decl_specifiers);
12171
12172 /* Enter the newly declared entry in the symbol table. If we're
12173 processing a declaration in a class-specifier, we wait until
12174 after processing the initializer. */
12175 if (!member_p)
12176 {
12177 if (parser->in_unbraced_linkage_specification_p)
12178 decl_specifiers->storage_class = sc_extern;
12179 decl = start_decl (declarator, decl_specifiers,
12180 is_initialized, attributes, prefix_attributes,
12181 &pushed_scope);
12182 }
12183 else if (scope)
12184 /* Enter the SCOPE. That way unqualified names appearing in the
12185 initializer will be looked up in SCOPE. */
12186 pushed_scope = push_scope (scope);
12187
12188 /* Perform deferred access control checks, now that we know in which
12189 SCOPE the declared entity resides. */
12190 if (!member_p && decl)
12191 {
12192 tree saved_current_function_decl = NULL_TREE;
12193
12194 /* If the entity being declared is a function, pretend that we
12195 are in its scope. If it is a `friend', it may have access to
12196 things that would not otherwise be accessible. */
12197 if (TREE_CODE (decl) == FUNCTION_DECL)
12198 {
12199 saved_current_function_decl = current_function_decl;
12200 current_function_decl = decl;
12201 }
12202
12203 /* Perform access checks for template parameters. */
12204 cp_parser_perform_template_parameter_access_checks (checks);
12205
12206 /* Perform the access control checks for the declarator and the
12207 the decl-specifiers. */
12208 perform_deferred_access_checks ();
12209
12210 /* Restore the saved value. */
12211 if (TREE_CODE (decl) == FUNCTION_DECL)
12212 current_function_decl = saved_current_function_decl;
12213 }
12214
12215 /* Parse the initializer. */
12216 initializer = NULL_TREE;
12217 is_parenthesized_init = false;
12218 is_non_constant_init = true;
12219 if (is_initialized)
12220 {
12221 if (function_declarator_p (declarator))
12222 {
12223 if (initialization_kind == CPP_EQ)
12224 initializer = cp_parser_pure_specifier (parser);
12225 else
12226 {
12227 /* If the declaration was erroneous, we don't really
12228 know what the user intended, so just silently
12229 consume the initializer. */
12230 if (decl != error_mark_node)
12231 error ("initializer provided for function");
12232 cp_parser_skip_to_closing_parenthesis (parser,
12233 /*recovering=*/true,
12234 /*or_comma=*/false,
12235 /*consume_paren=*/true);
12236 }
12237 }
12238 else
12239 initializer = cp_parser_initializer (parser,
12240 &is_parenthesized_init,
12241 &is_non_constant_init);
12242 }
12243
12244 /* The old parser allows attributes to appear after a parenthesized
12245 initializer. Mark Mitchell proposed removing this functionality
12246 on the GCC mailing lists on 2002-08-13. This parser accepts the
12247 attributes -- but ignores them. */
12248 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12249 if (cp_parser_attributes_opt (parser))
12250 warning (OPT_Wattributes,
12251 "attributes after parenthesized initializer ignored");
12252
12253 /* For an in-class declaration, use `grokfield' to create the
12254 declaration. */
12255 if (member_p)
12256 {
12257 if (pushed_scope)
12258 {
12259 pop_scope (pushed_scope);
12260 pushed_scope = false;
12261 }
12262 decl = grokfield (declarator, decl_specifiers,
12263 initializer, !is_non_constant_init,
12264 /*asmspec=*/NULL_TREE,
12265 prefix_attributes);
12266 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12267 cp_parser_save_default_args (parser, decl);
12268 }
12269
12270 /* Finish processing the declaration. But, skip friend
12271 declarations. */
12272 if (!friend_p && decl && decl != error_mark_node)
12273 {
12274 cp_finish_decl (decl,
12275 initializer, !is_non_constant_init,
12276 asm_specification,
12277 /* If the initializer is in parentheses, then this is
12278 a direct-initialization, which means that an
12279 `explicit' constructor is OK. Otherwise, an
12280 `explicit' constructor cannot be used. */
12281 ((is_parenthesized_init || !is_initialized)
12282 ? 0 : LOOKUP_ONLYCONVERTING));
12283 }
12284 else if ((cxx_dialect != cxx98) && friend_p
12285 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12286 /* Core issue #226 (C++0x only): A default template-argument
12287 shall not be specified in a friend class template
12288 declaration. */
12289 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12290 /*is_partial=*/0, /*is_friend_decl=*/1);
12291
12292 if (!friend_p && pushed_scope)
12293 pop_scope (pushed_scope);
12294
12295 return decl;
12296 }
12297
12298 /* Parse a declarator.
12299
12300 declarator:
12301 direct-declarator
12302 ptr-operator declarator
12303
12304 abstract-declarator:
12305 ptr-operator abstract-declarator [opt]
12306 direct-abstract-declarator
12307
12308 GNU Extensions:
12309
12310 declarator:
12311 attributes [opt] direct-declarator
12312 attributes [opt] ptr-operator declarator
12313
12314 abstract-declarator:
12315 attributes [opt] ptr-operator abstract-declarator [opt]
12316 attributes [opt] direct-abstract-declarator
12317
12318 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12319 detect constructor, destructor or conversion operators. It is set
12320 to -1 if the declarator is a name, and +1 if it is a
12321 function. Otherwise it is set to zero. Usually you just want to
12322 test for >0, but internally the negative value is used.
12323
12324 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12325 a decl-specifier-seq unless it declares a constructor, destructor,
12326 or conversion. It might seem that we could check this condition in
12327 semantic analysis, rather than parsing, but that makes it difficult
12328 to handle something like `f()'. We want to notice that there are
12329 no decl-specifiers, and therefore realize that this is an
12330 expression, not a declaration.)
12331
12332 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12333 the declarator is a direct-declarator of the form "(...)".
12334
12335 MEMBER_P is true iff this declarator is a member-declarator. */
12336
12337 static cp_declarator *
12338 cp_parser_declarator (cp_parser* parser,
12339 cp_parser_declarator_kind dcl_kind,
12340 int* ctor_dtor_or_conv_p,
12341 bool* parenthesized_p,
12342 bool member_p)
12343 {
12344 cp_token *token;
12345 cp_declarator *declarator;
12346 enum tree_code code;
12347 cp_cv_quals cv_quals;
12348 tree class_type;
12349 tree attributes = NULL_TREE;
12350
12351 /* Assume this is not a constructor, destructor, or type-conversion
12352 operator. */
12353 if (ctor_dtor_or_conv_p)
12354 *ctor_dtor_or_conv_p = 0;
12355
12356 if (cp_parser_allow_gnu_extensions_p (parser))
12357 attributes = cp_parser_attributes_opt (parser);
12358
12359 /* Peek at the next token. */
12360 token = cp_lexer_peek_token (parser->lexer);
12361
12362 /* Check for the ptr-operator production. */
12363 cp_parser_parse_tentatively (parser);
12364 /* Parse the ptr-operator. */
12365 code = cp_parser_ptr_operator (parser,
12366 &class_type,
12367 &cv_quals);
12368 /* If that worked, then we have a ptr-operator. */
12369 if (cp_parser_parse_definitely (parser))
12370 {
12371 /* If a ptr-operator was found, then this declarator was not
12372 parenthesized. */
12373 if (parenthesized_p)
12374 *parenthesized_p = true;
12375 /* The dependent declarator is optional if we are parsing an
12376 abstract-declarator. */
12377 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12378 cp_parser_parse_tentatively (parser);
12379
12380 /* Parse the dependent declarator. */
12381 declarator = cp_parser_declarator (parser, dcl_kind,
12382 /*ctor_dtor_or_conv_p=*/NULL,
12383 /*parenthesized_p=*/NULL,
12384 /*member_p=*/false);
12385
12386 /* If we are parsing an abstract-declarator, we must handle the
12387 case where the dependent declarator is absent. */
12388 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12389 && !cp_parser_parse_definitely (parser))
12390 declarator = NULL;
12391
12392 declarator = cp_parser_make_indirect_declarator
12393 (code, class_type, cv_quals, declarator);
12394 }
12395 /* Everything else is a direct-declarator. */
12396 else
12397 {
12398 if (parenthesized_p)
12399 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12400 CPP_OPEN_PAREN);
12401 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12402 ctor_dtor_or_conv_p,
12403 member_p);
12404 }
12405
12406 if (attributes && declarator && declarator != cp_error_declarator)
12407 declarator->attributes = attributes;
12408
12409 return declarator;
12410 }
12411
12412 /* Parse a direct-declarator or direct-abstract-declarator.
12413
12414 direct-declarator:
12415 declarator-id
12416 direct-declarator ( parameter-declaration-clause )
12417 cv-qualifier-seq [opt]
12418 exception-specification [opt]
12419 direct-declarator [ constant-expression [opt] ]
12420 ( declarator )
12421
12422 direct-abstract-declarator:
12423 direct-abstract-declarator [opt]
12424 ( parameter-declaration-clause )
12425 cv-qualifier-seq [opt]
12426 exception-specification [opt]
12427 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12428 ( abstract-declarator )
12429
12430 Returns a representation of the declarator. DCL_KIND is
12431 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12432 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12433 we are parsing a direct-declarator. It is
12434 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12435 of ambiguity we prefer an abstract declarator, as per
12436 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12437 cp_parser_declarator. */
12438
12439 static cp_declarator *
12440 cp_parser_direct_declarator (cp_parser* parser,
12441 cp_parser_declarator_kind dcl_kind,
12442 int* ctor_dtor_or_conv_p,
12443 bool member_p)
12444 {
12445 cp_token *token;
12446 cp_declarator *declarator = NULL;
12447 tree scope = NULL_TREE;
12448 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12449 bool saved_in_declarator_p = parser->in_declarator_p;
12450 bool first = true;
12451 tree pushed_scope = NULL_TREE;
12452
12453 while (true)
12454 {
12455 /* Peek at the next token. */
12456 token = cp_lexer_peek_token (parser->lexer);
12457 if (token->type == CPP_OPEN_PAREN)
12458 {
12459 /* This is either a parameter-declaration-clause, or a
12460 parenthesized declarator. When we know we are parsing a
12461 named declarator, it must be a parenthesized declarator
12462 if FIRST is true. For instance, `(int)' is a
12463 parameter-declaration-clause, with an omitted
12464 direct-abstract-declarator. But `((*))', is a
12465 parenthesized abstract declarator. Finally, when T is a
12466 template parameter `(T)' is a
12467 parameter-declaration-clause, and not a parenthesized
12468 named declarator.
12469
12470 We first try and parse a parameter-declaration-clause,
12471 and then try a nested declarator (if FIRST is true).
12472
12473 It is not an error for it not to be a
12474 parameter-declaration-clause, even when FIRST is
12475 false. Consider,
12476
12477 int i (int);
12478 int i (3);
12479
12480 The first is the declaration of a function while the
12481 second is a the definition of a variable, including its
12482 initializer.
12483
12484 Having seen only the parenthesis, we cannot know which of
12485 these two alternatives should be selected. Even more
12486 complex are examples like:
12487
12488 int i (int (a));
12489 int i (int (3));
12490
12491 The former is a function-declaration; the latter is a
12492 variable initialization.
12493
12494 Thus again, we try a parameter-declaration-clause, and if
12495 that fails, we back out and return. */
12496
12497 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12498 {
12499 cp_parameter_declarator *params;
12500 unsigned saved_num_template_parameter_lists;
12501
12502 /* In a member-declarator, the only valid interpretation
12503 of a parenthesis is the start of a
12504 parameter-declaration-clause. (It is invalid to
12505 initialize a static data member with a parenthesized
12506 initializer; only the "=" form of initialization is
12507 permitted.) */
12508 if (!member_p)
12509 cp_parser_parse_tentatively (parser);
12510
12511 /* Consume the `('. */
12512 cp_lexer_consume_token (parser->lexer);
12513 if (first)
12514 {
12515 /* If this is going to be an abstract declarator, we're
12516 in a declarator and we can't have default args. */
12517 parser->default_arg_ok_p = false;
12518 parser->in_declarator_p = true;
12519 }
12520
12521 /* Inside the function parameter list, surrounding
12522 template-parameter-lists do not apply. */
12523 saved_num_template_parameter_lists
12524 = parser->num_template_parameter_lists;
12525 parser->num_template_parameter_lists = 0;
12526
12527 /* Parse the parameter-declaration-clause. */
12528 params = cp_parser_parameter_declaration_clause (parser);
12529
12530 parser->num_template_parameter_lists
12531 = saved_num_template_parameter_lists;
12532
12533 /* If all went well, parse the cv-qualifier-seq and the
12534 exception-specification. */
12535 if (member_p || cp_parser_parse_definitely (parser))
12536 {
12537 cp_cv_quals cv_quals;
12538 tree exception_specification;
12539
12540 if (ctor_dtor_or_conv_p)
12541 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12542 first = false;
12543 /* Consume the `)'. */
12544 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12545
12546 /* Parse the cv-qualifier-seq. */
12547 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12548 /* And the exception-specification. */
12549 exception_specification
12550 = cp_parser_exception_specification_opt (parser);
12551
12552 /* Create the function-declarator. */
12553 declarator = make_call_declarator (declarator,
12554 params,
12555 cv_quals,
12556 exception_specification);
12557 /* Any subsequent parameter lists are to do with
12558 return type, so are not those of the declared
12559 function. */
12560 parser->default_arg_ok_p = false;
12561
12562 /* Repeat the main loop. */
12563 continue;
12564 }
12565 }
12566
12567 /* If this is the first, we can try a parenthesized
12568 declarator. */
12569 if (first)
12570 {
12571 bool saved_in_type_id_in_expr_p;
12572
12573 parser->default_arg_ok_p = saved_default_arg_ok_p;
12574 parser->in_declarator_p = saved_in_declarator_p;
12575
12576 /* Consume the `('. */
12577 cp_lexer_consume_token (parser->lexer);
12578 /* Parse the nested declarator. */
12579 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12580 parser->in_type_id_in_expr_p = true;
12581 declarator
12582 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12583 /*parenthesized_p=*/NULL,
12584 member_p);
12585 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12586 first = false;
12587 /* Expect a `)'. */
12588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12589 declarator = cp_error_declarator;
12590 if (declarator == cp_error_declarator)
12591 break;
12592
12593 goto handle_declarator;
12594 }
12595 /* Otherwise, we must be done. */
12596 else
12597 break;
12598 }
12599 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12600 && token->type == CPP_OPEN_SQUARE)
12601 {
12602 /* Parse an array-declarator. */
12603 tree bounds;
12604
12605 if (ctor_dtor_or_conv_p)
12606 *ctor_dtor_or_conv_p = 0;
12607
12608 first = false;
12609 parser->default_arg_ok_p = false;
12610 parser->in_declarator_p = true;
12611 /* Consume the `['. */
12612 cp_lexer_consume_token (parser->lexer);
12613 /* Peek at the next token. */
12614 token = cp_lexer_peek_token (parser->lexer);
12615 /* If the next token is `]', then there is no
12616 constant-expression. */
12617 if (token->type != CPP_CLOSE_SQUARE)
12618 {
12619 bool non_constant_p;
12620
12621 bounds
12622 = cp_parser_constant_expression (parser,
12623 /*allow_non_constant=*/true,
12624 &non_constant_p);
12625 if (!non_constant_p)
12626 bounds = fold_non_dependent_expr (bounds);
12627 /* Normally, the array bound must be an integral constant
12628 expression. However, as an extension, we allow VLAs
12629 in function scopes. */
12630 else if (!parser->in_function_body)
12631 {
12632 error ("array bound is not an integer constant");
12633 bounds = error_mark_node;
12634 }
12635 }
12636 else
12637 bounds = NULL_TREE;
12638 /* Look for the closing `]'. */
12639 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12640 {
12641 declarator = cp_error_declarator;
12642 break;
12643 }
12644
12645 declarator = make_array_declarator (declarator, bounds);
12646 }
12647 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12648 {
12649 tree qualifying_scope;
12650 tree unqualified_name;
12651 special_function_kind sfk;
12652 bool abstract_ok;
12653 bool pack_expansion_p = false;
12654
12655 /* Parse a declarator-id */
12656 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12657 if (abstract_ok)
12658 {
12659 cp_parser_parse_tentatively (parser);
12660
12661 /* If we see an ellipsis, we should be looking at a
12662 parameter pack. */
12663 if (token->type == CPP_ELLIPSIS)
12664 {
12665 /* Consume the `...' */
12666 cp_lexer_consume_token (parser->lexer);
12667
12668 pack_expansion_p = true;
12669 }
12670 }
12671
12672 unqualified_name
12673 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12674 qualifying_scope = parser->scope;
12675 if (abstract_ok)
12676 {
12677 bool okay = false;
12678
12679 if (!unqualified_name && pack_expansion_p)
12680 {
12681 /* Check whether an error occurred. */
12682 okay = !cp_parser_error_occurred (parser);
12683
12684 /* We already consumed the ellipsis to mark a
12685 parameter pack, but we have no way to report it,
12686 so abort the tentative parse. We will be exiting
12687 immediately anyway. */
12688 cp_parser_abort_tentative_parse (parser);
12689 }
12690 else
12691 okay = cp_parser_parse_definitely (parser);
12692
12693 if (!okay)
12694 unqualified_name = error_mark_node;
12695 else if (unqualified_name
12696 && (qualifying_scope
12697 || (TREE_CODE (unqualified_name)
12698 != IDENTIFIER_NODE)))
12699 {
12700 cp_parser_error (parser, "expected unqualified-id");
12701 unqualified_name = error_mark_node;
12702 }
12703 }
12704
12705 if (!unqualified_name)
12706 return NULL;
12707 if (unqualified_name == error_mark_node)
12708 {
12709 declarator = cp_error_declarator;
12710 pack_expansion_p = false;
12711 declarator->parameter_pack_p = false;
12712 break;
12713 }
12714
12715 if (qualifying_scope && at_namespace_scope_p ()
12716 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12717 {
12718 /* In the declaration of a member of a template class
12719 outside of the class itself, the SCOPE will sometimes
12720 be a TYPENAME_TYPE. For example, given:
12721
12722 template <typename T>
12723 int S<T>::R::i = 3;
12724
12725 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12726 this context, we must resolve S<T>::R to an ordinary
12727 type, rather than a typename type.
12728
12729 The reason we normally avoid resolving TYPENAME_TYPEs
12730 is that a specialization of `S' might render
12731 `S<T>::R' not a type. However, if `S' is
12732 specialized, then this `i' will not be used, so there
12733 is no harm in resolving the types here. */
12734 tree type;
12735
12736 /* Resolve the TYPENAME_TYPE. */
12737 type = resolve_typename_type (qualifying_scope,
12738 /*only_current_p=*/false);
12739 /* If that failed, the declarator is invalid. */
12740 if (TREE_CODE (type) == TYPENAME_TYPE)
12741 error ("%<%T::%E%> is not a type",
12742 TYPE_CONTEXT (qualifying_scope),
12743 TYPE_IDENTIFIER (qualifying_scope));
12744 qualifying_scope = type;
12745 }
12746
12747 sfk = sfk_none;
12748
12749 if (unqualified_name)
12750 {
12751 tree class_type;
12752
12753 if (qualifying_scope
12754 && CLASS_TYPE_P (qualifying_scope))
12755 class_type = qualifying_scope;
12756 else
12757 class_type = current_class_type;
12758
12759 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12760 {
12761 tree name_type = TREE_TYPE (unqualified_name);
12762 if (class_type && same_type_p (name_type, class_type))
12763 {
12764 if (qualifying_scope
12765 && CLASSTYPE_USE_TEMPLATE (name_type))
12766 {
12767 error ("invalid use of constructor as a template");
12768 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12769 "name the constructor in a qualified name",
12770 class_type,
12771 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12772 class_type, name_type);
12773 declarator = cp_error_declarator;
12774 break;
12775 }
12776 else
12777 unqualified_name = constructor_name (class_type);
12778 }
12779 else
12780 {
12781 /* We do not attempt to print the declarator
12782 here because we do not have enough
12783 information about its original syntactic
12784 form. */
12785 cp_parser_error (parser, "invalid declarator");
12786 declarator = cp_error_declarator;
12787 break;
12788 }
12789 }
12790
12791 if (class_type)
12792 {
12793 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12794 sfk = sfk_destructor;
12795 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12796 sfk = sfk_conversion;
12797 else if (/* There's no way to declare a constructor
12798 for an anonymous type, even if the type
12799 got a name for linkage purposes. */
12800 !TYPE_WAS_ANONYMOUS (class_type)
12801 && constructor_name_p (unqualified_name,
12802 class_type))
12803 {
12804 unqualified_name = constructor_name (class_type);
12805 sfk = sfk_constructor;
12806 }
12807
12808 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12809 *ctor_dtor_or_conv_p = -1;
12810 }
12811 }
12812 declarator = make_id_declarator (qualifying_scope,
12813 unqualified_name,
12814 sfk);
12815 declarator->id_loc = token->location;
12816 declarator->parameter_pack_p = pack_expansion_p;
12817
12818 if (pack_expansion_p)
12819 maybe_warn_variadic_templates ();
12820
12821 handle_declarator:;
12822 scope = get_scope_of_declarator (declarator);
12823 if (scope)
12824 /* Any names that appear after the declarator-id for a
12825 member are looked up in the containing scope. */
12826 pushed_scope = push_scope (scope);
12827 parser->in_declarator_p = true;
12828 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12829 || (declarator && declarator->kind == cdk_id))
12830 /* Default args are only allowed on function
12831 declarations. */
12832 parser->default_arg_ok_p = saved_default_arg_ok_p;
12833 else
12834 parser->default_arg_ok_p = false;
12835
12836 first = false;
12837 }
12838 /* We're done. */
12839 else
12840 break;
12841 }
12842
12843 /* For an abstract declarator, we might wind up with nothing at this
12844 point. That's an error; the declarator is not optional. */
12845 if (!declarator)
12846 cp_parser_error (parser, "expected declarator");
12847
12848 /* If we entered a scope, we must exit it now. */
12849 if (pushed_scope)
12850 pop_scope (pushed_scope);
12851
12852 parser->default_arg_ok_p = saved_default_arg_ok_p;
12853 parser->in_declarator_p = saved_in_declarator_p;
12854
12855 return declarator;
12856 }
12857
12858 /* Parse a ptr-operator.
12859
12860 ptr-operator:
12861 * cv-qualifier-seq [opt]
12862 &
12863 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12864
12865 GNU Extension:
12866
12867 ptr-operator:
12868 & cv-qualifier-seq [opt]
12869
12870 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12871 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12872 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12873 filled in with the TYPE containing the member. *CV_QUALS is
12874 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12875 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12876 Note that the tree codes returned by this function have nothing
12877 to do with the types of trees that will be eventually be created
12878 to represent the pointer or reference type being parsed. They are
12879 just constants with suggestive names. */
12880 static enum tree_code
12881 cp_parser_ptr_operator (cp_parser* parser,
12882 tree* type,
12883 cp_cv_quals *cv_quals)
12884 {
12885 enum tree_code code = ERROR_MARK;
12886 cp_token *token;
12887
12888 /* Assume that it's not a pointer-to-member. */
12889 *type = NULL_TREE;
12890 /* And that there are no cv-qualifiers. */
12891 *cv_quals = TYPE_UNQUALIFIED;
12892
12893 /* Peek at the next token. */
12894 token = cp_lexer_peek_token (parser->lexer);
12895
12896 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12897 if (token->type == CPP_MULT)
12898 code = INDIRECT_REF;
12899 else if (token->type == CPP_AND)
12900 code = ADDR_EXPR;
12901 else if ((cxx_dialect != cxx98) &&
12902 token->type == CPP_AND_AND) /* C++0x only */
12903 code = NON_LVALUE_EXPR;
12904
12905 if (code != ERROR_MARK)
12906 {
12907 /* Consume the `*', `&' or `&&'. */
12908 cp_lexer_consume_token (parser->lexer);
12909
12910 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12911 `&', if we are allowing GNU extensions. (The only qualifier
12912 that can legally appear after `&' is `restrict', but that is
12913 enforced during semantic analysis. */
12914 if (code == INDIRECT_REF
12915 || cp_parser_allow_gnu_extensions_p (parser))
12916 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12917 }
12918 else
12919 {
12920 /* Try the pointer-to-member case. */
12921 cp_parser_parse_tentatively (parser);
12922 /* Look for the optional `::' operator. */
12923 cp_parser_global_scope_opt (parser,
12924 /*current_scope_valid_p=*/false);
12925 /* Look for the nested-name specifier. */
12926 cp_parser_nested_name_specifier (parser,
12927 /*typename_keyword_p=*/false,
12928 /*check_dependency_p=*/true,
12929 /*type_p=*/false,
12930 /*is_declaration=*/false);
12931 /* If we found it, and the next token is a `*', then we are
12932 indeed looking at a pointer-to-member operator. */
12933 if (!cp_parser_error_occurred (parser)
12934 && cp_parser_require (parser, CPP_MULT, "`*'"))
12935 {
12936 /* Indicate that the `*' operator was used. */
12937 code = INDIRECT_REF;
12938
12939 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12940 error ("%qD is a namespace", parser->scope);
12941 else
12942 {
12943 /* The type of which the member is a member is given by the
12944 current SCOPE. */
12945 *type = parser->scope;
12946 /* The next name will not be qualified. */
12947 parser->scope = NULL_TREE;
12948 parser->qualifying_scope = NULL_TREE;
12949 parser->object_scope = NULL_TREE;
12950 /* Look for the optional cv-qualifier-seq. */
12951 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12952 }
12953 }
12954 /* If that didn't work we don't have a ptr-operator. */
12955 if (!cp_parser_parse_definitely (parser))
12956 cp_parser_error (parser, "expected ptr-operator");
12957 }
12958
12959 return code;
12960 }
12961
12962 /* Parse an (optional) cv-qualifier-seq.
12963
12964 cv-qualifier-seq:
12965 cv-qualifier cv-qualifier-seq [opt]
12966
12967 cv-qualifier:
12968 const
12969 volatile
12970
12971 GNU Extension:
12972
12973 cv-qualifier:
12974 __restrict__
12975
12976 Returns a bitmask representing the cv-qualifiers. */
12977
12978 static cp_cv_quals
12979 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12980 {
12981 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12982
12983 while (true)
12984 {
12985 cp_token *token;
12986 cp_cv_quals cv_qualifier;
12987
12988 /* Peek at the next token. */
12989 token = cp_lexer_peek_token (parser->lexer);
12990 /* See if it's a cv-qualifier. */
12991 switch (token->keyword)
12992 {
12993 case RID_CONST:
12994 cv_qualifier = TYPE_QUAL_CONST;
12995 break;
12996
12997 case RID_VOLATILE:
12998 cv_qualifier = TYPE_QUAL_VOLATILE;
12999 break;
13000
13001 case RID_RESTRICT:
13002 cv_qualifier = TYPE_QUAL_RESTRICT;
13003 break;
13004
13005 default:
13006 cv_qualifier = TYPE_UNQUALIFIED;
13007 break;
13008 }
13009
13010 if (!cv_qualifier)
13011 break;
13012
13013 if (cv_quals & cv_qualifier)
13014 {
13015 error ("duplicate cv-qualifier");
13016 cp_lexer_purge_token (parser->lexer);
13017 }
13018 else
13019 {
13020 cp_lexer_consume_token (parser->lexer);
13021 cv_quals |= cv_qualifier;
13022 }
13023 }
13024
13025 return cv_quals;
13026 }
13027
13028 /* Parse a declarator-id.
13029
13030 declarator-id:
13031 id-expression
13032 :: [opt] nested-name-specifier [opt] type-name
13033
13034 In the `id-expression' case, the value returned is as for
13035 cp_parser_id_expression if the id-expression was an unqualified-id.
13036 If the id-expression was a qualified-id, then a SCOPE_REF is
13037 returned. The first operand is the scope (either a NAMESPACE_DECL
13038 or TREE_TYPE), but the second is still just a representation of an
13039 unqualified-id. */
13040
13041 static tree
13042 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13043 {
13044 tree id;
13045 /* The expression must be an id-expression. Assume that qualified
13046 names are the names of types so that:
13047
13048 template <class T>
13049 int S<T>::R::i = 3;
13050
13051 will work; we must treat `S<T>::R' as the name of a type.
13052 Similarly, assume that qualified names are templates, where
13053 required, so that:
13054
13055 template <class T>
13056 int S<T>::R<T>::i = 3;
13057
13058 will work, too. */
13059 id = cp_parser_id_expression (parser,
13060 /*template_keyword_p=*/false,
13061 /*check_dependency_p=*/false,
13062 /*template_p=*/NULL,
13063 /*declarator_p=*/true,
13064 optional_p);
13065 if (id && BASELINK_P (id))
13066 id = BASELINK_FUNCTIONS (id);
13067 return id;
13068 }
13069
13070 /* Parse a type-id.
13071
13072 type-id:
13073 type-specifier-seq abstract-declarator [opt]
13074
13075 Returns the TYPE specified. */
13076
13077 static tree
13078 cp_parser_type_id (cp_parser* parser)
13079 {
13080 cp_decl_specifier_seq type_specifier_seq;
13081 cp_declarator *abstract_declarator;
13082
13083 /* Parse the type-specifier-seq. */
13084 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13085 &type_specifier_seq);
13086 if (type_specifier_seq.type == error_mark_node)
13087 return error_mark_node;
13088
13089 /* There might or might not be an abstract declarator. */
13090 cp_parser_parse_tentatively (parser);
13091 /* Look for the declarator. */
13092 abstract_declarator
13093 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13094 /*parenthesized_p=*/NULL,
13095 /*member_p=*/false);
13096 /* Check to see if there really was a declarator. */
13097 if (!cp_parser_parse_definitely (parser))
13098 abstract_declarator = NULL;
13099
13100 return groktypename (&type_specifier_seq, abstract_declarator);
13101 }
13102
13103 /* Parse a type-specifier-seq.
13104
13105 type-specifier-seq:
13106 type-specifier type-specifier-seq [opt]
13107
13108 GNU extension:
13109
13110 type-specifier-seq:
13111 attributes type-specifier-seq [opt]
13112
13113 If IS_CONDITION is true, we are at the start of a "condition",
13114 e.g., we've just seen "if (".
13115
13116 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13117
13118 static void
13119 cp_parser_type_specifier_seq (cp_parser* parser,
13120 bool is_condition,
13121 cp_decl_specifier_seq *type_specifier_seq)
13122 {
13123 bool seen_type_specifier = false;
13124 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13125
13126 /* Clear the TYPE_SPECIFIER_SEQ. */
13127 clear_decl_specs (type_specifier_seq);
13128
13129 /* Parse the type-specifiers and attributes. */
13130 while (true)
13131 {
13132 tree type_specifier;
13133 bool is_cv_qualifier;
13134
13135 /* Check for attributes first. */
13136 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13137 {
13138 type_specifier_seq->attributes =
13139 chainon (type_specifier_seq->attributes,
13140 cp_parser_attributes_opt (parser));
13141 continue;
13142 }
13143
13144 /* Look for the type-specifier. */
13145 type_specifier = cp_parser_type_specifier (parser,
13146 flags,
13147 type_specifier_seq,
13148 /*is_declaration=*/false,
13149 NULL,
13150 &is_cv_qualifier);
13151 if (!type_specifier)
13152 {
13153 /* If the first type-specifier could not be found, this is not a
13154 type-specifier-seq at all. */
13155 if (!seen_type_specifier)
13156 {
13157 cp_parser_error (parser, "expected type-specifier");
13158 type_specifier_seq->type = error_mark_node;
13159 return;
13160 }
13161 /* If subsequent type-specifiers could not be found, the
13162 type-specifier-seq is complete. */
13163 break;
13164 }
13165
13166 seen_type_specifier = true;
13167 /* The standard says that a condition can be:
13168
13169 type-specifier-seq declarator = assignment-expression
13170
13171 However, given:
13172
13173 struct S {};
13174 if (int S = ...)
13175
13176 we should treat the "S" as a declarator, not as a
13177 type-specifier. The standard doesn't say that explicitly for
13178 type-specifier-seq, but it does say that for
13179 decl-specifier-seq in an ordinary declaration. Perhaps it
13180 would be clearer just to allow a decl-specifier-seq here, and
13181 then add a semantic restriction that if any decl-specifiers
13182 that are not type-specifiers appear, the program is invalid. */
13183 if (is_condition && !is_cv_qualifier)
13184 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13185 }
13186
13187 cp_parser_check_decl_spec (type_specifier_seq);
13188 }
13189
13190 /* Parse a parameter-declaration-clause.
13191
13192 parameter-declaration-clause:
13193 parameter-declaration-list [opt] ... [opt]
13194 parameter-declaration-list , ...
13195
13196 Returns a representation for the parameter declarations. A return
13197 value of NULL indicates a parameter-declaration-clause consisting
13198 only of an ellipsis. */
13199
13200 static cp_parameter_declarator *
13201 cp_parser_parameter_declaration_clause (cp_parser* parser)
13202 {
13203 cp_parameter_declarator *parameters;
13204 cp_token *token;
13205 bool ellipsis_p;
13206 bool is_error;
13207
13208 /* Peek at the next token. */
13209 token = cp_lexer_peek_token (parser->lexer);
13210 /* Check for trivial parameter-declaration-clauses. */
13211 if (token->type == CPP_ELLIPSIS)
13212 {
13213 /* Consume the `...' token. */
13214 cp_lexer_consume_token (parser->lexer);
13215 return NULL;
13216 }
13217 else if (token->type == CPP_CLOSE_PAREN)
13218 /* There are no parameters. */
13219 {
13220 #ifndef NO_IMPLICIT_EXTERN_C
13221 if (in_system_header && current_class_type == NULL
13222 && current_lang_name == lang_name_c)
13223 return NULL;
13224 else
13225 #endif
13226 return no_parameters;
13227 }
13228 /* Check for `(void)', too, which is a special case. */
13229 else if (token->keyword == RID_VOID
13230 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13231 == CPP_CLOSE_PAREN))
13232 {
13233 /* Consume the `void' token. */
13234 cp_lexer_consume_token (parser->lexer);
13235 /* There are no parameters. */
13236 return no_parameters;
13237 }
13238
13239 /* Parse the parameter-declaration-list. */
13240 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13241 /* If a parse error occurred while parsing the
13242 parameter-declaration-list, then the entire
13243 parameter-declaration-clause is erroneous. */
13244 if (is_error)
13245 return NULL;
13246
13247 /* Peek at the next token. */
13248 token = cp_lexer_peek_token (parser->lexer);
13249 /* If it's a `,', the clause should terminate with an ellipsis. */
13250 if (token->type == CPP_COMMA)
13251 {
13252 /* Consume the `,'. */
13253 cp_lexer_consume_token (parser->lexer);
13254 /* Expect an ellipsis. */
13255 ellipsis_p
13256 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13257 }
13258 /* It might also be `...' if the optional trailing `,' was
13259 omitted. */
13260 else if (token->type == CPP_ELLIPSIS)
13261 {
13262 /* Consume the `...' token. */
13263 cp_lexer_consume_token (parser->lexer);
13264 /* And remember that we saw it. */
13265 ellipsis_p = true;
13266 }
13267 else
13268 ellipsis_p = false;
13269
13270 /* Finish the parameter list. */
13271 if (parameters && ellipsis_p)
13272 parameters->ellipsis_p = true;
13273
13274 return parameters;
13275 }
13276
13277 /* Parse a parameter-declaration-list.
13278
13279 parameter-declaration-list:
13280 parameter-declaration
13281 parameter-declaration-list , parameter-declaration
13282
13283 Returns a representation of the parameter-declaration-list, as for
13284 cp_parser_parameter_declaration_clause. However, the
13285 `void_list_node' is never appended to the list. Upon return,
13286 *IS_ERROR will be true iff an error occurred. */
13287
13288 static cp_parameter_declarator *
13289 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13290 {
13291 cp_parameter_declarator *parameters = NULL;
13292 cp_parameter_declarator **tail = &parameters;
13293 bool saved_in_unbraced_linkage_specification_p;
13294
13295 /* Assume all will go well. */
13296 *is_error = false;
13297 /* The special considerations that apply to a function within an
13298 unbraced linkage specifications do not apply to the parameters
13299 to the function. */
13300 saved_in_unbraced_linkage_specification_p
13301 = parser->in_unbraced_linkage_specification_p;
13302 parser->in_unbraced_linkage_specification_p = false;
13303
13304 /* Look for more parameters. */
13305 while (true)
13306 {
13307 cp_parameter_declarator *parameter;
13308 bool parenthesized_p;
13309 /* Parse the parameter. */
13310 parameter
13311 = cp_parser_parameter_declaration (parser,
13312 /*template_parm_p=*/false,
13313 &parenthesized_p);
13314
13315 /* If a parse error occurred parsing the parameter declaration,
13316 then the entire parameter-declaration-list is erroneous. */
13317 if (!parameter)
13318 {
13319 *is_error = true;
13320 parameters = NULL;
13321 break;
13322 }
13323 /* Add the new parameter to the list. */
13324 *tail = parameter;
13325 tail = &parameter->next;
13326
13327 /* Peek at the next token. */
13328 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13329 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13330 /* These are for Objective-C++ */
13331 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13332 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13333 /* The parameter-declaration-list is complete. */
13334 break;
13335 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13336 {
13337 cp_token *token;
13338
13339 /* Peek at the next token. */
13340 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13341 /* If it's an ellipsis, then the list is complete. */
13342 if (token->type == CPP_ELLIPSIS)
13343 break;
13344 /* Otherwise, there must be more parameters. Consume the
13345 `,'. */
13346 cp_lexer_consume_token (parser->lexer);
13347 /* When parsing something like:
13348
13349 int i(float f, double d)
13350
13351 we can tell after seeing the declaration for "f" that we
13352 are not looking at an initialization of a variable "i",
13353 but rather at the declaration of a function "i".
13354
13355 Due to the fact that the parsing of template arguments
13356 (as specified to a template-id) requires backtracking we
13357 cannot use this technique when inside a template argument
13358 list. */
13359 if (!parser->in_template_argument_list_p
13360 && !parser->in_type_id_in_expr_p
13361 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13362 /* However, a parameter-declaration of the form
13363 "foat(f)" (which is a valid declaration of a
13364 parameter "f") can also be interpreted as an
13365 expression (the conversion of "f" to "float"). */
13366 && !parenthesized_p)
13367 cp_parser_commit_to_tentative_parse (parser);
13368 }
13369 else
13370 {
13371 cp_parser_error (parser, "expected %<,%> or %<...%>");
13372 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13373 cp_parser_skip_to_closing_parenthesis (parser,
13374 /*recovering=*/true,
13375 /*or_comma=*/false,
13376 /*consume_paren=*/false);
13377 break;
13378 }
13379 }
13380
13381 parser->in_unbraced_linkage_specification_p
13382 = saved_in_unbraced_linkage_specification_p;
13383
13384 return parameters;
13385 }
13386
13387 /* Parse a parameter declaration.
13388
13389 parameter-declaration:
13390 decl-specifier-seq ... [opt] declarator
13391 decl-specifier-seq declarator = assignment-expression
13392 decl-specifier-seq ... [opt] abstract-declarator [opt]
13393 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13394
13395 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13396 declares a template parameter. (In that case, a non-nested `>'
13397 token encountered during the parsing of the assignment-expression
13398 is not interpreted as a greater-than operator.)
13399
13400 Returns a representation of the parameter, or NULL if an error
13401 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13402 true iff the declarator is of the form "(p)". */
13403
13404 static cp_parameter_declarator *
13405 cp_parser_parameter_declaration (cp_parser *parser,
13406 bool template_parm_p,
13407 bool *parenthesized_p)
13408 {
13409 int declares_class_or_enum;
13410 bool greater_than_is_operator_p;
13411 cp_decl_specifier_seq decl_specifiers;
13412 cp_declarator *declarator;
13413 tree default_argument;
13414 cp_token *token;
13415 const char *saved_message;
13416
13417 /* In a template parameter, `>' is not an operator.
13418
13419 [temp.param]
13420
13421 When parsing a default template-argument for a non-type
13422 template-parameter, the first non-nested `>' is taken as the end
13423 of the template parameter-list rather than a greater-than
13424 operator. */
13425 greater_than_is_operator_p = !template_parm_p;
13426
13427 /* Type definitions may not appear in parameter types. */
13428 saved_message = parser->type_definition_forbidden_message;
13429 parser->type_definition_forbidden_message
13430 = "types may not be defined in parameter types";
13431
13432 /* Parse the declaration-specifiers. */
13433 cp_parser_decl_specifier_seq (parser,
13434 CP_PARSER_FLAGS_NONE,
13435 &decl_specifiers,
13436 &declares_class_or_enum);
13437 /* If an error occurred, there's no reason to attempt to parse the
13438 rest of the declaration. */
13439 if (cp_parser_error_occurred (parser))
13440 {
13441 parser->type_definition_forbidden_message = saved_message;
13442 return NULL;
13443 }
13444
13445 /* Peek at the next token. */
13446 token = cp_lexer_peek_token (parser->lexer);
13447
13448 /* If the next token is a `)', `,', `=', `>', or `...', then there
13449 is no declarator. However, when variadic templates are enabled,
13450 there may be a declarator following `...'. */
13451 if (token->type == CPP_CLOSE_PAREN
13452 || token->type == CPP_COMMA
13453 || token->type == CPP_EQ
13454 || token->type == CPP_GREATER)
13455 {
13456 declarator = NULL;
13457 if (parenthesized_p)
13458 *parenthesized_p = false;
13459 }
13460 /* Otherwise, there should be a declarator. */
13461 else
13462 {
13463 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13464 parser->default_arg_ok_p = false;
13465
13466 /* After seeing a decl-specifier-seq, if the next token is not a
13467 "(", there is no possibility that the code is a valid
13468 expression. Therefore, if parsing tentatively, we commit at
13469 this point. */
13470 if (!parser->in_template_argument_list_p
13471 /* In an expression context, having seen:
13472
13473 (int((char ...
13474
13475 we cannot be sure whether we are looking at a
13476 function-type (taking a "char" as a parameter) or a cast
13477 of some object of type "char" to "int". */
13478 && !parser->in_type_id_in_expr_p
13479 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13480 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13481 cp_parser_commit_to_tentative_parse (parser);
13482 /* Parse the declarator. */
13483 declarator = cp_parser_declarator (parser,
13484 CP_PARSER_DECLARATOR_EITHER,
13485 /*ctor_dtor_or_conv_p=*/NULL,
13486 parenthesized_p,
13487 /*member_p=*/false);
13488 parser->default_arg_ok_p = saved_default_arg_ok_p;
13489 /* After the declarator, allow more attributes. */
13490 decl_specifiers.attributes
13491 = chainon (decl_specifiers.attributes,
13492 cp_parser_attributes_opt (parser));
13493 }
13494
13495 /* If the next token is an ellipsis, and we have not seen a
13496 declarator name, and the type of the declarator contains parameter
13497 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13498 a parameter pack expansion expression. Otherwise, leave the
13499 ellipsis for a C-style variadic function. */
13500 token = cp_lexer_peek_token (parser->lexer);
13501 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13502 {
13503 tree type = decl_specifiers.type;
13504
13505 if (type && DECL_P (type))
13506 type = TREE_TYPE (type);
13507
13508 if (type
13509 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13510 && declarator_can_be_parameter_pack (declarator)
13511 && (!declarator || !declarator->parameter_pack_p)
13512 && uses_parameter_packs (type))
13513 {
13514 /* Consume the `...'. */
13515 cp_lexer_consume_token (parser->lexer);
13516 maybe_warn_variadic_templates ();
13517
13518 /* Build a pack expansion type */
13519 if (declarator)
13520 declarator->parameter_pack_p = true;
13521 else
13522 decl_specifiers.type = make_pack_expansion (type);
13523 }
13524 }
13525
13526 /* The restriction on defining new types applies only to the type
13527 of the parameter, not to the default argument. */
13528 parser->type_definition_forbidden_message = saved_message;
13529
13530 /* If the next token is `=', then process a default argument. */
13531 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13532 {
13533 bool saved_greater_than_is_operator_p;
13534 /* Consume the `='. */
13535 cp_lexer_consume_token (parser->lexer);
13536
13537 /* If we are defining a class, then the tokens that make up the
13538 default argument must be saved and processed later. */
13539 if (!template_parm_p && at_class_scope_p ()
13540 && TYPE_BEING_DEFINED (current_class_type))
13541 {
13542 unsigned depth = 0;
13543 cp_token *first_token;
13544 cp_token *token;
13545
13546 /* Add tokens until we have processed the entire default
13547 argument. We add the range [first_token, token). */
13548 first_token = cp_lexer_peek_token (parser->lexer);
13549 while (true)
13550 {
13551 bool done = false;
13552
13553 /* Peek at the next token. */
13554 token = cp_lexer_peek_token (parser->lexer);
13555 /* What we do depends on what token we have. */
13556 switch (token->type)
13557 {
13558 /* In valid code, a default argument must be
13559 immediately followed by a `,' `)', or `...'. */
13560 case CPP_COMMA:
13561 case CPP_CLOSE_PAREN:
13562 case CPP_ELLIPSIS:
13563 /* If we run into a non-nested `;', `}', or `]',
13564 then the code is invalid -- but the default
13565 argument is certainly over. */
13566 case CPP_SEMICOLON:
13567 case CPP_CLOSE_BRACE:
13568 case CPP_CLOSE_SQUARE:
13569 if (depth == 0)
13570 done = true;
13571 /* Update DEPTH, if necessary. */
13572 else if (token->type == CPP_CLOSE_PAREN
13573 || token->type == CPP_CLOSE_BRACE
13574 || token->type == CPP_CLOSE_SQUARE)
13575 --depth;
13576 break;
13577
13578 case CPP_OPEN_PAREN:
13579 case CPP_OPEN_SQUARE:
13580 case CPP_OPEN_BRACE:
13581 ++depth;
13582 break;
13583
13584 case CPP_RSHIFT:
13585 if (cxx_dialect == cxx98)
13586 break;
13587 /* Fall through for C++0x, which treats the `>>'
13588 operator like two `>' tokens in certain
13589 cases. */
13590
13591 case CPP_GREATER:
13592 /* If we see a non-nested `>', and `>' is not an
13593 operator, then it marks the end of the default
13594 argument. */
13595 if (!depth && !greater_than_is_operator_p)
13596 done = true;
13597 break;
13598
13599 /* If we run out of tokens, issue an error message. */
13600 case CPP_EOF:
13601 case CPP_PRAGMA_EOL:
13602 error ("file ends in default argument");
13603 done = true;
13604 break;
13605
13606 case CPP_NAME:
13607 case CPP_SCOPE:
13608 /* In these cases, we should look for template-ids.
13609 For example, if the default argument is
13610 `X<int, double>()', we need to do name lookup to
13611 figure out whether or not `X' is a template; if
13612 so, the `,' does not end the default argument.
13613
13614 That is not yet done. */
13615 break;
13616
13617 default:
13618 break;
13619 }
13620
13621 /* If we've reached the end, stop. */
13622 if (done)
13623 break;
13624
13625 /* Add the token to the token block. */
13626 token = cp_lexer_consume_token (parser->lexer);
13627 }
13628
13629 /* Create a DEFAULT_ARG to represent the unparsed default
13630 argument. */
13631 default_argument = make_node (DEFAULT_ARG);
13632 DEFARG_TOKENS (default_argument)
13633 = cp_token_cache_new (first_token, token);
13634 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13635 }
13636 /* Outside of a class definition, we can just parse the
13637 assignment-expression. */
13638 else
13639 {
13640 bool saved_local_variables_forbidden_p;
13641
13642 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13643 set correctly. */
13644 saved_greater_than_is_operator_p
13645 = parser->greater_than_is_operator_p;
13646 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13647 /* Local variable names (and the `this' keyword) may not
13648 appear in a default argument. */
13649 saved_local_variables_forbidden_p
13650 = parser->local_variables_forbidden_p;
13651 parser->local_variables_forbidden_p = true;
13652 /* The default argument expression may cause implicitly
13653 defined member functions to be synthesized, which will
13654 result in garbage collection. We must treat this
13655 situation as if we were within the body of function so as
13656 to avoid collecting live data on the stack. */
13657 ++function_depth;
13658 /* Parse the assignment-expression. */
13659 if (template_parm_p)
13660 push_deferring_access_checks (dk_no_deferred);
13661 default_argument
13662 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13663 if (template_parm_p)
13664 pop_deferring_access_checks ();
13665 /* Restore saved state. */
13666 --function_depth;
13667 parser->greater_than_is_operator_p
13668 = saved_greater_than_is_operator_p;
13669 parser->local_variables_forbidden_p
13670 = saved_local_variables_forbidden_p;
13671 }
13672 if (!parser->default_arg_ok_p)
13673 {
13674 if (!flag_pedantic_errors)
13675 warning (0, "deprecated use of default argument for parameter of non-function");
13676 else
13677 {
13678 error ("default arguments are only permitted for function parameters");
13679 default_argument = NULL_TREE;
13680 }
13681 }
13682 }
13683 else
13684 default_argument = NULL_TREE;
13685
13686 return make_parameter_declarator (&decl_specifiers,
13687 declarator,
13688 default_argument);
13689 }
13690
13691 /* Parse a function-body.
13692
13693 function-body:
13694 compound_statement */
13695
13696 static void
13697 cp_parser_function_body (cp_parser *parser)
13698 {
13699 cp_parser_compound_statement (parser, NULL, false);
13700 }
13701
13702 /* Parse a ctor-initializer-opt followed by a function-body. Return
13703 true if a ctor-initializer was present. */
13704
13705 static bool
13706 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13707 {
13708 tree body;
13709 bool ctor_initializer_p;
13710
13711 /* Begin the function body. */
13712 body = begin_function_body ();
13713 /* Parse the optional ctor-initializer. */
13714 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13715 /* Parse the function-body. */
13716 cp_parser_function_body (parser);
13717 /* Finish the function body. */
13718 finish_function_body (body);
13719
13720 return ctor_initializer_p;
13721 }
13722
13723 /* Parse an initializer.
13724
13725 initializer:
13726 = initializer-clause
13727 ( expression-list )
13728
13729 Returns an expression representing the initializer. If no
13730 initializer is present, NULL_TREE is returned.
13731
13732 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13733 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13734 set to FALSE if there is no initializer present. If there is an
13735 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13736 is set to true; otherwise it is set to false. */
13737
13738 static tree
13739 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13740 bool* non_constant_p)
13741 {
13742 cp_token *token;
13743 tree init;
13744
13745 /* Peek at the next token. */
13746 token = cp_lexer_peek_token (parser->lexer);
13747
13748 /* Let our caller know whether or not this initializer was
13749 parenthesized. */
13750 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13751 /* Assume that the initializer is constant. */
13752 *non_constant_p = false;
13753
13754 if (token->type == CPP_EQ)
13755 {
13756 /* Consume the `='. */
13757 cp_lexer_consume_token (parser->lexer);
13758 /* Parse the initializer-clause. */
13759 init = cp_parser_initializer_clause (parser, non_constant_p);
13760 }
13761 else if (token->type == CPP_OPEN_PAREN)
13762 init = cp_parser_parenthesized_expression_list (parser, false,
13763 /*cast_p=*/false,
13764 /*allow_expansion_p=*/true,
13765 non_constant_p);
13766 else
13767 {
13768 /* Anything else is an error. */
13769 cp_parser_error (parser, "expected initializer");
13770 init = error_mark_node;
13771 }
13772
13773 return init;
13774 }
13775
13776 /* Parse an initializer-clause.
13777
13778 initializer-clause:
13779 assignment-expression
13780 { initializer-list , [opt] }
13781 { }
13782
13783 Returns an expression representing the initializer.
13784
13785 If the `assignment-expression' production is used the value
13786 returned is simply a representation for the expression.
13787
13788 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13789 the elements of the initializer-list (or NULL, if the last
13790 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13791 NULL_TREE. There is no way to detect whether or not the optional
13792 trailing `,' was provided. NON_CONSTANT_P is as for
13793 cp_parser_initializer. */
13794
13795 static tree
13796 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13797 {
13798 tree initializer;
13799
13800 /* Assume the expression is constant. */
13801 *non_constant_p = false;
13802
13803 /* If it is not a `{', then we are looking at an
13804 assignment-expression. */
13805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13806 {
13807 initializer
13808 = cp_parser_constant_expression (parser,
13809 /*allow_non_constant_p=*/true,
13810 non_constant_p);
13811 if (!*non_constant_p)
13812 initializer = fold_non_dependent_expr (initializer);
13813 }
13814 else
13815 {
13816 /* Consume the `{' token. */
13817 cp_lexer_consume_token (parser->lexer);
13818 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13819 initializer = make_node (CONSTRUCTOR);
13820 /* If it's not a `}', then there is a non-trivial initializer. */
13821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13822 {
13823 /* Parse the initializer list. */
13824 CONSTRUCTOR_ELTS (initializer)
13825 = cp_parser_initializer_list (parser, non_constant_p);
13826 /* A trailing `,' token is allowed. */
13827 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13828 cp_lexer_consume_token (parser->lexer);
13829 }
13830 /* Now, there should be a trailing `}'. */
13831 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13832 }
13833
13834 return initializer;
13835 }
13836
13837 /* Parse an initializer-list.
13838
13839 initializer-list:
13840 initializer-clause ... [opt]
13841 initializer-list , initializer-clause ... [opt]
13842
13843 GNU Extension:
13844
13845 initializer-list:
13846 identifier : initializer-clause
13847 initializer-list, identifier : initializer-clause
13848
13849 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13850 for the initializer. If the INDEX of the elt is non-NULL, it is the
13851 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13852 as for cp_parser_initializer. */
13853
13854 static VEC(constructor_elt,gc) *
13855 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13856 {
13857 VEC(constructor_elt,gc) *v = NULL;
13858
13859 /* Assume all of the expressions are constant. */
13860 *non_constant_p = false;
13861
13862 /* Parse the rest of the list. */
13863 while (true)
13864 {
13865 cp_token *token;
13866 tree identifier;
13867 tree initializer;
13868 bool clause_non_constant_p;
13869
13870 /* If the next token is an identifier and the following one is a
13871 colon, we are looking at the GNU designated-initializer
13872 syntax. */
13873 if (cp_parser_allow_gnu_extensions_p (parser)
13874 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13875 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13876 {
13877 /* Warn the user that they are using an extension. */
13878 if (pedantic)
13879 pedwarn ("ISO C++ does not allow designated initializers");
13880 /* Consume the identifier. */
13881 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13882 /* Consume the `:'. */
13883 cp_lexer_consume_token (parser->lexer);
13884 }
13885 else
13886 identifier = NULL_TREE;
13887
13888 /* Parse the initializer. */
13889 initializer = cp_parser_initializer_clause (parser,
13890 &clause_non_constant_p);
13891 /* If any clause is non-constant, so is the entire initializer. */
13892 if (clause_non_constant_p)
13893 *non_constant_p = true;
13894
13895 /* If we have an ellipsis, this is an initializer pack
13896 expansion. */
13897 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13898 {
13899 /* Consume the `...'. */
13900 cp_lexer_consume_token (parser->lexer);
13901
13902 /* Turn the initializer into an initializer expansion. */
13903 initializer = make_pack_expansion (initializer);
13904 }
13905
13906 /* Add it to the vector. */
13907 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13908
13909 /* If the next token is not a comma, we have reached the end of
13910 the list. */
13911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13912 break;
13913
13914 /* Peek at the next token. */
13915 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13916 /* If the next token is a `}', then we're still done. An
13917 initializer-clause can have a trailing `,' after the
13918 initializer-list and before the closing `}'. */
13919 if (token->type == CPP_CLOSE_BRACE)
13920 break;
13921
13922 /* Consume the `,' token. */
13923 cp_lexer_consume_token (parser->lexer);
13924 }
13925
13926 return v;
13927 }
13928
13929 /* Classes [gram.class] */
13930
13931 /* Parse a class-name.
13932
13933 class-name:
13934 identifier
13935 template-id
13936
13937 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13938 to indicate that names looked up in dependent types should be
13939 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13940 keyword has been used to indicate that the name that appears next
13941 is a template. TAG_TYPE indicates the explicit tag given before
13942 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13943 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13944 is the class being defined in a class-head.
13945
13946 Returns the TYPE_DECL representing the class. */
13947
13948 static tree
13949 cp_parser_class_name (cp_parser *parser,
13950 bool typename_keyword_p,
13951 bool template_keyword_p,
13952 enum tag_types tag_type,
13953 bool check_dependency_p,
13954 bool class_head_p,
13955 bool is_declaration)
13956 {
13957 tree decl;
13958 tree scope;
13959 bool typename_p;
13960 cp_token *token;
13961
13962 /* All class-names start with an identifier. */
13963 token = cp_lexer_peek_token (parser->lexer);
13964 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13965 {
13966 cp_parser_error (parser, "expected class-name");
13967 return error_mark_node;
13968 }
13969
13970 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13971 to a template-id, so we save it here. */
13972 scope = parser->scope;
13973 if (scope == error_mark_node)
13974 return error_mark_node;
13975
13976 /* Any name names a type if we're following the `typename' keyword
13977 in a qualified name where the enclosing scope is type-dependent. */
13978 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13979 && dependent_type_p (scope));
13980 /* Handle the common case (an identifier, but not a template-id)
13981 efficiently. */
13982 if (token->type == CPP_NAME
13983 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13984 {
13985 cp_token *identifier_token;
13986 tree identifier;
13987 bool ambiguous_p;
13988
13989 /* Look for the identifier. */
13990 identifier_token = cp_lexer_peek_token (parser->lexer);
13991 ambiguous_p = identifier_token->ambiguous_p;
13992 identifier = cp_parser_identifier (parser);
13993 /* If the next token isn't an identifier, we are certainly not
13994 looking at a class-name. */
13995 if (identifier == error_mark_node)
13996 decl = error_mark_node;
13997 /* If we know this is a type-name, there's no need to look it
13998 up. */
13999 else if (typename_p)
14000 decl = identifier;
14001 else
14002 {
14003 tree ambiguous_decls;
14004 /* If we already know that this lookup is ambiguous, then
14005 we've already issued an error message; there's no reason
14006 to check again. */
14007 if (ambiguous_p)
14008 {
14009 cp_parser_simulate_error (parser);
14010 return error_mark_node;
14011 }
14012 /* If the next token is a `::', then the name must be a type
14013 name.
14014
14015 [basic.lookup.qual]
14016
14017 During the lookup for a name preceding the :: scope
14018 resolution operator, object, function, and enumerator
14019 names are ignored. */
14020 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14021 tag_type = typename_type;
14022 /* Look up the name. */
14023 decl = cp_parser_lookup_name (parser, identifier,
14024 tag_type,
14025 /*is_template=*/false,
14026 /*is_namespace=*/false,
14027 check_dependency_p,
14028 &ambiguous_decls);
14029 if (ambiguous_decls)
14030 {
14031 error ("reference to %qD is ambiguous", identifier);
14032 print_candidates (ambiguous_decls);
14033 if (cp_parser_parsing_tentatively (parser))
14034 {
14035 identifier_token->ambiguous_p = true;
14036 cp_parser_simulate_error (parser);
14037 }
14038 return error_mark_node;
14039 }
14040 }
14041 }
14042 else
14043 {
14044 /* Try a template-id. */
14045 decl = cp_parser_template_id (parser, template_keyword_p,
14046 check_dependency_p,
14047 is_declaration);
14048 if (decl == error_mark_node)
14049 return error_mark_node;
14050 }
14051
14052 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14053
14054 /* If this is a typename, create a TYPENAME_TYPE. */
14055 if (typename_p && decl != error_mark_node)
14056 {
14057 decl = make_typename_type (scope, decl, typename_type,
14058 /*complain=*/tf_error);
14059 if (decl != error_mark_node)
14060 decl = TYPE_NAME (decl);
14061 }
14062
14063 /* Check to see that it is really the name of a class. */
14064 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14065 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14066 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14067 /* Situations like this:
14068
14069 template <typename T> struct A {
14070 typename T::template X<int>::I i;
14071 };
14072
14073 are problematic. Is `T::template X<int>' a class-name? The
14074 standard does not seem to be definitive, but there is no other
14075 valid interpretation of the following `::'. Therefore, those
14076 names are considered class-names. */
14077 {
14078 decl = make_typename_type (scope, decl, tag_type, tf_error);
14079 if (decl != error_mark_node)
14080 decl = TYPE_NAME (decl);
14081 }
14082 else if (TREE_CODE (decl) != TYPE_DECL
14083 || TREE_TYPE (decl) == error_mark_node
14084 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14085 decl = error_mark_node;
14086
14087 if (decl == error_mark_node)
14088 cp_parser_error (parser, "expected class-name");
14089
14090 return decl;
14091 }
14092
14093 /* Parse a class-specifier.
14094
14095 class-specifier:
14096 class-head { member-specification [opt] }
14097
14098 Returns the TREE_TYPE representing the class. */
14099
14100 static tree
14101 cp_parser_class_specifier (cp_parser* parser)
14102 {
14103 cp_token *token;
14104 tree type;
14105 tree attributes = NULL_TREE;
14106 int has_trailing_semicolon;
14107 bool nested_name_specifier_p;
14108 unsigned saved_num_template_parameter_lists;
14109 bool saved_in_function_body;
14110 tree old_scope = NULL_TREE;
14111 tree scope = NULL_TREE;
14112 tree bases;
14113
14114 push_deferring_access_checks (dk_no_deferred);
14115
14116 /* Parse the class-head. */
14117 type = cp_parser_class_head (parser,
14118 &nested_name_specifier_p,
14119 &attributes,
14120 &bases);
14121 /* If the class-head was a semantic disaster, skip the entire body
14122 of the class. */
14123 if (!type)
14124 {
14125 cp_parser_skip_to_end_of_block_or_statement (parser);
14126 pop_deferring_access_checks ();
14127 return error_mark_node;
14128 }
14129
14130 /* Look for the `{'. */
14131 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14132 {
14133 pop_deferring_access_checks ();
14134 return error_mark_node;
14135 }
14136
14137 /* Process the base classes. If they're invalid, skip the
14138 entire class body. */
14139 if (!xref_basetypes (type, bases))
14140 {
14141 /* Consuming the closing brace yields better error messages
14142 later on. */
14143 if (cp_parser_skip_to_closing_brace (parser))
14144 cp_lexer_consume_token (parser->lexer);
14145 pop_deferring_access_checks ();
14146 return error_mark_node;
14147 }
14148
14149 /* Issue an error message if type-definitions are forbidden here. */
14150 cp_parser_check_type_definition (parser);
14151 /* Remember that we are defining one more class. */
14152 ++parser->num_classes_being_defined;
14153 /* Inside the class, surrounding template-parameter-lists do not
14154 apply. */
14155 saved_num_template_parameter_lists
14156 = parser->num_template_parameter_lists;
14157 parser->num_template_parameter_lists = 0;
14158 /* We are not in a function body. */
14159 saved_in_function_body = parser->in_function_body;
14160 parser->in_function_body = false;
14161
14162 /* Start the class. */
14163 if (nested_name_specifier_p)
14164 {
14165 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14166 old_scope = push_inner_scope (scope);
14167 }
14168 type = begin_class_definition (type, attributes);
14169
14170 if (type == error_mark_node)
14171 /* If the type is erroneous, skip the entire body of the class. */
14172 cp_parser_skip_to_closing_brace (parser);
14173 else
14174 /* Parse the member-specification. */
14175 cp_parser_member_specification_opt (parser);
14176
14177 /* Look for the trailing `}'. */
14178 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14179 /* We get better error messages by noticing a common problem: a
14180 missing trailing `;'. */
14181 token = cp_lexer_peek_token (parser->lexer);
14182 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14183 /* Look for trailing attributes to apply to this class. */
14184 if (cp_parser_allow_gnu_extensions_p (parser))
14185 attributes = cp_parser_attributes_opt (parser);
14186 if (type != error_mark_node)
14187 type = finish_struct (type, attributes);
14188 if (nested_name_specifier_p)
14189 pop_inner_scope (old_scope, scope);
14190 /* If this class is not itself within the scope of another class,
14191 then we need to parse the bodies of all of the queued function
14192 definitions. Note that the queued functions defined in a class
14193 are not always processed immediately following the
14194 class-specifier for that class. Consider:
14195
14196 struct A {
14197 struct B { void f() { sizeof (A); } };
14198 };
14199
14200 If `f' were processed before the processing of `A' were
14201 completed, there would be no way to compute the size of `A'.
14202 Note that the nesting we are interested in here is lexical --
14203 not the semantic nesting given by TYPE_CONTEXT. In particular,
14204 for:
14205
14206 struct A { struct B; };
14207 struct A::B { void f() { } };
14208
14209 there is no need to delay the parsing of `A::B::f'. */
14210 if (--parser->num_classes_being_defined == 0)
14211 {
14212 tree queue_entry;
14213 tree fn;
14214 tree class_type = NULL_TREE;
14215 tree pushed_scope = NULL_TREE;
14216
14217 /* In a first pass, parse default arguments to the functions.
14218 Then, in a second pass, parse the bodies of the functions.
14219 This two-phased approach handles cases like:
14220
14221 struct S {
14222 void f() { g(); }
14223 void g(int i = 3);
14224 };
14225
14226 */
14227 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14228 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14229 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14230 TREE_PURPOSE (parser->unparsed_functions_queues)
14231 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14232 {
14233 fn = TREE_VALUE (queue_entry);
14234 /* If there are default arguments that have not yet been processed,
14235 take care of them now. */
14236 if (class_type != TREE_PURPOSE (queue_entry))
14237 {
14238 if (pushed_scope)
14239 pop_scope (pushed_scope);
14240 class_type = TREE_PURPOSE (queue_entry);
14241 pushed_scope = push_scope (class_type);
14242 }
14243 /* Make sure that any template parameters are in scope. */
14244 maybe_begin_member_template_processing (fn);
14245 /* Parse the default argument expressions. */
14246 cp_parser_late_parsing_default_args (parser, fn);
14247 /* Remove any template parameters from the symbol table. */
14248 maybe_end_member_template_processing ();
14249 }
14250 if (pushed_scope)
14251 pop_scope (pushed_scope);
14252 /* Now parse the body of the functions. */
14253 for (TREE_VALUE (parser->unparsed_functions_queues)
14254 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14255 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14256 TREE_VALUE (parser->unparsed_functions_queues)
14257 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14258 {
14259 /* Figure out which function we need to process. */
14260 fn = TREE_VALUE (queue_entry);
14261 /* Parse the function. */
14262 cp_parser_late_parsing_for_member (parser, fn);
14263 }
14264 }
14265
14266 /* Put back any saved access checks. */
14267 pop_deferring_access_checks ();
14268
14269 /* Restore saved state. */
14270 parser->in_function_body = saved_in_function_body;
14271 parser->num_template_parameter_lists
14272 = saved_num_template_parameter_lists;
14273
14274 return type;
14275 }
14276
14277 /* Parse a class-head.
14278
14279 class-head:
14280 class-key identifier [opt] base-clause [opt]
14281 class-key nested-name-specifier identifier base-clause [opt]
14282 class-key nested-name-specifier [opt] template-id
14283 base-clause [opt]
14284
14285 GNU Extensions:
14286 class-key attributes identifier [opt] base-clause [opt]
14287 class-key attributes nested-name-specifier identifier base-clause [opt]
14288 class-key attributes nested-name-specifier [opt] template-id
14289 base-clause [opt]
14290
14291 Upon return BASES is initialized to the list of base classes (or
14292 NULL, if there are none) in the same form returned by
14293 cp_parser_base_clause.
14294
14295 Returns the TYPE of the indicated class. Sets
14296 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14297 involving a nested-name-specifier was used, and FALSE otherwise.
14298
14299 Returns error_mark_node if this is not a class-head.
14300
14301 Returns NULL_TREE if the class-head is syntactically valid, but
14302 semantically invalid in a way that means we should skip the entire
14303 body of the class. */
14304
14305 static tree
14306 cp_parser_class_head (cp_parser* parser,
14307 bool* nested_name_specifier_p,
14308 tree *attributes_p,
14309 tree *bases)
14310 {
14311 tree nested_name_specifier;
14312 enum tag_types class_key;
14313 tree id = NULL_TREE;
14314 tree type = NULL_TREE;
14315 tree attributes;
14316 bool template_id_p = false;
14317 bool qualified_p = false;
14318 bool invalid_nested_name_p = false;
14319 bool invalid_explicit_specialization_p = false;
14320 tree pushed_scope = NULL_TREE;
14321 unsigned num_templates;
14322
14323 /* Assume no nested-name-specifier will be present. */
14324 *nested_name_specifier_p = false;
14325 /* Assume no template parameter lists will be used in defining the
14326 type. */
14327 num_templates = 0;
14328
14329 *bases = NULL_TREE;
14330
14331 /* Look for the class-key. */
14332 class_key = cp_parser_class_key (parser);
14333 if (class_key == none_type)
14334 return error_mark_node;
14335
14336 /* Parse the attributes. */
14337 attributes = cp_parser_attributes_opt (parser);
14338
14339 /* If the next token is `::', that is invalid -- but sometimes
14340 people do try to write:
14341
14342 struct ::S {};
14343
14344 Handle this gracefully by accepting the extra qualifier, and then
14345 issuing an error about it later if this really is a
14346 class-head. If it turns out just to be an elaborated type
14347 specifier, remain silent. */
14348 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14349 qualified_p = true;
14350
14351 push_deferring_access_checks (dk_no_check);
14352
14353 /* Determine the name of the class. Begin by looking for an
14354 optional nested-name-specifier. */
14355 nested_name_specifier
14356 = cp_parser_nested_name_specifier_opt (parser,
14357 /*typename_keyword_p=*/false,
14358 /*check_dependency_p=*/false,
14359 /*type_p=*/false,
14360 /*is_declaration=*/false);
14361 /* If there was a nested-name-specifier, then there *must* be an
14362 identifier. */
14363 if (nested_name_specifier)
14364 {
14365 /* Although the grammar says `identifier', it really means
14366 `class-name' or `template-name'. You are only allowed to
14367 define a class that has already been declared with this
14368 syntax.
14369
14370 The proposed resolution for Core Issue 180 says that wherever
14371 you see `class T::X' you should treat `X' as a type-name.
14372
14373 It is OK to define an inaccessible class; for example:
14374
14375 class A { class B; };
14376 class A::B {};
14377
14378 We do not know if we will see a class-name, or a
14379 template-name. We look for a class-name first, in case the
14380 class-name is a template-id; if we looked for the
14381 template-name first we would stop after the template-name. */
14382 cp_parser_parse_tentatively (parser);
14383 type = cp_parser_class_name (parser,
14384 /*typename_keyword_p=*/false,
14385 /*template_keyword_p=*/false,
14386 class_type,
14387 /*check_dependency_p=*/false,
14388 /*class_head_p=*/true,
14389 /*is_declaration=*/false);
14390 /* If that didn't work, ignore the nested-name-specifier. */
14391 if (!cp_parser_parse_definitely (parser))
14392 {
14393 invalid_nested_name_p = true;
14394 id = cp_parser_identifier (parser);
14395 if (id == error_mark_node)
14396 id = NULL_TREE;
14397 }
14398 /* If we could not find a corresponding TYPE, treat this
14399 declaration like an unqualified declaration. */
14400 if (type == error_mark_node)
14401 nested_name_specifier = NULL_TREE;
14402 /* Otherwise, count the number of templates used in TYPE and its
14403 containing scopes. */
14404 else
14405 {
14406 tree scope;
14407
14408 for (scope = TREE_TYPE (type);
14409 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14410 scope = (TYPE_P (scope)
14411 ? TYPE_CONTEXT (scope)
14412 : DECL_CONTEXT (scope)))
14413 if (TYPE_P (scope)
14414 && CLASS_TYPE_P (scope)
14415 && CLASSTYPE_TEMPLATE_INFO (scope)
14416 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14417 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14418 ++num_templates;
14419 }
14420 }
14421 /* Otherwise, the identifier is optional. */
14422 else
14423 {
14424 /* We don't know whether what comes next is a template-id,
14425 an identifier, or nothing at all. */
14426 cp_parser_parse_tentatively (parser);
14427 /* Check for a template-id. */
14428 id = cp_parser_template_id (parser,
14429 /*template_keyword_p=*/false,
14430 /*check_dependency_p=*/true,
14431 /*is_declaration=*/true);
14432 /* If that didn't work, it could still be an identifier. */
14433 if (!cp_parser_parse_definitely (parser))
14434 {
14435 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14436 id = cp_parser_identifier (parser);
14437 else
14438 id = NULL_TREE;
14439 }
14440 else
14441 {
14442 template_id_p = true;
14443 ++num_templates;
14444 }
14445 }
14446
14447 pop_deferring_access_checks ();
14448
14449 if (id)
14450 cp_parser_check_for_invalid_template_id (parser, id);
14451
14452 /* If it's not a `:' or a `{' then we can't really be looking at a
14453 class-head, since a class-head only appears as part of a
14454 class-specifier. We have to detect this situation before calling
14455 xref_tag, since that has irreversible side-effects. */
14456 if (!cp_parser_next_token_starts_class_definition_p (parser))
14457 {
14458 cp_parser_error (parser, "expected %<{%> or %<:%>");
14459 return error_mark_node;
14460 }
14461
14462 /* At this point, we're going ahead with the class-specifier, even
14463 if some other problem occurs. */
14464 cp_parser_commit_to_tentative_parse (parser);
14465 /* Issue the error about the overly-qualified name now. */
14466 if (qualified_p)
14467 cp_parser_error (parser,
14468 "global qualification of class name is invalid");
14469 else if (invalid_nested_name_p)
14470 cp_parser_error (parser,
14471 "qualified name does not name a class");
14472 else if (nested_name_specifier)
14473 {
14474 tree scope;
14475
14476 /* Reject typedef-names in class heads. */
14477 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14478 {
14479 error ("invalid class name in declaration of %qD", type);
14480 type = NULL_TREE;
14481 goto done;
14482 }
14483
14484 /* Figure out in what scope the declaration is being placed. */
14485 scope = current_scope ();
14486 /* If that scope does not contain the scope in which the
14487 class was originally declared, the program is invalid. */
14488 if (scope && !is_ancestor (scope, nested_name_specifier))
14489 {
14490 if (at_namespace_scope_p ())
14491 error ("declaration of %qD in namespace %qD which does not "
14492 "enclose %qD", type, scope, nested_name_specifier);
14493 else
14494 error ("declaration of %qD in %qD which does not enclose %qD",
14495 type, scope, nested_name_specifier);
14496 type = NULL_TREE;
14497 goto done;
14498 }
14499 /* [dcl.meaning]
14500
14501 A declarator-id shall not be qualified exception of the
14502 definition of a ... nested class outside of its class
14503 ... [or] a the definition or explicit instantiation of a
14504 class member of a namespace outside of its namespace. */
14505 if (scope == nested_name_specifier)
14506 {
14507 pedwarn ("extra qualification ignored");
14508 nested_name_specifier = NULL_TREE;
14509 num_templates = 0;
14510 }
14511 }
14512 /* An explicit-specialization must be preceded by "template <>". If
14513 it is not, try to recover gracefully. */
14514 if (at_namespace_scope_p ()
14515 && parser->num_template_parameter_lists == 0
14516 && template_id_p)
14517 {
14518 error ("an explicit specialization must be preceded by %<template <>%>");
14519 invalid_explicit_specialization_p = true;
14520 /* Take the same action that would have been taken by
14521 cp_parser_explicit_specialization. */
14522 ++parser->num_template_parameter_lists;
14523 begin_specialization ();
14524 }
14525 /* There must be no "return" statements between this point and the
14526 end of this function; set "type "to the correct return value and
14527 use "goto done;" to return. */
14528 /* Make sure that the right number of template parameters were
14529 present. */
14530 if (!cp_parser_check_template_parameters (parser, num_templates))
14531 {
14532 /* If something went wrong, there is no point in even trying to
14533 process the class-definition. */
14534 type = NULL_TREE;
14535 goto done;
14536 }
14537
14538 /* Look up the type. */
14539 if (template_id_p)
14540 {
14541 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14542 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14543 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14544 {
14545 error ("function template %qD redeclared as a class template", id);
14546 type = error_mark_node;
14547 }
14548 else
14549 {
14550 type = TREE_TYPE (id);
14551 type = maybe_process_partial_specialization (type);
14552 }
14553 if (nested_name_specifier)
14554 pushed_scope = push_scope (nested_name_specifier);
14555 }
14556 else if (nested_name_specifier)
14557 {
14558 tree class_type;
14559
14560 /* Given:
14561
14562 template <typename T> struct S { struct T };
14563 template <typename T> struct S<T>::T { };
14564
14565 we will get a TYPENAME_TYPE when processing the definition of
14566 `S::T'. We need to resolve it to the actual type before we
14567 try to define it. */
14568 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14569 {
14570 class_type = resolve_typename_type (TREE_TYPE (type),
14571 /*only_current_p=*/false);
14572 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14573 type = TYPE_NAME (class_type);
14574 else
14575 {
14576 cp_parser_error (parser, "could not resolve typename type");
14577 type = error_mark_node;
14578 }
14579 }
14580
14581 maybe_process_partial_specialization (TREE_TYPE (type));
14582 class_type = current_class_type;
14583 /* Enter the scope indicated by the nested-name-specifier. */
14584 pushed_scope = push_scope (nested_name_specifier);
14585 /* Get the canonical version of this type. */
14586 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14587 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14588 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14589 {
14590 type = push_template_decl (type);
14591 if (type == error_mark_node)
14592 {
14593 type = NULL_TREE;
14594 goto done;
14595 }
14596 }
14597
14598 type = TREE_TYPE (type);
14599 *nested_name_specifier_p = true;
14600 }
14601 else /* The name is not a nested name. */
14602 {
14603 /* If the class was unnamed, create a dummy name. */
14604 if (!id)
14605 id = make_anon_name ();
14606 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14607 parser->num_template_parameter_lists);
14608 }
14609
14610 /* Indicate whether this class was declared as a `class' or as a
14611 `struct'. */
14612 if (TREE_CODE (type) == RECORD_TYPE)
14613 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14614 cp_parser_check_class_key (class_key, type);
14615
14616 /* If this type was already complete, and we see another definition,
14617 that's an error. */
14618 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14619 {
14620 error ("redefinition of %q#T", type);
14621 error ("previous definition of %q+#T", type);
14622 type = NULL_TREE;
14623 goto done;
14624 }
14625 else if (type == error_mark_node)
14626 type = NULL_TREE;
14627
14628 /* We will have entered the scope containing the class; the names of
14629 base classes should be looked up in that context. For example:
14630
14631 struct A { struct B {}; struct C; };
14632 struct A::C : B {};
14633
14634 is valid. */
14635
14636 /* Get the list of base-classes, if there is one. */
14637 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14638 *bases = cp_parser_base_clause (parser);
14639
14640 done:
14641 /* Leave the scope given by the nested-name-specifier. We will
14642 enter the class scope itself while processing the members. */
14643 if (pushed_scope)
14644 pop_scope (pushed_scope);
14645
14646 if (invalid_explicit_specialization_p)
14647 {
14648 end_specialization ();
14649 --parser->num_template_parameter_lists;
14650 }
14651 *attributes_p = attributes;
14652 return type;
14653 }
14654
14655 /* Parse a class-key.
14656
14657 class-key:
14658 class
14659 struct
14660 union
14661
14662 Returns the kind of class-key specified, or none_type to indicate
14663 error. */
14664
14665 static enum tag_types
14666 cp_parser_class_key (cp_parser* parser)
14667 {
14668 cp_token *token;
14669 enum tag_types tag_type;
14670
14671 /* Look for the class-key. */
14672 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14673 if (!token)
14674 return none_type;
14675
14676 /* Check to see if the TOKEN is a class-key. */
14677 tag_type = cp_parser_token_is_class_key (token);
14678 if (!tag_type)
14679 cp_parser_error (parser, "expected class-key");
14680 return tag_type;
14681 }
14682
14683 /* Parse an (optional) member-specification.
14684
14685 member-specification:
14686 member-declaration member-specification [opt]
14687 access-specifier : member-specification [opt] */
14688
14689 static void
14690 cp_parser_member_specification_opt (cp_parser* parser)
14691 {
14692 while (true)
14693 {
14694 cp_token *token;
14695 enum rid keyword;
14696
14697 /* Peek at the next token. */
14698 token = cp_lexer_peek_token (parser->lexer);
14699 /* If it's a `}', or EOF then we've seen all the members. */
14700 if (token->type == CPP_CLOSE_BRACE
14701 || token->type == CPP_EOF
14702 || token->type == CPP_PRAGMA_EOL)
14703 break;
14704
14705 /* See if this token is a keyword. */
14706 keyword = token->keyword;
14707 switch (keyword)
14708 {
14709 case RID_PUBLIC:
14710 case RID_PROTECTED:
14711 case RID_PRIVATE:
14712 /* Consume the access-specifier. */
14713 cp_lexer_consume_token (parser->lexer);
14714 /* Remember which access-specifier is active. */
14715 current_access_specifier = token->u.value;
14716 /* Look for the `:'. */
14717 cp_parser_require (parser, CPP_COLON, "`:'");
14718 break;
14719
14720 default:
14721 /* Accept #pragmas at class scope. */
14722 if (token->type == CPP_PRAGMA)
14723 {
14724 cp_parser_pragma (parser, pragma_external);
14725 break;
14726 }
14727
14728 /* Otherwise, the next construction must be a
14729 member-declaration. */
14730 cp_parser_member_declaration (parser);
14731 }
14732 }
14733 }
14734
14735 /* Parse a member-declaration.
14736
14737 member-declaration:
14738 decl-specifier-seq [opt] member-declarator-list [opt] ;
14739 function-definition ; [opt]
14740 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14741 using-declaration
14742 template-declaration
14743
14744 member-declarator-list:
14745 member-declarator
14746 member-declarator-list , member-declarator
14747
14748 member-declarator:
14749 declarator pure-specifier [opt]
14750 declarator constant-initializer [opt]
14751 identifier [opt] : constant-expression
14752
14753 GNU Extensions:
14754
14755 member-declaration:
14756 __extension__ member-declaration
14757
14758 member-declarator:
14759 declarator attributes [opt] pure-specifier [opt]
14760 declarator attributes [opt] constant-initializer [opt]
14761 identifier [opt] attributes [opt] : constant-expression
14762
14763 C++0x Extensions:
14764
14765 member-declaration:
14766 static_assert-declaration */
14767
14768 static void
14769 cp_parser_member_declaration (cp_parser* parser)
14770 {
14771 cp_decl_specifier_seq decl_specifiers;
14772 tree prefix_attributes;
14773 tree decl;
14774 int declares_class_or_enum;
14775 bool friend_p;
14776 cp_token *token;
14777 int saved_pedantic;
14778
14779 /* Check for the `__extension__' keyword. */
14780 if (cp_parser_extension_opt (parser, &saved_pedantic))
14781 {
14782 /* Recurse. */
14783 cp_parser_member_declaration (parser);
14784 /* Restore the old value of the PEDANTIC flag. */
14785 pedantic = saved_pedantic;
14786
14787 return;
14788 }
14789
14790 /* Check for a template-declaration. */
14791 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14792 {
14793 /* An explicit specialization here is an error condition, and we
14794 expect the specialization handler to detect and report this. */
14795 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14796 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14797 cp_parser_explicit_specialization (parser);
14798 else
14799 cp_parser_template_declaration (parser, /*member_p=*/true);
14800
14801 return;
14802 }
14803
14804 /* Check for a using-declaration. */
14805 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14806 {
14807 /* Parse the using-declaration. */
14808 cp_parser_using_declaration (parser,
14809 /*access_declaration_p=*/false);
14810 return;
14811 }
14812
14813 /* Check for @defs. */
14814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14815 {
14816 tree ivar, member;
14817 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14818 ivar = ivar_chains;
14819 while (ivar)
14820 {
14821 member = ivar;
14822 ivar = TREE_CHAIN (member);
14823 TREE_CHAIN (member) = NULL_TREE;
14824 finish_member_declaration (member);
14825 }
14826 return;
14827 }
14828
14829 /* If the next token is `static_assert' we have a static assertion. */
14830 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14831 {
14832 cp_parser_static_assert (parser, /*member_p=*/true);
14833 return;
14834 }
14835
14836 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14837 return;
14838
14839 /* Parse the decl-specifier-seq. */
14840 cp_parser_decl_specifier_seq (parser,
14841 CP_PARSER_FLAGS_OPTIONAL,
14842 &decl_specifiers,
14843 &declares_class_or_enum);
14844 prefix_attributes = decl_specifiers.attributes;
14845 decl_specifiers.attributes = NULL_TREE;
14846 /* Check for an invalid type-name. */
14847 if (!decl_specifiers.type
14848 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14849 return;
14850 /* If there is no declarator, then the decl-specifier-seq should
14851 specify a type. */
14852 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14853 {
14854 /* If there was no decl-specifier-seq, and the next token is a
14855 `;', then we have something like:
14856
14857 struct S { ; };
14858
14859 [class.mem]
14860
14861 Each member-declaration shall declare at least one member
14862 name of the class. */
14863 if (!decl_specifiers.any_specifiers_p)
14864 {
14865 cp_token *token = cp_lexer_peek_token (parser->lexer);
14866 if (pedantic && !token->in_system_header)
14867 pedwarn ("%Hextra %<;%>", &token->location);
14868 }
14869 else
14870 {
14871 tree type;
14872
14873 /* See if this declaration is a friend. */
14874 friend_p = cp_parser_friend_p (&decl_specifiers);
14875 /* If there were decl-specifiers, check to see if there was
14876 a class-declaration. */
14877 type = check_tag_decl (&decl_specifiers);
14878 /* Nested classes have already been added to the class, but
14879 a `friend' needs to be explicitly registered. */
14880 if (friend_p)
14881 {
14882 /* If the `friend' keyword was present, the friend must
14883 be introduced with a class-key. */
14884 if (!declares_class_or_enum)
14885 error ("a class-key must be used when declaring a friend");
14886 /* In this case:
14887
14888 template <typename T> struct A {
14889 friend struct A<T>::B;
14890 };
14891
14892 A<T>::B will be represented by a TYPENAME_TYPE, and
14893 therefore not recognized by check_tag_decl. */
14894 if (!type
14895 && decl_specifiers.type
14896 && TYPE_P (decl_specifiers.type))
14897 type = decl_specifiers.type;
14898 if (!type || !TYPE_P (type))
14899 error ("friend declaration does not name a class or "
14900 "function");
14901 else
14902 make_friend_class (current_class_type, type,
14903 /*complain=*/true);
14904 }
14905 /* If there is no TYPE, an error message will already have
14906 been issued. */
14907 else if (!type || type == error_mark_node)
14908 ;
14909 /* An anonymous aggregate has to be handled specially; such
14910 a declaration really declares a data member (with a
14911 particular type), as opposed to a nested class. */
14912 else if (ANON_AGGR_TYPE_P (type))
14913 {
14914 /* Remove constructors and such from TYPE, now that we
14915 know it is an anonymous aggregate. */
14916 fixup_anonymous_aggr (type);
14917 /* And make the corresponding data member. */
14918 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14919 /* Add it to the class. */
14920 finish_member_declaration (decl);
14921 }
14922 else
14923 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14924 }
14925 }
14926 else
14927 {
14928 /* See if these declarations will be friends. */
14929 friend_p = cp_parser_friend_p (&decl_specifiers);
14930
14931 /* Keep going until we hit the `;' at the end of the
14932 declaration. */
14933 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14934 {
14935 tree attributes = NULL_TREE;
14936 tree first_attribute;
14937
14938 /* Peek at the next token. */
14939 token = cp_lexer_peek_token (parser->lexer);
14940
14941 /* Check for a bitfield declaration. */
14942 if (token->type == CPP_COLON
14943 || (token->type == CPP_NAME
14944 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14945 == CPP_COLON))
14946 {
14947 tree identifier;
14948 tree width;
14949
14950 /* Get the name of the bitfield. Note that we cannot just
14951 check TOKEN here because it may have been invalidated by
14952 the call to cp_lexer_peek_nth_token above. */
14953 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14954 identifier = cp_parser_identifier (parser);
14955 else
14956 identifier = NULL_TREE;
14957
14958 /* Consume the `:' token. */
14959 cp_lexer_consume_token (parser->lexer);
14960 /* Get the width of the bitfield. */
14961 width
14962 = cp_parser_constant_expression (parser,
14963 /*allow_non_constant=*/false,
14964 NULL);
14965
14966 /* Look for attributes that apply to the bitfield. */
14967 attributes = cp_parser_attributes_opt (parser);
14968 /* Remember which attributes are prefix attributes and
14969 which are not. */
14970 first_attribute = attributes;
14971 /* Combine the attributes. */
14972 attributes = chainon (prefix_attributes, attributes);
14973
14974 /* Create the bitfield declaration. */
14975 decl = grokbitfield (identifier
14976 ? make_id_declarator (NULL_TREE,
14977 identifier,
14978 sfk_none)
14979 : NULL,
14980 &decl_specifiers,
14981 width);
14982 /* Apply the attributes. */
14983 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14984 }
14985 else
14986 {
14987 cp_declarator *declarator;
14988 tree initializer;
14989 tree asm_specification;
14990 int ctor_dtor_or_conv_p;
14991
14992 /* Parse the declarator. */
14993 declarator
14994 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14995 &ctor_dtor_or_conv_p,
14996 /*parenthesized_p=*/NULL,
14997 /*member_p=*/true);
14998
14999 /* If something went wrong parsing the declarator, make sure
15000 that we at least consume some tokens. */
15001 if (declarator == cp_error_declarator)
15002 {
15003 /* Skip to the end of the statement. */
15004 cp_parser_skip_to_end_of_statement (parser);
15005 /* If the next token is not a semicolon, that is
15006 probably because we just skipped over the body of
15007 a function. So, we consume a semicolon if
15008 present, but do not issue an error message if it
15009 is not present. */
15010 if (cp_lexer_next_token_is (parser->lexer,
15011 CPP_SEMICOLON))
15012 cp_lexer_consume_token (parser->lexer);
15013 return;
15014 }
15015
15016 if (declares_class_or_enum & 2)
15017 cp_parser_check_for_definition_in_return_type
15018 (declarator, decl_specifiers.type);
15019
15020 /* Look for an asm-specification. */
15021 asm_specification = cp_parser_asm_specification_opt (parser);
15022 /* Look for attributes that apply to the declaration. */
15023 attributes = cp_parser_attributes_opt (parser);
15024 /* Remember which attributes are prefix attributes and
15025 which are not. */
15026 first_attribute = attributes;
15027 /* Combine the attributes. */
15028 attributes = chainon (prefix_attributes, attributes);
15029
15030 /* If it's an `=', then we have a constant-initializer or a
15031 pure-specifier. It is not correct to parse the
15032 initializer before registering the member declaration
15033 since the member declaration should be in scope while
15034 its initializer is processed. However, the rest of the
15035 front end does not yet provide an interface that allows
15036 us to handle this correctly. */
15037 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15038 {
15039 /* In [class.mem]:
15040
15041 A pure-specifier shall be used only in the declaration of
15042 a virtual function.
15043
15044 A member-declarator can contain a constant-initializer
15045 only if it declares a static member of integral or
15046 enumeration type.
15047
15048 Therefore, if the DECLARATOR is for a function, we look
15049 for a pure-specifier; otherwise, we look for a
15050 constant-initializer. When we call `grokfield', it will
15051 perform more stringent semantics checks. */
15052 if (function_declarator_p (declarator))
15053 initializer = cp_parser_pure_specifier (parser);
15054 else
15055 /* Parse the initializer. */
15056 initializer = cp_parser_constant_initializer (parser);
15057 }
15058 /* Otherwise, there is no initializer. */
15059 else
15060 initializer = NULL_TREE;
15061
15062 /* See if we are probably looking at a function
15063 definition. We are certainly not looking at a
15064 member-declarator. Calling `grokfield' has
15065 side-effects, so we must not do it unless we are sure
15066 that we are looking at a member-declarator. */
15067 if (cp_parser_token_starts_function_definition_p
15068 (cp_lexer_peek_token (parser->lexer)))
15069 {
15070 /* The grammar does not allow a pure-specifier to be
15071 used when a member function is defined. (It is
15072 possible that this fact is an oversight in the
15073 standard, since a pure function may be defined
15074 outside of the class-specifier. */
15075 if (initializer)
15076 error ("pure-specifier on function-definition");
15077 decl = cp_parser_save_member_function_body (parser,
15078 &decl_specifiers,
15079 declarator,
15080 attributes);
15081 /* If the member was not a friend, declare it here. */
15082 if (!friend_p)
15083 finish_member_declaration (decl);
15084 /* Peek at the next token. */
15085 token = cp_lexer_peek_token (parser->lexer);
15086 /* If the next token is a semicolon, consume it. */
15087 if (token->type == CPP_SEMICOLON)
15088 cp_lexer_consume_token (parser->lexer);
15089 return;
15090 }
15091 else
15092 /* Create the declaration. */
15093 decl = grokfield (declarator, &decl_specifiers,
15094 initializer, /*init_const_expr_p=*/true,
15095 asm_specification,
15096 attributes);
15097 }
15098
15099 /* Reset PREFIX_ATTRIBUTES. */
15100 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15101 attributes = TREE_CHAIN (attributes);
15102 if (attributes)
15103 TREE_CHAIN (attributes) = NULL_TREE;
15104
15105 /* If there is any qualification still in effect, clear it
15106 now; we will be starting fresh with the next declarator. */
15107 parser->scope = NULL_TREE;
15108 parser->qualifying_scope = NULL_TREE;
15109 parser->object_scope = NULL_TREE;
15110 /* If it's a `,', then there are more declarators. */
15111 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15112 cp_lexer_consume_token (parser->lexer);
15113 /* If the next token isn't a `;', then we have a parse error. */
15114 else if (cp_lexer_next_token_is_not (parser->lexer,
15115 CPP_SEMICOLON))
15116 {
15117 cp_parser_error (parser, "expected %<;%>");
15118 /* Skip tokens until we find a `;'. */
15119 cp_parser_skip_to_end_of_statement (parser);
15120
15121 break;
15122 }
15123
15124 if (decl)
15125 {
15126 /* Add DECL to the list of members. */
15127 if (!friend_p)
15128 finish_member_declaration (decl);
15129
15130 if (TREE_CODE (decl) == FUNCTION_DECL)
15131 cp_parser_save_default_args (parser, decl);
15132 }
15133 }
15134 }
15135
15136 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15137 }
15138
15139 /* Parse a pure-specifier.
15140
15141 pure-specifier:
15142 = 0
15143
15144 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15145 Otherwise, ERROR_MARK_NODE is returned. */
15146
15147 static tree
15148 cp_parser_pure_specifier (cp_parser* parser)
15149 {
15150 cp_token *token;
15151
15152 /* Look for the `=' token. */
15153 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15154 return error_mark_node;
15155 /* Look for the `0' token. */
15156 token = cp_lexer_consume_token (parser->lexer);
15157 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15158 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15159 {
15160 cp_parser_error (parser,
15161 "invalid pure specifier (only `= 0' is allowed)");
15162 cp_parser_skip_to_end_of_statement (parser);
15163 return error_mark_node;
15164 }
15165 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15166 {
15167 error ("templates may not be %<virtual%>");
15168 return error_mark_node;
15169 }
15170
15171 return integer_zero_node;
15172 }
15173
15174 /* Parse a constant-initializer.
15175
15176 constant-initializer:
15177 = constant-expression
15178
15179 Returns a representation of the constant-expression. */
15180
15181 static tree
15182 cp_parser_constant_initializer (cp_parser* parser)
15183 {
15184 /* Look for the `=' token. */
15185 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15186 return error_mark_node;
15187
15188 /* It is invalid to write:
15189
15190 struct S { static const int i = { 7 }; };
15191
15192 */
15193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15194 {
15195 cp_parser_error (parser,
15196 "a brace-enclosed initializer is not allowed here");
15197 /* Consume the opening brace. */
15198 cp_lexer_consume_token (parser->lexer);
15199 /* Skip the initializer. */
15200 cp_parser_skip_to_closing_brace (parser);
15201 /* Look for the trailing `}'. */
15202 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15203
15204 return error_mark_node;
15205 }
15206
15207 return cp_parser_constant_expression (parser,
15208 /*allow_non_constant=*/false,
15209 NULL);
15210 }
15211
15212 /* Derived classes [gram.class.derived] */
15213
15214 /* Parse a base-clause.
15215
15216 base-clause:
15217 : base-specifier-list
15218
15219 base-specifier-list:
15220 base-specifier ... [opt]
15221 base-specifier-list , base-specifier ... [opt]
15222
15223 Returns a TREE_LIST representing the base-classes, in the order in
15224 which they were declared. The representation of each node is as
15225 described by cp_parser_base_specifier.
15226
15227 In the case that no bases are specified, this function will return
15228 NULL_TREE, not ERROR_MARK_NODE. */
15229
15230 static tree
15231 cp_parser_base_clause (cp_parser* parser)
15232 {
15233 tree bases = NULL_TREE;
15234
15235 /* Look for the `:' that begins the list. */
15236 cp_parser_require (parser, CPP_COLON, "`:'");
15237
15238 /* Scan the base-specifier-list. */
15239 while (true)
15240 {
15241 cp_token *token;
15242 tree base;
15243 bool pack_expansion_p = false;
15244
15245 /* Look for the base-specifier. */
15246 base = cp_parser_base_specifier (parser);
15247 /* Look for the (optional) ellipsis. */
15248 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15249 {
15250 /* Consume the `...'. */
15251 cp_lexer_consume_token (parser->lexer);
15252
15253 pack_expansion_p = true;
15254 }
15255
15256 /* Add BASE to the front of the list. */
15257 if (base != error_mark_node)
15258 {
15259 if (pack_expansion_p)
15260 /* Make this a pack expansion type. */
15261 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15262 else
15263 check_for_bare_parameter_packs (&TREE_VALUE (base));
15264
15265 TREE_CHAIN (base) = bases;
15266 bases = base;
15267 }
15268 /* Peek at the next token. */
15269 token = cp_lexer_peek_token (parser->lexer);
15270 /* If it's not a comma, then the list is complete. */
15271 if (token->type != CPP_COMMA)
15272 break;
15273 /* Consume the `,'. */
15274 cp_lexer_consume_token (parser->lexer);
15275 }
15276
15277 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15278 base class had a qualified name. However, the next name that
15279 appears is certainly not qualified. */
15280 parser->scope = NULL_TREE;
15281 parser->qualifying_scope = NULL_TREE;
15282 parser->object_scope = NULL_TREE;
15283
15284 return nreverse (bases);
15285 }
15286
15287 /* Parse a base-specifier.
15288
15289 base-specifier:
15290 :: [opt] nested-name-specifier [opt] class-name
15291 virtual access-specifier [opt] :: [opt] nested-name-specifier
15292 [opt] class-name
15293 access-specifier virtual [opt] :: [opt] nested-name-specifier
15294 [opt] class-name
15295
15296 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15297 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15298 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15299 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15300
15301 static tree
15302 cp_parser_base_specifier (cp_parser* parser)
15303 {
15304 cp_token *token;
15305 bool done = false;
15306 bool virtual_p = false;
15307 bool duplicate_virtual_error_issued_p = false;
15308 bool duplicate_access_error_issued_p = false;
15309 bool class_scope_p, template_p;
15310 tree access = access_default_node;
15311 tree type;
15312
15313 /* Process the optional `virtual' and `access-specifier'. */
15314 while (!done)
15315 {
15316 /* Peek at the next token. */
15317 token = cp_lexer_peek_token (parser->lexer);
15318 /* Process `virtual'. */
15319 switch (token->keyword)
15320 {
15321 case RID_VIRTUAL:
15322 /* If `virtual' appears more than once, issue an error. */
15323 if (virtual_p && !duplicate_virtual_error_issued_p)
15324 {
15325 cp_parser_error (parser,
15326 "%<virtual%> specified more than once in base-specified");
15327 duplicate_virtual_error_issued_p = true;
15328 }
15329
15330 virtual_p = true;
15331
15332 /* Consume the `virtual' token. */
15333 cp_lexer_consume_token (parser->lexer);
15334
15335 break;
15336
15337 case RID_PUBLIC:
15338 case RID_PROTECTED:
15339 case RID_PRIVATE:
15340 /* If more than one access specifier appears, issue an
15341 error. */
15342 if (access != access_default_node
15343 && !duplicate_access_error_issued_p)
15344 {
15345 cp_parser_error (parser,
15346 "more than one access specifier in base-specified");
15347 duplicate_access_error_issued_p = true;
15348 }
15349
15350 access = ridpointers[(int) token->keyword];
15351
15352 /* Consume the access-specifier. */
15353 cp_lexer_consume_token (parser->lexer);
15354
15355 break;
15356
15357 default:
15358 done = true;
15359 break;
15360 }
15361 }
15362 /* It is not uncommon to see programs mechanically, erroneously, use
15363 the 'typename' keyword to denote (dependent) qualified types
15364 as base classes. */
15365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15366 {
15367 if (!processing_template_decl)
15368 error ("keyword %<typename%> not allowed outside of templates");
15369 else
15370 error ("keyword %<typename%> not allowed in this context "
15371 "(the base class is implicitly a type)");
15372 cp_lexer_consume_token (parser->lexer);
15373 }
15374
15375 /* Look for the optional `::' operator. */
15376 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15377 /* Look for the nested-name-specifier. The simplest way to
15378 implement:
15379
15380 [temp.res]
15381
15382 The keyword `typename' is not permitted in a base-specifier or
15383 mem-initializer; in these contexts a qualified name that
15384 depends on a template-parameter is implicitly assumed to be a
15385 type name.
15386
15387 is to pretend that we have seen the `typename' keyword at this
15388 point. */
15389 cp_parser_nested_name_specifier_opt (parser,
15390 /*typename_keyword_p=*/true,
15391 /*check_dependency_p=*/true,
15392 typename_type,
15393 /*is_declaration=*/true);
15394 /* If the base class is given by a qualified name, assume that names
15395 we see are type names or templates, as appropriate. */
15396 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15397 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15398
15399 /* Finally, look for the class-name. */
15400 type = cp_parser_class_name (parser,
15401 class_scope_p,
15402 template_p,
15403 typename_type,
15404 /*check_dependency_p=*/true,
15405 /*class_head_p=*/false,
15406 /*is_declaration=*/true);
15407
15408 if (type == error_mark_node)
15409 return error_mark_node;
15410
15411 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15412 }
15413
15414 /* Exception handling [gram.exception] */
15415
15416 /* Parse an (optional) exception-specification.
15417
15418 exception-specification:
15419 throw ( type-id-list [opt] )
15420
15421 Returns a TREE_LIST representing the exception-specification. The
15422 TREE_VALUE of each node is a type. */
15423
15424 static tree
15425 cp_parser_exception_specification_opt (cp_parser* parser)
15426 {
15427 cp_token *token;
15428 tree type_id_list;
15429
15430 /* Peek at the next token. */
15431 token = cp_lexer_peek_token (parser->lexer);
15432 /* If it's not `throw', then there's no exception-specification. */
15433 if (!cp_parser_is_keyword (token, RID_THROW))
15434 return NULL_TREE;
15435
15436 /* Consume the `throw'. */
15437 cp_lexer_consume_token (parser->lexer);
15438
15439 /* Look for the `('. */
15440 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15441
15442 /* Peek at the next token. */
15443 token = cp_lexer_peek_token (parser->lexer);
15444 /* If it's not a `)', then there is a type-id-list. */
15445 if (token->type != CPP_CLOSE_PAREN)
15446 {
15447 const char *saved_message;
15448
15449 /* Types may not be defined in an exception-specification. */
15450 saved_message = parser->type_definition_forbidden_message;
15451 parser->type_definition_forbidden_message
15452 = "types may not be defined in an exception-specification";
15453 /* Parse the type-id-list. */
15454 type_id_list = cp_parser_type_id_list (parser);
15455 /* Restore the saved message. */
15456 parser->type_definition_forbidden_message = saved_message;
15457 }
15458 else
15459 type_id_list = empty_except_spec;
15460
15461 /* Look for the `)'. */
15462 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15463
15464 return type_id_list;
15465 }
15466
15467 /* Parse an (optional) type-id-list.
15468
15469 type-id-list:
15470 type-id ... [opt]
15471 type-id-list , type-id ... [opt]
15472
15473 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15474 in the order that the types were presented. */
15475
15476 static tree
15477 cp_parser_type_id_list (cp_parser* parser)
15478 {
15479 tree types = NULL_TREE;
15480
15481 while (true)
15482 {
15483 cp_token *token;
15484 tree type;
15485
15486 /* Get the next type-id. */
15487 type = cp_parser_type_id (parser);
15488 /* Parse the optional ellipsis. */
15489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15490 {
15491 /* Consume the `...'. */
15492 cp_lexer_consume_token (parser->lexer);
15493
15494 /* Turn the type into a pack expansion expression. */
15495 type = make_pack_expansion (type);
15496 }
15497 /* Add it to the list. */
15498 types = add_exception_specifier (types, type, /*complain=*/1);
15499 /* Peek at the next token. */
15500 token = cp_lexer_peek_token (parser->lexer);
15501 /* If it is not a `,', we are done. */
15502 if (token->type != CPP_COMMA)
15503 break;
15504 /* Consume the `,'. */
15505 cp_lexer_consume_token (parser->lexer);
15506 }
15507
15508 return nreverse (types);
15509 }
15510
15511 /* Parse a try-block.
15512
15513 try-block:
15514 try compound-statement handler-seq */
15515
15516 static tree
15517 cp_parser_try_block (cp_parser* parser)
15518 {
15519 tree try_block;
15520
15521 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15522 try_block = begin_try_block ();
15523 cp_parser_compound_statement (parser, NULL, true);
15524 finish_try_block (try_block);
15525 cp_parser_handler_seq (parser);
15526 finish_handler_sequence (try_block);
15527
15528 return try_block;
15529 }
15530
15531 /* Parse a function-try-block.
15532
15533 function-try-block:
15534 try ctor-initializer [opt] function-body handler-seq */
15535
15536 static bool
15537 cp_parser_function_try_block (cp_parser* parser)
15538 {
15539 tree compound_stmt;
15540 tree try_block;
15541 bool ctor_initializer_p;
15542
15543 /* Look for the `try' keyword. */
15544 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15545 return false;
15546 /* Let the rest of the front end know where we are. */
15547 try_block = begin_function_try_block (&compound_stmt);
15548 /* Parse the function-body. */
15549 ctor_initializer_p
15550 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15551 /* We're done with the `try' part. */
15552 finish_function_try_block (try_block);
15553 /* Parse the handlers. */
15554 cp_parser_handler_seq (parser);
15555 /* We're done with the handlers. */
15556 finish_function_handler_sequence (try_block, compound_stmt);
15557
15558 return ctor_initializer_p;
15559 }
15560
15561 /* Parse a handler-seq.
15562
15563 handler-seq:
15564 handler handler-seq [opt] */
15565
15566 static void
15567 cp_parser_handler_seq (cp_parser* parser)
15568 {
15569 while (true)
15570 {
15571 cp_token *token;
15572
15573 /* Parse the handler. */
15574 cp_parser_handler (parser);
15575 /* Peek at the next token. */
15576 token = cp_lexer_peek_token (parser->lexer);
15577 /* If it's not `catch' then there are no more handlers. */
15578 if (!cp_parser_is_keyword (token, RID_CATCH))
15579 break;
15580 }
15581 }
15582
15583 /* Parse a handler.
15584
15585 handler:
15586 catch ( exception-declaration ) compound-statement */
15587
15588 static void
15589 cp_parser_handler (cp_parser* parser)
15590 {
15591 tree handler;
15592 tree declaration;
15593
15594 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15595 handler = begin_handler ();
15596 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15597 declaration = cp_parser_exception_declaration (parser);
15598 finish_handler_parms (declaration, handler);
15599 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15600 cp_parser_compound_statement (parser, NULL, false);
15601 finish_handler (handler);
15602 }
15603
15604 /* Parse an exception-declaration.
15605
15606 exception-declaration:
15607 type-specifier-seq declarator
15608 type-specifier-seq abstract-declarator
15609 type-specifier-seq
15610 ...
15611
15612 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15613 ellipsis variant is used. */
15614
15615 static tree
15616 cp_parser_exception_declaration (cp_parser* parser)
15617 {
15618 cp_decl_specifier_seq type_specifiers;
15619 cp_declarator *declarator;
15620 const char *saved_message;
15621
15622 /* If it's an ellipsis, it's easy to handle. */
15623 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15624 {
15625 /* Consume the `...' token. */
15626 cp_lexer_consume_token (parser->lexer);
15627 return NULL_TREE;
15628 }
15629
15630 /* Types may not be defined in exception-declarations. */
15631 saved_message = parser->type_definition_forbidden_message;
15632 parser->type_definition_forbidden_message
15633 = "types may not be defined in exception-declarations";
15634
15635 /* Parse the type-specifier-seq. */
15636 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15637 &type_specifiers);
15638 /* If it's a `)', then there is no declarator. */
15639 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15640 declarator = NULL;
15641 else
15642 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15643 /*ctor_dtor_or_conv_p=*/NULL,
15644 /*parenthesized_p=*/NULL,
15645 /*member_p=*/false);
15646
15647 /* Restore the saved message. */
15648 parser->type_definition_forbidden_message = saved_message;
15649
15650 if (!type_specifiers.any_specifiers_p)
15651 return error_mark_node;
15652
15653 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15654 }
15655
15656 /* Parse a throw-expression.
15657
15658 throw-expression:
15659 throw assignment-expression [opt]
15660
15661 Returns a THROW_EXPR representing the throw-expression. */
15662
15663 static tree
15664 cp_parser_throw_expression (cp_parser* parser)
15665 {
15666 tree expression;
15667 cp_token* token;
15668
15669 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15670 token = cp_lexer_peek_token (parser->lexer);
15671 /* Figure out whether or not there is an assignment-expression
15672 following the "throw" keyword. */
15673 if (token->type == CPP_COMMA
15674 || token->type == CPP_SEMICOLON
15675 || token->type == CPP_CLOSE_PAREN
15676 || token->type == CPP_CLOSE_SQUARE
15677 || token->type == CPP_CLOSE_BRACE
15678 || token->type == CPP_COLON)
15679 expression = NULL_TREE;
15680 else
15681 expression = cp_parser_assignment_expression (parser,
15682 /*cast_p=*/false);
15683
15684 return build_throw (expression);
15685 }
15686
15687 /* GNU Extensions */
15688
15689 /* Parse an (optional) asm-specification.
15690
15691 asm-specification:
15692 asm ( string-literal )
15693
15694 If the asm-specification is present, returns a STRING_CST
15695 corresponding to the string-literal. Otherwise, returns
15696 NULL_TREE. */
15697
15698 static tree
15699 cp_parser_asm_specification_opt (cp_parser* parser)
15700 {
15701 cp_token *token;
15702 tree asm_specification;
15703
15704 /* Peek at the next token. */
15705 token = cp_lexer_peek_token (parser->lexer);
15706 /* If the next token isn't the `asm' keyword, then there's no
15707 asm-specification. */
15708 if (!cp_parser_is_keyword (token, RID_ASM))
15709 return NULL_TREE;
15710
15711 /* Consume the `asm' token. */
15712 cp_lexer_consume_token (parser->lexer);
15713 /* Look for the `('. */
15714 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15715
15716 /* Look for the string-literal. */
15717 asm_specification = cp_parser_string_literal (parser, false, false);
15718
15719 /* Look for the `)'. */
15720 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15721
15722 return asm_specification;
15723 }
15724
15725 /* Parse an asm-operand-list.
15726
15727 asm-operand-list:
15728 asm-operand
15729 asm-operand-list , asm-operand
15730
15731 asm-operand:
15732 string-literal ( expression )
15733 [ string-literal ] string-literal ( expression )
15734
15735 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15736 each node is the expression. The TREE_PURPOSE is itself a
15737 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15738 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15739 is a STRING_CST for the string literal before the parenthesis. Returns
15740 ERROR_MARK_NODE if any of the operands are invalid. */
15741
15742 static tree
15743 cp_parser_asm_operand_list (cp_parser* parser)
15744 {
15745 tree asm_operands = NULL_TREE;
15746 bool invalid_operands = false;
15747
15748 while (true)
15749 {
15750 tree string_literal;
15751 tree expression;
15752 tree name;
15753
15754 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15755 {
15756 /* Consume the `[' token. */
15757 cp_lexer_consume_token (parser->lexer);
15758 /* Read the operand name. */
15759 name = cp_parser_identifier (parser);
15760 if (name != error_mark_node)
15761 name = build_string (IDENTIFIER_LENGTH (name),
15762 IDENTIFIER_POINTER (name));
15763 /* Look for the closing `]'. */
15764 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15765 }
15766 else
15767 name = NULL_TREE;
15768 /* Look for the string-literal. */
15769 string_literal = cp_parser_string_literal (parser, false, false);
15770
15771 /* Look for the `('. */
15772 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15773 /* Parse the expression. */
15774 expression = cp_parser_expression (parser, /*cast_p=*/false);
15775 /* Look for the `)'. */
15776 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15777
15778 if (name == error_mark_node
15779 || string_literal == error_mark_node
15780 || expression == error_mark_node)
15781 invalid_operands = true;
15782
15783 /* Add this operand to the list. */
15784 asm_operands = tree_cons (build_tree_list (name, string_literal),
15785 expression,
15786 asm_operands);
15787 /* If the next token is not a `,', there are no more
15788 operands. */
15789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15790 break;
15791 /* Consume the `,'. */
15792 cp_lexer_consume_token (parser->lexer);
15793 }
15794
15795 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15796 }
15797
15798 /* Parse an asm-clobber-list.
15799
15800 asm-clobber-list:
15801 string-literal
15802 asm-clobber-list , string-literal
15803
15804 Returns a TREE_LIST, indicating the clobbers in the order that they
15805 appeared. The TREE_VALUE of each node is a STRING_CST. */
15806
15807 static tree
15808 cp_parser_asm_clobber_list (cp_parser* parser)
15809 {
15810 tree clobbers = NULL_TREE;
15811
15812 while (true)
15813 {
15814 tree string_literal;
15815
15816 /* Look for the string literal. */
15817 string_literal = cp_parser_string_literal (parser, false, false);
15818 /* Add it to the list. */
15819 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15820 /* If the next token is not a `,', then the list is
15821 complete. */
15822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15823 break;
15824 /* Consume the `,' token. */
15825 cp_lexer_consume_token (parser->lexer);
15826 }
15827
15828 return clobbers;
15829 }
15830
15831 /* Parse an (optional) series of attributes.
15832
15833 attributes:
15834 attributes attribute
15835
15836 attribute:
15837 __attribute__ (( attribute-list [opt] ))
15838
15839 The return value is as for cp_parser_attribute_list. */
15840
15841 static tree
15842 cp_parser_attributes_opt (cp_parser* parser)
15843 {
15844 tree attributes = NULL_TREE;
15845
15846 while (true)
15847 {
15848 cp_token *token;
15849 tree attribute_list;
15850
15851 /* Peek at the next token. */
15852 token = cp_lexer_peek_token (parser->lexer);
15853 /* If it's not `__attribute__', then we're done. */
15854 if (token->keyword != RID_ATTRIBUTE)
15855 break;
15856
15857 /* Consume the `__attribute__' keyword. */
15858 cp_lexer_consume_token (parser->lexer);
15859 /* Look for the two `(' tokens. */
15860 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15861 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15862
15863 /* Peek at the next token. */
15864 token = cp_lexer_peek_token (parser->lexer);
15865 if (token->type != CPP_CLOSE_PAREN)
15866 /* Parse the attribute-list. */
15867 attribute_list = cp_parser_attribute_list (parser);
15868 else
15869 /* If the next token is a `)', then there is no attribute
15870 list. */
15871 attribute_list = NULL;
15872
15873 /* Look for the two `)' tokens. */
15874 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15875 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15876
15877 /* Add these new attributes to the list. */
15878 attributes = chainon (attributes, attribute_list);
15879 }
15880
15881 return attributes;
15882 }
15883
15884 /* Parse an attribute-list.
15885
15886 attribute-list:
15887 attribute
15888 attribute-list , attribute
15889
15890 attribute:
15891 identifier
15892 identifier ( identifier )
15893 identifier ( identifier , expression-list )
15894 identifier ( expression-list )
15895
15896 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15897 to an attribute. The TREE_PURPOSE of each node is the identifier
15898 indicating which attribute is in use. The TREE_VALUE represents
15899 the arguments, if any. */
15900
15901 static tree
15902 cp_parser_attribute_list (cp_parser* parser)
15903 {
15904 tree attribute_list = NULL_TREE;
15905 bool save_translate_strings_p = parser->translate_strings_p;
15906
15907 parser->translate_strings_p = false;
15908 while (true)
15909 {
15910 cp_token *token;
15911 tree identifier;
15912 tree attribute;
15913
15914 /* Look for the identifier. We also allow keywords here; for
15915 example `__attribute__ ((const))' is legal. */
15916 token = cp_lexer_peek_token (parser->lexer);
15917 if (token->type == CPP_NAME
15918 || token->type == CPP_KEYWORD)
15919 {
15920 tree arguments = NULL_TREE;
15921
15922 /* Consume the token. */
15923 token = cp_lexer_consume_token (parser->lexer);
15924
15925 /* Save away the identifier that indicates which attribute
15926 this is. */
15927 identifier = token->u.value;
15928 attribute = build_tree_list (identifier, NULL_TREE);
15929
15930 /* Peek at the next token. */
15931 token = cp_lexer_peek_token (parser->lexer);
15932 /* If it's an `(', then parse the attribute arguments. */
15933 if (token->type == CPP_OPEN_PAREN)
15934 {
15935 arguments = cp_parser_parenthesized_expression_list
15936 (parser, true, /*cast_p=*/false,
15937 /*allow_expansion_p=*/false,
15938 /*non_constant_p=*/NULL);
15939 /* Save the arguments away. */
15940 TREE_VALUE (attribute) = arguments;
15941 }
15942
15943 if (arguments != error_mark_node)
15944 {
15945 /* Add this attribute to the list. */
15946 TREE_CHAIN (attribute) = attribute_list;
15947 attribute_list = attribute;
15948 }
15949
15950 token = cp_lexer_peek_token (parser->lexer);
15951 }
15952 /* Now, look for more attributes. If the next token isn't a
15953 `,', we're done. */
15954 if (token->type != CPP_COMMA)
15955 break;
15956
15957 /* Consume the comma and keep going. */
15958 cp_lexer_consume_token (parser->lexer);
15959 }
15960 parser->translate_strings_p = save_translate_strings_p;
15961
15962 /* We built up the list in reverse order. */
15963 return nreverse (attribute_list);
15964 }
15965
15966 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15967 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15968 current value of the PEDANTIC flag, regardless of whether or not
15969 the `__extension__' keyword is present. The caller is responsible
15970 for restoring the value of the PEDANTIC flag. */
15971
15972 static bool
15973 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15974 {
15975 /* Save the old value of the PEDANTIC flag. */
15976 *saved_pedantic = pedantic;
15977
15978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15979 {
15980 /* Consume the `__extension__' token. */
15981 cp_lexer_consume_token (parser->lexer);
15982 /* We're not being pedantic while the `__extension__' keyword is
15983 in effect. */
15984 pedantic = 0;
15985
15986 return true;
15987 }
15988
15989 return false;
15990 }
15991
15992 /* Parse a label declaration.
15993
15994 label-declaration:
15995 __label__ label-declarator-seq ;
15996
15997 label-declarator-seq:
15998 identifier , label-declarator-seq
15999 identifier */
16000
16001 static void
16002 cp_parser_label_declaration (cp_parser* parser)
16003 {
16004 /* Look for the `__label__' keyword. */
16005 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16006
16007 while (true)
16008 {
16009 tree identifier;
16010
16011 /* Look for an identifier. */
16012 identifier = cp_parser_identifier (parser);
16013 /* If we failed, stop. */
16014 if (identifier == error_mark_node)
16015 break;
16016 /* Declare it as a label. */
16017 finish_label_decl (identifier);
16018 /* If the next token is a `;', stop. */
16019 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16020 break;
16021 /* Look for the `,' separating the label declarations. */
16022 cp_parser_require (parser, CPP_COMMA, "`,'");
16023 }
16024
16025 /* Look for the final `;'. */
16026 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16027 }
16028
16029 /* Support Functions */
16030
16031 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16032 NAME should have one of the representations used for an
16033 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16034 is returned. If PARSER->SCOPE is a dependent type, then a
16035 SCOPE_REF is returned.
16036
16037 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16038 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16039 was formed. Abstractly, such entities should not be passed to this
16040 function, because they do not need to be looked up, but it is
16041 simpler to check for this special case here, rather than at the
16042 call-sites.
16043
16044 In cases not explicitly covered above, this function returns a
16045 DECL, OVERLOAD, or baselink representing the result of the lookup.
16046 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16047 is returned.
16048
16049 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16050 (e.g., "struct") that was used. In that case bindings that do not
16051 refer to types are ignored.
16052
16053 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16054 ignored.
16055
16056 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16057 are ignored.
16058
16059 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16060 types.
16061
16062 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16063 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16064 NULL_TREE otherwise. */
16065
16066 static tree
16067 cp_parser_lookup_name (cp_parser *parser, tree name,
16068 enum tag_types tag_type,
16069 bool is_template,
16070 bool is_namespace,
16071 bool check_dependency,
16072 tree *ambiguous_decls)
16073 {
16074 int flags = 0;
16075 tree decl;
16076 tree object_type = parser->context->object_type;
16077
16078 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16079 flags |= LOOKUP_COMPLAIN;
16080
16081 /* Assume that the lookup will be unambiguous. */
16082 if (ambiguous_decls)
16083 *ambiguous_decls = NULL_TREE;
16084
16085 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16086 no longer valid. Note that if we are parsing tentatively, and
16087 the parse fails, OBJECT_TYPE will be automatically restored. */
16088 parser->context->object_type = NULL_TREE;
16089
16090 if (name == error_mark_node)
16091 return error_mark_node;
16092
16093 /* A template-id has already been resolved; there is no lookup to
16094 do. */
16095 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16096 return name;
16097 if (BASELINK_P (name))
16098 {
16099 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16100 == TEMPLATE_ID_EXPR);
16101 return name;
16102 }
16103
16104 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16105 it should already have been checked to make sure that the name
16106 used matches the type being destroyed. */
16107 if (TREE_CODE (name) == BIT_NOT_EXPR)
16108 {
16109 tree type;
16110
16111 /* Figure out to which type this destructor applies. */
16112 if (parser->scope)
16113 type = parser->scope;
16114 else if (object_type)
16115 type = object_type;
16116 else
16117 type = current_class_type;
16118 /* If that's not a class type, there is no destructor. */
16119 if (!type || !CLASS_TYPE_P (type))
16120 return error_mark_node;
16121 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16122 lazily_declare_fn (sfk_destructor, type);
16123 if (!CLASSTYPE_DESTRUCTORS (type))
16124 return error_mark_node;
16125 /* If it was a class type, return the destructor. */
16126 return CLASSTYPE_DESTRUCTORS (type);
16127 }
16128
16129 /* By this point, the NAME should be an ordinary identifier. If
16130 the id-expression was a qualified name, the qualifying scope is
16131 stored in PARSER->SCOPE at this point. */
16132 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16133
16134 /* Perform the lookup. */
16135 if (parser->scope)
16136 {
16137 bool dependent_p;
16138
16139 if (parser->scope == error_mark_node)
16140 return error_mark_node;
16141
16142 /* If the SCOPE is dependent, the lookup must be deferred until
16143 the template is instantiated -- unless we are explicitly
16144 looking up names in uninstantiated templates. Even then, we
16145 cannot look up the name if the scope is not a class type; it
16146 might, for example, be a template type parameter. */
16147 dependent_p = (TYPE_P (parser->scope)
16148 && !(parser->in_declarator_p
16149 && currently_open_class (parser->scope))
16150 && dependent_type_p (parser->scope));
16151 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16152 && dependent_p)
16153 {
16154 if (tag_type)
16155 {
16156 tree type;
16157
16158 /* The resolution to Core Issue 180 says that `struct
16159 A::B' should be considered a type-name, even if `A'
16160 is dependent. */
16161 type = make_typename_type (parser->scope, name, tag_type,
16162 /*complain=*/tf_error);
16163 decl = TYPE_NAME (type);
16164 }
16165 else if (is_template
16166 && (cp_parser_next_token_ends_template_argument_p (parser)
16167 || cp_lexer_next_token_is (parser->lexer,
16168 CPP_CLOSE_PAREN)))
16169 decl = make_unbound_class_template (parser->scope,
16170 name, NULL_TREE,
16171 /*complain=*/tf_error);
16172 else
16173 decl = build_qualified_name (/*type=*/NULL_TREE,
16174 parser->scope, name,
16175 is_template);
16176 }
16177 else
16178 {
16179 tree pushed_scope = NULL_TREE;
16180
16181 /* If PARSER->SCOPE is a dependent type, then it must be a
16182 class type, and we must not be checking dependencies;
16183 otherwise, we would have processed this lookup above. So
16184 that PARSER->SCOPE is not considered a dependent base by
16185 lookup_member, we must enter the scope here. */
16186 if (dependent_p)
16187 pushed_scope = push_scope (parser->scope);
16188 /* If the PARSER->SCOPE is a template specialization, it
16189 may be instantiated during name lookup. In that case,
16190 errors may be issued. Even if we rollback the current
16191 tentative parse, those errors are valid. */
16192 decl = lookup_qualified_name (parser->scope, name,
16193 tag_type != none_type,
16194 /*complain=*/true);
16195 if (pushed_scope)
16196 pop_scope (pushed_scope);
16197 }
16198 parser->qualifying_scope = parser->scope;
16199 parser->object_scope = NULL_TREE;
16200 }
16201 else if (object_type)
16202 {
16203 tree object_decl = NULL_TREE;
16204 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16205 OBJECT_TYPE is not a class. */
16206 if (CLASS_TYPE_P (object_type))
16207 /* If the OBJECT_TYPE is a template specialization, it may
16208 be instantiated during name lookup. In that case, errors
16209 may be issued. Even if we rollback the current tentative
16210 parse, those errors are valid. */
16211 object_decl = lookup_member (object_type,
16212 name,
16213 /*protect=*/0,
16214 tag_type != none_type);
16215 /* Look it up in the enclosing context, too. */
16216 decl = lookup_name_real (name, tag_type != none_type,
16217 /*nonclass=*/0,
16218 /*block_p=*/true, is_namespace, flags);
16219 parser->object_scope = object_type;
16220 parser->qualifying_scope = NULL_TREE;
16221 if (object_decl)
16222 decl = object_decl;
16223 }
16224 else
16225 {
16226 decl = lookup_name_real (name, tag_type != none_type,
16227 /*nonclass=*/0,
16228 /*block_p=*/true, is_namespace, flags);
16229 parser->qualifying_scope = NULL_TREE;
16230 parser->object_scope = NULL_TREE;
16231 }
16232
16233 /* If the lookup failed, let our caller know. */
16234 if (!decl || decl == error_mark_node)
16235 return error_mark_node;
16236
16237 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16238 if (TREE_CODE (decl) == TREE_LIST)
16239 {
16240 if (ambiguous_decls)
16241 *ambiguous_decls = decl;
16242 /* The error message we have to print is too complicated for
16243 cp_parser_error, so we incorporate its actions directly. */
16244 if (!cp_parser_simulate_error (parser))
16245 {
16246 error ("reference to %qD is ambiguous", name);
16247 print_candidates (decl);
16248 }
16249 return error_mark_node;
16250 }
16251
16252 gcc_assert (DECL_P (decl)
16253 || TREE_CODE (decl) == OVERLOAD
16254 || TREE_CODE (decl) == SCOPE_REF
16255 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16256 || BASELINK_P (decl));
16257
16258 /* If we have resolved the name of a member declaration, check to
16259 see if the declaration is accessible. When the name resolves to
16260 set of overloaded functions, accessibility is checked when
16261 overload resolution is done.
16262
16263 During an explicit instantiation, access is not checked at all,
16264 as per [temp.explicit]. */
16265 if (DECL_P (decl))
16266 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16267
16268 return decl;
16269 }
16270
16271 /* Like cp_parser_lookup_name, but for use in the typical case where
16272 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16273 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16274
16275 static tree
16276 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16277 {
16278 return cp_parser_lookup_name (parser, name,
16279 none_type,
16280 /*is_template=*/false,
16281 /*is_namespace=*/false,
16282 /*check_dependency=*/true,
16283 /*ambiguous_decls=*/NULL);
16284 }
16285
16286 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16287 the current context, return the TYPE_DECL. If TAG_NAME_P is
16288 true, the DECL indicates the class being defined in a class-head,
16289 or declared in an elaborated-type-specifier.
16290
16291 Otherwise, return DECL. */
16292
16293 static tree
16294 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16295 {
16296 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16297 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16298
16299 struct A {
16300 template <typename T> struct B;
16301 };
16302
16303 template <typename T> struct A::B {};
16304
16305 Similarly, in an elaborated-type-specifier:
16306
16307 namespace N { struct X{}; }
16308
16309 struct A {
16310 template <typename T> friend struct N::X;
16311 };
16312
16313 However, if the DECL refers to a class type, and we are in
16314 the scope of the class, then the name lookup automatically
16315 finds the TYPE_DECL created by build_self_reference rather
16316 than a TEMPLATE_DECL. For example, in:
16317
16318 template <class T> struct S {
16319 S s;
16320 };
16321
16322 there is no need to handle such case. */
16323
16324 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16325 return DECL_TEMPLATE_RESULT (decl);
16326
16327 return decl;
16328 }
16329
16330 /* If too many, or too few, template-parameter lists apply to the
16331 declarator, issue an error message. Returns TRUE if all went well,
16332 and FALSE otherwise. */
16333
16334 static bool
16335 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16336 cp_declarator *declarator)
16337 {
16338 unsigned num_templates;
16339
16340 /* We haven't seen any classes that involve template parameters yet. */
16341 num_templates = 0;
16342
16343 switch (declarator->kind)
16344 {
16345 case cdk_id:
16346 if (declarator->u.id.qualifying_scope)
16347 {
16348 tree scope;
16349 tree member;
16350
16351 scope = declarator->u.id.qualifying_scope;
16352 member = declarator->u.id.unqualified_name;
16353
16354 while (scope && CLASS_TYPE_P (scope))
16355 {
16356 /* You're supposed to have one `template <...>'
16357 for every template class, but you don't need one
16358 for a full specialization. For example:
16359
16360 template <class T> struct S{};
16361 template <> struct S<int> { void f(); };
16362 void S<int>::f () {}
16363
16364 is correct; there shouldn't be a `template <>' for
16365 the definition of `S<int>::f'. */
16366 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16367 /* If SCOPE does not have template information of any
16368 kind, then it is not a template, nor is it nested
16369 within a template. */
16370 break;
16371 if (explicit_class_specialization_p (scope))
16372 break;
16373 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16374 ++num_templates;
16375
16376 scope = TYPE_CONTEXT (scope);
16377 }
16378 }
16379 else if (TREE_CODE (declarator->u.id.unqualified_name)
16380 == TEMPLATE_ID_EXPR)
16381 /* If the DECLARATOR has the form `X<y>' then it uses one
16382 additional level of template parameters. */
16383 ++num_templates;
16384
16385 return cp_parser_check_template_parameters (parser,
16386 num_templates);
16387
16388 case cdk_function:
16389 case cdk_array:
16390 case cdk_pointer:
16391 case cdk_reference:
16392 case cdk_ptrmem:
16393 return (cp_parser_check_declarator_template_parameters
16394 (parser, declarator->declarator));
16395
16396 case cdk_error:
16397 return true;
16398
16399 default:
16400 gcc_unreachable ();
16401 }
16402 return false;
16403 }
16404
16405 /* NUM_TEMPLATES were used in the current declaration. If that is
16406 invalid, return FALSE and issue an error messages. Otherwise,
16407 return TRUE. */
16408
16409 static bool
16410 cp_parser_check_template_parameters (cp_parser* parser,
16411 unsigned num_templates)
16412 {
16413 /* If there are more template classes than parameter lists, we have
16414 something like:
16415
16416 template <class T> void S<T>::R<T>::f (); */
16417 if (parser->num_template_parameter_lists < num_templates)
16418 {
16419 error ("too few template-parameter-lists");
16420 return false;
16421 }
16422 /* If there are the same number of template classes and parameter
16423 lists, that's OK. */
16424 if (parser->num_template_parameter_lists == num_templates)
16425 return true;
16426 /* If there are more, but only one more, then we are referring to a
16427 member template. That's OK too. */
16428 if (parser->num_template_parameter_lists == num_templates + 1)
16429 return true;
16430 /* Otherwise, there are too many template parameter lists. We have
16431 something like:
16432
16433 template <class T> template <class U> void S::f(); */
16434 error ("too many template-parameter-lists");
16435 return false;
16436 }
16437
16438 /* Parse an optional `::' token indicating that the following name is
16439 from the global namespace. If so, PARSER->SCOPE is set to the
16440 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16441 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16442 Returns the new value of PARSER->SCOPE, if the `::' token is
16443 present, and NULL_TREE otherwise. */
16444
16445 static tree
16446 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16447 {
16448 cp_token *token;
16449
16450 /* Peek at the next token. */
16451 token = cp_lexer_peek_token (parser->lexer);
16452 /* If we're looking at a `::' token then we're starting from the
16453 global namespace, not our current location. */
16454 if (token->type == CPP_SCOPE)
16455 {
16456 /* Consume the `::' token. */
16457 cp_lexer_consume_token (parser->lexer);
16458 /* Set the SCOPE so that we know where to start the lookup. */
16459 parser->scope = global_namespace;
16460 parser->qualifying_scope = global_namespace;
16461 parser->object_scope = NULL_TREE;
16462
16463 return parser->scope;
16464 }
16465 else if (!current_scope_valid_p)
16466 {
16467 parser->scope = NULL_TREE;
16468 parser->qualifying_scope = NULL_TREE;
16469 parser->object_scope = NULL_TREE;
16470 }
16471
16472 return NULL_TREE;
16473 }
16474
16475 /* Returns TRUE if the upcoming token sequence is the start of a
16476 constructor declarator. If FRIEND_P is true, the declarator is
16477 preceded by the `friend' specifier. */
16478
16479 static bool
16480 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16481 {
16482 bool constructor_p;
16483 tree type_decl = NULL_TREE;
16484 bool nested_name_p;
16485 cp_token *next_token;
16486
16487 /* The common case is that this is not a constructor declarator, so
16488 try to avoid doing lots of work if at all possible. It's not
16489 valid declare a constructor at function scope. */
16490 if (parser->in_function_body)
16491 return false;
16492 /* And only certain tokens can begin a constructor declarator. */
16493 next_token = cp_lexer_peek_token (parser->lexer);
16494 if (next_token->type != CPP_NAME
16495 && next_token->type != CPP_SCOPE
16496 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16497 && next_token->type != CPP_TEMPLATE_ID)
16498 return false;
16499
16500 /* Parse tentatively; we are going to roll back all of the tokens
16501 consumed here. */
16502 cp_parser_parse_tentatively (parser);
16503 /* Assume that we are looking at a constructor declarator. */
16504 constructor_p = true;
16505
16506 /* Look for the optional `::' operator. */
16507 cp_parser_global_scope_opt (parser,
16508 /*current_scope_valid_p=*/false);
16509 /* Look for the nested-name-specifier. */
16510 nested_name_p
16511 = (cp_parser_nested_name_specifier_opt (parser,
16512 /*typename_keyword_p=*/false,
16513 /*check_dependency_p=*/false,
16514 /*type_p=*/false,
16515 /*is_declaration=*/false)
16516 != NULL_TREE);
16517 /* Outside of a class-specifier, there must be a
16518 nested-name-specifier. */
16519 if (!nested_name_p &&
16520 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16521 || friend_p))
16522 constructor_p = false;
16523 /* If we still think that this might be a constructor-declarator,
16524 look for a class-name. */
16525 if (constructor_p)
16526 {
16527 /* If we have:
16528
16529 template <typename T> struct S { S(); };
16530 template <typename T> S<T>::S ();
16531
16532 we must recognize that the nested `S' names a class.
16533 Similarly, for:
16534
16535 template <typename T> S<T>::S<T> ();
16536
16537 we must recognize that the nested `S' names a template. */
16538 type_decl = cp_parser_class_name (parser,
16539 /*typename_keyword_p=*/false,
16540 /*template_keyword_p=*/false,
16541 none_type,
16542 /*check_dependency_p=*/false,
16543 /*class_head_p=*/false,
16544 /*is_declaration=*/false);
16545 /* If there was no class-name, then this is not a constructor. */
16546 constructor_p = !cp_parser_error_occurred (parser);
16547 }
16548
16549 /* If we're still considering a constructor, we have to see a `(',
16550 to begin the parameter-declaration-clause, followed by either a
16551 `)', an `...', or a decl-specifier. We need to check for a
16552 type-specifier to avoid being fooled into thinking that:
16553
16554 S::S (f) (int);
16555
16556 is a constructor. (It is actually a function named `f' that
16557 takes one parameter (of type `int') and returns a value of type
16558 `S::S'. */
16559 if (constructor_p
16560 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16561 {
16562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16563 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16564 /* A parameter declaration begins with a decl-specifier,
16565 which is either the "attribute" keyword, a storage class
16566 specifier, or (usually) a type-specifier. */
16567 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16568 {
16569 tree type;
16570 tree pushed_scope = NULL_TREE;
16571 unsigned saved_num_template_parameter_lists;
16572
16573 /* Names appearing in the type-specifier should be looked up
16574 in the scope of the class. */
16575 if (current_class_type)
16576 type = NULL_TREE;
16577 else
16578 {
16579 type = TREE_TYPE (type_decl);
16580 if (TREE_CODE (type) == TYPENAME_TYPE)
16581 {
16582 type = resolve_typename_type (type,
16583 /*only_current_p=*/false);
16584 if (TREE_CODE (type) == TYPENAME_TYPE)
16585 {
16586 cp_parser_abort_tentative_parse (parser);
16587 return false;
16588 }
16589 }
16590 pushed_scope = push_scope (type);
16591 }
16592
16593 /* Inside the constructor parameter list, surrounding
16594 template-parameter-lists do not apply. */
16595 saved_num_template_parameter_lists
16596 = parser->num_template_parameter_lists;
16597 parser->num_template_parameter_lists = 0;
16598
16599 /* Look for the type-specifier. */
16600 cp_parser_type_specifier (parser,
16601 CP_PARSER_FLAGS_NONE,
16602 /*decl_specs=*/NULL,
16603 /*is_declarator=*/true,
16604 /*declares_class_or_enum=*/NULL,
16605 /*is_cv_qualifier=*/NULL);
16606
16607 parser->num_template_parameter_lists
16608 = saved_num_template_parameter_lists;
16609
16610 /* Leave the scope of the class. */
16611 if (pushed_scope)
16612 pop_scope (pushed_scope);
16613
16614 constructor_p = !cp_parser_error_occurred (parser);
16615 }
16616 }
16617 else
16618 constructor_p = false;
16619 /* We did not really want to consume any tokens. */
16620 cp_parser_abort_tentative_parse (parser);
16621
16622 return constructor_p;
16623 }
16624
16625 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16626 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16627 they must be performed once we are in the scope of the function.
16628
16629 Returns the function defined. */
16630
16631 static tree
16632 cp_parser_function_definition_from_specifiers_and_declarator
16633 (cp_parser* parser,
16634 cp_decl_specifier_seq *decl_specifiers,
16635 tree attributes,
16636 const cp_declarator *declarator)
16637 {
16638 tree fn;
16639 bool success_p;
16640
16641 /* Begin the function-definition. */
16642 success_p = start_function (decl_specifiers, declarator, attributes);
16643
16644 /* The things we're about to see are not directly qualified by any
16645 template headers we've seen thus far. */
16646 reset_specialization ();
16647
16648 /* If there were names looked up in the decl-specifier-seq that we
16649 did not check, check them now. We must wait until we are in the
16650 scope of the function to perform the checks, since the function
16651 might be a friend. */
16652 perform_deferred_access_checks ();
16653
16654 if (!success_p)
16655 {
16656 /* Skip the entire function. */
16657 cp_parser_skip_to_end_of_block_or_statement (parser);
16658 fn = error_mark_node;
16659 }
16660 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16661 {
16662 /* Seen already, skip it. An error message has already been output. */
16663 cp_parser_skip_to_end_of_block_or_statement (parser);
16664 fn = current_function_decl;
16665 current_function_decl = NULL_TREE;
16666 /* If this is a function from a class, pop the nested class. */
16667 if (current_class_name)
16668 pop_nested_class ();
16669 }
16670 else
16671 fn = cp_parser_function_definition_after_declarator (parser,
16672 /*inline_p=*/false);
16673
16674 return fn;
16675 }
16676
16677 /* Parse the part of a function-definition that follows the
16678 declarator. INLINE_P is TRUE iff this function is an inline
16679 function defined with a class-specifier.
16680
16681 Returns the function defined. */
16682
16683 static tree
16684 cp_parser_function_definition_after_declarator (cp_parser* parser,
16685 bool inline_p)
16686 {
16687 tree fn;
16688 bool ctor_initializer_p = false;
16689 bool saved_in_unbraced_linkage_specification_p;
16690 bool saved_in_function_body;
16691 unsigned saved_num_template_parameter_lists;
16692
16693 saved_in_function_body = parser->in_function_body;
16694 parser->in_function_body = true;
16695 /* If the next token is `return', then the code may be trying to
16696 make use of the "named return value" extension that G++ used to
16697 support. */
16698 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16699 {
16700 /* Consume the `return' keyword. */
16701 cp_lexer_consume_token (parser->lexer);
16702 /* Look for the identifier that indicates what value is to be
16703 returned. */
16704 cp_parser_identifier (parser);
16705 /* Issue an error message. */
16706 error ("named return values are no longer supported");
16707 /* Skip tokens until we reach the start of the function body. */
16708 while (true)
16709 {
16710 cp_token *token = cp_lexer_peek_token (parser->lexer);
16711 if (token->type == CPP_OPEN_BRACE
16712 || token->type == CPP_EOF
16713 || token->type == CPP_PRAGMA_EOL)
16714 break;
16715 cp_lexer_consume_token (parser->lexer);
16716 }
16717 }
16718 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16719 anything declared inside `f'. */
16720 saved_in_unbraced_linkage_specification_p
16721 = parser->in_unbraced_linkage_specification_p;
16722 parser->in_unbraced_linkage_specification_p = false;
16723 /* Inside the function, surrounding template-parameter-lists do not
16724 apply. */
16725 saved_num_template_parameter_lists
16726 = parser->num_template_parameter_lists;
16727 parser->num_template_parameter_lists = 0;
16728 /* If the next token is `try', then we are looking at a
16729 function-try-block. */
16730 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16731 ctor_initializer_p = cp_parser_function_try_block (parser);
16732 /* A function-try-block includes the function-body, so we only do
16733 this next part if we're not processing a function-try-block. */
16734 else
16735 ctor_initializer_p
16736 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16737
16738 /* Finish the function. */
16739 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16740 (inline_p ? 2 : 0));
16741 /* Generate code for it, if necessary. */
16742 expand_or_defer_fn (fn);
16743 /* Restore the saved values. */
16744 parser->in_unbraced_linkage_specification_p
16745 = saved_in_unbraced_linkage_specification_p;
16746 parser->num_template_parameter_lists
16747 = saved_num_template_parameter_lists;
16748 parser->in_function_body = saved_in_function_body;
16749
16750 return fn;
16751 }
16752
16753 /* Parse a template-declaration, assuming that the `export' (and
16754 `extern') keywords, if present, has already been scanned. MEMBER_P
16755 is as for cp_parser_template_declaration. */
16756
16757 static void
16758 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16759 {
16760 tree decl = NULL_TREE;
16761 VEC (deferred_access_check,gc) *checks;
16762 tree parameter_list;
16763 bool friend_p = false;
16764 bool need_lang_pop;
16765
16766 /* Look for the `template' keyword. */
16767 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16768 return;
16769
16770 /* And the `<'. */
16771 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16772 return;
16773 if (at_class_scope_p () && current_function_decl)
16774 {
16775 /* 14.5.2.2 [temp.mem]
16776
16777 A local class shall not have member templates. */
16778 error ("invalid declaration of member template in local class");
16779 cp_parser_skip_to_end_of_block_or_statement (parser);
16780 return;
16781 }
16782 /* [temp]
16783
16784 A template ... shall not have C linkage. */
16785 if (current_lang_name == lang_name_c)
16786 {
16787 error ("template with C linkage");
16788 /* Give it C++ linkage to avoid confusing other parts of the
16789 front end. */
16790 push_lang_context (lang_name_cplusplus);
16791 need_lang_pop = true;
16792 }
16793 else
16794 need_lang_pop = false;
16795
16796 /* We cannot perform access checks on the template parameter
16797 declarations until we know what is being declared, just as we
16798 cannot check the decl-specifier list. */
16799 push_deferring_access_checks (dk_deferred);
16800
16801 /* If the next token is `>', then we have an invalid
16802 specialization. Rather than complain about an invalid template
16803 parameter, issue an error message here. */
16804 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16805 {
16806 cp_parser_error (parser, "invalid explicit specialization");
16807 begin_specialization ();
16808 parameter_list = NULL_TREE;
16809 }
16810 else
16811 /* Parse the template parameters. */
16812 parameter_list = cp_parser_template_parameter_list (parser);
16813
16814 /* Get the deferred access checks from the parameter list. These
16815 will be checked once we know what is being declared, as for a
16816 member template the checks must be performed in the scope of the
16817 class containing the member. */
16818 checks = get_deferred_access_checks ();
16819
16820 /* Look for the `>'. */
16821 cp_parser_skip_to_end_of_template_parameter_list (parser);
16822 /* We just processed one more parameter list. */
16823 ++parser->num_template_parameter_lists;
16824 /* If the next token is `template', there are more template
16825 parameters. */
16826 if (cp_lexer_next_token_is_keyword (parser->lexer,
16827 RID_TEMPLATE))
16828 cp_parser_template_declaration_after_export (parser, member_p);
16829 else
16830 {
16831 /* There are no access checks when parsing a template, as we do not
16832 know if a specialization will be a friend. */
16833 push_deferring_access_checks (dk_no_check);
16834 decl = cp_parser_single_declaration (parser,
16835 checks,
16836 member_p,
16837 /*explicit_specialization_p=*/false,
16838 &friend_p);
16839 pop_deferring_access_checks ();
16840
16841 /* If this is a member template declaration, let the front
16842 end know. */
16843 if (member_p && !friend_p && decl)
16844 {
16845 if (TREE_CODE (decl) == TYPE_DECL)
16846 cp_parser_check_access_in_redeclaration (decl);
16847
16848 decl = finish_member_template_decl (decl);
16849 }
16850 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16851 make_friend_class (current_class_type, TREE_TYPE (decl),
16852 /*complain=*/true);
16853 }
16854 /* We are done with the current parameter list. */
16855 --parser->num_template_parameter_lists;
16856
16857 pop_deferring_access_checks ();
16858
16859 /* Finish up. */
16860 finish_template_decl (parameter_list);
16861
16862 /* Register member declarations. */
16863 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16864 finish_member_declaration (decl);
16865 /* For the erroneous case of a template with C linkage, we pushed an
16866 implicit C++ linkage scope; exit that scope now. */
16867 if (need_lang_pop)
16868 pop_lang_context ();
16869 /* If DECL is a function template, we must return to parse it later.
16870 (Even though there is no definition, there might be default
16871 arguments that need handling.) */
16872 if (member_p && decl
16873 && (TREE_CODE (decl) == FUNCTION_DECL
16874 || DECL_FUNCTION_TEMPLATE_P (decl)))
16875 TREE_VALUE (parser->unparsed_functions_queues)
16876 = tree_cons (NULL_TREE, decl,
16877 TREE_VALUE (parser->unparsed_functions_queues));
16878 }
16879
16880 /* Perform the deferred access checks from a template-parameter-list.
16881 CHECKS is a TREE_LIST of access checks, as returned by
16882 get_deferred_access_checks. */
16883
16884 static void
16885 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16886 {
16887 ++processing_template_parmlist;
16888 perform_access_checks (checks);
16889 --processing_template_parmlist;
16890 }
16891
16892 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16893 `function-definition' sequence. MEMBER_P is true, this declaration
16894 appears in a class scope.
16895
16896 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16897 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16898
16899 static tree
16900 cp_parser_single_declaration (cp_parser* parser,
16901 VEC (deferred_access_check,gc)* checks,
16902 bool member_p,
16903 bool explicit_specialization_p,
16904 bool* friend_p)
16905 {
16906 int declares_class_or_enum;
16907 tree decl = NULL_TREE;
16908 cp_decl_specifier_seq decl_specifiers;
16909 bool function_definition_p = false;
16910
16911 /* This function is only used when processing a template
16912 declaration. */
16913 gcc_assert (innermost_scope_kind () == sk_template_parms
16914 || innermost_scope_kind () == sk_template_spec);
16915
16916 /* Defer access checks until we know what is being declared. */
16917 push_deferring_access_checks (dk_deferred);
16918
16919 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16920 alternative. */
16921 cp_parser_decl_specifier_seq (parser,
16922 CP_PARSER_FLAGS_OPTIONAL,
16923 &decl_specifiers,
16924 &declares_class_or_enum);
16925 if (friend_p)
16926 *friend_p = cp_parser_friend_p (&decl_specifiers);
16927
16928 /* There are no template typedefs. */
16929 if (decl_specifiers.specs[(int) ds_typedef])
16930 {
16931 error ("template declaration of %qs", "typedef");
16932 decl = error_mark_node;
16933 }
16934
16935 /* Gather up the access checks that occurred the
16936 decl-specifier-seq. */
16937 stop_deferring_access_checks ();
16938
16939 /* Check for the declaration of a template class. */
16940 if (declares_class_or_enum)
16941 {
16942 if (cp_parser_declares_only_class_p (parser))
16943 {
16944 decl = shadow_tag (&decl_specifiers);
16945
16946 /* In this case:
16947
16948 struct C {
16949 friend template <typename T> struct A<T>::B;
16950 };
16951
16952 A<T>::B will be represented by a TYPENAME_TYPE, and
16953 therefore not recognized by shadow_tag. */
16954 if (friend_p && *friend_p
16955 && !decl
16956 && decl_specifiers.type
16957 && TYPE_P (decl_specifiers.type))
16958 decl = decl_specifiers.type;
16959
16960 if (decl && decl != error_mark_node)
16961 decl = TYPE_NAME (decl);
16962 else
16963 decl = error_mark_node;
16964
16965 /* Perform access checks for template parameters. */
16966 cp_parser_perform_template_parameter_access_checks (checks);
16967 }
16968 }
16969 /* If it's not a template class, try for a template function. If
16970 the next token is a `;', then this declaration does not declare
16971 anything. But, if there were errors in the decl-specifiers, then
16972 the error might well have come from an attempted class-specifier.
16973 In that case, there's no need to warn about a missing declarator. */
16974 if (!decl
16975 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16976 || decl_specifiers.type != error_mark_node))
16977 {
16978 decl = cp_parser_init_declarator (parser,
16979 &decl_specifiers,
16980 checks,
16981 /*function_definition_allowed_p=*/true,
16982 member_p,
16983 declares_class_or_enum,
16984 &function_definition_p);
16985
16986 /* 7.1.1-1 [dcl.stc]
16987
16988 A storage-class-specifier shall not be specified in an explicit
16989 specialization... */
16990 if (decl
16991 && explicit_specialization_p
16992 && decl_specifiers.storage_class != sc_none)
16993 {
16994 error ("explicit template specialization cannot have a storage class");
16995 decl = error_mark_node;
16996 }
16997 }
16998
16999 pop_deferring_access_checks ();
17000
17001 /* Clear any current qualification; whatever comes next is the start
17002 of something new. */
17003 parser->scope = NULL_TREE;
17004 parser->qualifying_scope = NULL_TREE;
17005 parser->object_scope = NULL_TREE;
17006 /* Look for a trailing `;' after the declaration. */
17007 if (!function_definition_p
17008 && (decl == error_mark_node
17009 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17010 cp_parser_skip_to_end_of_block_or_statement (parser);
17011
17012 return decl;
17013 }
17014
17015 /* Parse a cast-expression that is not the operand of a unary "&". */
17016
17017 static tree
17018 cp_parser_simple_cast_expression (cp_parser *parser)
17019 {
17020 return cp_parser_cast_expression (parser, /*address_p=*/false,
17021 /*cast_p=*/false);
17022 }
17023
17024 /* Parse a functional cast to TYPE. Returns an expression
17025 representing the cast. */
17026
17027 static tree
17028 cp_parser_functional_cast (cp_parser* parser, tree type)
17029 {
17030 tree expression_list;
17031 tree cast;
17032
17033 expression_list
17034 = cp_parser_parenthesized_expression_list (parser, false,
17035 /*cast_p=*/true,
17036 /*allow_expansion_p=*/true,
17037 /*non_constant_p=*/NULL);
17038
17039 cast = build_functional_cast (type, expression_list);
17040 /* [expr.const]/1: In an integral constant expression "only type
17041 conversions to integral or enumeration type can be used". */
17042 if (TREE_CODE (type) == TYPE_DECL)
17043 type = TREE_TYPE (type);
17044 if (cast != error_mark_node
17045 && !cast_valid_in_integral_constant_expression_p (type)
17046 && (cp_parser_non_integral_constant_expression
17047 (parser, "a call to a constructor")))
17048 return error_mark_node;
17049 return cast;
17050 }
17051
17052 /* Save the tokens that make up the body of a member function defined
17053 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17054 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17055 specifiers applied to the declaration. Returns the FUNCTION_DECL
17056 for the member function. */
17057
17058 static tree
17059 cp_parser_save_member_function_body (cp_parser* parser,
17060 cp_decl_specifier_seq *decl_specifiers,
17061 cp_declarator *declarator,
17062 tree attributes)
17063 {
17064 cp_token *first;
17065 cp_token *last;
17066 tree fn;
17067
17068 /* Create the function-declaration. */
17069 fn = start_method (decl_specifiers, declarator, attributes);
17070 /* If something went badly wrong, bail out now. */
17071 if (fn == error_mark_node)
17072 {
17073 /* If there's a function-body, skip it. */
17074 if (cp_parser_token_starts_function_definition_p
17075 (cp_lexer_peek_token (parser->lexer)))
17076 cp_parser_skip_to_end_of_block_or_statement (parser);
17077 return error_mark_node;
17078 }
17079
17080 /* Remember it, if there default args to post process. */
17081 cp_parser_save_default_args (parser, fn);
17082
17083 /* Save away the tokens that make up the body of the
17084 function. */
17085 first = parser->lexer->next_token;
17086 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17087 /* Handle function try blocks. */
17088 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17089 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17090 last = parser->lexer->next_token;
17091
17092 /* Save away the inline definition; we will process it when the
17093 class is complete. */
17094 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17095 DECL_PENDING_INLINE_P (fn) = 1;
17096
17097 /* We need to know that this was defined in the class, so that
17098 friend templates are handled correctly. */
17099 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17100
17101 /* We're done with the inline definition. */
17102 finish_method (fn);
17103
17104 /* Add FN to the queue of functions to be parsed later. */
17105 TREE_VALUE (parser->unparsed_functions_queues)
17106 = tree_cons (NULL_TREE, fn,
17107 TREE_VALUE (parser->unparsed_functions_queues));
17108
17109 return fn;
17110 }
17111
17112 /* Parse a template-argument-list, as well as the trailing ">" (but
17113 not the opening ">"). See cp_parser_template_argument_list for the
17114 return value. */
17115
17116 static tree
17117 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17118 {
17119 tree arguments;
17120 tree saved_scope;
17121 tree saved_qualifying_scope;
17122 tree saved_object_scope;
17123 bool saved_greater_than_is_operator_p;
17124 bool saved_skip_evaluation;
17125
17126 /* [temp.names]
17127
17128 When parsing a template-id, the first non-nested `>' is taken as
17129 the end of the template-argument-list rather than a greater-than
17130 operator. */
17131 saved_greater_than_is_operator_p
17132 = parser->greater_than_is_operator_p;
17133 parser->greater_than_is_operator_p = false;
17134 /* Parsing the argument list may modify SCOPE, so we save it
17135 here. */
17136 saved_scope = parser->scope;
17137 saved_qualifying_scope = parser->qualifying_scope;
17138 saved_object_scope = parser->object_scope;
17139 /* We need to evaluate the template arguments, even though this
17140 template-id may be nested within a "sizeof". */
17141 saved_skip_evaluation = skip_evaluation;
17142 skip_evaluation = false;
17143 /* Parse the template-argument-list itself. */
17144 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17145 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17146 arguments = NULL_TREE;
17147 else
17148 arguments = cp_parser_template_argument_list (parser);
17149 /* Look for the `>' that ends the template-argument-list. If we find
17150 a '>>' instead, it's probably just a typo. */
17151 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17152 {
17153 if (cxx_dialect != cxx98)
17154 {
17155 /* In C++0x, a `>>' in a template argument list or cast
17156 expression is considered to be two separate `>'
17157 tokens. So, change the current token to a `>', but don't
17158 consume it: it will be consumed later when the outer
17159 template argument list (or cast expression) is parsed.
17160 Note that this replacement of `>' for `>>' is necessary
17161 even if we are parsing tentatively: in the tentative
17162 case, after calling
17163 cp_parser_enclosed_template_argument_list we will always
17164 throw away all of the template arguments and the first
17165 closing `>', either because the template argument list
17166 was erroneous or because we are replacing those tokens
17167 with a CPP_TEMPLATE_ID token. The second `>' (which will
17168 not have been thrown away) is needed either to close an
17169 outer template argument list or to complete a new-style
17170 cast. */
17171 cp_token *token = cp_lexer_peek_token (parser->lexer);
17172 token->type = CPP_GREATER;
17173 }
17174 else if (!saved_greater_than_is_operator_p)
17175 {
17176 /* If we're in a nested template argument list, the '>>' has
17177 to be a typo for '> >'. We emit the error message, but we
17178 continue parsing and we push a '>' as next token, so that
17179 the argument list will be parsed correctly. Note that the
17180 global source location is still on the token before the
17181 '>>', so we need to say explicitly where we want it. */
17182 cp_token *token = cp_lexer_peek_token (parser->lexer);
17183 error ("%H%<>>%> should be %<> >%> "
17184 "within a nested template argument list",
17185 &token->location);
17186
17187 token->type = CPP_GREATER;
17188 }
17189 else
17190 {
17191 /* If this is not a nested template argument list, the '>>'
17192 is a typo for '>'. Emit an error message and continue.
17193 Same deal about the token location, but here we can get it
17194 right by consuming the '>>' before issuing the diagnostic. */
17195 cp_lexer_consume_token (parser->lexer);
17196 error ("spurious %<>>%>, use %<>%> to terminate "
17197 "a template argument list");
17198 }
17199 }
17200 else
17201 cp_parser_skip_to_end_of_template_parameter_list (parser);
17202 /* The `>' token might be a greater-than operator again now. */
17203 parser->greater_than_is_operator_p
17204 = saved_greater_than_is_operator_p;
17205 /* Restore the SAVED_SCOPE. */
17206 parser->scope = saved_scope;
17207 parser->qualifying_scope = saved_qualifying_scope;
17208 parser->object_scope = saved_object_scope;
17209 skip_evaluation = saved_skip_evaluation;
17210
17211 return arguments;
17212 }
17213
17214 /* MEMBER_FUNCTION is a member function, or a friend. If default
17215 arguments, or the body of the function have not yet been parsed,
17216 parse them now. */
17217
17218 static void
17219 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17220 {
17221 /* If this member is a template, get the underlying
17222 FUNCTION_DECL. */
17223 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17224 member_function = DECL_TEMPLATE_RESULT (member_function);
17225
17226 /* There should not be any class definitions in progress at this
17227 point; the bodies of members are only parsed outside of all class
17228 definitions. */
17229 gcc_assert (parser->num_classes_being_defined == 0);
17230 /* While we're parsing the member functions we might encounter more
17231 classes. We want to handle them right away, but we don't want
17232 them getting mixed up with functions that are currently in the
17233 queue. */
17234 parser->unparsed_functions_queues
17235 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17236
17237 /* Make sure that any template parameters are in scope. */
17238 maybe_begin_member_template_processing (member_function);
17239
17240 /* If the body of the function has not yet been parsed, parse it
17241 now. */
17242 if (DECL_PENDING_INLINE_P (member_function))
17243 {
17244 tree function_scope;
17245 cp_token_cache *tokens;
17246
17247 /* The function is no longer pending; we are processing it. */
17248 tokens = DECL_PENDING_INLINE_INFO (member_function);
17249 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17250 DECL_PENDING_INLINE_P (member_function) = 0;
17251
17252 /* If this is a local class, enter the scope of the containing
17253 function. */
17254 function_scope = current_function_decl;
17255 if (function_scope)
17256 push_function_context_to (function_scope);
17257
17258
17259 /* Push the body of the function onto the lexer stack. */
17260 cp_parser_push_lexer_for_tokens (parser, tokens);
17261
17262 /* Let the front end know that we going to be defining this
17263 function. */
17264 start_preparsed_function (member_function, NULL_TREE,
17265 SF_PRE_PARSED | SF_INCLASS_INLINE);
17266
17267 /* Don't do access checking if it is a templated function. */
17268 if (processing_template_decl)
17269 push_deferring_access_checks (dk_no_check);
17270
17271 /* Now, parse the body of the function. */
17272 cp_parser_function_definition_after_declarator (parser,
17273 /*inline_p=*/true);
17274
17275 if (processing_template_decl)
17276 pop_deferring_access_checks ();
17277
17278 /* Leave the scope of the containing function. */
17279 if (function_scope)
17280 pop_function_context_from (function_scope);
17281 cp_parser_pop_lexer (parser);
17282 }
17283
17284 /* Remove any template parameters from the symbol table. */
17285 maybe_end_member_template_processing ();
17286
17287 /* Restore the queue. */
17288 parser->unparsed_functions_queues
17289 = TREE_CHAIN (parser->unparsed_functions_queues);
17290 }
17291
17292 /* If DECL contains any default args, remember it on the unparsed
17293 functions queue. */
17294
17295 static void
17296 cp_parser_save_default_args (cp_parser* parser, tree decl)
17297 {
17298 tree probe;
17299
17300 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17301 probe;
17302 probe = TREE_CHAIN (probe))
17303 if (TREE_PURPOSE (probe))
17304 {
17305 TREE_PURPOSE (parser->unparsed_functions_queues)
17306 = tree_cons (current_class_type, decl,
17307 TREE_PURPOSE (parser->unparsed_functions_queues));
17308 break;
17309 }
17310 }
17311
17312 /* FN is a FUNCTION_DECL which may contains a parameter with an
17313 unparsed DEFAULT_ARG. Parse the default args now. This function
17314 assumes that the current scope is the scope in which the default
17315 argument should be processed. */
17316
17317 static void
17318 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17319 {
17320 bool saved_local_variables_forbidden_p;
17321 tree parm;
17322
17323 /* While we're parsing the default args, we might (due to the
17324 statement expression extension) encounter more classes. We want
17325 to handle them right away, but we don't want them getting mixed
17326 up with default args that are currently in the queue. */
17327 parser->unparsed_functions_queues
17328 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17329
17330 /* Local variable names (and the `this' keyword) may not appear
17331 in a default argument. */
17332 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17333 parser->local_variables_forbidden_p = true;
17334
17335 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17336 parm;
17337 parm = TREE_CHAIN (parm))
17338 {
17339 cp_token_cache *tokens;
17340 tree default_arg = TREE_PURPOSE (parm);
17341 tree parsed_arg;
17342 VEC(tree,gc) *insts;
17343 tree copy;
17344 unsigned ix;
17345
17346 if (!default_arg)
17347 continue;
17348
17349 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17350 /* This can happen for a friend declaration for a function
17351 already declared with default arguments. */
17352 continue;
17353
17354 /* Push the saved tokens for the default argument onto the parser's
17355 lexer stack. */
17356 tokens = DEFARG_TOKENS (default_arg);
17357 cp_parser_push_lexer_for_tokens (parser, tokens);
17358
17359 /* Parse the assignment-expression. */
17360 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17361
17362 if (!processing_template_decl)
17363 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17364
17365 TREE_PURPOSE (parm) = parsed_arg;
17366
17367 /* Update any instantiations we've already created. */
17368 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17369 VEC_iterate (tree, insts, ix, copy); ix++)
17370 TREE_PURPOSE (copy) = parsed_arg;
17371
17372 /* If the token stream has not been completely used up, then
17373 there was extra junk after the end of the default
17374 argument. */
17375 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17376 cp_parser_error (parser, "expected %<,%>");
17377
17378 /* Revert to the main lexer. */
17379 cp_parser_pop_lexer (parser);
17380 }
17381
17382 /* Make sure no default arg is missing. */
17383 check_default_args (fn);
17384
17385 /* Restore the state of local_variables_forbidden_p. */
17386 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17387
17388 /* Restore the queue. */
17389 parser->unparsed_functions_queues
17390 = TREE_CHAIN (parser->unparsed_functions_queues);
17391 }
17392
17393 /* Parse the operand of `sizeof' (or a similar operator). Returns
17394 either a TYPE or an expression, depending on the form of the
17395 input. The KEYWORD indicates which kind of expression we have
17396 encountered. */
17397
17398 static tree
17399 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17400 {
17401 static const char *format;
17402 tree expr = NULL_TREE;
17403 const char *saved_message;
17404 char *tmp;
17405 bool saved_integral_constant_expression_p;
17406 bool saved_non_integral_constant_expression_p;
17407 bool pack_expansion_p = false;
17408
17409 /* Initialize FORMAT the first time we get here. */
17410 if (!format)
17411 format = "types may not be defined in '%s' expressions";
17412
17413 /* Types cannot be defined in a `sizeof' expression. Save away the
17414 old message. */
17415 saved_message = parser->type_definition_forbidden_message;
17416 /* And create the new one. */
17417 parser->type_definition_forbidden_message = tmp
17418 = XNEWVEC (char, strlen (format)
17419 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17420 + 1 /* `\0' */);
17421 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17422
17423 /* The restrictions on constant-expressions do not apply inside
17424 sizeof expressions. */
17425 saved_integral_constant_expression_p
17426 = parser->integral_constant_expression_p;
17427 saved_non_integral_constant_expression_p
17428 = parser->non_integral_constant_expression_p;
17429 parser->integral_constant_expression_p = false;
17430
17431 /* If it's a `...', then we are computing the length of a parameter
17432 pack. */
17433 if (keyword == RID_SIZEOF
17434 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17435 {
17436 /* Consume the `...'. */
17437 cp_lexer_consume_token (parser->lexer);
17438 maybe_warn_variadic_templates ();
17439
17440 /* Note that this is an expansion. */
17441 pack_expansion_p = true;
17442 }
17443
17444 /* Do not actually evaluate the expression. */
17445 ++skip_evaluation;
17446 /* If it's a `(', then we might be looking at the type-id
17447 construction. */
17448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17449 {
17450 tree type;
17451 bool saved_in_type_id_in_expr_p;
17452
17453 /* We can't be sure yet whether we're looking at a type-id or an
17454 expression. */
17455 cp_parser_parse_tentatively (parser);
17456 /* Consume the `('. */
17457 cp_lexer_consume_token (parser->lexer);
17458 /* Parse the type-id. */
17459 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17460 parser->in_type_id_in_expr_p = true;
17461 type = cp_parser_type_id (parser);
17462 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17463 /* Now, look for the trailing `)'. */
17464 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17465 /* If all went well, then we're done. */
17466 if (cp_parser_parse_definitely (parser))
17467 {
17468 cp_decl_specifier_seq decl_specs;
17469
17470 /* Build a trivial decl-specifier-seq. */
17471 clear_decl_specs (&decl_specs);
17472 decl_specs.type = type;
17473
17474 /* Call grokdeclarator to figure out what type this is. */
17475 expr = grokdeclarator (NULL,
17476 &decl_specs,
17477 TYPENAME,
17478 /*initialized=*/0,
17479 /*attrlist=*/NULL);
17480 }
17481 }
17482
17483 /* If the type-id production did not work out, then we must be
17484 looking at the unary-expression production. */
17485 if (!expr)
17486 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17487 /*cast_p=*/false);
17488
17489 if (pack_expansion_p)
17490 /* Build a pack expansion. */
17491 expr = make_pack_expansion (expr);
17492
17493 /* Go back to evaluating expressions. */
17494 --skip_evaluation;
17495
17496 /* Free the message we created. */
17497 free (tmp);
17498 /* And restore the old one. */
17499 parser->type_definition_forbidden_message = saved_message;
17500 parser->integral_constant_expression_p
17501 = saved_integral_constant_expression_p;
17502 parser->non_integral_constant_expression_p
17503 = saved_non_integral_constant_expression_p;
17504
17505 return expr;
17506 }
17507
17508 /* If the current declaration has no declarator, return true. */
17509
17510 static bool
17511 cp_parser_declares_only_class_p (cp_parser *parser)
17512 {
17513 /* If the next token is a `;' or a `,' then there is no
17514 declarator. */
17515 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17516 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17517 }
17518
17519 /* Update the DECL_SPECS to reflect the storage class indicated by
17520 KEYWORD. */
17521
17522 static void
17523 cp_parser_set_storage_class (cp_parser *parser,
17524 cp_decl_specifier_seq *decl_specs,
17525 enum rid keyword)
17526 {
17527 cp_storage_class storage_class;
17528
17529 if (parser->in_unbraced_linkage_specification_p)
17530 {
17531 error ("invalid use of %qD in linkage specification",
17532 ridpointers[keyword]);
17533 return;
17534 }
17535 else if (decl_specs->storage_class != sc_none)
17536 {
17537 decl_specs->conflicting_specifiers_p = true;
17538 return;
17539 }
17540
17541 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17542 && decl_specs->specs[(int) ds_thread])
17543 {
17544 error ("%<__thread%> before %qD", ridpointers[keyword]);
17545 decl_specs->specs[(int) ds_thread] = 0;
17546 }
17547
17548 switch (keyword)
17549 {
17550 case RID_AUTO:
17551 storage_class = sc_auto;
17552 break;
17553 case RID_REGISTER:
17554 storage_class = sc_register;
17555 break;
17556 case RID_STATIC:
17557 storage_class = sc_static;
17558 break;
17559 case RID_EXTERN:
17560 storage_class = sc_extern;
17561 break;
17562 case RID_MUTABLE:
17563 storage_class = sc_mutable;
17564 break;
17565 default:
17566 gcc_unreachable ();
17567 }
17568 decl_specs->storage_class = storage_class;
17569
17570 /* A storage class specifier cannot be applied alongside a typedef
17571 specifier. If there is a typedef specifier present then set
17572 conflicting_specifiers_p which will trigger an error later
17573 on in grokdeclarator. */
17574 if (decl_specs->specs[(int)ds_typedef])
17575 decl_specs->conflicting_specifiers_p = true;
17576 }
17577
17578 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17579 is true, the type is a user-defined type; otherwise it is a
17580 built-in type specified by a keyword. */
17581
17582 static void
17583 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17584 tree type_spec,
17585 bool user_defined_p)
17586 {
17587 decl_specs->any_specifiers_p = true;
17588
17589 /* If the user tries to redeclare bool or wchar_t (with, for
17590 example, in "typedef int wchar_t;") we remember that this is what
17591 happened. In system headers, we ignore these declarations so
17592 that G++ can work with system headers that are not C++-safe. */
17593 if (decl_specs->specs[(int) ds_typedef]
17594 && !user_defined_p
17595 && (type_spec == boolean_type_node
17596 || type_spec == wchar_type_node)
17597 && (decl_specs->type
17598 || decl_specs->specs[(int) ds_long]
17599 || decl_specs->specs[(int) ds_short]
17600 || decl_specs->specs[(int) ds_unsigned]
17601 || decl_specs->specs[(int) ds_signed]))
17602 {
17603 decl_specs->redefined_builtin_type = type_spec;
17604 if (!decl_specs->type)
17605 {
17606 decl_specs->type = type_spec;
17607 decl_specs->user_defined_type_p = false;
17608 }
17609 }
17610 else if (decl_specs->type)
17611 decl_specs->multiple_types_p = true;
17612 else
17613 {
17614 decl_specs->type = type_spec;
17615 decl_specs->user_defined_type_p = user_defined_p;
17616 decl_specs->redefined_builtin_type = NULL_TREE;
17617 }
17618 }
17619
17620 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17621 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17622
17623 static bool
17624 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17625 {
17626 return decl_specifiers->specs[(int) ds_friend] != 0;
17627 }
17628
17629 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17630 issue an error message indicating that TOKEN_DESC was expected.
17631
17632 Returns the token consumed, if the token had the appropriate type.
17633 Otherwise, returns NULL. */
17634
17635 static cp_token *
17636 cp_parser_require (cp_parser* parser,
17637 enum cpp_ttype type,
17638 const char* token_desc)
17639 {
17640 if (cp_lexer_next_token_is (parser->lexer, type))
17641 return cp_lexer_consume_token (parser->lexer);
17642 else
17643 {
17644 /* Output the MESSAGE -- unless we're parsing tentatively. */
17645 if (!cp_parser_simulate_error (parser))
17646 {
17647 char *message = concat ("expected ", token_desc, NULL);
17648 cp_parser_error (parser, message);
17649 free (message);
17650 }
17651 return NULL;
17652 }
17653 }
17654
17655 /* An error message is produced if the next token is not '>'.
17656 All further tokens are skipped until the desired token is
17657 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17658
17659 static void
17660 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17661 {
17662 /* Current level of '< ... >'. */
17663 unsigned level = 0;
17664 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17665 unsigned nesting_depth = 0;
17666
17667 /* Are we ready, yet? If not, issue error message. */
17668 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17669 return;
17670
17671 /* Skip tokens until the desired token is found. */
17672 while (true)
17673 {
17674 /* Peek at the next token. */
17675 switch (cp_lexer_peek_token (parser->lexer)->type)
17676 {
17677 case CPP_LESS:
17678 if (!nesting_depth)
17679 ++level;
17680 break;
17681
17682 case CPP_RSHIFT:
17683 if (cxx_dialect == cxx98)
17684 /* C++0x views the `>>' operator as two `>' tokens, but
17685 C++98 does not. */
17686 break;
17687 else if (!nesting_depth && level-- == 0)
17688 {
17689 /* We've hit a `>>' where the first `>' closes the
17690 template argument list, and the second `>' is
17691 spurious. Just consume the `>>' and stop; we've
17692 already produced at least one error. */
17693 cp_lexer_consume_token (parser->lexer);
17694 return;
17695 }
17696 /* Fall through for C++0x, so we handle the second `>' in
17697 the `>>'. */
17698
17699 case CPP_GREATER:
17700 if (!nesting_depth && level-- == 0)
17701 {
17702 /* We've reached the token we want, consume it and stop. */
17703 cp_lexer_consume_token (parser->lexer);
17704 return;
17705 }
17706 break;
17707
17708 case CPP_OPEN_PAREN:
17709 case CPP_OPEN_SQUARE:
17710 ++nesting_depth;
17711 break;
17712
17713 case CPP_CLOSE_PAREN:
17714 case CPP_CLOSE_SQUARE:
17715 if (nesting_depth-- == 0)
17716 return;
17717 break;
17718
17719 case CPP_EOF:
17720 case CPP_PRAGMA_EOL:
17721 case CPP_SEMICOLON:
17722 case CPP_OPEN_BRACE:
17723 case CPP_CLOSE_BRACE:
17724 /* The '>' was probably forgotten, don't look further. */
17725 return;
17726
17727 default:
17728 break;
17729 }
17730
17731 /* Consume this token. */
17732 cp_lexer_consume_token (parser->lexer);
17733 }
17734 }
17735
17736 /* If the next token is the indicated keyword, 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_keyword (cp_parser* parser,
17744 enum rid keyword,
17745 const char* token_desc)
17746 {
17747 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17748
17749 if (token && token->keyword != keyword)
17750 {
17751 dyn_string_t error_msg;
17752
17753 /* Format the error message. */
17754 error_msg = dyn_string_new (0);
17755 dyn_string_append_cstr (error_msg, "expected ");
17756 dyn_string_append_cstr (error_msg, token_desc);
17757 cp_parser_error (parser, error_msg->s);
17758 dyn_string_delete (error_msg);
17759 return NULL;
17760 }
17761
17762 return token;
17763 }
17764
17765 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17766 function-definition. */
17767
17768 static bool
17769 cp_parser_token_starts_function_definition_p (cp_token* token)
17770 {
17771 return (/* An ordinary function-body begins with an `{'. */
17772 token->type == CPP_OPEN_BRACE
17773 /* A ctor-initializer begins with a `:'. */
17774 || token->type == CPP_COLON
17775 /* A function-try-block begins with `try'. */
17776 || token->keyword == RID_TRY
17777 /* The named return value extension begins with `return'. */
17778 || token->keyword == RID_RETURN);
17779 }
17780
17781 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17782 definition. */
17783
17784 static bool
17785 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17786 {
17787 cp_token *token;
17788
17789 token = cp_lexer_peek_token (parser->lexer);
17790 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17791 }
17792
17793 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17794 C++0x) ending a template-argument. */
17795
17796 static bool
17797 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17798 {
17799 cp_token *token;
17800
17801 token = cp_lexer_peek_token (parser->lexer);
17802 return (token->type == CPP_COMMA
17803 || token->type == CPP_GREATER
17804 || token->type == CPP_ELLIPSIS
17805 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17806 }
17807
17808 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17809 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17810
17811 static bool
17812 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17813 size_t n)
17814 {
17815 cp_token *token;
17816
17817 token = cp_lexer_peek_nth_token (parser->lexer, n);
17818 if (token->type == CPP_LESS)
17819 return true;
17820 /* Check for the sequence `<::' in the original code. It would be lexed as
17821 `[:', where `[' is a digraph, and there is no whitespace before
17822 `:'. */
17823 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17824 {
17825 cp_token *token2;
17826 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17827 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17828 return true;
17829 }
17830 return false;
17831 }
17832
17833 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17834 or none_type otherwise. */
17835
17836 static enum tag_types
17837 cp_parser_token_is_class_key (cp_token* token)
17838 {
17839 switch (token->keyword)
17840 {
17841 case RID_CLASS:
17842 return class_type;
17843 case RID_STRUCT:
17844 return record_type;
17845 case RID_UNION:
17846 return union_type;
17847
17848 default:
17849 return none_type;
17850 }
17851 }
17852
17853 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17854
17855 static void
17856 cp_parser_check_class_key (enum tag_types class_key, tree type)
17857 {
17858 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17859 pedwarn ("%qs tag used in naming %q#T",
17860 class_key == union_type ? "union"
17861 : class_key == record_type ? "struct" : "class",
17862 type);
17863 }
17864
17865 /* Issue an error message if DECL is redeclared with different
17866 access than its original declaration [class.access.spec/3].
17867 This applies to nested classes and nested class templates.
17868 [class.mem/1]. */
17869
17870 static void
17871 cp_parser_check_access_in_redeclaration (tree decl)
17872 {
17873 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17874 return;
17875
17876 if ((TREE_PRIVATE (decl)
17877 != (current_access_specifier == access_private_node))
17878 || (TREE_PROTECTED (decl)
17879 != (current_access_specifier == access_protected_node)))
17880 error ("%qD redeclared with different access", decl);
17881 }
17882
17883 /* Look for the `template' keyword, as a syntactic disambiguator.
17884 Return TRUE iff it is present, in which case it will be
17885 consumed. */
17886
17887 static bool
17888 cp_parser_optional_template_keyword (cp_parser *parser)
17889 {
17890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17891 {
17892 /* The `template' keyword can only be used within templates;
17893 outside templates the parser can always figure out what is a
17894 template and what is not. */
17895 if (!processing_template_decl)
17896 {
17897 error ("%<template%> (as a disambiguator) is only allowed "
17898 "within templates");
17899 /* If this part of the token stream is rescanned, the same
17900 error message would be generated. So, we purge the token
17901 from the stream. */
17902 cp_lexer_purge_token (parser->lexer);
17903 return false;
17904 }
17905 else
17906 {
17907 /* Consume the `template' keyword. */
17908 cp_lexer_consume_token (parser->lexer);
17909 return true;
17910 }
17911 }
17912
17913 return false;
17914 }
17915
17916 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17917 set PARSER->SCOPE, and perform other related actions. */
17918
17919 static void
17920 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17921 {
17922 int i;
17923 struct tree_check *check_value;
17924 deferred_access_check *chk;
17925 VEC (deferred_access_check,gc) *checks;
17926
17927 /* Get the stored value. */
17928 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17929 /* Perform any access checks that were deferred. */
17930 checks = check_value->checks;
17931 if (checks)
17932 {
17933 for (i = 0 ;
17934 VEC_iterate (deferred_access_check, checks, i, chk) ;
17935 ++i)
17936 {
17937 perform_or_defer_access_check (chk->binfo,
17938 chk->decl,
17939 chk->diag_decl);
17940 }
17941 }
17942 /* Set the scope from the stored value. */
17943 parser->scope = check_value->value;
17944 parser->qualifying_scope = check_value->qualifying_scope;
17945 parser->object_scope = NULL_TREE;
17946 }
17947
17948 /* Consume tokens up through a non-nested END token. */
17949
17950 static void
17951 cp_parser_cache_group (cp_parser *parser,
17952 enum cpp_ttype end,
17953 unsigned depth)
17954 {
17955 while (true)
17956 {
17957 cp_token *token;
17958
17959 /* Abort a parenthesized expression if we encounter a brace. */
17960 if ((end == CPP_CLOSE_PAREN || depth == 0)
17961 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17962 return;
17963 /* If we've reached the end of the file, stop. */
17964 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17965 || (end != CPP_PRAGMA_EOL
17966 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17967 return;
17968 /* Consume the next token. */
17969 token = cp_lexer_consume_token (parser->lexer);
17970 /* See if it starts a new group. */
17971 if (token->type == CPP_OPEN_BRACE)
17972 {
17973 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17974 if (depth == 0)
17975 return;
17976 }
17977 else if (token->type == CPP_OPEN_PAREN)
17978 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17979 else if (token->type == CPP_PRAGMA)
17980 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17981 else if (token->type == end)
17982 return;
17983 }
17984 }
17985
17986 /* Begin parsing tentatively. We always save tokens while parsing
17987 tentatively so that if the tentative parsing fails we can restore the
17988 tokens. */
17989
17990 static void
17991 cp_parser_parse_tentatively (cp_parser* parser)
17992 {
17993 /* Enter a new parsing context. */
17994 parser->context = cp_parser_context_new (parser->context);
17995 /* Begin saving tokens. */
17996 cp_lexer_save_tokens (parser->lexer);
17997 /* In order to avoid repetitive access control error messages,
17998 access checks are queued up until we are no longer parsing
17999 tentatively. */
18000 push_deferring_access_checks (dk_deferred);
18001 }
18002
18003 /* Commit to the currently active tentative parse. */
18004
18005 static void
18006 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18007 {
18008 cp_parser_context *context;
18009 cp_lexer *lexer;
18010
18011 /* Mark all of the levels as committed. */
18012 lexer = parser->lexer;
18013 for (context = parser->context; context->next; context = context->next)
18014 {
18015 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18016 break;
18017 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18018 while (!cp_lexer_saving_tokens (lexer))
18019 lexer = lexer->next;
18020 cp_lexer_commit_tokens (lexer);
18021 }
18022 }
18023
18024 /* Abort the currently active tentative parse. All consumed tokens
18025 will be rolled back, and no diagnostics will be issued. */
18026
18027 static void
18028 cp_parser_abort_tentative_parse (cp_parser* parser)
18029 {
18030 cp_parser_simulate_error (parser);
18031 /* Now, pretend that we want to see if the construct was
18032 successfully parsed. */
18033 cp_parser_parse_definitely (parser);
18034 }
18035
18036 /* Stop parsing tentatively. If a parse error has occurred, restore the
18037 token stream. Otherwise, commit to the tokens we have consumed.
18038 Returns true if no error occurred; false otherwise. */
18039
18040 static bool
18041 cp_parser_parse_definitely (cp_parser* parser)
18042 {
18043 bool error_occurred;
18044 cp_parser_context *context;
18045
18046 /* Remember whether or not an error occurred, since we are about to
18047 destroy that information. */
18048 error_occurred = cp_parser_error_occurred (parser);
18049 /* Remove the topmost context from the stack. */
18050 context = parser->context;
18051 parser->context = context->next;
18052 /* If no parse errors occurred, commit to the tentative parse. */
18053 if (!error_occurred)
18054 {
18055 /* Commit to the tokens read tentatively, unless that was
18056 already done. */
18057 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18058 cp_lexer_commit_tokens (parser->lexer);
18059
18060 pop_to_parent_deferring_access_checks ();
18061 }
18062 /* Otherwise, if errors occurred, roll back our state so that things
18063 are just as they were before we began the tentative parse. */
18064 else
18065 {
18066 cp_lexer_rollback_tokens (parser->lexer);
18067 pop_deferring_access_checks ();
18068 }
18069 /* Add the context to the front of the free list. */
18070 context->next = cp_parser_context_free_list;
18071 cp_parser_context_free_list = context;
18072
18073 return !error_occurred;
18074 }
18075
18076 /* Returns true if we are parsing tentatively and are not committed to
18077 this tentative parse. */
18078
18079 static bool
18080 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18081 {
18082 return (cp_parser_parsing_tentatively (parser)
18083 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18084 }
18085
18086 /* Returns nonzero iff an error has occurred during the most recent
18087 tentative parse. */
18088
18089 static bool
18090 cp_parser_error_occurred (cp_parser* parser)
18091 {
18092 return (cp_parser_parsing_tentatively (parser)
18093 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18094 }
18095
18096 /* Returns nonzero if GNU extensions are allowed. */
18097
18098 static bool
18099 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18100 {
18101 return parser->allow_gnu_extensions_p;
18102 }
18103 \f
18104 /* Objective-C++ Productions */
18105
18106
18107 /* Parse an Objective-C expression, which feeds into a primary-expression
18108 above.
18109
18110 objc-expression:
18111 objc-message-expression
18112 objc-string-literal
18113 objc-encode-expression
18114 objc-protocol-expression
18115 objc-selector-expression
18116
18117 Returns a tree representation of the expression. */
18118
18119 static tree
18120 cp_parser_objc_expression (cp_parser* parser)
18121 {
18122 /* Try to figure out what kind of declaration is present. */
18123 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18124
18125 switch (kwd->type)
18126 {
18127 case CPP_OPEN_SQUARE:
18128 return cp_parser_objc_message_expression (parser);
18129
18130 case CPP_OBJC_STRING:
18131 kwd = cp_lexer_consume_token (parser->lexer);
18132 return objc_build_string_object (kwd->u.value);
18133
18134 case CPP_KEYWORD:
18135 switch (kwd->keyword)
18136 {
18137 case RID_AT_ENCODE:
18138 return cp_parser_objc_encode_expression (parser);
18139
18140 case RID_AT_PROTOCOL:
18141 return cp_parser_objc_protocol_expression (parser);
18142
18143 case RID_AT_SELECTOR:
18144 return cp_parser_objc_selector_expression (parser);
18145
18146 default:
18147 break;
18148 }
18149 default:
18150 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18151 cp_parser_skip_to_end_of_block_or_statement (parser);
18152 }
18153
18154 return error_mark_node;
18155 }
18156
18157 /* Parse an Objective-C message expression.
18158
18159 objc-message-expression:
18160 [ objc-message-receiver objc-message-args ]
18161
18162 Returns a representation of an Objective-C message. */
18163
18164 static tree
18165 cp_parser_objc_message_expression (cp_parser* parser)
18166 {
18167 tree receiver, messageargs;
18168
18169 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18170 receiver = cp_parser_objc_message_receiver (parser);
18171 messageargs = cp_parser_objc_message_args (parser);
18172 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18173
18174 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18175 }
18176
18177 /* Parse an objc-message-receiver.
18178
18179 objc-message-receiver:
18180 expression
18181 simple-type-specifier
18182
18183 Returns a representation of the type or expression. */
18184
18185 static tree
18186 cp_parser_objc_message_receiver (cp_parser* parser)
18187 {
18188 tree rcv;
18189
18190 /* An Objective-C message receiver may be either (1) a type
18191 or (2) an expression. */
18192 cp_parser_parse_tentatively (parser);
18193 rcv = cp_parser_expression (parser, false);
18194
18195 if (cp_parser_parse_definitely (parser))
18196 return rcv;
18197
18198 rcv = cp_parser_simple_type_specifier (parser,
18199 /*decl_specs=*/NULL,
18200 CP_PARSER_FLAGS_NONE);
18201
18202 return objc_get_class_reference (rcv);
18203 }
18204
18205 /* Parse the arguments and selectors comprising an Objective-C message.
18206
18207 objc-message-args:
18208 objc-selector
18209 objc-selector-args
18210 objc-selector-args , objc-comma-args
18211
18212 objc-selector-args:
18213 objc-selector [opt] : assignment-expression
18214 objc-selector-args objc-selector [opt] : assignment-expression
18215
18216 objc-comma-args:
18217 assignment-expression
18218 objc-comma-args , assignment-expression
18219
18220 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18221 selector arguments and TREE_VALUE containing a list of comma
18222 arguments. */
18223
18224 static tree
18225 cp_parser_objc_message_args (cp_parser* parser)
18226 {
18227 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18228 bool maybe_unary_selector_p = true;
18229 cp_token *token = cp_lexer_peek_token (parser->lexer);
18230
18231 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18232 {
18233 tree selector = NULL_TREE, arg;
18234
18235 if (token->type != CPP_COLON)
18236 selector = cp_parser_objc_selector (parser);
18237
18238 /* Detect if we have a unary selector. */
18239 if (maybe_unary_selector_p
18240 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18241 return build_tree_list (selector, NULL_TREE);
18242
18243 maybe_unary_selector_p = false;
18244 cp_parser_require (parser, CPP_COLON, "`:'");
18245 arg = cp_parser_assignment_expression (parser, false);
18246
18247 sel_args
18248 = chainon (sel_args,
18249 build_tree_list (selector, arg));
18250
18251 token = cp_lexer_peek_token (parser->lexer);
18252 }
18253
18254 /* Handle non-selector arguments, if any. */
18255 while (token->type == CPP_COMMA)
18256 {
18257 tree arg;
18258
18259 cp_lexer_consume_token (parser->lexer);
18260 arg = cp_parser_assignment_expression (parser, false);
18261
18262 addl_args
18263 = chainon (addl_args,
18264 build_tree_list (NULL_TREE, arg));
18265
18266 token = cp_lexer_peek_token (parser->lexer);
18267 }
18268
18269 return build_tree_list (sel_args, addl_args);
18270 }
18271
18272 /* Parse an Objective-C encode expression.
18273
18274 objc-encode-expression:
18275 @encode objc-typename
18276
18277 Returns an encoded representation of the type argument. */
18278
18279 static tree
18280 cp_parser_objc_encode_expression (cp_parser* parser)
18281 {
18282 tree type;
18283
18284 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18285 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18286 type = complete_type (cp_parser_type_id (parser));
18287 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18288
18289 if (!type)
18290 {
18291 error ("%<@encode%> must specify a type as an argument");
18292 return error_mark_node;
18293 }
18294
18295 return objc_build_encode_expr (type);
18296 }
18297
18298 /* Parse an Objective-C @defs expression. */
18299
18300 static tree
18301 cp_parser_objc_defs_expression (cp_parser *parser)
18302 {
18303 tree name;
18304
18305 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18306 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18307 name = cp_parser_identifier (parser);
18308 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18309
18310 return objc_get_class_ivars (name);
18311 }
18312
18313 /* Parse an Objective-C protocol expression.
18314
18315 objc-protocol-expression:
18316 @protocol ( identifier )
18317
18318 Returns a representation of the protocol expression. */
18319
18320 static tree
18321 cp_parser_objc_protocol_expression (cp_parser* parser)
18322 {
18323 tree proto;
18324
18325 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18326 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18327 proto = cp_parser_identifier (parser);
18328 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18329
18330 return objc_build_protocol_expr (proto);
18331 }
18332
18333 /* Parse an Objective-C selector expression.
18334
18335 objc-selector-expression:
18336 @selector ( objc-method-signature )
18337
18338 objc-method-signature:
18339 objc-selector
18340 objc-selector-seq
18341
18342 objc-selector-seq:
18343 objc-selector :
18344 objc-selector-seq objc-selector :
18345
18346 Returns a representation of the method selector. */
18347
18348 static tree
18349 cp_parser_objc_selector_expression (cp_parser* parser)
18350 {
18351 tree sel_seq = NULL_TREE;
18352 bool maybe_unary_selector_p = true;
18353 cp_token *token;
18354
18355 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18356 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18357 token = cp_lexer_peek_token (parser->lexer);
18358
18359 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18360 || token->type == CPP_SCOPE)
18361 {
18362 tree selector = NULL_TREE;
18363
18364 if (token->type != CPP_COLON
18365 || token->type == CPP_SCOPE)
18366 selector = cp_parser_objc_selector (parser);
18367
18368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18369 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18370 {
18371 /* Detect if we have a unary selector. */
18372 if (maybe_unary_selector_p)
18373 {
18374 sel_seq = selector;
18375 goto finish_selector;
18376 }
18377 else
18378 {
18379 cp_parser_error (parser, "expected %<:%>");
18380 }
18381 }
18382 maybe_unary_selector_p = false;
18383 token = cp_lexer_consume_token (parser->lexer);
18384
18385 if (token->type == CPP_SCOPE)
18386 {
18387 sel_seq
18388 = chainon (sel_seq,
18389 build_tree_list (selector, NULL_TREE));
18390 sel_seq
18391 = chainon (sel_seq,
18392 build_tree_list (NULL_TREE, NULL_TREE));
18393 }
18394 else
18395 sel_seq
18396 = chainon (sel_seq,
18397 build_tree_list (selector, NULL_TREE));
18398
18399 token = cp_lexer_peek_token (parser->lexer);
18400 }
18401
18402 finish_selector:
18403 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18404
18405 return objc_build_selector_expr (sel_seq);
18406 }
18407
18408 /* Parse a list of identifiers.
18409
18410 objc-identifier-list:
18411 identifier
18412 objc-identifier-list , identifier
18413
18414 Returns a TREE_LIST of identifier nodes. */
18415
18416 static tree
18417 cp_parser_objc_identifier_list (cp_parser* parser)
18418 {
18419 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18420 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18421
18422 while (sep->type == CPP_COMMA)
18423 {
18424 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18425 list = chainon (list,
18426 build_tree_list (NULL_TREE,
18427 cp_parser_identifier (parser)));
18428 sep = cp_lexer_peek_token (parser->lexer);
18429 }
18430
18431 return list;
18432 }
18433
18434 /* Parse an Objective-C alias declaration.
18435
18436 objc-alias-declaration:
18437 @compatibility_alias identifier identifier ;
18438
18439 This function registers the alias mapping with the Objective-C front end.
18440 It returns nothing. */
18441
18442 static void
18443 cp_parser_objc_alias_declaration (cp_parser* parser)
18444 {
18445 tree alias, orig;
18446
18447 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18448 alias = cp_parser_identifier (parser);
18449 orig = cp_parser_identifier (parser);
18450 objc_declare_alias (alias, orig);
18451 cp_parser_consume_semicolon_at_end_of_statement (parser);
18452 }
18453
18454 /* Parse an Objective-C class forward-declaration.
18455
18456 objc-class-declaration:
18457 @class objc-identifier-list ;
18458
18459 The function registers the forward declarations with the Objective-C
18460 front end. It returns nothing. */
18461
18462 static void
18463 cp_parser_objc_class_declaration (cp_parser* parser)
18464 {
18465 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18466 objc_declare_class (cp_parser_objc_identifier_list (parser));
18467 cp_parser_consume_semicolon_at_end_of_statement (parser);
18468 }
18469
18470 /* Parse a list of Objective-C protocol references.
18471
18472 objc-protocol-refs-opt:
18473 objc-protocol-refs [opt]
18474
18475 objc-protocol-refs:
18476 < objc-identifier-list >
18477
18478 Returns a TREE_LIST of identifiers, if any. */
18479
18480 static tree
18481 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18482 {
18483 tree protorefs = NULL_TREE;
18484
18485 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18486 {
18487 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18488 protorefs = cp_parser_objc_identifier_list (parser);
18489 cp_parser_require (parser, CPP_GREATER, "`>'");
18490 }
18491
18492 return protorefs;
18493 }
18494
18495 /* Parse a Objective-C visibility specification. */
18496
18497 static void
18498 cp_parser_objc_visibility_spec (cp_parser* parser)
18499 {
18500 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18501
18502 switch (vis->keyword)
18503 {
18504 case RID_AT_PRIVATE:
18505 objc_set_visibility (2);
18506 break;
18507 case RID_AT_PROTECTED:
18508 objc_set_visibility (0);
18509 break;
18510 case RID_AT_PUBLIC:
18511 objc_set_visibility (1);
18512 break;
18513 default:
18514 return;
18515 }
18516
18517 /* Eat '@private'/'@protected'/'@public'. */
18518 cp_lexer_consume_token (parser->lexer);
18519 }
18520
18521 /* Parse an Objective-C method type. */
18522
18523 static void
18524 cp_parser_objc_method_type (cp_parser* parser)
18525 {
18526 objc_set_method_type
18527 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18528 ? PLUS_EXPR
18529 : MINUS_EXPR);
18530 }
18531
18532 /* Parse an Objective-C protocol qualifier. */
18533
18534 static tree
18535 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18536 {
18537 tree quals = NULL_TREE, node;
18538 cp_token *token = cp_lexer_peek_token (parser->lexer);
18539
18540 node = token->u.value;
18541
18542 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18543 && (node == ridpointers [(int) RID_IN]
18544 || node == ridpointers [(int) RID_OUT]
18545 || node == ridpointers [(int) RID_INOUT]
18546 || node == ridpointers [(int) RID_BYCOPY]
18547 || node == ridpointers [(int) RID_BYREF]
18548 || node == ridpointers [(int) RID_ONEWAY]))
18549 {
18550 quals = tree_cons (NULL_TREE, node, quals);
18551 cp_lexer_consume_token (parser->lexer);
18552 token = cp_lexer_peek_token (parser->lexer);
18553 node = token->u.value;
18554 }
18555
18556 return quals;
18557 }
18558
18559 /* Parse an Objective-C typename. */
18560
18561 static tree
18562 cp_parser_objc_typename (cp_parser* parser)
18563 {
18564 tree typename = NULL_TREE;
18565
18566 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18567 {
18568 tree proto_quals, cp_type = NULL_TREE;
18569
18570 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18571 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18572
18573 /* An ObjC type name may consist of just protocol qualifiers, in which
18574 case the type shall default to 'id'. */
18575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18576 cp_type = cp_parser_type_id (parser);
18577
18578 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18579 typename = build_tree_list (proto_quals, cp_type);
18580 }
18581
18582 return typename;
18583 }
18584
18585 /* Check to see if TYPE refers to an Objective-C selector name. */
18586
18587 static bool
18588 cp_parser_objc_selector_p (enum cpp_ttype type)
18589 {
18590 return (type == CPP_NAME || type == CPP_KEYWORD
18591 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18592 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18593 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18594 || type == CPP_XOR || type == CPP_XOR_EQ);
18595 }
18596
18597 /* Parse an Objective-C selector. */
18598
18599 static tree
18600 cp_parser_objc_selector (cp_parser* parser)
18601 {
18602 cp_token *token = cp_lexer_consume_token (parser->lexer);
18603
18604 if (!cp_parser_objc_selector_p (token->type))
18605 {
18606 error ("invalid Objective-C++ selector name");
18607 return error_mark_node;
18608 }
18609
18610 /* C++ operator names are allowed to appear in ObjC selectors. */
18611 switch (token->type)
18612 {
18613 case CPP_AND_AND: return get_identifier ("and");
18614 case CPP_AND_EQ: return get_identifier ("and_eq");
18615 case CPP_AND: return get_identifier ("bitand");
18616 case CPP_OR: return get_identifier ("bitor");
18617 case CPP_COMPL: return get_identifier ("compl");
18618 case CPP_NOT: return get_identifier ("not");
18619 case CPP_NOT_EQ: return get_identifier ("not_eq");
18620 case CPP_OR_OR: return get_identifier ("or");
18621 case CPP_OR_EQ: return get_identifier ("or_eq");
18622 case CPP_XOR: return get_identifier ("xor");
18623 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18624 default: return token->u.value;
18625 }
18626 }
18627
18628 /* Parse an Objective-C params list. */
18629
18630 static tree
18631 cp_parser_objc_method_keyword_params (cp_parser* parser)
18632 {
18633 tree params = NULL_TREE;
18634 bool maybe_unary_selector_p = true;
18635 cp_token *token = cp_lexer_peek_token (parser->lexer);
18636
18637 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18638 {
18639 tree selector = NULL_TREE, typename, identifier;
18640
18641 if (token->type != CPP_COLON)
18642 selector = cp_parser_objc_selector (parser);
18643
18644 /* Detect if we have a unary selector. */
18645 if (maybe_unary_selector_p
18646 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18647 return selector;
18648
18649 maybe_unary_selector_p = false;
18650 cp_parser_require (parser, CPP_COLON, "`:'");
18651 typename = cp_parser_objc_typename (parser);
18652 identifier = cp_parser_identifier (parser);
18653
18654 params
18655 = chainon (params,
18656 objc_build_keyword_decl (selector,
18657 typename,
18658 identifier));
18659
18660 token = cp_lexer_peek_token (parser->lexer);
18661 }
18662
18663 return params;
18664 }
18665
18666 /* Parse the non-keyword Objective-C params. */
18667
18668 static tree
18669 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18670 {
18671 tree params = make_node (TREE_LIST);
18672 cp_token *token = cp_lexer_peek_token (parser->lexer);
18673 *ellipsisp = false; /* Initially, assume no ellipsis. */
18674
18675 while (token->type == CPP_COMMA)
18676 {
18677 cp_parameter_declarator *parmdecl;
18678 tree parm;
18679
18680 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18681 token = cp_lexer_peek_token (parser->lexer);
18682
18683 if (token->type == CPP_ELLIPSIS)
18684 {
18685 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18686 *ellipsisp = true;
18687 break;
18688 }
18689
18690 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18691 parm = grokdeclarator (parmdecl->declarator,
18692 &parmdecl->decl_specifiers,
18693 PARM, /*initialized=*/0,
18694 /*attrlist=*/NULL);
18695
18696 chainon (params, build_tree_list (NULL_TREE, parm));
18697 token = cp_lexer_peek_token (parser->lexer);
18698 }
18699
18700 return params;
18701 }
18702
18703 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18704
18705 static void
18706 cp_parser_objc_interstitial_code (cp_parser* parser)
18707 {
18708 cp_token *token = cp_lexer_peek_token (parser->lexer);
18709
18710 /* If the next token is `extern' and the following token is a string
18711 literal, then we have a linkage specification. */
18712 if (token->keyword == RID_EXTERN
18713 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18714 cp_parser_linkage_specification (parser);
18715 /* Handle #pragma, if any. */
18716 else if (token->type == CPP_PRAGMA)
18717 cp_parser_pragma (parser, pragma_external);
18718 /* Allow stray semicolons. */
18719 else if (token->type == CPP_SEMICOLON)
18720 cp_lexer_consume_token (parser->lexer);
18721 /* Finally, try to parse a block-declaration, or a function-definition. */
18722 else
18723 cp_parser_block_declaration (parser, /*statement_p=*/false);
18724 }
18725
18726 /* Parse a method signature. */
18727
18728 static tree
18729 cp_parser_objc_method_signature (cp_parser* parser)
18730 {
18731 tree rettype, kwdparms, optparms;
18732 bool ellipsis = false;
18733
18734 cp_parser_objc_method_type (parser);
18735 rettype = cp_parser_objc_typename (parser);
18736 kwdparms = cp_parser_objc_method_keyword_params (parser);
18737 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18738
18739 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18740 }
18741
18742 /* Pars an Objective-C method prototype list. */
18743
18744 static void
18745 cp_parser_objc_method_prototype_list (cp_parser* parser)
18746 {
18747 cp_token *token = cp_lexer_peek_token (parser->lexer);
18748
18749 while (token->keyword != RID_AT_END)
18750 {
18751 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18752 {
18753 objc_add_method_declaration
18754 (cp_parser_objc_method_signature (parser));
18755 cp_parser_consume_semicolon_at_end_of_statement (parser);
18756 }
18757 else
18758 /* Allow for interspersed non-ObjC++ code. */
18759 cp_parser_objc_interstitial_code (parser);
18760
18761 token = cp_lexer_peek_token (parser->lexer);
18762 }
18763
18764 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18765 objc_finish_interface ();
18766 }
18767
18768 /* Parse an Objective-C method definition list. */
18769
18770 static void
18771 cp_parser_objc_method_definition_list (cp_parser* parser)
18772 {
18773 cp_token *token = cp_lexer_peek_token (parser->lexer);
18774
18775 while (token->keyword != RID_AT_END)
18776 {
18777 tree meth;
18778
18779 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18780 {
18781 push_deferring_access_checks (dk_deferred);
18782 objc_start_method_definition
18783 (cp_parser_objc_method_signature (parser));
18784
18785 /* For historical reasons, we accept an optional semicolon. */
18786 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18787 cp_lexer_consume_token (parser->lexer);
18788
18789 perform_deferred_access_checks ();
18790 stop_deferring_access_checks ();
18791 meth = cp_parser_function_definition_after_declarator (parser,
18792 false);
18793 pop_deferring_access_checks ();
18794 objc_finish_method_definition (meth);
18795 }
18796 else
18797 /* Allow for interspersed non-ObjC++ code. */
18798 cp_parser_objc_interstitial_code (parser);
18799
18800 token = cp_lexer_peek_token (parser->lexer);
18801 }
18802
18803 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18804 objc_finish_implementation ();
18805 }
18806
18807 /* Parse Objective-C ivars. */
18808
18809 static void
18810 cp_parser_objc_class_ivars (cp_parser* parser)
18811 {
18812 cp_token *token = cp_lexer_peek_token (parser->lexer);
18813
18814 if (token->type != CPP_OPEN_BRACE)
18815 return; /* No ivars specified. */
18816
18817 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18818 token = cp_lexer_peek_token (parser->lexer);
18819
18820 while (token->type != CPP_CLOSE_BRACE)
18821 {
18822 cp_decl_specifier_seq declspecs;
18823 int decl_class_or_enum_p;
18824 tree prefix_attributes;
18825
18826 cp_parser_objc_visibility_spec (parser);
18827
18828 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18829 break;
18830
18831 cp_parser_decl_specifier_seq (parser,
18832 CP_PARSER_FLAGS_OPTIONAL,
18833 &declspecs,
18834 &decl_class_or_enum_p);
18835 prefix_attributes = declspecs.attributes;
18836 declspecs.attributes = NULL_TREE;
18837
18838 /* Keep going until we hit the `;' at the end of the
18839 declaration. */
18840 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18841 {
18842 tree width = NULL_TREE, attributes, first_attribute, decl;
18843 cp_declarator *declarator = NULL;
18844 int ctor_dtor_or_conv_p;
18845
18846 /* Check for a (possibly unnamed) bitfield declaration. */
18847 token = cp_lexer_peek_token (parser->lexer);
18848 if (token->type == CPP_COLON)
18849 goto eat_colon;
18850
18851 if (token->type == CPP_NAME
18852 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18853 == CPP_COLON))
18854 {
18855 /* Get the name of the bitfield. */
18856 declarator = make_id_declarator (NULL_TREE,
18857 cp_parser_identifier (parser),
18858 sfk_none);
18859
18860 eat_colon:
18861 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18862 /* Get the width of the bitfield. */
18863 width
18864 = cp_parser_constant_expression (parser,
18865 /*allow_non_constant=*/false,
18866 NULL);
18867 }
18868 else
18869 {
18870 /* Parse the declarator. */
18871 declarator
18872 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18873 &ctor_dtor_or_conv_p,
18874 /*parenthesized_p=*/NULL,
18875 /*member_p=*/false);
18876 }
18877
18878 /* Look for attributes that apply to the ivar. */
18879 attributes = cp_parser_attributes_opt (parser);
18880 /* Remember which attributes are prefix attributes and
18881 which are not. */
18882 first_attribute = attributes;
18883 /* Combine the attributes. */
18884 attributes = chainon (prefix_attributes, attributes);
18885
18886 if (width)
18887 {
18888 /* Create the bitfield declaration. */
18889 decl = grokbitfield (declarator, &declspecs, width);
18890 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18891 }
18892 else
18893 decl = grokfield (declarator, &declspecs,
18894 NULL_TREE, /*init_const_expr_p=*/false,
18895 NULL_TREE, attributes);
18896
18897 /* Add the instance variable. */
18898 objc_add_instance_variable (decl);
18899
18900 /* Reset PREFIX_ATTRIBUTES. */
18901 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18902 attributes = TREE_CHAIN (attributes);
18903 if (attributes)
18904 TREE_CHAIN (attributes) = NULL_TREE;
18905
18906 token = cp_lexer_peek_token (parser->lexer);
18907
18908 if (token->type == CPP_COMMA)
18909 {
18910 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18911 continue;
18912 }
18913 break;
18914 }
18915
18916 cp_parser_consume_semicolon_at_end_of_statement (parser);
18917 token = cp_lexer_peek_token (parser->lexer);
18918 }
18919
18920 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18921 /* For historical reasons, we accept an optional semicolon. */
18922 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18923 cp_lexer_consume_token (parser->lexer);
18924 }
18925
18926 /* Parse an Objective-C protocol declaration. */
18927
18928 static void
18929 cp_parser_objc_protocol_declaration (cp_parser* parser)
18930 {
18931 tree proto, protorefs;
18932 cp_token *tok;
18933
18934 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18936 {
18937 error ("identifier expected after %<@protocol%>");
18938 goto finish;
18939 }
18940
18941 /* See if we have a forward declaration or a definition. */
18942 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18943
18944 /* Try a forward declaration first. */
18945 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18946 {
18947 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18948 finish:
18949 cp_parser_consume_semicolon_at_end_of_statement (parser);
18950 }
18951
18952 /* Ok, we got a full-fledged definition (or at least should). */
18953 else
18954 {
18955 proto = cp_parser_identifier (parser);
18956 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18957 objc_start_protocol (proto, protorefs);
18958 cp_parser_objc_method_prototype_list (parser);
18959 }
18960 }
18961
18962 /* Parse an Objective-C superclass or category. */
18963
18964 static void
18965 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18966 tree *categ)
18967 {
18968 cp_token *next = cp_lexer_peek_token (parser->lexer);
18969
18970 *super = *categ = NULL_TREE;
18971 if (next->type == CPP_COLON)
18972 {
18973 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18974 *super = cp_parser_identifier (parser);
18975 }
18976 else if (next->type == CPP_OPEN_PAREN)
18977 {
18978 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18979 *categ = cp_parser_identifier (parser);
18980 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18981 }
18982 }
18983
18984 /* Parse an Objective-C class interface. */
18985
18986 static void
18987 cp_parser_objc_class_interface (cp_parser* parser)
18988 {
18989 tree name, super, categ, protos;
18990
18991 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18992 name = cp_parser_identifier (parser);
18993 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18994 protos = cp_parser_objc_protocol_refs_opt (parser);
18995
18996 /* We have either a class or a category on our hands. */
18997 if (categ)
18998 objc_start_category_interface (name, categ, protos);
18999 else
19000 {
19001 objc_start_class_interface (name, super, protos);
19002 /* Handle instance variable declarations, if any. */
19003 cp_parser_objc_class_ivars (parser);
19004 objc_continue_interface ();
19005 }
19006
19007 cp_parser_objc_method_prototype_list (parser);
19008 }
19009
19010 /* Parse an Objective-C class implementation. */
19011
19012 static void
19013 cp_parser_objc_class_implementation (cp_parser* parser)
19014 {
19015 tree name, super, categ;
19016
19017 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19018 name = cp_parser_identifier (parser);
19019 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19020
19021 /* We have either a class or a category on our hands. */
19022 if (categ)
19023 objc_start_category_implementation (name, categ);
19024 else
19025 {
19026 objc_start_class_implementation (name, super);
19027 /* Handle instance variable declarations, if any. */
19028 cp_parser_objc_class_ivars (parser);
19029 objc_continue_implementation ();
19030 }
19031
19032 cp_parser_objc_method_definition_list (parser);
19033 }
19034
19035 /* Consume the @end token and finish off the implementation. */
19036
19037 static void
19038 cp_parser_objc_end_implementation (cp_parser* parser)
19039 {
19040 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19041 objc_finish_implementation ();
19042 }
19043
19044 /* Parse an Objective-C declaration. */
19045
19046 static void
19047 cp_parser_objc_declaration (cp_parser* parser)
19048 {
19049 /* Try to figure out what kind of declaration is present. */
19050 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19051
19052 switch (kwd->keyword)
19053 {
19054 case RID_AT_ALIAS:
19055 cp_parser_objc_alias_declaration (parser);
19056 break;
19057 case RID_AT_CLASS:
19058 cp_parser_objc_class_declaration (parser);
19059 break;
19060 case RID_AT_PROTOCOL:
19061 cp_parser_objc_protocol_declaration (parser);
19062 break;
19063 case RID_AT_INTERFACE:
19064 cp_parser_objc_class_interface (parser);
19065 break;
19066 case RID_AT_IMPLEMENTATION:
19067 cp_parser_objc_class_implementation (parser);
19068 break;
19069 case RID_AT_END:
19070 cp_parser_objc_end_implementation (parser);
19071 break;
19072 default:
19073 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19074 cp_parser_skip_to_end_of_block_or_statement (parser);
19075 }
19076 }
19077
19078 /* Parse an Objective-C try-catch-finally statement.
19079
19080 objc-try-catch-finally-stmt:
19081 @try compound-statement objc-catch-clause-seq [opt]
19082 objc-finally-clause [opt]
19083
19084 objc-catch-clause-seq:
19085 objc-catch-clause objc-catch-clause-seq [opt]
19086
19087 objc-catch-clause:
19088 @catch ( exception-declaration ) compound-statement
19089
19090 objc-finally-clause
19091 @finally compound-statement
19092
19093 Returns NULL_TREE. */
19094
19095 static tree
19096 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19097 location_t location;
19098 tree stmt;
19099
19100 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19101 location = cp_lexer_peek_token (parser->lexer)->location;
19102 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19103 node, lest it get absorbed into the surrounding block. */
19104 stmt = push_stmt_list ();
19105 cp_parser_compound_statement (parser, NULL, false);
19106 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19107
19108 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19109 {
19110 cp_parameter_declarator *parmdecl;
19111 tree parm;
19112
19113 cp_lexer_consume_token (parser->lexer);
19114 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19115 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19116 parm = grokdeclarator (parmdecl->declarator,
19117 &parmdecl->decl_specifiers,
19118 PARM, /*initialized=*/0,
19119 /*attrlist=*/NULL);
19120 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19121 objc_begin_catch_clause (parm);
19122 cp_parser_compound_statement (parser, NULL, false);
19123 objc_finish_catch_clause ();
19124 }
19125
19126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19127 {
19128 cp_lexer_consume_token (parser->lexer);
19129 location = cp_lexer_peek_token (parser->lexer)->location;
19130 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19131 node, lest it get absorbed into the surrounding block. */
19132 stmt = push_stmt_list ();
19133 cp_parser_compound_statement (parser, NULL, false);
19134 objc_build_finally_clause (location, pop_stmt_list (stmt));
19135 }
19136
19137 return objc_finish_try_stmt ();
19138 }
19139
19140 /* Parse an Objective-C synchronized statement.
19141
19142 objc-synchronized-stmt:
19143 @synchronized ( expression ) compound-statement
19144
19145 Returns NULL_TREE. */
19146
19147 static tree
19148 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19149 location_t location;
19150 tree lock, stmt;
19151
19152 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19153
19154 location = cp_lexer_peek_token (parser->lexer)->location;
19155 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19156 lock = cp_parser_expression (parser, false);
19157 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19158
19159 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19160 node, lest it get absorbed into the surrounding block. */
19161 stmt = push_stmt_list ();
19162 cp_parser_compound_statement (parser, NULL, false);
19163
19164 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19165 }
19166
19167 /* Parse an Objective-C throw statement.
19168
19169 objc-throw-stmt:
19170 @throw assignment-expression [opt] ;
19171
19172 Returns a constructed '@throw' statement. */
19173
19174 static tree
19175 cp_parser_objc_throw_statement (cp_parser *parser) {
19176 tree expr = NULL_TREE;
19177
19178 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19179
19180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19181 expr = cp_parser_assignment_expression (parser, false);
19182
19183 cp_parser_consume_semicolon_at_end_of_statement (parser);
19184
19185 return objc_build_throw_stmt (expr);
19186 }
19187
19188 /* Parse an Objective-C statement. */
19189
19190 static tree
19191 cp_parser_objc_statement (cp_parser * parser) {
19192 /* Try to figure out what kind of declaration is present. */
19193 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19194
19195 switch (kwd->keyword)
19196 {
19197 case RID_AT_TRY:
19198 return cp_parser_objc_try_catch_finally_statement (parser);
19199 case RID_AT_SYNCHRONIZED:
19200 return cp_parser_objc_synchronized_statement (parser);
19201 case RID_AT_THROW:
19202 return cp_parser_objc_throw_statement (parser);
19203 default:
19204 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19205 cp_parser_skip_to_end_of_block_or_statement (parser);
19206 }
19207
19208 return error_mark_node;
19209 }
19210 \f
19211 /* OpenMP 2.5 parsing routines. */
19212
19213 /* Returns name of the next clause.
19214 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19215 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19216 returned and the token is consumed. */
19217
19218 static pragma_omp_clause
19219 cp_parser_omp_clause_name (cp_parser *parser)
19220 {
19221 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19222
19223 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19224 result = PRAGMA_OMP_CLAUSE_IF;
19225 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19226 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19227 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19228 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19229 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19230 {
19231 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19232 const char *p = IDENTIFIER_POINTER (id);
19233
19234 switch (p[0])
19235 {
19236 case 'c':
19237 if (!strcmp ("copyin", p))
19238 result = PRAGMA_OMP_CLAUSE_COPYIN;
19239 else if (!strcmp ("copyprivate", p))
19240 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19241 break;
19242 case 'f':
19243 if (!strcmp ("firstprivate", p))
19244 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19245 break;
19246 case 'l':
19247 if (!strcmp ("lastprivate", p))
19248 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19249 break;
19250 case 'n':
19251 if (!strcmp ("nowait", p))
19252 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19253 else if (!strcmp ("num_threads", p))
19254 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19255 break;
19256 case 'o':
19257 if (!strcmp ("ordered", p))
19258 result = PRAGMA_OMP_CLAUSE_ORDERED;
19259 break;
19260 case 'r':
19261 if (!strcmp ("reduction", p))
19262 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19263 break;
19264 case 's':
19265 if (!strcmp ("schedule", p))
19266 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19267 else if (!strcmp ("shared", p))
19268 result = PRAGMA_OMP_CLAUSE_SHARED;
19269 break;
19270 }
19271 }
19272
19273 if (result != PRAGMA_OMP_CLAUSE_NONE)
19274 cp_lexer_consume_token (parser->lexer);
19275
19276 return result;
19277 }
19278
19279 /* Validate that a clause of the given type does not already exist. */
19280
19281 static void
19282 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19283 {
19284 tree c;
19285
19286 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19287 if (OMP_CLAUSE_CODE (c) == code)
19288 {
19289 error ("too many %qs clauses", name);
19290 break;
19291 }
19292 }
19293
19294 /* OpenMP 2.5:
19295 variable-list:
19296 identifier
19297 variable-list , identifier
19298
19299 In addition, we match a closing parenthesis. An opening parenthesis
19300 will have been consumed by the caller.
19301
19302 If KIND is nonzero, create the appropriate node and install the decl
19303 in OMP_CLAUSE_DECL and add the node to the head of the list.
19304
19305 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19306 return the list created. */
19307
19308 static tree
19309 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19310 tree list)
19311 {
19312 while (1)
19313 {
19314 tree name, decl;
19315
19316 name = cp_parser_id_expression (parser, /*template_p=*/false,
19317 /*check_dependency_p=*/true,
19318 /*template_p=*/NULL,
19319 /*declarator_p=*/false,
19320 /*optional_p=*/false);
19321 if (name == error_mark_node)
19322 goto skip_comma;
19323
19324 decl = cp_parser_lookup_name_simple (parser, name);
19325 if (decl == error_mark_node)
19326 cp_parser_name_lookup_error (parser, name, decl, NULL);
19327 else if (kind != 0)
19328 {
19329 tree u = build_omp_clause (kind);
19330 OMP_CLAUSE_DECL (u) = decl;
19331 OMP_CLAUSE_CHAIN (u) = list;
19332 list = u;
19333 }
19334 else
19335 list = tree_cons (decl, NULL_TREE, list);
19336
19337 get_comma:
19338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19339 break;
19340 cp_lexer_consume_token (parser->lexer);
19341 }
19342
19343 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19344 {
19345 int ending;
19346
19347 /* Try to resync to an unnested comma. Copied from
19348 cp_parser_parenthesized_expression_list. */
19349 skip_comma:
19350 ending = cp_parser_skip_to_closing_parenthesis (parser,
19351 /*recovering=*/true,
19352 /*or_comma=*/true,
19353 /*consume_paren=*/true);
19354 if (ending < 0)
19355 goto get_comma;
19356 }
19357
19358 return list;
19359 }
19360
19361 /* Similarly, but expect leading and trailing parenthesis. This is a very
19362 common case for omp clauses. */
19363
19364 static tree
19365 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19366 {
19367 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19368 return cp_parser_omp_var_list_no_open (parser, kind, list);
19369 return list;
19370 }
19371
19372 /* OpenMP 2.5:
19373 default ( shared | none ) */
19374
19375 static tree
19376 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19377 {
19378 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19379 tree c;
19380
19381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19382 return list;
19383 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19384 {
19385 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19386 const char *p = IDENTIFIER_POINTER (id);
19387
19388 switch (p[0])
19389 {
19390 case 'n':
19391 if (strcmp ("none", p) != 0)
19392 goto invalid_kind;
19393 kind = OMP_CLAUSE_DEFAULT_NONE;
19394 break;
19395
19396 case 's':
19397 if (strcmp ("shared", p) != 0)
19398 goto invalid_kind;
19399 kind = OMP_CLAUSE_DEFAULT_SHARED;
19400 break;
19401
19402 default:
19403 goto invalid_kind;
19404 }
19405
19406 cp_lexer_consume_token (parser->lexer);
19407 }
19408 else
19409 {
19410 invalid_kind:
19411 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19412 }
19413
19414 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19415 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19416 /*or_comma=*/false,
19417 /*consume_paren=*/true);
19418
19419 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19420 return list;
19421
19422 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19423 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19424 OMP_CLAUSE_CHAIN (c) = list;
19425 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19426
19427 return c;
19428 }
19429
19430 /* OpenMP 2.5:
19431 if ( expression ) */
19432
19433 static tree
19434 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19435 {
19436 tree t, c;
19437
19438 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19439 return list;
19440
19441 t = cp_parser_condition (parser);
19442
19443 if (t == error_mark_node
19444 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19446 /*or_comma=*/false,
19447 /*consume_paren=*/true);
19448
19449 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19450
19451 c = build_omp_clause (OMP_CLAUSE_IF);
19452 OMP_CLAUSE_IF_EXPR (c) = t;
19453 OMP_CLAUSE_CHAIN (c) = list;
19454
19455 return c;
19456 }
19457
19458 /* OpenMP 2.5:
19459 nowait */
19460
19461 static tree
19462 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19463 {
19464 tree c;
19465
19466 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19467
19468 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19469 OMP_CLAUSE_CHAIN (c) = list;
19470 return c;
19471 }
19472
19473 /* OpenMP 2.5:
19474 num_threads ( expression ) */
19475
19476 static tree
19477 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19478 {
19479 tree t, c;
19480
19481 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19482 return list;
19483
19484 t = cp_parser_expression (parser, false);
19485
19486 if (t == error_mark_node
19487 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19488 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19489 /*or_comma=*/false,
19490 /*consume_paren=*/true);
19491
19492 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19493
19494 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19495 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19496 OMP_CLAUSE_CHAIN (c) = list;
19497
19498 return c;
19499 }
19500
19501 /* OpenMP 2.5:
19502 ordered */
19503
19504 static tree
19505 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19506 {
19507 tree c;
19508
19509 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19510
19511 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19512 OMP_CLAUSE_CHAIN (c) = list;
19513 return c;
19514 }
19515
19516 /* OpenMP 2.5:
19517 reduction ( reduction-operator : variable-list )
19518
19519 reduction-operator:
19520 One of: + * - & ^ | && || */
19521
19522 static tree
19523 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19524 {
19525 enum tree_code code;
19526 tree nlist, c;
19527
19528 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19529 return list;
19530
19531 switch (cp_lexer_peek_token (parser->lexer)->type)
19532 {
19533 case CPP_PLUS:
19534 code = PLUS_EXPR;
19535 break;
19536 case CPP_MULT:
19537 code = MULT_EXPR;
19538 break;
19539 case CPP_MINUS:
19540 code = MINUS_EXPR;
19541 break;
19542 case CPP_AND:
19543 code = BIT_AND_EXPR;
19544 break;
19545 case CPP_XOR:
19546 code = BIT_XOR_EXPR;
19547 break;
19548 case CPP_OR:
19549 code = BIT_IOR_EXPR;
19550 break;
19551 case CPP_AND_AND:
19552 code = TRUTH_ANDIF_EXPR;
19553 break;
19554 case CPP_OR_OR:
19555 code = TRUTH_ORIF_EXPR;
19556 break;
19557 default:
19558 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19559 resync_fail:
19560 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19561 /*or_comma=*/false,
19562 /*consume_paren=*/true);
19563 return list;
19564 }
19565 cp_lexer_consume_token (parser->lexer);
19566
19567 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19568 goto resync_fail;
19569
19570 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19571 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19572 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19573
19574 return nlist;
19575 }
19576
19577 /* OpenMP 2.5:
19578 schedule ( schedule-kind )
19579 schedule ( schedule-kind , expression )
19580
19581 schedule-kind:
19582 static | dynamic | guided | runtime */
19583
19584 static tree
19585 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19586 {
19587 tree c, t;
19588
19589 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19590 return list;
19591
19592 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19593
19594 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19595 {
19596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19597 const char *p = IDENTIFIER_POINTER (id);
19598
19599 switch (p[0])
19600 {
19601 case 'd':
19602 if (strcmp ("dynamic", p) != 0)
19603 goto invalid_kind;
19604 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19605 break;
19606
19607 case 'g':
19608 if (strcmp ("guided", p) != 0)
19609 goto invalid_kind;
19610 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19611 break;
19612
19613 case 'r':
19614 if (strcmp ("runtime", p) != 0)
19615 goto invalid_kind;
19616 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19617 break;
19618
19619 default:
19620 goto invalid_kind;
19621 }
19622 }
19623 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19624 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19625 else
19626 goto invalid_kind;
19627 cp_lexer_consume_token (parser->lexer);
19628
19629 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19630 {
19631 cp_lexer_consume_token (parser->lexer);
19632
19633 t = cp_parser_assignment_expression (parser, false);
19634
19635 if (t == error_mark_node)
19636 goto resync_fail;
19637 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19638 error ("schedule %<runtime%> does not take "
19639 "a %<chunk_size%> parameter");
19640 else
19641 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19642
19643 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19644 goto resync_fail;
19645 }
19646 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19647 goto resync_fail;
19648
19649 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19650 OMP_CLAUSE_CHAIN (c) = list;
19651 return c;
19652
19653 invalid_kind:
19654 cp_parser_error (parser, "invalid schedule kind");
19655 resync_fail:
19656 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19657 /*or_comma=*/false,
19658 /*consume_paren=*/true);
19659 return list;
19660 }
19661
19662 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19663 is a bitmask in MASK. Return the list of clauses found; the result
19664 of clause default goes in *pdefault. */
19665
19666 static tree
19667 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19668 const char *where, cp_token *pragma_tok)
19669 {
19670 tree clauses = NULL;
19671
19672 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19673 {
19674 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19675 const char *c_name;
19676 tree prev = clauses;
19677
19678 switch (c_kind)
19679 {
19680 case PRAGMA_OMP_CLAUSE_COPYIN:
19681 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19682 c_name = "copyin";
19683 break;
19684 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19685 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19686 clauses);
19687 c_name = "copyprivate";
19688 break;
19689 case PRAGMA_OMP_CLAUSE_DEFAULT:
19690 clauses = cp_parser_omp_clause_default (parser, clauses);
19691 c_name = "default";
19692 break;
19693 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19694 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19695 clauses);
19696 c_name = "firstprivate";
19697 break;
19698 case PRAGMA_OMP_CLAUSE_IF:
19699 clauses = cp_parser_omp_clause_if (parser, clauses);
19700 c_name = "if";
19701 break;
19702 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19703 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19704 clauses);
19705 c_name = "lastprivate";
19706 break;
19707 case PRAGMA_OMP_CLAUSE_NOWAIT:
19708 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19709 c_name = "nowait";
19710 break;
19711 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19712 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19713 c_name = "num_threads";
19714 break;
19715 case PRAGMA_OMP_CLAUSE_ORDERED:
19716 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19717 c_name = "ordered";
19718 break;
19719 case PRAGMA_OMP_CLAUSE_PRIVATE:
19720 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19721 clauses);
19722 c_name = "private";
19723 break;
19724 case PRAGMA_OMP_CLAUSE_REDUCTION:
19725 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19726 c_name = "reduction";
19727 break;
19728 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19729 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19730 c_name = "schedule";
19731 break;
19732 case PRAGMA_OMP_CLAUSE_SHARED:
19733 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19734 clauses);
19735 c_name = "shared";
19736 break;
19737 default:
19738 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19739 goto saw_error;
19740 }
19741
19742 if (((mask >> c_kind) & 1) == 0)
19743 {
19744 /* Remove the invalid clause(s) from the list to avoid
19745 confusing the rest of the compiler. */
19746 clauses = prev;
19747 error ("%qs is not valid for %qs", c_name, where);
19748 }
19749 }
19750 saw_error:
19751 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19752 return finish_omp_clauses (clauses);
19753 }
19754
19755 /* OpenMP 2.5:
19756 structured-block:
19757 statement
19758
19759 In practice, we're also interested in adding the statement to an
19760 outer node. So it is convenient if we work around the fact that
19761 cp_parser_statement calls add_stmt. */
19762
19763 static unsigned
19764 cp_parser_begin_omp_structured_block (cp_parser *parser)
19765 {
19766 unsigned save = parser->in_statement;
19767
19768 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19769 This preserves the "not within loop or switch" style error messages
19770 for nonsense cases like
19771 void foo() {
19772 #pragma omp single
19773 break;
19774 }
19775 */
19776 if (parser->in_statement)
19777 parser->in_statement = IN_OMP_BLOCK;
19778
19779 return save;
19780 }
19781
19782 static void
19783 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19784 {
19785 parser->in_statement = save;
19786 }
19787
19788 static tree
19789 cp_parser_omp_structured_block (cp_parser *parser)
19790 {
19791 tree stmt = begin_omp_structured_block ();
19792 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19793
19794 cp_parser_statement (parser, NULL_TREE, false, NULL);
19795
19796 cp_parser_end_omp_structured_block (parser, save);
19797 return finish_omp_structured_block (stmt);
19798 }
19799
19800 /* OpenMP 2.5:
19801 # pragma omp atomic new-line
19802 expression-stmt
19803
19804 expression-stmt:
19805 x binop= expr | x++ | ++x | x-- | --x
19806 binop:
19807 +, *, -, /, &, ^, |, <<, >>
19808
19809 where x is an lvalue expression with scalar type. */
19810
19811 static void
19812 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19813 {
19814 tree lhs, rhs;
19815 enum tree_code code;
19816
19817 cp_parser_require_pragma_eol (parser, pragma_tok);
19818
19819 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19820 /*cast_p=*/false);
19821 switch (TREE_CODE (lhs))
19822 {
19823 case ERROR_MARK:
19824 goto saw_error;
19825
19826 case PREINCREMENT_EXPR:
19827 case POSTINCREMENT_EXPR:
19828 lhs = TREE_OPERAND (lhs, 0);
19829 code = PLUS_EXPR;
19830 rhs = integer_one_node;
19831 break;
19832
19833 case PREDECREMENT_EXPR:
19834 case POSTDECREMENT_EXPR:
19835 lhs = TREE_OPERAND (lhs, 0);
19836 code = MINUS_EXPR;
19837 rhs = integer_one_node;
19838 break;
19839
19840 default:
19841 switch (cp_lexer_peek_token (parser->lexer)->type)
19842 {
19843 case CPP_MULT_EQ:
19844 code = MULT_EXPR;
19845 break;
19846 case CPP_DIV_EQ:
19847 code = TRUNC_DIV_EXPR;
19848 break;
19849 case CPP_PLUS_EQ:
19850 code = PLUS_EXPR;
19851 break;
19852 case CPP_MINUS_EQ:
19853 code = MINUS_EXPR;
19854 break;
19855 case CPP_LSHIFT_EQ:
19856 code = LSHIFT_EXPR;
19857 break;
19858 case CPP_RSHIFT_EQ:
19859 code = RSHIFT_EXPR;
19860 break;
19861 case CPP_AND_EQ:
19862 code = BIT_AND_EXPR;
19863 break;
19864 case CPP_OR_EQ:
19865 code = BIT_IOR_EXPR;
19866 break;
19867 case CPP_XOR_EQ:
19868 code = BIT_XOR_EXPR;
19869 break;
19870 default:
19871 cp_parser_error (parser,
19872 "invalid operator for %<#pragma omp atomic%>");
19873 goto saw_error;
19874 }
19875 cp_lexer_consume_token (parser->lexer);
19876
19877 rhs = cp_parser_expression (parser, false);
19878 if (rhs == error_mark_node)
19879 goto saw_error;
19880 break;
19881 }
19882 finish_omp_atomic (code, lhs, rhs);
19883 cp_parser_consume_semicolon_at_end_of_statement (parser);
19884 return;
19885
19886 saw_error:
19887 cp_parser_skip_to_end_of_block_or_statement (parser);
19888 }
19889
19890
19891 /* OpenMP 2.5:
19892 # pragma omp barrier new-line */
19893
19894 static void
19895 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19896 {
19897 cp_parser_require_pragma_eol (parser, pragma_tok);
19898 finish_omp_barrier ();
19899 }
19900
19901 /* OpenMP 2.5:
19902 # pragma omp critical [(name)] new-line
19903 structured-block */
19904
19905 static tree
19906 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19907 {
19908 tree stmt, name = NULL;
19909
19910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19911 {
19912 cp_lexer_consume_token (parser->lexer);
19913
19914 name = cp_parser_identifier (parser);
19915
19916 if (name == error_mark_node
19917 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19918 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19919 /*or_comma=*/false,
19920 /*consume_paren=*/true);
19921 if (name == error_mark_node)
19922 name = NULL;
19923 }
19924 cp_parser_require_pragma_eol (parser, pragma_tok);
19925
19926 stmt = cp_parser_omp_structured_block (parser);
19927 return c_finish_omp_critical (stmt, name);
19928 }
19929
19930 /* OpenMP 2.5:
19931 # pragma omp flush flush-vars[opt] new-line
19932
19933 flush-vars:
19934 ( variable-list ) */
19935
19936 static void
19937 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19938 {
19939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19940 (void) cp_parser_omp_var_list (parser, 0, NULL);
19941 cp_parser_require_pragma_eol (parser, pragma_tok);
19942
19943 finish_omp_flush ();
19944 }
19945
19946 /* Parse the restricted form of the for statment allowed by OpenMP. */
19947
19948 static tree
19949 cp_parser_omp_for_loop (cp_parser *parser)
19950 {
19951 tree init, cond, incr, body, decl, pre_body;
19952 location_t loc;
19953
19954 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19955 {
19956 cp_parser_error (parser, "for statement expected");
19957 return NULL;
19958 }
19959 loc = cp_lexer_consume_token (parser->lexer)->location;
19960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19961 return NULL;
19962
19963 init = decl = NULL;
19964 pre_body = push_stmt_list ();
19965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19966 {
19967 cp_decl_specifier_seq type_specifiers;
19968
19969 /* First, try to parse as an initialized declaration. See
19970 cp_parser_condition, from whence the bulk of this is copied. */
19971
19972 cp_parser_parse_tentatively (parser);
19973 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19974 &type_specifiers);
19975 if (!cp_parser_error_occurred (parser))
19976 {
19977 tree asm_specification, attributes;
19978 cp_declarator *declarator;
19979
19980 declarator = cp_parser_declarator (parser,
19981 CP_PARSER_DECLARATOR_NAMED,
19982 /*ctor_dtor_or_conv_p=*/NULL,
19983 /*parenthesized_p=*/NULL,
19984 /*member_p=*/false);
19985 attributes = cp_parser_attributes_opt (parser);
19986 asm_specification = cp_parser_asm_specification_opt (parser);
19987
19988 cp_parser_require (parser, CPP_EQ, "`='");
19989 if (cp_parser_parse_definitely (parser))
19990 {
19991 tree pushed_scope;
19992
19993 decl = start_decl (declarator, &type_specifiers,
19994 /*initialized_p=*/false, attributes,
19995 /*prefix_attributes=*/NULL_TREE,
19996 &pushed_scope);
19997
19998 init = cp_parser_assignment_expression (parser, false);
19999
20000 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20001 asm_specification, LOOKUP_ONLYCONVERTING);
20002
20003 if (pushed_scope)
20004 pop_scope (pushed_scope);
20005 }
20006 }
20007 else
20008 cp_parser_abort_tentative_parse (parser);
20009
20010 /* If parsing as an initialized declaration failed, try again as
20011 a simple expression. */
20012 if (decl == NULL)
20013 init = cp_parser_expression (parser, false);
20014 }
20015 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20016 pre_body = pop_stmt_list (pre_body);
20017
20018 cond = NULL;
20019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20020 cond = cp_parser_condition (parser);
20021 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20022
20023 incr = NULL;
20024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20025 incr = cp_parser_expression (parser, false);
20026
20027 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20028 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20029 /*or_comma=*/false,
20030 /*consume_paren=*/true);
20031
20032 /* Note that we saved the original contents of this flag when we entered
20033 the structured block, and so we don't need to re-save it here. */
20034 parser->in_statement = IN_OMP_FOR;
20035
20036 /* Note that the grammar doesn't call for a structured block here,
20037 though the loop as a whole is a structured block. */
20038 body = push_stmt_list ();
20039 cp_parser_statement (parser, NULL_TREE, false, NULL);
20040 body = pop_stmt_list (body);
20041
20042 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20043 }
20044
20045 /* OpenMP 2.5:
20046 #pragma omp for for-clause[optseq] new-line
20047 for-loop */
20048
20049 #define OMP_FOR_CLAUSE_MASK \
20050 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20051 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20052 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20053 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20054 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20055 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20056 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20057
20058 static tree
20059 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20060 {
20061 tree clauses, sb, ret;
20062 unsigned int save;
20063
20064 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20065 "#pragma omp for", pragma_tok);
20066
20067 sb = begin_omp_structured_block ();
20068 save = cp_parser_begin_omp_structured_block (parser);
20069
20070 ret = cp_parser_omp_for_loop (parser);
20071 if (ret)
20072 OMP_FOR_CLAUSES (ret) = clauses;
20073
20074 cp_parser_end_omp_structured_block (parser, save);
20075 add_stmt (finish_omp_structured_block (sb));
20076
20077 return ret;
20078 }
20079
20080 /* OpenMP 2.5:
20081 # pragma omp master new-line
20082 structured-block */
20083
20084 static tree
20085 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20086 {
20087 cp_parser_require_pragma_eol (parser, pragma_tok);
20088 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20089 }
20090
20091 /* OpenMP 2.5:
20092 # pragma omp ordered new-line
20093 structured-block */
20094
20095 static tree
20096 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20097 {
20098 cp_parser_require_pragma_eol (parser, pragma_tok);
20099 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20100 }
20101
20102 /* OpenMP 2.5:
20103
20104 section-scope:
20105 { section-sequence }
20106
20107 section-sequence:
20108 section-directive[opt] structured-block
20109 section-sequence section-directive structured-block */
20110
20111 static tree
20112 cp_parser_omp_sections_scope (cp_parser *parser)
20113 {
20114 tree stmt, substmt;
20115 bool error_suppress = false;
20116 cp_token *tok;
20117
20118 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20119 return NULL_TREE;
20120
20121 stmt = push_stmt_list ();
20122
20123 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20124 {
20125 unsigned save;
20126
20127 substmt = begin_omp_structured_block ();
20128 save = cp_parser_begin_omp_structured_block (parser);
20129
20130 while (1)
20131 {
20132 cp_parser_statement (parser, NULL_TREE, false, NULL);
20133
20134 tok = cp_lexer_peek_token (parser->lexer);
20135 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20136 break;
20137 if (tok->type == CPP_CLOSE_BRACE)
20138 break;
20139 if (tok->type == CPP_EOF)
20140 break;
20141 }
20142
20143 cp_parser_end_omp_structured_block (parser, save);
20144 substmt = finish_omp_structured_block (substmt);
20145 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20146 add_stmt (substmt);
20147 }
20148
20149 while (1)
20150 {
20151 tok = cp_lexer_peek_token (parser->lexer);
20152 if (tok->type == CPP_CLOSE_BRACE)
20153 break;
20154 if (tok->type == CPP_EOF)
20155 break;
20156
20157 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20158 {
20159 cp_lexer_consume_token (parser->lexer);
20160 cp_parser_require_pragma_eol (parser, tok);
20161 error_suppress = false;
20162 }
20163 else if (!error_suppress)
20164 {
20165 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20166 error_suppress = true;
20167 }
20168
20169 substmt = cp_parser_omp_structured_block (parser);
20170 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20171 add_stmt (substmt);
20172 }
20173 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20174
20175 substmt = pop_stmt_list (stmt);
20176
20177 stmt = make_node (OMP_SECTIONS);
20178 TREE_TYPE (stmt) = void_type_node;
20179 OMP_SECTIONS_BODY (stmt) = substmt;
20180
20181 add_stmt (stmt);
20182 return stmt;
20183 }
20184
20185 /* OpenMP 2.5:
20186 # pragma omp sections sections-clause[optseq] newline
20187 sections-scope */
20188
20189 #define OMP_SECTIONS_CLAUSE_MASK \
20190 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20191 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20192 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20193 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20194 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20195
20196 static tree
20197 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20198 {
20199 tree clauses, ret;
20200
20201 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20202 "#pragma omp sections", pragma_tok);
20203
20204 ret = cp_parser_omp_sections_scope (parser);
20205 if (ret)
20206 OMP_SECTIONS_CLAUSES (ret) = clauses;
20207
20208 return ret;
20209 }
20210
20211 /* OpenMP 2.5:
20212 # pragma parallel parallel-clause new-line
20213 # pragma parallel for parallel-for-clause new-line
20214 # pragma parallel sections parallel-sections-clause new-line */
20215
20216 #define OMP_PARALLEL_CLAUSE_MASK \
20217 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20218 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20219 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20220 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20221 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20222 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20223 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20224 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20225
20226 static tree
20227 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20228 {
20229 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20230 const char *p_name = "#pragma omp parallel";
20231 tree stmt, clauses, par_clause, ws_clause, block;
20232 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20233 unsigned int save;
20234
20235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20236 {
20237 cp_lexer_consume_token (parser->lexer);
20238 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20239 p_name = "#pragma omp parallel for";
20240 mask |= OMP_FOR_CLAUSE_MASK;
20241 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20242 }
20243 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20244 {
20245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20246 const char *p = IDENTIFIER_POINTER (id);
20247 if (strcmp (p, "sections") == 0)
20248 {
20249 cp_lexer_consume_token (parser->lexer);
20250 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20251 p_name = "#pragma omp parallel sections";
20252 mask |= OMP_SECTIONS_CLAUSE_MASK;
20253 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20254 }
20255 }
20256
20257 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20258 block = begin_omp_parallel ();
20259 save = cp_parser_begin_omp_structured_block (parser);
20260
20261 switch (p_kind)
20262 {
20263 case PRAGMA_OMP_PARALLEL:
20264 cp_parser_already_scoped_statement (parser);
20265 par_clause = clauses;
20266 break;
20267
20268 case PRAGMA_OMP_PARALLEL_FOR:
20269 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20270 stmt = cp_parser_omp_for_loop (parser);
20271 if (stmt)
20272 OMP_FOR_CLAUSES (stmt) = ws_clause;
20273 break;
20274
20275 case PRAGMA_OMP_PARALLEL_SECTIONS:
20276 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20277 stmt = cp_parser_omp_sections_scope (parser);
20278 if (stmt)
20279 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20280 break;
20281
20282 default:
20283 gcc_unreachable ();
20284 }
20285
20286 cp_parser_end_omp_structured_block (parser, save);
20287 stmt = finish_omp_parallel (par_clause, block);
20288 if (p_kind != PRAGMA_OMP_PARALLEL)
20289 OMP_PARALLEL_COMBINED (stmt) = 1;
20290 return stmt;
20291 }
20292
20293 /* OpenMP 2.5:
20294 # pragma omp single single-clause[optseq] new-line
20295 structured-block */
20296
20297 #define OMP_SINGLE_CLAUSE_MASK \
20298 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20299 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20300 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20301 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20302
20303 static tree
20304 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20305 {
20306 tree stmt = make_node (OMP_SINGLE);
20307 TREE_TYPE (stmt) = void_type_node;
20308
20309 OMP_SINGLE_CLAUSES (stmt)
20310 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20311 "#pragma omp single", pragma_tok);
20312 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20313
20314 return add_stmt (stmt);
20315 }
20316
20317 /* OpenMP 2.5:
20318 # pragma omp threadprivate (variable-list) */
20319
20320 static void
20321 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20322 {
20323 tree vars;
20324
20325 vars = cp_parser_omp_var_list (parser, 0, NULL);
20326 cp_parser_require_pragma_eol (parser, pragma_tok);
20327
20328 finish_omp_threadprivate (vars);
20329 }
20330
20331 /* Main entry point to OpenMP statement pragmas. */
20332
20333 static void
20334 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20335 {
20336 tree stmt;
20337
20338 switch (pragma_tok->pragma_kind)
20339 {
20340 case PRAGMA_OMP_ATOMIC:
20341 cp_parser_omp_atomic (parser, pragma_tok);
20342 return;
20343 case PRAGMA_OMP_CRITICAL:
20344 stmt = cp_parser_omp_critical (parser, pragma_tok);
20345 break;
20346 case PRAGMA_OMP_FOR:
20347 stmt = cp_parser_omp_for (parser, pragma_tok);
20348 break;
20349 case PRAGMA_OMP_MASTER:
20350 stmt = cp_parser_omp_master (parser, pragma_tok);
20351 break;
20352 case PRAGMA_OMP_ORDERED:
20353 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20354 break;
20355 case PRAGMA_OMP_PARALLEL:
20356 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20357 break;
20358 case PRAGMA_OMP_SECTIONS:
20359 stmt = cp_parser_omp_sections (parser, pragma_tok);
20360 break;
20361 case PRAGMA_OMP_SINGLE:
20362 stmt = cp_parser_omp_single (parser, pragma_tok);
20363 break;
20364 default:
20365 gcc_unreachable ();
20366 }
20367
20368 if (stmt)
20369 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20370 }
20371 \f
20372 /* The parser. */
20373
20374 static GTY (()) cp_parser *the_parser;
20375
20376 \f
20377 /* Special handling for the first token or line in the file. The first
20378 thing in the file might be #pragma GCC pch_preprocess, which loads a
20379 PCH file, which is a GC collection point. So we need to handle this
20380 first pragma without benefit of an existing lexer structure.
20381
20382 Always returns one token to the caller in *FIRST_TOKEN. This is
20383 either the true first token of the file, or the first token after
20384 the initial pragma. */
20385
20386 static void
20387 cp_parser_initial_pragma (cp_token *first_token)
20388 {
20389 tree name = NULL;
20390
20391 cp_lexer_get_preprocessor_token (NULL, first_token);
20392 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20393 return;
20394
20395 cp_lexer_get_preprocessor_token (NULL, first_token);
20396 if (first_token->type == CPP_STRING)
20397 {
20398 name = first_token->u.value;
20399
20400 cp_lexer_get_preprocessor_token (NULL, first_token);
20401 if (first_token->type != CPP_PRAGMA_EOL)
20402 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20403 }
20404 else
20405 error ("expected string literal");
20406
20407 /* Skip to the end of the pragma. */
20408 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20409 cp_lexer_get_preprocessor_token (NULL, first_token);
20410
20411 /* Now actually load the PCH file. */
20412 if (name)
20413 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20414
20415 /* Read one more token to return to our caller. We have to do this
20416 after reading the PCH file in, since its pointers have to be
20417 live. */
20418 cp_lexer_get_preprocessor_token (NULL, first_token);
20419 }
20420
20421 /* Normal parsing of a pragma token. Here we can (and must) use the
20422 regular lexer. */
20423
20424 static bool
20425 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20426 {
20427 cp_token *pragma_tok;
20428 unsigned int id;
20429
20430 pragma_tok = cp_lexer_consume_token (parser->lexer);
20431 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20432 parser->lexer->in_pragma = true;
20433
20434 id = pragma_tok->pragma_kind;
20435 switch (id)
20436 {
20437 case PRAGMA_GCC_PCH_PREPROCESS:
20438 error ("%<#pragma GCC pch_preprocess%> must be first");
20439 break;
20440
20441 case PRAGMA_OMP_BARRIER:
20442 switch (context)
20443 {
20444 case pragma_compound:
20445 cp_parser_omp_barrier (parser, pragma_tok);
20446 return false;
20447 case pragma_stmt:
20448 error ("%<#pragma omp barrier%> may only be "
20449 "used in compound statements");
20450 break;
20451 default:
20452 goto bad_stmt;
20453 }
20454 break;
20455
20456 case PRAGMA_OMP_FLUSH:
20457 switch (context)
20458 {
20459 case pragma_compound:
20460 cp_parser_omp_flush (parser, pragma_tok);
20461 return false;
20462 case pragma_stmt:
20463 error ("%<#pragma omp flush%> may only be "
20464 "used in compound statements");
20465 break;
20466 default:
20467 goto bad_stmt;
20468 }
20469 break;
20470
20471 case PRAGMA_OMP_THREADPRIVATE:
20472 cp_parser_omp_threadprivate (parser, pragma_tok);
20473 return false;
20474
20475 case PRAGMA_OMP_ATOMIC:
20476 case PRAGMA_OMP_CRITICAL:
20477 case PRAGMA_OMP_FOR:
20478 case PRAGMA_OMP_MASTER:
20479 case PRAGMA_OMP_ORDERED:
20480 case PRAGMA_OMP_PARALLEL:
20481 case PRAGMA_OMP_SECTIONS:
20482 case PRAGMA_OMP_SINGLE:
20483 if (context == pragma_external)
20484 goto bad_stmt;
20485 cp_parser_omp_construct (parser, pragma_tok);
20486 return true;
20487
20488 case PRAGMA_OMP_SECTION:
20489 error ("%<#pragma omp section%> may only be used in "
20490 "%<#pragma omp sections%> construct");
20491 break;
20492
20493 default:
20494 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20495 c_invoke_pragma_handler (id);
20496 break;
20497
20498 bad_stmt:
20499 cp_parser_error (parser, "expected declaration specifiers");
20500 break;
20501 }
20502
20503 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20504 return false;
20505 }
20506
20507 /* The interface the pragma parsers have to the lexer. */
20508
20509 enum cpp_ttype
20510 pragma_lex (tree *value)
20511 {
20512 cp_token *tok;
20513 enum cpp_ttype ret;
20514
20515 tok = cp_lexer_peek_token (the_parser->lexer);
20516
20517 ret = tok->type;
20518 *value = tok->u.value;
20519
20520 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20521 ret = CPP_EOF;
20522 else if (ret == CPP_STRING)
20523 *value = cp_parser_string_literal (the_parser, false, false);
20524 else
20525 {
20526 cp_lexer_consume_token (the_parser->lexer);
20527 if (ret == CPP_KEYWORD)
20528 ret = CPP_NAME;
20529 }
20530
20531 return ret;
20532 }
20533
20534 \f
20535 /* External interface. */
20536
20537 /* Parse one entire translation unit. */
20538
20539 void
20540 c_parse_file (void)
20541 {
20542 bool error_occurred;
20543 static bool already_called = false;
20544
20545 if (already_called)
20546 {
20547 sorry ("inter-module optimizations not implemented for C++");
20548 return;
20549 }
20550 already_called = true;
20551
20552 the_parser = cp_parser_new ();
20553 push_deferring_access_checks (flag_access_control
20554 ? dk_no_deferred : dk_no_check);
20555 error_occurred = cp_parser_translation_unit (the_parser);
20556 the_parser = NULL;
20557 }
20558
20559 #include "gt-cp-parser.h"