1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 /* A token's value and its associated deferred access checks and
49 qualifying scope. */
50
51 struct tree_check GTY(())
52 {
53 /* The value associated with the token. */
54 tree value;
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
60 };
61
62 /* A C++ token. */
63
64 typedef struct cp_token GTY (())
65 {
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
71 /* Token flags. */
72 unsigned char flags;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
93 location_t location;
94 } cp_token;
95
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
100
101 static const cp_token eof_token =
102 {
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
105 0
106 #else
107 {0, 0}
108 #endif
109 };
110
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
114 it is created. */
115
116 typedef struct cp_lexer GTY (())
117 {
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
122 buffer. */
123 size_t buffer_length;
124
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
128
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
132
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
136 tokens. */
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
141
142 /* True if we should output debugging information. */
143 bool debugging_p;
144
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
147 bool in_pragma;
148 } cp_lexer;
149
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
154 a lexer. */
155
156 typedef struct cp_token_cache GTY(())
157 {
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
160
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
163 } cp_token_cache;
164
165 /* Prototypes. */
166
167 static cp_lexer *cp_lexer_new_main
168 (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172 (cp_lexer *);
173 static int cp_lexer_saving_tokens
174 (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176 (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182 (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192 (cp_lexer *);
193 static void cp_lexer_purge_token
194 (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198 (cp_lexer *);
199 static void cp_lexer_commit_tokens
200 (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202 (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207 (cp_lexer *);
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
221
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
224
225 static void cp_parser_initial_pragma
226 (cp_token *);
227
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
231
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255
256 /* Variables. */
257
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
262
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264 preprocessor. */
265
266 static cp_lexer *
cp_lexer_new_main(void)267 cp_lexer_new_main (void)
268 {
269 cp_token first_token;
270 cp_lexer *lexer;
271 cp_token *pos;
272 size_t alloc;
273 size_t space;
274 cp_token *buffer;
275
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
280
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
283
284 c_common_no_more_pch ();
285
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
288
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
295
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
299
300 /* Put the first token in the buffer. */
301 space = alloc;
302 pos = buffer;
303 *pos = first_token;
304
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
307 {
308 pos++;
309 if (!--space)
310 {
311 space = alloc;
312 alloc *= 2;
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
315 }
316 cp_lexer_get_preprocessor_token (lexer, pos);
317 }
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
322
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
329 return lexer;
330 }
331
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
334
335 static cp_lexer *
cp_lexer_new_from_tokens(cp_token_cache * cache)336 cp_lexer_new_from_tokens (cp_token_cache *cache)
337 {
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
341
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
347
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
350
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
354 #endif
355
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
357 return lexer;
358 }
359
360 /* Frees all resources associated with LEXER. */
361
362 static void
cp_lexer_destroy(cp_lexer * lexer)363 cp_lexer_destroy (cp_lexer *lexer)
364 {
365 if (lexer->buffer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
368 ggc_free (lexer);
369 }
370
371 /* Returns nonzero if debugging information should be output. */
372
373 #ifdef ENABLE_CHECKING
374
375 static inline bool
cp_lexer_debugging_p(cp_lexer * lexer)376 cp_lexer_debugging_p (cp_lexer *lexer)
377 {
378 return lexer->debugging_p;
379 }
380
381 #endif /* ENABLE_CHECKING */
382
383 static inline cp_token_position
cp_lexer_token_position(cp_lexer * lexer,bool previous_p)384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385 {
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
387
388 return lexer->next_token - previous_p;
389 }
390
391 static inline cp_token *
cp_lexer_token_at(cp_lexer * lexer ATTRIBUTE_UNUSED,cp_token_position pos)392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393 {
394 return pos;
395 }
396
397 /* nonzero if we are presently saving tokens. */
398
399 static inline int
cp_lexer_saving_tokens(const cp_lexer * lexer)400 cp_lexer_saving_tokens (const cp_lexer* lexer)
401 {
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
403 }
404
405 /* Store the next token from the preprocessor in *TOKEN. Return true
406 if we reach EOF. */
407
408 static void
cp_lexer_get_preprocessor_token(cp_lexer * lexer ATTRIBUTE_UNUSED,cp_token * token)409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410 cp_token *token)
411 {
412 static int is_extern_c = 0;
413
414 /* Get a new token from the preprocessor. */
415 token->type
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
421
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
428
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
431 {
432 if (C_IS_RESERVED_WORD (token->u.value))
433 {
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
443 }
444 else
445 {
446 token->ambiguous_p = false;
447 token->keyword = RID_MAX;
448 }
449 }
450 /* Handle Objective-C++ keywords. */
451 else if (token->type == CPP_AT_NAME)
452 {
453 token->type = CPP_KEYWORD;
454 switch (C_RID_CODE (token->u.value))
455 {
456 /* Map 'class' to '@class', 'private' to '@private', etc. */
457 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
458 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
459 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
460 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
461 case RID_THROW: token->keyword = RID_AT_THROW; break;
462 case RID_TRY: token->keyword = RID_AT_TRY; break;
463 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
464 default: token->keyword = C_RID_CODE (token->u.value);
465 }
466 }
467 else if (token->type == CPP_PRAGMA)
468 {
469 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
470 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
471 token->u.value = NULL_TREE;
472 }
473 }
474
475 /* Update the globals input_location and in_system_header and the
476 input file stack from TOKEN. */
477 static inline void
cp_lexer_set_source_position_from_token(cp_token * token)478 cp_lexer_set_source_position_from_token (cp_token *token)
479 {
480 if (token->type != CPP_EOF)
481 {
482 input_location = token->location;
483 in_system_header = token->in_system_header;
484 restore_input_file_stack (token->input_file_stack_index);
485 }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489 consume it. */
490
491 static inline cp_token *
cp_lexer_peek_token(cp_lexer * lexer)492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494 if (cp_lexer_debugging_p (lexer))
495 {
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
499 }
500 return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE. */
504
505 static inline bool
cp_lexer_next_token_is(cp_lexer * lexer,enum cpp_ttype type)506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508 return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE. */
512
513 static inline bool
cp_lexer_next_token_is_not(cp_lexer * lexer,enum cpp_ttype type)514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516 return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD. */
520
521 static inline bool
cp_lexer_next_token_is_keyword(cp_lexer * lexer,enum rid keyword)522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is a keyword for a decl-specifier. */
528
529 static bool
cp_lexer_next_token_is_decl_specifier_keyword(cp_lexer * lexer)530 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
531 {
532 cp_token *token;
533
534 token = cp_lexer_peek_token (lexer);
535 switch (token->keyword)
536 {
537 /* Storage classes. */
538 case RID_AUTO:
539 case RID_REGISTER:
540 case RID_STATIC:
541 case RID_EXTERN:
542 case RID_MUTABLE:
543 case RID_THREAD:
544 /* Elaborated type specifiers. */
545 case RID_ENUM:
546 case RID_CLASS:
547 case RID_STRUCT:
548 case RID_UNION:
549 case RID_TYPENAME:
550 /* Simple type specifiers. */
551 case RID_CHAR:
552 case RID_WCHAR:
553 case RID_BOOL:
554 case RID_SHORT:
555 case RID_INT:
556 case RID_LONG:
557 case RID_SIGNED:
558 case RID_UNSIGNED:
559 case RID_FLOAT:
560 case RID_DOUBLE:
561 case RID_VOID:
562 /* GNU extensions. */
563 case RID_ATTRIBUTE:
564 case RID_TYPEOF:
565 return true;
566
567 default:
568 return false;
569 }
570 }
571
572 /* Return a pointer to the Nth token in the token stream. If N is 1,
573 then this is precisely equivalent to cp_lexer_peek_token (except
574 that it is not inline). One would like to disallow that case, but
575 there is one case (cp_parser_nth_token_starts_template_id) where
576 the caller passes a variable for N and it might be 1. */
577
578 static cp_token *
cp_lexer_peek_nth_token(cp_lexer * lexer,size_t n)579 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
580 {
581 cp_token *token;
582
583 /* N is 1-based, not zero-based. */
584 gcc_assert (n > 0);
585
586 if (cp_lexer_debugging_p (lexer))
587 fprintf (cp_lexer_debug_stream,
588 "cp_lexer: peeking ahead %ld at token: ", (long)n);
589
590 --n;
591 token = lexer->next_token;
592 gcc_assert (!n || token != &eof_token);
593 while (n != 0)
594 {
595 ++token;
596 if (token == lexer->last_token)
597 {
598 token = (cp_token *)&eof_token;
599 break;
600 }
601
602 if (token->type != CPP_PURGED)
603 --n;
604 }
605
606 if (cp_lexer_debugging_p (lexer))
607 {
608 cp_lexer_print_token (cp_lexer_debug_stream, token);
609 putc ('\n', cp_lexer_debug_stream);
610 }
611
612 return token;
613 }
614
615 /* Return the next token, and advance the lexer's next_token pointer
616 to point to the next non-purged token. */
617
618 static cp_token *
cp_lexer_consume_token(cp_lexer * lexer)619 cp_lexer_consume_token (cp_lexer* lexer)
620 {
621 cp_token *token = lexer->next_token;
622
623 gcc_assert (token != &eof_token);
624 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
625
626 do
627 {
628 lexer->next_token++;
629 if (lexer->next_token == lexer->last_token)
630 {
631 lexer->next_token = (cp_token *)&eof_token;
632 break;
633 }
634
635 }
636 while (lexer->next_token->type == CPP_PURGED);
637
638 cp_lexer_set_source_position_from_token (token);
639
640 /* Provide debugging output. */
641 if (cp_lexer_debugging_p (lexer))
642 {
643 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
644 cp_lexer_print_token (cp_lexer_debug_stream, token);
645 putc ('\n', cp_lexer_debug_stream);
646 }
647
648 return token;
649 }
650
651 /* Permanently remove the next token from the token stream, and
652 advance the next_token pointer to refer to the next non-purged
653 token. */
654
655 static void
cp_lexer_purge_token(cp_lexer * lexer)656 cp_lexer_purge_token (cp_lexer *lexer)
657 {
658 cp_token *tok = lexer->next_token;
659
660 gcc_assert (tok != &eof_token);
661 tok->type = CPP_PURGED;
662 tok->location = UNKNOWN_LOCATION;
663 tok->u.value = NULL_TREE;
664 tok->keyword = RID_MAX;
665
666 do
667 {
668 tok++;
669 if (tok == lexer->last_token)
670 {
671 tok = (cp_token *)&eof_token;
672 break;
673 }
674 }
675 while (tok->type == CPP_PURGED);
676 lexer->next_token = tok;
677 }
678
679 /* Permanently remove all tokens after TOK, up to, but not
680 including, the token that will be returned next by
681 cp_lexer_peek_token. */
682
683 static void
cp_lexer_purge_tokens_after(cp_lexer * lexer,cp_token * tok)684 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
685 {
686 cp_token *peek = lexer->next_token;
687
688 if (peek == &eof_token)
689 peek = lexer->last_token;
690
691 gcc_assert (tok < peek);
692
693 for ( tok += 1; tok != peek; tok += 1)
694 {
695 tok->type = CPP_PURGED;
696 tok->location = UNKNOWN_LOCATION;
697 tok->u.value = NULL_TREE;
698 tok->keyword = RID_MAX;
699 }
700 }
701
702 /* Begin saving tokens. All tokens consumed after this point will be
703 preserved. */
704
705 static void
cp_lexer_save_tokens(cp_lexer * lexer)706 cp_lexer_save_tokens (cp_lexer* lexer)
707 {
708 /* Provide debugging output. */
709 if (cp_lexer_debugging_p (lexer))
710 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
711
712 VEC_safe_push (cp_token_position, heap,
713 lexer->saved_tokens, lexer->next_token);
714 }
715
716 /* Commit to the portion of the token stream most recently saved. */
717
718 static void
cp_lexer_commit_tokens(cp_lexer * lexer)719 cp_lexer_commit_tokens (cp_lexer* lexer)
720 {
721 /* Provide debugging output. */
722 if (cp_lexer_debugging_p (lexer))
723 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
724
725 VEC_pop (cp_token_position, lexer->saved_tokens);
726 }
727
728 /* Return all tokens saved since the last call to cp_lexer_save_tokens
729 to the token stream. Stop saving tokens. */
730
731 static void
cp_lexer_rollback_tokens(cp_lexer * lexer)732 cp_lexer_rollback_tokens (cp_lexer* lexer)
733 {
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
736 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
737
738 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
739 }
740
741 /* Print a representation of the TOKEN on the STREAM. */
742
743 #ifdef ENABLE_CHECKING
744
745 static void
cp_lexer_print_token(FILE * stream,cp_token * token)746 cp_lexer_print_token (FILE * stream, cp_token *token)
747 {
748 /* We don't use cpp_type2name here because the parser defines
749 a few tokens of its own. */
750 static const char *const token_names[] = {
751 /* cpplib-defined token types */
752 #define OP(e, s) #e,
753 #define TK(e, s) #e,
754 TTYPE_TABLE
755 #undef OP
756 #undef TK
757 /* C++ parser token types - see "Manifest constants", above. */
758 "KEYWORD",
759 "TEMPLATE_ID",
760 "NESTED_NAME_SPECIFIER",
761 "PURGED"
762 };
763
764 /* If we have a name for the token, print it out. Otherwise, we
765 simply give the numeric code. */
766 gcc_assert (token->type < ARRAY_SIZE(token_names));
767 fputs (token_names[token->type], stream);
768
769 /* For some tokens, print the associated data. */
770 switch (token->type)
771 {
772 case CPP_KEYWORD:
773 /* Some keywords have a value that is not an IDENTIFIER_NODE.
774 For example, `struct' is mapped to an INTEGER_CST. */
775 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
776 break;
777 /* else fall through */
778 case CPP_NAME:
779 fputs (IDENTIFIER_POINTER (token->u.value), stream);
780 break;
781
782 case CPP_STRING:
783 case CPP_WSTRING:
784 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
785 break;
786
787 default:
788 break;
789 }
790 }
791
792 /* Start emitting debugging information. */
793
794 static void
cp_lexer_start_debugging(cp_lexer * lexer)795 cp_lexer_start_debugging (cp_lexer* lexer)
796 {
797 lexer->debugging_p = true;
798 }
799
800 /* Stop emitting debugging information. */
801
802 static void
cp_lexer_stop_debugging(cp_lexer * lexer)803 cp_lexer_stop_debugging (cp_lexer* lexer)
804 {
805 lexer->debugging_p = false;
806 }
807
808 #endif /* ENABLE_CHECKING */
809
810 /* Create a new cp_token_cache, representing a range of tokens. */
811
812 static cp_token_cache *
cp_token_cache_new(cp_token * first,cp_token * last)813 cp_token_cache_new (cp_token *first, cp_token *last)
814 {
815 cp_token_cache *cache = GGC_NEW (cp_token_cache);
816 cache->first = first;
817 cache->last = last;
818 return cache;
819 }
820
821
822 /* Decl-specifiers. */
823
824 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
825
826 static void
clear_decl_specs(cp_decl_specifier_seq * decl_specs)827 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
828 {
829 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
830 }
831
832 /* Declarators. */
833
834 /* Nothing other than the parser should be creating declarators;
835 declarators are a semi-syntactic representation of C++ entities.
836 Other parts of the front end that need to create entities (like
837 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
838
839 static cp_declarator *make_call_declarator
840 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
841 static cp_declarator *make_array_declarator
842 (cp_declarator *, tree);
843 static cp_declarator *make_pointer_declarator
844 (cp_cv_quals, cp_declarator *);
845 static cp_declarator *make_reference_declarator
846 (cp_cv_quals, cp_declarator *);
847 static cp_parameter_declarator *make_parameter_declarator
848 (cp_decl_specifier_seq *, cp_declarator *, tree);
849 static cp_declarator *make_ptrmem_declarator
850 (cp_cv_quals, tree, cp_declarator *);
851
852 /* An erroneous declarator. */
853 static cp_declarator *cp_error_declarator;
854
855 /* The obstack on which declarators and related data structures are
856 allocated. */
857 static struct obstack declarator_obstack;
858
859 /* Alloc BYTES from the declarator memory pool. */
860
861 static inline void *
alloc_declarator(size_t bytes)862 alloc_declarator (size_t bytes)
863 {
864 return obstack_alloc (&declarator_obstack, bytes);
865 }
866
867 /* Allocate a declarator of the indicated KIND. Clear fields that are
868 common to all declarators. */
869
870 static cp_declarator *
make_declarator(cp_declarator_kind kind)871 make_declarator (cp_declarator_kind kind)
872 {
873 cp_declarator *declarator;
874
875 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
876 declarator->kind = kind;
877 declarator->attributes = NULL_TREE;
878 declarator->declarator = NULL;
879
880 return declarator;
881 }
882
883 /* Make a declarator for a generalized identifier. If
884 QUALIFYING_SCOPE is non-NULL, the identifier is
885 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
886 UNQUALIFIED_NAME. SFK indicates the kind of special function this
887 is, if any. */
888
889 static cp_declarator *
make_id_declarator(tree qualifying_scope,tree unqualified_name,special_function_kind sfk)890 make_id_declarator (tree qualifying_scope, tree unqualified_name,
891 special_function_kind sfk)
892 {
893 cp_declarator *declarator;
894
895 /* It is valid to write:
896
897 class C { void f(); };
898 typedef C D;
899 void D::f();
900
901 The standard is not clear about whether `typedef const C D' is
902 legal; as of 2002-09-15 the committee is considering that
903 question. EDG 3.0 allows that syntax. Therefore, we do as
904 well. */
905 if (qualifying_scope && TYPE_P (qualifying_scope))
906 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
907
908 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
909 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
910 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
911
912 declarator = make_declarator (cdk_id);
913 declarator->u.id.qualifying_scope = qualifying_scope;
914 declarator->u.id.unqualified_name = unqualified_name;
915 declarator->u.id.sfk = sfk;
916
917 return declarator;
918 }
919
920 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
921 of modifiers such as const or volatile to apply to the pointer
922 type, represented as identifiers. */
923
924 cp_declarator *
make_pointer_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target)925 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
926 {
927 cp_declarator *declarator;
928
929 declarator = make_declarator (cdk_pointer);
930 declarator->declarator = target;
931 declarator->u.pointer.qualifiers = cv_qualifiers;
932 declarator->u.pointer.class_type = NULL_TREE;
933
934 return declarator;
935 }
936
937 /* Like make_pointer_declarator -- but for references. */
938
939 cp_declarator *
make_reference_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target)940 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 {
942 cp_declarator *declarator;
943
944 declarator = make_declarator (cdk_reference);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
948
949 return declarator;
950 }
951
952 /* Like make_pointer_declarator -- but for a pointer to a non-static
953 member of CLASS_TYPE. */
954
955 cp_declarator *
make_ptrmem_declarator(cp_cv_quals cv_qualifiers,tree class_type,cp_declarator * pointee)956 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
957 cp_declarator *pointee)
958 {
959 cp_declarator *declarator;
960
961 declarator = make_declarator (cdk_ptrmem);
962 declarator->declarator = pointee;
963 declarator->u.pointer.qualifiers = cv_qualifiers;
964 declarator->u.pointer.class_type = class_type;
965
966 return declarator;
967 }
968
969 /* Make a declarator for the function given by TARGET, with the
970 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
971 "const"-qualified member function. The EXCEPTION_SPECIFICATION
972 indicates what exceptions can be thrown. */
973
974 cp_declarator *
make_call_declarator(cp_declarator * target,cp_parameter_declarator * parms,cp_cv_quals cv_qualifiers,tree exception_specification)975 make_call_declarator (cp_declarator *target,
976 cp_parameter_declarator *parms,
977 cp_cv_quals cv_qualifiers,
978 tree exception_specification)
979 {
980 cp_declarator *declarator;
981
982 declarator = make_declarator (cdk_function);
983 declarator->declarator = target;
984 declarator->u.function.parameters = parms;
985 declarator->u.function.qualifiers = cv_qualifiers;
986 declarator->u.function.exception_specification = exception_specification;
987
988 return declarator;
989 }
990
991 /* Make a declarator for an array of BOUNDS elements, each of which is
992 defined by ELEMENT. */
993
994 cp_declarator *
make_array_declarator(cp_declarator * element,tree bounds)995 make_array_declarator (cp_declarator *element, tree bounds)
996 {
997 cp_declarator *declarator;
998
999 declarator = make_declarator (cdk_array);
1000 declarator->declarator = element;
1001 declarator->u.array.bounds = bounds;
1002
1003 return declarator;
1004 }
1005
1006 cp_parameter_declarator *no_parameters;
1007
1008 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1009 DECLARATOR and DEFAULT_ARGUMENT. */
1010
1011 cp_parameter_declarator *
make_parameter_declarator(cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree default_argument)1012 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1013 cp_declarator *declarator,
1014 tree default_argument)
1015 {
1016 cp_parameter_declarator *parameter;
1017
1018 parameter = ((cp_parameter_declarator *)
1019 alloc_declarator (sizeof (cp_parameter_declarator)));
1020 parameter->next = NULL;
1021 if (decl_specifiers)
1022 parameter->decl_specifiers = *decl_specifiers;
1023 else
1024 clear_decl_specs (¶meter->decl_specifiers);
1025 parameter->declarator = declarator;
1026 parameter->default_argument = default_argument;
1027 parameter->ellipsis_p = false;
1028
1029 return parameter;
1030 }
1031
1032 /* Returns true iff DECLARATOR is a declaration for a function. */
1033
1034 static bool
function_declarator_p(const cp_declarator * declarator)1035 function_declarator_p (const cp_declarator *declarator)
1036 {
1037 while (declarator)
1038 {
1039 if (declarator->kind == cdk_function
1040 && declarator->declarator->kind == cdk_id)
1041 return true;
1042 if (declarator->kind == cdk_id
1043 || declarator->kind == cdk_error)
1044 return false;
1045 declarator = declarator->declarator;
1046 }
1047 return false;
1048 }
1049
1050 /* The parser. */
1051
1052 /* Overview
1053 --------
1054
1055 A cp_parser parses the token stream as specified by the C++
1056 grammar. Its job is purely parsing, not semantic analysis. For
1057 example, the parser breaks the token stream into declarators,
1058 expressions, statements, and other similar syntactic constructs.
1059 It does not check that the types of the expressions on either side
1060 of an assignment-statement are compatible, or that a function is
1061 not declared with a parameter of type `void'.
1062
1063 The parser invokes routines elsewhere in the compiler to perform
1064 semantic analysis and to build up the abstract syntax tree for the
1065 code processed.
1066
1067 The parser (and the template instantiation code, which is, in a
1068 way, a close relative of parsing) are the only parts of the
1069 compiler that should be calling push_scope and pop_scope, or
1070 related functions. The parser (and template instantiation code)
1071 keeps track of what scope is presently active; everything else
1072 should simply honor that. (The code that generates static
1073 initializers may also need to set the scope, in order to check
1074 access control correctly when emitting the initializers.)
1075
1076 Methodology
1077 -----------
1078
1079 The parser is of the standard recursive-descent variety. Upcoming
1080 tokens in the token stream are examined in order to determine which
1081 production to use when parsing a non-terminal. Some C++ constructs
1082 require arbitrary look ahead to disambiguate. For example, it is
1083 impossible, in the general case, to tell whether a statement is an
1084 expression or declaration without scanning the entire statement.
1085 Therefore, the parser is capable of "parsing tentatively." When the
1086 parser is not sure what construct comes next, it enters this mode.
1087 Then, while we attempt to parse the construct, the parser queues up
1088 error messages, rather than issuing them immediately, and saves the
1089 tokens it consumes. If the construct is parsed successfully, the
1090 parser "commits", i.e., it issues any queued error messages and
1091 the tokens that were being preserved are permanently discarded.
1092 If, however, the construct is not parsed successfully, the parser
1093 rolls back its state completely so that it can resume parsing using
1094 a different alternative.
1095
1096 Future Improvements
1097 -------------------
1098
1099 The performance of the parser could probably be improved substantially.
1100 We could often eliminate the need to parse tentatively by looking ahead
1101 a little bit. In some places, this approach might not entirely eliminate
1102 the need to parse tentatively, but it might still speed up the average
1103 case. */
1104
1105 /* Flags that are passed to some parsing functions. These values can
1106 be bitwise-ored together. */
1107
1108 typedef enum cp_parser_flags
1109 {
1110 /* No flags. */
1111 CP_PARSER_FLAGS_NONE = 0x0,
1112 /* The construct is optional. If it is not present, then no error
1113 should be issued. */
1114 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1115 /* When parsing a type-specifier, do not allow user-defined types. */
1116 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1117 } cp_parser_flags;
1118
1119 /* The different kinds of declarators we want to parse. */
1120
1121 typedef enum cp_parser_declarator_kind
1122 {
1123 /* We want an abstract declarator. */
1124 CP_PARSER_DECLARATOR_ABSTRACT,
1125 /* We want a named declarator. */
1126 CP_PARSER_DECLARATOR_NAMED,
1127 /* We don't mind, but the name must be an unqualified-id. */
1128 CP_PARSER_DECLARATOR_EITHER
1129 } cp_parser_declarator_kind;
1130
1131 /* The precedence values used to parse binary expressions. The minimum value
1132 of PREC must be 1, because zero is reserved to quickly discriminate
1133 binary operators from other tokens. */
1134
1135 enum cp_parser_prec
1136 {
1137 PREC_NOT_OPERATOR,
1138 PREC_LOGICAL_OR_EXPRESSION,
1139 PREC_LOGICAL_AND_EXPRESSION,
1140 PREC_INCLUSIVE_OR_EXPRESSION,
1141 PREC_EXCLUSIVE_OR_EXPRESSION,
1142 PREC_AND_EXPRESSION,
1143 PREC_EQUALITY_EXPRESSION,
1144 PREC_RELATIONAL_EXPRESSION,
1145 PREC_SHIFT_EXPRESSION,
1146 PREC_ADDITIVE_EXPRESSION,
1147 PREC_MULTIPLICATIVE_EXPRESSION,
1148 PREC_PM_EXPRESSION,
1149 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1150 };
1151
1152 /* A mapping from a token type to a corresponding tree node type, with a
1153 precedence value. */
1154
1155 typedef struct cp_parser_binary_operations_map_node
1156 {
1157 /* The token type. */
1158 enum cpp_ttype token_type;
1159 /* The corresponding tree code. */
1160 enum tree_code tree_type;
1161 /* The precedence of this operator. */
1162 enum cp_parser_prec prec;
1163 } cp_parser_binary_operations_map_node;
1164
1165 /* The status of a tentative parse. */
1166
1167 typedef enum cp_parser_status_kind
1168 {
1169 /* No errors have occurred. */
1170 CP_PARSER_STATUS_KIND_NO_ERROR,
1171 /* An error has occurred. */
1172 CP_PARSER_STATUS_KIND_ERROR,
1173 /* We are committed to this tentative parse, whether or not an error
1174 has occurred. */
1175 CP_PARSER_STATUS_KIND_COMMITTED
1176 } cp_parser_status_kind;
1177
1178 typedef struct cp_parser_expression_stack_entry
1179 {
1180 tree lhs;
1181 enum tree_code tree_type;
1182 int prec;
1183 } cp_parser_expression_stack_entry;
1184
1185 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1186 entries because precedence levels on the stack are monotonically
1187 increasing. */
1188 typedef struct cp_parser_expression_stack_entry
1189 cp_parser_expression_stack[NUM_PREC_VALUES];
1190
1191 /* Context that is saved and restored when parsing tentatively. */
1192 typedef struct cp_parser_context GTY (())
1193 {
1194 /* If this is a tentative parsing context, the status of the
1195 tentative parse. */
1196 enum cp_parser_status_kind status;
1197 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1198 that are looked up in this context must be looked up both in the
1199 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1200 the context of the containing expression. */
1201 tree object_type;
1202
1203 /* The next parsing context in the stack. */
1204 struct cp_parser_context *next;
1205 } cp_parser_context;
1206
1207 /* Prototypes. */
1208
1209 /* Constructors and destructors. */
1210
1211 static cp_parser_context *cp_parser_context_new
1212 (cp_parser_context *);
1213
1214 /* Class variables. */
1215
1216 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1217
1218 /* The operator-precedence table used by cp_parser_binary_expression.
1219 Transformed into an associative array (binops_by_token) by
1220 cp_parser_new. */
1221
1222 static const cp_parser_binary_operations_map_node binops[] = {
1223 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1224 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1225
1226 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1227 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1228 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1229
1230 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1231 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1232
1233 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1234 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1235
1236 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1237 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1238 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1239 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1240
1241 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1242 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1243
1244 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1245
1246 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1247
1248 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1249
1250 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1251
1252 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1253 };
1254
1255 /* The same as binops, but initialized by cp_parser_new so that
1256 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1257 for speed. */
1258 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1259
1260 /* Constructors and destructors. */
1261
1262 /* Construct a new context. The context below this one on the stack
1263 is given by NEXT. */
1264
1265 static cp_parser_context *
cp_parser_context_new(cp_parser_context * next)1266 cp_parser_context_new (cp_parser_context* next)
1267 {
1268 cp_parser_context *context;
1269
1270 /* Allocate the storage. */
1271 if (cp_parser_context_free_list != NULL)
1272 {
1273 /* Pull the first entry from the free list. */
1274 context = cp_parser_context_free_list;
1275 cp_parser_context_free_list = context->next;
1276 memset (context, 0, sizeof (*context));
1277 }
1278 else
1279 context = GGC_CNEW (cp_parser_context);
1280
1281 /* No errors have occurred yet in this context. */
1282 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1283 /* If this is not the bottomost context, copy information that we
1284 need from the previous context. */
1285 if (next)
1286 {
1287 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1288 expression, then we are parsing one in this context, too. */
1289 context->object_type = next->object_type;
1290 /* Thread the stack. */
1291 context->next = next;
1292 }
1293
1294 return context;
1295 }
1296
1297 /* The cp_parser structure represents the C++ parser. */
1298
1299 typedef struct cp_parser GTY(())
1300 {
1301 /* The lexer from which we are obtaining tokens. */
1302 cp_lexer *lexer;
1303
1304 /* The scope in which names should be looked up. If NULL_TREE, then
1305 we look up names in the scope that is currently open in the
1306 source program. If non-NULL, this is either a TYPE or
1307 NAMESPACE_DECL for the scope in which we should look. It can
1308 also be ERROR_MARK, when we've parsed a bogus scope.
1309
1310 This value is not cleared automatically after a name is looked
1311 up, so we must be careful to clear it before starting a new look
1312 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1313 will look up `Z' in the scope of `X', rather than the current
1314 scope.) Unfortunately, it is difficult to tell when name lookup
1315 is complete, because we sometimes peek at a token, look it up,
1316 and then decide not to consume it. */
1317 tree scope;
1318
1319 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1320 last lookup took place. OBJECT_SCOPE is used if an expression
1321 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1322 respectively. QUALIFYING_SCOPE is used for an expression of the
1323 form "X::Y"; it refers to X. */
1324 tree object_scope;
1325 tree qualifying_scope;
1326
1327 /* A stack of parsing contexts. All but the bottom entry on the
1328 stack will be tentative contexts.
1329
1330 We parse tentatively in order to determine which construct is in
1331 use in some situations. For example, in order to determine
1332 whether a statement is an expression-statement or a
1333 declaration-statement we parse it tentatively as a
1334 declaration-statement. If that fails, we then reparse the same
1335 token stream as an expression-statement. */
1336 cp_parser_context *context;
1337
1338 /* True if we are parsing GNU C++. If this flag is not set, then
1339 GNU extensions are not recognized. */
1340 bool allow_gnu_extensions_p;
1341
1342 /* TRUE if the `>' token should be interpreted as the greater-than
1343 operator. FALSE if it is the end of a template-id or
1344 template-parameter-list. */
1345 bool greater_than_is_operator_p;
1346
1347 /* TRUE if default arguments are allowed within a parameter list
1348 that starts at this point. FALSE if only a gnu extension makes
1349 them permissible. */
1350 bool default_arg_ok_p;
1351
1352 /* TRUE if we are parsing an integral constant-expression. See
1353 [expr.const] for a precise definition. */
1354 bool integral_constant_expression_p;
1355
1356 /* TRUE if we are parsing an integral constant-expression -- but a
1357 non-constant expression should be permitted as well. This flag
1358 is used when parsing an array bound so that GNU variable-length
1359 arrays are tolerated. */
1360 bool allow_non_integral_constant_expression_p;
1361
1362 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1363 been seen that makes the expression non-constant. */
1364 bool non_integral_constant_expression_p;
1365
1366 /* TRUE if local variable names and `this' are forbidden in the
1367 current context. */
1368 bool local_variables_forbidden_p;
1369
1370 /* TRUE if the declaration we are parsing is part of a
1371 linkage-specification of the form `extern string-literal
1372 declaration'. */
1373 bool in_unbraced_linkage_specification_p;
1374
1375 /* TRUE if we are presently parsing a declarator, after the
1376 direct-declarator. */
1377 bool in_declarator_p;
1378
1379 /* TRUE if we are presently parsing a template-argument-list. */
1380 bool in_template_argument_list_p;
1381
1382 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1383 to IN_OMP_BLOCK if parsing OpenMP structured block and
1384 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1385 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1386 iteration-statement, OpenMP block or loop within that switch. */
1387 #define IN_SWITCH_STMT 1
1388 #define IN_ITERATION_STMT 2
1389 #define IN_OMP_BLOCK 4
1390 #define IN_OMP_FOR 8
1391 unsigned char in_statement;
1392
1393 /* TRUE if we are presently parsing the body of a switch statement.
1394 Note that this doesn't quite overlap with in_statement above.
1395 The difference relates to giving the right sets of error messages:
1396 "case not in switch" vs "break statement used with OpenMP...". */
1397 bool in_switch_statement_p;
1398
1399 /* TRUE if we are parsing a type-id in an expression context. In
1400 such a situation, both "type (expr)" and "type (type)" are valid
1401 alternatives. */
1402 bool in_type_id_in_expr_p;
1403
1404 /* TRUE if we are currently in a header file where declarations are
1405 implicitly extern "C". */
1406 bool implicit_extern_c;
1407
1408 /* TRUE if strings in expressions should be translated to the execution
1409 character set. */
1410 bool translate_strings_p;
1411
1412 /* TRUE if we are presently parsing the body of a function, but not
1413 a local class. */
1414 bool in_function_body;
1415
1416 /* If non-NULL, then we are parsing a construct where new type
1417 definitions are not permitted. The string stored here will be
1418 issued as an error message if a type is defined. */
1419 const char *type_definition_forbidden_message;
1420
1421 /* A list of lists. The outer list is a stack, used for member
1422 functions of local classes. At each level there are two sub-list,
1423 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1424 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1425 TREE_VALUE's. The functions are chained in reverse declaration
1426 order.
1427
1428 The TREE_PURPOSE sublist contains those functions with default
1429 arguments that need post processing, and the TREE_VALUE sublist
1430 contains those functions with definitions that need post
1431 processing.
1432
1433 These lists can only be processed once the outermost class being
1434 defined is complete. */
1435 tree unparsed_functions_queues;
1436
1437 /* The number of classes whose definitions are currently in
1438 progress. */
1439 unsigned num_classes_being_defined;
1440
1441 /* The number of template parameter lists that apply directly to the
1442 current declaration. */
1443 unsigned num_template_parameter_lists;
1444 } cp_parser;
1445
1446 /* Prototypes. */
1447
1448 /* Constructors and destructors. */
1449
1450 static cp_parser *cp_parser_new
1451 (void);
1452
1453 /* Routines to parse various constructs.
1454
1455 Those that return `tree' will return the error_mark_node (rather
1456 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1457 Sometimes, they will return an ordinary node if error-recovery was
1458 attempted, even though a parse error occurred. So, to check
1459 whether or not a parse error occurred, you should always use
1460 cp_parser_error_occurred. If the construct is optional (indicated
1461 either by an `_opt' in the name of the function that does the
1462 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1463 the construct is not present. */
1464
1465 /* Lexical conventions [gram.lex] */
1466
1467 static tree cp_parser_identifier
1468 (cp_parser *);
1469 static tree cp_parser_string_literal
1470 (cp_parser *, bool, bool);
1471
1472 /* Basic concepts [gram.basic] */
1473
1474 static bool cp_parser_translation_unit
1475 (cp_parser *);
1476
1477 /* Expressions [gram.expr] */
1478
1479 static tree cp_parser_primary_expression
1480 (cp_parser *, bool, bool, bool, cp_id_kind *);
1481 static tree cp_parser_id_expression
1482 (cp_parser *, bool, bool, bool *, bool, bool);
1483 static tree cp_parser_unqualified_id
1484 (cp_parser *, bool, bool, bool, bool);
1485 static tree cp_parser_nested_name_specifier_opt
1486 (cp_parser *, bool, bool, bool, bool);
1487 static tree cp_parser_nested_name_specifier
1488 (cp_parser *, bool, bool, bool, bool);
1489 static tree cp_parser_class_or_namespace_name
1490 (cp_parser *, bool, bool, bool, bool, bool);
1491 static tree cp_parser_postfix_expression
1492 (cp_parser *, bool, bool);
1493 static tree cp_parser_postfix_open_square_expression
1494 (cp_parser *, tree, bool);
1495 static tree cp_parser_postfix_dot_deref_expression
1496 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1497 static tree cp_parser_parenthesized_expression_list
1498 (cp_parser *, bool, bool, bool *);
1499 static void cp_parser_pseudo_destructor_name
1500 (cp_parser *, tree *, tree *);
1501 static tree cp_parser_unary_expression
1502 (cp_parser *, bool, bool);
1503 static enum tree_code cp_parser_unary_operator
1504 (cp_token *);
1505 static tree cp_parser_new_expression
1506 (cp_parser *);
1507 static tree cp_parser_new_placement
1508 (cp_parser *);
1509 static tree cp_parser_new_type_id
1510 (cp_parser *, tree *);
1511 static cp_declarator *cp_parser_new_declarator_opt
1512 (cp_parser *);
1513 static cp_declarator *cp_parser_direct_new_declarator
1514 (cp_parser *);
1515 static tree cp_parser_new_initializer
1516 (cp_parser *);
1517 static tree cp_parser_delete_expression
1518 (cp_parser *);
1519 static tree cp_parser_cast_expression
1520 (cp_parser *, bool, bool);
1521 static tree cp_parser_binary_expression
1522 (cp_parser *, bool);
1523 static tree cp_parser_question_colon_clause
1524 (cp_parser *, tree);
1525 static tree cp_parser_assignment_expression
1526 (cp_parser *, bool);
1527 static enum tree_code cp_parser_assignment_operator_opt
1528 (cp_parser *);
1529 static tree cp_parser_expression
1530 (cp_parser *, bool);
1531 static tree cp_parser_constant_expression
1532 (cp_parser *, bool, bool *);
1533 static tree cp_parser_builtin_offsetof
1534 (cp_parser *);
1535
1536 /* Statements [gram.stmt.stmt] */
1537
1538 static void cp_parser_statement
1539 (cp_parser *, tree, bool);
1540 static void cp_parser_label_for_labeled_statement
1541 (cp_parser *);
1542 static tree cp_parser_expression_statement
1543 (cp_parser *, tree);
1544 static tree cp_parser_compound_statement
1545 (cp_parser *, tree, bool);
1546 static void cp_parser_statement_seq_opt
1547 (cp_parser *, tree);
1548 static tree cp_parser_selection_statement
1549 (cp_parser *);
1550 static tree cp_parser_condition
1551 (cp_parser *);
1552 static tree cp_parser_iteration_statement
1553 (cp_parser *);
1554 static void cp_parser_for_init_statement
1555 (cp_parser *);
1556 static tree cp_parser_jump_statement
1557 (cp_parser *);
1558 static void cp_parser_declaration_statement
1559 (cp_parser *);
1560
1561 static tree cp_parser_implicitly_scoped_statement
1562 (cp_parser *);
1563 static void cp_parser_already_scoped_statement
1564 (cp_parser *);
1565
1566 /* Declarations [gram.dcl.dcl] */
1567
1568 static void cp_parser_declaration_seq_opt
1569 (cp_parser *);
1570 static void cp_parser_declaration
1571 (cp_parser *);
1572 static void cp_parser_block_declaration
1573 (cp_parser *, bool);
1574 static void cp_parser_simple_declaration
1575 (cp_parser *, bool);
1576 static void cp_parser_decl_specifier_seq
1577 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1578 static tree cp_parser_storage_class_specifier_opt
1579 (cp_parser *);
1580 static tree cp_parser_function_specifier_opt
1581 (cp_parser *, cp_decl_specifier_seq *);
1582 static tree cp_parser_type_specifier
1583 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1584 int *, bool *);
1585 static tree cp_parser_simple_type_specifier
1586 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1587 static tree cp_parser_type_name
1588 (cp_parser *);
1589 static tree cp_parser_elaborated_type_specifier
1590 (cp_parser *, bool, bool);
1591 static tree cp_parser_enum_specifier
1592 (cp_parser *);
1593 static void cp_parser_enumerator_list
1594 (cp_parser *, tree);
1595 static void cp_parser_enumerator_definition
1596 (cp_parser *, tree);
1597 static tree cp_parser_namespace_name
1598 (cp_parser *);
1599 static void cp_parser_namespace_definition
1600 (cp_parser *);
1601 static void cp_parser_namespace_body
1602 (cp_parser *);
1603 static tree cp_parser_qualified_namespace_specifier
1604 (cp_parser *);
1605 static void cp_parser_namespace_alias_definition
1606 (cp_parser *);
1607 static bool cp_parser_using_declaration
1608 (cp_parser *, bool);
1609 static void cp_parser_using_directive
1610 (cp_parser *);
1611 static void cp_parser_asm_definition
1612 (cp_parser *);
1613 static void cp_parser_linkage_specification
1614 (cp_parser *);
1615
1616 /* Declarators [gram.dcl.decl] */
1617
1618 static tree cp_parser_init_declarator
1619 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1620 static cp_declarator *cp_parser_declarator
1621 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1622 static cp_declarator *cp_parser_direct_declarator
1623 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1624 static enum tree_code cp_parser_ptr_operator
1625 (cp_parser *, tree *, cp_cv_quals *);
1626 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1627 (cp_parser *);
1628 static tree cp_parser_declarator_id
1629 (cp_parser *, bool);
1630 static tree cp_parser_type_id
1631 (cp_parser *);
1632 static void cp_parser_type_specifier_seq
1633 (cp_parser *, bool, cp_decl_specifier_seq *);
1634 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1635 (cp_parser *);
1636 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1637 (cp_parser *, bool *);
1638 static cp_parameter_declarator *cp_parser_parameter_declaration
1639 (cp_parser *, bool, bool *);
1640 static void cp_parser_function_body
1641 (cp_parser *);
1642 static tree cp_parser_initializer
1643 (cp_parser *, bool *, bool *);
1644 static tree cp_parser_initializer_clause
1645 (cp_parser *, bool *);
1646 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1647 (cp_parser *, bool *);
1648
1649 static bool cp_parser_ctor_initializer_opt_and_function_body
1650 (cp_parser *);
1651
1652 /* Classes [gram.class] */
1653
1654 static tree cp_parser_class_name
1655 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1656 static tree cp_parser_class_specifier
1657 (cp_parser *);
1658 static tree cp_parser_class_head
1659 (cp_parser *, bool *, tree *, tree *);
1660 static enum tag_types cp_parser_class_key
1661 (cp_parser *);
1662 static void cp_parser_member_specification_opt
1663 (cp_parser *);
1664 static void cp_parser_member_declaration
1665 (cp_parser *);
1666 static tree cp_parser_pure_specifier
1667 (cp_parser *);
1668 static tree cp_parser_constant_initializer
1669 (cp_parser *);
1670
1671 /* Derived classes [gram.class.derived] */
1672
1673 static tree cp_parser_base_clause
1674 (cp_parser *);
1675 static tree cp_parser_base_specifier
1676 (cp_parser *);
1677
1678 /* Special member functions [gram.special] */
1679
1680 static tree cp_parser_conversion_function_id
1681 (cp_parser *);
1682 static tree cp_parser_conversion_type_id
1683 (cp_parser *);
1684 static cp_declarator *cp_parser_conversion_declarator_opt
1685 (cp_parser *);
1686 static bool cp_parser_ctor_initializer_opt
1687 (cp_parser *);
1688 static void cp_parser_mem_initializer_list
1689 (cp_parser *);
1690 static tree cp_parser_mem_initializer
1691 (cp_parser *);
1692 static tree cp_parser_mem_initializer_id
1693 (cp_parser *);
1694
1695 /* Overloading [gram.over] */
1696
1697 static tree cp_parser_operator_function_id
1698 (cp_parser *);
1699 static tree cp_parser_operator
1700 (cp_parser *);
1701
1702 /* Templates [gram.temp] */
1703
1704 static void cp_parser_template_declaration
1705 (cp_parser *, bool);
1706 static tree cp_parser_template_parameter_list
1707 (cp_parser *);
1708 static tree cp_parser_template_parameter
1709 (cp_parser *, bool *);
1710 static tree cp_parser_type_parameter
1711 (cp_parser *);
1712 static tree cp_parser_template_id
1713 (cp_parser *, bool, bool, bool);
1714 static tree cp_parser_template_name
1715 (cp_parser *, bool, bool, bool, bool *);
1716 static tree cp_parser_template_argument_list
1717 (cp_parser *);
1718 static tree cp_parser_template_argument
1719 (cp_parser *);
1720 static void cp_parser_explicit_instantiation
1721 (cp_parser *);
1722 static void cp_parser_explicit_specialization
1723 (cp_parser *);
1724
1725 /* Exception handling [gram.exception] */
1726
1727 static tree cp_parser_try_block
1728 (cp_parser *);
1729 static bool cp_parser_function_try_block
1730 (cp_parser *);
1731 static void cp_parser_handler_seq
1732 (cp_parser *);
1733 static void cp_parser_handler
1734 (cp_parser *);
1735 static tree cp_parser_exception_declaration
1736 (cp_parser *);
1737 static tree cp_parser_throw_expression
1738 (cp_parser *);
1739 static tree cp_parser_exception_specification_opt
1740 (cp_parser *);
1741 static tree cp_parser_type_id_list
1742 (cp_parser *);
1743
1744 /* GNU Extensions */
1745
1746 static tree cp_parser_asm_specification_opt
1747 (cp_parser *);
1748 static tree cp_parser_asm_operand_list
1749 (cp_parser *);
1750 static tree cp_parser_asm_clobber_list
1751 (cp_parser *);
1752 static tree cp_parser_attributes_opt
1753 (cp_parser *);
1754 static tree cp_parser_attribute_list
1755 (cp_parser *);
1756 static bool cp_parser_extension_opt
1757 (cp_parser *, int *);
1758 static void cp_parser_label_declaration
1759 (cp_parser *);
1760
1761 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1762 static bool cp_parser_pragma
1763 (cp_parser *, enum pragma_context);
1764
1765 /* Objective-C++ Productions */
1766
1767 static tree cp_parser_objc_message_receiver
1768 (cp_parser *);
1769 static tree cp_parser_objc_message_args
1770 (cp_parser *);
1771 static tree cp_parser_objc_message_expression
1772 (cp_parser *);
1773 static tree cp_parser_objc_encode_expression
1774 (cp_parser *);
1775 static tree cp_parser_objc_defs_expression
1776 (cp_parser *);
1777 static tree cp_parser_objc_protocol_expression
1778 (cp_parser *);
1779 static tree cp_parser_objc_selector_expression
1780 (cp_parser *);
1781 static tree cp_parser_objc_expression
1782 (cp_parser *);
1783 static bool cp_parser_objc_selector_p
1784 (enum cpp_ttype);
1785 static tree cp_parser_objc_selector
1786 (cp_parser *);
1787 static tree cp_parser_objc_protocol_refs_opt
1788 (cp_parser *);
1789 static void cp_parser_objc_declaration
1790 (cp_parser *);
1791 static tree cp_parser_objc_statement
1792 (cp_parser *);
1793
1794 /* Utility Routines */
1795
1796 static tree cp_parser_lookup_name
1797 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1798 static tree cp_parser_lookup_name_simple
1799 (cp_parser *, tree);
1800 static tree cp_parser_maybe_treat_template_as_class
1801 (tree, bool);
1802 static bool cp_parser_check_declarator_template_parameters
1803 (cp_parser *, cp_declarator *);
1804 static bool cp_parser_check_template_parameters
1805 (cp_parser *, unsigned);
1806 static tree cp_parser_simple_cast_expression
1807 (cp_parser *);
1808 static tree cp_parser_global_scope_opt
1809 (cp_parser *, bool);
1810 static bool cp_parser_constructor_declarator_p
1811 (cp_parser *, bool);
1812 static tree cp_parser_function_definition_from_specifiers_and_declarator
1813 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1814 static tree cp_parser_function_definition_after_declarator
1815 (cp_parser *, bool);
1816 static void cp_parser_template_declaration_after_export
1817 (cp_parser *, bool);
1818 static void cp_parser_perform_template_parameter_access_checks
1819 (VEC (deferred_access_check,gc)*);
1820 static tree cp_parser_single_declaration
1821 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1822 static tree cp_parser_functional_cast
1823 (cp_parser *, tree);
1824 static tree cp_parser_save_member_function_body
1825 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1826 static tree cp_parser_enclosed_template_argument_list
1827 (cp_parser *);
1828 static void cp_parser_save_default_args
1829 (cp_parser *, tree);
1830 static void cp_parser_late_parsing_for_member
1831 (cp_parser *, tree);
1832 static void cp_parser_late_parsing_default_args
1833 (cp_parser *, tree);
1834 static tree cp_parser_sizeof_operand
1835 (cp_parser *, enum rid);
1836 static bool cp_parser_declares_only_class_p
1837 (cp_parser *);
1838 static void cp_parser_set_storage_class
1839 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1840 static void cp_parser_set_decl_spec_type
1841 (cp_decl_specifier_seq *, tree, bool);
1842 static bool cp_parser_friend_p
1843 (const cp_decl_specifier_seq *);
1844 static cp_token *cp_parser_require
1845 (cp_parser *, enum cpp_ttype, const char *);
1846 static cp_token *cp_parser_require_keyword
1847 (cp_parser *, enum rid, const char *);
1848 static bool cp_parser_token_starts_function_definition_p
1849 (cp_token *);
1850 static bool cp_parser_next_token_starts_class_definition_p
1851 (cp_parser *);
1852 static bool cp_parser_next_token_ends_template_argument_p
1853 (cp_parser *);
1854 static bool cp_parser_nth_token_starts_template_argument_list_p
1855 (cp_parser *, size_t);
1856 static enum tag_types cp_parser_token_is_class_key
1857 (cp_token *);
1858 static void cp_parser_check_class_key
1859 (enum tag_types, tree type);
1860 static void cp_parser_check_access_in_redeclaration
1861 (tree type);
1862 static bool cp_parser_optional_template_keyword
1863 (cp_parser *);
1864 static void cp_parser_pre_parsed_nested_name_specifier
1865 (cp_parser *);
1866 static void cp_parser_cache_group
1867 (cp_parser *, enum cpp_ttype, unsigned);
1868 static void cp_parser_parse_tentatively
1869 (cp_parser *);
1870 static void cp_parser_commit_to_tentative_parse
1871 (cp_parser *);
1872 static void cp_parser_abort_tentative_parse
1873 (cp_parser *);
1874 static bool cp_parser_parse_definitely
1875 (cp_parser *);
1876 static inline bool cp_parser_parsing_tentatively
1877 (cp_parser *);
1878 static bool cp_parser_uncommitted_to_tentative_parse_p
1879 (cp_parser *);
1880 static void cp_parser_error
1881 (cp_parser *, const char *);
1882 static void cp_parser_name_lookup_error
1883 (cp_parser *, tree, tree, const char *);
1884 static bool cp_parser_simulate_error
1885 (cp_parser *);
1886 static bool cp_parser_check_type_definition
1887 (cp_parser *);
1888 static void cp_parser_check_for_definition_in_return_type
1889 (cp_declarator *, tree);
1890 static void cp_parser_check_for_invalid_template_id
1891 (cp_parser *, tree);
1892 static bool cp_parser_non_integral_constant_expression
1893 (cp_parser *, const char *);
1894 static void cp_parser_diagnose_invalid_type_name
1895 (cp_parser *, tree, tree);
1896 static bool cp_parser_parse_and_diagnose_invalid_type_name
1897 (cp_parser *);
1898 static int cp_parser_skip_to_closing_parenthesis
1899 (cp_parser *, bool, bool, bool);
1900 static void cp_parser_skip_to_end_of_statement
1901 (cp_parser *);
1902 static void cp_parser_consume_semicolon_at_end_of_statement
1903 (cp_parser *);
1904 static void cp_parser_skip_to_end_of_block_or_statement
1905 (cp_parser *);
1906 static void cp_parser_skip_to_closing_brace
1907 (cp_parser *);
1908 static void cp_parser_skip_to_end_of_template_parameter_list
1909 (cp_parser *);
1910 static void cp_parser_skip_to_pragma_eol
1911 (cp_parser*, cp_token *);
1912 static bool cp_parser_error_occurred
1913 (cp_parser *);
1914 static bool cp_parser_allow_gnu_extensions_p
1915 (cp_parser *);
1916 static bool cp_parser_is_string_literal
1917 (cp_token *);
1918 static bool cp_parser_is_keyword
1919 (cp_token *, enum rid);
1920 static tree cp_parser_make_typename_type
1921 (cp_parser *, tree, tree);
1922
1923 /* Returns nonzero if we are parsing tentatively. */
1924
1925 static inline bool
cp_parser_parsing_tentatively(cp_parser * parser)1926 cp_parser_parsing_tentatively (cp_parser* parser)
1927 {
1928 return parser->context->next != NULL;
1929 }
1930
1931 /* Returns nonzero if TOKEN is a string literal. */
1932
1933 static bool
cp_parser_is_string_literal(cp_token * token)1934 cp_parser_is_string_literal (cp_token* token)
1935 {
1936 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1937 }
1938
1939 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1940
1941 static bool
cp_parser_is_keyword(cp_token * token,enum rid keyword)1942 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1943 {
1944 return token->keyword == keyword;
1945 }
1946
1947 /* If not parsing tentatively, issue a diagnostic of the form
1948 FILE:LINE: MESSAGE before TOKEN
1949 where TOKEN is the next token in the input stream. MESSAGE
1950 (specified by the caller) is usually of the form "expected
1951 OTHER-TOKEN". */
1952
1953 static void
cp_parser_error(cp_parser * parser,const char * message)1954 cp_parser_error (cp_parser* parser, const char* message)
1955 {
1956 if (!cp_parser_simulate_error (parser))
1957 {
1958 cp_token *token = cp_lexer_peek_token (parser->lexer);
1959 /* This diagnostic makes more sense if it is tagged to the line
1960 of the token we just peeked at. */
1961 cp_lexer_set_source_position_from_token (token);
1962
1963 if (token->type == CPP_PRAGMA)
1964 {
1965 error ("%<#pragma%> is not allowed here");
1966 cp_parser_skip_to_pragma_eol (parser, token);
1967 return;
1968 }
1969
1970 c_parse_error (message,
1971 /* Because c_parser_error does not understand
1972 CPP_KEYWORD, keywords are treated like
1973 identifiers. */
1974 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1975 token->u.value);
1976 }
1977 }
1978
1979 /* Issue an error about name-lookup failing. NAME is the
1980 IDENTIFIER_NODE DECL is the result of
1981 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1982 the thing that we hoped to find. */
1983
1984 static void
cp_parser_name_lookup_error(cp_parser * parser,tree name,tree decl,const char * desired)1985 cp_parser_name_lookup_error (cp_parser* parser,
1986 tree name,
1987 tree decl,
1988 const char* desired)
1989 {
1990 /* If name lookup completely failed, tell the user that NAME was not
1991 declared. */
1992 if (decl == error_mark_node)
1993 {
1994 if (parser->scope && parser->scope != global_namespace)
1995 error ("%<%D::%D%> has not been declared",
1996 parser->scope, name);
1997 else if (parser->scope == global_namespace)
1998 error ("%<::%D%> has not been declared", name);
1999 else if (parser->object_scope
2000 && !CLASS_TYPE_P (parser->object_scope))
2001 error ("request for member %qD in non-class type %qT",
2002 name, parser->object_scope);
2003 else if (parser->object_scope)
2004 error ("%<%T::%D%> has not been declared",
2005 parser->object_scope, name);
2006 else
2007 error ("%qD has not been declared", name);
2008 }
2009 else if (parser->scope && parser->scope != global_namespace)
2010 error ("%<%D::%D%> %s", parser->scope, name, desired);
2011 else if (parser->scope == global_namespace)
2012 error ("%<::%D%> %s", name, desired);
2013 else
2014 error ("%qD %s", name, desired);
2015 }
2016
2017 /* If we are parsing tentatively, remember that an error has occurred
2018 during this tentative parse. Returns true if the error was
2019 simulated; false if a message should be issued by the caller. */
2020
2021 static bool
cp_parser_simulate_error(cp_parser * parser)2022 cp_parser_simulate_error (cp_parser* parser)
2023 {
2024 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2025 {
2026 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2027 return true;
2028 }
2029 return false;
2030 }
2031
2032 /* Check for repeated decl-specifiers. */
2033
2034 static void
cp_parser_check_decl_spec(cp_decl_specifier_seq * decl_specs)2035 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2036 {
2037 cp_decl_spec ds;
2038
2039 for (ds = ds_first; ds != ds_last; ++ds)
2040 {
2041 unsigned count = decl_specs->specs[(int)ds];
2042 if (count < 2)
2043 continue;
2044 /* The "long" specifier is a special case because of "long long". */
2045 if (ds == ds_long)
2046 {
2047 if (count > 2)
2048 error ("%<long long long%> is too long for GCC");
2049 else if (pedantic && !in_system_header && warn_long_long)
2050 pedwarn ("ISO C++ does not support %<long long%>");
2051 }
2052 else if (count > 1)
2053 {
2054 static const char *const decl_spec_names[] = {
2055 "signed",
2056 "unsigned",
2057 "short",
2058 "long",
2059 "const",
2060 "volatile",
2061 "restrict",
2062 "inline",
2063 "virtual",
2064 "explicit",
2065 "friend",
2066 "typedef",
2067 "__complex",
2068 "__thread"
2069 };
2070 error ("duplicate %qs", decl_spec_names[(int)ds]);
2071 }
2072 }
2073 }
2074
2075 /* This function is called when a type is defined. If type
2076 definitions are forbidden at this point, an error message is
2077 issued. */
2078
2079 static bool
cp_parser_check_type_definition(cp_parser * parser)2080 cp_parser_check_type_definition (cp_parser* parser)
2081 {
2082 /* If types are forbidden here, issue a message. */
2083 if (parser->type_definition_forbidden_message)
2084 {
2085 /* Use `%s' to print the string in case there are any escape
2086 characters in the message. */
2087 error ("%s", parser->type_definition_forbidden_message);
2088 return false;
2089 }
2090 return true;
2091 }
2092
2093 /* This function is called when the DECLARATOR is processed. The TYPE
2094 was a type defined in the decl-specifiers. If it is invalid to
2095 define a type in the decl-specifiers for DECLARATOR, an error is
2096 issued. */
2097
2098 static void
cp_parser_check_for_definition_in_return_type(cp_declarator * declarator,tree type)2099 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2100 tree type)
2101 {
2102 /* [dcl.fct] forbids type definitions in return types.
2103 Unfortunately, it's not easy to know whether or not we are
2104 processing a return type until after the fact. */
2105 while (declarator
2106 && (declarator->kind == cdk_pointer
2107 || declarator->kind == cdk_reference
2108 || declarator->kind == cdk_ptrmem))
2109 declarator = declarator->declarator;
2110 if (declarator
2111 && declarator->kind == cdk_function)
2112 {
2113 error ("new types may not be defined in a return type");
2114 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2115 type);
2116 }
2117 }
2118
2119 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2120 "<" in any valid C++ program. If the next token is indeed "<",
2121 issue a message warning the user about what appears to be an
2122 invalid attempt to form a template-id. */
2123
2124 static void
cp_parser_check_for_invalid_template_id(cp_parser * parser,tree type)2125 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2126 tree type)
2127 {
2128 cp_token_position start = 0;
2129
2130 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2131 {
2132 if (TYPE_P (type))
2133 error ("%qT is not a template", type);
2134 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2135 error ("%qE is not a template", type);
2136 else
2137 error ("invalid template-id");
2138 /* Remember the location of the invalid "<". */
2139 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2140 start = cp_lexer_token_position (parser->lexer, true);
2141 /* Consume the "<". */
2142 cp_lexer_consume_token (parser->lexer);
2143 /* Parse the template arguments. */
2144 cp_parser_enclosed_template_argument_list (parser);
2145 /* Permanently remove the invalid template arguments so that
2146 this error message is not issued again. */
2147 if (start)
2148 cp_lexer_purge_tokens_after (parser->lexer, start);
2149 }
2150 }
2151
2152 /* If parsing an integral constant-expression, issue an error message
2153 about the fact that THING appeared and return true. Otherwise,
2154 return false. In either case, set
2155 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2156
2157 static bool
cp_parser_non_integral_constant_expression(cp_parser * parser,const char * thing)2158 cp_parser_non_integral_constant_expression (cp_parser *parser,
2159 const char *thing)
2160 {
2161 parser->non_integral_constant_expression_p = true;
2162 if (parser->integral_constant_expression_p)
2163 {
2164 if (!parser->allow_non_integral_constant_expression_p)
2165 {
2166 error ("%s cannot appear in a constant-expression", thing);
2167 return true;
2168 }
2169 }
2170 return false;
2171 }
2172
2173 /* Emit a diagnostic for an invalid type name. SCOPE is the
2174 qualifying scope (or NULL, if none) for ID. This function commits
2175 to the current active tentative parse, if any. (Otherwise, the
2176 problematic construct might be encountered again later, resulting
2177 in duplicate error messages.) */
2178
2179 static void
cp_parser_diagnose_invalid_type_name(cp_parser * parser,tree scope,tree id)2180 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2181 {
2182 tree decl, old_scope;
2183 /* Try to lookup the identifier. */
2184 old_scope = parser->scope;
2185 parser->scope = scope;
2186 decl = cp_parser_lookup_name_simple (parser, id);
2187 parser->scope = old_scope;
2188 /* If the lookup found a template-name, it means that the user forgot
2189 to specify an argument list. Emit a useful error message. */
2190 if (TREE_CODE (decl) == TEMPLATE_DECL)
2191 error ("invalid use of template-name %qE without an argument list", decl);
2192 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2193 error ("invalid use of destructor %qD as a type", id);
2194 else if (TREE_CODE (decl) == TYPE_DECL)
2195 /* Something like 'unsigned A a;' */
2196 error ("invalid combination of multiple type-specifiers");
2197 else if (!parser->scope)
2198 {
2199 /* Issue an error message. */
2200 error ("%qE does not name a type", id);
2201 /* If we're in a template class, it's possible that the user was
2202 referring to a type from a base class. For example:
2203
2204 template <typename T> struct A { typedef T X; };
2205 template <typename T> struct B : public A<T> { X x; };
2206
2207 The user should have said "typename A<T>::X". */
2208 if (processing_template_decl && current_class_type
2209 && TYPE_BINFO (current_class_type))
2210 {
2211 tree b;
2212
2213 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2214 b;
2215 b = TREE_CHAIN (b))
2216 {
2217 tree base_type = BINFO_TYPE (b);
2218 if (CLASS_TYPE_P (base_type)
2219 && dependent_type_p (base_type))
2220 {
2221 tree field;
2222 /* Go from a particular instantiation of the
2223 template (which will have an empty TYPE_FIELDs),
2224 to the main version. */
2225 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2226 for (field = TYPE_FIELDS (base_type);
2227 field;
2228 field = TREE_CHAIN (field))
2229 if (TREE_CODE (field) == TYPE_DECL
2230 && DECL_NAME (field) == id)
2231 {
2232 inform ("(perhaps %<typename %T::%E%> was intended)",
2233 BINFO_TYPE (b), id);
2234 break;
2235 }
2236 if (field)
2237 break;
2238 }
2239 }
2240 }
2241 }
2242 /* Here we diagnose qualified-ids where the scope is actually correct,
2243 but the identifier does not resolve to a valid type name. */
2244 else if (parser->scope != error_mark_node)
2245 {
2246 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2247 error ("%qE in namespace %qE does not name a type",
2248 id, parser->scope);
2249 else if (TYPE_P (parser->scope))
2250 error ("%qE in class %qT does not name a type", id, parser->scope);
2251 else
2252 gcc_unreachable ();
2253 }
2254 cp_parser_commit_to_tentative_parse (parser);
2255 }
2256
2257 /* Check for a common situation where a type-name should be present,
2258 but is not, and issue a sensible error message. Returns true if an
2259 invalid type-name was detected.
2260
2261 The situation handled by this function are variable declarations of the
2262 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2263 Usually, `ID' should name a type, but if we got here it means that it
2264 does not. We try to emit the best possible error message depending on
2265 how exactly the id-expression looks like. */
2266
2267 static bool
cp_parser_parse_and_diagnose_invalid_type_name(cp_parser * parser)2268 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2269 {
2270 tree id;
2271
2272 cp_parser_parse_tentatively (parser);
2273 id = cp_parser_id_expression (parser,
2274 /*template_keyword_p=*/false,
2275 /*check_dependency_p=*/true,
2276 /*template_p=*/NULL,
2277 /*declarator_p=*/true,
2278 /*optional_p=*/false);
2279 /* After the id-expression, there should be a plain identifier,
2280 otherwise this is not a simple variable declaration. Also, if
2281 the scope is dependent, we cannot do much. */
2282 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2283 || (parser->scope && TYPE_P (parser->scope)
2284 && dependent_type_p (parser->scope))
2285 || TREE_CODE (id) == TYPE_DECL)
2286 {
2287 cp_parser_abort_tentative_parse (parser);
2288 return false;
2289 }
2290 if (!cp_parser_parse_definitely (parser))
2291 return false;
2292
2293 /* Emit a diagnostic for the invalid type. */
2294 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2295 /* Skip to the end of the declaration; there's no point in
2296 trying to process it. */
2297 cp_parser_skip_to_end_of_block_or_statement (parser);
2298 return true;
2299 }
2300
2301 /* Consume tokens up to, and including, the next non-nested closing `)'.
2302 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2303 are doing error recovery. Returns -1 if OR_COMMA is true and we
2304 found an unnested comma. */
2305
2306 static int
cp_parser_skip_to_closing_parenthesis(cp_parser * parser,bool recovering,bool or_comma,bool consume_paren)2307 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2308 bool recovering,
2309 bool or_comma,
2310 bool consume_paren)
2311 {
2312 unsigned paren_depth = 0;
2313 unsigned brace_depth = 0;
2314
2315 if (recovering && !or_comma
2316 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2317 return 0;
2318
2319 while (true)
2320 {
2321 cp_token * token = cp_lexer_peek_token (parser->lexer);
2322
2323 switch (token->type)
2324 {
2325 case CPP_EOF:
2326 case CPP_PRAGMA_EOL:
2327 /* If we've run out of tokens, then there is no closing `)'. */
2328 return 0;
2329
2330 case CPP_SEMICOLON:
2331 /* This matches the processing in skip_to_end_of_statement. */
2332 if (!brace_depth)
2333 return 0;
2334 break;
2335
2336 case CPP_OPEN_BRACE:
2337 ++brace_depth;
2338 break;
2339 case CPP_CLOSE_BRACE:
2340 if (!brace_depth--)
2341 return 0;
2342 break;
2343
2344 case CPP_COMMA:
2345 if (recovering && or_comma && !brace_depth && !paren_depth)
2346 return -1;
2347 break;
2348
2349 case CPP_OPEN_PAREN:
2350 if (!brace_depth)
2351 ++paren_depth;
2352 break;
2353
2354 case CPP_CLOSE_PAREN:
2355 if (!brace_depth && !paren_depth--)
2356 {
2357 if (consume_paren)
2358 cp_lexer_consume_token (parser->lexer);
2359 return 1;
2360 }
2361 break;
2362
2363 default:
2364 break;
2365 }
2366
2367 /* Consume the token. */
2368 cp_lexer_consume_token (parser->lexer);
2369 }
2370 }
2371
2372 /* Consume tokens until we reach the end of the current statement.
2373 Normally, that will be just before consuming a `;'. However, if a
2374 non-nested `}' comes first, then we stop before consuming that. */
2375
2376 static void
cp_parser_skip_to_end_of_statement(cp_parser * parser)2377 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2378 {
2379 unsigned nesting_depth = 0;
2380
2381 while (true)
2382 {
2383 cp_token *token = cp_lexer_peek_token (parser->lexer);
2384
2385 switch (token->type)
2386 {
2387 case CPP_EOF:
2388 case CPP_PRAGMA_EOL:
2389 /* If we've run out of tokens, stop. */
2390 return;
2391
2392 case CPP_SEMICOLON:
2393 /* If the next token is a `;', we have reached the end of the
2394 statement. */
2395 if (!nesting_depth)
2396 return;
2397 break;
2398
2399 case CPP_CLOSE_BRACE:
2400 /* If this is a non-nested '}', stop before consuming it.
2401 That way, when confronted with something like:
2402
2403 { 3 + }
2404
2405 we stop before consuming the closing '}', even though we
2406 have not yet reached a `;'. */
2407 if (nesting_depth == 0)
2408 return;
2409
2410 /* If it is the closing '}' for a block that we have
2411 scanned, stop -- but only after consuming the token.
2412 That way given:
2413
2414 void f g () { ... }
2415 typedef int I;
2416
2417 we will stop after the body of the erroneously declared
2418 function, but before consuming the following `typedef'
2419 declaration. */
2420 if (--nesting_depth == 0)
2421 {
2422 cp_lexer_consume_token (parser->lexer);
2423 return;
2424 }
2425
2426 case CPP_OPEN_BRACE:
2427 ++nesting_depth;
2428 break;
2429
2430 default:
2431 break;
2432 }
2433
2434 /* Consume the token. */
2435 cp_lexer_consume_token (parser->lexer);
2436 }
2437 }
2438
2439 /* This function is called at the end of a statement or declaration.
2440 If the next token is a semicolon, it is consumed; otherwise, error
2441 recovery is attempted. */
2442
2443 static void
cp_parser_consume_semicolon_at_end_of_statement(cp_parser * parser)2444 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2445 {
2446 /* Look for the trailing `;'. */
2447 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2448 {
2449 /* If there is additional (erroneous) input, skip to the end of
2450 the statement. */
2451 cp_parser_skip_to_end_of_statement (parser);
2452 /* If the next token is now a `;', consume it. */
2453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2454 cp_lexer_consume_token (parser->lexer);
2455 }
2456 }
2457
2458 /* Skip tokens until we have consumed an entire block, or until we
2459 have consumed a non-nested `;'. */
2460
2461 static void
cp_parser_skip_to_end_of_block_or_statement(cp_parser * parser)2462 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2463 {
2464 int nesting_depth = 0;
2465
2466 while (nesting_depth >= 0)
2467 {
2468 cp_token *token = cp_lexer_peek_token (parser->lexer);
2469
2470 switch (token->type)
2471 {
2472 case CPP_EOF:
2473 case CPP_PRAGMA_EOL:
2474 /* If we've run out of tokens, stop. */
2475 return;
2476
2477 case CPP_SEMICOLON:
2478 /* Stop if this is an unnested ';'. */
2479 if (!nesting_depth)
2480 nesting_depth = -1;
2481 break;
2482
2483 case CPP_CLOSE_BRACE:
2484 /* Stop if this is an unnested '}', or closes the outermost
2485 nesting level. */
2486 nesting_depth--;
2487 if (!nesting_depth)
2488 nesting_depth = -1;
2489 break;
2490
2491 case CPP_OPEN_BRACE:
2492 /* Nest. */
2493 nesting_depth++;
2494 break;
2495
2496 default:
2497 break;
2498 }
2499
2500 /* Consume the token. */
2501 cp_lexer_consume_token (parser->lexer);
2502 }
2503 }
2504
2505 /* Skip tokens until a non-nested closing curly brace is the next
2506 token. */
2507
2508 static void
cp_parser_skip_to_closing_brace(cp_parser * parser)2509 cp_parser_skip_to_closing_brace (cp_parser *parser)
2510 {
2511 unsigned nesting_depth = 0;
2512
2513 while (true)
2514 {
2515 cp_token *token = cp_lexer_peek_token (parser->lexer);
2516
2517 switch (token->type)
2518 {
2519 case CPP_EOF:
2520 case CPP_PRAGMA_EOL:
2521 /* If we've run out of tokens, stop. */
2522 return;
2523
2524 case CPP_CLOSE_BRACE:
2525 /* If the next token is a non-nested `}', then we have reached
2526 the end of the current block. */
2527 if (nesting_depth-- == 0)
2528 return;
2529 break;
2530
2531 case CPP_OPEN_BRACE:
2532 /* If it the next token is a `{', then we are entering a new
2533 block. Consume the entire block. */
2534 ++nesting_depth;
2535 break;
2536
2537 default:
2538 break;
2539 }
2540
2541 /* Consume the token. */
2542 cp_lexer_consume_token (parser->lexer);
2543 }
2544 }
2545
2546 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2547 parameter is the PRAGMA token, allowing us to purge the entire pragma
2548 sequence. */
2549
2550 static void
cp_parser_skip_to_pragma_eol(cp_parser * parser,cp_token * pragma_tok)2551 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2552 {
2553 cp_token *token;
2554
2555 parser->lexer->in_pragma = false;
2556
2557 do
2558 token = cp_lexer_consume_token (parser->lexer);
2559 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2560
2561 /* Ensure that the pragma is not parsed again. */
2562 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2563 }
2564
2565 /* Require pragma end of line, resyncing with it as necessary. The
2566 arguments are as for cp_parser_skip_to_pragma_eol. */
2567
2568 static void
cp_parser_require_pragma_eol(cp_parser * parser,cp_token * pragma_tok)2569 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2570 {
2571 parser->lexer->in_pragma = false;
2572 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2573 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2574 }
2575
2576 /* This is a simple wrapper around make_typename_type. When the id is
2577 an unresolved identifier node, we can provide a superior diagnostic
2578 using cp_parser_diagnose_invalid_type_name. */
2579
2580 static tree
cp_parser_make_typename_type(cp_parser * parser,tree scope,tree id)2581 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2582 {
2583 tree result;
2584 if (TREE_CODE (id) == IDENTIFIER_NODE)
2585 {
2586 result = make_typename_type (scope, id, typename_type,
2587 /*complain=*/tf_none);
2588 if (result == error_mark_node)
2589 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2590 return result;
2591 }
2592 return make_typename_type (scope, id, typename_type, tf_error);
2593 }
2594
2595
2596 /* Create a new C++ parser. */
2597
2598 static cp_parser *
cp_parser_new(void)2599 cp_parser_new (void)
2600 {
2601 cp_parser *parser;
2602 cp_lexer *lexer;
2603 unsigned i;
2604
2605 /* cp_lexer_new_main is called before calling ggc_alloc because
2606 cp_lexer_new_main might load a PCH file. */
2607 lexer = cp_lexer_new_main ();
2608
2609 /* Initialize the binops_by_token so that we can get the tree
2610 directly from the token. */
2611 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2612 binops_by_token[binops[i].token_type] = binops[i];
2613
2614 parser = GGC_CNEW (cp_parser);
2615 parser->lexer = lexer;
2616 parser->context = cp_parser_context_new (NULL);
2617
2618 /* For now, we always accept GNU extensions. */
2619 parser->allow_gnu_extensions_p = 1;
2620
2621 /* The `>' token is a greater-than operator, not the end of a
2622 template-id. */
2623 parser->greater_than_is_operator_p = true;
2624
2625 parser->default_arg_ok_p = true;
2626
2627 /* We are not parsing a constant-expression. */
2628 parser->integral_constant_expression_p = false;
2629 parser->allow_non_integral_constant_expression_p = false;
2630 parser->non_integral_constant_expression_p = false;
2631
2632 /* Local variable names are not forbidden. */
2633 parser->local_variables_forbidden_p = false;
2634
2635 /* We are not processing an `extern "C"' declaration. */
2636 parser->in_unbraced_linkage_specification_p = false;
2637
2638 /* We are not processing a declarator. */
2639 parser->in_declarator_p = false;
2640
2641 /* We are not processing a template-argument-list. */
2642 parser->in_template_argument_list_p = false;
2643
2644 /* We are not in an iteration statement. */
2645 parser->in_statement = 0;
2646
2647 /* We are not in a switch statement. */
2648 parser->in_switch_statement_p = false;
2649
2650 /* We are not parsing a type-id inside an expression. */
2651 parser->in_type_id_in_expr_p = false;
2652
2653 /* Declarations aren't implicitly extern "C". */
2654 parser->implicit_extern_c = false;
2655
2656 /* String literals should be translated to the execution character set. */
2657 parser->translate_strings_p = true;
2658
2659 /* We are not parsing a function body. */
2660 parser->in_function_body = false;
2661
2662 /* The unparsed function queue is empty. */
2663 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2664
2665 /* There are no classes being defined. */
2666 parser->num_classes_being_defined = 0;
2667
2668 /* No template parameters apply. */
2669 parser->num_template_parameter_lists = 0;
2670
2671 return parser;
2672 }
2673
2674 /* Create a cp_lexer structure which will emit the tokens in CACHE
2675 and push it onto the parser's lexer stack. This is used for delayed
2676 parsing of in-class method bodies and default arguments, and should
2677 not be confused with tentative parsing. */
2678 static void
cp_parser_push_lexer_for_tokens(cp_parser * parser,cp_token_cache * cache)2679 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2680 {
2681 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2682 lexer->next = parser->lexer;
2683 parser->lexer = lexer;
2684
2685 /* Move the current source position to that of the first token in the
2686 new lexer. */
2687 cp_lexer_set_source_position_from_token (lexer->next_token);
2688 }
2689
2690 /* Pop the top lexer off the parser stack. This is never used for the
2691 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2692 static void
cp_parser_pop_lexer(cp_parser * parser)2693 cp_parser_pop_lexer (cp_parser *parser)
2694 {
2695 cp_lexer *lexer = parser->lexer;
2696 parser->lexer = lexer->next;
2697 cp_lexer_destroy (lexer);
2698
2699 /* Put the current source position back where it was before this
2700 lexer was pushed. */
2701 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2702 }
2703
2704 /* Lexical conventions [gram.lex] */
2705
2706 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2707 identifier. */
2708
2709 static tree
cp_parser_identifier(cp_parser * parser)2710 cp_parser_identifier (cp_parser* parser)
2711 {
2712 cp_token *token;
2713
2714 /* Look for the identifier. */
2715 token = cp_parser_require (parser, CPP_NAME, "identifier");
2716 /* Return the value. */
2717 return token ? token->u.value : error_mark_node;
2718 }
2719
2720 /* Parse a sequence of adjacent string constants. Returns a
2721 TREE_STRING representing the combined, nul-terminated string
2722 constant. If TRANSLATE is true, translate the string to the
2723 execution character set. If WIDE_OK is true, a wide string is
2724 invalid here.
2725
2726 C++98 [lex.string] says that if a narrow string literal token is
2727 adjacent to a wide string literal token, the behavior is undefined.
2728 However, C99 6.4.5p4 says that this results in a wide string literal.
2729 We follow C99 here, for consistency with the C front end.
2730
2731 This code is largely lifted from lex_string() in c-lex.c.
2732
2733 FUTURE: ObjC++ will need to handle @-strings here. */
2734 static tree
cp_parser_string_literal(cp_parser * parser,bool translate,bool wide_ok)2735 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2736 {
2737 tree value;
2738 bool wide = false;
2739 size_t count;
2740 struct obstack str_ob;
2741 cpp_string str, istr, *strs;
2742 cp_token *tok;
2743
2744 tok = cp_lexer_peek_token (parser->lexer);
2745 if (!cp_parser_is_string_literal (tok))
2746 {
2747 cp_parser_error (parser, "expected string-literal");
2748 return error_mark_node;
2749 }
2750
2751 /* Try to avoid the overhead of creating and destroying an obstack
2752 for the common case of just one string. */
2753 if (!cp_parser_is_string_literal
2754 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2755 {
2756 cp_lexer_consume_token (parser->lexer);
2757
2758 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2759 str.len = TREE_STRING_LENGTH (tok->u.value);
2760 count = 1;
2761 if (tok->type == CPP_WSTRING)
2762 wide = true;
2763
2764 strs = &str;
2765 }
2766 else
2767 {
2768 gcc_obstack_init (&str_ob);
2769 count = 0;
2770
2771 do
2772 {
2773 cp_lexer_consume_token (parser->lexer);
2774 count++;
2775 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2776 str.len = TREE_STRING_LENGTH (tok->u.value);
2777 if (tok->type == CPP_WSTRING)
2778 wide = true;
2779
2780 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2781
2782 tok = cp_lexer_peek_token (parser->lexer);
2783 }
2784 while (cp_parser_is_string_literal (tok));
2785
2786 strs = (cpp_string *) obstack_finish (&str_ob);
2787 }
2788
2789 if (wide && !wide_ok)
2790 {
2791 cp_parser_error (parser, "a wide string is invalid in this context");
2792 wide = false;
2793 }
2794
2795 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2796 (parse_in, strs, count, &istr, wide))
2797 {
2798 value = build_string (istr.len, (char *)istr.text);
2799 free ((void *)istr.text);
2800
2801 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2802 value = fix_string_type (value);
2803 }
2804 else
2805 /* cpp_interpret_string has issued an error. */
2806 value = error_mark_node;
2807
2808 if (count > 1)
2809 obstack_free (&str_ob, 0);
2810
2811 return value;
2812 }
2813
2814
2815 /* Basic concepts [gram.basic] */
2816
2817 /* Parse a translation-unit.
2818
2819 translation-unit:
2820 declaration-seq [opt]
2821
2822 Returns TRUE if all went well. */
2823
2824 static bool
cp_parser_translation_unit(cp_parser * parser)2825 cp_parser_translation_unit (cp_parser* parser)
2826 {
2827 /* The address of the first non-permanent object on the declarator
2828 obstack. */
2829 static void *declarator_obstack_base;
2830
2831 bool success;
2832
2833 /* Create the declarator obstack, if necessary. */
2834 if (!cp_error_declarator)
2835 {
2836 gcc_obstack_init (&declarator_obstack);
2837 /* Create the error declarator. */
2838 cp_error_declarator = make_declarator (cdk_error);
2839 /* Create the empty parameter list. */
2840 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2841 /* Remember where the base of the declarator obstack lies. */
2842 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2843 }
2844
2845 cp_parser_declaration_seq_opt (parser);
2846
2847 /* If there are no tokens left then all went well. */
2848 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2849 {
2850 /* Get rid of the token array; we don't need it any more. */
2851 cp_lexer_destroy (parser->lexer);
2852 parser->lexer = NULL;
2853
2854 /* This file might have been a context that's implicitly extern
2855 "C". If so, pop the lang context. (Only relevant for PCH.) */
2856 if (parser->implicit_extern_c)
2857 {
2858 pop_lang_context ();
2859 parser->implicit_extern_c = false;
2860 }
2861
2862 /* Finish up. */
2863 finish_translation_unit ();
2864
2865 success = true;
2866 }
2867 else
2868 {
2869 cp_parser_error (parser, "expected declaration");
2870 success = false;
2871 }
2872
2873 /* Make sure the declarator obstack was fully cleaned up. */
2874 gcc_assert (obstack_next_free (&declarator_obstack)
2875 == declarator_obstack_base);
2876
2877 /* All went well. */
2878 return success;
2879 }
2880
2881 /* Expressions [gram.expr] */
2882
2883 /* Parse a primary-expression.
2884
2885 primary-expression:
2886 literal
2887 this
2888 ( expression )
2889 id-expression
2890
2891 GNU Extensions:
2892
2893 primary-expression:
2894 ( compound-statement )
2895 __builtin_va_arg ( assignment-expression , type-id )
2896 __builtin_offsetof ( type-id , offsetof-expression )
2897
2898 Objective-C++ Extension:
2899
2900 primary-expression:
2901 objc-expression
2902
2903 literal:
2904 __null
2905
2906 ADDRESS_P is true iff this expression was immediately preceded by
2907 "&" and therefore might denote a pointer-to-member. CAST_P is true
2908 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2909 true iff this expression is a template argument.
2910
2911 Returns a representation of the expression. Upon return, *IDK
2912 indicates what kind of id-expression (if any) was present. */
2913
2914 static tree
cp_parser_primary_expression(cp_parser * parser,bool address_p,bool cast_p,bool template_arg_p,cp_id_kind * idk)2915 cp_parser_primary_expression (cp_parser *parser,
2916 bool address_p,
2917 bool cast_p,
2918 bool template_arg_p,
2919 cp_id_kind *idk)
2920 {
2921 cp_token *token;
2922
2923 /* Assume the primary expression is not an id-expression. */
2924 *idk = CP_ID_KIND_NONE;
2925
2926 /* Peek at the next token. */
2927 token = cp_lexer_peek_token (parser->lexer);
2928 switch (token->type)
2929 {
2930 /* literal:
2931 integer-literal
2932 character-literal
2933 floating-literal
2934 string-literal
2935 boolean-literal */
2936 case CPP_CHAR:
2937 case CPP_WCHAR:
2938 case CPP_NUMBER:
2939 token = cp_lexer_consume_token (parser->lexer);
2940 /* Floating-point literals are only allowed in an integral
2941 constant expression if they are cast to an integral or
2942 enumeration type. */
2943 if (TREE_CODE (token->u.value) == REAL_CST
2944 && parser->integral_constant_expression_p
2945 && pedantic)
2946 {
2947 /* CAST_P will be set even in invalid code like "int(2.7 +
2948 ...)". Therefore, we have to check that the next token
2949 is sure to end the cast. */
2950 if (cast_p)
2951 {
2952 cp_token *next_token;
2953
2954 next_token = cp_lexer_peek_token (parser->lexer);
2955 if (/* The comma at the end of an
2956 enumerator-definition. */
2957 next_token->type != CPP_COMMA
2958 /* The curly brace at the end of an enum-specifier. */
2959 && next_token->type != CPP_CLOSE_BRACE
2960 /* The end of a statement. */
2961 && next_token->type != CPP_SEMICOLON
2962 /* The end of the cast-expression. */
2963 && next_token->type != CPP_CLOSE_PAREN
2964 /* The end of an array bound. */
2965 && next_token->type != CPP_CLOSE_SQUARE
2966 /* The closing ">" in a template-argument-list. */
2967 && (next_token->type != CPP_GREATER
2968 || parser->greater_than_is_operator_p))
2969 cast_p = false;
2970 }
2971
2972 /* If we are within a cast, then the constraint that the
2973 cast is to an integral or enumeration type will be
2974 checked at that point. If we are not within a cast, then
2975 this code is invalid. */
2976 if (!cast_p)
2977 cp_parser_non_integral_constant_expression
2978 (parser, "floating-point literal");
2979 }
2980 return token->u.value;
2981
2982 case CPP_STRING:
2983 case CPP_WSTRING:
2984 /* ??? Should wide strings be allowed when parser->translate_strings_p
2985 is false (i.e. in attributes)? If not, we can kill the third
2986 argument to cp_parser_string_literal. */
2987 return cp_parser_string_literal (parser,
2988 parser->translate_strings_p,
2989 true);
2990
2991 case CPP_OPEN_PAREN:
2992 {
2993 tree expr;
2994 bool saved_greater_than_is_operator_p;
2995
2996 /* Consume the `('. */
2997 cp_lexer_consume_token (parser->lexer);
2998 /* Within a parenthesized expression, a `>' token is always
2999 the greater-than operator. */
3000 saved_greater_than_is_operator_p
3001 = parser->greater_than_is_operator_p;
3002 parser->greater_than_is_operator_p = true;
3003 /* If we see `( { ' then we are looking at the beginning of
3004 a GNU statement-expression. */
3005 if (cp_parser_allow_gnu_extensions_p (parser)
3006 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3007 {
3008 /* Statement-expressions are not allowed by the standard. */
3009 if (pedantic)
3010 pedwarn ("ISO C++ forbids braced-groups within expressions");
3011
3012 /* And they're not allowed outside of a function-body; you
3013 cannot, for example, write:
3014
3015 int i = ({ int j = 3; j + 1; });
3016
3017 at class or namespace scope. */
3018 if (!parser->in_function_body)
3019 error ("statement-expressions are allowed only inside functions");
3020 /* Start the statement-expression. */
3021 expr = begin_stmt_expr ();
3022 /* Parse the compound-statement. */
3023 cp_parser_compound_statement (parser, expr, false);
3024 /* Finish up. */
3025 expr = finish_stmt_expr (expr, false);
3026 }
3027 else
3028 {
3029 /* Parse the parenthesized expression. */
3030 expr = cp_parser_expression (parser, cast_p);
3031 /* Let the front end know that this expression was
3032 enclosed in parentheses. This matters in case, for
3033 example, the expression is of the form `A::B', since
3034 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3035 not. */
3036 finish_parenthesized_expr (expr);
3037 }
3038 /* The `>' token might be the end of a template-id or
3039 template-parameter-list now. */
3040 parser->greater_than_is_operator_p
3041 = saved_greater_than_is_operator_p;
3042 /* Consume the `)'. */
3043 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3044 cp_parser_skip_to_end_of_statement (parser);
3045
3046 return expr;
3047 }
3048
3049 case CPP_KEYWORD:
3050 switch (token->keyword)
3051 {
3052 /* These two are the boolean literals. */
3053 case RID_TRUE:
3054 cp_lexer_consume_token (parser->lexer);
3055 return boolean_true_node;
3056 case RID_FALSE:
3057 cp_lexer_consume_token (parser->lexer);
3058 return boolean_false_node;
3059
3060 /* The `__null' literal. */
3061 case RID_NULL:
3062 cp_lexer_consume_token (parser->lexer);
3063 return null_node;
3064
3065 /* Recognize the `this' keyword. */
3066 case RID_THIS:
3067 cp_lexer_consume_token (parser->lexer);
3068 if (parser->local_variables_forbidden_p)
3069 {
3070 error ("%<this%> may not be used in this context");
3071 return error_mark_node;
3072 }
3073 /* Pointers cannot appear in constant-expressions. */
3074 if (cp_parser_non_integral_constant_expression (parser,
3075 "`this'"))
3076 return error_mark_node;
3077 return finish_this_expr ();
3078
3079 /* The `operator' keyword can be the beginning of an
3080 id-expression. */
3081 case RID_OPERATOR:
3082 goto id_expression;
3083
3084 case RID_FUNCTION_NAME:
3085 case RID_PRETTY_FUNCTION_NAME:
3086 case RID_C99_FUNCTION_NAME:
3087 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3088 __func__ are the names of variables -- but they are
3089 treated specially. Therefore, they are handled here,
3090 rather than relying on the generic id-expression logic
3091 below. Grammatically, these names are id-expressions.
3092
3093 Consume the token. */
3094 token = cp_lexer_consume_token (parser->lexer);
3095 /* Look up the name. */
3096 return finish_fname (token->u.value);
3097
3098 case RID_VA_ARG:
3099 {
3100 tree expression;
3101 tree type;
3102
3103 /* The `__builtin_va_arg' construct is used to handle
3104 `va_arg'. Consume the `__builtin_va_arg' token. */
3105 cp_lexer_consume_token (parser->lexer);
3106 /* Look for the opening `('. */
3107 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3108 /* Now, parse the assignment-expression. */
3109 expression = cp_parser_assignment_expression (parser,
3110 /*cast_p=*/false);
3111 /* Look for the `,'. */
3112 cp_parser_require (parser, CPP_COMMA, "`,'");
3113 /* Parse the type-id. */
3114 type = cp_parser_type_id (parser);
3115 /* Look for the closing `)'. */
3116 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3117 /* Using `va_arg' in a constant-expression is not
3118 allowed. */
3119 if (cp_parser_non_integral_constant_expression (parser,
3120 "`va_arg'"))
3121 return error_mark_node;
3122 return build_x_va_arg (expression, type);
3123 }
3124
3125 case RID_OFFSETOF:
3126 return cp_parser_builtin_offsetof (parser);
3127
3128 /* Objective-C++ expressions. */
3129 case RID_AT_ENCODE:
3130 case RID_AT_PROTOCOL:
3131 case RID_AT_SELECTOR:
3132 return cp_parser_objc_expression (parser);
3133
3134 default:
3135 cp_parser_error (parser, "expected primary-expression");
3136 return error_mark_node;
3137 }
3138
3139 /* An id-expression can start with either an identifier, a
3140 `::' as the beginning of a qualified-id, or the "operator"
3141 keyword. */
3142 case CPP_NAME:
3143 case CPP_SCOPE:
3144 case CPP_TEMPLATE_ID:
3145 case CPP_NESTED_NAME_SPECIFIER:
3146 {
3147 tree id_expression;
3148 tree decl;
3149 const char *error_msg;
3150 bool template_p;
3151 bool done;
3152
3153 id_expression:
3154 /* Parse the id-expression. */
3155 id_expression
3156 = cp_parser_id_expression (parser,
3157 /*template_keyword_p=*/false,
3158 /*check_dependency_p=*/true,
3159 &template_p,
3160 /*declarator_p=*/false,
3161 /*optional_p=*/false);
3162 if (id_expression == error_mark_node)
3163 return error_mark_node;
3164 token = cp_lexer_peek_token (parser->lexer);
3165 done = (token->type != CPP_OPEN_SQUARE
3166 && token->type != CPP_OPEN_PAREN
3167 && token->type != CPP_DOT
3168 && token->type != CPP_DEREF
3169 && token->type != CPP_PLUS_PLUS
3170 && token->type != CPP_MINUS_MINUS);
3171 /* If we have a template-id, then no further lookup is
3172 required. If the template-id was for a template-class, we
3173 will sometimes have a TYPE_DECL at this point. */
3174 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3175 || TREE_CODE (id_expression) == TYPE_DECL)
3176 decl = id_expression;
3177 /* Look up the name. */
3178 else
3179 {
3180 tree ambiguous_decls;
3181
3182 decl = cp_parser_lookup_name (parser, id_expression,
3183 none_type,
3184 template_p,
3185 /*is_namespace=*/false,
3186 /*check_dependency=*/true,
3187 &ambiguous_decls);
3188 /* If the lookup was ambiguous, an error will already have
3189 been issued. */
3190 if (ambiguous_decls)
3191 return error_mark_node;
3192
3193 /* In Objective-C++, an instance variable (ivar) may be preferred
3194 to whatever cp_parser_lookup_name() found. */
3195 decl = objc_lookup_ivar (decl, id_expression);
3196
3197 /* If name lookup gives us a SCOPE_REF, then the
3198 qualifying scope was dependent. */
3199 if (TREE_CODE (decl) == SCOPE_REF)
3200 {
3201 /* At this point, we do not know if DECL is a valid
3202 integral constant expression. We assume that it is
3203 in fact such an expression, so that code like:
3204
3205 template <int N> struct A {
3206 int a[B<N>::i];
3207 };
3208
3209 is accepted. At template-instantiation time, we
3210 will check that B<N>::i is actually a constant. */
3211 return decl;
3212 }
3213 /* Check to see if DECL is a local variable in a context
3214 where that is forbidden. */
3215 if (parser->local_variables_forbidden_p
3216 && local_variable_p (decl))
3217 {
3218 /* It might be that we only found DECL because we are
3219 trying to be generous with pre-ISO scoping rules.
3220 For example, consider:
3221
3222 int i;
3223 void g() {
3224 for (int i = 0; i < 10; ++i) {}
3225 extern void f(int j = i);
3226 }
3227
3228 Here, name look up will originally find the out
3229 of scope `i'. We need to issue a warning message,
3230 but then use the global `i'. */
3231 decl = check_for_out_of_scope_variable (decl);
3232 if (local_variable_p (decl))
3233 {
3234 error ("local variable %qD may not appear in this context",
3235 decl);
3236 return error_mark_node;
3237 }
3238 }
3239 }
3240
3241 decl = (finish_id_expression
3242 (id_expression, decl, parser->scope,
3243 idk,
3244 parser->integral_constant_expression_p,
3245 parser->allow_non_integral_constant_expression_p,
3246 &parser->non_integral_constant_expression_p,
3247 template_p, done, address_p,
3248 template_arg_p,
3249 &error_msg));
3250 if (error_msg)
3251 cp_parser_error (parser, error_msg);
3252 return decl;
3253 }
3254
3255 /* Anything else is an error. */
3256 default:
3257 /* ...unless we have an Objective-C++ message or string literal, that is. */
3258 if (c_dialect_objc ()
3259 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3260 return cp_parser_objc_expression (parser);
3261
3262 cp_parser_error (parser, "expected primary-expression");
3263 return error_mark_node;
3264 }
3265 }
3266
3267 /* Parse an id-expression.
3268
3269 id-expression:
3270 unqualified-id
3271 qualified-id
3272
3273 qualified-id:
3274 :: [opt] nested-name-specifier template [opt] unqualified-id
3275 :: identifier
3276 :: operator-function-id
3277 :: template-id
3278
3279 Return a representation of the unqualified portion of the
3280 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3281 a `::' or nested-name-specifier.
3282
3283 Often, if the id-expression was a qualified-id, the caller will
3284 want to make a SCOPE_REF to represent the qualified-id. This
3285 function does not do this in order to avoid wastefully creating
3286 SCOPE_REFs when they are not required.
3287
3288 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3289 `template' keyword.
3290
3291 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3292 uninstantiated templates.
3293
3294 If *TEMPLATE_P is non-NULL, it is set to true iff the
3295 `template' keyword is used to explicitly indicate that the entity
3296 named is a template.
3297
3298 If DECLARATOR_P is true, the id-expression is appearing as part of
3299 a declarator, rather than as part of an expression. */
3300
3301 static tree
cp_parser_id_expression(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool * template_p,bool declarator_p,bool optional_p)3302 cp_parser_id_expression (cp_parser *parser,
3303 bool template_keyword_p,
3304 bool check_dependency_p,
3305 bool *template_p,
3306 bool declarator_p,
3307 bool optional_p)
3308 {
3309 bool global_scope_p;
3310 bool nested_name_specifier_p;
3311
3312 /* Assume the `template' keyword was not used. */
3313 if (template_p)
3314 *template_p = template_keyword_p;
3315
3316 /* Look for the optional `::' operator. */
3317 global_scope_p
3318 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3319 != NULL_TREE);
3320 /* Look for the optional nested-name-specifier. */
3321 nested_name_specifier_p
3322 = (cp_parser_nested_name_specifier_opt (parser,
3323 /*typename_keyword_p=*/false,
3324 check_dependency_p,
3325 /*type_p=*/false,
3326 declarator_p)
3327 != NULL_TREE);
3328 /* If there is a nested-name-specifier, then we are looking at
3329 the first qualified-id production. */
3330 if (nested_name_specifier_p)
3331 {
3332 tree saved_scope;
3333 tree saved_object_scope;
3334 tree saved_qualifying_scope;
3335 tree unqualified_id;
3336 bool is_template;
3337
3338 /* See if the next token is the `template' keyword. */
3339 if (!template_p)
3340 template_p = &is_template;
3341 *template_p = cp_parser_optional_template_keyword (parser);
3342 /* Name lookup we do during the processing of the
3343 unqualified-id might obliterate SCOPE. */
3344 saved_scope = parser->scope;
3345 saved_object_scope = parser->object_scope;
3346 saved_qualifying_scope = parser->qualifying_scope;
3347 /* Process the final unqualified-id. */
3348 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3349 check_dependency_p,
3350 declarator_p,
3351 /*optional_p=*/false);
3352 /* Restore the SAVED_SCOPE for our caller. */
3353 parser->scope = saved_scope;
3354 parser->object_scope = saved_object_scope;
3355 parser->qualifying_scope = saved_qualifying_scope;
3356
3357 return unqualified_id;
3358 }
3359 /* Otherwise, if we are in global scope, then we are looking at one
3360 of the other qualified-id productions. */
3361 else if (global_scope_p)
3362 {
3363 cp_token *token;
3364 tree id;
3365
3366 /* Peek at the next token. */
3367 token = cp_lexer_peek_token (parser->lexer);
3368
3369 /* If it's an identifier, and the next token is not a "<", then
3370 we can avoid the template-id case. This is an optimization
3371 for this common case. */
3372 if (token->type == CPP_NAME
3373 && !cp_parser_nth_token_starts_template_argument_list_p
3374 (parser, 2))
3375 return cp_parser_identifier (parser);
3376
3377 cp_parser_parse_tentatively (parser);
3378 /* Try a template-id. */
3379 id = cp_parser_template_id (parser,
3380 /*template_keyword_p=*/false,
3381 /*check_dependency_p=*/true,
3382 declarator_p);
3383 /* If that worked, we're done. */
3384 if (cp_parser_parse_definitely (parser))
3385 return id;
3386
3387 /* Peek at the next token. (Changes in the token buffer may
3388 have invalidated the pointer obtained above.) */
3389 token = cp_lexer_peek_token (parser->lexer);
3390
3391 switch (token->type)
3392 {
3393 case CPP_NAME:
3394 return cp_parser_identifier (parser);
3395
3396 case CPP_KEYWORD:
3397 if (token->keyword == RID_OPERATOR)
3398 return cp_parser_operator_function_id (parser);
3399 /* Fall through. */
3400
3401 default:
3402 cp_parser_error (parser, "expected id-expression");
3403 return error_mark_node;
3404 }
3405 }
3406 else
3407 return cp_parser_unqualified_id (parser, template_keyword_p,
3408 /*check_dependency_p=*/true,
3409 declarator_p,
3410 optional_p);
3411 }
3412
3413 /* Parse an unqualified-id.
3414
3415 unqualified-id:
3416 identifier
3417 operator-function-id
3418 conversion-function-id
3419 ~ class-name
3420 template-id
3421
3422 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3423 keyword, in a construct like `A::template ...'.
3424
3425 Returns a representation of unqualified-id. For the `identifier'
3426 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3427 production a BIT_NOT_EXPR is returned; the operand of the
3428 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3429 other productions, see the documentation accompanying the
3430 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3431 names are looked up in uninstantiated templates. If DECLARATOR_P
3432 is true, the unqualified-id is appearing as part of a declarator,
3433 rather than as part of an expression. */
3434
3435 static tree
cp_parser_unqualified_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool declarator_p,bool optional_p)3436 cp_parser_unqualified_id (cp_parser* parser,
3437 bool template_keyword_p,
3438 bool check_dependency_p,
3439 bool declarator_p,
3440 bool optional_p)
3441 {
3442 cp_token *token;
3443
3444 /* Peek at the next token. */
3445 token = cp_lexer_peek_token (parser->lexer);
3446
3447 switch (token->type)
3448 {
3449 case CPP_NAME:
3450 {
3451 tree id;
3452
3453 /* We don't know yet whether or not this will be a
3454 template-id. */
3455 cp_parser_parse_tentatively (parser);
3456 /* Try a template-id. */
3457 id = cp_parser_template_id (parser, template_keyword_p,
3458 check_dependency_p,
3459 declarator_p);
3460 /* If it worked, we're done. */
3461 if (cp_parser_parse_definitely (parser))
3462 return id;
3463 /* Otherwise, it's an ordinary identifier. */
3464 return cp_parser_identifier (parser);
3465 }
3466
3467 case CPP_TEMPLATE_ID:
3468 return cp_parser_template_id (parser, template_keyword_p,
3469 check_dependency_p,
3470 declarator_p);
3471
3472 case CPP_COMPL:
3473 {
3474 tree type_decl;
3475 tree qualifying_scope;
3476 tree object_scope;
3477 tree scope;
3478 bool done;
3479
3480 /* Consume the `~' token. */
3481 cp_lexer_consume_token (parser->lexer);
3482 /* Parse the class-name. The standard, as written, seems to
3483 say that:
3484
3485 template <typename T> struct S { ~S (); };
3486 template <typename T> S<T>::~S() {}
3487
3488 is invalid, since `~' must be followed by a class-name, but
3489 `S<T>' is dependent, and so not known to be a class.
3490 That's not right; we need to look in uninstantiated
3491 templates. A further complication arises from:
3492
3493 template <typename T> void f(T t) {
3494 t.T::~T();
3495 }
3496
3497 Here, it is not possible to look up `T' in the scope of `T'
3498 itself. We must look in both the current scope, and the
3499 scope of the containing complete expression.
3500
3501 Yet another issue is:
3502
3503 struct S {
3504 int S;
3505 ~S();
3506 };
3507
3508 S::~S() {}
3509
3510 The standard does not seem to say that the `S' in `~S'
3511 should refer to the type `S' and not the data member
3512 `S::S'. */
3513
3514 /* DR 244 says that we look up the name after the "~" in the
3515 same scope as we looked up the qualifying name. That idea
3516 isn't fully worked out; it's more complicated than that. */
3517 scope = parser->scope;
3518 object_scope = parser->object_scope;
3519 qualifying_scope = parser->qualifying_scope;
3520
3521 /* Check for invalid scopes. */
3522 if (scope == error_mark_node)
3523 {
3524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3525 cp_lexer_consume_token (parser->lexer);
3526 return error_mark_node;
3527 }
3528 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3529 {
3530 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3531 error ("scope %qT before %<~%> is not a class-name", scope);
3532 cp_parser_simulate_error (parser);
3533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3534 cp_lexer_consume_token (parser->lexer);
3535 return error_mark_node;
3536 }
3537 gcc_assert (!scope || TYPE_P (scope));
3538
3539 /* If the name is of the form "X::~X" it's OK. */
3540 token = cp_lexer_peek_token (parser->lexer);
3541 if (scope
3542 && token->type == CPP_NAME
3543 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3544 == CPP_OPEN_PAREN)
3545 && constructor_name_p (token->u.value, scope))
3546 {
3547 cp_lexer_consume_token (parser->lexer);
3548 return build_nt (BIT_NOT_EXPR, scope);
3549 }
3550
3551 /* If there was an explicit qualification (S::~T), first look
3552 in the scope given by the qualification (i.e., S). */
3553 done = false;
3554 type_decl = NULL_TREE;
3555 if (scope)
3556 {
3557 cp_parser_parse_tentatively (parser);
3558 type_decl = cp_parser_class_name (parser,
3559 /*typename_keyword_p=*/false,
3560 /*template_keyword_p=*/false,
3561 none_type,
3562 /*check_dependency=*/false,
3563 /*class_head_p=*/false,
3564 declarator_p);
3565 if (cp_parser_parse_definitely (parser))
3566 done = true;
3567 }
3568 /* In "N::S::~S", look in "N" as well. */
3569 if (!done && scope && qualifying_scope)
3570 {
3571 cp_parser_parse_tentatively (parser);
3572 parser->scope = qualifying_scope;
3573 parser->object_scope = NULL_TREE;
3574 parser->qualifying_scope = NULL_TREE;
3575 type_decl
3576 = cp_parser_class_name (parser,
3577 /*typename_keyword_p=*/false,
3578 /*template_keyword_p=*/false,
3579 none_type,
3580 /*check_dependency=*/false,
3581 /*class_head_p=*/false,
3582 declarator_p);
3583 if (cp_parser_parse_definitely (parser))
3584 done = true;
3585 }
3586 /* In "p->S::~T", look in the scope given by "*p" as well. */
3587 else if (!done && object_scope)
3588 {
3589 cp_parser_parse_tentatively (parser);
3590 parser->scope = object_scope;
3591 parser->object_scope = NULL_TREE;
3592 parser->qualifying_scope = NULL_TREE;
3593 type_decl
3594 = cp_parser_class_name (parser,
3595 /*typename_keyword_p=*/false,
3596 /*template_keyword_p=*/false,
3597 none_type,
3598 /*check_dependency=*/false,
3599 /*class_head_p=*/false,
3600 declarator_p);
3601 if (cp_parser_parse_definitely (parser))
3602 done = true;
3603 }
3604 /* Look in the surrounding context. */
3605 if (!done)
3606 {
3607 parser->scope = NULL_TREE;
3608 parser->object_scope = NULL_TREE;
3609 parser->qualifying_scope = NULL_TREE;
3610 type_decl
3611 = cp_parser_class_name (parser,
3612 /*typename_keyword_p=*/false,
3613 /*template_keyword_p=*/false,
3614 none_type,
3615 /*check_dependency=*/false,
3616 /*class_head_p=*/false,
3617 declarator_p);
3618 }
3619 /* If an error occurred, assume that the name of the
3620 destructor is the same as the name of the qualifying
3621 class. That allows us to keep parsing after running
3622 into ill-formed destructor names. */
3623 if (type_decl == error_mark_node && scope)
3624 return build_nt (BIT_NOT_EXPR, scope);
3625 else if (type_decl == error_mark_node)
3626 return error_mark_node;
3627
3628 /* Check that destructor name and scope match. */
3629 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3630 {
3631 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3632 error ("declaration of %<~%T%> as member of %qT",
3633 type_decl, scope);
3634 cp_parser_simulate_error (parser);
3635 return error_mark_node;
3636 }
3637
3638 /* [class.dtor]
3639
3640 A typedef-name that names a class shall not be used as the
3641 identifier in the declarator for a destructor declaration. */
3642 if (declarator_p
3643 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3644 && !DECL_SELF_REFERENCE_P (type_decl)
3645 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3646 error ("typedef-name %qD used as destructor declarator",
3647 type_decl);
3648
3649 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3650 }
3651
3652 case CPP_KEYWORD:
3653 if (token->keyword == RID_OPERATOR)
3654 {
3655 tree id;
3656
3657 /* This could be a template-id, so we try that first. */
3658 cp_parser_parse_tentatively (parser);
3659 /* Try a template-id. */
3660 id = cp_parser_template_id (parser, template_keyword_p,
3661 /*check_dependency_p=*/true,
3662 declarator_p);
3663 /* If that worked, we're done. */
3664 if (cp_parser_parse_definitely (parser))
3665 return id;
3666 /* We still don't know whether we're looking at an
3667 operator-function-id or a conversion-function-id. */
3668 cp_parser_parse_tentatively (parser);
3669 /* Try an operator-function-id. */
3670 id = cp_parser_operator_function_id (parser);
3671 /* If that didn't work, try a conversion-function-id. */
3672 if (!cp_parser_parse_definitely (parser))
3673 id = cp_parser_conversion_function_id (parser);
3674
3675 return id;
3676 }
3677 /* Fall through. */
3678
3679 default:
3680 if (optional_p)
3681 return NULL_TREE;
3682 cp_parser_error (parser, "expected unqualified-id");
3683 return error_mark_node;
3684 }
3685 }
3686
3687 /* Parse an (optional) nested-name-specifier.
3688
3689 nested-name-specifier:
3690 class-or-namespace-name :: nested-name-specifier [opt]
3691 class-or-namespace-name :: template nested-name-specifier [opt]
3692
3693 PARSER->SCOPE should be set appropriately before this function is
3694 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3695 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3696 in name lookups.
3697
3698 Sets PARSER->SCOPE to the class (TYPE) or namespace
3699 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3700 it unchanged if there is no nested-name-specifier. Returns the new
3701 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3702
3703 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3704 part of a declaration and/or decl-specifier. */
3705
3706 static tree
cp_parser_nested_name_specifier_opt(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)3707 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3708 bool typename_keyword_p,
3709 bool check_dependency_p,
3710 bool type_p,
3711 bool is_declaration)
3712 {
3713 bool success = false;
3714 cp_token_position start = 0;
3715 cp_token *token;
3716
3717 /* Remember where the nested-name-specifier starts. */
3718 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3719 {
3720 start = cp_lexer_token_position (parser->lexer, false);
3721 push_deferring_access_checks (dk_deferred);
3722 }
3723
3724 while (true)
3725 {
3726 tree new_scope;
3727 tree old_scope;
3728 tree saved_qualifying_scope;
3729 bool template_keyword_p;
3730
3731 /* Spot cases that cannot be the beginning of a
3732 nested-name-specifier. */
3733 token = cp_lexer_peek_token (parser->lexer);
3734
3735 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3736 the already parsed nested-name-specifier. */
3737 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3738 {
3739 /* Grab the nested-name-specifier and continue the loop. */
3740 cp_parser_pre_parsed_nested_name_specifier (parser);
3741 /* If we originally encountered this nested-name-specifier
3742 with IS_DECLARATION set to false, we will not have
3743 resolved TYPENAME_TYPEs, so we must do so here. */
3744 if (is_declaration
3745 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3746 {
3747 new_scope = resolve_typename_type (parser->scope,
3748 /*only_current_p=*/false);
3749 if (new_scope != error_mark_node)
3750 parser->scope = new_scope;
3751 }
3752 success = true;
3753 continue;
3754 }
3755
3756 /* Spot cases that cannot be the beginning of a
3757 nested-name-specifier. On the second and subsequent times
3758 through the loop, we look for the `template' keyword. */
3759 if (success && token->keyword == RID_TEMPLATE)
3760 ;
3761 /* A template-id can start a nested-name-specifier. */
3762 else if (token->type == CPP_TEMPLATE_ID)
3763 ;
3764 else
3765 {
3766 /* If the next token is not an identifier, then it is
3767 definitely not a class-or-namespace-name. */
3768 if (token->type != CPP_NAME)
3769 break;
3770 /* If the following token is neither a `<' (to begin a
3771 template-id), nor a `::', then we are not looking at a
3772 nested-name-specifier. */
3773 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3774 if (token->type != CPP_SCOPE
3775 && !cp_parser_nth_token_starts_template_argument_list_p
3776 (parser, 2))
3777 break;
3778 }
3779
3780 /* The nested-name-specifier is optional, so we parse
3781 tentatively. */
3782 cp_parser_parse_tentatively (parser);
3783
3784 /* Look for the optional `template' keyword, if this isn't the
3785 first time through the loop. */
3786 if (success)
3787 template_keyword_p = cp_parser_optional_template_keyword (parser);
3788 else
3789 template_keyword_p = false;
3790
3791 /* Save the old scope since the name lookup we are about to do
3792 might destroy it. */
3793 old_scope = parser->scope;
3794 saved_qualifying_scope = parser->qualifying_scope;
3795 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3796 look up names in "X<T>::I" in order to determine that "Y" is
3797 a template. So, if we have a typename at this point, we make
3798 an effort to look through it. */
3799 if (is_declaration
3800 && !typename_keyword_p
3801 && parser->scope
3802 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3803 parser->scope = resolve_typename_type (parser->scope,
3804 /*only_current_p=*/false);
3805 /* Parse the qualifying entity. */
3806 new_scope
3807 = cp_parser_class_or_namespace_name (parser,
3808 typename_keyword_p,
3809 template_keyword_p,
3810 check_dependency_p,
3811 type_p,
3812 is_declaration);
3813 /* Look for the `::' token. */
3814 cp_parser_require (parser, CPP_SCOPE, "`::'");
3815
3816 /* If we found what we wanted, we keep going; otherwise, we're
3817 done. */
3818 if (!cp_parser_parse_definitely (parser))
3819 {
3820 bool error_p = false;
3821
3822 /* Restore the OLD_SCOPE since it was valid before the
3823 failed attempt at finding the last
3824 class-or-namespace-name. */
3825 parser->scope = old_scope;
3826 parser->qualifying_scope = saved_qualifying_scope;
3827 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3828 break;
3829 /* If the next token is an identifier, and the one after
3830 that is a `::', then any valid interpretation would have
3831 found a class-or-namespace-name. */
3832 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3833 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3834 == CPP_SCOPE)
3835 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3836 != CPP_COMPL))
3837 {
3838 token = cp_lexer_consume_token (parser->lexer);
3839 if (!error_p)
3840 {
3841 if (!token->ambiguous_p)
3842 {
3843 tree decl;
3844 tree ambiguous_decls;
3845
3846 decl = cp_parser_lookup_name (parser, token->u.value,
3847 none_type,
3848 /*is_template=*/false,
3849 /*is_namespace=*/false,
3850 /*check_dependency=*/true,
3851 &ambiguous_decls);
3852 if (TREE_CODE (decl) == TEMPLATE_DECL)
3853 error ("%qD used without template parameters", decl);
3854 else if (ambiguous_decls)
3855 {
3856 error ("reference to %qD is ambiguous",
3857 token->u.value);
3858 print_candidates (ambiguous_decls);
3859 decl = error_mark_node;
3860 }
3861 else
3862 cp_parser_name_lookup_error
3863 (parser, token->u.value, decl,
3864 "is not a class or namespace");
3865 }
3866 parser->scope = error_mark_node;
3867 error_p = true;
3868 /* Treat this as a successful nested-name-specifier
3869 due to:
3870
3871 [basic.lookup.qual]
3872
3873 If the name found is not a class-name (clause
3874 _class_) or namespace-name (_namespace.def_), the
3875 program is ill-formed. */
3876 success = true;
3877 }
3878 cp_lexer_consume_token (parser->lexer);
3879 }
3880 break;
3881 }
3882 /* We've found one valid nested-name-specifier. */
3883 success = true;
3884 /* Name lookup always gives us a DECL. */
3885 if (TREE_CODE (new_scope) == TYPE_DECL)
3886 new_scope = TREE_TYPE (new_scope);
3887 /* Uses of "template" must be followed by actual templates. */
3888 if (template_keyword_p
3889 && !(CLASS_TYPE_P (new_scope)
3890 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3891 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3892 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3893 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3894 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3895 == TEMPLATE_ID_EXPR)))
3896 pedwarn (TYPE_P (new_scope)
3897 ? "%qT is not a template"
3898 : "%qD is not a template",
3899 new_scope);
3900 /* If it is a class scope, try to complete it; we are about to
3901 be looking up names inside the class. */
3902 if (TYPE_P (new_scope)
3903 /* Since checking types for dependency can be expensive,
3904 avoid doing it if the type is already complete. */
3905 && !COMPLETE_TYPE_P (new_scope)
3906 /* Do not try to complete dependent types. */
3907 && !dependent_type_p (new_scope))
3908 new_scope = complete_type (new_scope);
3909 /* Make sure we look in the right scope the next time through
3910 the loop. */
3911 parser->scope = new_scope;
3912 }
3913
3914 /* If parsing tentatively, replace the sequence of tokens that makes
3915 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3916 token. That way, should we re-parse the token stream, we will
3917 not have to repeat the effort required to do the parse, nor will
3918 we issue duplicate error messages. */
3919 if (success && start)
3920 {
3921 cp_token *token;
3922
3923 token = cp_lexer_token_at (parser->lexer, start);
3924 /* Reset the contents of the START token. */
3925 token->type = CPP_NESTED_NAME_SPECIFIER;
3926 /* Retrieve any deferred checks. Do not pop this access checks yet
3927 so the memory will not be reclaimed during token replacing below. */
3928 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3929 token->u.tree_check_value->value = parser->scope;
3930 token->u.tree_check_value->checks = get_deferred_access_checks ();
3931 token->u.tree_check_value->qualifying_scope =
3932 parser->qualifying_scope;
3933 token->keyword = RID_MAX;
3934
3935 /* Purge all subsequent tokens. */
3936 cp_lexer_purge_tokens_after (parser->lexer, start);
3937 }
3938
3939 if (start)
3940 pop_to_parent_deferring_access_checks ();
3941
3942 return success ? parser->scope : NULL_TREE;
3943 }
3944
3945 /* Parse a nested-name-specifier. See
3946 cp_parser_nested_name_specifier_opt for details. This function
3947 behaves identically, except that it will an issue an error if no
3948 nested-name-specifier is present. */
3949
3950 static tree
cp_parser_nested_name_specifier(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)3951 cp_parser_nested_name_specifier (cp_parser *parser,
3952 bool typename_keyword_p,
3953 bool check_dependency_p,
3954 bool type_p,
3955 bool is_declaration)
3956 {
3957 tree scope;
3958
3959 /* Look for the nested-name-specifier. */
3960 scope = cp_parser_nested_name_specifier_opt (parser,
3961 typename_keyword_p,
3962 check_dependency_p,
3963 type_p,
3964 is_declaration);
3965 /* If it was not present, issue an error message. */
3966 if (!scope)
3967 {
3968 cp_parser_error (parser, "expected nested-name-specifier");
3969 parser->scope = NULL_TREE;
3970 }
3971
3972 return scope;
3973 }
3974
3975 /* Parse a class-or-namespace-name.
3976
3977 class-or-namespace-name:
3978 class-name
3979 namespace-name
3980
3981 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3982 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3983 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3984 TYPE_P is TRUE iff the next name should be taken as a class-name,
3985 even the same name is declared to be another entity in the same
3986 scope.
3987
3988 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3989 specified by the class-or-namespace-name. If neither is found the
3990 ERROR_MARK_NODE is returned. */
3991
3992 static tree
cp_parser_class_or_namespace_name(cp_parser * parser,bool typename_keyword_p,bool template_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)3993 cp_parser_class_or_namespace_name (cp_parser *parser,
3994 bool typename_keyword_p,
3995 bool template_keyword_p,
3996 bool check_dependency_p,
3997 bool type_p,
3998 bool is_declaration)
3999 {
4000 tree saved_scope;
4001 tree saved_qualifying_scope;
4002 tree saved_object_scope;
4003 tree scope;
4004 bool only_class_p;
4005
4006 /* Before we try to parse the class-name, we must save away the
4007 current PARSER->SCOPE since cp_parser_class_name will destroy
4008 it. */
4009 saved_scope = parser->scope;
4010 saved_qualifying_scope = parser->qualifying_scope;
4011 saved_object_scope = parser->object_scope;
4012 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4013 there is no need to look for a namespace-name. */
4014 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4015 if (!only_class_p)
4016 cp_parser_parse_tentatively (parser);
4017 scope = cp_parser_class_name (parser,
4018 typename_keyword_p,
4019 template_keyword_p,
4020 type_p ? class_type : none_type,
4021 check_dependency_p,
4022 /*class_head_p=*/false,
4023 is_declaration);
4024 /* If that didn't work, try for a namespace-name. */
4025 if (!only_class_p && !cp_parser_parse_definitely (parser))
4026 {
4027 /* Restore the saved scope. */
4028 parser->scope = saved_scope;
4029 parser->qualifying_scope = saved_qualifying_scope;
4030 parser->object_scope = saved_object_scope;
4031 /* If we are not looking at an identifier followed by the scope
4032 resolution operator, then this is not part of a
4033 nested-name-specifier. (Note that this function is only used
4034 to parse the components of a nested-name-specifier.) */
4035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4036 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4037 return error_mark_node;
4038 scope = cp_parser_namespace_name (parser);
4039 }
4040
4041 return scope;
4042 }
4043
4044 /* Parse a postfix-expression.
4045
4046 postfix-expression:
4047 primary-expression
4048 postfix-expression [ expression ]
4049 postfix-expression ( expression-list [opt] )
4050 simple-type-specifier ( expression-list [opt] )
4051 typename :: [opt] nested-name-specifier identifier
4052 ( expression-list [opt] )
4053 typename :: [opt] nested-name-specifier template [opt] template-id
4054 ( expression-list [opt] )
4055 postfix-expression . template [opt] id-expression
4056 postfix-expression -> template [opt] id-expression
4057 postfix-expression . pseudo-destructor-name
4058 postfix-expression -> pseudo-destructor-name
4059 postfix-expression ++
4060 postfix-expression --
4061 dynamic_cast < type-id > ( expression )
4062 static_cast < type-id > ( expression )
4063 reinterpret_cast < type-id > ( expression )
4064 const_cast < type-id > ( expression )
4065 typeid ( expression )
4066 typeid ( type-id )
4067
4068 GNU Extension:
4069
4070 postfix-expression:
4071 ( type-id ) { initializer-list , [opt] }
4072
4073 This extension is a GNU version of the C99 compound-literal
4074 construct. (The C99 grammar uses `type-name' instead of `type-id',
4075 but they are essentially the same concept.)
4076
4077 If ADDRESS_P is true, the postfix expression is the operand of the
4078 `&' operator. CAST_P is true if this expression is the target of a
4079 cast.
4080
4081 Returns a representation of the expression. */
4082
4083 static tree
cp_parser_postfix_expression(cp_parser * parser,bool address_p,bool cast_p)4084 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4085 {
4086 cp_token *token;
4087 enum rid keyword;
4088 cp_id_kind idk = CP_ID_KIND_NONE;
4089 tree postfix_expression = NULL_TREE;
4090
4091 /* Peek at the next token. */
4092 token = cp_lexer_peek_token (parser->lexer);
4093 /* Some of the productions are determined by keywords. */
4094 keyword = token->keyword;
4095 switch (keyword)
4096 {
4097 case RID_DYNCAST:
4098 case RID_STATCAST:
4099 case RID_REINTCAST:
4100 case RID_CONSTCAST:
4101 {
4102 tree type;
4103 tree expression;
4104 const char *saved_message;
4105
4106 /* All of these can be handled in the same way from the point
4107 of view of parsing. Begin by consuming the token
4108 identifying the cast. */
4109 cp_lexer_consume_token (parser->lexer);
4110
4111 /* New types cannot be defined in the cast. */
4112 saved_message = parser->type_definition_forbidden_message;
4113 parser->type_definition_forbidden_message
4114 = "types may not be defined in casts";
4115
4116 /* Look for the opening `<'. */
4117 cp_parser_require (parser, CPP_LESS, "`<'");
4118 /* Parse the type to which we are casting. */
4119 type = cp_parser_type_id (parser);
4120 /* Look for the closing `>'. */
4121 cp_parser_require (parser, CPP_GREATER, "`>'");
4122 /* Restore the old message. */
4123 parser->type_definition_forbidden_message = saved_message;
4124
4125 /* And the expression which is being cast. */
4126 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4127 expression = cp_parser_expression (parser, /*cast_p=*/true);
4128 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4129
4130 /* Only type conversions to integral or enumeration types
4131 can be used in constant-expressions. */
4132 if (!cast_valid_in_integral_constant_expression_p (type)
4133 && (cp_parser_non_integral_constant_expression
4134 (parser,
4135 "a cast to a type other than an integral or "
4136 "enumeration type")))
4137 return error_mark_node;
4138
4139 switch (keyword)
4140 {
4141 case RID_DYNCAST:
4142 postfix_expression
4143 = build_dynamic_cast (type, expression);
4144 break;
4145 case RID_STATCAST:
4146 postfix_expression
4147 = build_static_cast (type, expression);
4148 break;
4149 case RID_REINTCAST:
4150 postfix_expression
4151 = build_reinterpret_cast (type, expression);
4152 break;
4153 case RID_CONSTCAST:
4154 postfix_expression
4155 = build_const_cast (type, expression);
4156 break;
4157 default:
4158 gcc_unreachable ();
4159 }
4160 }
4161 break;
4162
4163 case RID_TYPEID:
4164 {
4165 tree type;
4166 const char *saved_message;
4167 bool saved_in_type_id_in_expr_p;
4168
4169 /* Consume the `typeid' token. */
4170 cp_lexer_consume_token (parser->lexer);
4171 /* Look for the `(' token. */
4172 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4173 /* Types cannot be defined in a `typeid' expression. */
4174 saved_message = parser->type_definition_forbidden_message;
4175 parser->type_definition_forbidden_message
4176 = "types may not be defined in a `typeid\' expression";
4177 /* We can't be sure yet whether we're looking at a type-id or an
4178 expression. */
4179 cp_parser_parse_tentatively (parser);
4180 /* Try a type-id first. */
4181 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4182 parser->in_type_id_in_expr_p = true;
4183 type = cp_parser_type_id (parser);
4184 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4185 /* Look for the `)' token. Otherwise, we can't be sure that
4186 we're not looking at an expression: consider `typeid (int
4187 (3))', for example. */
4188 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4189 /* If all went well, simply lookup the type-id. */
4190 if (cp_parser_parse_definitely (parser))
4191 postfix_expression = get_typeid (type);
4192 /* Otherwise, fall back to the expression variant. */
4193 else
4194 {
4195 tree expression;
4196
4197 /* Look for an expression. */
4198 expression = cp_parser_expression (parser, /*cast_p=*/false);
4199 /* Compute its typeid. */
4200 postfix_expression = build_typeid (expression);
4201 /* Look for the `)' token. */
4202 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4203 }
4204 /* Restore the saved message. */
4205 parser->type_definition_forbidden_message = saved_message;
4206 /* `typeid' may not appear in an integral constant expression. */
4207 if (cp_parser_non_integral_constant_expression(parser,
4208 "`typeid' operator"))
4209 return error_mark_node;
4210 }
4211 break;
4212
4213 case RID_TYPENAME:
4214 {
4215 tree type;
4216 /* The syntax permitted here is the same permitted for an
4217 elaborated-type-specifier. */
4218 type = cp_parser_elaborated_type_specifier (parser,
4219 /*is_friend=*/false,
4220 /*is_declaration=*/false);
4221 postfix_expression = cp_parser_functional_cast (parser, type);
4222 }
4223 break;
4224
4225 default:
4226 {
4227 tree type;
4228
4229 /* If the next thing is a simple-type-specifier, we may be
4230 looking at a functional cast. We could also be looking at
4231 an id-expression. So, we try the functional cast, and if
4232 that doesn't work we fall back to the primary-expression. */
4233 cp_parser_parse_tentatively (parser);
4234 /* Look for the simple-type-specifier. */
4235 type = cp_parser_simple_type_specifier (parser,
4236 /*decl_specs=*/NULL,
4237 CP_PARSER_FLAGS_NONE);
4238 /* Parse the cast itself. */
4239 if (!cp_parser_error_occurred (parser))
4240 postfix_expression
4241 = cp_parser_functional_cast (parser, type);
4242 /* If that worked, we're done. */
4243 if (cp_parser_parse_definitely (parser))
4244 break;
4245
4246 /* If the functional-cast didn't work out, try a
4247 compound-literal. */
4248 if (cp_parser_allow_gnu_extensions_p (parser)
4249 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4250 {
4251 VEC(constructor_elt,gc) *initializer_list = NULL;
4252 bool saved_in_type_id_in_expr_p;
4253
4254 cp_parser_parse_tentatively (parser);
4255 /* Consume the `('. */
4256 cp_lexer_consume_token (parser->lexer);
4257 /* Parse the type. */
4258 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4259 parser->in_type_id_in_expr_p = true;
4260 type = cp_parser_type_id (parser);
4261 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4262 /* Look for the `)'. */
4263 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4264 /* Look for the `{'. */
4265 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4266 /* If things aren't going well, there's no need to
4267 keep going. */
4268 if (!cp_parser_error_occurred (parser))
4269 {
4270 bool non_constant_p;
4271 /* Parse the initializer-list. */
4272 initializer_list
4273 = cp_parser_initializer_list (parser, &non_constant_p);
4274 /* Allow a trailing `,'. */
4275 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4276 cp_lexer_consume_token (parser->lexer);
4277 /* Look for the final `}'. */
4278 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4279 }
4280 /* If that worked, we're definitely looking at a
4281 compound-literal expression. */
4282 if (cp_parser_parse_definitely (parser))
4283 {
4284 /* Warn the user that a compound literal is not
4285 allowed in standard C++. */
4286 if (pedantic)
4287 pedwarn ("ISO C++ forbids compound-literals");
4288 /* For simplicitly, we disallow compound literals in
4289 constant-expressions for simpliicitly. We could
4290 allow compound literals of integer type, whose
4291 initializer was a constant, in constant
4292 expressions. Permitting that usage, as a further
4293 extension, would not change the meaning of any
4294 currently accepted programs. (Of course, as
4295 compound literals are not part of ISO C++, the
4296 standard has nothing to say.) */
4297 if (cp_parser_non_integral_constant_expression
4298 (parser, "non-constant compound literals"))
4299 {
4300 postfix_expression = error_mark_node;
4301 break;
4302 }
4303 /* Form the representation of the compound-literal. */
4304 postfix_expression
4305 = finish_compound_literal (type, initializer_list);
4306 break;
4307 }
4308 }
4309
4310 /* It must be a primary-expression. */
4311 postfix_expression
4312 = cp_parser_primary_expression (parser, address_p, cast_p,
4313 /*template_arg_p=*/false,
4314 &idk);
4315 }
4316 break;
4317 }
4318
4319 /* Keep looping until the postfix-expression is complete. */
4320 while (true)
4321 {
4322 if (idk == CP_ID_KIND_UNQUALIFIED
4323 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4324 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4325 /* It is not a Koenig lookup function call. */
4326 postfix_expression
4327 = unqualified_name_lookup_error (postfix_expression);
4328
4329 /* Peek at the next token. */
4330 token = cp_lexer_peek_token (parser->lexer);
4331
4332 switch (token->type)
4333 {
4334 case CPP_OPEN_SQUARE:
4335 postfix_expression
4336 = cp_parser_postfix_open_square_expression (parser,
4337 postfix_expression,
4338 false);
4339 idk = CP_ID_KIND_NONE;
4340 break;
4341
4342 case CPP_OPEN_PAREN:
4343 /* postfix-expression ( expression-list [opt] ) */
4344 {
4345 bool koenig_p;
4346 bool is_builtin_constant_p;
4347 bool saved_integral_constant_expression_p = false;
4348 bool saved_non_integral_constant_expression_p = false;
4349 tree args;
4350
4351 is_builtin_constant_p
4352 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4353 if (is_builtin_constant_p)
4354 {
4355 /* The whole point of __builtin_constant_p is to allow
4356 non-constant expressions to appear as arguments. */
4357 saved_integral_constant_expression_p
4358 = parser->integral_constant_expression_p;
4359 saved_non_integral_constant_expression_p
4360 = parser->non_integral_constant_expression_p;
4361 parser->integral_constant_expression_p = false;
4362 }
4363 args = (cp_parser_parenthesized_expression_list
4364 (parser, /*is_attribute_list=*/false,
4365 /*cast_p=*/false,
4366 /*non_constant_p=*/NULL));
4367 if (is_builtin_constant_p)
4368 {
4369 parser->integral_constant_expression_p
4370 = saved_integral_constant_expression_p;
4371 parser->non_integral_constant_expression_p
4372 = saved_non_integral_constant_expression_p;
4373 }
4374
4375 if (args == error_mark_node)
4376 {
4377 postfix_expression = error_mark_node;
4378 break;
4379 }
4380
4381 /* Function calls are not permitted in
4382 constant-expressions. */
4383 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4384 && cp_parser_non_integral_constant_expression (parser,
4385 "a function call"))
4386 {
4387 postfix_expression = error_mark_node;
4388 break;
4389 }
4390
4391 koenig_p = false;
4392 if (idk == CP_ID_KIND_UNQUALIFIED)
4393 {
4394 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4395 {
4396 if (args)
4397 {
4398 koenig_p = true;
4399 postfix_expression
4400 = perform_koenig_lookup (postfix_expression, args);
4401 }
4402 else
4403 postfix_expression
4404 = unqualified_fn_lookup_error (postfix_expression);
4405 }
4406 /* We do not perform argument-dependent lookup if
4407 normal lookup finds a non-function, in accordance
4408 with the expected resolution of DR 218. */
4409 else if (args && is_overloaded_fn (postfix_expression))
4410 {
4411 tree fn = get_first_fn (postfix_expression);
4412
4413 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4414 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4415
4416 /* Only do argument dependent lookup if regular
4417 lookup does not find a set of member functions.
4418 [basic.lookup.koenig]/2a */
4419 if (!DECL_FUNCTION_MEMBER_P (fn))
4420 {
4421 koenig_p = true;
4422 postfix_expression
4423 = perform_koenig_lookup (postfix_expression, args);
4424 }
4425 }
4426 }
4427
4428 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4429 {
4430 tree instance = TREE_OPERAND (postfix_expression, 0);
4431 tree fn = TREE_OPERAND (postfix_expression, 1);
4432
4433 if (processing_template_decl
4434 && (type_dependent_expression_p (instance)
4435 || (!BASELINK_P (fn)
4436 && TREE_CODE (fn) != FIELD_DECL)
4437 || type_dependent_expression_p (fn)
4438 || any_type_dependent_arguments_p (args)))
4439 {
4440 postfix_expression
4441 = build_min_nt (CALL_EXPR, postfix_expression,
4442 args, NULL_TREE);
4443 break;
4444 }
4445
4446 if (BASELINK_P (fn))
4447 postfix_expression
4448 = (build_new_method_call
4449 (instance, fn, args, NULL_TREE,
4450 (idk == CP_ID_KIND_QUALIFIED
4451 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4452 /*fn_p=*/NULL));
4453 else
4454 postfix_expression
4455 = finish_call_expr (postfix_expression, args,
4456 /*disallow_virtual=*/false,
4457 /*koenig_p=*/false);
4458 }
4459 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4460 || TREE_CODE (postfix_expression) == MEMBER_REF
4461 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4462 postfix_expression = (build_offset_ref_call_from_tree
4463 (postfix_expression, args));
4464 else if (idk == CP_ID_KIND_QUALIFIED)
4465 /* A call to a static class member, or a namespace-scope
4466 function. */
4467 postfix_expression
4468 = finish_call_expr (postfix_expression, args,
4469 /*disallow_virtual=*/true,
4470 koenig_p);
4471 else
4472 /* All other function calls. */
4473 postfix_expression
4474 = finish_call_expr (postfix_expression, args,
4475 /*disallow_virtual=*/false,
4476 koenig_p);
4477
4478 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4479 idk = CP_ID_KIND_NONE;
4480 }
4481 break;
4482
4483 case CPP_DOT:
4484 case CPP_DEREF:
4485 /* postfix-expression . template [opt] id-expression
4486 postfix-expression . pseudo-destructor-name
4487 postfix-expression -> template [opt] id-expression
4488 postfix-expression -> pseudo-destructor-name */
4489
4490 /* Consume the `.' or `->' operator. */
4491 cp_lexer_consume_token (parser->lexer);
4492
4493 postfix_expression
4494 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4495 postfix_expression,
4496 false, &idk);
4497 break;
4498
4499 case CPP_PLUS_PLUS:
4500 /* postfix-expression ++ */
4501 /* Consume the `++' token. */
4502 cp_lexer_consume_token (parser->lexer);
4503 /* Generate a representation for the complete expression. */
4504 postfix_expression
4505 = finish_increment_expr (postfix_expression,
4506 POSTINCREMENT_EXPR);
4507 /* Increments may not appear in constant-expressions. */
4508 if (cp_parser_non_integral_constant_expression (parser,
4509 "an increment"))
4510 postfix_expression = error_mark_node;
4511 idk = CP_ID_KIND_NONE;
4512 break;
4513
4514 case CPP_MINUS_MINUS:
4515 /* postfix-expression -- */
4516 /* Consume the `--' token. */
4517 cp_lexer_consume_token (parser->lexer);
4518 /* Generate a representation for the complete expression. */
4519 postfix_expression
4520 = finish_increment_expr (postfix_expression,
4521 POSTDECREMENT_EXPR);
4522 /* Decrements may not appear in constant-expressions. */
4523 if (cp_parser_non_integral_constant_expression (parser,
4524 "a decrement"))
4525 postfix_expression = error_mark_node;
4526 idk = CP_ID_KIND_NONE;
4527 break;
4528
4529 default:
4530 return postfix_expression;
4531 }
4532 }
4533
4534 /* We should never get here. */
4535 gcc_unreachable ();
4536 return error_mark_node;
4537 }
4538
4539 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4540 by cp_parser_builtin_offsetof. We're looking for
4541
4542 postfix-expression [ expression ]
4543
4544 FOR_OFFSETOF is set if we're being called in that context, which
4545 changes how we deal with integer constant expressions. */
4546
4547 static tree
cp_parser_postfix_open_square_expression(cp_parser * parser,tree postfix_expression,bool for_offsetof)4548 cp_parser_postfix_open_square_expression (cp_parser *parser,
4549 tree postfix_expression,
4550 bool for_offsetof)
4551 {
4552 tree index;
4553
4554 /* Consume the `[' token. */
4555 cp_lexer_consume_token (parser->lexer);
4556
4557 /* Parse the index expression. */
4558 /* ??? For offsetof, there is a question of what to allow here. If
4559 offsetof is not being used in an integral constant expression context,
4560 then we *could* get the right answer by computing the value at runtime.
4561 If we are in an integral constant expression context, then we might
4562 could accept any constant expression; hard to say without analysis.
4563 Rather than open the barn door too wide right away, allow only integer
4564 constant expressions here. */
4565 if (for_offsetof)
4566 index = cp_parser_constant_expression (parser, false, NULL);
4567 else
4568 index = cp_parser_expression (parser, /*cast_p=*/false);
4569
4570 /* Look for the closing `]'. */
4571 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4572
4573 /* Build the ARRAY_REF. */
4574 postfix_expression = grok_array_decl (postfix_expression, index);
4575
4576 /* When not doing offsetof, array references are not permitted in
4577 constant-expressions. */
4578 if (!for_offsetof
4579 && (cp_parser_non_integral_constant_expression
4580 (parser, "an array reference")))
4581 postfix_expression = error_mark_node;
4582
4583 return postfix_expression;
4584 }
4585
4586 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4587 by cp_parser_builtin_offsetof. We're looking for
4588
4589 postfix-expression . template [opt] id-expression
4590 postfix-expression . pseudo-destructor-name
4591 postfix-expression -> template [opt] id-expression
4592 postfix-expression -> pseudo-destructor-name
4593
4594 FOR_OFFSETOF is set if we're being called in that context. That sorta
4595 limits what of the above we'll actually accept, but nevermind.
4596 TOKEN_TYPE is the "." or "->" token, which will already have been
4597 removed from the stream. */
4598
4599 static tree
cp_parser_postfix_dot_deref_expression(cp_parser * parser,enum cpp_ttype token_type,tree postfix_expression,bool for_offsetof,cp_id_kind * idk)4600 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4601 enum cpp_ttype token_type,
4602 tree postfix_expression,
4603 bool for_offsetof, cp_id_kind *idk)
4604 {
4605 tree name;
4606 bool dependent_p;
4607 bool pseudo_destructor_p;
4608 tree scope = NULL_TREE;
4609
4610 /* If this is a `->' operator, dereference the pointer. */
4611 if (token_type == CPP_DEREF)
4612 postfix_expression = build_x_arrow (postfix_expression);
4613 /* Check to see whether or not the expression is type-dependent. */
4614 dependent_p = type_dependent_expression_p (postfix_expression);
4615 /* The identifier following the `->' or `.' is not qualified. */
4616 parser->scope = NULL_TREE;
4617 parser->qualifying_scope = NULL_TREE;
4618 parser->object_scope = NULL_TREE;
4619 *idk = CP_ID_KIND_NONE;
4620 /* Enter the scope corresponding to the type of the object
4621 given by the POSTFIX_EXPRESSION. */
4622 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4623 {
4624 scope = TREE_TYPE (postfix_expression);
4625 /* According to the standard, no expression should ever have
4626 reference type. Unfortunately, we do not currently match
4627 the standard in this respect in that our internal representation
4628 of an expression may have reference type even when the standard
4629 says it does not. Therefore, we have to manually obtain the
4630 underlying type here. */
4631 scope = non_reference (scope);
4632 /* The type of the POSTFIX_EXPRESSION must be complete. */
4633 if (scope == unknown_type_node)
4634 {
4635 error ("%qE does not have class type", postfix_expression);
4636 scope = NULL_TREE;
4637 }
4638 else
4639 scope = complete_type_or_else (scope, NULL_TREE);
4640 /* Let the name lookup machinery know that we are processing a
4641 class member access expression. */
4642 parser->context->object_type = scope;
4643 /* If something went wrong, we want to be able to discern that case,
4644 as opposed to the case where there was no SCOPE due to the type
4645 of expression being dependent. */
4646 if (!scope)
4647 scope = error_mark_node;
4648 /* If the SCOPE was erroneous, make the various semantic analysis
4649 functions exit quickly -- and without issuing additional error
4650 messages. */
4651 if (scope == error_mark_node)
4652 postfix_expression = error_mark_node;
4653 }
4654
4655 /* Assume this expression is not a pseudo-destructor access. */
4656 pseudo_destructor_p = false;
4657
4658 /* If the SCOPE is a scalar type, then, if this is a valid program,
4659 we must be looking at a pseudo-destructor-name. */
4660 if (scope && SCALAR_TYPE_P (scope))
4661 {
4662 tree s;
4663 tree type;
4664
4665 cp_parser_parse_tentatively (parser);
4666 /* Parse the pseudo-destructor-name. */
4667 s = NULL_TREE;
4668 cp_parser_pseudo_destructor_name (parser, &s, &type);
4669 if (cp_parser_parse_definitely (parser))
4670 {
4671 pseudo_destructor_p = true;
4672 postfix_expression
4673 = finish_pseudo_destructor_expr (postfix_expression,
4674 s, TREE_TYPE (type));
4675 }
4676 }
4677
4678 if (!pseudo_destructor_p)
4679 {
4680 /* If the SCOPE is not a scalar type, we are looking at an
4681 ordinary class member access expression, rather than a
4682 pseudo-destructor-name. */
4683 bool template_p;
4684 /* Parse the id-expression. */
4685 name = (cp_parser_id_expression
4686 (parser,
4687 cp_parser_optional_template_keyword (parser),
4688 /*check_dependency_p=*/true,
4689 &template_p,
4690 /*declarator_p=*/false,
4691 /*optional_p=*/false));
4692 /* In general, build a SCOPE_REF if the member name is qualified.
4693 However, if the name was not dependent and has already been
4694 resolved; there is no need to build the SCOPE_REF. For example;
4695
4696 struct X { void f(); };
4697 template <typename T> void f(T* t) { t->X::f(); }
4698
4699 Even though "t" is dependent, "X::f" is not and has been resolved
4700 to a BASELINK; there is no need to include scope information. */
4701
4702 /* But we do need to remember that there was an explicit scope for
4703 virtual function calls. */
4704 if (parser->scope)
4705 *idk = CP_ID_KIND_QUALIFIED;
4706
4707 /* If the name is a template-id that names a type, we will get a
4708 TYPE_DECL here. That is invalid code. */
4709 if (TREE_CODE (name) == TYPE_DECL)
4710 {
4711 error ("invalid use of %qD", name);
4712 postfix_expression = error_mark_node;
4713 }
4714 else
4715 {
4716 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4717 {
4718 name = build_qualified_name (/*type=*/NULL_TREE,
4719 parser->scope,
4720 name,
4721 template_p);
4722 parser->scope = NULL_TREE;
4723 parser->qualifying_scope = NULL_TREE;
4724 parser->object_scope = NULL_TREE;
4725 }
4726 if (scope && name && BASELINK_P (name))
4727 adjust_result_of_qualified_name_lookup
4728 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4729 postfix_expression
4730 = finish_class_member_access_expr (postfix_expression, name,
4731 template_p);
4732 }
4733 }
4734
4735 /* We no longer need to look up names in the scope of the object on
4736 the left-hand side of the `.' or `->' operator. */
4737 parser->context->object_type = NULL_TREE;
4738
4739 /* Outside of offsetof, these operators may not appear in
4740 constant-expressions. */
4741 if (!for_offsetof
4742 && (cp_parser_non_integral_constant_expression
4743 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4744 postfix_expression = error_mark_node;
4745
4746 return postfix_expression;
4747 }
4748
4749 /* Parse a parenthesized expression-list.
4750
4751 expression-list:
4752 assignment-expression
4753 expression-list, assignment-expression
4754
4755 attribute-list:
4756 expression-list
4757 identifier
4758 identifier, expression-list
4759
4760 CAST_P is true if this expression is the target of a cast.
4761
4762 Returns a TREE_LIST. The TREE_VALUE of each node is a
4763 representation of an assignment-expression. Note that a TREE_LIST
4764 is returned even if there is only a single expression in the list.
4765 error_mark_node is returned if the ( and or ) are
4766 missing. NULL_TREE is returned on no expressions. The parentheses
4767 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4768 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4769 indicates whether or not all of the expressions in the list were
4770 constant. */
4771
4772 static tree
cp_parser_parenthesized_expression_list(cp_parser * parser,bool is_attribute_list,bool cast_p,bool * non_constant_p)4773 cp_parser_parenthesized_expression_list (cp_parser* parser,
4774 bool is_attribute_list,
4775 bool cast_p,
4776 bool *non_constant_p)
4777 {
4778 tree expression_list = NULL_TREE;
4779 bool fold_expr_p = is_attribute_list;
4780 tree identifier = NULL_TREE;
4781
4782 /* Assume all the expressions will be constant. */
4783 if (non_constant_p)
4784 *non_constant_p = false;
4785
4786 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4787 return error_mark_node;
4788
4789 /* Consume expressions until there are no more. */
4790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4791 while (true)
4792 {
4793 tree expr;
4794
4795 /* At the beginning of attribute lists, check to see if the
4796 next token is an identifier. */
4797 if (is_attribute_list
4798 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4799 {
4800 cp_token *token;
4801
4802 /* Consume the identifier. */
4803 token = cp_lexer_consume_token (parser->lexer);
4804 /* Save the identifier. */
4805 identifier = token->u.value;
4806 }
4807 else
4808 {
4809 /* Parse the next assignment-expression. */
4810 if (non_constant_p)
4811 {
4812 bool expr_non_constant_p;
4813 expr = (cp_parser_constant_expression
4814 (parser, /*allow_non_constant_p=*/true,
4815 &expr_non_constant_p));
4816 if (expr_non_constant_p)
4817 *non_constant_p = true;
4818 }
4819 else
4820 expr = cp_parser_assignment_expression (parser, cast_p);
4821
4822 if (fold_expr_p)
4823 expr = fold_non_dependent_expr (expr);
4824
4825 /* Add it to the list. We add error_mark_node
4826 expressions to the list, so that we can still tell if
4827 the correct form for a parenthesized expression-list
4828 is found. That gives better errors. */
4829 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4830
4831 if (expr == error_mark_node)
4832 goto skip_comma;
4833 }
4834
4835 /* After the first item, attribute lists look the same as
4836 expression lists. */
4837 is_attribute_list = false;
4838
4839 get_comma:;
4840 /* If the next token isn't a `,', then we are done. */
4841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4842 break;
4843
4844 /* Otherwise, consume the `,' and keep going. */
4845 cp_lexer_consume_token (parser->lexer);
4846 }
4847
4848 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4849 {
4850 int ending;
4851
4852 skip_comma:;
4853 /* We try and resync to an unnested comma, as that will give the
4854 user better diagnostics. */
4855 ending = cp_parser_skip_to_closing_parenthesis (parser,
4856 /*recovering=*/true,
4857 /*or_comma=*/true,
4858 /*consume_paren=*/true);
4859 if (ending < 0)
4860 goto get_comma;
4861 if (!ending)
4862 return error_mark_node;
4863 }
4864
4865 /* We built up the list in reverse order so we must reverse it now. */
4866 expression_list = nreverse (expression_list);
4867 if (identifier)
4868 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4869
4870 return expression_list;
4871 }
4872
4873 /* Parse a pseudo-destructor-name.
4874
4875 pseudo-destructor-name:
4876 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4877 :: [opt] nested-name-specifier template template-id :: ~ type-name
4878 :: [opt] nested-name-specifier [opt] ~ type-name
4879
4880 If either of the first two productions is used, sets *SCOPE to the
4881 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4882 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4883 or ERROR_MARK_NODE if the parse fails. */
4884
4885 static void
cp_parser_pseudo_destructor_name(cp_parser * parser,tree * scope,tree * type)4886 cp_parser_pseudo_destructor_name (cp_parser* parser,
4887 tree* scope,
4888 tree* type)
4889 {
4890 bool nested_name_specifier_p;
4891
4892 /* Assume that things will not work out. */
4893 *type = error_mark_node;
4894
4895 /* Look for the optional `::' operator. */
4896 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4897 /* Look for the optional nested-name-specifier. */
4898 nested_name_specifier_p
4899 = (cp_parser_nested_name_specifier_opt (parser,
4900 /*typename_keyword_p=*/false,
4901 /*check_dependency_p=*/true,
4902 /*type_p=*/false,
4903 /*is_declaration=*/true)
4904 != NULL_TREE);
4905 /* Now, if we saw a nested-name-specifier, we might be doing the
4906 second production. */
4907 if (nested_name_specifier_p
4908 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4909 {
4910 /* Consume the `template' keyword. */
4911 cp_lexer_consume_token (parser->lexer);
4912 /* Parse the template-id. */
4913 cp_parser_template_id (parser,
4914 /*template_keyword_p=*/true,
4915 /*check_dependency_p=*/false,
4916 /*is_declaration=*/true);
4917 /* Look for the `::' token. */
4918 cp_parser_require (parser, CPP_SCOPE, "`::'");
4919 }
4920 /* If the next token is not a `~', then there might be some
4921 additional qualification. */
4922 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4923 {
4924 /* Look for the type-name. */
4925 *scope = TREE_TYPE (cp_parser_type_name (parser));
4926
4927 if (*scope == error_mark_node)
4928 return;
4929
4930 /* If we don't have ::~, then something has gone wrong. Since
4931 the only caller of this function is looking for something
4932 after `.' or `->' after a scalar type, most likely the
4933 program is trying to get a member of a non-aggregate
4934 type. */
4935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4936 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4937 {
4938 cp_parser_error (parser, "request for member of non-aggregate type");
4939 return;
4940 }
4941
4942 /* Look for the `::' token. */
4943 cp_parser_require (parser, CPP_SCOPE, "`::'");
4944 }
4945 else
4946 *scope = NULL_TREE;
4947
4948 /* Look for the `~'. */
4949 cp_parser_require (parser, CPP_COMPL, "`~'");
4950 /* Look for the type-name again. We are not responsible for
4951 checking that it matches the first type-name. */
4952 *type = cp_parser_type_name (parser);
4953 }
4954
4955 /* Parse a unary-expression.
4956
4957 unary-expression:
4958 postfix-expression
4959 ++ cast-expression
4960 -- cast-expression
4961 unary-operator cast-expression
4962 sizeof unary-expression
4963 sizeof ( type-id )
4964 new-expression
4965 delete-expression
4966
4967 GNU Extensions:
4968
4969 unary-expression:
4970 __extension__ cast-expression
4971 __alignof__ unary-expression
4972 __alignof__ ( type-id )
4973 __real__ cast-expression
4974 __imag__ cast-expression
4975 && identifier
4976
4977 ADDRESS_P is true iff the unary-expression is appearing as the
4978 operand of the `&' operator. CAST_P is true if this expression is
4979 the target of a cast.
4980
4981 Returns a representation of the expression. */
4982
4983 static tree
cp_parser_unary_expression(cp_parser * parser,bool address_p,bool cast_p)4984 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4985 {
4986 cp_token *token;
4987 enum tree_code unary_operator;
4988
4989 /* Peek at the next token. */
4990 token = cp_lexer_peek_token (parser->lexer);
4991 /* Some keywords give away the kind of expression. */
4992 if (token->type == CPP_KEYWORD)
4993 {
4994 enum rid keyword = token->keyword;
4995
4996 switch (keyword)
4997 {
4998 case RID_ALIGNOF:
4999 case RID_SIZEOF:
5000 {
5001 tree operand;
5002 enum tree_code op;
5003
5004 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5005 /* Consume the token. */
5006 cp_lexer_consume_token (parser->lexer);
5007 /* Parse the operand. */
5008 operand = cp_parser_sizeof_operand (parser, keyword);
5009
5010 if (TYPE_P (operand))
5011 return cxx_sizeof_or_alignof_type (operand, op, true);
5012 else
5013 return cxx_sizeof_or_alignof_expr (operand, op);
5014 }
5015
5016 case RID_NEW:
5017 return cp_parser_new_expression (parser);
5018
5019 case RID_DELETE:
5020 return cp_parser_delete_expression (parser);
5021
5022 case RID_EXTENSION:
5023 {
5024 /* The saved value of the PEDANTIC flag. */
5025 int saved_pedantic;
5026 tree expr;
5027
5028 /* Save away the PEDANTIC flag. */
5029 cp_parser_extension_opt (parser, &saved_pedantic);
5030 /* Parse the cast-expression. */
5031 expr = cp_parser_simple_cast_expression (parser);
5032 /* Restore the PEDANTIC flag. */
5033 pedantic = saved_pedantic;
5034
5035 return expr;
5036 }
5037
5038 case RID_REALPART:
5039 case RID_IMAGPART:
5040 {
5041 tree expression;
5042
5043 /* Consume the `__real__' or `__imag__' token. */
5044 cp_lexer_consume_token (parser->lexer);
5045 /* Parse the cast-expression. */
5046 expression = cp_parser_simple_cast_expression (parser);
5047 /* Create the complete representation. */
5048 return build_x_unary_op ((keyword == RID_REALPART
5049 ? REALPART_EXPR : IMAGPART_EXPR),
5050 expression);
5051 }
5052 break;
5053
5054 default:
5055 break;
5056 }
5057 }
5058
5059 /* Look for the `:: new' and `:: delete', which also signal the
5060 beginning of a new-expression, or delete-expression,
5061 respectively. If the next token is `::', then it might be one of
5062 these. */
5063 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5064 {
5065 enum rid keyword;
5066
5067 /* See if the token after the `::' is one of the keywords in
5068 which we're interested. */
5069 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5070 /* If it's `new', we have a new-expression. */
5071 if (keyword == RID_NEW)
5072 return cp_parser_new_expression (parser);
5073 /* Similarly, for `delete'. */
5074 else if (keyword == RID_DELETE)
5075 return cp_parser_delete_expression (parser);
5076 }
5077
5078 /* Look for a unary operator. */
5079 unary_operator = cp_parser_unary_operator (token);
5080 /* The `++' and `--' operators can be handled similarly, even though
5081 they are not technically unary-operators in the grammar. */
5082 if (unary_operator == ERROR_MARK)
5083 {
5084 if (token->type == CPP_PLUS_PLUS)
5085 unary_operator = PREINCREMENT_EXPR;
5086 else if (token->type == CPP_MINUS_MINUS)
5087 unary_operator = PREDECREMENT_EXPR;
5088 /* Handle the GNU address-of-label extension. */
5089 else if (cp_parser_allow_gnu_extensions_p (parser)
5090 && token->type == CPP_AND_AND)
5091 {
5092 tree identifier;
5093
5094 /* Consume the '&&' token. */
5095 cp_lexer_consume_token (parser->lexer);
5096 /* Look for the identifier. */
5097 identifier = cp_parser_identifier (parser);
5098 /* Create an expression representing the address. */
5099 return finish_label_address_expr (identifier);
5100 }
5101 }
5102 if (unary_operator != ERROR_MARK)
5103 {
5104 tree cast_expression;
5105 tree expression = error_mark_node;
5106 const char *non_constant_p = NULL;
5107
5108 /* Consume the operator token. */
5109 token = cp_lexer_consume_token (parser->lexer);
5110 /* Parse the cast-expression. */
5111 cast_expression
5112 = cp_parser_cast_expression (parser,
5113 unary_operator == ADDR_EXPR,
5114 /*cast_p=*/false);
5115 /* Now, build an appropriate representation. */
5116 switch (unary_operator)
5117 {
5118 case INDIRECT_REF:
5119 non_constant_p = "`*'";
5120 expression = build_x_indirect_ref (cast_expression, "unary *");
5121 break;
5122
5123 case ADDR_EXPR:
5124 non_constant_p = "`&'";
5125 /* Fall through. */
5126 case BIT_NOT_EXPR:
5127 expression = build_x_unary_op (unary_operator, cast_expression);
5128 break;
5129
5130 case PREINCREMENT_EXPR:
5131 case PREDECREMENT_EXPR:
5132 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5133 ? "`++'" : "`--'");
5134 /* Fall through. */
5135 case UNARY_PLUS_EXPR:
5136 case NEGATE_EXPR:
5137 case TRUTH_NOT_EXPR:
5138 expression = finish_unary_op_expr (unary_operator, cast_expression);
5139 break;
5140
5141 default:
5142 gcc_unreachable ();
5143 }
5144
5145 if (non_constant_p
5146 && cp_parser_non_integral_constant_expression (parser,
5147 non_constant_p))
5148 expression = error_mark_node;
5149
5150 return expression;
5151 }
5152
5153 return cp_parser_postfix_expression (parser, address_p, cast_p);
5154 }
5155
5156 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5157 unary-operator, the corresponding tree code is returned. */
5158
5159 static enum tree_code
cp_parser_unary_operator(cp_token * token)5160 cp_parser_unary_operator (cp_token* token)
5161 {
5162 switch (token->type)
5163 {
5164 case CPP_MULT:
5165 return INDIRECT_REF;
5166
5167 case CPP_AND:
5168 return ADDR_EXPR;
5169
5170 case CPP_PLUS:
5171 return UNARY_PLUS_EXPR;
5172
5173 case CPP_MINUS:
5174 return NEGATE_EXPR;
5175
5176 case CPP_NOT:
5177 return TRUTH_NOT_EXPR;
5178
5179 case CPP_COMPL:
5180 return BIT_NOT_EXPR;
5181
5182 default:
5183 return ERROR_MARK;
5184 }
5185 }
5186
5187 /* Parse a new-expression.
5188
5189 new-expression:
5190 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5191 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5192
5193 Returns a representation of the expression. */
5194
5195 static tree
cp_parser_new_expression(cp_parser * parser)5196 cp_parser_new_expression (cp_parser* parser)
5197 {
5198 bool global_scope_p;
5199 tree placement;
5200 tree type;
5201 tree initializer;
5202 tree nelts;
5203
5204 /* Look for the optional `::' operator. */
5205 global_scope_p
5206 = (cp_parser_global_scope_opt (parser,
5207 /*current_scope_valid_p=*/false)
5208 != NULL_TREE);
5209 /* Look for the `new' operator. */
5210 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5211 /* There's no easy way to tell a new-placement from the
5212 `( type-id )' construct. */
5213 cp_parser_parse_tentatively (parser);
5214 /* Look for a new-placement. */
5215 placement = cp_parser_new_placement (parser);
5216 /* If that didn't work out, there's no new-placement. */
5217 if (!cp_parser_parse_definitely (parser))
5218 placement = NULL_TREE;
5219
5220 /* If the next token is a `(', then we have a parenthesized
5221 type-id. */
5222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5223 {
5224 /* Consume the `('. */
5225 cp_lexer_consume_token (parser->lexer);
5226 /* Parse the type-id. */
5227 type = cp_parser_type_id (parser);
5228 /* Look for the closing `)'. */
5229 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5230 /* There should not be a direct-new-declarator in this production,
5231 but GCC used to allowed this, so we check and emit a sensible error
5232 message for this case. */
5233 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5234 {
5235 error ("array bound forbidden after parenthesized type-id");
5236 inform ("try removing the parentheses around the type-id");
5237 cp_parser_direct_new_declarator (parser);
5238 }
5239 nelts = NULL_TREE;
5240 }
5241 /* Otherwise, there must be a new-type-id. */
5242 else
5243 type = cp_parser_new_type_id (parser, &nelts);
5244
5245 /* If the next token is a `(', then we have a new-initializer. */
5246 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5247 initializer = cp_parser_new_initializer (parser);
5248 else
5249 initializer = NULL_TREE;
5250
5251 /* A new-expression may not appear in an integral constant
5252 expression. */
5253 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5254 return error_mark_node;
5255
5256 /* Create a representation of the new-expression. */
5257 return build_new (placement, type, nelts, initializer, global_scope_p);
5258 }
5259
5260 /* Parse a new-placement.
5261
5262 new-placement:
5263 ( expression-list )
5264
5265 Returns the same representation as for an expression-list. */
5266
5267 static tree
cp_parser_new_placement(cp_parser * parser)5268 cp_parser_new_placement (cp_parser* parser)
5269 {
5270 tree expression_list;
5271
5272 /* Parse the expression-list. */
5273 expression_list = (cp_parser_parenthesized_expression_list
5274 (parser, false, /*cast_p=*/false,
5275 /*non_constant_p=*/NULL));
5276
5277 return expression_list;
5278 }
5279
5280 /* Parse a new-type-id.
5281
5282 new-type-id:
5283 type-specifier-seq new-declarator [opt]
5284
5285 Returns the TYPE allocated. If the new-type-id indicates an array
5286 type, *NELTS is set to the number of elements in the last array
5287 bound; the TYPE will not include the last array bound. */
5288
5289 static tree
cp_parser_new_type_id(cp_parser * parser,tree * nelts)5290 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5291 {
5292 cp_decl_specifier_seq type_specifier_seq;
5293 cp_declarator *new_declarator;
5294 cp_declarator *declarator;
5295 cp_declarator *outer_declarator;
5296 const char *saved_message;
5297 tree type;
5298
5299 /* The type-specifier sequence must not contain type definitions.
5300 (It cannot contain declarations of new types either, but if they
5301 are not definitions we will catch that because they are not
5302 complete.) */
5303 saved_message = parser->type_definition_forbidden_message;
5304 parser->type_definition_forbidden_message
5305 = "types may not be defined in a new-type-id";
5306 /* Parse the type-specifier-seq. */
5307 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5308 &type_specifier_seq);
5309 /* Restore the old message. */
5310 parser->type_definition_forbidden_message = saved_message;
5311 /* Parse the new-declarator. */
5312 new_declarator = cp_parser_new_declarator_opt (parser);
5313
5314 /* Determine the number of elements in the last array dimension, if
5315 any. */
5316 *nelts = NULL_TREE;
5317 /* Skip down to the last array dimension. */
5318 declarator = new_declarator;
5319 outer_declarator = NULL;
5320 while (declarator && (declarator->kind == cdk_pointer
5321 || declarator->kind == cdk_ptrmem))
5322 {
5323 outer_declarator = declarator;
5324 declarator = declarator->declarator;
5325 }
5326 while (declarator
5327 && declarator->kind == cdk_array
5328 && declarator->declarator
5329 && declarator->declarator->kind == cdk_array)
5330 {
5331 outer_declarator = declarator;
5332 declarator = declarator->declarator;
5333 }
5334
5335 if (declarator && declarator->kind == cdk_array)
5336 {
5337 *nelts = declarator->u.array.bounds;
5338 if (*nelts == error_mark_node)
5339 *nelts = integer_one_node;
5340
5341 if (outer_declarator)
5342 outer_declarator->declarator = declarator->declarator;
5343 else
5344 new_declarator = NULL;
5345 }
5346
5347 type = groktypename (&type_specifier_seq, new_declarator);
5348 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5349 {
5350 *nelts = array_type_nelts_top (type);
5351 type = TREE_TYPE (type);
5352 }
5353 return type;
5354 }
5355
5356 /* Parse an (optional) new-declarator.
5357
5358 new-declarator:
5359 ptr-operator new-declarator [opt]
5360 direct-new-declarator
5361
5362 Returns the declarator. */
5363
5364 static cp_declarator *
cp_parser_new_declarator_opt(cp_parser * parser)5365 cp_parser_new_declarator_opt (cp_parser* parser)
5366 {
5367 enum tree_code code;
5368 tree type;
5369 cp_cv_quals cv_quals;
5370
5371 /* We don't know if there's a ptr-operator next, or not. */
5372 cp_parser_parse_tentatively (parser);
5373 /* Look for a ptr-operator. */
5374 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5375 /* If that worked, look for more new-declarators. */
5376 if (cp_parser_parse_definitely (parser))
5377 {
5378 cp_declarator *declarator;
5379
5380 /* Parse another optional declarator. */
5381 declarator = cp_parser_new_declarator_opt (parser);
5382
5383 /* Create the representation of the declarator. */
5384 if (type)
5385 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5386 else if (code == INDIRECT_REF)
5387 declarator = make_pointer_declarator (cv_quals, declarator);
5388 else
5389 declarator = make_reference_declarator (cv_quals, declarator);
5390
5391 return declarator;
5392 }
5393
5394 /* If the next token is a `[', there is a direct-new-declarator. */
5395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5396 return cp_parser_direct_new_declarator (parser);
5397
5398 return NULL;
5399 }
5400
5401 /* Parse a direct-new-declarator.
5402
5403 direct-new-declarator:
5404 [ expression ]
5405 direct-new-declarator [constant-expression]
5406
5407 */
5408
5409 static cp_declarator *
cp_parser_direct_new_declarator(cp_parser * parser)5410 cp_parser_direct_new_declarator (cp_parser* parser)
5411 {
5412 cp_declarator *declarator = NULL;
5413
5414 while (true)
5415 {
5416 tree expression;
5417
5418 /* Look for the opening `['. */
5419 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5420 /* The first expression is not required to be constant. */
5421 if (!declarator)
5422 {
5423 expression = cp_parser_expression (parser, /*cast_p=*/false);
5424 /* The standard requires that the expression have integral
5425 type. DR 74 adds enumeration types. We believe that the
5426 real intent is that these expressions be handled like the
5427 expression in a `switch' condition, which also allows
5428 classes with a single conversion to integral or
5429 enumeration type. */
5430 if (!processing_template_decl)
5431 {
5432 expression
5433 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5434 expression,
5435 /*complain=*/true);
5436 if (!expression)
5437 {
5438 error ("expression in new-declarator must have integral "
5439 "or enumeration type");
5440 expression = error_mark_node;
5441 }
5442 }
5443 }
5444 /* But all the other expressions must be. */
5445 else
5446 expression
5447 = cp_parser_constant_expression (parser,
5448 /*allow_non_constant=*/false,
5449 NULL);
5450 /* Look for the closing `]'. */
5451 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5452
5453 /* Add this bound to the declarator. */
5454 declarator = make_array_declarator (declarator, expression);
5455
5456 /* If the next token is not a `[', then there are no more
5457 bounds. */
5458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5459 break;
5460 }
5461
5462 return declarator;
5463 }
5464
5465 /* Parse a new-initializer.
5466
5467 new-initializer:
5468 ( expression-list [opt] )
5469
5470 Returns a representation of the expression-list. If there is no
5471 expression-list, VOID_ZERO_NODE is returned. */
5472
5473 static tree
cp_parser_new_initializer(cp_parser * parser)5474 cp_parser_new_initializer (cp_parser* parser)
5475 {
5476 tree expression_list;
5477
5478 expression_list = (cp_parser_parenthesized_expression_list
5479 (parser, false, /*cast_p=*/false,
5480 /*non_constant_p=*/NULL));
5481 if (!expression_list)
5482 expression_list = void_zero_node;
5483
5484 return expression_list;
5485 }
5486
5487 /* Parse a delete-expression.
5488
5489 delete-expression:
5490 :: [opt] delete cast-expression
5491 :: [opt] delete [ ] cast-expression
5492
5493 Returns a representation of the expression. */
5494
5495 static tree
cp_parser_delete_expression(cp_parser * parser)5496 cp_parser_delete_expression (cp_parser* parser)
5497 {
5498 bool global_scope_p;
5499 bool array_p;
5500 tree expression;
5501
5502 /* Look for the optional `::' operator. */
5503 global_scope_p
5504 = (cp_parser_global_scope_opt (parser,
5505 /*current_scope_valid_p=*/false)
5506 != NULL_TREE);
5507 /* Look for the `delete' keyword. */
5508 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5509 /* See if the array syntax is in use. */
5510 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5511 {
5512 /* Consume the `[' token. */
5513 cp_lexer_consume_token (parser->lexer);
5514 /* Look for the `]' token. */
5515 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5516 /* Remember that this is the `[]' construct. */
5517 array_p = true;
5518 }
5519 else
5520 array_p = false;
5521
5522 /* Parse the cast-expression. */
5523 expression = cp_parser_simple_cast_expression (parser);
5524
5525 /* A delete-expression may not appear in an integral constant
5526 expression. */
5527 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5528 return error_mark_node;
5529
5530 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5531 }
5532
5533 /* Parse a cast-expression.
5534
5535 cast-expression:
5536 unary-expression
5537 ( type-id ) cast-expression
5538
5539 ADDRESS_P is true iff the unary-expression is appearing as the
5540 operand of the `&' operator. CAST_P is true if this expression is
5541 the target of a cast.
5542
5543 Returns a representation of the expression. */
5544
5545 static tree
cp_parser_cast_expression(cp_parser * parser,bool address_p,bool cast_p)5546 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5547 {
5548 /* If it's a `(', then we might be looking at a cast. */
5549 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5550 {
5551 tree type = NULL_TREE;
5552 tree expr = NULL_TREE;
5553 bool compound_literal_p;
5554 const char *saved_message;
5555
5556 /* There's no way to know yet whether or not this is a cast.
5557 For example, `(int (3))' is a unary-expression, while `(int)
5558 3' is a cast. So, we resort to parsing tentatively. */
5559 cp_parser_parse_tentatively (parser);
5560 /* Types may not be defined in a cast. */
5561 saved_message = parser->type_definition_forbidden_message;
5562 parser->type_definition_forbidden_message
5563 = "types may not be defined in casts";
5564 /* Consume the `('. */
5565 cp_lexer_consume_token (parser->lexer);
5566 /* A very tricky bit is that `(struct S) { 3 }' is a
5567 compound-literal (which we permit in C++ as an extension).
5568 But, that construct is not a cast-expression -- it is a
5569 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5570 is legal; if the compound-literal were a cast-expression,
5571 you'd need an extra set of parentheses.) But, if we parse
5572 the type-id, and it happens to be a class-specifier, then we
5573 will commit to the parse at that point, because we cannot
5574 undo the action that is done when creating a new class. So,
5575 then we cannot back up and do a postfix-expression.
5576
5577 Therefore, we scan ahead to the closing `)', and check to see
5578 if the token after the `)' is a `{'. If so, we are not
5579 looking at a cast-expression.
5580
5581 Save tokens so that we can put them back. */
5582 cp_lexer_save_tokens (parser->lexer);
5583 /* Skip tokens until the next token is a closing parenthesis.
5584 If we find the closing `)', and the next token is a `{', then
5585 we are looking at a compound-literal. */
5586 compound_literal_p
5587 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5588 /*consume_paren=*/true)
5589 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5590 /* Roll back the tokens we skipped. */
5591 cp_lexer_rollback_tokens (parser->lexer);
5592 /* If we were looking at a compound-literal, simulate an error
5593 so that the call to cp_parser_parse_definitely below will
5594 fail. */
5595 if (compound_literal_p)
5596 cp_parser_simulate_error (parser);
5597 else
5598 {
5599 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5600 parser->in_type_id_in_expr_p = true;
5601 /* Look for the type-id. */
5602 type = cp_parser_type_id (parser);
5603 /* Look for the closing `)'. */
5604 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5605 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5606 }
5607
5608 /* Restore the saved message. */
5609 parser->type_definition_forbidden_message = saved_message;
5610
5611 /* If ok so far, parse the dependent expression. We cannot be
5612 sure it is a cast. Consider `(T ())'. It is a parenthesized
5613 ctor of T, but looks like a cast to function returning T
5614 without a dependent expression. */
5615 if (!cp_parser_error_occurred (parser))
5616 expr = cp_parser_cast_expression (parser,
5617 /*address_p=*/false,
5618 /*cast_p=*/true);
5619
5620 if (cp_parser_parse_definitely (parser))
5621 {
5622 /* Warn about old-style casts, if so requested. */
5623 if (warn_old_style_cast
5624 && !in_system_header
5625 && !VOID_TYPE_P (type)
5626 && current_lang_name != lang_name_c)
5627 warning (OPT_Wold_style_cast, "use of old-style cast");
5628
5629 /* Only type conversions to integral or enumeration types
5630 can be used in constant-expressions. */
5631 if (!cast_valid_in_integral_constant_expression_p (type)
5632 && (cp_parser_non_integral_constant_expression
5633 (parser,
5634 "a cast to a type other than an integral or "
5635 "enumeration type")))
5636 return error_mark_node;
5637
5638 /* Perform the cast. */
5639 expr = build_c_cast (type, expr);
5640 return expr;
5641 }
5642 }
5643
5644 /* If we get here, then it's not a cast, so it must be a
5645 unary-expression. */
5646 return cp_parser_unary_expression (parser, address_p, cast_p);
5647 }
5648
5649 /* Parse a binary expression of the general form:
5650
5651 pm-expression:
5652 cast-expression
5653 pm-expression .* cast-expression
5654 pm-expression ->* cast-expression
5655
5656 multiplicative-expression:
5657 pm-expression
5658 multiplicative-expression * pm-expression
5659 multiplicative-expression / pm-expression
5660 multiplicative-expression % pm-expression
5661
5662 additive-expression:
5663 multiplicative-expression
5664 additive-expression + multiplicative-expression
5665 additive-expression - multiplicative-expression
5666
5667 shift-expression:
5668 additive-expression
5669 shift-expression << additive-expression
5670 shift-expression >> additive-expression
5671
5672 relational-expression:
5673 shift-expression
5674 relational-expression < shift-expression
5675 relational-expression > shift-expression
5676 relational-expression <= shift-expression
5677 relational-expression >= shift-expression
5678
5679 GNU Extension:
5680
5681 relational-expression:
5682 relational-expression <? shift-expression
5683 relational-expression >? shift-expression
5684
5685 equality-expression:
5686 relational-expression
5687 equality-expression == relational-expression
5688 equality-expression != relational-expression
5689
5690 and-expression:
5691 equality-expression
5692 and-expression & equality-expression
5693
5694 exclusive-or-expression:
5695 and-expression
5696 exclusive-or-expression ^ and-expression
5697
5698 inclusive-or-expression:
5699 exclusive-or-expression
5700 inclusive-or-expression | exclusive-or-expression
5701
5702 logical-and-expression:
5703 inclusive-or-expression
5704 logical-and-expression && inclusive-or-expression
5705
5706 logical-or-expression:
5707 logical-and-expression
5708 logical-or-expression || logical-and-expression
5709
5710 All these are implemented with a single function like:
5711
5712 binary-expression:
5713 simple-cast-expression
5714 binary-expression <token> binary-expression
5715
5716 CAST_P is true if this expression is the target of a cast.
5717
5718 The binops_by_token map is used to get the tree codes for each <token> type.
5719 binary-expressions are associated according to a precedence table. */
5720
5721 #define TOKEN_PRECEDENCE(token) \
5722 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5723 ? PREC_NOT_OPERATOR \
5724 : binops_by_token[token->type].prec)
5725
5726 static tree
cp_parser_binary_expression(cp_parser * parser,bool cast_p)5727 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5728 {
5729 cp_parser_expression_stack stack;
5730 cp_parser_expression_stack_entry *sp = &stack[0];
5731 tree lhs, rhs;
5732 cp_token *token;
5733 enum tree_code tree_type;
5734 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5735 bool overloaded_p;
5736
5737 /* Parse the first expression. */
5738 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5739
5740 for (;;)
5741 {
5742 /* Get an operator token. */
5743 token = cp_lexer_peek_token (parser->lexer);
5744
5745 new_prec = TOKEN_PRECEDENCE (token);
5746
5747 /* Popping an entry off the stack means we completed a subexpression:
5748 - either we found a token which is not an operator (`>' where it is not
5749 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5750 will happen repeatedly;
5751 - or, we found an operator which has lower priority. This is the case
5752 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5753 parsing `3 * 4'. */
5754 if (new_prec <= prec)
5755 {
5756 if (sp == stack)
5757 break;
5758 else
5759 goto pop;
5760 }
5761
5762 get_rhs:
5763 tree_type = binops_by_token[token->type].tree_type;
5764
5765 /* We used the operator token. */
5766 cp_lexer_consume_token (parser->lexer);
5767
5768 /* Extract another operand. It may be the RHS of this expression
5769 or the LHS of a new, higher priority expression. */
5770 rhs = cp_parser_simple_cast_expression (parser);
5771
5772 /* Get another operator token. Look up its precedence to avoid
5773 building a useless (immediately popped) stack entry for common
5774 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5775 token = cp_lexer_peek_token (parser->lexer);
5776 lookahead_prec = TOKEN_PRECEDENCE (token);
5777 if (lookahead_prec > new_prec)
5778 {
5779 /* ... and prepare to parse the RHS of the new, higher priority
5780 expression. Since precedence levels on the stack are
5781 monotonically increasing, we do not have to care about
5782 stack overflows. */
5783 sp->prec = prec;
5784 sp->tree_type = tree_type;
5785 sp->lhs = lhs;
5786 sp++;
5787 lhs = rhs;
5788 prec = new_prec;
5789 new_prec = lookahead_prec;
5790 goto get_rhs;
5791
5792 pop:
5793 /* If the stack is not empty, we have parsed into LHS the right side
5794 (`4' in the example above) of an expression we had suspended.
5795 We can use the information on the stack to recover the LHS (`3')
5796 from the stack together with the tree code (`MULT_EXPR'), and
5797 the precedence of the higher level subexpression
5798 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5799 which will be used to actually build the additive expression. */
5800 --sp;
5801 prec = sp->prec;
5802 tree_type = sp->tree_type;
5803 rhs = lhs;
5804 lhs = sp->lhs;
5805 }
5806
5807 overloaded_p = false;
5808 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5809
5810 /* If the binary operator required the use of an overloaded operator,
5811 then this expression cannot be an integral constant-expression.
5812 An overloaded operator can be used even if both operands are
5813 otherwise permissible in an integral constant-expression if at
5814 least one of the operands is of enumeration type. */
5815
5816 if (overloaded_p
5817 && (cp_parser_non_integral_constant_expression
5818 (parser, "calls to overloaded operators")))
5819 return error_mark_node;
5820 }
5821
5822 return lhs;
5823 }
5824
5825
5826 /* Parse the `? expression : assignment-expression' part of a
5827 conditional-expression. The LOGICAL_OR_EXPR is the
5828 logical-or-expression that started the conditional-expression.
5829 Returns a representation of the entire conditional-expression.
5830
5831 This routine is used by cp_parser_assignment_expression.
5832
5833 ? expression : assignment-expression
5834
5835 GNU Extensions:
5836
5837 ? : assignment-expression */
5838
5839 static tree
cp_parser_question_colon_clause(cp_parser * parser,tree logical_or_expr)5840 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5841 {
5842 tree expr;
5843 tree assignment_expr;
5844
5845 /* Consume the `?' token. */
5846 cp_lexer_consume_token (parser->lexer);
5847 if (cp_parser_allow_gnu_extensions_p (parser)
5848 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5849 /* Implicit true clause. */
5850 expr = NULL_TREE;
5851 else
5852 /* Parse the expression. */
5853 expr = cp_parser_expression (parser, /*cast_p=*/false);
5854
5855 /* The next token should be a `:'. */
5856 cp_parser_require (parser, CPP_COLON, "`:'");
5857 /* Parse the assignment-expression. */
5858 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5859
5860 /* Build the conditional-expression. */
5861 return build_x_conditional_expr (logical_or_expr,
5862 expr,
5863 assignment_expr);
5864 }
5865
5866 /* Parse an assignment-expression.
5867
5868 assignment-expression:
5869 conditional-expression
5870 logical-or-expression assignment-operator assignment_expression
5871 throw-expression
5872
5873 CAST_P is true if this expression is the target of a cast.
5874
5875 Returns a representation for the expression. */
5876
5877 static tree
cp_parser_assignment_expression(cp_parser * parser,bool cast_p)5878 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5879 {
5880 tree expr;
5881
5882 /* If the next token is the `throw' keyword, then we're looking at
5883 a throw-expression. */
5884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5885 expr = cp_parser_throw_expression (parser);
5886 /* Otherwise, it must be that we are looking at a
5887 logical-or-expression. */
5888 else
5889 {
5890 /* Parse the binary expressions (logical-or-expression). */
5891 expr = cp_parser_binary_expression (parser, cast_p);
5892 /* If the next token is a `?' then we're actually looking at a
5893 conditional-expression. */
5894 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5895 return cp_parser_question_colon_clause (parser, expr);
5896 else
5897 {
5898 enum tree_code assignment_operator;
5899
5900 /* If it's an assignment-operator, we're using the second
5901 production. */
5902 assignment_operator
5903 = cp_parser_assignment_operator_opt (parser);
5904 if (assignment_operator != ERROR_MARK)
5905 {
5906 tree rhs;
5907
5908 /* Parse the right-hand side of the assignment. */
5909 rhs = cp_parser_assignment_expression (parser, cast_p);
5910 /* An assignment may not appear in a
5911 constant-expression. */
5912 if (cp_parser_non_integral_constant_expression (parser,
5913 "an assignment"))
5914 return error_mark_node;
5915 /* Build the assignment expression. */
5916 expr = build_x_modify_expr (expr,
5917 assignment_operator,
5918 rhs);
5919 }
5920 }
5921 }
5922
5923 return expr;
5924 }
5925
5926 /* Parse an (optional) assignment-operator.
5927
5928 assignment-operator: one of
5929 = *= /= %= += -= >>= <<= &= ^= |=
5930
5931 GNU Extension:
5932
5933 assignment-operator: one of
5934 <?= >?=
5935
5936 If the next token is an assignment operator, the corresponding tree
5937 code is returned, and the token is consumed. For example, for
5938 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5939 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5940 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5941 operator, ERROR_MARK is returned. */
5942
5943 static enum tree_code
cp_parser_assignment_operator_opt(cp_parser * parser)5944 cp_parser_assignment_operator_opt (cp_parser* parser)
5945 {
5946 enum tree_code op;
5947 cp_token *token;
5948
5949 /* Peek at the next toen. */
5950 token = cp_lexer_peek_token (parser->lexer);
5951
5952 switch (token->type)
5953 {
5954 case CPP_EQ:
5955 op = NOP_EXPR;
5956 break;
5957
5958 case CPP_MULT_EQ:
5959 op = MULT_EXPR;
5960 break;
5961
5962 case CPP_DIV_EQ:
5963 op = TRUNC_DIV_EXPR;
5964 break;
5965
5966 case CPP_MOD_EQ:
5967 op = TRUNC_MOD_EXPR;
5968 break;
5969
5970 case CPP_PLUS_EQ:
5971 op = PLUS_EXPR;
5972 break;
5973
5974 case CPP_MINUS_EQ:
5975 op = MINUS_EXPR;
5976 break;
5977
5978 case CPP_RSHIFT_EQ:
5979 op = RSHIFT_EXPR;
5980 break;
5981
5982 case CPP_LSHIFT_EQ:
5983 op = LSHIFT_EXPR;
5984 break;
5985
5986 case CPP_AND_EQ:
5987 op = BIT_AND_EXPR;
5988 break;
5989
5990 case CPP_XOR_EQ:
5991 op = BIT_XOR_EXPR;
5992 break;
5993
5994 case CPP_OR_EQ:
5995 op = BIT_IOR_EXPR;
5996 break;
5997
5998 default:
5999 /* Nothing else is an assignment operator. */
6000 op = ERROR_MARK;
6001 }
6002
6003 /* If it was an assignment operator, consume it. */
6004 if (op != ERROR_MARK)
6005 cp_lexer_consume_token (parser->lexer);
6006
6007 return op;
6008 }
6009
6010 /* Parse an expression.
6011
6012 expression:
6013 assignment-expression
6014 expression , assignment-expression
6015
6016 CAST_P is true if this expression is the target of a cast.
6017
6018 Returns a representation of the expression. */
6019
6020 static tree
cp_parser_expression(cp_parser * parser,bool cast_p)6021 cp_parser_expression (cp_parser* parser, bool cast_p)
6022 {
6023 tree expression = NULL_TREE;
6024
6025 while (true)
6026 {
6027 tree assignment_expression;
6028
6029 /* Parse the next assignment-expression. */
6030 assignment_expression
6031 = cp_parser_assignment_expression (parser, cast_p);
6032 /* If this is the first assignment-expression, we can just
6033 save it away. */
6034 if (!expression)
6035 expression = assignment_expression;
6036 else
6037 expression = build_x_compound_expr (expression,
6038 assignment_expression);
6039 /* If the next token is not a comma, then we are done with the
6040 expression. */
6041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6042 break;
6043 /* Consume the `,'. */
6044 cp_lexer_consume_token (parser->lexer);
6045 /* A comma operator cannot appear in a constant-expression. */
6046 if (cp_parser_non_integral_constant_expression (parser,
6047 "a comma operator"))
6048 expression = error_mark_node;
6049 }
6050
6051 return expression;
6052 }
6053
6054 /* Parse a constant-expression.
6055
6056 constant-expression:
6057 conditional-expression
6058
6059 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6060 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6061 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6062 is false, NON_CONSTANT_P should be NULL. */
6063
6064 static tree
cp_parser_constant_expression(cp_parser * parser,bool allow_non_constant_p,bool * non_constant_p)6065 cp_parser_constant_expression (cp_parser* parser,
6066 bool allow_non_constant_p,
6067 bool *non_constant_p)
6068 {
6069 bool saved_integral_constant_expression_p;
6070 bool saved_allow_non_integral_constant_expression_p;
6071 bool saved_non_integral_constant_expression_p;
6072 tree expression;
6073
6074 /* It might seem that we could simply parse the
6075 conditional-expression, and then check to see if it were
6076 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6077 one that the compiler can figure out is constant, possibly after
6078 doing some simplifications or optimizations. The standard has a
6079 precise definition of constant-expression, and we must honor
6080 that, even though it is somewhat more restrictive.
6081
6082 For example:
6083
6084 int i[(2, 3)];
6085
6086 is not a legal declaration, because `(2, 3)' is not a
6087 constant-expression. The `,' operator is forbidden in a
6088 constant-expression. However, GCC's constant-folding machinery
6089 will fold this operation to an INTEGER_CST for `3'. */
6090
6091 /* Save the old settings. */
6092 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6093 saved_allow_non_integral_constant_expression_p
6094 = parser->allow_non_integral_constant_expression_p;
6095 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6096 /* We are now parsing a constant-expression. */
6097 parser->integral_constant_expression_p = true;
6098 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6099 parser->non_integral_constant_expression_p = false;
6100 /* Although the grammar says "conditional-expression", we parse an
6101 "assignment-expression", which also permits "throw-expression"
6102 and the use of assignment operators. In the case that
6103 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6104 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6105 actually essential that we look for an assignment-expression.
6106 For example, cp_parser_initializer_clauses uses this function to
6107 determine whether a particular assignment-expression is in fact
6108 constant. */
6109 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6110 /* Restore the old settings. */
6111 parser->integral_constant_expression_p
6112 = saved_integral_constant_expression_p;
6113 parser->allow_non_integral_constant_expression_p
6114 = saved_allow_non_integral_constant_expression_p;
6115 if (allow_non_constant_p)
6116 *non_constant_p = parser->non_integral_constant_expression_p;
6117 else if (parser->non_integral_constant_expression_p)
6118 expression = error_mark_node;
6119 parser->non_integral_constant_expression_p
6120 = saved_non_integral_constant_expression_p;
6121
6122 return expression;
6123 }
6124
6125 /* Parse __builtin_offsetof.
6126
6127 offsetof-expression:
6128 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6129
6130 offsetof-member-designator:
6131 id-expression
6132 | offsetof-member-designator "." id-expression
6133 | offsetof-member-designator "[" expression "]" */
6134
6135 static tree
cp_parser_builtin_offsetof(cp_parser * parser)6136 cp_parser_builtin_offsetof (cp_parser *parser)
6137 {
6138 int save_ice_p, save_non_ice_p;
6139 tree type, expr;
6140 cp_id_kind dummy;
6141
6142 /* We're about to accept non-integral-constant things, but will
6143 definitely yield an integral constant expression. Save and
6144 restore these values around our local parsing. */
6145 save_ice_p = parser->integral_constant_expression_p;
6146 save_non_ice_p = parser->non_integral_constant_expression_p;
6147
6148 /* Consume the "__builtin_offsetof" token. */
6149 cp_lexer_consume_token (parser->lexer);
6150 /* Consume the opening `('. */
6151 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6152 /* Parse the type-id. */
6153 type = cp_parser_type_id (parser);
6154 /* Look for the `,'. */
6155 cp_parser_require (parser, CPP_COMMA, "`,'");
6156
6157 /* Build the (type *)null that begins the traditional offsetof macro. */
6158 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6159
6160 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6161 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6162 true, &dummy);
6163 while (true)
6164 {
6165 cp_token *token = cp_lexer_peek_token (parser->lexer);
6166 switch (token->type)
6167 {
6168 case CPP_OPEN_SQUARE:
6169 /* offsetof-member-designator "[" expression "]" */
6170 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6171 break;
6172
6173 case CPP_DOT:
6174 /* offsetof-member-designator "." identifier */
6175 cp_lexer_consume_token (parser->lexer);
6176 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6177 true, &dummy);
6178 break;
6179
6180 case CPP_CLOSE_PAREN:
6181 /* Consume the ")" token. */
6182 cp_lexer_consume_token (parser->lexer);
6183 goto success;
6184
6185 default:
6186 /* Error. We know the following require will fail, but
6187 that gives the proper error message. */
6188 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6189 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6190 expr = error_mark_node;
6191 goto failure;
6192 }
6193 }
6194
6195 success:
6196 /* If we're processing a template, we can't finish the semantics yet.
6197 Otherwise we can fold the entire expression now. */
6198 if (processing_template_decl)
6199 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6200 else
6201 expr = finish_offsetof (expr);
6202
6203 failure:
6204 parser->integral_constant_expression_p = save_ice_p;
6205 parser->non_integral_constant_expression_p = save_non_ice_p;
6206
6207 return expr;
6208 }
6209
6210 /* Statements [gram.stmt.stmt] */
6211
6212 /* Parse a statement.
6213
6214 statement:
6215 labeled-statement
6216 expression-statement
6217 compound-statement
6218 selection-statement
6219 iteration-statement
6220 jump-statement
6221 declaration-statement
6222 try-block
6223
6224 IN_COMPOUND is true when the statement is nested inside a
6225 cp_parser_compound_statement; this matters for certain pragmas. */
6226
6227 static void
cp_parser_statement(cp_parser * parser,tree in_statement_expr,bool in_compound)6228 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6229 bool in_compound)
6230 {
6231 tree statement;
6232 cp_token *token;
6233 location_t statement_location;
6234
6235 restart:
6236 /* There is no statement yet. */
6237 statement = NULL_TREE;
6238 /* Peek at the next token. */
6239 token = cp_lexer_peek_token (parser->lexer);
6240 /* Remember the location of the first token in the statement. */
6241 statement_location = token->location;
6242 /* If this is a keyword, then that will often determine what kind of
6243 statement we have. */
6244 if (token->type == CPP_KEYWORD)
6245 {
6246 enum rid keyword = token->keyword;
6247
6248 switch (keyword)
6249 {
6250 case RID_CASE:
6251 case RID_DEFAULT:
6252 /* Looks like a labeled-statement with a case label.
6253 Parse the label, and then use tail recursion to parse
6254 the statement. */
6255 cp_parser_label_for_labeled_statement (parser);
6256 goto restart;
6257
6258 case RID_IF:
6259 case RID_SWITCH:
6260 statement = cp_parser_selection_statement (parser);
6261 break;
6262
6263 case RID_WHILE:
6264 case RID_DO:
6265 case RID_FOR:
6266 statement = cp_parser_iteration_statement (parser);
6267 break;
6268
6269 case RID_BREAK:
6270 case RID_CONTINUE:
6271 case RID_RETURN:
6272 case RID_GOTO:
6273 statement = cp_parser_jump_statement (parser);
6274 break;
6275
6276 /* Objective-C++ exception-handling constructs. */
6277 case RID_AT_TRY:
6278 case RID_AT_CATCH:
6279 case RID_AT_FINALLY:
6280 case RID_AT_SYNCHRONIZED:
6281 case RID_AT_THROW:
6282 statement = cp_parser_objc_statement (parser);
6283 break;
6284
6285 case RID_TRY:
6286 statement = cp_parser_try_block (parser);
6287 break;
6288
6289 default:
6290 /* It might be a keyword like `int' that can start a
6291 declaration-statement. */
6292 break;
6293 }
6294 }
6295 else if (token->type == CPP_NAME)
6296 {
6297 /* If the next token is a `:', then we are looking at a
6298 labeled-statement. */
6299 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6300 if (token->type == CPP_COLON)
6301 {
6302 /* Looks like a labeled-statement with an ordinary label.
6303 Parse the label, and then use tail recursion to parse
6304 the statement. */
6305 cp_parser_label_for_labeled_statement (parser);
6306 goto restart;
6307 }
6308 }
6309 /* Anything that starts with a `{' must be a compound-statement. */
6310 else if (token->type == CPP_OPEN_BRACE)
6311 statement = cp_parser_compound_statement (parser, NULL, false);
6312 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6313 a statement all its own. */
6314 else if (token->type == CPP_PRAGMA)
6315 {
6316 /* Only certain OpenMP pragmas are attached to statements, and thus
6317 are considered statements themselves. All others are not. In
6318 the context of a compound, accept the pragma as a "statement" and
6319 return so that we can check for a close brace. Otherwise we
6320 require a real statement and must go back and read one. */
6321 if (in_compound)
6322 cp_parser_pragma (parser, pragma_compound);
6323 else if (!cp_parser_pragma (parser, pragma_stmt))
6324 goto restart;
6325 return;
6326 }
6327 else if (token->type == CPP_EOF)
6328 {
6329 cp_parser_error (parser, "expected statement");
6330 return;
6331 }
6332
6333 /* Everything else must be a declaration-statement or an
6334 expression-statement. Try for the declaration-statement
6335 first, unless we are looking at a `;', in which case we know that
6336 we have an expression-statement. */
6337 if (!statement)
6338 {
6339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6340 {
6341 cp_parser_parse_tentatively (parser);
6342 /* Try to parse the declaration-statement. */
6343 cp_parser_declaration_statement (parser);
6344 /* If that worked, we're done. */
6345 if (cp_parser_parse_definitely (parser))
6346 return;
6347 }
6348 /* Look for an expression-statement instead. */
6349 statement = cp_parser_expression_statement (parser, in_statement_expr);
6350 }
6351
6352 /* Set the line number for the statement. */
6353 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6354 SET_EXPR_LOCATION (statement, statement_location);
6355 }
6356
6357 /* Parse the label for a labeled-statement, i.e.
6358
6359 identifier :
6360 case constant-expression :
6361 default :
6362
6363 GNU Extension:
6364 case constant-expression ... constant-expression : statement
6365
6366 When a label is parsed without errors, the label is added to the
6367 parse tree by the finish_* functions, so this function doesn't
6368 have to return the label. */
6369
6370 static void
cp_parser_label_for_labeled_statement(cp_parser * parser)6371 cp_parser_label_for_labeled_statement (cp_parser* parser)
6372 {
6373 cp_token *token;
6374
6375 /* The next token should be an identifier. */
6376 token = cp_lexer_peek_token (parser->lexer);
6377 if (token->type != CPP_NAME
6378 && token->type != CPP_KEYWORD)
6379 {
6380 cp_parser_error (parser, "expected labeled-statement");
6381 return;
6382 }
6383
6384 switch (token->keyword)
6385 {
6386 case RID_CASE:
6387 {
6388 tree expr, expr_hi;
6389 cp_token *ellipsis;
6390
6391 /* Consume the `case' token. */
6392 cp_lexer_consume_token (parser->lexer);
6393 /* Parse the constant-expression. */
6394 expr = cp_parser_constant_expression (parser,
6395 /*allow_non_constant_p=*/false,
6396 NULL);
6397
6398 ellipsis = cp_lexer_peek_token (parser->lexer);
6399 if (ellipsis->type == CPP_ELLIPSIS)
6400 {
6401 /* Consume the `...' token. */
6402 cp_lexer_consume_token (parser->lexer);
6403 expr_hi =
6404 cp_parser_constant_expression (parser,
6405 /*allow_non_constant_p=*/false,
6406 NULL);
6407 /* We don't need to emit warnings here, as the common code
6408 will do this for us. */
6409 }
6410 else
6411 expr_hi = NULL_TREE;
6412
6413 if (parser->in_switch_statement_p)
6414 finish_case_label (expr, expr_hi);
6415 else
6416 error ("case label %qE not within a switch statement", expr);
6417 }
6418 break;
6419
6420 case RID_DEFAULT:
6421 /* Consume the `default' token. */
6422 cp_lexer_consume_token (parser->lexer);
6423
6424 if (parser->in_switch_statement_p)
6425 finish_case_label (NULL_TREE, NULL_TREE);
6426 else
6427 error ("case label not within a switch statement");
6428 break;
6429
6430 default:
6431 /* Anything else must be an ordinary label. */
6432 finish_label_stmt (cp_parser_identifier (parser));
6433 break;
6434 }
6435
6436 /* Require the `:' token. */
6437 cp_parser_require (parser, CPP_COLON, "`:'");
6438 }
6439
6440 /* Parse an expression-statement.
6441
6442 expression-statement:
6443 expression [opt] ;
6444
6445 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6446 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6447 indicates whether this expression-statement is part of an
6448 expression statement. */
6449
6450 static tree
cp_parser_expression_statement(cp_parser * parser,tree in_statement_expr)6451 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6452 {
6453 tree statement = NULL_TREE;
6454
6455 /* If the next token is a ';', then there is no expression
6456 statement. */
6457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6458 statement = cp_parser_expression (parser, /*cast_p=*/false);
6459
6460 /* Consume the final `;'. */
6461 cp_parser_consume_semicolon_at_end_of_statement (parser);
6462
6463 if (in_statement_expr
6464 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6465 /* This is the final expression statement of a statement
6466 expression. */
6467 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6468 else if (statement)
6469 statement = finish_expr_stmt (statement);
6470 else
6471 finish_stmt ();
6472
6473 return statement;
6474 }
6475
6476 /* Parse a compound-statement.
6477
6478 compound-statement:
6479 { statement-seq [opt] }
6480
6481 Returns a tree representing the statement. */
6482
6483 static tree
cp_parser_compound_statement(cp_parser * parser,tree in_statement_expr,bool in_try)6484 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6485 bool in_try)
6486 {
6487 tree compound_stmt;
6488
6489 /* Consume the `{'. */
6490 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6491 return error_mark_node;
6492 /* Begin the compound-statement. */
6493 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6494 /* Parse an (optional) statement-seq. */
6495 cp_parser_statement_seq_opt (parser, in_statement_expr);
6496 /* Finish the compound-statement. */
6497 finish_compound_stmt (compound_stmt);
6498 /* Consume the `}'. */
6499 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6500
6501 return compound_stmt;
6502 }
6503
6504 /* Parse an (optional) statement-seq.
6505
6506 statement-seq:
6507 statement
6508 statement-seq [opt] statement */
6509
6510 static void
cp_parser_statement_seq_opt(cp_parser * parser,tree in_statement_expr)6511 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6512 {
6513 /* Scan statements until there aren't any more. */
6514 while (true)
6515 {
6516 cp_token *token = cp_lexer_peek_token (parser->lexer);
6517
6518 /* If we're looking at a `}', then we've run out of statements. */
6519 if (token->type == CPP_CLOSE_BRACE
6520 || token->type == CPP_EOF
6521 || token->type == CPP_PRAGMA_EOL)
6522 break;
6523
6524 /* Parse the statement. */
6525 cp_parser_statement (parser, in_statement_expr, true);
6526 }
6527 }
6528
6529 /* Parse a selection-statement.
6530
6531 selection-statement:
6532 if ( condition ) statement
6533 if ( condition ) statement else statement
6534 switch ( condition ) statement
6535
6536 Returns the new IF_STMT or SWITCH_STMT. */
6537
6538 static tree
cp_parser_selection_statement(cp_parser * parser)6539 cp_parser_selection_statement (cp_parser* parser)
6540 {
6541 cp_token *token;
6542 enum rid keyword;
6543
6544 /* Peek at the next token. */
6545 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6546
6547 /* See what kind of keyword it is. */
6548 keyword = token->keyword;
6549 switch (keyword)
6550 {
6551 case RID_IF:
6552 case RID_SWITCH:
6553 {
6554 tree statement;
6555 tree condition;
6556
6557 /* Look for the `('. */
6558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6559 {
6560 cp_parser_skip_to_end_of_statement (parser);
6561 return error_mark_node;
6562 }
6563
6564 /* Begin the selection-statement. */
6565 if (keyword == RID_IF)
6566 statement = begin_if_stmt ();
6567 else
6568 statement = begin_switch_stmt ();
6569
6570 /* Parse the condition. */
6571 condition = cp_parser_condition (parser);
6572 /* Look for the `)'. */
6573 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6574 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6575 /*consume_paren=*/true);
6576
6577 if (keyword == RID_IF)
6578 {
6579 /* Add the condition. */
6580 finish_if_stmt_cond (condition, statement);
6581
6582 /* Parse the then-clause. */
6583 cp_parser_implicitly_scoped_statement (parser);
6584 finish_then_clause (statement);
6585
6586 /* If the next token is `else', parse the else-clause. */
6587 if (cp_lexer_next_token_is_keyword (parser->lexer,
6588 RID_ELSE))
6589 {
6590 /* Consume the `else' keyword. */
6591 cp_lexer_consume_token (parser->lexer);
6592 begin_else_clause (statement);
6593 /* Parse the else-clause. */
6594 cp_parser_implicitly_scoped_statement (parser);
6595 finish_else_clause (statement);
6596 }
6597
6598 /* Now we're all done with the if-statement. */
6599 finish_if_stmt (statement);
6600 }
6601 else
6602 {
6603 bool in_switch_statement_p;
6604 unsigned char in_statement;
6605
6606 /* Add the condition. */
6607 finish_switch_cond (condition, statement);
6608
6609 /* Parse the body of the switch-statement. */
6610 in_switch_statement_p = parser->in_switch_statement_p;
6611 in_statement = parser->in_statement;
6612 parser->in_switch_statement_p = true;
6613 parser->in_statement |= IN_SWITCH_STMT;
6614 cp_parser_implicitly_scoped_statement (parser);
6615 parser->in_switch_statement_p = in_switch_statement_p;
6616 parser->in_statement = in_statement;
6617
6618 /* Now we're all done with the switch-statement. */
6619 finish_switch_stmt (statement);
6620 }
6621
6622 return statement;
6623 }
6624 break;
6625
6626 default:
6627 cp_parser_error (parser, "expected selection-statement");
6628 return error_mark_node;
6629 }
6630 }
6631
6632 /* Parse a condition.
6633
6634 condition:
6635 expression
6636 type-specifier-seq declarator = assignment-expression
6637
6638 GNU Extension:
6639
6640 condition:
6641 type-specifier-seq declarator asm-specification [opt]
6642 attributes [opt] = assignment-expression
6643
6644 Returns the expression that should be tested. */
6645
6646 static tree
cp_parser_condition(cp_parser * parser)6647 cp_parser_condition (cp_parser* parser)
6648 {
6649 cp_decl_specifier_seq type_specifiers;
6650 const char *saved_message;
6651
6652 /* Try the declaration first. */
6653 cp_parser_parse_tentatively (parser);
6654 /* New types are not allowed in the type-specifier-seq for a
6655 condition. */
6656 saved_message = parser->type_definition_forbidden_message;
6657 parser->type_definition_forbidden_message
6658 = "types may not be defined in conditions";
6659 /* Parse the type-specifier-seq. */
6660 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6661 &type_specifiers);
6662 /* Restore the saved message. */
6663 parser->type_definition_forbidden_message = saved_message;
6664 /* If all is well, we might be looking at a declaration. */
6665 if (!cp_parser_error_occurred (parser))
6666 {
6667 tree decl;
6668 tree asm_specification;
6669 tree attributes;
6670 cp_declarator *declarator;
6671 tree initializer = NULL_TREE;
6672
6673 /* Parse the declarator. */
6674 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6675 /*ctor_dtor_or_conv_p=*/NULL,
6676 /*parenthesized_p=*/NULL,
6677 /*member_p=*/false);
6678 /* Parse the attributes. */
6679 attributes = cp_parser_attributes_opt (parser);
6680 /* Parse the asm-specification. */
6681 asm_specification = cp_parser_asm_specification_opt (parser);
6682 /* If the next token is not an `=', then we might still be
6683 looking at an expression. For example:
6684
6685 if (A(a).x)
6686
6687 looks like a decl-specifier-seq and a declarator -- but then
6688 there is no `=', so this is an expression. */
6689 cp_parser_require (parser, CPP_EQ, "`='");
6690 /* If we did see an `=', then we are looking at a declaration
6691 for sure. */
6692 if (cp_parser_parse_definitely (parser))
6693 {
6694 tree pushed_scope;
6695 bool non_constant_p;
6696
6697 /* Create the declaration. */
6698 decl = start_decl (declarator, &type_specifiers,
6699 /*initialized_p=*/true,
6700 attributes, /*prefix_attributes=*/NULL_TREE,
6701 &pushed_scope);
6702 /* Parse the assignment-expression. */
6703 initializer
6704 = cp_parser_constant_expression (parser,
6705 /*allow_non_constant_p=*/true,
6706 &non_constant_p);
6707 if (!non_constant_p)
6708 initializer = fold_non_dependent_expr (initializer);
6709
6710 /* Process the initializer. */
6711 cp_finish_decl (decl,
6712 initializer, !non_constant_p,
6713 asm_specification,
6714 LOOKUP_ONLYCONVERTING);
6715
6716 if (pushed_scope)
6717 pop_scope (pushed_scope);
6718
6719 return convert_from_reference (decl);
6720 }
6721 }
6722 /* If we didn't even get past the declarator successfully, we are
6723 definitely not looking at a declaration. */
6724 else
6725 cp_parser_abort_tentative_parse (parser);
6726
6727 /* Otherwise, we are looking at an expression. */
6728 return cp_parser_expression (parser, /*cast_p=*/false);
6729 }
6730
6731 /* Parse an iteration-statement.
6732
6733 iteration-statement:
6734 while ( condition ) statement
6735 do statement while ( expression ) ;
6736 for ( for-init-statement condition [opt] ; expression [opt] )
6737 statement
6738
6739 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6740
6741 static tree
cp_parser_iteration_statement(cp_parser * parser)6742 cp_parser_iteration_statement (cp_parser* parser)
6743 {
6744 cp_token *token;
6745 enum rid keyword;
6746 tree statement;
6747 unsigned char in_statement;
6748
6749 /* Peek at the next token. */
6750 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6751 if (!token)
6752 return error_mark_node;
6753
6754 /* Remember whether or not we are already within an iteration
6755 statement. */
6756 in_statement = parser->in_statement;
6757
6758 /* See what kind of keyword it is. */
6759 keyword = token->keyword;
6760 switch (keyword)
6761 {
6762 case RID_WHILE:
6763 {
6764 tree condition;
6765
6766 /* Begin the while-statement. */
6767 statement = begin_while_stmt ();
6768 /* Look for the `('. */
6769 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6770 /* Parse the condition. */
6771 condition = cp_parser_condition (parser);
6772 finish_while_stmt_cond (condition, statement);
6773 /* Look for the `)'. */
6774 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6775 /* Parse the dependent statement. */
6776 parser->in_statement = IN_ITERATION_STMT;
6777 cp_parser_already_scoped_statement (parser);
6778 parser->in_statement = in_statement;
6779 /* We're done with the while-statement. */
6780 finish_while_stmt (statement);
6781 }
6782 break;
6783
6784 case RID_DO:
6785 {
6786 tree expression;
6787
6788 /* Begin the do-statement. */
6789 statement = begin_do_stmt ();
6790 /* Parse the body of the do-statement. */
6791 parser->in_statement = IN_ITERATION_STMT;
6792 cp_parser_implicitly_scoped_statement (parser);
6793 parser->in_statement = in_statement;
6794 finish_do_body (statement);
6795 /* Look for the `while' keyword. */
6796 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6797 /* Look for the `('. */
6798 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6799 /* Parse the expression. */
6800 expression = cp_parser_expression (parser, /*cast_p=*/false);
6801 /* We're done with the do-statement. */
6802 finish_do_stmt (expression, statement);
6803 /* Look for the `)'. */
6804 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6805 /* Look for the `;'. */
6806 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6807 }
6808 break;
6809
6810 case RID_FOR:
6811 {
6812 tree condition = NULL_TREE;
6813 tree expression = NULL_TREE;
6814
6815 /* Begin the for-statement. */
6816 statement = begin_for_stmt ();
6817 /* Look for the `('. */
6818 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6819 /* Parse the initialization. */
6820 cp_parser_for_init_statement (parser);
6821 finish_for_init_stmt (statement);
6822
6823 /* If there's a condition, process it. */
6824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6825 condition = cp_parser_condition (parser);
6826 finish_for_cond (condition, statement);
6827 /* Look for the `;'. */
6828 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6829
6830 /* If there's an expression, process it. */
6831 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6832 expression = cp_parser_expression (parser, /*cast_p=*/false);
6833 finish_for_expr (expression, statement);
6834 /* Look for the `)'. */
6835 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6836
6837 /* Parse the body of the for-statement. */
6838 parser->in_statement = IN_ITERATION_STMT;
6839 cp_parser_already_scoped_statement (parser);
6840 parser->in_statement = in_statement;
6841
6842 /* We're done with the for-statement. */
6843 finish_for_stmt (statement);
6844 }
6845 break;
6846
6847 default:
6848 cp_parser_error (parser, "expected iteration-statement");
6849 statement = error_mark_node;
6850 break;
6851 }
6852
6853 return statement;
6854 }
6855
6856 /* Parse a for-init-statement.
6857
6858 for-init-statement:
6859 expression-statement
6860 simple-declaration */
6861
6862 static void
cp_parser_for_init_statement(cp_parser * parser)6863 cp_parser_for_init_statement (cp_parser* parser)
6864 {
6865 /* If the next token is a `;', then we have an empty
6866 expression-statement. Grammatically, this is also a
6867 simple-declaration, but an invalid one, because it does not
6868 declare anything. Therefore, if we did not handle this case
6869 specially, we would issue an error message about an invalid
6870 declaration. */
6871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6872 {
6873 /* We're going to speculatively look for a declaration, falling back
6874 to an expression, if necessary. */
6875 cp_parser_parse_tentatively (parser);
6876 /* Parse the declaration. */
6877 cp_parser_simple_declaration (parser,
6878 /*function_definition_allowed_p=*/false);
6879 /* If the tentative parse failed, then we shall need to look for an
6880 expression-statement. */
6881 if (cp_parser_parse_definitely (parser))
6882 return;
6883 }
6884
6885 cp_parser_expression_statement (parser, false);
6886 }
6887
6888 /* Parse a jump-statement.
6889
6890 jump-statement:
6891 break ;
6892 continue ;
6893 return expression [opt] ;
6894 goto identifier ;
6895
6896 GNU extension:
6897
6898 jump-statement:
6899 goto * expression ;
6900
6901 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6902
6903 static tree
cp_parser_jump_statement(cp_parser * parser)6904 cp_parser_jump_statement (cp_parser* parser)
6905 {
6906 tree statement = error_mark_node;
6907 cp_token *token;
6908 enum rid keyword;
6909
6910 /* Peek at the next token. */
6911 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6912 if (!token)
6913 return error_mark_node;
6914
6915 /* See what kind of keyword it is. */
6916 keyword = token->keyword;
6917 switch (keyword)
6918 {
6919 case RID_BREAK:
6920 switch (parser->in_statement)
6921 {
6922 case 0:
6923 error ("break statement not within loop or switch");
6924 break;
6925 default:
6926 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6927 || parser->in_statement == IN_ITERATION_STMT);
6928 statement = finish_break_stmt ();
6929 break;
6930 case IN_OMP_BLOCK:
6931 error ("invalid exit from OpenMP structured block");
6932 break;
6933 case IN_OMP_FOR:
6934 error ("break statement used with OpenMP for loop");
6935 break;
6936 }
6937 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6938 break;
6939
6940 case RID_CONTINUE:
6941 switch (parser->in_statement & ~IN_SWITCH_STMT)
6942 {
6943 case 0:
6944 error ("continue statement not within a loop");
6945 break;
6946 case IN_ITERATION_STMT:
6947 case IN_OMP_FOR:
6948 statement = finish_continue_stmt ();
6949 break;
6950 case IN_OMP_BLOCK:
6951 error ("invalid exit from OpenMP structured block");
6952 break;
6953 default:
6954 gcc_unreachable ();
6955 }
6956 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6957 break;
6958
6959 case RID_RETURN:
6960 {
6961 tree expr;
6962
6963 /* If the next token is a `;', then there is no
6964 expression. */
6965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6966 expr = cp_parser_expression (parser, /*cast_p=*/false);
6967 else
6968 expr = NULL_TREE;
6969 /* Build the return-statement. */
6970 statement = finish_return_stmt (expr);
6971 /* Look for the final `;'. */
6972 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6973 }
6974 break;
6975
6976 case RID_GOTO:
6977 /* Create the goto-statement. */
6978 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6979 {
6980 /* Issue a warning about this use of a GNU extension. */
6981 if (pedantic)
6982 pedwarn ("ISO C++ forbids computed gotos");
6983 /* Consume the '*' token. */
6984 cp_lexer_consume_token (parser->lexer);
6985 /* Parse the dependent expression. */
6986 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6987 }
6988 else
6989 finish_goto_stmt (cp_parser_identifier (parser));
6990 /* Look for the final `;'. */
6991 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6992 break;
6993
6994 default:
6995 cp_parser_error (parser, "expected jump-statement");
6996 break;
6997 }
6998
6999 return statement;
7000 }
7001
7002 /* Parse a declaration-statement.
7003
7004 declaration-statement:
7005 block-declaration */
7006
7007 static void
cp_parser_declaration_statement(cp_parser * parser)7008 cp_parser_declaration_statement (cp_parser* parser)
7009 {
7010 void *p;
7011
7012 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7013 p = obstack_alloc (&declarator_obstack, 0);
7014
7015 /* Parse the block-declaration. */
7016 cp_parser_block_declaration (parser, /*statement_p=*/true);
7017
7018 /* Free any declarators allocated. */
7019 obstack_free (&declarator_obstack, p);
7020
7021 /* Finish off the statement. */
7022 finish_stmt ();
7023 }
7024
7025 /* Some dependent statements (like `if (cond) statement'), are
7026 implicitly in their own scope. In other words, if the statement is
7027 a single statement (as opposed to a compound-statement), it is
7028 none-the-less treated as if it were enclosed in braces. Any
7029 declarations appearing in the dependent statement are out of scope
7030 after control passes that point. This function parses a statement,
7031 but ensures that is in its own scope, even if it is not a
7032 compound-statement.
7033
7034 Returns the new statement. */
7035
7036 static tree
cp_parser_implicitly_scoped_statement(cp_parser * parser)7037 cp_parser_implicitly_scoped_statement (cp_parser* parser)
7038 {
7039 tree statement;
7040
7041 /* Mark if () ; with a special NOP_EXPR. */
7042 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7043 {
7044 cp_lexer_consume_token (parser->lexer);
7045 statement = add_stmt (build_empty_stmt ());
7046 }
7047 /* if a compound is opened, we simply parse the statement directly. */
7048 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7049 statement = cp_parser_compound_statement (parser, NULL, false);
7050 /* If the token is not a `{', then we must take special action. */
7051 else
7052 {
7053 /* Create a compound-statement. */
7054 statement = begin_compound_stmt (0);
7055 /* Parse the dependent-statement. */
7056 cp_parser_statement (parser, NULL_TREE, false);
7057 /* Finish the dummy compound-statement. */
7058 finish_compound_stmt (statement);
7059 }
7060
7061 /* Return the statement. */
7062 return statement;
7063 }
7064
7065 /* For some dependent statements (like `while (cond) statement'), we
7066 have already created a scope. Therefore, even if the dependent
7067 statement is a compound-statement, we do not want to create another
7068 scope. */
7069
7070 static void
cp_parser_already_scoped_statement(cp_parser * parser)7071 cp_parser_already_scoped_statement (cp_parser* parser)
7072 {
7073 /* If the token is a `{', then we must take special action. */
7074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7075 cp_parser_statement (parser, NULL_TREE, false);
7076 else
7077 {
7078 /* Avoid calling cp_parser_compound_statement, so that we
7079 don't create a new scope. Do everything else by hand. */
7080 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7081 cp_parser_statement_seq_opt (parser, NULL_TREE);
7082 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7083 }
7084 }
7085
7086 /* Declarations [gram.dcl.dcl] */
7087
7088 /* Parse an optional declaration-sequence.
7089
7090 declaration-seq:
7091 declaration
7092 declaration-seq declaration */
7093
7094 static void
cp_parser_declaration_seq_opt(cp_parser * parser)7095 cp_parser_declaration_seq_opt (cp_parser* parser)
7096 {
7097 while (true)
7098 {
7099 cp_token *token;
7100
7101 token = cp_lexer_peek_token (parser->lexer);
7102
7103 if (token->type == CPP_CLOSE_BRACE
7104 || token->type == CPP_EOF
7105 || token->type == CPP_PRAGMA_EOL)
7106 break;
7107
7108 if (token->type == CPP_SEMICOLON)
7109 {
7110 /* A declaration consisting of a single semicolon is
7111 invalid. Allow it unless we're being pedantic. */
7112 cp_lexer_consume_token (parser->lexer);
7113 if (pedantic && !in_system_header)
7114 pedwarn ("extra %<;%>");
7115 continue;
7116 }
7117
7118 /* If we're entering or exiting a region that's implicitly
7119 extern "C", modify the lang context appropriately. */
7120 if (!parser->implicit_extern_c && token->implicit_extern_c)
7121 {
7122 push_lang_context (lang_name_c);
7123 parser->implicit_extern_c = true;
7124 }
7125 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7126 {
7127 pop_lang_context ();
7128 parser->implicit_extern_c = false;
7129 }
7130
7131 if (token->type == CPP_PRAGMA)
7132 {
7133 /* A top-level declaration can consist solely of a #pragma.
7134 A nested declaration cannot, so this is done here and not
7135 in cp_parser_declaration. (A #pragma at block scope is
7136 handled in cp_parser_statement.) */
7137 cp_parser_pragma (parser, pragma_external);
7138 continue;
7139 }
7140
7141 /* Parse the declaration itself. */
7142 cp_parser_declaration (parser);
7143 }
7144 }
7145
7146 /* Parse a declaration.
7147
7148 declaration:
7149 block-declaration
7150 function-definition
7151 template-declaration
7152 explicit-instantiation
7153 explicit-specialization
7154 linkage-specification
7155 namespace-definition
7156
7157 GNU extension:
7158
7159 declaration:
7160 __extension__ declaration */
7161
7162 static void
cp_parser_declaration(cp_parser * parser)7163 cp_parser_declaration (cp_parser* parser)
7164 {
7165 cp_token token1;
7166 cp_token token2;
7167 int saved_pedantic;
7168 void *p;
7169
7170 /* Check for the `__extension__' keyword. */
7171 if (cp_parser_extension_opt (parser, &saved_pedantic))
7172 {
7173 /* Parse the qualified declaration. */
7174 cp_parser_declaration (parser);
7175 /* Restore the PEDANTIC flag. */
7176 pedantic = saved_pedantic;
7177
7178 return;
7179 }
7180
7181 /* Try to figure out what kind of declaration is present. */
7182 token1 = *cp_lexer_peek_token (parser->lexer);
7183
7184 if (token1.type != CPP_EOF)
7185 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7186 else
7187 {
7188 token2.type = CPP_EOF;
7189 token2.keyword = RID_MAX;
7190 }
7191
7192 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7193 p = obstack_alloc (&declarator_obstack, 0);
7194
7195 /* If the next token is `extern' and the following token is a string
7196 literal, then we have a linkage specification. */
7197 if (token1.keyword == RID_EXTERN
7198 && cp_parser_is_string_literal (&token2))
7199 cp_parser_linkage_specification (parser);
7200 /* If the next token is `template', then we have either a template
7201 declaration, an explicit instantiation, or an explicit
7202 specialization. */
7203 else if (token1.keyword == RID_TEMPLATE)
7204 {
7205 /* `template <>' indicates a template specialization. */
7206 if (token2.type == CPP_LESS
7207 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7208 cp_parser_explicit_specialization (parser);
7209 /* `template <' indicates a template declaration. */
7210 else if (token2.type == CPP_LESS)
7211 cp_parser_template_declaration (parser, /*member_p=*/false);
7212 /* Anything else must be an explicit instantiation. */
7213 else
7214 cp_parser_explicit_instantiation (parser);
7215 }
7216 /* If the next token is `export', then we have a template
7217 declaration. */
7218 else if (token1.keyword == RID_EXPORT)
7219 cp_parser_template_declaration (parser, /*member_p=*/false);
7220 /* If the next token is `extern', 'static' or 'inline' and the one
7221 after that is `template', we have a GNU extended explicit
7222 instantiation directive. */
7223 else if (cp_parser_allow_gnu_extensions_p (parser)
7224 && (token1.keyword == RID_EXTERN
7225 || token1.keyword == RID_STATIC
7226 || token1.keyword == RID_INLINE)
7227 && token2.keyword == RID_TEMPLATE)
7228 cp_parser_explicit_instantiation (parser);
7229 /* If the next token is `namespace', check for a named or unnamed
7230 namespace definition. */
7231 else if (token1.keyword == RID_NAMESPACE
7232 && (/* A named namespace definition. */
7233 (token2.type == CPP_NAME
7234 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7235 != CPP_EQ))
7236 /* An unnamed namespace definition. */
7237 || token2.type == CPP_OPEN_BRACE
7238 || token2.keyword == RID_ATTRIBUTE))
7239 cp_parser_namespace_definition (parser);
7240 /* Objective-C++ declaration/definition. */
7241 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7242 cp_parser_objc_declaration (parser);
7243 /* We must have either a block declaration or a function
7244 definition. */
7245 else
7246 /* Try to parse a block-declaration, or a function-definition. */
7247 cp_parser_block_declaration (parser, /*statement_p=*/false);
7248
7249 /* Free any declarators allocated. */
7250 obstack_free (&declarator_obstack, p);
7251 }
7252
7253 /* Parse a block-declaration.
7254
7255 block-declaration:
7256 simple-declaration
7257 asm-definition
7258 namespace-alias-definition
7259 using-declaration
7260 using-directive
7261
7262 GNU Extension:
7263
7264 block-declaration:
7265 __extension__ block-declaration
7266 label-declaration
7267
7268 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7269 part of a declaration-statement. */
7270
7271 static void
cp_parser_block_declaration(cp_parser * parser,bool statement_p)7272 cp_parser_block_declaration (cp_parser *parser,
7273 bool statement_p)
7274 {
7275 cp_token *token1;
7276 int saved_pedantic;
7277
7278 /* Check for the `__extension__' keyword. */
7279 if (cp_parser_extension_opt (parser, &saved_pedantic))
7280 {
7281 /* Parse the qualified declaration. */
7282 cp_parser_block_declaration (parser, statement_p);
7283 /* Restore the PEDANTIC flag. */
7284 pedantic = saved_pedantic;
7285
7286 return;
7287 }
7288
7289 /* Peek at the next token to figure out which kind of declaration is
7290 present. */
7291 token1 = cp_lexer_peek_token (parser->lexer);
7292
7293 /* If the next keyword is `asm', we have an asm-definition. */
7294 if (token1->keyword == RID_ASM)
7295 {
7296 if (statement_p)
7297 cp_parser_commit_to_tentative_parse (parser);
7298 cp_parser_asm_definition (parser);
7299 }
7300 /* If the next keyword is `namespace', we have a
7301 namespace-alias-definition. */
7302 else if (token1->keyword == RID_NAMESPACE)
7303 cp_parser_namespace_alias_definition (parser);
7304 /* If the next keyword is `using', we have either a
7305 using-declaration or a using-directive. */
7306 else if (token1->keyword == RID_USING)
7307 {
7308 cp_token *token2;
7309
7310 if (statement_p)
7311 cp_parser_commit_to_tentative_parse (parser);
7312 /* If the token after `using' is `namespace', then we have a
7313 using-directive. */
7314 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7315 if (token2->keyword == RID_NAMESPACE)
7316 cp_parser_using_directive (parser);
7317 /* Otherwise, it's a using-declaration. */
7318 else
7319 cp_parser_using_declaration (parser,
7320 /*access_declaration_p=*/false);
7321 }
7322 /* If the next keyword is `__label__' we have a label declaration. */
7323 else if (token1->keyword == RID_LABEL)
7324 {
7325 if (statement_p)
7326 cp_parser_commit_to_tentative_parse (parser);
7327 cp_parser_label_declaration (parser);
7328 }
7329 /* Anything else must be a simple-declaration. */
7330 else
7331 cp_parser_simple_declaration (parser, !statement_p);
7332 }
7333
7334 /* Parse a simple-declaration.
7335
7336 simple-declaration:
7337 decl-specifier-seq [opt] init-declarator-list [opt] ;
7338
7339 init-declarator-list:
7340 init-declarator
7341 init-declarator-list , init-declarator
7342
7343 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7344 function-definition as a simple-declaration. */
7345
7346 static void
cp_parser_simple_declaration(cp_parser * parser,bool function_definition_allowed_p)7347 cp_parser_simple_declaration (cp_parser* parser,
7348 bool function_definition_allowed_p)
7349 {
7350 cp_decl_specifier_seq decl_specifiers;
7351 int declares_class_or_enum;
7352 bool saw_declarator;
7353
7354 /* Defer access checks until we know what is being declared; the
7355 checks for names appearing in the decl-specifier-seq should be
7356 done as if we were in the scope of the thing being declared. */
7357 push_deferring_access_checks (dk_deferred);
7358
7359 /* Parse the decl-specifier-seq. We have to keep track of whether
7360 or not the decl-specifier-seq declares a named class or
7361 enumeration type, since that is the only case in which the
7362 init-declarator-list is allowed to be empty.
7363
7364 [dcl.dcl]
7365
7366 In a simple-declaration, the optional init-declarator-list can be
7367 omitted only when declaring a class or enumeration, that is when
7368 the decl-specifier-seq contains either a class-specifier, an
7369 elaborated-type-specifier, or an enum-specifier. */
7370 cp_parser_decl_specifier_seq (parser,
7371 CP_PARSER_FLAGS_OPTIONAL,
7372 &decl_specifiers,
7373 &declares_class_or_enum);
7374 /* We no longer need to defer access checks. */
7375 stop_deferring_access_checks ();
7376
7377 /* In a block scope, a valid declaration must always have a
7378 decl-specifier-seq. By not trying to parse declarators, we can
7379 resolve the declaration/expression ambiguity more quickly. */
7380 if (!function_definition_allowed_p
7381 && !decl_specifiers.any_specifiers_p)
7382 {
7383 cp_parser_error (parser, "expected declaration");
7384 goto done;
7385 }
7386
7387 /* If the next two tokens are both identifiers, the code is
7388 erroneous. The usual cause of this situation is code like:
7389
7390 T t;
7391
7392 where "T" should name a type -- but does not. */
7393 if (!decl_specifiers.type
7394 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7395 {
7396 /* If parsing tentatively, we should commit; we really are
7397 looking at a declaration. */
7398 cp_parser_commit_to_tentative_parse (parser);
7399 /* Give up. */
7400 goto done;
7401 }
7402
7403 /* If we have seen at least one decl-specifier, and the next token
7404 is not a parenthesis, then we must be looking at a declaration.
7405 (After "int (" we might be looking at a functional cast.) */
7406 if (decl_specifiers.any_specifiers_p
7407 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7408 cp_parser_commit_to_tentative_parse (parser);
7409
7410 /* Keep going until we hit the `;' at the end of the simple
7411 declaration. */
7412 saw_declarator = false;
7413 while (cp_lexer_next_token_is_not (parser->lexer,
7414 CPP_SEMICOLON))
7415 {
7416 cp_token *token;
7417 bool function_definition_p;
7418 tree decl;
7419
7420 if (saw_declarator)
7421 {
7422 /* If we are processing next declarator, coma is expected */
7423 token = cp_lexer_peek_token (parser->lexer);
7424 gcc_assert (token->type == CPP_COMMA);
7425 cp_lexer_consume_token (parser->lexer);
7426 }
7427 else
7428 saw_declarator = true;
7429
7430 /* Parse the init-declarator. */
7431 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7432 /*checks=*/NULL,
7433 function_definition_allowed_p,
7434 /*member_p=*/false,
7435 declares_class_or_enum,
7436 &function_definition_p);
7437 /* If an error occurred while parsing tentatively, exit quickly.
7438 (That usually happens when in the body of a function; each
7439 statement is treated as a declaration-statement until proven
7440 otherwise.) */
7441 if (cp_parser_error_occurred (parser))
7442 goto done;
7443 /* Handle function definitions specially. */
7444 if (function_definition_p)
7445 {
7446 /* If the next token is a `,', then we are probably
7447 processing something like:
7448
7449 void f() {}, *p;
7450
7451 which is erroneous. */
7452 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7453 error ("mixing declarations and function-definitions is forbidden");
7454 /* Otherwise, we're done with the list of declarators. */
7455 else
7456 {
7457 pop_deferring_access_checks ();
7458 return;
7459 }
7460 }
7461 /* The next token should be either a `,' or a `;'. */
7462 token = cp_lexer_peek_token (parser->lexer);
7463 /* If it's a `,', there are more declarators to come. */
7464 if (token->type == CPP_COMMA)
7465 /* will be consumed next time around */;
7466 /* If it's a `;', we are done. */
7467 else if (token->type == CPP_SEMICOLON)
7468 break;
7469 /* Anything else is an error. */
7470 else
7471 {
7472 /* If we have already issued an error message we don't need
7473 to issue another one. */
7474 if (decl != error_mark_node
7475 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7476 cp_parser_error (parser, "expected %<,%> or %<;%>");
7477 /* Skip tokens until we reach the end of the statement. */
7478 cp_parser_skip_to_end_of_statement (parser);
7479 /* If the next token is now a `;', consume it. */
7480 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7481 cp_lexer_consume_token (parser->lexer);
7482 goto done;
7483 }
7484 /* After the first time around, a function-definition is not
7485 allowed -- even if it was OK at first. For example:
7486
7487 int i, f() {}
7488
7489 is not valid. */
7490 function_definition_allowed_p = false;
7491 }
7492
7493 /* Issue an error message if no declarators are present, and the
7494 decl-specifier-seq does not itself declare a class or
7495 enumeration. */
7496 if (!saw_declarator)
7497 {
7498 if (cp_parser_declares_only_class_p (parser))
7499 shadow_tag (&decl_specifiers);
7500 /* Perform any deferred access checks. */
7501 perform_deferred_access_checks ();
7502 }
7503
7504 /* Consume the `;'. */
7505 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7506
7507 done:
7508 pop_deferring_access_checks ();
7509 }
7510
7511 /* Parse a decl-specifier-seq.
7512
7513 decl-specifier-seq:
7514 decl-specifier-seq [opt] decl-specifier
7515
7516 decl-specifier:
7517 storage-class-specifier
7518 type-specifier
7519 function-specifier
7520 friend
7521 typedef
7522
7523 GNU Extension:
7524
7525 decl-specifier:
7526 attributes
7527
7528 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7529
7530 The parser flags FLAGS is used to control type-specifier parsing.
7531
7532 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7533 flags:
7534
7535 1: one of the decl-specifiers is an elaborated-type-specifier
7536 (i.e., a type declaration)
7537 2: one of the decl-specifiers is an enum-specifier or a
7538 class-specifier (i.e., a type definition)
7539
7540 */
7541
7542 static void
cp_parser_decl_specifier_seq(cp_parser * parser,cp_parser_flags flags,cp_decl_specifier_seq * decl_specs,int * declares_class_or_enum)7543 cp_parser_decl_specifier_seq (cp_parser* parser,
7544 cp_parser_flags flags,
7545 cp_decl_specifier_seq *decl_specs,
7546 int* declares_class_or_enum)
7547 {
7548 bool constructor_possible_p = !parser->in_declarator_p;
7549
7550 /* Clear DECL_SPECS. */
7551 clear_decl_specs (decl_specs);
7552
7553 /* Assume no class or enumeration type is declared. */
7554 *declares_class_or_enum = 0;
7555
7556 /* Keep reading specifiers until there are no more to read. */
7557 while (true)
7558 {
7559 bool constructor_p;
7560 bool found_decl_spec;
7561 cp_token *token;
7562
7563 /* Peek at the next token. */
7564 token = cp_lexer_peek_token (parser->lexer);
7565 /* Handle attributes. */
7566 if (token->keyword == RID_ATTRIBUTE)
7567 {
7568 /* Parse the attributes. */
7569 decl_specs->attributes
7570 = chainon (decl_specs->attributes,
7571 cp_parser_attributes_opt (parser));
7572 continue;
7573 }
7574 /* Assume we will find a decl-specifier keyword. */
7575 found_decl_spec = true;
7576 /* If the next token is an appropriate keyword, we can simply
7577 add it to the list. */
7578 switch (token->keyword)
7579 {
7580 /* decl-specifier:
7581 friend */
7582 case RID_FRIEND:
7583 if (!at_class_scope_p ())
7584 {
7585 error ("%<friend%> used outside of class");
7586 cp_lexer_purge_token (parser->lexer);
7587 }
7588 else
7589 {
7590 ++decl_specs->specs[(int) ds_friend];
7591 /* Consume the token. */
7592 cp_lexer_consume_token (parser->lexer);
7593 }
7594 break;
7595
7596 /* function-specifier:
7597 inline
7598 virtual
7599 explicit */
7600 case RID_INLINE:
7601 case RID_VIRTUAL:
7602 case RID_EXPLICIT:
7603 cp_parser_function_specifier_opt (parser, decl_specs);
7604 break;
7605
7606 /* decl-specifier:
7607 typedef */
7608 case RID_TYPEDEF:
7609 ++decl_specs->specs[(int) ds_typedef];
7610 /* Consume the token. */
7611 cp_lexer_consume_token (parser->lexer);
7612 /* A constructor declarator cannot appear in a typedef. */
7613 constructor_possible_p = false;
7614 /* The "typedef" keyword can only occur in a declaration; we
7615 may as well commit at this point. */
7616 cp_parser_commit_to_tentative_parse (parser);
7617
7618 if (decl_specs->storage_class != sc_none)
7619 decl_specs->conflicting_specifiers_p = true;
7620 break;
7621
7622 /* storage-class-specifier:
7623 auto
7624 register
7625 static
7626 extern
7627 mutable
7628
7629 GNU Extension:
7630 thread */
7631 case RID_AUTO:
7632 case RID_REGISTER:
7633 case RID_STATIC:
7634 case RID_EXTERN:
7635 case RID_MUTABLE:
7636 /* Consume the token. */
7637 cp_lexer_consume_token (parser->lexer);
7638 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7639 break;
7640 case RID_THREAD:
7641 /* Consume the token. */
7642 cp_lexer_consume_token (parser->lexer);
7643 ++decl_specs->specs[(int) ds_thread];
7644 break;
7645
7646 default:
7647 /* We did not yet find a decl-specifier yet. */
7648 found_decl_spec = false;
7649 break;
7650 }
7651
7652 /* Constructors are a special case. The `S' in `S()' is not a
7653 decl-specifier; it is the beginning of the declarator. */
7654 constructor_p
7655 = (!found_decl_spec
7656 && constructor_possible_p
7657 && (cp_parser_constructor_declarator_p
7658 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7659
7660 /* If we don't have a DECL_SPEC yet, then we must be looking at
7661 a type-specifier. */
7662 if (!found_decl_spec && !constructor_p)
7663 {
7664 int decl_spec_declares_class_or_enum;
7665 bool is_cv_qualifier;
7666 tree type_spec;
7667
7668 type_spec
7669 = cp_parser_type_specifier (parser, flags,
7670 decl_specs,
7671 /*is_declaration=*/true,
7672 &decl_spec_declares_class_or_enum,
7673 &is_cv_qualifier);
7674
7675 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7676
7677 /* If this type-specifier referenced a user-defined type
7678 (a typedef, class-name, etc.), then we can't allow any
7679 more such type-specifiers henceforth.
7680
7681 [dcl.spec]
7682
7683 The longest sequence of decl-specifiers that could
7684 possibly be a type name is taken as the
7685 decl-specifier-seq of a declaration. The sequence shall
7686 be self-consistent as described below.
7687
7688 [dcl.type]
7689
7690 As a general rule, at most one type-specifier is allowed
7691 in the complete decl-specifier-seq of a declaration. The
7692 only exceptions are the following:
7693
7694 -- const or volatile can be combined with any other
7695 type-specifier.
7696
7697 -- signed or unsigned can be combined with char, long,
7698 short, or int.
7699
7700 -- ..
7701
7702 Example:
7703
7704 typedef char* Pc;
7705 void g (const int Pc);
7706
7707 Here, Pc is *not* part of the decl-specifier seq; it's
7708 the declarator. Therefore, once we see a type-specifier
7709 (other than a cv-qualifier), we forbid any additional
7710 user-defined types. We *do* still allow things like `int
7711 int' to be considered a decl-specifier-seq, and issue the
7712 error message later. */
7713 if (type_spec && !is_cv_qualifier)
7714 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7715 /* A constructor declarator cannot follow a type-specifier. */
7716 if (type_spec)
7717 {
7718 constructor_possible_p = false;
7719 found_decl_spec = true;
7720 }
7721 }
7722
7723 /* If we still do not have a DECL_SPEC, then there are no more
7724 decl-specifiers. */
7725 if (!found_decl_spec)
7726 break;
7727
7728 decl_specs->any_specifiers_p = true;
7729 /* After we see one decl-specifier, further decl-specifiers are
7730 always optional. */
7731 flags |= CP_PARSER_FLAGS_OPTIONAL;
7732 }
7733
7734 cp_parser_check_decl_spec (decl_specs);
7735
7736 /* Don't allow a friend specifier with a class definition. */
7737 if (decl_specs->specs[(int) ds_friend] != 0
7738 && (*declares_class_or_enum & 2))
7739 error ("class definition may not be declared a friend");
7740 }
7741
7742 /* Parse an (optional) storage-class-specifier.
7743
7744 storage-class-specifier:
7745 auto
7746 register
7747 static
7748 extern
7749 mutable
7750
7751 GNU Extension:
7752
7753 storage-class-specifier:
7754 thread
7755
7756 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7757
7758 static tree
cp_parser_storage_class_specifier_opt(cp_parser * parser)7759 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7760 {
7761 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7762 {
7763 case RID_AUTO:
7764 case RID_REGISTER:
7765 case RID_STATIC:
7766 case RID_EXTERN:
7767 case RID_MUTABLE:
7768 case RID_THREAD:
7769 /* Consume the token. */
7770 return cp_lexer_consume_token (parser->lexer)->u.value;
7771
7772 default:
7773 return NULL_TREE;
7774 }
7775 }
7776
7777 /* Parse an (optional) function-specifier.
7778
7779 function-specifier:
7780 inline
7781 virtual
7782 explicit
7783
7784 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7785 Updates DECL_SPECS, if it is non-NULL. */
7786
7787 static tree
cp_parser_function_specifier_opt(cp_parser * parser,cp_decl_specifier_seq * decl_specs)7788 cp_parser_function_specifier_opt (cp_parser* parser,
7789 cp_decl_specifier_seq *decl_specs)
7790 {
7791 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7792 {
7793 case RID_INLINE:
7794 if (decl_specs)
7795 ++decl_specs->specs[(int) ds_inline];
7796 break;
7797
7798 case RID_VIRTUAL:
7799 /* 14.5.2.3 [temp.mem]
7800
7801 A member function template shall not be virtual. */
7802 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7803 error ("templates may not be %<virtual%>");
7804 else if (decl_specs)
7805 ++decl_specs->specs[(int) ds_virtual];
7806 break;
7807
7808 case RID_EXPLICIT:
7809 if (decl_specs)
7810 ++decl_specs->specs[(int) ds_explicit];
7811 break;
7812
7813 default:
7814 return NULL_TREE;
7815 }
7816
7817 /* Consume the token. */
7818 return cp_lexer_consume_token (parser->lexer)->u.value;
7819 }
7820
7821 /* Parse a linkage-specification.
7822
7823 linkage-specification:
7824 extern string-literal { declaration-seq [opt] }
7825 extern string-literal declaration */
7826
7827 static void
cp_parser_linkage_specification(cp_parser * parser)7828 cp_parser_linkage_specification (cp_parser* parser)
7829 {
7830 tree linkage;
7831
7832 /* Look for the `extern' keyword. */
7833 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7834
7835 /* Look for the string-literal. */
7836 linkage = cp_parser_string_literal (parser, false, false);
7837
7838 /* Transform the literal into an identifier. If the literal is a
7839 wide-character string, or contains embedded NULs, then we can't
7840 handle it as the user wants. */
7841 if (strlen (TREE_STRING_POINTER (linkage))
7842 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7843 {
7844 cp_parser_error (parser, "invalid linkage-specification");
7845 /* Assume C++ linkage. */
7846 linkage = lang_name_cplusplus;
7847 }
7848 else
7849 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7850
7851 /* We're now using the new linkage. */
7852 push_lang_context (linkage);
7853
7854 /* If the next token is a `{', then we're using the first
7855 production. */
7856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7857 {
7858 /* Consume the `{' token. */
7859 cp_lexer_consume_token (parser->lexer);
7860 /* Parse the declarations. */
7861 cp_parser_declaration_seq_opt (parser);
7862 /* Look for the closing `}'. */
7863 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7864 }
7865 /* Otherwise, there's just one declaration. */
7866 else
7867 {
7868 bool saved_in_unbraced_linkage_specification_p;
7869
7870 saved_in_unbraced_linkage_specification_p
7871 = parser->in_unbraced_linkage_specification_p;
7872 parser->in_unbraced_linkage_specification_p = true;
7873 cp_parser_declaration (parser);
7874 parser->in_unbraced_linkage_specification_p
7875 = saved_in_unbraced_linkage_specification_p;
7876 }
7877
7878 /* We're done with the linkage-specification. */
7879 pop_lang_context ();
7880 }
7881
7882 /* Special member functions [gram.special] */
7883
7884 /* Parse a conversion-function-id.
7885
7886 conversion-function-id:
7887 operator conversion-type-id
7888
7889 Returns an IDENTIFIER_NODE representing the operator. */
7890
7891 static tree
cp_parser_conversion_function_id(cp_parser * parser)7892 cp_parser_conversion_function_id (cp_parser* parser)
7893 {
7894 tree type;
7895 tree saved_scope;
7896 tree saved_qualifying_scope;
7897 tree saved_object_scope;
7898 tree pushed_scope = NULL_TREE;
7899
7900 /* Look for the `operator' token. */
7901 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7902 return error_mark_node;
7903 /* When we parse the conversion-type-id, the current scope will be
7904 reset. However, we need that information in able to look up the
7905 conversion function later, so we save it here. */
7906 saved_scope = parser->scope;
7907 saved_qualifying_scope = parser->qualifying_scope;
7908 saved_object_scope = parser->object_scope;
7909 /* We must enter the scope of the class so that the names of
7910 entities declared within the class are available in the
7911 conversion-type-id. For example, consider:
7912
7913 struct S {
7914 typedef int I;
7915 operator I();
7916 };
7917
7918 S::operator I() { ... }
7919
7920 In order to see that `I' is a type-name in the definition, we
7921 must be in the scope of `S'. */
7922 if (saved_scope)
7923 pushed_scope = push_scope (saved_scope);
7924 /* Parse the conversion-type-id. */
7925 type = cp_parser_conversion_type_id (parser);
7926 /* Leave the scope of the class, if any. */
7927 if (pushed_scope)
7928 pop_scope (pushed_scope);
7929 /* Restore the saved scope. */
7930 parser->scope = saved_scope;
7931 parser->qualifying_scope = saved_qualifying_scope;
7932 parser->object_scope = saved_object_scope;
7933 /* If the TYPE is invalid, indicate failure. */
7934 if (type == error_mark_node)
7935 return error_mark_node;
7936 return mangle_conv_op_name_for_type (type);
7937 }
7938
7939 /* Parse a conversion-type-id:
7940
7941 conversion-type-id:
7942 type-specifier-seq conversion-declarator [opt]
7943
7944 Returns the TYPE specified. */
7945
7946 static tree
cp_parser_conversion_type_id(cp_parser * parser)7947 cp_parser_conversion_type_id (cp_parser* parser)
7948 {
7949 tree attributes;
7950 cp_decl_specifier_seq type_specifiers;
7951 cp_declarator *declarator;
7952 tree type_specified;
7953
7954 /* Parse the attributes. */
7955 attributes = cp_parser_attributes_opt (parser);
7956 /* Parse the type-specifiers. */
7957 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7958 &type_specifiers);
7959 /* If that didn't work, stop. */
7960 if (type_specifiers.type == error_mark_node)
7961 return error_mark_node;
7962 /* Parse the conversion-declarator. */
7963 declarator = cp_parser_conversion_declarator_opt (parser);
7964
7965 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7966 /*initialized=*/0, &attributes);
7967 if (attributes)
7968 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7969 return type_specified;
7970 }
7971
7972 /* Parse an (optional) conversion-declarator.
7973
7974 conversion-declarator:
7975 ptr-operator conversion-declarator [opt]
7976
7977 */
7978
7979 static cp_declarator *
cp_parser_conversion_declarator_opt(cp_parser * parser)7980 cp_parser_conversion_declarator_opt (cp_parser* parser)
7981 {
7982 enum tree_code code;
7983 tree class_type;
7984 cp_cv_quals cv_quals;
7985
7986 /* We don't know if there's a ptr-operator next, or not. */
7987 cp_parser_parse_tentatively (parser);
7988 /* Try the ptr-operator. */
7989 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7990 /* If it worked, look for more conversion-declarators. */
7991 if (cp_parser_parse_definitely (parser))
7992 {
7993 cp_declarator *declarator;
7994
7995 /* Parse another optional declarator. */
7996 declarator = cp_parser_conversion_declarator_opt (parser);
7997
7998 /* Create the representation of the declarator. */
7999 if (class_type)
8000 declarator = make_ptrmem_declarator (cv_quals, class_type,
8001 declarator);
8002 else if (code == INDIRECT_REF)
8003 declarator = make_pointer_declarator (cv_quals, declarator);
8004 else
8005 declarator = make_reference_declarator (cv_quals, declarator);
8006
8007 return declarator;
8008 }
8009
8010 return NULL;
8011 }
8012
8013 /* Parse an (optional) ctor-initializer.
8014
8015 ctor-initializer:
8016 : mem-initializer-list
8017
8018 Returns TRUE iff the ctor-initializer was actually present. */
8019
8020 static bool
cp_parser_ctor_initializer_opt(cp_parser * parser)8021 cp_parser_ctor_initializer_opt (cp_parser* parser)
8022 {
8023 /* If the next token is not a `:', then there is no
8024 ctor-initializer. */
8025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8026 {
8027 /* Do default initialization of any bases and members. */
8028 if (DECL_CONSTRUCTOR_P (current_function_decl))
8029 finish_mem_initializers (NULL_TREE);
8030
8031 return false;
8032 }
8033
8034 /* Consume the `:' token. */
8035 cp_lexer_consume_token (parser->lexer);
8036 /* And the mem-initializer-list. */
8037 cp_parser_mem_initializer_list (parser);
8038
8039 return true;
8040 }
8041
8042 /* Parse a mem-initializer-list.
8043
8044 mem-initializer-list:
8045 mem-initializer
8046 mem-initializer , mem-initializer-list */
8047
8048 static void
cp_parser_mem_initializer_list(cp_parser * parser)8049 cp_parser_mem_initializer_list (cp_parser* parser)
8050 {
8051 tree mem_initializer_list = NULL_TREE;
8052
8053 /* Let the semantic analysis code know that we are starting the
8054 mem-initializer-list. */
8055 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8056 error ("only constructors take base initializers");
8057
8058 /* Loop through the list. */
8059 while (true)
8060 {
8061 tree mem_initializer;
8062
8063 /* Parse the mem-initializer. */
8064 mem_initializer = cp_parser_mem_initializer (parser);
8065 /* Add it to the list, unless it was erroneous. */
8066 if (mem_initializer != error_mark_node)
8067 {
8068 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8069 mem_initializer_list = mem_initializer;
8070 }
8071 /* If the next token is not a `,', we're done. */
8072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8073 break;
8074 /* Consume the `,' token. */
8075 cp_lexer_consume_token (parser->lexer);
8076 }
8077
8078 /* Perform semantic analysis. */
8079 if (DECL_CONSTRUCTOR_P (current_function_decl))
8080 finish_mem_initializers (mem_initializer_list);
8081 }
8082
8083 /* Parse a mem-initializer.
8084
8085 mem-initializer:
8086 mem-initializer-id ( expression-list [opt] )
8087
8088 GNU extension:
8089
8090 mem-initializer:
8091 ( expression-list [opt] )
8092
8093 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8094 class) or FIELD_DECL (for a non-static data member) to initialize;
8095 the TREE_VALUE is the expression-list. An empty initialization
8096 list is represented by void_list_node. */
8097
8098 static tree
cp_parser_mem_initializer(cp_parser * parser)8099 cp_parser_mem_initializer (cp_parser* parser)
8100 {
8101 tree mem_initializer_id;
8102 tree expression_list;
8103 tree member;
8104
8105 /* Find out what is being initialized. */
8106 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8107 {
8108 pedwarn ("anachronistic old-style base class initializer");
8109 mem_initializer_id = NULL_TREE;
8110 }
8111 else
8112 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8113 member = expand_member_init (mem_initializer_id);
8114 if (member && !DECL_P (member))
8115 in_base_initializer = 1;
8116
8117 expression_list
8118 = cp_parser_parenthesized_expression_list (parser, false,
8119 /*cast_p=*/false,
8120 /*non_constant_p=*/NULL);
8121 if (expression_list == error_mark_node)
8122 return error_mark_node;
8123 if (!expression_list)
8124 expression_list = void_type_node;
8125
8126 in_base_initializer = 0;
8127
8128 return member ? build_tree_list (member, expression_list) : error_mark_node;
8129 }
8130
8131 /* Parse a mem-initializer-id.
8132
8133 mem-initializer-id:
8134 :: [opt] nested-name-specifier [opt] class-name
8135 identifier
8136
8137 Returns a TYPE indicating the class to be initializer for the first
8138 production. Returns an IDENTIFIER_NODE indicating the data member
8139 to be initialized for the second production. */
8140
8141 static tree
cp_parser_mem_initializer_id(cp_parser * parser)8142 cp_parser_mem_initializer_id (cp_parser* parser)
8143 {
8144 bool global_scope_p;
8145 bool nested_name_specifier_p;
8146 bool template_p = false;
8147 tree id;
8148
8149 /* `typename' is not allowed in this context ([temp.res]). */
8150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8151 {
8152 error ("keyword %<typename%> not allowed in this context (a qualified "
8153 "member initializer is implicitly a type)");
8154 cp_lexer_consume_token (parser->lexer);
8155 }
8156 /* Look for the optional `::' operator. */
8157 global_scope_p
8158 = (cp_parser_global_scope_opt (parser,
8159 /*current_scope_valid_p=*/false)
8160 != NULL_TREE);
8161 /* Look for the optional nested-name-specifier. The simplest way to
8162 implement:
8163
8164 [temp.res]
8165
8166 The keyword `typename' is not permitted in a base-specifier or
8167 mem-initializer; in these contexts a qualified name that
8168 depends on a template-parameter is implicitly assumed to be a
8169 type name.
8170
8171 is to assume that we have seen the `typename' keyword at this
8172 point. */
8173 nested_name_specifier_p
8174 = (cp_parser_nested_name_specifier_opt (parser,
8175 /*typename_keyword_p=*/true,
8176 /*check_dependency_p=*/true,
8177 /*type_p=*/true,
8178 /*is_declaration=*/true)
8179 != NULL_TREE);
8180 if (nested_name_specifier_p)
8181 template_p = cp_parser_optional_template_keyword (parser);
8182 /* If there is a `::' operator or a nested-name-specifier, then we
8183 are definitely looking for a class-name. */
8184 if (global_scope_p || nested_name_specifier_p)
8185 return cp_parser_class_name (parser,
8186 /*typename_keyword_p=*/true,
8187 /*template_keyword_p=*/template_p,
8188 none_type,
8189 /*check_dependency_p=*/true,
8190 /*class_head_p=*/false,
8191 /*is_declaration=*/true);
8192 /* Otherwise, we could also be looking for an ordinary identifier. */
8193 cp_parser_parse_tentatively (parser);
8194 /* Try a class-name. */
8195 id = cp_parser_class_name (parser,
8196 /*typename_keyword_p=*/true,
8197 /*template_keyword_p=*/false,
8198 none_type,
8199 /*check_dependency_p=*/true,
8200 /*class_head_p=*/false,
8201 /*is_declaration=*/true);
8202 /* If we found one, we're done. */
8203 if (cp_parser_parse_definitely (parser))
8204 return id;
8205 /* Otherwise, look for an ordinary identifier. */
8206 return cp_parser_identifier (parser);
8207 }
8208
8209 /* Overloading [gram.over] */
8210
8211 /* Parse an operator-function-id.
8212
8213 operator-function-id:
8214 operator operator
8215
8216 Returns an IDENTIFIER_NODE for the operator which is a
8217 human-readable spelling of the identifier, e.g., `operator +'. */
8218
8219 static tree
cp_parser_operator_function_id(cp_parser * parser)8220 cp_parser_operator_function_id (cp_parser* parser)
8221 {
8222 /* Look for the `operator' keyword. */
8223 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8224 return error_mark_node;
8225 /* And then the name of the operator itself. */
8226 return cp_parser_operator (parser);
8227 }
8228
8229 /* Parse an operator.
8230
8231 operator:
8232 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8233 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8234 || ++ -- , ->* -> () []
8235
8236 GNU Extensions:
8237
8238 operator:
8239 <? >? <?= >?=
8240
8241 Returns an IDENTIFIER_NODE for the operator which is a
8242 human-readable spelling of the identifier, e.g., `operator +'. */
8243
8244 static tree
cp_parser_operator(cp_parser * parser)8245 cp_parser_operator (cp_parser* parser)
8246 {
8247 tree id = NULL_TREE;
8248 cp_token *token;
8249
8250 /* Peek at the next token. */
8251 token = cp_lexer_peek_token (parser->lexer);
8252 /* Figure out which operator we have. */
8253 switch (token->type)
8254 {
8255 case CPP_KEYWORD:
8256 {
8257 enum tree_code op;
8258
8259 /* The keyword should be either `new' or `delete'. */
8260 if (token->keyword == RID_NEW)
8261 op = NEW_EXPR;
8262 else if (token->keyword == RID_DELETE)
8263 op = DELETE_EXPR;
8264 else
8265 break;
8266
8267 /* Consume the `new' or `delete' token. */
8268 cp_lexer_consume_token (parser->lexer);
8269
8270 /* Peek at the next token. */
8271 token = cp_lexer_peek_token (parser->lexer);
8272 /* If it's a `[' token then this is the array variant of the
8273 operator. */
8274 if (token->type == CPP_OPEN_SQUARE)
8275 {
8276 /* Consume the `[' token. */
8277 cp_lexer_consume_token (parser->lexer);
8278 /* Look for the `]' token. */
8279 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8280 id = ansi_opname (op == NEW_EXPR
8281 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8282 }
8283 /* Otherwise, we have the non-array variant. */
8284 else
8285 id = ansi_opname (op);
8286
8287 return id;
8288 }
8289
8290 case CPP_PLUS:
8291 id = ansi_opname (PLUS_EXPR);
8292 break;
8293
8294 case CPP_MINUS:
8295 id = ansi_opname (MINUS_EXPR);
8296 break;
8297
8298 case CPP_MULT:
8299 id = ansi_opname (MULT_EXPR);
8300 break;
8301
8302 case CPP_DIV:
8303 id = ansi_opname (TRUNC_DIV_EXPR);
8304 break;
8305
8306 case CPP_MOD:
8307 id = ansi_opname (TRUNC_MOD_EXPR);
8308 break;
8309
8310 case CPP_XOR:
8311 id = ansi_opname (BIT_XOR_EXPR);
8312 break;
8313
8314 case CPP_AND:
8315 id = ansi_opname (BIT_AND_EXPR);
8316 break;
8317
8318 case CPP_OR:
8319 id = ansi_opname (BIT_IOR_EXPR);
8320 break;
8321
8322 case CPP_COMPL:
8323 id = ansi_opname (BIT_NOT_EXPR);
8324 break;
8325
8326 case CPP_NOT:
8327 id = ansi_opname (TRUTH_NOT_EXPR);
8328 break;
8329
8330 case CPP_EQ:
8331 id = ansi_assopname (NOP_EXPR);
8332 break;
8333
8334 case CPP_LESS:
8335 id = ansi_opname (LT_EXPR);
8336 break;
8337
8338 case CPP_GREATER:
8339 id = ansi_opname (GT_EXPR);
8340 break;
8341
8342 case CPP_PLUS_EQ:
8343 id = ansi_assopname (PLUS_EXPR);
8344 break;
8345
8346 case CPP_MINUS_EQ:
8347 id = ansi_assopname (MINUS_EXPR);
8348 break;
8349
8350 case CPP_MULT_EQ:
8351 id = ansi_assopname (MULT_EXPR);
8352 break;
8353
8354 case CPP_DIV_EQ:
8355 id = ansi_assopname (TRUNC_DIV_EXPR);
8356 break;
8357
8358 case CPP_MOD_EQ:
8359 id = ansi_assopname (TRUNC_MOD_EXPR);
8360 break;
8361
8362 case CPP_XOR_EQ:
8363 id = ansi_assopname (BIT_XOR_EXPR);
8364 break;
8365
8366 case CPP_AND_EQ:
8367 id = ansi_assopname (BIT_AND_EXPR);
8368 break;
8369
8370 case CPP_OR_EQ:
8371 id = ansi_assopname (BIT_IOR_EXPR);
8372 break;
8373
8374 case CPP_LSHIFT:
8375 id = ansi_opname (LSHIFT_EXPR);
8376 break;
8377
8378 case CPP_RSHIFT:
8379 id = ansi_opname (RSHIFT_EXPR);
8380 break;
8381
8382 case CPP_LSHIFT_EQ:
8383 id = ansi_assopname (LSHIFT_EXPR);
8384 break;
8385
8386 case CPP_RSHIFT_EQ:
8387 id = ansi_assopname (RSHIFT_EXPR);
8388 break;
8389
8390 case CPP_EQ_EQ:
8391 id = ansi_opname (EQ_EXPR);
8392 break;
8393
8394 case CPP_NOT_EQ:
8395 id = ansi_opname (NE_EXPR);
8396 break;
8397
8398 case CPP_LESS_EQ:
8399 id = ansi_opname (LE_EXPR);
8400 break;
8401
8402 case CPP_GREATER_EQ:
8403 id = ansi_opname (GE_EXPR);
8404 break;
8405
8406 case CPP_AND_AND:
8407 id = ansi_opname (TRUTH_ANDIF_EXPR);
8408 break;
8409
8410 case CPP_OR_OR:
8411 id = ansi_opname (TRUTH_ORIF_EXPR);
8412 break;
8413
8414 case CPP_PLUS_PLUS:
8415 id = ansi_opname (POSTINCREMENT_EXPR);
8416 break;
8417
8418 case CPP_MINUS_MINUS:
8419 id = ansi_opname (PREDECREMENT_EXPR);
8420 break;
8421
8422 case CPP_COMMA:
8423 id = ansi_opname (COMPOUND_EXPR);
8424 break;
8425
8426 case CPP_DEREF_STAR:
8427 id = ansi_opname (MEMBER_REF);
8428 break;
8429
8430 case CPP_DEREF:
8431 id = ansi_opname (COMPONENT_REF);
8432 break;
8433
8434 case CPP_OPEN_PAREN:
8435 /* Consume the `('. */
8436 cp_lexer_consume_token (parser->lexer);
8437 /* Look for the matching `)'. */
8438 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8439 return ansi_opname (CALL_EXPR);
8440
8441 case CPP_OPEN_SQUARE:
8442 /* Consume the `['. */
8443 cp_lexer_consume_token (parser->lexer);
8444 /* Look for the matching `]'. */
8445 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8446 return ansi_opname (ARRAY_REF);
8447
8448 default:
8449 /* Anything else is an error. */
8450 break;
8451 }
8452
8453 /* If we have selected an identifier, we need to consume the
8454 operator token. */
8455 if (id)
8456 cp_lexer_consume_token (parser->lexer);
8457 /* Otherwise, no valid operator name was present. */
8458 else
8459 {
8460 cp_parser_error (parser, "expected operator");
8461 id = error_mark_node;
8462 }
8463
8464 return id;
8465 }
8466
8467 /* Parse a template-declaration.
8468
8469 template-declaration:
8470 export [opt] template < template-parameter-list > declaration
8471
8472 If MEMBER_P is TRUE, this template-declaration occurs within a
8473 class-specifier.
8474
8475 The grammar rule given by the standard isn't correct. What
8476 is really meant is:
8477
8478 template-declaration:
8479 export [opt] template-parameter-list-seq
8480 decl-specifier-seq [opt] init-declarator [opt] ;
8481 export [opt] template-parameter-list-seq
8482 function-definition
8483
8484 template-parameter-list-seq:
8485 template-parameter-list-seq [opt]
8486 template < template-parameter-list > */
8487
8488 static void
cp_parser_template_declaration(cp_parser * parser,bool member_p)8489 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8490 {
8491 /* Check for `export'. */
8492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8493 {
8494 /* Consume the `export' token. */
8495 cp_lexer_consume_token (parser->lexer);
8496 /* Warn that we do not support `export'. */
8497 warning (0, "keyword %<export%> not implemented, and will be ignored");
8498 }
8499
8500 cp_parser_template_declaration_after_export (parser, member_p);
8501 }
8502
8503 /* Parse a template-parameter-list.
8504
8505 template-parameter-list:
8506 template-parameter
8507 template-parameter-list , template-parameter
8508
8509 Returns a TREE_LIST. Each node represents a template parameter.
8510 The nodes are connected via their TREE_CHAINs. */
8511
8512 static tree
cp_parser_template_parameter_list(cp_parser * parser)8513 cp_parser_template_parameter_list (cp_parser* parser)
8514 {
8515 tree parameter_list = NULL_TREE;
8516
8517 begin_template_parm_list ();
8518 while (true)
8519 {
8520 tree parameter;
8521 cp_token *token;
8522 bool is_non_type;
8523
8524 /* Parse the template-parameter. */
8525 parameter = cp_parser_template_parameter (parser, &is_non_type);
8526 /* Add it to the list. */
8527 if (parameter != error_mark_node)
8528 parameter_list = process_template_parm (parameter_list,
8529 parameter,
8530 is_non_type);
8531 else
8532 {
8533 tree err_parm = build_tree_list (parameter, parameter);
8534 TREE_VALUE (err_parm) = error_mark_node;
8535 parameter_list = chainon (parameter_list, err_parm);
8536 }
8537
8538 /* Peek at the next token. */
8539 token = cp_lexer_peek_token (parser->lexer);
8540 /* If it's not a `,', we're done. */
8541 if (token->type != CPP_COMMA)
8542 break;
8543 /* Otherwise, consume the `,' token. */
8544 cp_lexer_consume_token (parser->lexer);
8545 }
8546
8547 return end_template_parm_list (parameter_list);
8548 }
8549
8550 /* Parse a template-parameter.
8551
8552 template-parameter:
8553 type-parameter
8554 parameter-declaration
8555
8556 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8557 the parameter. The TREE_PURPOSE is the default value, if any.
8558 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8559 iff this parameter is a non-type parameter. */
8560
8561 static tree
cp_parser_template_parameter(cp_parser * parser,bool * is_non_type)8562 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8563 {
8564 cp_token *token;
8565 cp_parameter_declarator *parameter_declarator;
8566 tree parm;
8567
8568 /* Assume it is a type parameter or a template parameter. */
8569 *is_non_type = false;
8570 /* Peek at the next token. */
8571 token = cp_lexer_peek_token (parser->lexer);
8572 /* If it is `class' or `template', we have a type-parameter. */
8573 if (token->keyword == RID_TEMPLATE)
8574 return cp_parser_type_parameter (parser);
8575 /* If it is `class' or `typename' we do not know yet whether it is a
8576 type parameter or a non-type parameter. Consider:
8577
8578 template <typename T, typename T::X X> ...
8579
8580 or:
8581
8582 template <class C, class D*> ...
8583
8584 Here, the first parameter is a type parameter, and the second is
8585 a non-type parameter. We can tell by looking at the token after
8586 the identifier -- if it is a `,', `=', or `>' then we have a type
8587 parameter. */
8588 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8589 {
8590 /* Peek at the token after `class' or `typename'. */
8591 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8592 /* If it's an identifier, skip it. */
8593 if (token->type == CPP_NAME)
8594 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8595 /* Now, see if the token looks like the end of a template
8596 parameter. */
8597 if (token->type == CPP_COMMA
8598 || token->type == CPP_EQ
8599 || token->type == CPP_GREATER)
8600 return cp_parser_type_parameter (parser);
8601 }
8602
8603 /* Otherwise, it is a non-type parameter.
8604
8605 [temp.param]
8606
8607 When parsing a default template-argument for a non-type
8608 template-parameter, the first non-nested `>' is taken as the end
8609 of the template parameter-list rather than a greater-than
8610 operator. */
8611 *is_non_type = true;
8612 parameter_declarator
8613 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8614 /*parenthesized_p=*/NULL);
8615 parm = grokdeclarator (parameter_declarator->declarator,
8616 ¶meter_declarator->decl_specifiers,
8617 PARM, /*initialized=*/0,
8618 /*attrlist=*/NULL);
8619 if (parm == error_mark_node)
8620 return error_mark_node;
8621 return build_tree_list (parameter_declarator->default_argument, parm);
8622 }
8623
8624 /* Parse a type-parameter.
8625
8626 type-parameter:
8627 class identifier [opt]
8628 class identifier [opt] = type-id
8629 typename identifier [opt]
8630 typename identifier [opt] = type-id
8631 template < template-parameter-list > class identifier [opt]
8632 template < template-parameter-list > class identifier [opt]
8633 = id-expression
8634
8635 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8636 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8637 the declaration of the parameter. */
8638
8639 static tree
cp_parser_type_parameter(cp_parser * parser)8640 cp_parser_type_parameter (cp_parser* parser)
8641 {
8642 cp_token *token;
8643 tree parameter;
8644
8645 /* Look for a keyword to tell us what kind of parameter this is. */
8646 token = cp_parser_require (parser, CPP_KEYWORD,
8647 "`class', `typename', or `template'");
8648 if (!token)
8649 return error_mark_node;
8650
8651 switch (token->keyword)
8652 {
8653 case RID_CLASS:
8654 case RID_TYPENAME:
8655 {
8656 tree identifier;
8657 tree default_argument;
8658
8659 /* If the next token is an identifier, then it names the
8660 parameter. */
8661 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8662 identifier = cp_parser_identifier (parser);
8663 else
8664 identifier = NULL_TREE;
8665
8666 /* Create the parameter. */
8667 parameter = finish_template_type_parm (class_type_node, identifier);
8668
8669 /* If the next token is an `=', we have a default argument. */
8670 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8671 {
8672 /* Consume the `=' token. */
8673 cp_lexer_consume_token (parser->lexer);
8674 /* Parse the default-argument. */
8675 push_deferring_access_checks (dk_no_deferred);
8676 default_argument = cp_parser_type_id (parser);
8677 pop_deferring_access_checks ();
8678 }
8679 else
8680 default_argument = NULL_TREE;
8681
8682 /* Create the combined representation of the parameter and the
8683 default argument. */
8684 parameter = build_tree_list (default_argument, parameter);
8685 }
8686 break;
8687
8688 case RID_TEMPLATE:
8689 {
8690 tree parameter_list;
8691 tree identifier;
8692 tree default_argument;
8693
8694 /* Look for the `<'. */
8695 cp_parser_require (parser, CPP_LESS, "`<'");
8696 /* Parse the template-parameter-list. */
8697 parameter_list = cp_parser_template_parameter_list (parser);
8698 /* Look for the `>'. */
8699 cp_parser_require (parser, CPP_GREATER, "`>'");
8700 /* Look for the `class' keyword. */
8701 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8702 /* If the next token is an `=', then there is a
8703 default-argument. If the next token is a `>', we are at
8704 the end of the parameter-list. If the next token is a `,',
8705 then we are at the end of this parameter. */
8706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8707 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8708 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8709 {
8710 identifier = cp_parser_identifier (parser);
8711 /* Treat invalid names as if the parameter were nameless. */
8712 if (identifier == error_mark_node)
8713 identifier = NULL_TREE;
8714 }
8715 else
8716 identifier = NULL_TREE;
8717
8718 /* Create the template parameter. */
8719 parameter = finish_template_template_parm (class_type_node,
8720 identifier);
8721
8722 /* If the next token is an `=', then there is a
8723 default-argument. */
8724 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8725 {
8726 bool is_template;
8727
8728 /* Consume the `='. */
8729 cp_lexer_consume_token (parser->lexer);
8730 /* Parse the id-expression. */
8731 push_deferring_access_checks (dk_no_deferred);
8732 default_argument
8733 = cp_parser_id_expression (parser,
8734 /*template_keyword_p=*/false,
8735 /*check_dependency_p=*/true,
8736 /*template_p=*/&is_template,
8737 /*declarator_p=*/false,
8738 /*optional_p=*/false);
8739 if (TREE_CODE (default_argument) == TYPE_DECL)
8740 /* If the id-expression was a template-id that refers to
8741 a template-class, we already have the declaration here,
8742 so no further lookup is needed. */
8743 ;
8744 else
8745 /* Look up the name. */
8746 default_argument
8747 = cp_parser_lookup_name (parser, default_argument,
8748 none_type,
8749 /*is_template=*/is_template,
8750 /*is_namespace=*/false,
8751 /*check_dependency=*/true,
8752 /*ambiguous_decls=*/NULL);
8753 /* See if the default argument is valid. */
8754 default_argument
8755 = check_template_template_default_arg (default_argument);
8756 pop_deferring_access_checks ();
8757 }
8758 else
8759 default_argument = NULL_TREE;
8760
8761 /* Create the combined representation of the parameter and the
8762 default argument. */
8763 parameter = build_tree_list (default_argument, parameter);
8764 }
8765 break;
8766
8767 default:
8768 gcc_unreachable ();
8769 break;
8770 }
8771
8772 return parameter;
8773 }
8774
8775 /* Parse a template-id.
8776
8777 template-id:
8778 template-name < template-argument-list [opt] >
8779
8780 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8781 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8782 returned. Otherwise, if the template-name names a function, or set
8783 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8784 names a class, returns a TYPE_DECL for the specialization.
8785
8786 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8787 uninstantiated templates. */
8788
8789 static tree
cp_parser_template_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool is_declaration)8790 cp_parser_template_id (cp_parser *parser,
8791 bool template_keyword_p,
8792 bool check_dependency_p,
8793 bool is_declaration)
8794 {
8795 int i;
8796 tree template;
8797 tree arguments;
8798 tree template_id;
8799 cp_token_position start_of_id = 0;
8800 deferred_access_check *chk;
8801 VEC (deferred_access_check,gc) *access_check;
8802 cp_token *next_token, *next_token_2;
8803 bool is_identifier;
8804
8805 /* If the next token corresponds to a template-id, there is no need
8806 to reparse it. */
8807 next_token = cp_lexer_peek_token (parser->lexer);
8808 if (next_token->type == CPP_TEMPLATE_ID)
8809 {
8810 struct tree_check *check_value;
8811
8812 /* Get the stored value. */
8813 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8814 /* Perform any access checks that were deferred. */
8815 access_check = check_value->checks;
8816 if (access_check)
8817 {
8818 for (i = 0 ;
8819 VEC_iterate (deferred_access_check, access_check, i, chk) ;
8820 ++i)
8821 {
8822 perform_or_defer_access_check (chk->binfo,
8823 chk->decl,
8824 chk->diag_decl);
8825 }
8826 }
8827 /* Return the stored value. */
8828 return check_value->value;
8829 }
8830
8831 /* Avoid performing name lookup if there is no possibility of
8832 finding a template-id. */
8833 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8834 || (next_token->type == CPP_NAME
8835 && !cp_parser_nth_token_starts_template_argument_list_p
8836 (parser, 2)))
8837 {
8838 cp_parser_error (parser, "expected template-id");
8839 return error_mark_node;
8840 }
8841
8842 /* Remember where the template-id starts. */
8843 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8844 start_of_id = cp_lexer_token_position (parser->lexer, false);
8845
8846 push_deferring_access_checks (dk_deferred);
8847
8848 /* Parse the template-name. */
8849 is_identifier = false;
8850 template = cp_parser_template_name (parser, template_keyword_p,
8851 check_dependency_p,
8852 is_declaration,
8853 &is_identifier);
8854 if (template == error_mark_node || is_identifier)
8855 {
8856 pop_deferring_access_checks ();
8857 return template;
8858 }
8859
8860 /* If we find the sequence `[:' after a template-name, it's probably
8861 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8862 parse correctly the argument list. */
8863 next_token = cp_lexer_peek_token (parser->lexer);
8864 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8865 if (next_token->type == CPP_OPEN_SQUARE
8866 && next_token->flags & DIGRAPH
8867 && next_token_2->type == CPP_COLON
8868 && !(next_token_2->flags & PREV_WHITE))
8869 {
8870 cp_parser_parse_tentatively (parser);
8871 /* Change `:' into `::'. */
8872 next_token_2->type = CPP_SCOPE;
8873 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8874 CPP_LESS. */
8875 cp_lexer_consume_token (parser->lexer);
8876 /* Parse the arguments. */
8877 arguments = cp_parser_enclosed_template_argument_list (parser);
8878 if (!cp_parser_parse_definitely (parser))
8879 {
8880 /* If we couldn't parse an argument list, then we revert our changes
8881 and return simply an error. Maybe this is not a template-id
8882 after all. */
8883 next_token_2->type = CPP_COLON;
8884 cp_parser_error (parser, "expected %<<%>");
8885 pop_deferring_access_checks ();
8886 return error_mark_node;
8887 }
8888 /* Otherwise, emit an error about the invalid digraph, but continue
8889 parsing because we got our argument list. */
8890 pedwarn ("%<<::%> cannot begin a template-argument list");
8891 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8892 "between %<<%> and %<::%>");
8893 if (!flag_permissive)
8894 {
8895 static bool hint;
8896 if (!hint)
8897 {
8898 inform ("(if you use -fpermissive G++ will accept your code)");
8899 hint = true;
8900 }
8901 }
8902 }
8903 else
8904 {
8905 /* Look for the `<' that starts the template-argument-list. */
8906 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8907 {
8908 pop_deferring_access_checks ();
8909 return error_mark_node;
8910 }
8911 /* Parse the arguments. */
8912 arguments = cp_parser_enclosed_template_argument_list (parser);
8913 }
8914
8915 /* Build a representation of the specialization. */
8916 if (TREE_CODE (template) == IDENTIFIER_NODE)
8917 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8918 else if (DECL_CLASS_TEMPLATE_P (template)
8919 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8920 {
8921 bool entering_scope;
8922 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8923 template (rather than some instantiation thereof) only if
8924 is not nested within some other construct. For example, in
8925 "template <typename T> void f(T) { A<T>::", A<T> is just an
8926 instantiation of A. */
8927 entering_scope = (template_parm_scope_p ()
8928 && cp_lexer_next_token_is (parser->lexer,
8929 CPP_SCOPE));
8930 template_id
8931 = finish_template_type (template, arguments, entering_scope);
8932 }
8933 else
8934 {
8935 /* If it's not a class-template or a template-template, it should be
8936 a function-template. */
8937 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8938 || TREE_CODE (template) == OVERLOAD
8939 || BASELINK_P (template)));
8940
8941 template_id = lookup_template_function (template, arguments);
8942 }
8943
8944 /* If parsing tentatively, replace the sequence of tokens that makes
8945 up the template-id with a CPP_TEMPLATE_ID token. That way,
8946 should we re-parse the token stream, we will not have to repeat
8947 the effort required to do the parse, nor will we issue duplicate
8948 error messages about problems during instantiation of the
8949 template. */
8950 if (start_of_id)
8951 {
8952 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8953
8954 /* Reset the contents of the START_OF_ID token. */
8955 token->type = CPP_TEMPLATE_ID;
8956 /* Retrieve any deferred checks. Do not pop this access checks yet
8957 so the memory will not be reclaimed during token replacing below. */
8958 token->u.tree_check_value = GGC_CNEW (struct tree_check);
8959 token->u.tree_check_value->value = template_id;
8960 token->u.tree_check_value->checks = get_deferred_access_checks ();
8961 token->keyword = RID_MAX;
8962
8963 /* Purge all subsequent tokens. */
8964 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8965
8966 /* ??? Can we actually assume that, if template_id ==
8967 error_mark_node, we will have issued a diagnostic to the
8968 user, as opposed to simply marking the tentative parse as
8969 failed? */
8970 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8971 error ("parse error in template argument list");
8972 }
8973
8974 pop_deferring_access_checks ();
8975 return template_id;
8976 }
8977
8978 /* Parse a template-name.
8979
8980 template-name:
8981 identifier
8982
8983 The standard should actually say:
8984
8985 template-name:
8986 identifier
8987 operator-function-id
8988
8989 A defect report has been filed about this issue.
8990
8991 A conversion-function-id cannot be a template name because they cannot
8992 be part of a template-id. In fact, looking at this code:
8993
8994 a.operator K<int>()
8995
8996 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8997 It is impossible to call a templated conversion-function-id with an
8998 explicit argument list, since the only allowed template parameter is
8999 the type to which it is converting.
9000
9001 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9002 `template' keyword, in a construction like:
9003
9004 T::template f<3>()
9005
9006 In that case `f' is taken to be a template-name, even though there
9007 is no way of knowing for sure.
9008
9009 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9010 name refers to a set of overloaded functions, at least one of which
9011 is a template, or an IDENTIFIER_NODE with the name of the template,
9012 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9013 names are looked up inside uninstantiated templates. */
9014
9015 static tree
cp_parser_template_name(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool is_declaration,bool * is_identifier)9016 cp_parser_template_name (cp_parser* parser,
9017 bool template_keyword_p,
9018 bool check_dependency_p,
9019 bool is_declaration,
9020 bool *is_identifier)
9021 {
9022 tree identifier;
9023 tree decl;
9024 tree fns;
9025
9026 /* If the next token is `operator', then we have either an
9027 operator-function-id or a conversion-function-id. */
9028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9029 {
9030 /* We don't know whether we're looking at an
9031 operator-function-id or a conversion-function-id. */
9032 cp_parser_parse_tentatively (parser);
9033 /* Try an operator-function-id. */
9034 identifier = cp_parser_operator_function_id (parser);
9035 /* If that didn't work, try a conversion-function-id. */
9036 if (!cp_parser_parse_definitely (parser))
9037 {
9038 cp_parser_error (parser, "expected template-name");
9039 return error_mark_node;
9040 }
9041 }
9042 /* Look for the identifier. */
9043 else
9044 identifier = cp_parser_identifier (parser);
9045
9046 /* If we didn't find an identifier, we don't have a template-id. */
9047 if (identifier == error_mark_node)
9048 return error_mark_node;
9049
9050 /* If the name immediately followed the `template' keyword, then it
9051 is a template-name. However, if the next token is not `<', then
9052 we do not treat it as a template-name, since it is not being used
9053 as part of a template-id. This enables us to handle constructs
9054 like:
9055
9056 template <typename T> struct S { S(); };
9057 template <typename T> S<T>::S();
9058
9059 correctly. We would treat `S' as a template -- if it were `S<T>'
9060 -- but we do not if there is no `<'. */
9061
9062 if (processing_template_decl
9063 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9064 {
9065 /* In a declaration, in a dependent context, we pretend that the
9066 "template" keyword was present in order to improve error
9067 recovery. For example, given:
9068
9069 template <typename T> void f(T::X<int>);
9070
9071 we want to treat "X<int>" as a template-id. */
9072 if (is_declaration
9073 && !template_keyword_p
9074 && parser->scope && TYPE_P (parser->scope)
9075 && check_dependency_p
9076 && dependent_type_p (parser->scope)
9077 /* Do not do this for dtors (or ctors), since they never
9078 need the template keyword before their name. */
9079 && !constructor_name_p (identifier, parser->scope))
9080 {
9081 cp_token_position start = 0;
9082
9083 /* Explain what went wrong. */
9084 error ("non-template %qD used as template", identifier);
9085 inform ("use %<%T::template %D%> to indicate that it is a template",
9086 parser->scope, identifier);
9087 /* If parsing tentatively, find the location of the "<" token. */
9088 if (cp_parser_simulate_error (parser))
9089 start = cp_lexer_token_position (parser->lexer, true);
9090 /* Parse the template arguments so that we can issue error
9091 messages about them. */
9092 cp_lexer_consume_token (parser->lexer);
9093 cp_parser_enclosed_template_argument_list (parser);
9094 /* Skip tokens until we find a good place from which to
9095 continue parsing. */
9096 cp_parser_skip_to_closing_parenthesis (parser,
9097 /*recovering=*/true,
9098 /*or_comma=*/true,
9099 /*consume_paren=*/false);
9100 /* If parsing tentatively, permanently remove the
9101 template argument list. That will prevent duplicate
9102 error messages from being issued about the missing
9103 "template" keyword. */
9104 if (start)
9105 cp_lexer_purge_tokens_after (parser->lexer, start);
9106 if (is_identifier)
9107 *is_identifier = true;
9108 return identifier;
9109 }
9110
9111 /* If the "template" keyword is present, then there is generally
9112 no point in doing name-lookup, so we just return IDENTIFIER.
9113 But, if the qualifying scope is non-dependent then we can
9114 (and must) do name-lookup normally. */
9115 if (template_keyword_p
9116 && (!parser->scope
9117 || (TYPE_P (parser->scope)
9118 && dependent_type_p (parser->scope))))
9119 return identifier;
9120 }
9121
9122 /* Look up the name. */
9123 decl = cp_parser_lookup_name (parser, identifier,
9124 none_type,
9125 /*is_template=*/false,
9126 /*is_namespace=*/false,
9127 check_dependency_p,
9128 /*ambiguous_decls=*/NULL);
9129 decl = maybe_get_template_decl_from_type_decl (decl);
9130
9131 /* If DECL is a template, then the name was a template-name. */
9132 if (TREE_CODE (decl) == TEMPLATE_DECL)
9133 ;
9134 else
9135 {
9136 tree fn = NULL_TREE;
9137
9138 /* The standard does not explicitly indicate whether a name that
9139 names a set of overloaded declarations, some of which are
9140 templates, is a template-name. However, such a name should
9141 be a template-name; otherwise, there is no way to form a
9142 template-id for the overloaded templates. */
9143 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9144 if (TREE_CODE (fns) == OVERLOAD)
9145 for (fn = fns; fn; fn = OVL_NEXT (fn))
9146 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9147 break;
9148
9149 if (!fn)
9150 {
9151 /* The name does not name a template. */
9152 cp_parser_error (parser, "expected template-name");
9153 return error_mark_node;
9154 }
9155 }
9156
9157 /* If DECL is dependent, and refers to a function, then just return
9158 its name; we will look it up again during template instantiation. */
9159 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9160 {
9161 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9162 if (TYPE_P (scope) && dependent_type_p (scope))
9163 return identifier;
9164 }
9165
9166 return decl;
9167 }
9168
9169 /* Parse a template-argument-list.
9170
9171 template-argument-list:
9172 template-argument
9173 template-argument-list , template-argument
9174
9175 Returns a TREE_VEC containing the arguments. */
9176
9177 static tree
cp_parser_template_argument_list(cp_parser * parser)9178 cp_parser_template_argument_list (cp_parser* parser)
9179 {
9180 tree fixed_args[10];
9181 unsigned n_args = 0;
9182 unsigned alloced = 10;
9183 tree *arg_ary = fixed_args;
9184 tree vec;
9185 bool saved_in_template_argument_list_p;
9186 bool saved_ice_p;
9187 bool saved_non_ice_p;
9188
9189 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9190 parser->in_template_argument_list_p = true;
9191 /* Even if the template-id appears in an integral
9192 constant-expression, the contents of the argument list do
9193 not. */
9194 saved_ice_p = parser->integral_constant_expression_p;
9195 parser->integral_constant_expression_p = false;
9196 saved_non_ice_p = parser->non_integral_constant_expression_p;
9197 parser->non_integral_constant_expression_p = false;
9198 /* Parse the arguments. */
9199 do
9200 {
9201 tree argument;
9202
9203 if (n_args)
9204 /* Consume the comma. */
9205 cp_lexer_consume_token (parser->lexer);
9206
9207 /* Parse the template-argument. */
9208 argument = cp_parser_template_argument (parser);
9209 if (n_args == alloced)
9210 {
9211 alloced *= 2;
9212
9213 if (arg_ary == fixed_args)
9214 {
9215 arg_ary = XNEWVEC (tree, alloced);
9216 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9217 }
9218 else
9219 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9220 }
9221 arg_ary[n_args++] = argument;
9222 }
9223 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9224
9225 vec = make_tree_vec (n_args);
9226
9227 while (n_args--)
9228 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9229
9230 if (arg_ary != fixed_args)
9231 free (arg_ary);
9232 parser->non_integral_constant_expression_p = saved_non_ice_p;
9233 parser->integral_constant_expression_p = saved_ice_p;
9234 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9235 return vec;
9236 }
9237
9238 /* Parse a template-argument.
9239
9240 template-argument:
9241 assignment-expression
9242 type-id
9243 id-expression
9244
9245 The representation is that of an assignment-expression, type-id, or
9246 id-expression -- except that the qualified id-expression is
9247 evaluated, so that the value returned is either a DECL or an
9248 OVERLOAD.
9249
9250 Although the standard says "assignment-expression", it forbids
9251 throw-expressions or assignments in the template argument.
9252 Therefore, we use "conditional-expression" instead. */
9253
9254 static tree
cp_parser_template_argument(cp_parser * parser)9255 cp_parser_template_argument (cp_parser* parser)
9256 {
9257 tree argument;
9258 bool template_p;
9259 bool address_p;
9260 bool maybe_type_id = false;
9261 cp_token *token;
9262 cp_id_kind idk;
9263
9264 /* There's really no way to know what we're looking at, so we just
9265 try each alternative in order.
9266
9267 [temp.arg]
9268
9269 In a template-argument, an ambiguity between a type-id and an
9270 expression is resolved to a type-id, regardless of the form of
9271 the corresponding template-parameter.
9272
9273 Therefore, we try a type-id first. */
9274 cp_parser_parse_tentatively (parser);
9275 argument = cp_parser_type_id (parser);
9276 /* If there was no error parsing the type-id but the next token is a '>>',
9277 we probably found a typo for '> >'. But there are type-id which are
9278 also valid expressions. For instance:
9279
9280 struct X { int operator >> (int); };
9281 template <int V> struct Foo {};
9282 Foo<X () >> 5> r;
9283
9284 Here 'X()' is a valid type-id of a function type, but the user just
9285 wanted to write the expression "X() >> 5". Thus, we remember that we
9286 found a valid type-id, but we still try to parse the argument as an
9287 expression to see what happens. */
9288 if (!cp_parser_error_occurred (parser)
9289 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9290 {
9291 maybe_type_id = true;
9292 cp_parser_abort_tentative_parse (parser);
9293 }
9294 else
9295 {
9296 /* If the next token isn't a `,' or a `>', then this argument wasn't
9297 really finished. This means that the argument is not a valid
9298 type-id. */
9299 if (!cp_parser_next_token_ends_template_argument_p (parser))
9300 cp_parser_error (parser, "expected template-argument");
9301 /* If that worked, we're done. */
9302 if (cp_parser_parse_definitely (parser))
9303 return argument;
9304 }
9305 /* We're still not sure what the argument will be. */
9306 cp_parser_parse_tentatively (parser);
9307 /* Try a template. */
9308 argument = cp_parser_id_expression (parser,
9309 /*template_keyword_p=*/false,
9310 /*check_dependency_p=*/true,
9311 &template_p,
9312 /*declarator_p=*/false,
9313 /*optional_p=*/false);
9314 /* If the next token isn't a `,' or a `>', then this argument wasn't
9315 really finished. */
9316 if (!cp_parser_next_token_ends_template_argument_p (parser))
9317 cp_parser_error (parser, "expected template-argument");
9318 if (!cp_parser_error_occurred (parser))
9319 {
9320 /* Figure out what is being referred to. If the id-expression
9321 was for a class template specialization, then we will have a
9322 TYPE_DECL at this point. There is no need to do name lookup
9323 at this point in that case. */
9324 if (TREE_CODE (argument) != TYPE_DECL)
9325 argument = cp_parser_lookup_name (parser, argument,
9326 none_type,
9327 /*is_template=*/template_p,
9328 /*is_namespace=*/false,
9329 /*check_dependency=*/true,
9330 /*ambiguous_decls=*/NULL);
9331 if (TREE_CODE (argument) != TEMPLATE_DECL
9332 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9333 cp_parser_error (parser, "expected template-name");
9334 }
9335 if (cp_parser_parse_definitely (parser))
9336 return argument;
9337 /* It must be a non-type argument. There permitted cases are given
9338 in [temp.arg.nontype]:
9339
9340 -- an integral constant-expression of integral or enumeration
9341 type; or
9342
9343 -- the name of a non-type template-parameter; or
9344
9345 -- the name of an object or function with external linkage...
9346
9347 -- the address of an object or function with external linkage...
9348
9349 -- a pointer to member... */
9350 /* Look for a non-type template parameter. */
9351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9352 {
9353 cp_parser_parse_tentatively (parser);
9354 argument = cp_parser_primary_expression (parser,
9355 /*adress_p=*/false,
9356 /*cast_p=*/false,
9357 /*template_arg_p=*/true,
9358 &idk);
9359 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9360 || !cp_parser_next_token_ends_template_argument_p (parser))
9361 cp_parser_simulate_error (parser);
9362 if (cp_parser_parse_definitely (parser))
9363 return argument;
9364 }
9365
9366 /* If the next token is "&", the argument must be the address of an
9367 object or function with external linkage. */
9368 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9369 if (address_p)
9370 cp_lexer_consume_token (parser->lexer);
9371 /* See if we might have an id-expression. */
9372 token = cp_lexer_peek_token (parser->lexer);
9373 if (token->type == CPP_NAME
9374 || token->keyword == RID_OPERATOR
9375 || token->type == CPP_SCOPE
9376 || token->type == CPP_TEMPLATE_ID
9377 || token->type == CPP_NESTED_NAME_SPECIFIER)
9378 {
9379 cp_parser_parse_tentatively (parser);
9380 argument = cp_parser_primary_expression (parser,
9381 address_p,
9382 /*cast_p=*/false,
9383 /*template_arg_p=*/true,
9384 &idk);
9385 if (cp_parser_error_occurred (parser)
9386 || !cp_parser_next_token_ends_template_argument_p (parser))
9387 cp_parser_abort_tentative_parse (parser);
9388 else
9389 {
9390 if (TREE_CODE (argument) == INDIRECT_REF)
9391 {
9392 gcc_assert (REFERENCE_REF_P (argument));
9393 argument = TREE_OPERAND (argument, 0);
9394 }
9395
9396 if (TREE_CODE (argument) == VAR_DECL)
9397 {
9398 /* A variable without external linkage might still be a
9399 valid constant-expression, so no error is issued here
9400 if the external-linkage check fails. */
9401 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9402 cp_parser_simulate_error (parser);
9403 }
9404 else if (is_overloaded_fn (argument))
9405 /* All overloaded functions are allowed; if the external
9406 linkage test does not pass, an error will be issued
9407 later. */
9408 ;
9409 else if (address_p
9410 && (TREE_CODE (argument) == OFFSET_REF
9411 || TREE_CODE (argument) == SCOPE_REF))
9412 /* A pointer-to-member. */
9413 ;
9414 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9415 ;
9416 else
9417 cp_parser_simulate_error (parser);
9418
9419 if (cp_parser_parse_definitely (parser))
9420 {
9421 if (address_p)
9422 argument = build_x_unary_op (ADDR_EXPR, argument);
9423 return argument;
9424 }
9425 }
9426 }
9427 /* If the argument started with "&", there are no other valid
9428 alternatives at this point. */
9429 if (address_p)
9430 {
9431 cp_parser_error (parser, "invalid non-type template argument");
9432 return error_mark_node;
9433 }
9434
9435 /* If the argument wasn't successfully parsed as a type-id followed
9436 by '>>', the argument can only be a constant expression now.
9437 Otherwise, we try parsing the constant-expression tentatively,
9438 because the argument could really be a type-id. */
9439 if (maybe_type_id)
9440 cp_parser_parse_tentatively (parser);
9441 argument = cp_parser_constant_expression (parser,
9442 /*allow_non_constant_p=*/false,
9443 /*non_constant_p=*/NULL);
9444 argument = fold_non_dependent_expr (argument);
9445 if (!maybe_type_id)
9446 return argument;
9447 if (!cp_parser_next_token_ends_template_argument_p (parser))
9448 cp_parser_error (parser, "expected template-argument");
9449 if (cp_parser_parse_definitely (parser))
9450 return argument;
9451 /* We did our best to parse the argument as a non type-id, but that
9452 was the only alternative that matched (albeit with a '>' after
9453 it). We can assume it's just a typo from the user, and a
9454 diagnostic will then be issued. */
9455 return cp_parser_type_id (parser);
9456 }
9457
9458 /* Parse an explicit-instantiation.
9459
9460 explicit-instantiation:
9461 template declaration
9462
9463 Although the standard says `declaration', what it really means is:
9464
9465 explicit-instantiation:
9466 template decl-specifier-seq [opt] declarator [opt] ;
9467
9468 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9469 supposed to be allowed. A defect report has been filed about this
9470 issue.
9471
9472 GNU Extension:
9473
9474 explicit-instantiation:
9475 storage-class-specifier template
9476 decl-specifier-seq [opt] declarator [opt] ;
9477 function-specifier template
9478 decl-specifier-seq [opt] declarator [opt] ; */
9479
9480 static void
cp_parser_explicit_instantiation(cp_parser * parser)9481 cp_parser_explicit_instantiation (cp_parser* parser)
9482 {
9483 int declares_class_or_enum;
9484 cp_decl_specifier_seq decl_specifiers;
9485 tree extension_specifier = NULL_TREE;
9486
9487 /* Look for an (optional) storage-class-specifier or
9488 function-specifier. */
9489 if (cp_parser_allow_gnu_extensions_p (parser))
9490 {
9491 extension_specifier
9492 = cp_parser_storage_class_specifier_opt (parser);
9493 if (!extension_specifier)
9494 extension_specifier
9495 = cp_parser_function_specifier_opt (parser,
9496 /*decl_specs=*/NULL);
9497 }
9498
9499 /* Look for the `template' keyword. */
9500 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9501 /* Let the front end know that we are processing an explicit
9502 instantiation. */
9503 begin_explicit_instantiation ();
9504 /* [temp.explicit] says that we are supposed to ignore access
9505 control while processing explicit instantiation directives. */
9506 push_deferring_access_checks (dk_no_check);
9507 /* Parse a decl-specifier-seq. */
9508 cp_parser_decl_specifier_seq (parser,
9509 CP_PARSER_FLAGS_OPTIONAL,
9510 &decl_specifiers,
9511 &declares_class_or_enum);
9512 /* If there was exactly one decl-specifier, and it declared a class,
9513 and there's no declarator, then we have an explicit type
9514 instantiation. */
9515 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9516 {
9517 tree type;
9518
9519 type = check_tag_decl (&decl_specifiers);
9520 /* Turn access control back on for names used during
9521 template instantiation. */
9522 pop_deferring_access_checks ();
9523 if (type)
9524 do_type_instantiation (type, extension_specifier,
9525 /*complain=*/tf_error);
9526 }
9527 else
9528 {
9529 cp_declarator *declarator;
9530 tree decl;
9531
9532 /* Parse the declarator. */
9533 declarator
9534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9535 /*ctor_dtor_or_conv_p=*/NULL,
9536 /*parenthesized_p=*/NULL,
9537 /*member_p=*/false);
9538 if (declares_class_or_enum & 2)
9539 cp_parser_check_for_definition_in_return_type (declarator,
9540 decl_specifiers.type);
9541 if (declarator != cp_error_declarator)
9542 {
9543 decl = grokdeclarator (declarator, &decl_specifiers,
9544 NORMAL, 0, &decl_specifiers.attributes);
9545 /* Turn access control back on for names used during
9546 template instantiation. */
9547 pop_deferring_access_checks ();
9548 /* Do the explicit instantiation. */
9549 do_decl_instantiation (decl, extension_specifier);
9550 }
9551 else
9552 {
9553 pop_deferring_access_checks ();
9554 /* Skip the body of the explicit instantiation. */
9555 cp_parser_skip_to_end_of_statement (parser);
9556 }
9557 }
9558 /* We're done with the instantiation. */
9559 end_explicit_instantiation ();
9560
9561 cp_parser_consume_semicolon_at_end_of_statement (parser);
9562 }
9563
9564 /* Parse an explicit-specialization.
9565
9566 explicit-specialization:
9567 template < > declaration
9568
9569 Although the standard says `declaration', what it really means is:
9570
9571 explicit-specialization:
9572 template <> decl-specifier [opt] init-declarator [opt] ;
9573 template <> function-definition
9574 template <> explicit-specialization
9575 template <> template-declaration */
9576
9577 static void
cp_parser_explicit_specialization(cp_parser * parser)9578 cp_parser_explicit_specialization (cp_parser* parser)
9579 {
9580 bool need_lang_pop;
9581 /* Look for the `template' keyword. */
9582 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9583 /* Look for the `<'. */
9584 cp_parser_require (parser, CPP_LESS, "`<'");
9585 /* Look for the `>'. */
9586 cp_parser_require (parser, CPP_GREATER, "`>'");
9587 /* We have processed another parameter list. */
9588 ++parser->num_template_parameter_lists;
9589 /* [temp]
9590
9591 A template ... explicit specialization ... shall not have C
9592 linkage. */
9593 if (current_lang_name == lang_name_c)
9594 {
9595 error ("template specialization with C linkage");
9596 /* Give it C++ linkage to avoid confusing other parts of the
9597 front end. */
9598 push_lang_context (lang_name_cplusplus);
9599 need_lang_pop = true;
9600 }
9601 else
9602 need_lang_pop = false;
9603 /* Let the front end know that we are beginning a specialization. */
9604 if (!begin_specialization ())
9605 {
9606 end_specialization ();
9607 cp_parser_skip_to_end_of_block_or_statement (parser);
9608 return;
9609 }
9610
9611 /* If the next keyword is `template', we need to figure out whether
9612 or not we're looking a template-declaration. */
9613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9614 {
9615 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9616 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9617 cp_parser_template_declaration_after_export (parser,
9618 /*member_p=*/false);
9619 else
9620 cp_parser_explicit_specialization (parser);
9621 }
9622 else
9623 /* Parse the dependent declaration. */
9624 cp_parser_single_declaration (parser,
9625 /*checks=*/NULL,
9626 /*member_p=*/false,
9627 /*friend_p=*/NULL);
9628 /* We're done with the specialization. */
9629 end_specialization ();
9630 /* For the erroneous case of a template with C linkage, we pushed an
9631 implicit C++ linkage scope; exit that scope now. */
9632 if (need_lang_pop)
9633 pop_lang_context ();
9634 /* We're done with this parameter list. */
9635 --parser->num_template_parameter_lists;
9636 }
9637
9638 /* Parse a type-specifier.
9639
9640 type-specifier:
9641 simple-type-specifier
9642 class-specifier
9643 enum-specifier
9644 elaborated-type-specifier
9645 cv-qualifier
9646
9647 GNU Extension:
9648
9649 type-specifier:
9650 __complex__
9651
9652 Returns a representation of the type-specifier. For a
9653 class-specifier, enum-specifier, or elaborated-type-specifier, a
9654 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9655
9656 The parser flags FLAGS is used to control type-specifier parsing.
9657
9658 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9659 in a decl-specifier-seq.
9660
9661 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9662 class-specifier, enum-specifier, or elaborated-type-specifier, then
9663 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9664 if a type is declared; 2 if it is defined. Otherwise, it is set to
9665 zero.
9666
9667 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9668 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9669 is set to FALSE. */
9670
9671 static tree
cp_parser_type_specifier(cp_parser * parser,cp_parser_flags flags,cp_decl_specifier_seq * decl_specs,bool is_declaration,int * declares_class_or_enum,bool * is_cv_qualifier)9672 cp_parser_type_specifier (cp_parser* parser,
9673 cp_parser_flags flags,
9674 cp_decl_specifier_seq *decl_specs,
9675 bool is_declaration,
9676 int* declares_class_or_enum,
9677 bool* is_cv_qualifier)
9678 {
9679 tree type_spec = NULL_TREE;
9680 cp_token *token;
9681 enum rid keyword;
9682 cp_decl_spec ds = ds_last;
9683
9684 /* Assume this type-specifier does not declare a new type. */
9685 if (declares_class_or_enum)
9686 *declares_class_or_enum = 0;
9687 /* And that it does not specify a cv-qualifier. */
9688 if (is_cv_qualifier)
9689 *is_cv_qualifier = false;
9690 /* Peek at the next token. */
9691 token = cp_lexer_peek_token (parser->lexer);
9692
9693 /* If we're looking at a keyword, we can use that to guide the
9694 production we choose. */
9695 keyword = token->keyword;
9696 switch (keyword)
9697 {
9698 case RID_ENUM:
9699 /* Look for the enum-specifier. */
9700 type_spec = cp_parser_enum_specifier (parser);
9701 /* If that worked, we're done. */
9702 if (type_spec)
9703 {
9704 if (declares_class_or_enum)
9705 *declares_class_or_enum = 2;
9706 if (decl_specs)
9707 cp_parser_set_decl_spec_type (decl_specs,
9708 type_spec,
9709 /*user_defined_p=*/true);
9710 return type_spec;
9711 }
9712 else
9713 goto elaborated_type_specifier;
9714
9715 /* Any of these indicate either a class-specifier, or an
9716 elaborated-type-specifier. */
9717 case RID_CLASS:
9718 case RID_STRUCT:
9719 case RID_UNION:
9720 /* Parse tentatively so that we can back up if we don't find a
9721 class-specifier. */
9722 cp_parser_parse_tentatively (parser);
9723 /* Look for the class-specifier. */
9724 type_spec = cp_parser_class_specifier (parser);
9725 /* If that worked, we're done. */
9726 if (cp_parser_parse_definitely (parser))
9727 {
9728 if (declares_class_or_enum)
9729 *declares_class_or_enum = 2;
9730 if (decl_specs)
9731 cp_parser_set_decl_spec_type (decl_specs,
9732 type_spec,
9733 /*user_defined_p=*/true);
9734 return type_spec;
9735 }
9736
9737 /* Fall through. */
9738 elaborated_type_specifier:
9739 /* We're declaring (not defining) a class or enum. */
9740 if (declares_class_or_enum)
9741 *declares_class_or_enum = 1;
9742
9743 /* Fall through. */
9744 case RID_TYPENAME:
9745 /* Look for an elaborated-type-specifier. */
9746 type_spec
9747 = (cp_parser_elaborated_type_specifier
9748 (parser,
9749 decl_specs && decl_specs->specs[(int) ds_friend],
9750 is_declaration));
9751 if (decl_specs)
9752 cp_parser_set_decl_spec_type (decl_specs,
9753 type_spec,
9754 /*user_defined_p=*/true);
9755 return type_spec;
9756
9757 case RID_CONST:
9758 ds = ds_const;
9759 if (is_cv_qualifier)
9760 *is_cv_qualifier = true;
9761 break;
9762
9763 case RID_VOLATILE:
9764 ds = ds_volatile;
9765 if (is_cv_qualifier)
9766 *is_cv_qualifier = true;
9767 break;
9768
9769 case RID_RESTRICT:
9770 ds = ds_restrict;
9771 if (is_cv_qualifier)
9772 *is_cv_qualifier = true;
9773 break;
9774
9775 case RID_COMPLEX:
9776 /* The `__complex__' keyword is a GNU extension. */
9777 ds = ds_complex;
9778 break;
9779
9780 default:
9781 break;
9782 }
9783
9784 /* Handle simple keywords. */
9785 if (ds != ds_last)
9786 {
9787 if (decl_specs)
9788 {
9789 ++decl_specs->specs[(int)ds];
9790 decl_specs->any_specifiers_p = true;
9791 }
9792 return cp_lexer_consume_token (parser->lexer)->u.value;
9793 }
9794
9795 /* If we do not already have a type-specifier, assume we are looking
9796 at a simple-type-specifier. */
9797 type_spec = cp_parser_simple_type_specifier (parser,
9798 decl_specs,
9799 flags);
9800
9801 /* If we didn't find a type-specifier, and a type-specifier was not
9802 optional in this context, issue an error message. */
9803 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9804 {
9805 cp_parser_error (parser, "expected type specifier");
9806 return error_mark_node;
9807 }
9808
9809 return type_spec;
9810 }
9811
9812 /* Parse a simple-type-specifier.
9813
9814 simple-type-specifier:
9815 :: [opt] nested-name-specifier [opt] type-name
9816 :: [opt] nested-name-specifier template template-id
9817 char
9818 wchar_t
9819 bool
9820 short
9821 int
9822 long
9823 signed
9824 unsigned
9825 float
9826 double
9827 void
9828
9829 GNU Extension:
9830
9831 simple-type-specifier:
9832 __typeof__ unary-expression
9833 __typeof__ ( type-id )
9834
9835 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9836 appropriately updated. */
9837
9838 static tree
cp_parser_simple_type_specifier(cp_parser * parser,cp_decl_specifier_seq * decl_specs,cp_parser_flags flags)9839 cp_parser_simple_type_specifier (cp_parser* parser,
9840 cp_decl_specifier_seq *decl_specs,
9841 cp_parser_flags flags)
9842 {
9843 tree type = NULL_TREE;
9844 cp_token *token;
9845
9846 /* Peek at the next token. */
9847 token = cp_lexer_peek_token (parser->lexer);
9848
9849 /* If we're looking at a keyword, things are easy. */
9850 switch (token->keyword)
9851 {
9852 case RID_CHAR:
9853 if (decl_specs)
9854 decl_specs->explicit_char_p = true;
9855 type = char_type_node;
9856 break;
9857 case RID_WCHAR:
9858 type = wchar_type_node;
9859 break;
9860 case RID_BOOL:
9861 type = boolean_type_node;
9862 break;
9863 case RID_SHORT:
9864 if (decl_specs)
9865 ++decl_specs->specs[(int) ds_short];
9866 type = short_integer_type_node;
9867 break;
9868 case RID_INT:
9869 if (decl_specs)
9870 decl_specs->explicit_int_p = true;
9871 type = integer_type_node;
9872 break;
9873 case RID_LONG:
9874 if (decl_specs)
9875 ++decl_specs->specs[(int) ds_long];
9876 type = long_integer_type_node;
9877 break;
9878 case RID_SIGNED:
9879 if (decl_specs)
9880 ++decl_specs->specs[(int) ds_signed];
9881 type = integer_type_node;
9882 break;
9883 case RID_UNSIGNED:
9884 if (decl_specs)
9885 ++decl_specs->specs[(int) ds_unsigned];
9886 type = unsigned_type_node;
9887 break;
9888 case RID_FLOAT:
9889 type = float_type_node;
9890 break;
9891 case RID_DOUBLE:
9892 type = double_type_node;
9893 break;
9894 case RID_VOID:
9895 type = void_type_node;
9896 break;
9897
9898 case RID_TYPEOF:
9899 /* Consume the `typeof' token. */
9900 cp_lexer_consume_token (parser->lexer);
9901 /* Parse the operand to `typeof'. */
9902 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9903 /* If it is not already a TYPE, take its type. */
9904 if (!TYPE_P (type))
9905 type = finish_typeof (type);
9906
9907 if (decl_specs)
9908 cp_parser_set_decl_spec_type (decl_specs, type,
9909 /*user_defined_p=*/true);
9910
9911 return type;
9912
9913 default:
9914 break;
9915 }
9916
9917 /* If the type-specifier was for a built-in type, we're done. */
9918 if (type)
9919 {
9920 tree id;
9921
9922 /* Record the type. */
9923 if (decl_specs
9924 && (token->keyword != RID_SIGNED
9925 && token->keyword != RID_UNSIGNED
9926 && token->keyword != RID_SHORT
9927 && token->keyword != RID_LONG))
9928 cp_parser_set_decl_spec_type (decl_specs,
9929 type,
9930 /*user_defined=*/false);
9931 if (decl_specs)
9932 decl_specs->any_specifiers_p = true;
9933
9934 /* Consume the token. */
9935 id = cp_lexer_consume_token (parser->lexer)->u.value;
9936
9937 /* There is no valid C++ program where a non-template type is
9938 followed by a "<". That usually indicates that the user thought
9939 that the type was a template. */
9940 cp_parser_check_for_invalid_template_id (parser, type);
9941
9942 return TYPE_NAME (type);
9943 }
9944
9945 /* The type-specifier must be a user-defined type. */
9946 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9947 {
9948 bool qualified_p;
9949 bool global_p;
9950
9951 /* Don't gobble tokens or issue error messages if this is an
9952 optional type-specifier. */
9953 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9954 cp_parser_parse_tentatively (parser);
9955
9956 /* Look for the optional `::' operator. */
9957 global_p
9958 = (cp_parser_global_scope_opt (parser,
9959 /*current_scope_valid_p=*/false)
9960 != NULL_TREE);
9961 /* Look for the nested-name specifier. */
9962 qualified_p
9963 = (cp_parser_nested_name_specifier_opt (parser,
9964 /*typename_keyword_p=*/false,
9965 /*check_dependency_p=*/true,
9966 /*type_p=*/false,
9967 /*is_declaration=*/false)
9968 != NULL_TREE);
9969 /* If we have seen a nested-name-specifier, and the next token
9970 is `template', then we are using the template-id production. */
9971 if (parser->scope
9972 && cp_parser_optional_template_keyword (parser))
9973 {
9974 /* Look for the template-id. */
9975 type = cp_parser_template_id (parser,
9976 /*template_keyword_p=*/true,
9977 /*check_dependency_p=*/true,
9978 /*is_declaration=*/false);
9979 /* If the template-id did not name a type, we are out of
9980 luck. */
9981 if (TREE_CODE (type) != TYPE_DECL)
9982 {
9983 cp_parser_error (parser, "expected template-id for type");
9984 type = NULL_TREE;
9985 }
9986 }
9987 /* Otherwise, look for a type-name. */
9988 else
9989 type = cp_parser_type_name (parser);
9990 /* Keep track of all name-lookups performed in class scopes. */
9991 if (type
9992 && !global_p
9993 && !qualified_p
9994 && TREE_CODE (type) == TYPE_DECL
9995 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9996 maybe_note_name_used_in_class (DECL_NAME (type), type);
9997 /* If it didn't work out, we don't have a TYPE. */
9998 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9999 && !cp_parser_parse_definitely (parser))
10000 type = NULL_TREE;
10001 if (type && decl_specs)
10002 cp_parser_set_decl_spec_type (decl_specs, type,
10003 /*user_defined=*/true);
10004 }
10005
10006 /* If we didn't get a type-name, issue an error message. */
10007 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10008 {
10009 cp_parser_error (parser, "expected type-name");
10010 return error_mark_node;
10011 }
10012
10013 /* There is no valid C++ program where a non-template type is
10014 followed by a "<". That usually indicates that the user thought
10015 that the type was a template. */
10016 if (type && type != error_mark_node)
10017 {
10018 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10019 If it is, then the '<'...'>' enclose protocol names rather than
10020 template arguments, and so everything is fine. */
10021 if (c_dialect_objc ()
10022 && (objc_is_id (type) || objc_is_class_name (type)))
10023 {
10024 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10025 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10026
10027 /* Clobber the "unqualified" type previously entered into
10028 DECL_SPECS with the new, improved protocol-qualified version. */
10029 if (decl_specs)
10030 decl_specs->type = qual_type;
10031
10032 return qual_type;
10033 }
10034
10035 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10036 }
10037
10038 return type;
10039 }
10040
10041 /* Parse a type-name.
10042
10043 type-name:
10044 class-name
10045 enum-name
10046 typedef-name
10047
10048 enum-name:
10049 identifier
10050
10051 typedef-name:
10052 identifier
10053
10054 Returns a TYPE_DECL for the type. */
10055
10056 static tree
cp_parser_type_name(cp_parser * parser)10057 cp_parser_type_name (cp_parser* parser)
10058 {
10059 tree type_decl;
10060 tree identifier;
10061
10062 /* We can't know yet whether it is a class-name or not. */
10063 cp_parser_parse_tentatively (parser);
10064 /* Try a class-name. */
10065 type_decl = cp_parser_class_name (parser,
10066 /*typename_keyword_p=*/false,
10067 /*template_keyword_p=*/false,
10068 none_type,
10069 /*check_dependency_p=*/true,
10070 /*class_head_p=*/false,
10071 /*is_declaration=*/false);
10072 /* If it's not a class-name, keep looking. */
10073 if (!cp_parser_parse_definitely (parser))
10074 {
10075 /* It must be a typedef-name or an enum-name. */
10076 identifier = cp_parser_identifier (parser);
10077 if (identifier == error_mark_node)
10078 return error_mark_node;
10079
10080 /* Look up the type-name. */
10081 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10082
10083 if (TREE_CODE (type_decl) != TYPE_DECL
10084 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10085 {
10086 /* See if this is an Objective-C type. */
10087 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10088 tree type = objc_get_protocol_qualified_type (identifier, protos);
10089 if (type)
10090 type_decl = TYPE_NAME (type);
10091 }
10092
10093 /* Issue an error if we did not find a type-name. */
10094 if (TREE_CODE (type_decl) != TYPE_DECL)
10095 {
10096 if (!cp_parser_simulate_error (parser))
10097 cp_parser_name_lookup_error (parser, identifier, type_decl,
10098 "is not a type");
10099 type_decl = error_mark_node;
10100 }
10101 /* Remember that the name was used in the definition of the
10102 current class so that we can check later to see if the
10103 meaning would have been different after the class was
10104 entirely defined. */
10105 else if (type_decl != error_mark_node
10106 && !parser->scope)
10107 maybe_note_name_used_in_class (identifier, type_decl);
10108 }
10109
10110 return type_decl;
10111 }
10112
10113
10114 /* Parse an elaborated-type-specifier. Note that the grammar given
10115 here incorporates the resolution to DR68.
10116
10117 elaborated-type-specifier:
10118 class-key :: [opt] nested-name-specifier [opt] identifier
10119 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10120 enum :: [opt] nested-name-specifier [opt] identifier
10121 typename :: [opt] nested-name-specifier identifier
10122 typename :: [opt] nested-name-specifier template [opt]
10123 template-id
10124
10125 GNU extension:
10126
10127 elaborated-type-specifier:
10128 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10129 class-key attributes :: [opt] nested-name-specifier [opt]
10130 template [opt] template-id
10131 enum attributes :: [opt] nested-name-specifier [opt] identifier
10132
10133 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10134 declared `friend'. If IS_DECLARATION is TRUE, then this
10135 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10136 something is being declared.
10137
10138 Returns the TYPE specified. */
10139
10140 static tree
cp_parser_elaborated_type_specifier(cp_parser * parser,bool is_friend,bool is_declaration)10141 cp_parser_elaborated_type_specifier (cp_parser* parser,
10142 bool is_friend,
10143 bool is_declaration)
10144 {
10145 enum tag_types tag_type;
10146 tree identifier;
10147 tree type = NULL_TREE;
10148 tree attributes = NULL_TREE;
10149
10150 /* See if we're looking at the `enum' keyword. */
10151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10152 {
10153 /* Consume the `enum' token. */
10154 cp_lexer_consume_token (parser->lexer);
10155 /* Remember that it's an enumeration type. */
10156 tag_type = enum_type;
10157 /* Parse the attributes. */
10158 attributes = cp_parser_attributes_opt (parser);
10159 }
10160 /* Or, it might be `typename'. */
10161 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10162 RID_TYPENAME))
10163 {
10164 /* Consume the `typename' token. */
10165 cp_lexer_consume_token (parser->lexer);
10166 /* Remember that it's a `typename' type. */
10167 tag_type = typename_type;
10168 /* The `typename' keyword is only allowed in templates. */
10169 if (!processing_template_decl)
10170 pedwarn ("using %<typename%> outside of template");
10171 }
10172 /* Otherwise it must be a class-key. */
10173 else
10174 {
10175 tag_type = cp_parser_class_key (parser);
10176 if (tag_type == none_type)
10177 return error_mark_node;
10178 /* Parse the attributes. */
10179 attributes = cp_parser_attributes_opt (parser);
10180 }
10181
10182 /* Look for the `::' operator. */
10183 cp_parser_global_scope_opt (parser,
10184 /*current_scope_valid_p=*/false);
10185 /* Look for the nested-name-specifier. */
10186 if (tag_type == typename_type)
10187 {
10188 if (!cp_parser_nested_name_specifier (parser,
10189 /*typename_keyword_p=*/true,
10190 /*check_dependency_p=*/true,
10191 /*type_p=*/true,
10192 is_declaration))
10193 return error_mark_node;
10194 }
10195 else
10196 /* Even though `typename' is not present, the proposed resolution
10197 to Core Issue 180 says that in `class A<T>::B', `B' should be
10198 considered a type-name, even if `A<T>' is dependent. */
10199 cp_parser_nested_name_specifier_opt (parser,
10200 /*typename_keyword_p=*/true,
10201 /*check_dependency_p=*/true,
10202 /*type_p=*/true,
10203 is_declaration);
10204 /* For everything but enumeration types, consider a template-id.
10205 For an enumeration type, consider only a plain identifier. */
10206 if (tag_type != enum_type)
10207 {
10208 bool template_p = false;
10209 tree decl;
10210
10211 /* Allow the `template' keyword. */
10212 template_p = cp_parser_optional_template_keyword (parser);
10213 /* If we didn't see `template', we don't know if there's a
10214 template-id or not. */
10215 if (!template_p)
10216 cp_parser_parse_tentatively (parser);
10217 /* Parse the template-id. */
10218 decl = cp_parser_template_id (parser, template_p,
10219 /*check_dependency_p=*/true,
10220 is_declaration);
10221 /* If we didn't find a template-id, look for an ordinary
10222 identifier. */
10223 if (!template_p && !cp_parser_parse_definitely (parser))
10224 ;
10225 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10226 in effect, then we must assume that, upon instantiation, the
10227 template will correspond to a class. */
10228 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10229 && tag_type == typename_type)
10230 type = make_typename_type (parser->scope, decl,
10231 typename_type,
10232 /*complain=*/tf_error);
10233 else
10234 type = TREE_TYPE (decl);
10235 }
10236
10237 if (!type)
10238 {
10239 identifier = cp_parser_identifier (parser);
10240
10241 if (identifier == error_mark_node)
10242 {
10243 parser->scope = NULL_TREE;
10244 return error_mark_node;
10245 }
10246
10247 /* For a `typename', we needn't call xref_tag. */
10248 if (tag_type == typename_type
10249 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10250 return cp_parser_make_typename_type (parser, parser->scope,
10251 identifier);
10252 /* Look up a qualified name in the usual way. */
10253 if (parser->scope)
10254 {
10255 tree decl;
10256
10257 decl = cp_parser_lookup_name (parser, identifier,
10258 tag_type,
10259 /*is_template=*/false,
10260 /*is_namespace=*/false,
10261 /*check_dependency=*/true,
10262 /*ambiguous_decls=*/NULL);
10263
10264 /* If we are parsing friend declaration, DECL may be a
10265 TEMPLATE_DECL tree node here. However, we need to check
10266 whether this TEMPLATE_DECL results in valid code. Consider
10267 the following example:
10268
10269 namespace N {
10270 template <class T> class C {};
10271 }
10272 class X {
10273 template <class T> friend class N::C; // #1, valid code
10274 };
10275 template <class T> class Y {
10276 friend class N::C; // #2, invalid code
10277 };
10278
10279 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10280 name lookup of `N::C'. We see that friend declaration must
10281 be template for the code to be valid. Note that
10282 processing_template_decl does not work here since it is
10283 always 1 for the above two cases. */
10284
10285 decl = (cp_parser_maybe_treat_template_as_class
10286 (decl, /*tag_name_p=*/is_friend
10287 && parser->num_template_parameter_lists));
10288
10289 if (TREE_CODE (decl) != TYPE_DECL)
10290 {
10291 cp_parser_diagnose_invalid_type_name (parser,
10292 parser->scope,
10293 identifier);
10294 return error_mark_node;
10295 }
10296
10297 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10298 {
10299 bool allow_template = (parser->num_template_parameter_lists
10300 || DECL_SELF_REFERENCE_P (decl));
10301 type = check_elaborated_type_specifier (tag_type, decl,
10302 allow_template);
10303
10304 if (type == error_mark_node)
10305 return error_mark_node;
10306 }
10307
10308 type = TREE_TYPE (decl);
10309 }
10310 else
10311 {
10312 /* An elaborated-type-specifier sometimes introduces a new type and
10313 sometimes names an existing type. Normally, the rule is that it
10314 introduces a new type only if there is not an existing type of
10315 the same name already in scope. For example, given:
10316
10317 struct S {};
10318 void f() { struct S s; }
10319
10320 the `struct S' in the body of `f' is the same `struct S' as in
10321 the global scope; the existing definition is used. However, if
10322 there were no global declaration, this would introduce a new
10323 local class named `S'.
10324
10325 An exception to this rule applies to the following code:
10326
10327 namespace N { struct S; }
10328
10329 Here, the elaborated-type-specifier names a new type
10330 unconditionally; even if there is already an `S' in the
10331 containing scope this declaration names a new type.
10332 This exception only applies if the elaborated-type-specifier
10333 forms the complete declaration:
10334
10335 [class.name]
10336
10337 A declaration consisting solely of `class-key identifier ;' is
10338 either a redeclaration of the name in the current scope or a
10339 forward declaration of the identifier as a class name. It
10340 introduces the name into the current scope.
10341
10342 We are in this situation precisely when the next token is a `;'.
10343
10344 An exception to the exception is that a `friend' declaration does
10345 *not* name a new type; i.e., given:
10346
10347 struct S { friend struct T; };
10348
10349 `T' is not a new type in the scope of `S'.
10350
10351 Also, `new struct S' or `sizeof (struct S)' never results in the
10352 definition of a new type; a new type can only be declared in a
10353 declaration context. */
10354
10355 tag_scope ts;
10356 bool template_p;
10357
10358 if (is_friend)
10359 /* Friends have special name lookup rules. */
10360 ts = ts_within_enclosing_non_class;
10361 else if (is_declaration
10362 && cp_lexer_next_token_is (parser->lexer,
10363 CPP_SEMICOLON))
10364 /* This is a `class-key identifier ;' */
10365 ts = ts_current;
10366 else
10367 ts = ts_global;
10368
10369 template_p =
10370 (parser->num_template_parameter_lists
10371 && (cp_parser_next_token_starts_class_definition_p (parser)
10372 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10373 /* An unqualified name was used to reference this type, so
10374 there were no qualifying templates. */
10375 if (!cp_parser_check_template_parameters (parser,
10376 /*num_templates=*/0))
10377 return error_mark_node;
10378 type = xref_tag (tag_type, identifier, ts, template_p);
10379 }
10380 }
10381
10382 if (type == error_mark_node)
10383 return error_mark_node;
10384
10385 /* Allow attributes on forward declarations of classes. */
10386 if (attributes)
10387 {
10388 if (TREE_CODE (type) == TYPENAME_TYPE)
10389 warning (OPT_Wattributes,
10390 "attributes ignored on uninstantiated type");
10391 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10392 && ! processing_explicit_instantiation)
10393 warning (OPT_Wattributes,
10394 "attributes ignored on template instantiation");
10395 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10396 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10397 else
10398 warning (OPT_Wattributes,
10399 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10400 }
10401
10402 if (tag_type != enum_type)
10403 cp_parser_check_class_key (tag_type, type);
10404
10405 /* A "<" cannot follow an elaborated type specifier. If that
10406 happens, the user was probably trying to form a template-id. */
10407 cp_parser_check_for_invalid_template_id (parser, type);
10408
10409 return type;
10410 }
10411
10412 /* Parse an enum-specifier.
10413
10414 enum-specifier:
10415 enum identifier [opt] { enumerator-list [opt] }
10416
10417 GNU Extensions:
10418 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10419 attributes[opt]
10420
10421 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10422 if the token stream isn't an enum-specifier after all. */
10423
10424 static tree
cp_parser_enum_specifier(cp_parser * parser)10425 cp_parser_enum_specifier (cp_parser* parser)
10426 {
10427 tree identifier;
10428 tree type;
10429 tree attributes;
10430
10431 /* Parse tentatively so that we can back up if we don't find a
10432 enum-specifier. */
10433 cp_parser_parse_tentatively (parser);
10434
10435 /* Caller guarantees that the current token is 'enum', an identifier
10436 possibly follows, and the token after that is an opening brace.
10437 If we don't have an identifier, fabricate an anonymous name for
10438 the enumeration being defined. */
10439 cp_lexer_consume_token (parser->lexer);
10440
10441 attributes = cp_parser_attributes_opt (parser);
10442
10443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10444 identifier = cp_parser_identifier (parser);
10445 else
10446 identifier = make_anon_name ();
10447
10448 /* Look for the `{' but don't consume it yet. */
10449 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10450 cp_parser_simulate_error (parser);
10451
10452 if (!cp_parser_parse_definitely (parser))
10453 return NULL_TREE;
10454
10455 /* Issue an error message if type-definitions are forbidden here. */
10456 if (!cp_parser_check_type_definition (parser))
10457 type = error_mark_node;
10458 else
10459 /* Create the new type. We do this before consuming the opening
10460 brace so the enum will be recorded as being on the line of its
10461 tag (or the 'enum' keyword, if there is no tag). */
10462 type = start_enum (identifier);
10463
10464 /* Consume the opening brace. */
10465 cp_lexer_consume_token (parser->lexer);
10466
10467 if (type == error_mark_node)
10468 {
10469 cp_parser_skip_to_end_of_block_or_statement (parser);
10470 return error_mark_node;
10471 }
10472
10473 /* If the next token is not '}', then there are some enumerators. */
10474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10475 cp_parser_enumerator_list (parser, type);
10476
10477 /* Consume the final '}'. */
10478 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10479
10480 /* Look for trailing attributes to apply to this enumeration, and
10481 apply them if appropriate. */
10482 if (cp_parser_allow_gnu_extensions_p (parser))
10483 {
10484 tree trailing_attr = cp_parser_attributes_opt (parser);
10485 trailing_attr = chainon (trailing_attr, attributes);
10486 cplus_decl_attributes (&type,
10487 trailing_attr,
10488 (int) ATTR_FLAG_TYPE_IN_PLACE);
10489 }
10490
10491 /* Finish up the enumeration. */
10492 finish_enum (type);
10493
10494 return type;
10495 }
10496
10497 /* Parse an enumerator-list. The enumerators all have the indicated
10498 TYPE.
10499
10500 enumerator-list:
10501 enumerator-definition
10502 enumerator-list , enumerator-definition */
10503
10504 static void
cp_parser_enumerator_list(cp_parser * parser,tree type)10505 cp_parser_enumerator_list (cp_parser* parser, tree type)
10506 {
10507 while (true)
10508 {
10509 /* Parse an enumerator-definition. */
10510 cp_parser_enumerator_definition (parser, type);
10511
10512 /* If the next token is not a ',', we've reached the end of
10513 the list. */
10514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10515 break;
10516 /* Otherwise, consume the `,' and keep going. */
10517 cp_lexer_consume_token (parser->lexer);
10518 /* If the next token is a `}', there is a trailing comma. */
10519 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10520 {
10521 if (pedantic && !in_system_header)
10522 pedwarn ("comma at end of enumerator list");
10523 break;
10524 }
10525 }
10526 }
10527
10528 /* Parse an enumerator-definition. The enumerator has the indicated
10529 TYPE.
10530
10531 enumerator-definition:
10532 enumerator
10533 enumerator = constant-expression
10534
10535 enumerator:
10536 identifier */
10537
10538 static void
cp_parser_enumerator_definition(cp_parser * parser,tree type)10539 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10540 {
10541 tree identifier;
10542 tree value;
10543
10544 /* Look for the identifier. */
10545 identifier = cp_parser_identifier (parser);
10546 if (identifier == error_mark_node)
10547 return;
10548
10549 /* If the next token is an '=', then there is an explicit value. */
10550 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10551 {
10552 /* Consume the `=' token. */
10553 cp_lexer_consume_token (parser->lexer);
10554 /* Parse the value. */
10555 value = cp_parser_constant_expression (parser,
10556 /*allow_non_constant_p=*/false,
10557 NULL);
10558 }
10559 else
10560 value = NULL_TREE;
10561
10562 /* Create the enumerator. */
10563 build_enumerator (identifier, value, type);
10564 }
10565
10566 /* Parse a namespace-name.
10567
10568 namespace-name:
10569 original-namespace-name
10570 namespace-alias
10571
10572 Returns the NAMESPACE_DECL for the namespace. */
10573
10574 static tree
cp_parser_namespace_name(cp_parser * parser)10575 cp_parser_namespace_name (cp_parser* parser)
10576 {
10577 tree identifier;
10578 tree namespace_decl;
10579
10580 /* Get the name of the namespace. */
10581 identifier = cp_parser_identifier (parser);
10582 if (identifier == error_mark_node)
10583 return error_mark_node;
10584
10585 /* Look up the identifier in the currently active scope. Look only
10586 for namespaces, due to:
10587
10588 [basic.lookup.udir]
10589
10590 When looking up a namespace-name in a using-directive or alias
10591 definition, only namespace names are considered.
10592
10593 And:
10594
10595 [basic.lookup.qual]
10596
10597 During the lookup of a name preceding the :: scope resolution
10598 operator, object, function, and enumerator names are ignored.
10599
10600 (Note that cp_parser_class_or_namespace_name only calls this
10601 function if the token after the name is the scope resolution
10602 operator.) */
10603 namespace_decl = cp_parser_lookup_name (parser, identifier,
10604 none_type,
10605 /*is_template=*/false,
10606 /*is_namespace=*/true,
10607 /*check_dependency=*/true,
10608 /*ambiguous_decls=*/NULL);
10609 /* If it's not a namespace, issue an error. */
10610 if (namespace_decl == error_mark_node
10611 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10612 {
10613 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10614 error ("%qD is not a namespace-name", identifier);
10615 cp_parser_error (parser, "expected namespace-name");
10616 namespace_decl = error_mark_node;
10617 }
10618
10619 return namespace_decl;
10620 }
10621
10622 /* Parse a namespace-definition.
10623
10624 namespace-definition:
10625 named-namespace-definition
10626 unnamed-namespace-definition
10627
10628 named-namespace-definition:
10629 original-namespace-definition
10630 extension-namespace-definition
10631
10632 original-namespace-definition:
10633 namespace identifier { namespace-body }
10634
10635 extension-namespace-definition:
10636 namespace original-namespace-name { namespace-body }
10637
10638 unnamed-namespace-definition:
10639 namespace { namespace-body } */
10640
10641 static void
cp_parser_namespace_definition(cp_parser * parser)10642 cp_parser_namespace_definition (cp_parser* parser)
10643 {
10644 tree identifier, attribs;
10645
10646 /* Look for the `namespace' keyword. */
10647 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10648
10649 /* Get the name of the namespace. We do not attempt to distinguish
10650 between an original-namespace-definition and an
10651 extension-namespace-definition at this point. The semantic
10652 analysis routines are responsible for that. */
10653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10654 identifier = cp_parser_identifier (parser);
10655 else
10656 identifier = NULL_TREE;
10657
10658 /* Parse any specified attributes. */
10659 attribs = cp_parser_attributes_opt (parser);
10660
10661 /* Look for the `{' to start the namespace. */
10662 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10663 /* Start the namespace. */
10664 push_namespace_with_attribs (identifier, attribs);
10665 /* Parse the body of the namespace. */
10666 cp_parser_namespace_body (parser);
10667 /* Finish the namespace. */
10668 pop_namespace ();
10669 /* Look for the final `}'. */
10670 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10671 }
10672
10673 /* Parse a namespace-body.
10674
10675 namespace-body:
10676 declaration-seq [opt] */
10677
10678 static void
cp_parser_namespace_body(cp_parser * parser)10679 cp_parser_namespace_body (cp_parser* parser)
10680 {
10681 cp_parser_declaration_seq_opt (parser);
10682 }
10683
10684 /* Parse a namespace-alias-definition.
10685
10686 namespace-alias-definition:
10687 namespace identifier = qualified-namespace-specifier ; */
10688
10689 static void
cp_parser_namespace_alias_definition(cp_parser * parser)10690 cp_parser_namespace_alias_definition (cp_parser* parser)
10691 {
10692 tree identifier;
10693 tree namespace_specifier;
10694
10695 /* Look for the `namespace' keyword. */
10696 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10697 /* Look for the identifier. */
10698 identifier = cp_parser_identifier (parser);
10699 if (identifier == error_mark_node)
10700 return;
10701 /* Look for the `=' token. */
10702 cp_parser_require (parser, CPP_EQ, "`='");
10703 /* Look for the qualified-namespace-specifier. */
10704 namespace_specifier
10705 = cp_parser_qualified_namespace_specifier (parser);
10706 /* Look for the `;' token. */
10707 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10708
10709 /* Register the alias in the symbol table. */
10710 do_namespace_alias (identifier, namespace_specifier);
10711 }
10712
10713 /* Parse a qualified-namespace-specifier.
10714
10715 qualified-namespace-specifier:
10716 :: [opt] nested-name-specifier [opt] namespace-name
10717
10718 Returns a NAMESPACE_DECL corresponding to the specified
10719 namespace. */
10720
10721 static tree
cp_parser_qualified_namespace_specifier(cp_parser * parser)10722 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10723 {
10724 /* Look for the optional `::'. */
10725 cp_parser_global_scope_opt (parser,
10726 /*current_scope_valid_p=*/false);
10727
10728 /* Look for the optional nested-name-specifier. */
10729 cp_parser_nested_name_specifier_opt (parser,
10730 /*typename_keyword_p=*/false,
10731 /*check_dependency_p=*/true,
10732 /*type_p=*/false,
10733 /*is_declaration=*/true);
10734
10735 return cp_parser_namespace_name (parser);
10736 }
10737
10738 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10739 access declaration.
10740
10741 using-declaration:
10742 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10743 using :: unqualified-id ;
10744
10745 access-declaration:
10746 qualified-id ;
10747
10748 */
10749
10750 static bool
cp_parser_using_declaration(cp_parser * parser,bool access_declaration_p)10751 cp_parser_using_declaration (cp_parser* parser,
10752 bool access_declaration_p)
10753 {
10754 cp_token *token;
10755 bool typename_p = false;
10756 bool global_scope_p;
10757 tree decl;
10758 tree identifier;
10759 tree qscope;
10760
10761 if (access_declaration_p)
10762 cp_parser_parse_tentatively (parser);
10763 else
10764 {
10765 /* Look for the `using' keyword. */
10766 cp_parser_require_keyword (parser, RID_USING, "`using'");
10767
10768 /* Peek at the next token. */
10769 token = cp_lexer_peek_token (parser->lexer);
10770 /* See if it's `typename'. */
10771 if (token->keyword == RID_TYPENAME)
10772 {
10773 /* Remember that we've seen it. */
10774 typename_p = true;
10775 /* Consume the `typename' token. */
10776 cp_lexer_consume_token (parser->lexer);
10777 }
10778 }
10779
10780 /* Look for the optional global scope qualification. */
10781 global_scope_p
10782 = (cp_parser_global_scope_opt (parser,
10783 /*current_scope_valid_p=*/false)
10784 != NULL_TREE);
10785
10786 /* If we saw `typename', or didn't see `::', then there must be a
10787 nested-name-specifier present. */
10788 if (typename_p || !global_scope_p)
10789 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10790 /*check_dependency_p=*/true,
10791 /*type_p=*/false,
10792 /*is_declaration=*/true);
10793 /* Otherwise, we could be in either of the two productions. In that
10794 case, treat the nested-name-specifier as optional. */
10795 else
10796 qscope = cp_parser_nested_name_specifier_opt (parser,
10797 /*typename_keyword_p=*/false,
10798 /*check_dependency_p=*/true,
10799 /*type_p=*/false,
10800 /*is_declaration=*/true);
10801 if (!qscope)
10802 qscope = global_namespace;
10803
10804 if (access_declaration_p && cp_parser_error_occurred (parser))
10805 /* Something has already gone wrong; there's no need to parse
10806 further. Since an error has occurred, the return value of
10807 cp_parser_parse_definitely will be false, as required. */
10808 return cp_parser_parse_definitely (parser);
10809
10810 /* Parse the unqualified-id. */
10811 identifier = cp_parser_unqualified_id (parser,
10812 /*template_keyword_p=*/false,
10813 /*check_dependency_p=*/true,
10814 /*declarator_p=*/true,
10815 /*optional_p=*/false);
10816
10817 if (access_declaration_p)
10818 {
10819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10820 cp_parser_simulate_error (parser);
10821 if (!cp_parser_parse_definitely (parser))
10822 return false;
10823 }
10824
10825 /* The function we call to handle a using-declaration is different
10826 depending on what scope we are in. */
10827 if (qscope == error_mark_node || identifier == error_mark_node)
10828 ;
10829 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10830 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10831 /* [namespace.udecl]
10832
10833 A using declaration shall not name a template-id. */
10834 error ("a template-id may not appear in a using-declaration");
10835 else
10836 {
10837 if (at_class_scope_p ())
10838 {
10839 /* Create the USING_DECL. */
10840 decl = do_class_using_decl (parser->scope, identifier);
10841 /* Add it to the list of members in this class. */
10842 finish_member_declaration (decl);
10843 }
10844 else
10845 {
10846 decl = cp_parser_lookup_name_simple (parser, identifier);
10847 if (decl == error_mark_node)
10848 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10849 else if (!at_namespace_scope_p ())
10850 do_local_using_decl (decl, qscope, identifier);
10851 else
10852 do_toplevel_using_decl (decl, qscope, identifier);
10853 }
10854 }
10855
10856 /* Look for the final `;'. */
10857 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10858
10859 return true;
10860 }
10861
10862 /* Parse a using-directive.
10863
10864 using-directive:
10865 using namespace :: [opt] nested-name-specifier [opt]
10866 namespace-name ; */
10867
10868 static void
cp_parser_using_directive(cp_parser * parser)10869 cp_parser_using_directive (cp_parser* parser)
10870 {
10871 tree namespace_decl;
10872 tree attribs;
10873
10874 /* Look for the `using' keyword. */
10875 cp_parser_require_keyword (parser, RID_USING, "`using'");
10876 /* And the `namespace' keyword. */
10877 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10878 /* Look for the optional `::' operator. */
10879 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10880 /* And the optional nested-name-specifier. */
10881 cp_parser_nested_name_specifier_opt (parser,
10882 /*typename_keyword_p=*/false,
10883 /*check_dependency_p=*/true,
10884 /*type_p=*/false,
10885 /*is_declaration=*/true);
10886 /* Get the namespace being used. */
10887 namespace_decl = cp_parser_namespace_name (parser);
10888 /* And any specified attributes. */
10889 attribs = cp_parser_attributes_opt (parser);
10890 /* Update the symbol table. */
10891 parse_using_directive (namespace_decl, attribs);
10892 /* Look for the final `;'. */
10893 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10894 }
10895
10896 /* Parse an asm-definition.
10897
10898 asm-definition:
10899 asm ( string-literal ) ;
10900
10901 GNU Extension:
10902
10903 asm-definition:
10904 asm volatile [opt] ( string-literal ) ;
10905 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10906 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10907 : asm-operand-list [opt] ) ;
10908 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10909 : asm-operand-list [opt]
10910 : asm-operand-list [opt] ) ; */
10911
10912 static void
cp_parser_asm_definition(cp_parser * parser)10913 cp_parser_asm_definition (cp_parser* parser)
10914 {
10915 tree string;
10916 tree outputs = NULL_TREE;
10917 tree inputs = NULL_TREE;
10918 tree clobbers = NULL_TREE;
10919 tree asm_stmt;
10920 bool volatile_p = false;
10921 bool extended_p = false;
10922
10923 /* Look for the `asm' keyword. */
10924 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10925 /* See if the next token is `volatile'. */
10926 if (cp_parser_allow_gnu_extensions_p (parser)
10927 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10928 {
10929 /* Remember that we saw the `volatile' keyword. */
10930 volatile_p = true;
10931 /* Consume the token. */
10932 cp_lexer_consume_token (parser->lexer);
10933 }
10934 /* Look for the opening `('. */
10935 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10936 return;
10937 /* Look for the string. */
10938 string = cp_parser_string_literal (parser, false, false);
10939 if (string == error_mark_node)
10940 {
10941 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10942 /*consume_paren=*/true);
10943 return;
10944 }
10945
10946 /* If we're allowing GNU extensions, check for the extended assembly
10947 syntax. Unfortunately, the `:' tokens need not be separated by
10948 a space in C, and so, for compatibility, we tolerate that here
10949 too. Doing that means that we have to treat the `::' operator as
10950 two `:' tokens. */
10951 if (cp_parser_allow_gnu_extensions_p (parser)
10952 && parser->in_function_body
10953 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10954 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10955 {
10956 bool inputs_p = false;
10957 bool clobbers_p = false;
10958
10959 /* The extended syntax was used. */
10960 extended_p = true;
10961
10962 /* Look for outputs. */
10963 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10964 {
10965 /* Consume the `:'. */
10966 cp_lexer_consume_token (parser->lexer);
10967 /* Parse the output-operands. */
10968 if (cp_lexer_next_token_is_not (parser->lexer,
10969 CPP_COLON)
10970 && cp_lexer_next_token_is_not (parser->lexer,
10971 CPP_SCOPE)
10972 && cp_lexer_next_token_is_not (parser->lexer,
10973 CPP_CLOSE_PAREN))
10974 outputs = cp_parser_asm_operand_list (parser);
10975 }
10976 /* If the next token is `::', there are no outputs, and the
10977 next token is the beginning of the inputs. */
10978 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10979 /* The inputs are coming next. */
10980 inputs_p = true;
10981
10982 /* Look for inputs. */
10983 if (inputs_p
10984 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10985 {
10986 /* Consume the `:' or `::'. */
10987 cp_lexer_consume_token (parser->lexer);
10988 /* Parse the output-operands. */
10989 if (cp_lexer_next_token_is_not (parser->lexer,
10990 CPP_COLON)
10991 && cp_lexer_next_token_is_not (parser->lexer,
10992 CPP_CLOSE_PAREN))
10993 inputs = cp_parser_asm_operand_list (parser);
10994 }
10995 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10996 /* The clobbers are coming next. */
10997 clobbers_p = true;
10998
10999 /* Look for clobbers. */
11000 if (clobbers_p
11001 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11002 {
11003 /* Consume the `:' or `::'. */
11004 cp_lexer_consume_token (parser->lexer);
11005 /* Parse the clobbers. */
11006 if (cp_lexer_next_token_is_not (parser->lexer,
11007 CPP_CLOSE_PAREN))
11008 clobbers = cp_parser_asm_clobber_list (parser);
11009 }
11010 }
11011 /* Look for the closing `)'. */
11012 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11013 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11014 /*consume_paren=*/true);
11015 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11016
11017 /* Create the ASM_EXPR. */
11018 if (parser->in_function_body)
11019 {
11020 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11021 inputs, clobbers);
11022 /* If the extended syntax was not used, mark the ASM_EXPR. */
11023 if (!extended_p)
11024 {
11025 tree temp = asm_stmt;
11026 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11027 temp = TREE_OPERAND (temp, 0);
11028
11029 ASM_INPUT_P (temp) = 1;
11030 }
11031 }
11032 else
11033 cgraph_add_asm_node (string);
11034 }
11035
11036 /* Declarators [gram.dcl.decl] */
11037
11038 /* Parse an init-declarator.
11039
11040 init-declarator:
11041 declarator initializer [opt]
11042
11043 GNU Extension:
11044
11045 init-declarator:
11046 declarator asm-specification [opt] attributes [opt] initializer [opt]
11047
11048 function-definition:
11049 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11050 function-body
11051 decl-specifier-seq [opt] declarator function-try-block
11052
11053 GNU Extension:
11054
11055 function-definition:
11056 __extension__ function-definition
11057
11058 The DECL_SPECIFIERS apply to this declarator. Returns a
11059 representation of the entity declared. If MEMBER_P is TRUE, then
11060 this declarator appears in a class scope. The new DECL created by
11061 this declarator is returned.
11062
11063 The CHECKS are access checks that should be performed once we know
11064 what entity is being declared (and, therefore, what classes have
11065 befriended it).
11066
11067 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11068 for a function-definition here as well. If the declarator is a
11069 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11070 be TRUE upon return. By that point, the function-definition will
11071 have been completely parsed.
11072
11073 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11074 is FALSE. */
11075
11076 static tree
cp_parser_init_declarator(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,VEC (deferred_access_check,gc)* checks,bool function_definition_allowed_p,bool member_p,int declares_class_or_enum,bool * function_definition_p)11077 cp_parser_init_declarator (cp_parser* parser,
11078 cp_decl_specifier_seq *decl_specifiers,
11079 VEC (deferred_access_check,gc)* checks,
11080 bool function_definition_allowed_p,
11081 bool member_p,
11082 int declares_class_or_enum,
11083 bool* function_definition_p)
11084 {
11085 cp_token *token;
11086 cp_declarator *declarator;
11087 tree prefix_attributes;
11088 tree attributes;
11089 tree asm_specification;
11090 tree initializer;
11091 tree decl = NULL_TREE;
11092 tree scope;
11093 bool is_initialized;
11094 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11095 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11096 "(...)". */
11097 enum cpp_ttype initialization_kind;
11098 bool is_parenthesized_init = false;
11099 bool is_non_constant_init;
11100 int ctor_dtor_or_conv_p;
11101 bool friend_p;
11102 tree pushed_scope = NULL;
11103
11104 /* Gather the attributes that were provided with the
11105 decl-specifiers. */
11106 prefix_attributes = decl_specifiers->attributes;
11107
11108 /* Assume that this is not the declarator for a function
11109 definition. */
11110 if (function_definition_p)
11111 *function_definition_p = false;
11112
11113 /* Defer access checks while parsing the declarator; we cannot know
11114 what names are accessible until we know what is being
11115 declared. */
11116 resume_deferring_access_checks ();
11117
11118 /* Parse the declarator. */
11119 declarator
11120 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11121 &ctor_dtor_or_conv_p,
11122 /*parenthesized_p=*/NULL,
11123 /*member_p=*/false);
11124 /* Gather up the deferred checks. */
11125 stop_deferring_access_checks ();
11126
11127 /* If the DECLARATOR was erroneous, there's no need to go
11128 further. */
11129 if (declarator == cp_error_declarator)
11130 return error_mark_node;
11131
11132 /* Check that the number of template-parameter-lists is OK. */
11133 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11134 return error_mark_node;
11135
11136 if (declares_class_or_enum & 2)
11137 cp_parser_check_for_definition_in_return_type (declarator,
11138 decl_specifiers->type);
11139
11140 /* Figure out what scope the entity declared by the DECLARATOR is
11141 located in. `grokdeclarator' sometimes changes the scope, so
11142 we compute it now. */
11143 scope = get_scope_of_declarator (declarator);
11144
11145 /* If we're allowing GNU extensions, look for an asm-specification
11146 and attributes. */
11147 if (cp_parser_allow_gnu_extensions_p (parser))
11148 {
11149 /* Look for an asm-specification. */
11150 asm_specification = cp_parser_asm_specification_opt (parser);
11151 /* And attributes. */
11152 attributes = cp_parser_attributes_opt (parser);
11153 }
11154 else
11155 {
11156 asm_specification = NULL_TREE;
11157 attributes = NULL_TREE;
11158 }
11159
11160 /* Peek at the next token. */
11161 token = cp_lexer_peek_token (parser->lexer);
11162 /* Check to see if the token indicates the start of a
11163 function-definition. */
11164 if (cp_parser_token_starts_function_definition_p (token))
11165 {
11166 if (!function_definition_allowed_p)
11167 {
11168 /* If a function-definition should not appear here, issue an
11169 error message. */
11170 cp_parser_error (parser,
11171 "a function-definition is not allowed here");
11172 return error_mark_node;
11173 }
11174 else
11175 {
11176 /* Neither attributes nor an asm-specification are allowed
11177 on a function-definition. */
11178 if (asm_specification)
11179 error ("an asm-specification is not allowed on a function-definition");
11180 if (attributes)
11181 error ("attributes are not allowed on a function-definition");
11182 /* This is a function-definition. */
11183 *function_definition_p = true;
11184
11185 /* Parse the function definition. */
11186 if (member_p)
11187 decl = cp_parser_save_member_function_body (parser,
11188 decl_specifiers,
11189 declarator,
11190 prefix_attributes);
11191 else
11192 decl
11193 = (cp_parser_function_definition_from_specifiers_and_declarator
11194 (parser, decl_specifiers, prefix_attributes, declarator));
11195
11196 return decl;
11197 }
11198 }
11199
11200 /* [dcl.dcl]
11201
11202 Only in function declarations for constructors, destructors, and
11203 type conversions can the decl-specifier-seq be omitted.
11204
11205 We explicitly postpone this check past the point where we handle
11206 function-definitions because we tolerate function-definitions
11207 that are missing their return types in some modes. */
11208 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11209 {
11210 cp_parser_error (parser,
11211 "expected constructor, destructor, or type conversion");
11212 return error_mark_node;
11213 }
11214
11215 /* An `=' or an `(' indicates an initializer. */
11216 if (token->type == CPP_EQ
11217 || token->type == CPP_OPEN_PAREN)
11218 {
11219 is_initialized = true;
11220 initialization_kind = token->type;
11221 }
11222 else
11223 {
11224 /* If the init-declarator isn't initialized and isn't followed by a
11225 `,' or `;', it's not a valid init-declarator. */
11226 if (token->type != CPP_COMMA
11227 && token->type != CPP_SEMICOLON)
11228 {
11229 cp_parser_error (parser, "expected initializer");
11230 return error_mark_node;
11231 }
11232 is_initialized = false;
11233 initialization_kind = CPP_EOF;
11234 }
11235
11236 /* Because start_decl has side-effects, we should only call it if we
11237 know we're going ahead. By this point, we know that we cannot
11238 possibly be looking at any other construct. */
11239 cp_parser_commit_to_tentative_parse (parser);
11240
11241 /* If the decl specifiers were bad, issue an error now that we're
11242 sure this was intended to be a declarator. Then continue
11243 declaring the variable(s), as int, to try to cut down on further
11244 errors. */
11245 if (decl_specifiers->any_specifiers_p
11246 && decl_specifiers->type == error_mark_node)
11247 {
11248 cp_parser_error (parser, "invalid type in declaration");
11249 decl_specifiers->type = integer_type_node;
11250 }
11251
11252 /* Check to see whether or not this declaration is a friend. */
11253 friend_p = cp_parser_friend_p (decl_specifiers);
11254
11255 /* Enter the newly declared entry in the symbol table. If we're
11256 processing a declaration in a class-specifier, we wait until
11257 after processing the initializer. */
11258 if (!member_p)
11259 {
11260 if (parser->in_unbraced_linkage_specification_p)
11261 decl_specifiers->storage_class = sc_extern;
11262 decl = start_decl (declarator, decl_specifiers,
11263 is_initialized, attributes, prefix_attributes,
11264 &pushed_scope);
11265 }
11266 else if (scope)
11267 /* Enter the SCOPE. That way unqualified names appearing in the
11268 initializer will be looked up in SCOPE. */
11269 pushed_scope = push_scope (scope);
11270
11271 /* Perform deferred access control checks, now that we know in which
11272 SCOPE the declared entity resides. */
11273 if (!member_p && decl)
11274 {
11275 tree saved_current_function_decl = NULL_TREE;
11276
11277 /* If the entity being declared is a function, pretend that we
11278 are in its scope. If it is a `friend', it may have access to
11279 things that would not otherwise be accessible. */
11280 if (TREE_CODE (decl) == FUNCTION_DECL)
11281 {
11282 saved_current_function_decl = current_function_decl;
11283 current_function_decl = decl;
11284 }
11285
11286 /* Perform access checks for template parameters. */
11287 cp_parser_perform_template_parameter_access_checks (checks);
11288
11289 /* Perform the access control checks for the declarator and the
11290 the decl-specifiers. */
11291 perform_deferred_access_checks ();
11292
11293 /* Restore the saved value. */
11294 if (TREE_CODE (decl) == FUNCTION_DECL)
11295 current_function_decl = saved_current_function_decl;
11296 }
11297
11298 /* Parse the initializer. */
11299 initializer = NULL_TREE;
11300 is_parenthesized_init = false;
11301 is_non_constant_init = true;
11302 if (is_initialized)
11303 {
11304 if (function_declarator_p (declarator))
11305 {
11306 if (initialization_kind == CPP_EQ)
11307 initializer = cp_parser_pure_specifier (parser);
11308 else
11309 {
11310 /* If the declaration was erroneous, we don't really
11311 know what the user intended, so just silently
11312 consume the initializer. */
11313 if (decl != error_mark_node)
11314 error ("initializer provided for function");
11315 cp_parser_skip_to_closing_parenthesis (parser,
11316 /*recovering=*/true,
11317 /*or_comma=*/false,
11318 /*consume_paren=*/true);
11319 }
11320 }
11321 else
11322 initializer = cp_parser_initializer (parser,
11323 &is_parenthesized_init,
11324 &is_non_constant_init);
11325 }
11326
11327 /* The old parser allows attributes to appear after a parenthesized
11328 initializer. Mark Mitchell proposed removing this functionality
11329 on the GCC mailing lists on 2002-08-13. This parser accepts the
11330 attributes -- but ignores them. */
11331 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11332 if (cp_parser_attributes_opt (parser))
11333 warning (OPT_Wattributes,
11334 "attributes after parenthesized initializer ignored");
11335
11336 /* For an in-class declaration, use `grokfield' to create the
11337 declaration. */
11338 if (member_p)
11339 {
11340 if (pushed_scope)
11341 {
11342 pop_scope (pushed_scope);
11343 pushed_scope = false;
11344 }
11345 decl = grokfield (declarator, decl_specifiers,
11346 initializer, !is_non_constant_init,
11347 /*asmspec=*/NULL_TREE,
11348 prefix_attributes);
11349 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11350 cp_parser_save_default_args (parser, decl);
11351 }
11352
11353 /* Finish processing the declaration. But, skip friend
11354 declarations. */
11355 if (!friend_p && decl && decl != error_mark_node)
11356 {
11357 cp_finish_decl (decl,
11358 initializer, !is_non_constant_init,
11359 asm_specification,
11360 /* If the initializer is in parentheses, then this is
11361 a direct-initialization, which means that an
11362 `explicit' constructor is OK. Otherwise, an
11363 `explicit' constructor cannot be used. */
11364 ((is_parenthesized_init || !is_initialized)
11365 ? 0 : LOOKUP_ONLYCONVERTING));
11366 }
11367 if (!friend_p && pushed_scope)
11368 pop_scope (pushed_scope);
11369
11370 return decl;
11371 }
11372
11373 /* Parse a declarator.
11374
11375 declarator:
11376 direct-declarator
11377 ptr-operator declarator
11378
11379 abstract-declarator:
11380 ptr-operator abstract-declarator [opt]
11381 direct-abstract-declarator
11382
11383 GNU Extensions:
11384
11385 declarator:
11386 attributes [opt] direct-declarator
11387 attributes [opt] ptr-operator declarator
11388
11389 abstract-declarator:
11390 attributes [opt] ptr-operator abstract-declarator [opt]
11391 attributes [opt] direct-abstract-declarator
11392
11393 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11394 detect constructor, destructor or conversion operators. It is set
11395 to -1 if the declarator is a name, and +1 if it is a
11396 function. Otherwise it is set to zero. Usually you just want to
11397 test for >0, but internally the negative value is used.
11398
11399 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11400 a decl-specifier-seq unless it declares a constructor, destructor,
11401 or conversion. It might seem that we could check this condition in
11402 semantic analysis, rather than parsing, but that makes it difficult
11403 to handle something like `f()'. We want to notice that there are
11404 no decl-specifiers, and therefore realize that this is an
11405 expression, not a declaration.)
11406
11407 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11408 the declarator is a direct-declarator of the form "(...)".
11409
11410 MEMBER_P is true iff this declarator is a member-declarator. */
11411
11412 static cp_declarator *
cp_parser_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p,bool * parenthesized_p,bool member_p)11413 cp_parser_declarator (cp_parser* parser,
11414 cp_parser_declarator_kind dcl_kind,
11415 int* ctor_dtor_or_conv_p,
11416 bool* parenthesized_p,
11417 bool member_p)
11418 {
11419 cp_token *token;
11420 cp_declarator *declarator;
11421 enum tree_code code;
11422 cp_cv_quals cv_quals;
11423 tree class_type;
11424 tree attributes = NULL_TREE;
11425
11426 /* Assume this is not a constructor, destructor, or type-conversion
11427 operator. */
11428 if (ctor_dtor_or_conv_p)
11429 *ctor_dtor_or_conv_p = 0;
11430
11431 if (cp_parser_allow_gnu_extensions_p (parser))
11432 attributes = cp_parser_attributes_opt (parser);
11433
11434 /* Peek at the next token. */
11435 token = cp_lexer_peek_token (parser->lexer);
11436
11437 /* Check for the ptr-operator production. */
11438 cp_parser_parse_tentatively (parser);
11439 /* Parse the ptr-operator. */
11440 code = cp_parser_ptr_operator (parser,
11441 &class_type,
11442 &cv_quals);
11443 /* If that worked, then we have a ptr-operator. */
11444 if (cp_parser_parse_definitely (parser))
11445 {
11446 /* If a ptr-operator was found, then this declarator was not
11447 parenthesized. */
11448 if (parenthesized_p)
11449 *parenthesized_p = true;
11450 /* The dependent declarator is optional if we are parsing an
11451 abstract-declarator. */
11452 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11453 cp_parser_parse_tentatively (parser);
11454
11455 /* Parse the dependent declarator. */
11456 declarator = cp_parser_declarator (parser, dcl_kind,
11457 /*ctor_dtor_or_conv_p=*/NULL,
11458 /*parenthesized_p=*/NULL,
11459 /*member_p=*/false);
11460
11461 /* If we are parsing an abstract-declarator, we must handle the
11462 case where the dependent declarator is absent. */
11463 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11464 && !cp_parser_parse_definitely (parser))
11465 declarator = NULL;
11466
11467 /* Build the representation of the ptr-operator. */
11468 if (class_type)
11469 declarator = make_ptrmem_declarator (cv_quals,
11470 class_type,
11471 declarator);
11472 else if (code == INDIRECT_REF)
11473 declarator = make_pointer_declarator (cv_quals, declarator);
11474 else
11475 declarator = make_reference_declarator (cv_quals, declarator);
11476 }
11477 /* Everything else is a direct-declarator. */
11478 else
11479 {
11480 if (parenthesized_p)
11481 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11482 CPP_OPEN_PAREN);
11483 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11484 ctor_dtor_or_conv_p,
11485 member_p);
11486 }
11487
11488 if (attributes && declarator && declarator != cp_error_declarator)
11489 declarator->attributes = attributes;
11490
11491 return declarator;
11492 }
11493
11494 /* Parse a direct-declarator or direct-abstract-declarator.
11495
11496 direct-declarator:
11497 declarator-id
11498 direct-declarator ( parameter-declaration-clause )
11499 cv-qualifier-seq [opt]
11500 exception-specification [opt]
11501 direct-declarator [ constant-expression [opt] ]
11502 ( declarator )
11503
11504 direct-abstract-declarator:
11505 direct-abstract-declarator [opt]
11506 ( parameter-declaration-clause )
11507 cv-qualifier-seq [opt]
11508 exception-specification [opt]
11509 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11510 ( abstract-declarator )
11511
11512 Returns a representation of the declarator. DCL_KIND is
11513 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11514 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11515 we are parsing a direct-declarator. It is
11516 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11517 of ambiguity we prefer an abstract declarator, as per
11518 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11519 cp_parser_declarator. */
11520
11521 static cp_declarator *
cp_parser_direct_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p,bool member_p)11522 cp_parser_direct_declarator (cp_parser* parser,
11523 cp_parser_declarator_kind dcl_kind,
11524 int* ctor_dtor_or_conv_p,
11525 bool member_p)
11526 {
11527 cp_token *token;
11528 cp_declarator *declarator = NULL;
11529 tree scope = NULL_TREE;
11530 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11531 bool saved_in_declarator_p = parser->in_declarator_p;
11532 bool first = true;
11533 tree pushed_scope = NULL_TREE;
11534
11535 while (true)
11536 {
11537 /* Peek at the next token. */
11538 token = cp_lexer_peek_token (parser->lexer);
11539 if (token->type == CPP_OPEN_PAREN)
11540 {
11541 /* This is either a parameter-declaration-clause, or a
11542 parenthesized declarator. When we know we are parsing a
11543 named declarator, it must be a parenthesized declarator
11544 if FIRST is true. For instance, `(int)' is a
11545 parameter-declaration-clause, with an omitted
11546 direct-abstract-declarator. But `((*))', is a
11547 parenthesized abstract declarator. Finally, when T is a
11548 template parameter `(T)' is a
11549 parameter-declaration-clause, and not a parenthesized
11550 named declarator.
11551
11552 We first try and parse a parameter-declaration-clause,
11553 and then try a nested declarator (if FIRST is true).
11554
11555 It is not an error for it not to be a
11556 parameter-declaration-clause, even when FIRST is
11557 false. Consider,
11558
11559 int i (int);
11560 int i (3);
11561
11562 The first is the declaration of a function while the
11563 second is a the definition of a variable, including its
11564 initializer.
11565
11566 Having seen only the parenthesis, we cannot know which of
11567 these two alternatives should be selected. Even more
11568 complex are examples like:
11569
11570 int i (int (a));
11571 int i (int (3));
11572
11573 The former is a function-declaration; the latter is a
11574 variable initialization.
11575
11576 Thus again, we try a parameter-declaration-clause, and if
11577 that fails, we back out and return. */
11578
11579 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11580 {
11581 cp_parameter_declarator *params;
11582 unsigned saved_num_template_parameter_lists;
11583
11584 /* In a member-declarator, the only valid interpretation
11585 of a parenthesis is the start of a
11586 parameter-declaration-clause. (It is invalid to
11587 initialize a static data member with a parenthesized
11588 initializer; only the "=" form of initialization is
11589 permitted.) */
11590 if (!member_p)
11591 cp_parser_parse_tentatively (parser);
11592
11593 /* Consume the `('. */
11594 cp_lexer_consume_token (parser->lexer);
11595 if (first)
11596 {
11597 /* If this is going to be an abstract declarator, we're
11598 in a declarator and we can't have default args. */
11599 parser->default_arg_ok_p = false;
11600 parser->in_declarator_p = true;
11601 }
11602
11603 /* Inside the function parameter list, surrounding
11604 template-parameter-lists do not apply. */
11605 saved_num_template_parameter_lists
11606 = parser->num_template_parameter_lists;
11607 parser->num_template_parameter_lists = 0;
11608
11609 /* Parse the parameter-declaration-clause. */
11610 params = cp_parser_parameter_declaration_clause (parser);
11611
11612 parser->num_template_parameter_lists
11613 = saved_num_template_parameter_lists;
11614
11615 /* If all went well, parse the cv-qualifier-seq and the
11616 exception-specification. */
11617 if (member_p || cp_parser_parse_definitely (parser))
11618 {
11619 cp_cv_quals cv_quals;
11620 tree exception_specification;
11621
11622 if (ctor_dtor_or_conv_p)
11623 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11624 first = false;
11625 /* Consume the `)'. */
11626 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11627
11628 /* Parse the cv-qualifier-seq. */
11629 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11630 /* And the exception-specification. */
11631 exception_specification
11632 = cp_parser_exception_specification_opt (parser);
11633
11634 /* Create the function-declarator. */
11635 declarator = make_call_declarator (declarator,
11636 params,
11637 cv_quals,
11638 exception_specification);
11639 /* Any subsequent parameter lists are to do with
11640 return type, so are not those of the declared
11641 function. */
11642 parser->default_arg_ok_p = false;
11643
11644 /* Repeat the main loop. */
11645 continue;
11646 }
11647 }
11648
11649 /* If this is the first, we can try a parenthesized
11650 declarator. */
11651 if (first)
11652 {
11653 bool saved_in_type_id_in_expr_p;
11654
11655 parser->default_arg_ok_p = saved_default_arg_ok_p;
11656 parser->in_declarator_p = saved_in_declarator_p;
11657
11658 /* Consume the `('. */
11659 cp_lexer_consume_token (parser->lexer);
11660 /* Parse the nested declarator. */
11661 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11662 parser->in_type_id_in_expr_p = true;
11663 declarator
11664 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11665 /*parenthesized_p=*/NULL,
11666 member_p);
11667 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11668 first = false;
11669 /* Expect a `)'. */
11670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11671 declarator = cp_error_declarator;
11672 if (declarator == cp_error_declarator)
11673 break;
11674
11675 goto handle_declarator;
11676 }
11677 /* Otherwise, we must be done. */
11678 else
11679 break;
11680 }
11681 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11682 && token->type == CPP_OPEN_SQUARE)
11683 {
11684 /* Parse an array-declarator. */
11685 tree bounds;
11686
11687 if (ctor_dtor_or_conv_p)
11688 *ctor_dtor_or_conv_p = 0;
11689
11690 first = false;
11691 parser->default_arg_ok_p = false;
11692 parser->in_declarator_p = true;
11693 /* Consume the `['. */
11694 cp_lexer_consume_token (parser->lexer);
11695 /* Peek at the next token. */
11696 token = cp_lexer_peek_token (parser->lexer);
11697 /* If the next token is `]', then there is no
11698 constant-expression. */
11699 if (token->type != CPP_CLOSE_SQUARE)
11700 {
11701 bool non_constant_p;
11702
11703 bounds
11704 = cp_parser_constant_expression (parser,
11705 /*allow_non_constant=*/true,
11706 &non_constant_p);
11707 if (!non_constant_p)
11708 bounds = fold_non_dependent_expr (bounds);
11709 /* Normally, the array bound must be an integral constant
11710 expression. However, as an extension, we allow VLAs
11711 in function scopes. */
11712 else if (!parser->in_function_body)
11713 {
11714 error ("array bound is not an integer constant");
11715 bounds = error_mark_node;
11716 }
11717 }
11718 else
11719 bounds = NULL_TREE;
11720 /* Look for the closing `]'. */
11721 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11722 {
11723 declarator = cp_error_declarator;
11724 break;
11725 }
11726
11727 declarator = make_array_declarator (declarator, bounds);
11728 }
11729 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11730 {
11731 tree qualifying_scope;
11732 tree unqualified_name;
11733 special_function_kind sfk;
11734 bool abstract_ok;
11735
11736 /* Parse a declarator-id */
11737 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11738 if (abstract_ok)
11739 cp_parser_parse_tentatively (parser);
11740 unqualified_name
11741 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11742 qualifying_scope = parser->scope;
11743 if (abstract_ok)
11744 {
11745 if (!cp_parser_parse_definitely (parser))
11746 unqualified_name = error_mark_node;
11747 else if (unqualified_name
11748 && (qualifying_scope
11749 || (TREE_CODE (unqualified_name)
11750 != IDENTIFIER_NODE)))
11751 {
11752 cp_parser_error (parser, "expected unqualified-id");
11753 unqualified_name = error_mark_node;
11754 }
11755 }
11756
11757 if (!unqualified_name)
11758 return NULL;
11759 if (unqualified_name == error_mark_node)
11760 {
11761 declarator = cp_error_declarator;
11762 break;
11763 }
11764
11765 if (qualifying_scope && at_namespace_scope_p ()
11766 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11767 {
11768 /* In the declaration of a member of a template class
11769 outside of the class itself, the SCOPE will sometimes
11770 be a TYPENAME_TYPE. For example, given:
11771
11772 template <typename T>
11773 int S<T>::R::i = 3;
11774
11775 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11776 this context, we must resolve S<T>::R to an ordinary
11777 type, rather than a typename type.
11778
11779 The reason we normally avoid resolving TYPENAME_TYPEs
11780 is that a specialization of `S' might render
11781 `S<T>::R' not a type. However, if `S' is
11782 specialized, then this `i' will not be used, so there
11783 is no harm in resolving the types here. */
11784 tree type;
11785
11786 /* Resolve the TYPENAME_TYPE. */
11787 type = resolve_typename_type (qualifying_scope,
11788 /*only_current_p=*/false);
11789 /* If that failed, the declarator is invalid. */
11790 if (type == error_mark_node)
11791 error ("%<%T::%D%> is not a type",
11792 TYPE_CONTEXT (qualifying_scope),
11793 TYPE_IDENTIFIER (qualifying_scope));
11794 qualifying_scope = type;
11795 }
11796
11797 sfk = sfk_none;
11798 if (unqualified_name)
11799 {
11800 tree class_type;
11801
11802 if (qualifying_scope
11803 && CLASS_TYPE_P (qualifying_scope))
11804 class_type = qualifying_scope;
11805 else
11806 class_type = current_class_type;
11807
11808 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11809 {
11810 tree name_type = TREE_TYPE (unqualified_name);
11811 if (class_type && same_type_p (name_type, class_type))
11812 {
11813 if (qualifying_scope
11814 && CLASSTYPE_USE_TEMPLATE (name_type))
11815 {
11816 error ("invalid use of constructor as a template");
11817 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11818 "name the constructor in a qualified name",
11819 class_type,
11820 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11821 class_type, name_type);
11822 declarator = cp_error_declarator;
11823 break;
11824 }
11825 else
11826 unqualified_name = constructor_name (class_type);
11827 }
11828 else
11829 {
11830 /* We do not attempt to print the declarator
11831 here because we do not have enough
11832 information about its original syntactic
11833 form. */
11834 cp_parser_error (parser, "invalid declarator");
11835 declarator = cp_error_declarator;
11836 break;
11837 }
11838 }
11839
11840 if (class_type)
11841 {
11842 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11843 sfk = sfk_destructor;
11844 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11845 sfk = sfk_conversion;
11846 else if (/* There's no way to declare a constructor
11847 for an anonymous type, even if the type
11848 got a name for linkage purposes. */
11849 !TYPE_WAS_ANONYMOUS (class_type)
11850 && constructor_name_p (unqualified_name,
11851 class_type))
11852 {
11853 unqualified_name = constructor_name (class_type);
11854 sfk = sfk_constructor;
11855 }
11856
11857 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11858 *ctor_dtor_or_conv_p = -1;
11859 }
11860 }
11861 declarator = make_id_declarator (qualifying_scope,
11862 unqualified_name,
11863 sfk);
11864 declarator->id_loc = token->location;
11865
11866 handle_declarator:;
11867 scope = get_scope_of_declarator (declarator);
11868 if (scope)
11869 /* Any names that appear after the declarator-id for a
11870 member are looked up in the containing scope. */
11871 pushed_scope = push_scope (scope);
11872 parser->in_declarator_p = true;
11873 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11874 || (declarator && declarator->kind == cdk_id))
11875 /* Default args are only allowed on function
11876 declarations. */
11877 parser->default_arg_ok_p = saved_default_arg_ok_p;
11878 else
11879 parser->default_arg_ok_p = false;
11880
11881 first = false;
11882 }
11883 /* We're done. */
11884 else
11885 break;
11886 }
11887
11888 /* For an abstract declarator, we might wind up with nothing at this
11889 point. That's an error; the declarator is not optional. */
11890 if (!declarator)
11891 cp_parser_error (parser, "expected declarator");
11892
11893 /* If we entered a scope, we must exit it now. */
11894 if (pushed_scope)
11895 pop_scope (pushed_scope);
11896
11897 parser->default_arg_ok_p = saved_default_arg_ok_p;
11898 parser->in_declarator_p = saved_in_declarator_p;
11899
11900 return declarator;
11901 }
11902
11903 /* Parse a ptr-operator.
11904
11905 ptr-operator:
11906 * cv-qualifier-seq [opt]
11907 &
11908 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11909
11910 GNU Extension:
11911
11912 ptr-operator:
11913 & cv-qualifier-seq [opt]
11914
11915 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11916 Returns ADDR_EXPR if a reference was used. In the case of a
11917 pointer-to-member, *TYPE is filled in with the TYPE containing the
11918 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11919 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11920 ERROR_MARK if an error occurred. */
11921
11922 static enum tree_code
cp_parser_ptr_operator(cp_parser * parser,tree * type,cp_cv_quals * cv_quals)11923 cp_parser_ptr_operator (cp_parser* parser,
11924 tree* type,
11925 cp_cv_quals *cv_quals)
11926 {
11927 enum tree_code code = ERROR_MARK;
11928 cp_token *token;
11929
11930 /* Assume that it's not a pointer-to-member. */
11931 *type = NULL_TREE;
11932 /* And that there are no cv-qualifiers. */
11933 *cv_quals = TYPE_UNQUALIFIED;
11934
11935 /* Peek at the next token. */
11936 token = cp_lexer_peek_token (parser->lexer);
11937 /* If it's a `*' or `&' we have a pointer or reference. */
11938 if (token->type == CPP_MULT || token->type == CPP_AND)
11939 {
11940 /* Remember which ptr-operator we were processing. */
11941 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11942
11943 /* Consume the `*' or `&'. */
11944 cp_lexer_consume_token (parser->lexer);
11945
11946 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11947 `&', if we are allowing GNU extensions. (The only qualifier
11948 that can legally appear after `&' is `restrict', but that is
11949 enforced during semantic analysis. */
11950 if (code == INDIRECT_REF
11951 || cp_parser_allow_gnu_extensions_p (parser))
11952 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11953 }
11954 else
11955 {
11956 /* Try the pointer-to-member case. */
11957 cp_parser_parse_tentatively (parser);
11958 /* Look for the optional `::' operator. */
11959 cp_parser_global_scope_opt (parser,
11960 /*current_scope_valid_p=*/false);
11961 /* Look for the nested-name specifier. */
11962 cp_parser_nested_name_specifier (parser,
11963 /*typename_keyword_p=*/false,
11964 /*check_dependency_p=*/true,
11965 /*type_p=*/false,
11966 /*is_declaration=*/false);
11967 /* If we found it, and the next token is a `*', then we are
11968 indeed looking at a pointer-to-member operator. */
11969 if (!cp_parser_error_occurred (parser)
11970 && cp_parser_require (parser, CPP_MULT, "`*'"))
11971 {
11972 /* Indicate that the `*' operator was used. */
11973 code = INDIRECT_REF;
11974
11975 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11976 error ("%qD is a namespace", parser->scope);
11977 else
11978 {
11979 /* The type of which the member is a member is given by the
11980 current SCOPE. */
11981 *type = parser->scope;
11982 /* The next name will not be qualified. */
11983 parser->scope = NULL_TREE;
11984 parser->qualifying_scope = NULL_TREE;
11985 parser->object_scope = NULL_TREE;
11986 /* Look for the optional cv-qualifier-seq. */
11987 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11988 }
11989 }
11990 /* If that didn't work we don't have a ptr-operator. */
11991 if (!cp_parser_parse_definitely (parser))
11992 cp_parser_error (parser, "expected ptr-operator");
11993 }
11994
11995 return code;
11996 }
11997
11998 /* Parse an (optional) cv-qualifier-seq.
11999
12000 cv-qualifier-seq:
12001 cv-qualifier cv-qualifier-seq [opt]
12002
12003 cv-qualifier:
12004 const
12005 volatile
12006
12007 GNU Extension:
12008
12009 cv-qualifier:
12010 __restrict__
12011
12012 Returns a bitmask representing the cv-qualifiers. */
12013
12014 static cp_cv_quals
cp_parser_cv_qualifier_seq_opt(cp_parser * parser)12015 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12016 {
12017 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12018
12019 while (true)
12020 {
12021 cp_token *token;
12022 cp_cv_quals cv_qualifier;
12023
12024 /* Peek at the next token. */
12025 token = cp_lexer_peek_token (parser->lexer);
12026 /* See if it's a cv-qualifier. */
12027 switch (token->keyword)
12028 {
12029 case RID_CONST:
12030 cv_qualifier = TYPE_QUAL_CONST;
12031 break;
12032
12033 case RID_VOLATILE:
12034 cv_qualifier = TYPE_QUAL_VOLATILE;
12035 break;
12036
12037 case RID_RESTRICT:
12038 cv_qualifier = TYPE_QUAL_RESTRICT;
12039 break;
12040
12041 default:
12042 cv_qualifier = TYPE_UNQUALIFIED;
12043 break;
12044 }
12045
12046 if (!cv_qualifier)
12047 break;
12048
12049 if (cv_quals & cv_qualifier)
12050 {
12051 error ("duplicate cv-qualifier");
12052 cp_lexer_purge_token (parser->lexer);
12053 }
12054 else
12055 {
12056 cp_lexer_consume_token (parser->lexer);
12057 cv_quals |= cv_qualifier;
12058 }
12059 }
12060
12061 return cv_quals;
12062 }
12063
12064 /* Parse a declarator-id.
12065
12066 declarator-id:
12067 id-expression
12068 :: [opt] nested-name-specifier [opt] type-name
12069
12070 In the `id-expression' case, the value returned is as for
12071 cp_parser_id_expression if the id-expression was an unqualified-id.
12072 If the id-expression was a qualified-id, then a SCOPE_REF is
12073 returned. The first operand is the scope (either a NAMESPACE_DECL
12074 or TREE_TYPE), but the second is still just a representation of an
12075 unqualified-id. */
12076
12077 static tree
cp_parser_declarator_id(cp_parser * parser,bool optional_p)12078 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12079 {
12080 tree id;
12081 /* The expression must be an id-expression. Assume that qualified
12082 names are the names of types so that:
12083
12084 template <class T>
12085 int S<T>::R::i = 3;
12086
12087 will work; we must treat `S<T>::R' as the name of a type.
12088 Similarly, assume that qualified names are templates, where
12089 required, so that:
12090
12091 template <class T>
12092 int S<T>::R<T>::i = 3;
12093
12094 will work, too. */
12095 id = cp_parser_id_expression (parser,
12096 /*template_keyword_p=*/false,
12097 /*check_dependency_p=*/false,
12098 /*template_p=*/NULL,
12099 /*declarator_p=*/true,
12100 optional_p);
12101 if (id && BASELINK_P (id))
12102 id = BASELINK_FUNCTIONS (id);
12103 return id;
12104 }
12105
12106 /* Parse a type-id.
12107
12108 type-id:
12109 type-specifier-seq abstract-declarator [opt]
12110
12111 Returns the TYPE specified. */
12112
12113 static tree
cp_parser_type_id(cp_parser * parser)12114 cp_parser_type_id (cp_parser* parser)
12115 {
12116 cp_decl_specifier_seq type_specifier_seq;
12117 cp_declarator *abstract_declarator;
12118
12119 /* Parse the type-specifier-seq. */
12120 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12121 &type_specifier_seq);
12122 if (type_specifier_seq.type == error_mark_node)
12123 return error_mark_node;
12124
12125 /* There might or might not be an abstract declarator. */
12126 cp_parser_parse_tentatively (parser);
12127 /* Look for the declarator. */
12128 abstract_declarator
12129 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12130 /*parenthesized_p=*/NULL,
12131 /*member_p=*/false);
12132 /* Check to see if there really was a declarator. */
12133 if (!cp_parser_parse_definitely (parser))
12134 abstract_declarator = NULL;
12135
12136 return groktypename (&type_specifier_seq, abstract_declarator);
12137 }
12138
12139 /* Parse a type-specifier-seq.
12140
12141 type-specifier-seq:
12142 type-specifier type-specifier-seq [opt]
12143
12144 GNU extension:
12145
12146 type-specifier-seq:
12147 attributes type-specifier-seq [opt]
12148
12149 If IS_CONDITION is true, we are at the start of a "condition",
12150 e.g., we've just seen "if (".
12151
12152 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12153
12154 static void
cp_parser_type_specifier_seq(cp_parser * parser,bool is_condition,cp_decl_specifier_seq * type_specifier_seq)12155 cp_parser_type_specifier_seq (cp_parser* parser,
12156 bool is_condition,
12157 cp_decl_specifier_seq *type_specifier_seq)
12158 {
12159 bool seen_type_specifier = false;
12160 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12161
12162 /* Clear the TYPE_SPECIFIER_SEQ. */
12163 clear_decl_specs (type_specifier_seq);
12164
12165 /* Parse the type-specifiers and attributes. */
12166 while (true)
12167 {
12168 tree type_specifier;
12169 bool is_cv_qualifier;
12170
12171 /* Check for attributes first. */
12172 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12173 {
12174 type_specifier_seq->attributes =
12175 chainon (type_specifier_seq->attributes,
12176 cp_parser_attributes_opt (parser));
12177 continue;
12178 }
12179
12180 /* Look for the type-specifier. */
12181 type_specifier = cp_parser_type_specifier (parser,
12182 flags,
12183 type_specifier_seq,
12184 /*is_declaration=*/false,
12185 NULL,
12186 &is_cv_qualifier);
12187 if (!type_specifier)
12188 {
12189 /* If the first type-specifier could not be found, this is not a
12190 type-specifier-seq at all. */
12191 if (!seen_type_specifier)
12192 {
12193 cp_parser_error (parser, "expected type-specifier");
12194 type_specifier_seq->type = error_mark_node;
12195 return;
12196 }
12197 /* If subsequent type-specifiers could not be found, the
12198 type-specifier-seq is complete. */
12199 break;
12200 }
12201
12202 seen_type_specifier = true;
12203 /* The standard says that a condition can be:
12204
12205 type-specifier-seq declarator = assignment-expression
12206
12207 However, given:
12208
12209 struct S {};
12210 if (int S = ...)
12211
12212 we should treat the "S" as a declarator, not as a
12213 type-specifier. The standard doesn't say that explicitly for
12214 type-specifier-seq, but it does say that for
12215 decl-specifier-seq in an ordinary declaration. Perhaps it
12216 would be clearer just to allow a decl-specifier-seq here, and
12217 then add a semantic restriction that if any decl-specifiers
12218 that are not type-specifiers appear, the program is invalid. */
12219 if (is_condition && !is_cv_qualifier)
12220 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12221 }
12222
12223 cp_parser_check_decl_spec (type_specifier_seq);
12224 }
12225
12226 /* Parse a parameter-declaration-clause.
12227
12228 parameter-declaration-clause:
12229 parameter-declaration-list [opt] ... [opt]
12230 parameter-declaration-list , ...
12231
12232 Returns a representation for the parameter declarations. A return
12233 value of NULL indicates a parameter-declaration-clause consisting
12234 only of an ellipsis. */
12235
12236 static cp_parameter_declarator *
cp_parser_parameter_declaration_clause(cp_parser * parser)12237 cp_parser_parameter_declaration_clause (cp_parser* parser)
12238 {
12239 cp_parameter_declarator *parameters;
12240 cp_token *token;
12241 bool ellipsis_p;
12242 bool is_error;
12243
12244 /* Peek at the next token. */
12245 token = cp_lexer_peek_token (parser->lexer);
12246 /* Check for trivial parameter-declaration-clauses. */
12247 if (token->type == CPP_ELLIPSIS)
12248 {
12249 /* Consume the `...' token. */
12250 cp_lexer_consume_token (parser->lexer);
12251 return NULL;
12252 }
12253 else if (token->type == CPP_CLOSE_PAREN)
12254 /* There are no parameters. */
12255 {
12256 #ifndef NO_IMPLICIT_EXTERN_C
12257 if (in_system_header && current_class_type == NULL
12258 && current_lang_name == lang_name_c)
12259 return NULL;
12260 else
12261 #endif
12262 return no_parameters;
12263 }
12264 /* Check for `(void)', too, which is a special case. */
12265 else if (token->keyword == RID_VOID
12266 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12267 == CPP_CLOSE_PAREN))
12268 {
12269 /* Consume the `void' token. */
12270 cp_lexer_consume_token (parser->lexer);
12271 /* There are no parameters. */
12272 return no_parameters;
12273 }
12274
12275 /* Parse the parameter-declaration-list. */
12276 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12277 /* If a parse error occurred while parsing the
12278 parameter-declaration-list, then the entire
12279 parameter-declaration-clause is erroneous. */
12280 if (is_error)
12281 return NULL;
12282
12283 /* Peek at the next token. */
12284 token = cp_lexer_peek_token (parser->lexer);
12285 /* If it's a `,', the clause should terminate with an ellipsis. */
12286 if (token->type == CPP_COMMA)
12287 {
12288 /* Consume the `,'. */
12289 cp_lexer_consume_token (parser->lexer);
12290 /* Expect an ellipsis. */
12291 ellipsis_p
12292 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12293 }
12294 /* It might also be `...' if the optional trailing `,' was
12295 omitted. */
12296 else if (token->type == CPP_ELLIPSIS)
12297 {
12298 /* Consume the `...' token. */
12299 cp_lexer_consume_token (parser->lexer);
12300 /* And remember that we saw it. */
12301 ellipsis_p = true;
12302 }
12303 else
12304 ellipsis_p = false;
12305
12306 /* Finish the parameter list. */
12307 if (parameters && ellipsis_p)
12308 parameters->ellipsis_p = true;
12309
12310 return parameters;
12311 }
12312
12313 /* Parse a parameter-declaration-list.
12314
12315 parameter-declaration-list:
12316 parameter-declaration
12317 parameter-declaration-list , parameter-declaration
12318
12319 Returns a representation of the parameter-declaration-list, as for
12320 cp_parser_parameter_declaration_clause. However, the
12321 `void_list_node' is never appended to the list. Upon return,
12322 *IS_ERROR will be true iff an error occurred. */
12323
12324 static cp_parameter_declarator *
cp_parser_parameter_declaration_list(cp_parser * parser,bool * is_error)12325 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12326 {
12327 cp_parameter_declarator *parameters = NULL;
12328 cp_parameter_declarator **tail = ¶meters;
12329 bool saved_in_unbraced_linkage_specification_p;
12330
12331 /* Assume all will go well. */
12332 *is_error = false;
12333 /* The special considerations that apply to a function within an
12334 unbraced linkage specifications do not apply to the parameters
12335 to the function. */
12336 saved_in_unbraced_linkage_specification_p
12337 = parser->in_unbraced_linkage_specification_p;
12338 parser->in_unbraced_linkage_specification_p = false;
12339
12340 /* Look for more parameters. */
12341 while (true)
12342 {
12343 cp_parameter_declarator *parameter;
12344 bool parenthesized_p;
12345 /* Parse the parameter. */
12346 parameter
12347 = cp_parser_parameter_declaration (parser,
12348 /*template_parm_p=*/false,
12349 &parenthesized_p);
12350
12351 /* If a parse error occurred parsing the parameter declaration,
12352 then the entire parameter-declaration-list is erroneous. */
12353 if (!parameter)
12354 {
12355 *is_error = true;
12356 parameters = NULL;
12357 break;
12358 }
12359 /* Add the new parameter to the list. */
12360 *tail = parameter;
12361 tail = ¶meter->next;
12362
12363 /* Peek at the next token. */
12364 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12365 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12366 /* These are for Objective-C++ */
12367 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12368 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12369 /* The parameter-declaration-list is complete. */
12370 break;
12371 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12372 {
12373 cp_token *token;
12374
12375 /* Peek at the next token. */
12376 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12377 /* If it's an ellipsis, then the list is complete. */
12378 if (token->type == CPP_ELLIPSIS)
12379 break;
12380 /* Otherwise, there must be more parameters. Consume the
12381 `,'. */
12382 cp_lexer_consume_token (parser->lexer);
12383 /* When parsing something like:
12384
12385 int i(float f, double d)
12386
12387 we can tell after seeing the declaration for "f" that we
12388 are not looking at an initialization of a variable "i",
12389 but rather at the declaration of a function "i".
12390
12391 Due to the fact that the parsing of template arguments
12392 (as specified to a template-id) requires backtracking we
12393 cannot use this technique when inside a template argument
12394 list. */
12395 if (!parser->in_template_argument_list_p
12396 && !parser->in_type_id_in_expr_p
12397 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12398 /* However, a parameter-declaration of the form
12399 "foat(f)" (which is a valid declaration of a
12400 parameter "f") can also be interpreted as an
12401 expression (the conversion of "f" to "float"). */
12402 && !parenthesized_p)
12403 cp_parser_commit_to_tentative_parse (parser);
12404 }
12405 else
12406 {
12407 cp_parser_error (parser, "expected %<,%> or %<...%>");
12408 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12409 cp_parser_skip_to_closing_parenthesis (parser,
12410 /*recovering=*/true,
12411 /*or_comma=*/false,
12412 /*consume_paren=*/false);
12413 break;
12414 }
12415 }
12416
12417 parser->in_unbraced_linkage_specification_p
12418 = saved_in_unbraced_linkage_specification_p;
12419
12420 return parameters;
12421 }
12422
12423 /* Parse a parameter declaration.
12424
12425 parameter-declaration:
12426 decl-specifier-seq declarator
12427 decl-specifier-seq declarator = assignment-expression
12428 decl-specifier-seq abstract-declarator [opt]
12429 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12430
12431 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12432 declares a template parameter. (In that case, a non-nested `>'
12433 token encountered during the parsing of the assignment-expression
12434 is not interpreted as a greater-than operator.)
12435
12436 Returns a representation of the parameter, or NULL if an error
12437 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12438 true iff the declarator is of the form "(p)". */
12439
12440 static cp_parameter_declarator *
cp_parser_parameter_declaration(cp_parser * parser,bool template_parm_p,bool * parenthesized_p)12441 cp_parser_parameter_declaration (cp_parser *parser,
12442 bool template_parm_p,
12443 bool *parenthesized_p)
12444 {
12445 int declares_class_or_enum;
12446 bool greater_than_is_operator_p;
12447 cp_decl_specifier_seq decl_specifiers;
12448 cp_declarator *declarator;
12449 tree default_argument;
12450 cp_token *token;
12451 const char *saved_message;
12452
12453 /* In a template parameter, `>' is not an operator.
12454
12455 [temp.param]
12456
12457 When parsing a default template-argument for a non-type
12458 template-parameter, the first non-nested `>' is taken as the end
12459 of the template parameter-list rather than a greater-than
12460 operator. */
12461 greater_than_is_operator_p = !template_parm_p;
12462
12463 /* Type definitions may not appear in parameter types. */
12464 saved_message = parser->type_definition_forbidden_message;
12465 parser->type_definition_forbidden_message
12466 = "types may not be defined in parameter types";
12467
12468 /* Parse the declaration-specifiers. */
12469 cp_parser_decl_specifier_seq (parser,
12470 CP_PARSER_FLAGS_NONE,
12471 &decl_specifiers,
12472 &declares_class_or_enum);
12473 /* If an error occurred, there's no reason to attempt to parse the
12474 rest of the declaration. */
12475 if (cp_parser_error_occurred (parser))
12476 {
12477 parser->type_definition_forbidden_message = saved_message;
12478 return NULL;
12479 }
12480
12481 /* Peek at the next token. */
12482 token = cp_lexer_peek_token (parser->lexer);
12483 /* If the next token is a `)', `,', `=', `>', or `...', then there
12484 is no declarator. */
12485 if (token->type == CPP_CLOSE_PAREN
12486 || token->type == CPP_COMMA
12487 || token->type == CPP_EQ
12488 || token->type == CPP_ELLIPSIS
12489 || token->type == CPP_GREATER)
12490 {
12491 declarator = NULL;
12492 if (parenthesized_p)
12493 *parenthesized_p = false;
12494 }
12495 /* Otherwise, there should be a declarator. */
12496 else
12497 {
12498 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12499 parser->default_arg_ok_p = false;
12500
12501 /* After seeing a decl-specifier-seq, if the next token is not a
12502 "(", there is no possibility that the code is a valid
12503 expression. Therefore, if parsing tentatively, we commit at
12504 this point. */
12505 if (!parser->in_template_argument_list_p
12506 /* In an expression context, having seen:
12507
12508 (int((char ...
12509
12510 we cannot be sure whether we are looking at a
12511 function-type (taking a "char" as a parameter) or a cast
12512 of some object of type "char" to "int". */
12513 && !parser->in_type_id_in_expr_p
12514 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12515 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12516 cp_parser_commit_to_tentative_parse (parser);
12517 /* Parse the declarator. */
12518 declarator = cp_parser_declarator (parser,
12519 CP_PARSER_DECLARATOR_EITHER,
12520 /*ctor_dtor_or_conv_p=*/NULL,
12521 parenthesized_p,
12522 /*member_p=*/false);
12523 parser->default_arg_ok_p = saved_default_arg_ok_p;
12524 /* After the declarator, allow more attributes. */
12525 decl_specifiers.attributes
12526 = chainon (decl_specifiers.attributes,
12527 cp_parser_attributes_opt (parser));
12528 }
12529
12530 /* The restriction on defining new types applies only to the type
12531 of the parameter, not to the default argument. */
12532 parser->type_definition_forbidden_message = saved_message;
12533
12534 /* If the next token is `=', then process a default argument. */
12535 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12536 {
12537 bool saved_greater_than_is_operator_p;
12538 /* Consume the `='. */
12539 cp_lexer_consume_token (parser->lexer);
12540
12541 /* If we are defining a class, then the tokens that make up the
12542 default argument must be saved and processed later. */
12543 if (!template_parm_p && at_class_scope_p ()
12544 && TYPE_BEING_DEFINED (current_class_type))
12545 {
12546 unsigned depth = 0;
12547 cp_token *first_token;
12548 cp_token *token;
12549
12550 /* Add tokens until we have processed the entire default
12551 argument. We add the range [first_token, token). */
12552 first_token = cp_lexer_peek_token (parser->lexer);
12553 while (true)
12554 {
12555 bool done = false;
12556
12557 /* Peek at the next token. */
12558 token = cp_lexer_peek_token (parser->lexer);
12559 /* What we do depends on what token we have. */
12560 switch (token->type)
12561 {
12562 /* In valid code, a default argument must be
12563 immediately followed by a `,' `)', or `...'. */
12564 case CPP_COMMA:
12565 case CPP_CLOSE_PAREN:
12566 case CPP_ELLIPSIS:
12567 /* If we run into a non-nested `;', `}', or `]',
12568 then the code is invalid -- but the default
12569 argument is certainly over. */
12570 case CPP_SEMICOLON:
12571 case CPP_CLOSE_BRACE:
12572 case CPP_CLOSE_SQUARE:
12573 if (depth == 0)
12574 done = true;
12575 /* Update DEPTH, if necessary. */
12576 else if (token->type == CPP_CLOSE_PAREN
12577 || token->type == CPP_CLOSE_BRACE
12578 || token->type == CPP_CLOSE_SQUARE)
12579 --depth;
12580 break;
12581
12582 case CPP_OPEN_PAREN:
12583 case CPP_OPEN_SQUARE:
12584 case CPP_OPEN_BRACE:
12585 ++depth;
12586 break;
12587
12588 case CPP_GREATER:
12589 /* If we see a non-nested `>', and `>' is not an
12590 operator, then it marks the end of the default
12591 argument. */
12592 if (!depth && !greater_than_is_operator_p)
12593 done = true;
12594 break;
12595
12596 /* If we run out of tokens, issue an error message. */
12597 case CPP_EOF:
12598 case CPP_PRAGMA_EOL:
12599 error ("file ends in default argument");
12600 done = true;
12601 break;
12602
12603 case CPP_NAME:
12604 case CPP_SCOPE:
12605 /* In these cases, we should look for template-ids.
12606 For example, if the default argument is
12607 `X<int, double>()', we need to do name lookup to
12608 figure out whether or not `X' is a template; if
12609 so, the `,' does not end the default argument.
12610
12611 That is not yet done. */
12612 break;
12613
12614 default:
12615 break;
12616 }
12617
12618 /* If we've reached the end, stop. */
12619 if (done)
12620 break;
12621
12622 /* Add the token to the token block. */
12623 token = cp_lexer_consume_token (parser->lexer);
12624 }
12625
12626 /* Create a DEFAULT_ARG to represented the unparsed default
12627 argument. */
12628 default_argument = make_node (DEFAULT_ARG);
12629 DEFARG_TOKENS (default_argument)
12630 = cp_token_cache_new (first_token, token);
12631 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12632 }
12633 /* Outside of a class definition, we can just parse the
12634 assignment-expression. */
12635 else
12636 {
12637 bool saved_local_variables_forbidden_p;
12638
12639 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12640 set correctly. */
12641 saved_greater_than_is_operator_p
12642 = parser->greater_than_is_operator_p;
12643 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12644 /* Local variable names (and the `this' keyword) may not
12645 appear in a default argument. */
12646 saved_local_variables_forbidden_p
12647 = parser->local_variables_forbidden_p;
12648 parser->local_variables_forbidden_p = true;
12649 /* The default argument expression may cause implicitly
12650 defined member functions to be synthesized, which will
12651 result in garbage collection. We must treat this
12652 situation as if we were within the body of function so as
12653 to avoid collecting live data on the stack. */
12654 ++function_depth;
12655 /* Parse the assignment-expression. */
12656 if (template_parm_p)
12657 push_deferring_access_checks (dk_no_deferred);
12658 default_argument
12659 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12660 if (template_parm_p)
12661 pop_deferring_access_checks ();
12662 /* Restore saved state. */
12663 --function_depth;
12664 parser->greater_than_is_operator_p
12665 = saved_greater_than_is_operator_p;
12666 parser->local_variables_forbidden_p
12667 = saved_local_variables_forbidden_p;
12668 }
12669 if (!parser->default_arg_ok_p)
12670 {
12671 if (!flag_pedantic_errors)
12672 warning (0, "deprecated use of default argument for parameter of non-function");
12673 else
12674 {
12675 error ("default arguments are only permitted for function parameters");
12676 default_argument = NULL_TREE;
12677 }
12678 }
12679 }
12680 else
12681 default_argument = NULL_TREE;
12682
12683 return make_parameter_declarator (&decl_specifiers,
12684 declarator,
12685 default_argument);
12686 }
12687
12688 /* Parse a function-body.
12689
12690 function-body:
12691 compound_statement */
12692
12693 static void
cp_parser_function_body(cp_parser * parser)12694 cp_parser_function_body (cp_parser *parser)
12695 {
12696 cp_parser_compound_statement (parser, NULL, false);
12697 }
12698
12699 /* Parse a ctor-initializer-opt followed by a function-body. Return
12700 true if a ctor-initializer was present. */
12701
12702 static bool
cp_parser_ctor_initializer_opt_and_function_body(cp_parser * parser)12703 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12704 {
12705 tree body;
12706 bool ctor_initializer_p;
12707
12708 /* Begin the function body. */
12709 body = begin_function_body ();
12710 /* Parse the optional ctor-initializer. */
12711 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12712 /* Parse the function-body. */
12713 cp_parser_function_body (parser);
12714 /* Finish the function body. */
12715 finish_function_body (body);
12716
12717 return ctor_initializer_p;
12718 }
12719
12720 /* Parse an initializer.
12721
12722 initializer:
12723 = initializer-clause
12724 ( expression-list )
12725
12726 Returns an expression representing the initializer. If no
12727 initializer is present, NULL_TREE is returned.
12728
12729 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12730 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12731 set to FALSE if there is no initializer present. If there is an
12732 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12733 is set to true; otherwise it is set to false. */
12734
12735 static tree
cp_parser_initializer(cp_parser * parser,bool * is_parenthesized_init,bool * non_constant_p)12736 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12737 bool* non_constant_p)
12738 {
12739 cp_token *token;
12740 tree init;
12741
12742 /* Peek at the next token. */
12743 token = cp_lexer_peek_token (parser->lexer);
12744
12745 /* Let our caller know whether or not this initializer was
12746 parenthesized. */
12747 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12748 /* Assume that the initializer is constant. */
12749 *non_constant_p = false;
12750
12751 if (token->type == CPP_EQ)
12752 {
12753 /* Consume the `='. */
12754 cp_lexer_consume_token (parser->lexer);
12755 /* Parse the initializer-clause. */
12756 init = cp_parser_initializer_clause (parser, non_constant_p);
12757 }
12758 else if (token->type == CPP_OPEN_PAREN)
12759 init = cp_parser_parenthesized_expression_list (parser, false,
12760 /*cast_p=*/false,
12761 non_constant_p);
12762 else
12763 {
12764 /* Anything else is an error. */
12765 cp_parser_error (parser, "expected initializer");
12766 init = error_mark_node;
12767 }
12768
12769 return init;
12770 }
12771
12772 /* Parse an initializer-clause.
12773
12774 initializer-clause:
12775 assignment-expression
12776 { initializer-list , [opt] }
12777 { }
12778
12779 Returns an expression representing the initializer.
12780
12781 If the `assignment-expression' production is used the value
12782 returned is simply a representation for the expression.
12783
12784 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12785 the elements of the initializer-list (or NULL, if the last
12786 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12787 NULL_TREE. There is no way to detect whether or not the optional
12788 trailing `,' was provided. NON_CONSTANT_P is as for
12789 cp_parser_initializer. */
12790
12791 static tree
cp_parser_initializer_clause(cp_parser * parser,bool * non_constant_p)12792 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12793 {
12794 tree initializer;
12795
12796 /* Assume the expression is constant. */
12797 *non_constant_p = false;
12798
12799 /* If it is not a `{', then we are looking at an
12800 assignment-expression. */
12801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12802 {
12803 initializer
12804 = cp_parser_constant_expression (parser,
12805 /*allow_non_constant_p=*/true,
12806 non_constant_p);
12807 if (!*non_constant_p)
12808 initializer = fold_non_dependent_expr (initializer);
12809 }
12810 else
12811 {
12812 /* Consume the `{' token. */
12813 cp_lexer_consume_token (parser->lexer);
12814 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12815 initializer = make_node (CONSTRUCTOR);
12816 /* If it's not a `}', then there is a non-trivial initializer. */
12817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12818 {
12819 /* Parse the initializer list. */
12820 CONSTRUCTOR_ELTS (initializer)
12821 = cp_parser_initializer_list (parser, non_constant_p);
12822 /* A trailing `,' token is allowed. */
12823 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12824 cp_lexer_consume_token (parser->lexer);
12825 }
12826 /* Now, there should be a trailing `}'. */
12827 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12828 }
12829
12830 return initializer;
12831 }
12832
12833 /* Parse an initializer-list.
12834
12835 initializer-list:
12836 initializer-clause
12837 initializer-list , initializer-clause
12838
12839 GNU Extension:
12840
12841 initializer-list:
12842 identifier : initializer-clause
12843 initializer-list, identifier : initializer-clause
12844
12845 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12846 for the initializer. If the INDEX of the elt is non-NULL, it is the
12847 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12848 as for cp_parser_initializer. */
12849
VEC(constructor_elt,gc)12850 static VEC(constructor_elt,gc) *
12851 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12852 {
12853 VEC(constructor_elt,gc) *v = NULL;
12854
12855 /* Assume all of the expressions are constant. */
12856 *non_constant_p = false;
12857
12858 /* Parse the rest of the list. */
12859 while (true)
12860 {
12861 cp_token *token;
12862 tree identifier;
12863 tree initializer;
12864 bool clause_non_constant_p;
12865
12866 /* If the next token is an identifier and the following one is a
12867 colon, we are looking at the GNU designated-initializer
12868 syntax. */
12869 if (cp_parser_allow_gnu_extensions_p (parser)
12870 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12871 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12872 {
12873 /* Warn the user that they are using an extension. */
12874 if (pedantic)
12875 pedwarn ("ISO C++ does not allow designated initializers");
12876 /* Consume the identifier. */
12877 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
12878 /* Consume the `:'. */
12879 cp_lexer_consume_token (parser->lexer);
12880 }
12881 else
12882 identifier = NULL_TREE;
12883
12884 /* Parse the initializer. */
12885 initializer = cp_parser_initializer_clause (parser,
12886 &clause_non_constant_p);
12887 /* If any clause is non-constant, so is the entire initializer. */
12888 if (clause_non_constant_p)
12889 *non_constant_p = true;
12890
12891 /* Add it to the vector. */
12892 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12893
12894 /* If the next token is not a comma, we have reached the end of
12895 the list. */
12896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12897 break;
12898
12899 /* Peek at the next token. */
12900 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12901 /* If the next token is a `}', then we're still done. An
12902 initializer-clause can have a trailing `,' after the
12903 initializer-list and before the closing `}'. */
12904 if (token->type == CPP_CLOSE_BRACE)
12905 break;
12906
12907 /* Consume the `,' token. */
12908 cp_lexer_consume_token (parser->lexer);
12909 }
12910
12911 return v;
12912 }
12913
12914 /* Classes [gram.class] */
12915
12916 /* Parse a class-name.
12917
12918 class-name:
12919 identifier
12920 template-id
12921
12922 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12923 to indicate that names looked up in dependent types should be
12924 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12925 keyword has been used to indicate that the name that appears next
12926 is a template. TAG_TYPE indicates the explicit tag given before
12927 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12928 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12929 is the class being defined in a class-head.
12930
12931 Returns the TYPE_DECL representing the class. */
12932
12933 static tree
cp_parser_class_name(cp_parser * parser,bool typename_keyword_p,bool template_keyword_p,enum tag_types tag_type,bool check_dependency_p,bool class_head_p,bool is_declaration)12934 cp_parser_class_name (cp_parser *parser,
12935 bool typename_keyword_p,
12936 bool template_keyword_p,
12937 enum tag_types tag_type,
12938 bool check_dependency_p,
12939 bool class_head_p,
12940 bool is_declaration)
12941 {
12942 tree decl;
12943 tree scope;
12944 bool typename_p;
12945 cp_token *token;
12946
12947 /* All class-names start with an identifier. */
12948 token = cp_lexer_peek_token (parser->lexer);
12949 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12950 {
12951 cp_parser_error (parser, "expected class-name");
12952 return error_mark_node;
12953 }
12954
12955 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12956 to a template-id, so we save it here. */
12957 scope = parser->scope;
12958 if (scope == error_mark_node)
12959 return error_mark_node;
12960
12961 /* Any name names a type if we're following the `typename' keyword
12962 in a qualified name where the enclosing scope is type-dependent. */
12963 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12964 && dependent_type_p (scope));
12965 /* Handle the common case (an identifier, but not a template-id)
12966 efficiently. */
12967 if (token->type == CPP_NAME
12968 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12969 {
12970 cp_token *identifier_token;
12971 tree identifier;
12972 bool ambiguous_p;
12973
12974 /* Look for the identifier. */
12975 identifier_token = cp_lexer_peek_token (parser->lexer);
12976 ambiguous_p = identifier_token->ambiguous_p;
12977 identifier = cp_parser_identifier (parser);
12978 /* If the next token isn't an identifier, we are certainly not
12979 looking at a class-name. */
12980 if (identifier == error_mark_node)
12981 decl = error_mark_node;
12982 /* If we know this is a type-name, there's no need to look it
12983 up. */
12984 else if (typename_p)
12985 decl = identifier;
12986 else
12987 {
12988 tree ambiguous_decls;
12989 /* If we already know that this lookup is ambiguous, then
12990 we've already issued an error message; there's no reason
12991 to check again. */
12992 if (ambiguous_p)
12993 {
12994 cp_parser_simulate_error (parser);
12995 return error_mark_node;
12996 }
12997 /* If the next token is a `::', then the name must be a type
12998 name.
12999
13000 [basic.lookup.qual]
13001
13002 During the lookup for a name preceding the :: scope
13003 resolution operator, object, function, and enumerator
13004 names are ignored. */
13005 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13006 tag_type = typename_type;
13007 /* Look up the name. */
13008 decl = cp_parser_lookup_name (parser, identifier,
13009 tag_type,
13010 /*is_template=*/false,
13011 /*is_namespace=*/false,
13012 check_dependency_p,
13013 &ambiguous_decls);
13014 if (ambiguous_decls)
13015 {
13016 error ("reference to %qD is ambiguous", identifier);
13017 print_candidates (ambiguous_decls);
13018 if (cp_parser_parsing_tentatively (parser))
13019 {
13020 identifier_token->ambiguous_p = true;
13021 cp_parser_simulate_error (parser);
13022 }
13023 return error_mark_node;
13024 }
13025 }
13026 }
13027 else
13028 {
13029 /* Try a template-id. */
13030 decl = cp_parser_template_id (parser, template_keyword_p,
13031 check_dependency_p,
13032 is_declaration);
13033 if (decl == error_mark_node)
13034 return error_mark_node;
13035 }
13036
13037 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13038
13039 /* If this is a typename, create a TYPENAME_TYPE. */
13040 if (typename_p && decl != error_mark_node)
13041 {
13042 decl = make_typename_type (scope, decl, typename_type,
13043 /*complain=*/tf_error);
13044 if (decl != error_mark_node)
13045 decl = TYPE_NAME (decl);
13046 }
13047
13048 /* Check to see that it is really the name of a class. */
13049 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13050 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13051 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13052 /* Situations like this:
13053
13054 template <typename T> struct A {
13055 typename T::template X<int>::I i;
13056 };
13057
13058 are problematic. Is `T::template X<int>' a class-name? The
13059 standard does not seem to be definitive, but there is no other
13060 valid interpretation of the following `::'. Therefore, those
13061 names are considered class-names. */
13062 {
13063 decl = make_typename_type (scope, decl, tag_type, tf_error);
13064 if (decl != error_mark_node)
13065 decl = TYPE_NAME (decl);
13066 }
13067 else if (TREE_CODE (decl) != TYPE_DECL
13068 || TREE_TYPE (decl) == error_mark_node
13069 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13070 decl = error_mark_node;
13071
13072 if (decl == error_mark_node)
13073 cp_parser_error (parser, "expected class-name");
13074
13075 return decl;
13076 }
13077
13078 /* Parse a class-specifier.
13079
13080 class-specifier:
13081 class-head { member-specification [opt] }
13082
13083 Returns the TREE_TYPE representing the class. */
13084
13085 static tree
cp_parser_class_specifier(cp_parser * parser)13086 cp_parser_class_specifier (cp_parser* parser)
13087 {
13088 cp_token *token;
13089 tree type;
13090 tree attributes = NULL_TREE;
13091 int has_trailing_semicolon;
13092 bool nested_name_specifier_p;
13093 unsigned saved_num_template_parameter_lists;
13094 bool saved_in_function_body;
13095 tree old_scope = NULL_TREE;
13096 tree scope = NULL_TREE;
13097 tree bases;
13098
13099 push_deferring_access_checks (dk_no_deferred);
13100
13101 /* Parse the class-head. */
13102 type = cp_parser_class_head (parser,
13103 &nested_name_specifier_p,
13104 &attributes,
13105 &bases);
13106 /* If the class-head was a semantic disaster, skip the entire body
13107 of the class. */
13108 if (!type)
13109 {
13110 cp_parser_skip_to_end_of_block_or_statement (parser);
13111 pop_deferring_access_checks ();
13112 return error_mark_node;
13113 }
13114
13115 /* Look for the `{'. */
13116 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13117 {
13118 pop_deferring_access_checks ();
13119 return error_mark_node;
13120 }
13121
13122 /* Process the base classes. If they're invalid, skip the
13123 entire class body. */
13124 if (!xref_basetypes (type, bases))
13125 {
13126 cp_parser_skip_to_closing_brace (parser);
13127
13128 /* Consuming the closing brace yields better error messages
13129 later on. */
13130 cp_lexer_consume_token (parser->lexer);
13131 pop_deferring_access_checks ();
13132 return error_mark_node;
13133 }
13134
13135 /* Issue an error message if type-definitions are forbidden here. */
13136 cp_parser_check_type_definition (parser);
13137 /* Remember that we are defining one more class. */
13138 ++parser->num_classes_being_defined;
13139 /* Inside the class, surrounding template-parameter-lists do not
13140 apply. */
13141 saved_num_template_parameter_lists
13142 = parser->num_template_parameter_lists;
13143 parser->num_template_parameter_lists = 0;
13144 /* We are not in a function body. */
13145 saved_in_function_body = parser->in_function_body;
13146 parser->in_function_body = false;
13147
13148 /* Start the class. */
13149 if (nested_name_specifier_p)
13150 {
13151 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13152 old_scope = push_inner_scope (scope);
13153 }
13154 type = begin_class_definition (type, attributes);
13155
13156 if (type == error_mark_node)
13157 /* If the type is erroneous, skip the entire body of the class. */
13158 cp_parser_skip_to_closing_brace (parser);
13159 else
13160 /* Parse the member-specification. */
13161 cp_parser_member_specification_opt (parser);
13162
13163 /* Look for the trailing `}'. */
13164 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13165 /* We get better error messages by noticing a common problem: a
13166 missing trailing `;'. */
13167 token = cp_lexer_peek_token (parser->lexer);
13168 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13169 /* Look for trailing attributes to apply to this class. */
13170 if (cp_parser_allow_gnu_extensions_p (parser))
13171 attributes = cp_parser_attributes_opt (parser);
13172 if (type != error_mark_node)
13173 type = finish_struct (type, attributes);
13174 if (nested_name_specifier_p)
13175 pop_inner_scope (old_scope, scope);
13176 /* If this class is not itself within the scope of another class,
13177 then we need to parse the bodies of all of the queued function
13178 definitions. Note that the queued functions defined in a class
13179 are not always processed immediately following the
13180 class-specifier for that class. Consider:
13181
13182 struct A {
13183 struct B { void f() { sizeof (A); } };
13184 };
13185
13186 If `f' were processed before the processing of `A' were
13187 completed, there would be no way to compute the size of `A'.
13188 Note that the nesting we are interested in here is lexical --
13189 not the semantic nesting given by TYPE_CONTEXT. In particular,
13190 for:
13191
13192 struct A { struct B; };
13193 struct A::B { void f() { } };
13194
13195 there is no need to delay the parsing of `A::B::f'. */
13196 if (--parser->num_classes_being_defined == 0)
13197 {
13198 tree queue_entry;
13199 tree fn;
13200 tree class_type = NULL_TREE;
13201 tree pushed_scope = NULL_TREE;
13202
13203 /* In a first pass, parse default arguments to the functions.
13204 Then, in a second pass, parse the bodies of the functions.
13205 This two-phased approach handles cases like:
13206
13207 struct S {
13208 void f() { g(); }
13209 void g(int i = 3);
13210 };
13211
13212 */
13213 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13214 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13215 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13216 TREE_PURPOSE (parser->unparsed_functions_queues)
13217 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13218 {
13219 fn = TREE_VALUE (queue_entry);
13220 /* If there are default arguments that have not yet been processed,
13221 take care of them now. */
13222 if (class_type != TREE_PURPOSE (queue_entry))
13223 {
13224 if (pushed_scope)
13225 pop_scope (pushed_scope);
13226 class_type = TREE_PURPOSE (queue_entry);
13227 pushed_scope = push_scope (class_type);
13228 }
13229 /* Make sure that any template parameters are in scope. */
13230 maybe_begin_member_template_processing (fn);
13231 /* Parse the default argument expressions. */
13232 cp_parser_late_parsing_default_args (parser, fn);
13233 /* Remove any template parameters from the symbol table. */
13234 maybe_end_member_template_processing ();
13235 }
13236 if (pushed_scope)
13237 pop_scope (pushed_scope);
13238 /* Now parse the body of the functions. */
13239 for (TREE_VALUE (parser->unparsed_functions_queues)
13240 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13241 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13242 TREE_VALUE (parser->unparsed_functions_queues)
13243 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13244 {
13245 /* Figure out which function we need to process. */
13246 fn = TREE_VALUE (queue_entry);
13247 /* Parse the function. */
13248 cp_parser_late_parsing_for_member (parser, fn);
13249 }
13250 }
13251
13252 /* Put back any saved access checks. */
13253 pop_deferring_access_checks ();
13254
13255 /* Restore saved state. */
13256 parser->in_function_body = saved_in_function_body;
13257 parser->num_template_parameter_lists
13258 = saved_num_template_parameter_lists;
13259
13260 return type;
13261 }
13262
13263 /* Parse a class-head.
13264
13265 class-head:
13266 class-key identifier [opt] base-clause [opt]
13267 class-key nested-name-specifier identifier base-clause [opt]
13268 class-key nested-name-specifier [opt] template-id
13269 base-clause [opt]
13270
13271 GNU Extensions:
13272 class-key attributes identifier [opt] base-clause [opt]
13273 class-key attributes nested-name-specifier identifier base-clause [opt]
13274 class-key attributes nested-name-specifier [opt] template-id
13275 base-clause [opt]
13276
13277 Returns the TYPE of the indicated class. Sets
13278 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13279 involving a nested-name-specifier was used, and FALSE otherwise.
13280
13281 Returns error_mark_node if this is not a class-head.
13282
13283 Returns NULL_TREE if the class-head is syntactically valid, but
13284 semantically invalid in a way that means we should skip the entire
13285 body of the class. */
13286
13287 static tree
cp_parser_class_head(cp_parser * parser,bool * nested_name_specifier_p,tree * attributes_p,tree * bases)13288 cp_parser_class_head (cp_parser* parser,
13289 bool* nested_name_specifier_p,
13290 tree *attributes_p,
13291 tree *bases)
13292 {
13293 tree nested_name_specifier;
13294 enum tag_types class_key;
13295 tree id = NULL_TREE;
13296 tree type = NULL_TREE;
13297 tree attributes;
13298 bool template_id_p = false;
13299 bool qualified_p = false;
13300 bool invalid_nested_name_p = false;
13301 bool invalid_explicit_specialization_p = false;
13302 tree pushed_scope = NULL_TREE;
13303 unsigned num_templates;
13304
13305 /* Assume no nested-name-specifier will be present. */
13306 *nested_name_specifier_p = false;
13307 /* Assume no template parameter lists will be used in defining the
13308 type. */
13309 num_templates = 0;
13310
13311 /* Look for the class-key. */
13312 class_key = cp_parser_class_key (parser);
13313 if (class_key == none_type)
13314 return error_mark_node;
13315
13316 /* Parse the attributes. */
13317 attributes = cp_parser_attributes_opt (parser);
13318
13319 /* If the next token is `::', that is invalid -- but sometimes
13320 people do try to write:
13321
13322 struct ::S {};
13323
13324 Handle this gracefully by accepting the extra qualifier, and then
13325 issuing an error about it later if this really is a
13326 class-head. If it turns out just to be an elaborated type
13327 specifier, remain silent. */
13328 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13329 qualified_p = true;
13330
13331 push_deferring_access_checks (dk_no_check);
13332
13333 /* Determine the name of the class. Begin by looking for an
13334 optional nested-name-specifier. */
13335 nested_name_specifier
13336 = cp_parser_nested_name_specifier_opt (parser,
13337 /*typename_keyword_p=*/false,
13338 /*check_dependency_p=*/false,
13339 /*type_p=*/false,
13340 /*is_declaration=*/false);
13341 /* If there was a nested-name-specifier, then there *must* be an
13342 identifier. */
13343 if (nested_name_specifier)
13344 {
13345 /* Although the grammar says `identifier', it really means
13346 `class-name' or `template-name'. You are only allowed to
13347 define a class that has already been declared with this
13348 syntax.
13349
13350 The proposed resolution for Core Issue 180 says that wherever
13351 you see `class T::X' you should treat `X' as a type-name.
13352
13353 It is OK to define an inaccessible class; for example:
13354
13355 class A { class B; };
13356 class A::B {};
13357
13358 We do not know if we will see a class-name, or a
13359 template-name. We look for a class-name first, in case the
13360 class-name is a template-id; if we looked for the
13361 template-name first we would stop after the template-name. */
13362 cp_parser_parse_tentatively (parser);
13363 type = cp_parser_class_name (parser,
13364 /*typename_keyword_p=*/false,
13365 /*template_keyword_p=*/false,
13366 class_type,
13367 /*check_dependency_p=*/false,
13368 /*class_head_p=*/true,
13369 /*is_declaration=*/false);
13370 /* If that didn't work, ignore the nested-name-specifier. */
13371 if (!cp_parser_parse_definitely (parser))
13372 {
13373 invalid_nested_name_p = true;
13374 id = cp_parser_identifier (parser);
13375 if (id == error_mark_node)
13376 id = NULL_TREE;
13377 }
13378 /* If we could not find a corresponding TYPE, treat this
13379 declaration like an unqualified declaration. */
13380 if (type == error_mark_node)
13381 nested_name_specifier = NULL_TREE;
13382 /* Otherwise, count the number of templates used in TYPE and its
13383 containing scopes. */
13384 else
13385 {
13386 tree scope;
13387
13388 for (scope = TREE_TYPE (type);
13389 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13390 scope = (TYPE_P (scope)
13391 ? TYPE_CONTEXT (scope)
13392 : DECL_CONTEXT (scope)))
13393 if (TYPE_P (scope)
13394 && CLASS_TYPE_P (scope)
13395 && CLASSTYPE_TEMPLATE_INFO (scope)
13396 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13397 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13398 ++num_templates;
13399 }
13400 }
13401 /* Otherwise, the identifier is optional. */
13402 else
13403 {
13404 /* We don't know whether what comes next is a template-id,
13405 an identifier, or nothing at all. */
13406 cp_parser_parse_tentatively (parser);
13407 /* Check for a template-id. */
13408 id = cp_parser_template_id (parser,
13409 /*template_keyword_p=*/false,
13410 /*check_dependency_p=*/true,
13411 /*is_declaration=*/true);
13412 /* If that didn't work, it could still be an identifier. */
13413 if (!cp_parser_parse_definitely (parser))
13414 {
13415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13416 id = cp_parser_identifier (parser);
13417 else
13418 id = NULL_TREE;
13419 }
13420 else
13421 {
13422 template_id_p = true;
13423 ++num_templates;
13424 }
13425 }
13426
13427 pop_deferring_access_checks ();
13428
13429 if (id)
13430 cp_parser_check_for_invalid_template_id (parser, id);
13431
13432 /* If it's not a `:' or a `{' then we can't really be looking at a
13433 class-head, since a class-head only appears as part of a
13434 class-specifier. We have to detect this situation before calling
13435 xref_tag, since that has irreversible side-effects. */
13436 if (!cp_parser_next_token_starts_class_definition_p (parser))
13437 {
13438 cp_parser_error (parser, "expected %<{%> or %<:%>");
13439 return error_mark_node;
13440 }
13441
13442 /* At this point, we're going ahead with the class-specifier, even
13443 if some other problem occurs. */
13444 cp_parser_commit_to_tentative_parse (parser);
13445 /* Issue the error about the overly-qualified name now. */
13446 if (qualified_p)
13447 cp_parser_error (parser,
13448 "global qualification of class name is invalid");
13449 else if (invalid_nested_name_p)
13450 cp_parser_error (parser,
13451 "qualified name does not name a class");
13452 else if (nested_name_specifier)
13453 {
13454 tree scope;
13455
13456 /* Reject typedef-names in class heads. */
13457 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13458 {
13459 error ("invalid class name in declaration of %qD", type);
13460 type = NULL_TREE;
13461 goto done;
13462 }
13463
13464 /* Figure out in what scope the declaration is being placed. */
13465 scope = current_scope ();
13466 /* If that scope does not contain the scope in which the
13467 class was originally declared, the program is invalid. */
13468 if (scope && !is_ancestor (scope, nested_name_specifier))
13469 {
13470 error ("declaration of %qD in %qD which does not enclose %qD",
13471 type, scope, nested_name_specifier);
13472 type = NULL_TREE;
13473 goto done;
13474 }
13475 /* [dcl.meaning]
13476
13477 A declarator-id shall not be qualified exception of the
13478 definition of a ... nested class outside of its class
13479 ... [or] a the definition or explicit instantiation of a
13480 class member of a namespace outside of its namespace. */
13481 if (scope == nested_name_specifier)
13482 {
13483 pedwarn ("extra qualification ignored");
13484 nested_name_specifier = NULL_TREE;
13485 num_templates = 0;
13486 }
13487 }
13488 /* An explicit-specialization must be preceded by "template <>". If
13489 it is not, try to recover gracefully. */
13490 if (at_namespace_scope_p ()
13491 && parser->num_template_parameter_lists == 0
13492 && template_id_p)
13493 {
13494 error ("an explicit specialization must be preceded by %<template <>%>");
13495 invalid_explicit_specialization_p = true;
13496 /* Take the same action that would have been taken by
13497 cp_parser_explicit_specialization. */
13498 ++parser->num_template_parameter_lists;
13499 begin_specialization ();
13500 }
13501 /* There must be no "return" statements between this point and the
13502 end of this function; set "type "to the correct return value and
13503 use "goto done;" to return. */
13504 /* Make sure that the right number of template parameters were
13505 present. */
13506 if (!cp_parser_check_template_parameters (parser, num_templates))
13507 {
13508 /* If something went wrong, there is no point in even trying to
13509 process the class-definition. */
13510 type = NULL_TREE;
13511 goto done;
13512 }
13513
13514 /* Look up the type. */
13515 if (template_id_p)
13516 {
13517 type = TREE_TYPE (id);
13518 type = maybe_process_partial_specialization (type);
13519 if (nested_name_specifier)
13520 pushed_scope = push_scope (nested_name_specifier);
13521 }
13522 else if (nested_name_specifier)
13523 {
13524 tree class_type;
13525
13526 /* Given:
13527
13528 template <typename T> struct S { struct T };
13529 template <typename T> struct S<T>::T { };
13530
13531 we will get a TYPENAME_TYPE when processing the definition of
13532 `S::T'. We need to resolve it to the actual type before we
13533 try to define it. */
13534 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13535 {
13536 class_type = resolve_typename_type (TREE_TYPE (type),
13537 /*only_current_p=*/false);
13538 if (class_type != error_mark_node)
13539 type = TYPE_NAME (class_type);
13540 else
13541 {
13542 cp_parser_error (parser, "could not resolve typename type");
13543 type = error_mark_node;
13544 }
13545 }
13546
13547 maybe_process_partial_specialization (TREE_TYPE (type));
13548 class_type = current_class_type;
13549 /* Enter the scope indicated by the nested-name-specifier. */
13550 pushed_scope = push_scope (nested_name_specifier);
13551 /* Get the canonical version of this type. */
13552 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13553 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13554 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13555 {
13556 type = push_template_decl (type);
13557 if (type == error_mark_node)
13558 {
13559 type = NULL_TREE;
13560 goto done;
13561 }
13562 }
13563
13564 type = TREE_TYPE (type);
13565 *nested_name_specifier_p = true;
13566 }
13567 else /* The name is not a nested name. */
13568 {
13569 /* If the class was unnamed, create a dummy name. */
13570 if (!id)
13571 id = make_anon_name ();
13572 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13573 parser->num_template_parameter_lists);
13574 }
13575
13576 /* Indicate whether this class was declared as a `class' or as a
13577 `struct'. */
13578 if (TREE_CODE (type) == RECORD_TYPE)
13579 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13580 cp_parser_check_class_key (class_key, type);
13581
13582 /* If this type was already complete, and we see another definition,
13583 that's an error. */
13584 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13585 {
13586 error ("redefinition of %q#T", type);
13587 error ("previous definition of %q+#T", type);
13588 type = NULL_TREE;
13589 goto done;
13590 }
13591 else if (type == error_mark_node)
13592 type = NULL_TREE;
13593
13594 /* We will have entered the scope containing the class; the names of
13595 base classes should be looked up in that context. For example:
13596
13597 struct A { struct B {}; struct C; };
13598 struct A::C : B {};
13599
13600 is valid. */
13601 *bases = NULL_TREE;
13602
13603 /* Get the list of base-classes, if there is one. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13605 *bases = cp_parser_base_clause (parser);
13606
13607 done:
13608 /* Leave the scope given by the nested-name-specifier. We will
13609 enter the class scope itself while processing the members. */
13610 if (pushed_scope)
13611 pop_scope (pushed_scope);
13612
13613 if (invalid_explicit_specialization_p)
13614 {
13615 end_specialization ();
13616 --parser->num_template_parameter_lists;
13617 }
13618 *attributes_p = attributes;
13619 return type;
13620 }
13621
13622 /* Parse a class-key.
13623
13624 class-key:
13625 class
13626 struct
13627 union
13628
13629 Returns the kind of class-key specified, or none_type to indicate
13630 error. */
13631
13632 static enum tag_types
cp_parser_class_key(cp_parser * parser)13633 cp_parser_class_key (cp_parser* parser)
13634 {
13635 cp_token *token;
13636 enum tag_types tag_type;
13637
13638 /* Look for the class-key. */
13639 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13640 if (!token)
13641 return none_type;
13642
13643 /* Check to see if the TOKEN is a class-key. */
13644 tag_type = cp_parser_token_is_class_key (token);
13645 if (!tag_type)
13646 cp_parser_error (parser, "expected class-key");
13647 return tag_type;
13648 }
13649
13650 /* Parse an (optional) member-specification.
13651
13652 member-specification:
13653 member-declaration member-specification [opt]
13654 access-specifier : member-specification [opt] */
13655
13656 static void
cp_parser_member_specification_opt(cp_parser * parser)13657 cp_parser_member_specification_opt (cp_parser* parser)
13658 {
13659 while (true)
13660 {
13661 cp_token *token;
13662 enum rid keyword;
13663
13664 /* Peek at the next token. */
13665 token = cp_lexer_peek_token (parser->lexer);
13666 /* If it's a `}', or EOF then we've seen all the members. */
13667 if (token->type == CPP_CLOSE_BRACE
13668 || token->type == CPP_EOF
13669 || token->type == CPP_PRAGMA_EOL)
13670 break;
13671
13672 /* See if this token is a keyword. */
13673 keyword = token->keyword;
13674 switch (keyword)
13675 {
13676 case RID_PUBLIC:
13677 case RID_PROTECTED:
13678 case RID_PRIVATE:
13679 /* Consume the access-specifier. */
13680 cp_lexer_consume_token (parser->lexer);
13681 /* Remember which access-specifier is active. */
13682 current_access_specifier = token->u.value;
13683 /* Look for the `:'. */
13684 cp_parser_require (parser, CPP_COLON, "`:'");
13685 break;
13686
13687 default:
13688 /* Accept #pragmas at class scope. */
13689 if (token->type == CPP_PRAGMA)
13690 {
13691 cp_parser_pragma (parser, pragma_external);
13692 break;
13693 }
13694
13695 /* Otherwise, the next construction must be a
13696 member-declaration. */
13697 cp_parser_member_declaration (parser);
13698 }
13699 }
13700 }
13701
13702 /* Parse a member-declaration.
13703
13704 member-declaration:
13705 decl-specifier-seq [opt] member-declarator-list [opt] ;
13706 function-definition ; [opt]
13707 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13708 using-declaration
13709 template-declaration
13710
13711 member-declarator-list:
13712 member-declarator
13713 member-declarator-list , member-declarator
13714
13715 member-declarator:
13716 declarator pure-specifier [opt]
13717 declarator constant-initializer [opt]
13718 identifier [opt] : constant-expression
13719
13720 GNU Extensions:
13721
13722 member-declaration:
13723 __extension__ member-declaration
13724
13725 member-declarator:
13726 declarator attributes [opt] pure-specifier [opt]
13727 declarator attributes [opt] constant-initializer [opt]
13728 identifier [opt] attributes [opt] : constant-expression */
13729
13730 static void
cp_parser_member_declaration(cp_parser * parser)13731 cp_parser_member_declaration (cp_parser* parser)
13732 {
13733 cp_decl_specifier_seq decl_specifiers;
13734 tree prefix_attributes;
13735 tree decl;
13736 int declares_class_or_enum;
13737 bool friend_p;
13738 cp_token *token;
13739 int saved_pedantic;
13740
13741 /* Check for the `__extension__' keyword. */
13742 if (cp_parser_extension_opt (parser, &saved_pedantic))
13743 {
13744 /* Recurse. */
13745 cp_parser_member_declaration (parser);
13746 /* Restore the old value of the PEDANTIC flag. */
13747 pedantic = saved_pedantic;
13748
13749 return;
13750 }
13751
13752 /* Check for a template-declaration. */
13753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13754 {
13755 /* An explicit specialization here is an error condition, and we
13756 expect the specialization handler to detect and report this. */
13757 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13758 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13759 cp_parser_explicit_specialization (parser);
13760 else
13761 cp_parser_template_declaration (parser, /*member_p=*/true);
13762
13763 return;
13764 }
13765
13766 /* Check for a using-declaration. */
13767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13768 {
13769 /* Parse the using-declaration. */
13770 cp_parser_using_declaration (parser,
13771 /*access_declaration_p=*/false);
13772 return;
13773 }
13774
13775 /* Check for @defs. */
13776 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13777 {
13778 tree ivar, member;
13779 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13780 ivar = ivar_chains;
13781 while (ivar)
13782 {
13783 member = ivar;
13784 ivar = TREE_CHAIN (member);
13785 TREE_CHAIN (member) = NULL_TREE;
13786 finish_member_declaration (member);
13787 }
13788 return;
13789 }
13790
13791 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13792 return;
13793
13794 /* Parse the decl-specifier-seq. */
13795 cp_parser_decl_specifier_seq (parser,
13796 CP_PARSER_FLAGS_OPTIONAL,
13797 &decl_specifiers,
13798 &declares_class_or_enum);
13799 prefix_attributes = decl_specifiers.attributes;
13800 decl_specifiers.attributes = NULL_TREE;
13801 /* Check for an invalid type-name. */
13802 if (!decl_specifiers.type
13803 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13804 return;
13805 /* If there is no declarator, then the decl-specifier-seq should
13806 specify a type. */
13807 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13808 {
13809 /* If there was no decl-specifier-seq, and the next token is a
13810 `;', then we have something like:
13811
13812 struct S { ; };
13813
13814 [class.mem]
13815
13816 Each member-declaration shall declare at least one member
13817 name of the class. */
13818 if (!decl_specifiers.any_specifiers_p)
13819 {
13820 cp_token *token = cp_lexer_peek_token (parser->lexer);
13821 if (pedantic && !token->in_system_header)
13822 pedwarn ("%Hextra %<;%>", &token->location);
13823 }
13824 else
13825 {
13826 tree type;
13827
13828 /* See if this declaration is a friend. */
13829 friend_p = cp_parser_friend_p (&decl_specifiers);
13830 /* If there were decl-specifiers, check to see if there was
13831 a class-declaration. */
13832 type = check_tag_decl (&decl_specifiers);
13833 /* Nested classes have already been added to the class, but
13834 a `friend' needs to be explicitly registered. */
13835 if (friend_p)
13836 {
13837 /* If the `friend' keyword was present, the friend must
13838 be introduced with a class-key. */
13839 if (!declares_class_or_enum)
13840 error ("a class-key must be used when declaring a friend");
13841 /* In this case:
13842
13843 template <typename T> struct A {
13844 friend struct A<T>::B;
13845 };
13846
13847 A<T>::B will be represented by a TYPENAME_TYPE, and
13848 therefore not recognized by check_tag_decl. */
13849 if (!type
13850 && decl_specifiers.type
13851 && TYPE_P (decl_specifiers.type))
13852 type = decl_specifiers.type;
13853 if (!type || !TYPE_P (type))
13854 error ("friend declaration does not name a class or "
13855 "function");
13856 else
13857 make_friend_class (current_class_type, type,
13858 /*complain=*/true);
13859 }
13860 /* If there is no TYPE, an error message will already have
13861 been issued. */
13862 else if (!type || type == error_mark_node)
13863 ;
13864 /* An anonymous aggregate has to be handled specially; such
13865 a declaration really declares a data member (with a
13866 particular type), as opposed to a nested class. */
13867 else if (ANON_AGGR_TYPE_P (type))
13868 {
13869 /* Remove constructors and such from TYPE, now that we
13870 know it is an anonymous aggregate. */
13871 fixup_anonymous_aggr (type);
13872 /* And make the corresponding data member. */
13873 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13874 /* Add it to the class. */
13875 finish_member_declaration (decl);
13876 }
13877 else
13878 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13879 }
13880 }
13881 else
13882 {
13883 /* See if these declarations will be friends. */
13884 friend_p = cp_parser_friend_p (&decl_specifiers);
13885
13886 /* Keep going until we hit the `;' at the end of the
13887 declaration. */
13888 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13889 {
13890 tree attributes = NULL_TREE;
13891 tree first_attribute;
13892
13893 /* Peek at the next token. */
13894 token = cp_lexer_peek_token (parser->lexer);
13895
13896 /* Check for a bitfield declaration. */
13897 if (token->type == CPP_COLON
13898 || (token->type == CPP_NAME
13899 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13900 == CPP_COLON))
13901 {
13902 tree identifier;
13903 tree width;
13904
13905 /* Get the name of the bitfield. Note that we cannot just
13906 check TOKEN here because it may have been invalidated by
13907 the call to cp_lexer_peek_nth_token above. */
13908 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13909 identifier = cp_parser_identifier (parser);
13910 else
13911 identifier = NULL_TREE;
13912
13913 /* Consume the `:' token. */
13914 cp_lexer_consume_token (parser->lexer);
13915 /* Get the width of the bitfield. */
13916 width
13917 = cp_parser_constant_expression (parser,
13918 /*allow_non_constant=*/false,
13919 NULL);
13920
13921 /* Look for attributes that apply to the bitfield. */
13922 attributes = cp_parser_attributes_opt (parser);
13923 /* Remember which attributes are prefix attributes and
13924 which are not. */
13925 first_attribute = attributes;
13926 /* Combine the attributes. */
13927 attributes = chainon (prefix_attributes, attributes);
13928
13929 /* Create the bitfield declaration. */
13930 decl = grokbitfield (identifier
13931 ? make_id_declarator (NULL_TREE,
13932 identifier,
13933 sfk_none)
13934 : NULL,
13935 &decl_specifiers,
13936 width);
13937 /* Apply the attributes. */
13938 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13939 }
13940 else
13941 {
13942 cp_declarator *declarator;
13943 tree initializer;
13944 tree asm_specification;
13945 int ctor_dtor_or_conv_p;
13946
13947 /* Parse the declarator. */
13948 declarator
13949 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13950 &ctor_dtor_or_conv_p,
13951 /*parenthesized_p=*/NULL,
13952 /*member_p=*/true);
13953
13954 /* If something went wrong parsing the declarator, make sure
13955 that we at least consume some tokens. */
13956 if (declarator == cp_error_declarator)
13957 {
13958 /* Skip to the end of the statement. */
13959 cp_parser_skip_to_end_of_statement (parser);
13960 /* If the next token is not a semicolon, that is
13961 probably because we just skipped over the body of
13962 a function. So, we consume a semicolon if
13963 present, but do not issue an error message if it
13964 is not present. */
13965 if (cp_lexer_next_token_is (parser->lexer,
13966 CPP_SEMICOLON))
13967 cp_lexer_consume_token (parser->lexer);
13968 return;
13969 }
13970
13971 if (declares_class_or_enum & 2)
13972 cp_parser_check_for_definition_in_return_type
13973 (declarator, decl_specifiers.type);
13974
13975 /* Look for an asm-specification. */
13976 asm_specification = cp_parser_asm_specification_opt (parser);
13977 /* Look for attributes that apply to the declaration. */
13978 attributes = cp_parser_attributes_opt (parser);
13979 /* Remember which attributes are prefix attributes and
13980 which are not. */
13981 first_attribute = attributes;
13982 /* Combine the attributes. */
13983 attributes = chainon (prefix_attributes, attributes);
13984
13985 /* If it's an `=', then we have a constant-initializer or a
13986 pure-specifier. It is not correct to parse the
13987 initializer before registering the member declaration
13988 since the member declaration should be in scope while
13989 its initializer is processed. However, the rest of the
13990 front end does not yet provide an interface that allows
13991 us to handle this correctly. */
13992 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13993 {
13994 /* In [class.mem]:
13995
13996 A pure-specifier shall be used only in the declaration of
13997 a virtual function.
13998
13999 A member-declarator can contain a constant-initializer
14000 only if it declares a static member of integral or
14001 enumeration type.
14002
14003 Therefore, if the DECLARATOR is for a function, we look
14004 for a pure-specifier; otherwise, we look for a
14005 constant-initializer. When we call `grokfield', it will
14006 perform more stringent semantics checks. */
14007 if (function_declarator_p (declarator))
14008 initializer = cp_parser_pure_specifier (parser);
14009 else
14010 /* Parse the initializer. */
14011 initializer = cp_parser_constant_initializer (parser);
14012 }
14013 /* Otherwise, there is no initializer. */
14014 else
14015 initializer = NULL_TREE;
14016
14017 /* See if we are probably looking at a function
14018 definition. We are certainly not looking at a
14019 member-declarator. Calling `grokfield' has
14020 side-effects, so we must not do it unless we are sure
14021 that we are looking at a member-declarator. */
14022 if (cp_parser_token_starts_function_definition_p
14023 (cp_lexer_peek_token (parser->lexer)))
14024 {
14025 /* The grammar does not allow a pure-specifier to be
14026 used when a member function is defined. (It is
14027 possible that this fact is an oversight in the
14028 standard, since a pure function may be defined
14029 outside of the class-specifier. */
14030 if (initializer)
14031 error ("pure-specifier on function-definition");
14032 decl = cp_parser_save_member_function_body (parser,
14033 &decl_specifiers,
14034 declarator,
14035 attributes);
14036 /* If the member was not a friend, declare it here. */
14037 if (!friend_p)
14038 finish_member_declaration (decl);
14039 /* Peek at the next token. */
14040 token = cp_lexer_peek_token (parser->lexer);
14041 /* If the next token is a semicolon, consume it. */
14042 if (token->type == CPP_SEMICOLON)
14043 cp_lexer_consume_token (parser->lexer);
14044 return;
14045 }
14046 else
14047 /* Create the declaration. */
14048 decl = grokfield (declarator, &decl_specifiers,
14049 initializer, /*init_const_expr_p=*/true,
14050 asm_specification,
14051 attributes);
14052 }
14053
14054 /* Reset PREFIX_ATTRIBUTES. */
14055 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14056 attributes = TREE_CHAIN (attributes);
14057 if (attributes)
14058 TREE_CHAIN (attributes) = NULL_TREE;
14059
14060 /* If there is any qualification still in effect, clear it
14061 now; we will be starting fresh with the next declarator. */
14062 parser->scope = NULL_TREE;
14063 parser->qualifying_scope = NULL_TREE;
14064 parser->object_scope = NULL_TREE;
14065 /* If it's a `,', then there are more declarators. */
14066 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14067 cp_lexer_consume_token (parser->lexer);
14068 /* If the next token isn't a `;', then we have a parse error. */
14069 else if (cp_lexer_next_token_is_not (parser->lexer,
14070 CPP_SEMICOLON))
14071 {
14072 cp_parser_error (parser, "expected %<;%>");
14073 /* Skip tokens until we find a `;'. */
14074 cp_parser_skip_to_end_of_statement (parser);
14075
14076 break;
14077 }
14078
14079 if (decl)
14080 {
14081 /* Add DECL to the list of members. */
14082 if (!friend_p)
14083 finish_member_declaration (decl);
14084
14085 if (TREE_CODE (decl) == FUNCTION_DECL)
14086 cp_parser_save_default_args (parser, decl);
14087 }
14088 }
14089 }
14090
14091 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14092 }
14093
14094 /* Parse a pure-specifier.
14095
14096 pure-specifier:
14097 = 0
14098
14099 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14100 Otherwise, ERROR_MARK_NODE is returned. */
14101
14102 static tree
cp_parser_pure_specifier(cp_parser * parser)14103 cp_parser_pure_specifier (cp_parser* parser)
14104 {
14105 cp_token *token;
14106
14107 /* Look for the `=' token. */
14108 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14109 return error_mark_node;
14110 /* Look for the `0' token. */
14111 token = cp_lexer_consume_token (parser->lexer);
14112 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14113 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14114 {
14115 cp_parser_error (parser,
14116 "invalid pure specifier (only `= 0' is allowed)");
14117 cp_parser_skip_to_end_of_statement (parser);
14118 return error_mark_node;
14119 }
14120 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14121 {
14122 error ("templates may not be %<virtual%>");
14123 return error_mark_node;
14124 }
14125
14126 return integer_zero_node;
14127 }
14128
14129 /* Parse a constant-initializer.
14130
14131 constant-initializer:
14132 = constant-expression
14133
14134 Returns a representation of the constant-expression. */
14135
14136 static tree
cp_parser_constant_initializer(cp_parser * parser)14137 cp_parser_constant_initializer (cp_parser* parser)
14138 {
14139 /* Look for the `=' token. */
14140 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14141 return error_mark_node;
14142
14143 /* It is invalid to write:
14144
14145 struct S { static const int i = { 7 }; };
14146
14147 */
14148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14149 {
14150 cp_parser_error (parser,
14151 "a brace-enclosed initializer is not allowed here");
14152 /* Consume the opening brace. */
14153 cp_lexer_consume_token (parser->lexer);
14154 /* Skip the initializer. */
14155 cp_parser_skip_to_closing_brace (parser);
14156 /* Look for the trailing `}'. */
14157 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14158
14159 return error_mark_node;
14160 }
14161
14162 return cp_parser_constant_expression (parser,
14163 /*allow_non_constant=*/false,
14164 NULL);
14165 }
14166
14167 /* Derived classes [gram.class.derived] */
14168
14169 /* Parse a base-clause.
14170
14171 base-clause:
14172 : base-specifier-list
14173
14174 base-specifier-list:
14175 base-specifier
14176 base-specifier-list , base-specifier
14177
14178 Returns a TREE_LIST representing the base-classes, in the order in
14179 which they were declared. The representation of each node is as
14180 described by cp_parser_base_specifier.
14181
14182 In the case that no bases are specified, this function will return
14183 NULL_TREE, not ERROR_MARK_NODE. */
14184
14185 static tree
cp_parser_base_clause(cp_parser * parser)14186 cp_parser_base_clause (cp_parser* parser)
14187 {
14188 tree bases = NULL_TREE;
14189
14190 /* Look for the `:' that begins the list. */
14191 cp_parser_require (parser, CPP_COLON, "`:'");
14192
14193 /* Scan the base-specifier-list. */
14194 while (true)
14195 {
14196 cp_token *token;
14197 tree base;
14198
14199 /* Look for the base-specifier. */
14200 base = cp_parser_base_specifier (parser);
14201 /* Add BASE to the front of the list. */
14202 if (base != error_mark_node)
14203 {
14204 TREE_CHAIN (base) = bases;
14205 bases = base;
14206 }
14207 /* Peek at the next token. */
14208 token = cp_lexer_peek_token (parser->lexer);
14209 /* If it's not a comma, then the list is complete. */
14210 if (token->type != CPP_COMMA)
14211 break;
14212 /* Consume the `,'. */
14213 cp_lexer_consume_token (parser->lexer);
14214 }
14215
14216 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14217 base class had a qualified name. However, the next name that
14218 appears is certainly not qualified. */
14219 parser->scope = NULL_TREE;
14220 parser->qualifying_scope = NULL_TREE;
14221 parser->object_scope = NULL_TREE;
14222
14223 return nreverse (bases);
14224 }
14225
14226 /* Parse a base-specifier.
14227
14228 base-specifier:
14229 :: [opt] nested-name-specifier [opt] class-name
14230 virtual access-specifier [opt] :: [opt] nested-name-specifier
14231 [opt] class-name
14232 access-specifier virtual [opt] :: [opt] nested-name-specifier
14233 [opt] class-name
14234
14235 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14236 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14237 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14238 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14239
14240 static tree
cp_parser_base_specifier(cp_parser * parser)14241 cp_parser_base_specifier (cp_parser* parser)
14242 {
14243 cp_token *token;
14244 bool done = false;
14245 bool virtual_p = false;
14246 bool duplicate_virtual_error_issued_p = false;
14247 bool duplicate_access_error_issued_p = false;
14248 bool class_scope_p, template_p;
14249 tree access = access_default_node;
14250 tree type;
14251
14252 /* Process the optional `virtual' and `access-specifier'. */
14253 while (!done)
14254 {
14255 /* Peek at the next token. */
14256 token = cp_lexer_peek_token (parser->lexer);
14257 /* Process `virtual'. */
14258 switch (token->keyword)
14259 {
14260 case RID_VIRTUAL:
14261 /* If `virtual' appears more than once, issue an error. */
14262 if (virtual_p && !duplicate_virtual_error_issued_p)
14263 {
14264 cp_parser_error (parser,
14265 "%<virtual%> specified more than once in base-specified");
14266 duplicate_virtual_error_issued_p = true;
14267 }
14268
14269 virtual_p = true;
14270
14271 /* Consume the `virtual' token. */
14272 cp_lexer_consume_token (parser->lexer);
14273
14274 break;
14275
14276 case RID_PUBLIC:
14277 case RID_PROTECTED:
14278 case RID_PRIVATE:
14279 /* If more than one access specifier appears, issue an
14280 error. */
14281 if (access != access_default_node
14282 && !duplicate_access_error_issued_p)
14283 {
14284 cp_parser_error (parser,
14285 "more than one access specifier in base-specified");
14286 duplicate_access_error_issued_p = true;
14287 }
14288
14289 access = ridpointers[(int) token->keyword];
14290
14291 /* Consume the access-specifier. */
14292 cp_lexer_consume_token (parser->lexer);
14293
14294 break;
14295
14296 default:
14297 done = true;
14298 break;
14299 }
14300 }
14301 /* It is not uncommon to see programs mechanically, erroneously, use
14302 the 'typename' keyword to denote (dependent) qualified types
14303 as base classes. */
14304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14305 {
14306 if (!processing_template_decl)
14307 error ("keyword %<typename%> not allowed outside of templates");
14308 else
14309 error ("keyword %<typename%> not allowed in this context "
14310 "(the base class is implicitly a type)");
14311 cp_lexer_consume_token (parser->lexer);
14312 }
14313
14314 /* Look for the optional `::' operator. */
14315 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14316 /* Look for the nested-name-specifier. The simplest way to
14317 implement:
14318
14319 [temp.res]
14320
14321 The keyword `typename' is not permitted in a base-specifier or
14322 mem-initializer; in these contexts a qualified name that
14323 depends on a template-parameter is implicitly assumed to be a
14324 type name.
14325
14326 is to pretend that we have seen the `typename' keyword at this
14327 point. */
14328 cp_parser_nested_name_specifier_opt (parser,
14329 /*typename_keyword_p=*/true,
14330 /*check_dependency_p=*/true,
14331 typename_type,
14332 /*is_declaration=*/true);
14333 /* If the base class is given by a qualified name, assume that names
14334 we see are type names or templates, as appropriate. */
14335 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14336 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14337
14338 /* Finally, look for the class-name. */
14339 type = cp_parser_class_name (parser,
14340 class_scope_p,
14341 template_p,
14342 typename_type,
14343 /*check_dependency_p=*/true,
14344 /*class_head_p=*/false,
14345 /*is_declaration=*/true);
14346
14347 if (type == error_mark_node)
14348 return error_mark_node;
14349
14350 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14351 }
14352
14353 /* Exception handling [gram.exception] */
14354
14355 /* Parse an (optional) exception-specification.
14356
14357 exception-specification:
14358 throw ( type-id-list [opt] )
14359
14360 Returns a TREE_LIST representing the exception-specification. The
14361 TREE_VALUE of each node is a type. */
14362
14363 static tree
cp_parser_exception_specification_opt(cp_parser * parser)14364 cp_parser_exception_specification_opt (cp_parser* parser)
14365 {
14366 cp_token *token;
14367 tree type_id_list;
14368
14369 /* Peek at the next token. */
14370 token = cp_lexer_peek_token (parser->lexer);
14371 /* If it's not `throw', then there's no exception-specification. */
14372 if (!cp_parser_is_keyword (token, RID_THROW))
14373 return NULL_TREE;
14374
14375 /* Consume the `throw'. */
14376 cp_lexer_consume_token (parser->lexer);
14377
14378 /* Look for the `('. */
14379 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14380
14381 /* Peek at the next token. */
14382 token = cp_lexer_peek_token (parser->lexer);
14383 /* If it's not a `)', then there is a type-id-list. */
14384 if (token->type != CPP_CLOSE_PAREN)
14385 {
14386 const char *saved_message;
14387
14388 /* Types may not be defined in an exception-specification. */
14389 saved_message = parser->type_definition_forbidden_message;
14390 parser->type_definition_forbidden_message
14391 = "types may not be defined in an exception-specification";
14392 /* Parse the type-id-list. */
14393 type_id_list = cp_parser_type_id_list (parser);
14394 /* Restore the saved message. */
14395 parser->type_definition_forbidden_message = saved_message;
14396 }
14397 else
14398 type_id_list = empty_except_spec;
14399
14400 /* Look for the `)'. */
14401 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14402
14403 return type_id_list;
14404 }
14405
14406 /* Parse an (optional) type-id-list.
14407
14408 type-id-list:
14409 type-id
14410 type-id-list , type-id
14411
14412 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14413 in the order that the types were presented. */
14414
14415 static tree
cp_parser_type_id_list(cp_parser * parser)14416 cp_parser_type_id_list (cp_parser* parser)
14417 {
14418 tree types = NULL_TREE;
14419
14420 while (true)
14421 {
14422 cp_token *token;
14423 tree type;
14424
14425 /* Get the next type-id. */
14426 type = cp_parser_type_id (parser);
14427 /* Add it to the list. */
14428 types = add_exception_specifier (types, type, /*complain=*/1);
14429 /* Peek at the next token. */
14430 token = cp_lexer_peek_token (parser->lexer);
14431 /* If it is not a `,', we are done. */
14432 if (token->type != CPP_COMMA)
14433 break;
14434 /* Consume the `,'. */
14435 cp_lexer_consume_token (parser->lexer);
14436 }
14437
14438 return nreverse (types);
14439 }
14440
14441 /* Parse a try-block.
14442
14443 try-block:
14444 try compound-statement handler-seq */
14445
14446 static tree
cp_parser_try_block(cp_parser * parser)14447 cp_parser_try_block (cp_parser* parser)
14448 {
14449 tree try_block;
14450
14451 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14452 try_block = begin_try_block ();
14453 cp_parser_compound_statement (parser, NULL, true);
14454 finish_try_block (try_block);
14455 cp_parser_handler_seq (parser);
14456 finish_handler_sequence (try_block);
14457
14458 return try_block;
14459 }
14460
14461 /* Parse a function-try-block.
14462
14463 function-try-block:
14464 try ctor-initializer [opt] function-body handler-seq */
14465
14466 static bool
cp_parser_function_try_block(cp_parser * parser)14467 cp_parser_function_try_block (cp_parser* parser)
14468 {
14469 tree compound_stmt;
14470 tree try_block;
14471 bool ctor_initializer_p;
14472
14473 /* Look for the `try' keyword. */
14474 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14475 return false;
14476 /* Let the rest of the front-end know where we are. */
14477 try_block = begin_function_try_block (&compound_stmt);
14478 /* Parse the function-body. */
14479 ctor_initializer_p
14480 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14481 /* We're done with the `try' part. */
14482 finish_function_try_block (try_block);
14483 /* Parse the handlers. */
14484 cp_parser_handler_seq (parser);
14485 /* We're done with the handlers. */
14486 finish_function_handler_sequence (try_block, compound_stmt);
14487
14488 return ctor_initializer_p;
14489 }
14490
14491 /* Parse a handler-seq.
14492
14493 handler-seq:
14494 handler handler-seq [opt] */
14495
14496 static void
cp_parser_handler_seq(cp_parser * parser)14497 cp_parser_handler_seq (cp_parser* parser)
14498 {
14499 while (true)
14500 {
14501 cp_token *token;
14502
14503 /* Parse the handler. */
14504 cp_parser_handler (parser);
14505 /* Peek at the next token. */
14506 token = cp_lexer_peek_token (parser->lexer);
14507 /* If it's not `catch' then there are no more handlers. */
14508 if (!cp_parser_is_keyword (token, RID_CATCH))
14509 break;
14510 }
14511 }
14512
14513 /* Parse a handler.
14514
14515 handler:
14516 catch ( exception-declaration ) compound-statement */
14517
14518 static void
cp_parser_handler(cp_parser * parser)14519 cp_parser_handler (cp_parser* parser)
14520 {
14521 tree handler;
14522 tree declaration;
14523
14524 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14525 handler = begin_handler ();
14526 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14527 declaration = cp_parser_exception_declaration (parser);
14528 finish_handler_parms (declaration, handler);
14529 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14530 cp_parser_compound_statement (parser, NULL, false);
14531 finish_handler (handler);
14532 }
14533
14534 /* Parse an exception-declaration.
14535
14536 exception-declaration:
14537 type-specifier-seq declarator
14538 type-specifier-seq abstract-declarator
14539 type-specifier-seq
14540 ...
14541
14542 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14543 ellipsis variant is used. */
14544
14545 static tree
cp_parser_exception_declaration(cp_parser * parser)14546 cp_parser_exception_declaration (cp_parser* parser)
14547 {
14548 cp_decl_specifier_seq type_specifiers;
14549 cp_declarator *declarator;
14550 const char *saved_message;
14551
14552 /* If it's an ellipsis, it's easy to handle. */
14553 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14554 {
14555 /* Consume the `...' token. */
14556 cp_lexer_consume_token (parser->lexer);
14557 return NULL_TREE;
14558 }
14559
14560 /* Types may not be defined in exception-declarations. */
14561 saved_message = parser->type_definition_forbidden_message;
14562 parser->type_definition_forbidden_message
14563 = "types may not be defined in exception-declarations";
14564
14565 /* Parse the type-specifier-seq. */
14566 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14567 &type_specifiers);
14568 /* If it's a `)', then there is no declarator. */
14569 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14570 declarator = NULL;
14571 else
14572 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14573 /*ctor_dtor_or_conv_p=*/NULL,
14574 /*parenthesized_p=*/NULL,
14575 /*member_p=*/false);
14576
14577 /* Restore the saved message. */
14578 parser->type_definition_forbidden_message = saved_message;
14579
14580 if (!type_specifiers.any_specifiers_p)
14581 return error_mark_node;
14582
14583 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14584 }
14585
14586 /* Parse a throw-expression.
14587
14588 throw-expression:
14589 throw assignment-expression [opt]
14590
14591 Returns a THROW_EXPR representing the throw-expression. */
14592
14593 static tree
cp_parser_throw_expression(cp_parser * parser)14594 cp_parser_throw_expression (cp_parser* parser)
14595 {
14596 tree expression;
14597 cp_token* token;
14598
14599 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14600 token = cp_lexer_peek_token (parser->lexer);
14601 /* Figure out whether or not there is an assignment-expression
14602 following the "throw" keyword. */
14603 if (token->type == CPP_COMMA
14604 || token->type == CPP_SEMICOLON
14605 || token->type == CPP_CLOSE_PAREN
14606 || token->type == CPP_CLOSE_SQUARE
14607 || token->type == CPP_CLOSE_BRACE
14608 || token->type == CPP_COLON)
14609 expression = NULL_TREE;
14610 else
14611 expression = cp_parser_assignment_expression (parser,
14612 /*cast_p=*/false);
14613
14614 return build_throw (expression);
14615 }
14616
14617 /* GNU Extensions */
14618
14619 /* Parse an (optional) asm-specification.
14620
14621 asm-specification:
14622 asm ( string-literal )
14623
14624 If the asm-specification is present, returns a STRING_CST
14625 corresponding to the string-literal. Otherwise, returns
14626 NULL_TREE. */
14627
14628 static tree
cp_parser_asm_specification_opt(cp_parser * parser)14629 cp_parser_asm_specification_opt (cp_parser* parser)
14630 {
14631 cp_token *token;
14632 tree asm_specification;
14633
14634 /* Peek at the next token. */
14635 token = cp_lexer_peek_token (parser->lexer);
14636 /* If the next token isn't the `asm' keyword, then there's no
14637 asm-specification. */
14638 if (!cp_parser_is_keyword (token, RID_ASM))
14639 return NULL_TREE;
14640
14641 /* Consume the `asm' token. */
14642 cp_lexer_consume_token (parser->lexer);
14643 /* Look for the `('. */
14644 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14645
14646 /* Look for the string-literal. */
14647 asm_specification = cp_parser_string_literal (parser, false, false);
14648
14649 /* Look for the `)'. */
14650 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14651
14652 return asm_specification;
14653 }
14654
14655 /* Parse an asm-operand-list.
14656
14657 asm-operand-list:
14658 asm-operand
14659 asm-operand-list , asm-operand
14660
14661 asm-operand:
14662 string-literal ( expression )
14663 [ string-literal ] string-literal ( expression )
14664
14665 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14666 each node is the expression. The TREE_PURPOSE is itself a
14667 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14668 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14669 is a STRING_CST for the string literal before the parenthesis. */
14670
14671 static tree
cp_parser_asm_operand_list(cp_parser * parser)14672 cp_parser_asm_operand_list (cp_parser* parser)
14673 {
14674 tree asm_operands = NULL_TREE;
14675
14676 while (true)
14677 {
14678 tree string_literal;
14679 tree expression;
14680 tree name;
14681
14682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14683 {
14684 /* Consume the `[' token. */
14685 cp_lexer_consume_token (parser->lexer);
14686 /* Read the operand name. */
14687 name = cp_parser_identifier (parser);
14688 if (name != error_mark_node)
14689 name = build_string (IDENTIFIER_LENGTH (name),
14690 IDENTIFIER_POINTER (name));
14691 /* Look for the closing `]'. */
14692 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14693 }
14694 else
14695 name = NULL_TREE;
14696 /* Look for the string-literal. */
14697 string_literal = cp_parser_string_literal (parser, false, false);
14698
14699 /* Look for the `('. */
14700 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14701 /* Parse the expression. */
14702 expression = cp_parser_expression (parser, /*cast_p=*/false);
14703 /* Look for the `)'. */
14704 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14705
14706 /* Add this operand to the list. */
14707 asm_operands = tree_cons (build_tree_list (name, string_literal),
14708 expression,
14709 asm_operands);
14710 /* If the next token is not a `,', there are no more
14711 operands. */
14712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14713 break;
14714 /* Consume the `,'. */
14715 cp_lexer_consume_token (parser->lexer);
14716 }
14717
14718 return nreverse (asm_operands);
14719 }
14720
14721 /* Parse an asm-clobber-list.
14722
14723 asm-clobber-list:
14724 string-literal
14725 asm-clobber-list , string-literal
14726
14727 Returns a TREE_LIST, indicating the clobbers in the order that they
14728 appeared. The TREE_VALUE of each node is a STRING_CST. */
14729
14730 static tree
cp_parser_asm_clobber_list(cp_parser * parser)14731 cp_parser_asm_clobber_list (cp_parser* parser)
14732 {
14733 tree clobbers = NULL_TREE;
14734
14735 while (true)
14736 {
14737 tree string_literal;
14738
14739 /* Look for the string literal. */
14740 string_literal = cp_parser_string_literal (parser, false, false);
14741 /* Add it to the list. */
14742 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14743 /* If the next token is not a `,', then the list is
14744 complete. */
14745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14746 break;
14747 /* Consume the `,' token. */
14748 cp_lexer_consume_token (parser->lexer);
14749 }
14750
14751 return clobbers;
14752 }
14753
14754 /* Parse an (optional) series of attributes.
14755
14756 attributes:
14757 attributes attribute
14758
14759 attribute:
14760 __attribute__ (( attribute-list [opt] ))
14761
14762 The return value is as for cp_parser_attribute_list. */
14763
14764 static tree
cp_parser_attributes_opt(cp_parser * parser)14765 cp_parser_attributes_opt (cp_parser* parser)
14766 {
14767 tree attributes = NULL_TREE;
14768
14769 while (true)
14770 {
14771 cp_token *token;
14772 tree attribute_list;
14773
14774 /* Peek at the next token. */
14775 token = cp_lexer_peek_token (parser->lexer);
14776 /* If it's not `__attribute__', then we're done. */
14777 if (token->keyword != RID_ATTRIBUTE)
14778 break;
14779
14780 /* Consume the `__attribute__' keyword. */
14781 cp_lexer_consume_token (parser->lexer);
14782 /* Look for the two `(' tokens. */
14783 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14784 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14785
14786 /* Peek at the next token. */
14787 token = cp_lexer_peek_token (parser->lexer);
14788 if (token->type != CPP_CLOSE_PAREN)
14789 /* Parse the attribute-list. */
14790 attribute_list = cp_parser_attribute_list (parser);
14791 else
14792 /* If the next token is a `)', then there is no attribute
14793 list. */
14794 attribute_list = NULL;
14795
14796 /* Look for the two `)' tokens. */
14797 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14798 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14799
14800 /* Add these new attributes to the list. */
14801 attributes = chainon (attributes, attribute_list);
14802 }
14803
14804 return attributes;
14805 }
14806
14807 /* Parse an attribute-list.
14808
14809 attribute-list:
14810 attribute
14811 attribute-list , attribute
14812
14813 attribute:
14814 identifier
14815 identifier ( identifier )
14816 identifier ( identifier , expression-list )
14817 identifier ( expression-list )
14818
14819 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14820 to an attribute. The TREE_PURPOSE of each node is the identifier
14821 indicating which attribute is in use. The TREE_VALUE represents
14822 the arguments, if any. */
14823
14824 static tree
cp_parser_attribute_list(cp_parser * parser)14825 cp_parser_attribute_list (cp_parser* parser)
14826 {
14827 tree attribute_list = NULL_TREE;
14828 bool save_translate_strings_p = parser->translate_strings_p;
14829
14830 parser->translate_strings_p = false;
14831 while (true)
14832 {
14833 cp_token *token;
14834 tree identifier;
14835 tree attribute;
14836
14837 /* Look for the identifier. We also allow keywords here; for
14838 example `__attribute__ ((const))' is legal. */
14839 token = cp_lexer_peek_token (parser->lexer);
14840 if (token->type == CPP_NAME
14841 || token->type == CPP_KEYWORD)
14842 {
14843 tree arguments = NULL_TREE;
14844
14845 /* Consume the token. */
14846 token = cp_lexer_consume_token (parser->lexer);
14847
14848 /* Save away the identifier that indicates which attribute
14849 this is. */
14850 identifier = token->u.value;
14851 attribute = build_tree_list (identifier, NULL_TREE);
14852
14853 /* Peek at the next token. */
14854 token = cp_lexer_peek_token (parser->lexer);
14855 /* If it's an `(', then parse the attribute arguments. */
14856 if (token->type == CPP_OPEN_PAREN)
14857 {
14858 arguments = cp_parser_parenthesized_expression_list
14859 (parser, true, /*cast_p=*/false,
14860 /*non_constant_p=*/NULL);
14861 /* Save the arguments away. */
14862 TREE_VALUE (attribute) = arguments;
14863 }
14864
14865 if (arguments != error_mark_node)
14866 {
14867 /* Add this attribute to the list. */
14868 TREE_CHAIN (attribute) = attribute_list;
14869 attribute_list = attribute;
14870 }
14871
14872 token = cp_lexer_peek_token (parser->lexer);
14873 }
14874 /* Now, look for more attributes. If the next token isn't a
14875 `,', we're done. */
14876 if (token->type != CPP_COMMA)
14877 break;
14878
14879 /* Consume the comma and keep going. */
14880 cp_lexer_consume_token (parser->lexer);
14881 }
14882 parser->translate_strings_p = save_translate_strings_p;
14883
14884 /* We built up the list in reverse order. */
14885 return nreverse (attribute_list);
14886 }
14887
14888 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14889 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14890 current value of the PEDANTIC flag, regardless of whether or not
14891 the `__extension__' keyword is present. The caller is responsible
14892 for restoring the value of the PEDANTIC flag. */
14893
14894 static bool
cp_parser_extension_opt(cp_parser * parser,int * saved_pedantic)14895 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14896 {
14897 /* Save the old value of the PEDANTIC flag. */
14898 *saved_pedantic = pedantic;
14899
14900 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14901 {
14902 /* Consume the `__extension__' token. */
14903 cp_lexer_consume_token (parser->lexer);
14904 /* We're not being pedantic while the `__extension__' keyword is
14905 in effect. */
14906 pedantic = 0;
14907
14908 return true;
14909 }
14910
14911 return false;
14912 }
14913
14914 /* Parse a label declaration.
14915
14916 label-declaration:
14917 __label__ label-declarator-seq ;
14918
14919 label-declarator-seq:
14920 identifier , label-declarator-seq
14921 identifier */
14922
14923 static void
cp_parser_label_declaration(cp_parser * parser)14924 cp_parser_label_declaration (cp_parser* parser)
14925 {
14926 /* Look for the `__label__' keyword. */
14927 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14928
14929 while (true)
14930 {
14931 tree identifier;
14932
14933 /* Look for an identifier. */
14934 identifier = cp_parser_identifier (parser);
14935 /* If we failed, stop. */
14936 if (identifier == error_mark_node)
14937 break;
14938 /* Declare it as a label. */
14939 finish_label_decl (identifier);
14940 /* If the next token is a `;', stop. */
14941 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14942 break;
14943 /* Look for the `,' separating the label declarations. */
14944 cp_parser_require (parser, CPP_COMMA, "`,'");
14945 }
14946
14947 /* Look for the final `;'. */
14948 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14949 }
14950
14951 /* Support Functions */
14952
14953 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14954 NAME should have one of the representations used for an
14955 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14956 is returned. If PARSER->SCOPE is a dependent type, then a
14957 SCOPE_REF is returned.
14958
14959 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14960 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14961 was formed. Abstractly, such entities should not be passed to this
14962 function, because they do not need to be looked up, but it is
14963 simpler to check for this special case here, rather than at the
14964 call-sites.
14965
14966 In cases not explicitly covered above, this function returns a
14967 DECL, OVERLOAD, or baselink representing the result of the lookup.
14968 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14969 is returned.
14970
14971 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14972 (e.g., "struct") that was used. In that case bindings that do not
14973 refer to types are ignored.
14974
14975 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14976 ignored.
14977
14978 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14979 are ignored.
14980
14981 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14982 types.
14983
14984 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14985 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14986 NULL_TREE otherwise. */
14987
14988 static tree
cp_parser_lookup_name(cp_parser * parser,tree name,enum tag_types tag_type,bool is_template,bool is_namespace,bool check_dependency,tree * ambiguous_decls)14989 cp_parser_lookup_name (cp_parser *parser, tree name,
14990 enum tag_types tag_type,
14991 bool is_template,
14992 bool is_namespace,
14993 bool check_dependency,
14994 tree *ambiguous_decls)
14995 {
14996 int flags = 0;
14997 tree decl;
14998 tree object_type = parser->context->object_type;
14999
15000 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15001 flags |= LOOKUP_COMPLAIN;
15002
15003 /* Assume that the lookup will be unambiguous. */
15004 if (ambiguous_decls)
15005 *ambiguous_decls = NULL_TREE;
15006
15007 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15008 no longer valid. Note that if we are parsing tentatively, and
15009 the parse fails, OBJECT_TYPE will be automatically restored. */
15010 parser->context->object_type = NULL_TREE;
15011
15012 if (name == error_mark_node)
15013 return error_mark_node;
15014
15015 /* A template-id has already been resolved; there is no lookup to
15016 do. */
15017 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15018 return name;
15019 if (BASELINK_P (name))
15020 {
15021 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15022 == TEMPLATE_ID_EXPR);
15023 return name;
15024 }
15025
15026 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15027 it should already have been checked to make sure that the name
15028 used matches the type being destroyed. */
15029 if (TREE_CODE (name) == BIT_NOT_EXPR)
15030 {
15031 tree type;
15032
15033 /* Figure out to which type this destructor applies. */
15034 if (parser->scope)
15035 type = parser->scope;
15036 else if (object_type)
15037 type = object_type;
15038 else
15039 type = current_class_type;
15040 /* If that's not a class type, there is no destructor. */
15041 if (!type || !CLASS_TYPE_P (type))
15042 return error_mark_node;
15043 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15044 lazily_declare_fn (sfk_destructor, type);
15045 if (!CLASSTYPE_DESTRUCTORS (type))
15046 return error_mark_node;
15047 /* If it was a class type, return the destructor. */
15048 return CLASSTYPE_DESTRUCTORS (type);
15049 }
15050
15051 /* By this point, the NAME should be an ordinary identifier. If
15052 the id-expression was a qualified name, the qualifying scope is
15053 stored in PARSER->SCOPE at this point. */
15054 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15055
15056 /* Perform the lookup. */
15057 if (parser->scope)
15058 {
15059 bool dependent_p;
15060
15061 if (parser->scope == error_mark_node)
15062 return error_mark_node;
15063
15064 /* If the SCOPE is dependent, the lookup must be deferred until
15065 the template is instantiated -- unless we are explicitly
15066 looking up names in uninstantiated templates. Even then, we
15067 cannot look up the name if the scope is not a class type; it
15068 might, for example, be a template type parameter. */
15069 dependent_p = (TYPE_P (parser->scope)
15070 && !(parser->in_declarator_p
15071 && currently_open_class (parser->scope))
15072 && dependent_type_p (parser->scope));
15073 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15074 && dependent_p)
15075 {
15076 if (tag_type)
15077 {
15078 tree type;
15079
15080 /* The resolution to Core Issue 180 says that `struct
15081 A::B' should be considered a type-name, even if `A'
15082 is dependent. */
15083 type = make_typename_type (parser->scope, name, tag_type,
15084 /*complain=*/tf_error);
15085 decl = TYPE_NAME (type);
15086 }
15087 else if (is_template
15088 && (cp_parser_next_token_ends_template_argument_p (parser)
15089 || cp_lexer_next_token_is (parser->lexer,
15090 CPP_CLOSE_PAREN)))
15091 decl = make_unbound_class_template (parser->scope,
15092 name, NULL_TREE,
15093 /*complain=*/tf_error);
15094 else
15095 decl = build_qualified_name (/*type=*/NULL_TREE,
15096 parser->scope, name,
15097 is_template);
15098 }
15099 else
15100 {
15101 tree pushed_scope = NULL_TREE;
15102
15103 /* If PARSER->SCOPE is a dependent type, then it must be a
15104 class type, and we must not be checking dependencies;
15105 otherwise, we would have processed this lookup above. So
15106 that PARSER->SCOPE is not considered a dependent base by
15107 lookup_member, we must enter the scope here. */
15108 if (dependent_p)
15109 pushed_scope = push_scope (parser->scope);
15110 /* If the PARSER->SCOPE is a template specialization, it
15111 may be instantiated during name lookup. In that case,
15112 errors may be issued. Even if we rollback the current
15113 tentative parse, those errors are valid. */
15114 decl = lookup_qualified_name (parser->scope, name,
15115 tag_type != none_type,
15116 /*complain=*/true);
15117 if (pushed_scope)
15118 pop_scope (pushed_scope);
15119 }
15120 parser->qualifying_scope = parser->scope;
15121 parser->object_scope = NULL_TREE;
15122 }
15123 else if (object_type)
15124 {
15125 tree object_decl = NULL_TREE;
15126 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15127 OBJECT_TYPE is not a class. */
15128 if (CLASS_TYPE_P (object_type))
15129 /* If the OBJECT_TYPE is a template specialization, it may
15130 be instantiated during name lookup. In that case, errors
15131 may be issued. Even if we rollback the current tentative
15132 parse, those errors are valid. */
15133 object_decl = lookup_member (object_type,
15134 name,
15135 /*protect=*/0,
15136 tag_type != none_type);
15137 /* Look it up in the enclosing context, too. */
15138 decl = lookup_name_real (name, tag_type != none_type,
15139 /*nonclass=*/0,
15140 /*block_p=*/true, is_namespace, flags);
15141 parser->object_scope = object_type;
15142 parser->qualifying_scope = NULL_TREE;
15143 if (object_decl)
15144 decl = object_decl;
15145 }
15146 else
15147 {
15148 decl = lookup_name_real (name, tag_type != none_type,
15149 /*nonclass=*/0,
15150 /*block_p=*/true, is_namespace, flags);
15151 parser->qualifying_scope = NULL_TREE;
15152 parser->object_scope = NULL_TREE;
15153 }
15154
15155 /* If the lookup failed, let our caller know. */
15156 if (!decl || decl == error_mark_node)
15157 return error_mark_node;
15158
15159 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15160 if (TREE_CODE (decl) == TREE_LIST)
15161 {
15162 if (ambiguous_decls)
15163 *ambiguous_decls = decl;
15164 /* The error message we have to print is too complicated for
15165 cp_parser_error, so we incorporate its actions directly. */
15166 if (!cp_parser_simulate_error (parser))
15167 {
15168 error ("reference to %qD is ambiguous", name);
15169 print_candidates (decl);
15170 }
15171 return error_mark_node;
15172 }
15173
15174 gcc_assert (DECL_P (decl)
15175 || TREE_CODE (decl) == OVERLOAD
15176 || TREE_CODE (decl) == SCOPE_REF
15177 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15178 || BASELINK_P (decl));
15179
15180 /* If we have resolved the name of a member declaration, check to
15181 see if the declaration is accessible. When the name resolves to
15182 set of overloaded functions, accessibility is checked when
15183 overload resolution is done.
15184
15185 During an explicit instantiation, access is not checked at all,
15186 as per [temp.explicit]. */
15187 if (DECL_P (decl))
15188 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15189
15190 return decl;
15191 }
15192
15193 /* Like cp_parser_lookup_name, but for use in the typical case where
15194 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15195 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15196
15197 static tree
cp_parser_lookup_name_simple(cp_parser * parser,tree name)15198 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15199 {
15200 return cp_parser_lookup_name (parser, name,
15201 none_type,
15202 /*is_template=*/false,
15203 /*is_namespace=*/false,
15204 /*check_dependency=*/true,
15205 /*ambiguous_decls=*/NULL);
15206 }
15207
15208 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15209 the current context, return the TYPE_DECL. If TAG_NAME_P is
15210 true, the DECL indicates the class being defined in a class-head,
15211 or declared in an elaborated-type-specifier.
15212
15213 Otherwise, return DECL. */
15214
15215 static tree
cp_parser_maybe_treat_template_as_class(tree decl,bool tag_name_p)15216 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15217 {
15218 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15219 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15220
15221 struct A {
15222 template <typename T> struct B;
15223 };
15224
15225 template <typename T> struct A::B {};
15226
15227 Similarly, in an elaborated-type-specifier:
15228
15229 namespace N { struct X{}; }
15230
15231 struct A {
15232 template <typename T> friend struct N::X;
15233 };
15234
15235 However, if the DECL refers to a class type, and we are in
15236 the scope of the class, then the name lookup automatically
15237 finds the TYPE_DECL created by build_self_reference rather
15238 than a TEMPLATE_DECL. For example, in:
15239
15240 template <class T> struct S {
15241 S s;
15242 };
15243
15244 there is no need to handle such case. */
15245
15246 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15247 return DECL_TEMPLATE_RESULT (decl);
15248
15249 return decl;
15250 }
15251
15252 /* If too many, or too few, template-parameter lists apply to the
15253 declarator, issue an error message. Returns TRUE if all went well,
15254 and FALSE otherwise. */
15255
15256 static bool
cp_parser_check_declarator_template_parameters(cp_parser * parser,cp_declarator * declarator)15257 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15258 cp_declarator *declarator)
15259 {
15260 unsigned num_templates;
15261
15262 /* We haven't seen any classes that involve template parameters yet. */
15263 num_templates = 0;
15264
15265 switch (declarator->kind)
15266 {
15267 case cdk_id:
15268 if (declarator->u.id.qualifying_scope)
15269 {
15270 tree scope;
15271 tree member;
15272
15273 scope = declarator->u.id.qualifying_scope;
15274 member = declarator->u.id.unqualified_name;
15275
15276 while (scope && CLASS_TYPE_P (scope))
15277 {
15278 /* You're supposed to have one `template <...>'
15279 for every template class, but you don't need one
15280 for a full specialization. For example:
15281
15282 template <class T> struct S{};
15283 template <> struct S<int> { void f(); };
15284 void S<int>::f () {}
15285
15286 is correct; there shouldn't be a `template <>' for
15287 the definition of `S<int>::f'. */
15288 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15289 /* If SCOPE does not have template information of any
15290 kind, then it is not a template, nor is it nested
15291 within a template. */
15292 break;
15293 if (explicit_class_specialization_p (scope))
15294 break;
15295 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15296 ++num_templates;
15297
15298 scope = TYPE_CONTEXT (scope);
15299 }
15300 }
15301 else if (TREE_CODE (declarator->u.id.unqualified_name)
15302 == TEMPLATE_ID_EXPR)
15303 /* If the DECLARATOR has the form `X<y>' then it uses one
15304 additional level of template parameters. */
15305 ++num_templates;
15306
15307 return cp_parser_check_template_parameters (parser,
15308 num_templates);
15309
15310 case cdk_function:
15311 case cdk_array:
15312 case cdk_pointer:
15313 case cdk_reference:
15314 case cdk_ptrmem:
15315 return (cp_parser_check_declarator_template_parameters
15316 (parser, declarator->declarator));
15317
15318 case cdk_error:
15319 return true;
15320
15321 default:
15322 gcc_unreachable ();
15323 }
15324 return false;
15325 }
15326
15327 /* NUM_TEMPLATES were used in the current declaration. If that is
15328 invalid, return FALSE and issue an error messages. Otherwise,
15329 return TRUE. */
15330
15331 static bool
cp_parser_check_template_parameters(cp_parser * parser,unsigned num_templates)15332 cp_parser_check_template_parameters (cp_parser* parser,
15333 unsigned num_templates)
15334 {
15335 /* If there are more template classes than parameter lists, we have
15336 something like:
15337
15338 template <class T> void S<T>::R<T>::f (); */
15339 if (parser->num_template_parameter_lists < num_templates)
15340 {
15341 error ("too few template-parameter-lists");
15342 return false;
15343 }
15344 /* If there are the same number of template classes and parameter
15345 lists, that's OK. */
15346 if (parser->num_template_parameter_lists == num_templates)
15347 return true;
15348 /* If there are more, but only one more, then we are referring to a
15349 member template. That's OK too. */
15350 if (parser->num_template_parameter_lists == num_templates + 1)
15351 return true;
15352 /* Otherwise, there are too many template parameter lists. We have
15353 something like:
15354
15355 template <class T> template <class U> void S::f(); */
15356 error ("too many template-parameter-lists");
15357 return false;
15358 }
15359
15360 /* Parse an optional `::' token indicating that the following name is
15361 from the global namespace. If so, PARSER->SCOPE is set to the
15362 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15363 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15364 Returns the new value of PARSER->SCOPE, if the `::' token is
15365 present, and NULL_TREE otherwise. */
15366
15367 static tree
cp_parser_global_scope_opt(cp_parser * parser,bool current_scope_valid_p)15368 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15369 {
15370 cp_token *token;
15371
15372 /* Peek at the next token. */
15373 token = cp_lexer_peek_token (parser->lexer);
15374 /* If we're looking at a `::' token then we're starting from the
15375 global namespace, not our current location. */
15376 if (token->type == CPP_SCOPE)
15377 {
15378 /* Consume the `::' token. */
15379 cp_lexer_consume_token (parser->lexer);
15380 /* Set the SCOPE so that we know where to start the lookup. */
15381 parser->scope = global_namespace;
15382 parser->qualifying_scope = global_namespace;
15383 parser->object_scope = NULL_TREE;
15384
15385 return parser->scope;
15386 }
15387 else if (!current_scope_valid_p)
15388 {
15389 parser->scope = NULL_TREE;
15390 parser->qualifying_scope = NULL_TREE;
15391 parser->object_scope = NULL_TREE;
15392 }
15393
15394 return NULL_TREE;
15395 }
15396
15397 /* Returns TRUE if the upcoming token sequence is the start of a
15398 constructor declarator. If FRIEND_P is true, the declarator is
15399 preceded by the `friend' specifier. */
15400
15401 static bool
cp_parser_constructor_declarator_p(cp_parser * parser,bool friend_p)15402 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15403 {
15404 bool constructor_p;
15405 tree type_decl = NULL_TREE;
15406 bool nested_name_p;
15407 cp_token *next_token;
15408
15409 /* The common case is that this is not a constructor declarator, so
15410 try to avoid doing lots of work if at all possible. It's not
15411 valid declare a constructor at function scope. */
15412 if (parser->in_function_body)
15413 return false;
15414 /* And only certain tokens can begin a constructor declarator. */
15415 next_token = cp_lexer_peek_token (parser->lexer);
15416 if (next_token->type != CPP_NAME
15417 && next_token->type != CPP_SCOPE
15418 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15419 && next_token->type != CPP_TEMPLATE_ID)
15420 return false;
15421
15422 /* Parse tentatively; we are going to roll back all of the tokens
15423 consumed here. */
15424 cp_parser_parse_tentatively (parser);
15425 /* Assume that we are looking at a constructor declarator. */
15426 constructor_p = true;
15427
15428 /* Look for the optional `::' operator. */
15429 cp_parser_global_scope_opt (parser,
15430 /*current_scope_valid_p=*/false);
15431 /* Look for the nested-name-specifier. */
15432 nested_name_p
15433 = (cp_parser_nested_name_specifier_opt (parser,
15434 /*typename_keyword_p=*/false,
15435 /*check_dependency_p=*/false,
15436 /*type_p=*/false,
15437 /*is_declaration=*/false)
15438 != NULL_TREE);
15439 /* Outside of a class-specifier, there must be a
15440 nested-name-specifier. */
15441 if (!nested_name_p &&
15442 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15443 || friend_p))
15444 constructor_p = false;
15445 /* If we still think that this might be a constructor-declarator,
15446 look for a class-name. */
15447 if (constructor_p)
15448 {
15449 /* If we have:
15450
15451 template <typename T> struct S { S(); };
15452 template <typename T> S<T>::S ();
15453
15454 we must recognize that the nested `S' names a class.
15455 Similarly, for:
15456
15457 template <typename T> S<T>::S<T> ();
15458
15459 we must recognize that the nested `S' names a template. */
15460 type_decl = cp_parser_class_name (parser,
15461 /*typename_keyword_p=*/false,
15462 /*template_keyword_p=*/false,
15463 none_type,
15464 /*check_dependency_p=*/false,
15465 /*class_head_p=*/false,
15466 /*is_declaration=*/false);
15467 /* If there was no class-name, then this is not a constructor. */
15468 constructor_p = !cp_parser_error_occurred (parser);
15469 }
15470
15471 /* If we're still considering a constructor, we have to see a `(',
15472 to begin the parameter-declaration-clause, followed by either a
15473 `)', an `...', or a decl-specifier. We need to check for a
15474 type-specifier to avoid being fooled into thinking that:
15475
15476 S::S (f) (int);
15477
15478 is a constructor. (It is actually a function named `f' that
15479 takes one parameter (of type `int') and returns a value of type
15480 `S::S'. */
15481 if (constructor_p
15482 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15483 {
15484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15485 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15486 /* A parameter declaration begins with a decl-specifier,
15487 which is either the "attribute" keyword, a storage class
15488 specifier, or (usually) a type-specifier. */
15489 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15490 {
15491 tree type;
15492 tree pushed_scope = NULL_TREE;
15493 unsigned saved_num_template_parameter_lists;
15494
15495 /* Names appearing in the type-specifier should be looked up
15496 in the scope of the class. */
15497 if (current_class_type)
15498 type = NULL_TREE;
15499 else
15500 {
15501 type = TREE_TYPE (type_decl);
15502 if (TREE_CODE (type) == TYPENAME_TYPE)
15503 {
15504 type = resolve_typename_type (type,
15505 /*only_current_p=*/false);
15506 if (type == error_mark_node)
15507 {
15508 cp_parser_abort_tentative_parse (parser);
15509 return false;
15510 }
15511 }
15512 pushed_scope = push_scope (type);
15513 }
15514
15515 /* Inside the constructor parameter list, surrounding
15516 template-parameter-lists do not apply. */
15517 saved_num_template_parameter_lists
15518 = parser->num_template_parameter_lists;
15519 parser->num_template_parameter_lists = 0;
15520
15521 /* Look for the type-specifier. */
15522 cp_parser_type_specifier (parser,
15523 CP_PARSER_FLAGS_NONE,
15524 /*decl_specs=*/NULL,
15525 /*is_declarator=*/true,
15526 /*declares_class_or_enum=*/NULL,
15527 /*is_cv_qualifier=*/NULL);
15528
15529 parser->num_template_parameter_lists
15530 = saved_num_template_parameter_lists;
15531
15532 /* Leave the scope of the class. */
15533 if (pushed_scope)
15534 pop_scope (pushed_scope);
15535
15536 constructor_p = !cp_parser_error_occurred (parser);
15537 }
15538 }
15539 else
15540 constructor_p = false;
15541 /* We did not really want to consume any tokens. */
15542 cp_parser_abort_tentative_parse (parser);
15543
15544 return constructor_p;
15545 }
15546
15547 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15548 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15549 they must be performed once we are in the scope of the function.
15550
15551 Returns the function defined. */
15552
15553 static tree
cp_parser_function_definition_from_specifiers_and_declarator(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,tree attributes,const cp_declarator * declarator)15554 cp_parser_function_definition_from_specifiers_and_declarator
15555 (cp_parser* parser,
15556 cp_decl_specifier_seq *decl_specifiers,
15557 tree attributes,
15558 const cp_declarator *declarator)
15559 {
15560 tree fn;
15561 bool success_p;
15562
15563 /* Begin the function-definition. */
15564 success_p = start_function (decl_specifiers, declarator, attributes);
15565
15566 /* The things we're about to see are not directly qualified by any
15567 template headers we've seen thus far. */
15568 reset_specialization ();
15569
15570 /* If there were names looked up in the decl-specifier-seq that we
15571 did not check, check them now. We must wait until we are in the
15572 scope of the function to perform the checks, since the function
15573 might be a friend. */
15574 perform_deferred_access_checks ();
15575
15576 if (!success_p)
15577 {
15578 /* Skip the entire function. */
15579 cp_parser_skip_to_end_of_block_or_statement (parser);
15580 fn = error_mark_node;
15581 }
15582 else
15583 fn = cp_parser_function_definition_after_declarator (parser,
15584 /*inline_p=*/false);
15585
15586 return fn;
15587 }
15588
15589 /* Parse the part of a function-definition that follows the
15590 declarator. INLINE_P is TRUE iff this function is an inline
15591 function defined with a class-specifier.
15592
15593 Returns the function defined. */
15594
15595 static tree
cp_parser_function_definition_after_declarator(cp_parser * parser,bool inline_p)15596 cp_parser_function_definition_after_declarator (cp_parser* parser,
15597 bool inline_p)
15598 {
15599 tree fn;
15600 bool ctor_initializer_p = false;
15601 bool saved_in_unbraced_linkage_specification_p;
15602 bool saved_in_function_body;
15603 unsigned saved_num_template_parameter_lists;
15604
15605 saved_in_function_body = parser->in_function_body;
15606 parser->in_function_body = true;
15607 /* If the next token is `return', then the code may be trying to
15608 make use of the "named return value" extension that G++ used to
15609 support. */
15610 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15611 {
15612 /* Consume the `return' keyword. */
15613 cp_lexer_consume_token (parser->lexer);
15614 /* Look for the identifier that indicates what value is to be
15615 returned. */
15616 cp_parser_identifier (parser);
15617 /* Issue an error message. */
15618 error ("named return values are no longer supported");
15619 /* Skip tokens until we reach the start of the function body. */
15620 while (true)
15621 {
15622 cp_token *token = cp_lexer_peek_token (parser->lexer);
15623 if (token->type == CPP_OPEN_BRACE
15624 || token->type == CPP_EOF
15625 || token->type == CPP_PRAGMA_EOL)
15626 break;
15627 cp_lexer_consume_token (parser->lexer);
15628 }
15629 }
15630 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15631 anything declared inside `f'. */
15632 saved_in_unbraced_linkage_specification_p
15633 = parser->in_unbraced_linkage_specification_p;
15634 parser->in_unbraced_linkage_specification_p = false;
15635 /* Inside the function, surrounding template-parameter-lists do not
15636 apply. */
15637 saved_num_template_parameter_lists
15638 = parser->num_template_parameter_lists;
15639 parser->num_template_parameter_lists = 0;
15640 /* If the next token is `try', then we are looking at a
15641 function-try-block. */
15642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15643 ctor_initializer_p = cp_parser_function_try_block (parser);
15644 /* A function-try-block includes the function-body, so we only do
15645 this next part if we're not processing a function-try-block. */
15646 else
15647 ctor_initializer_p
15648 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15649
15650 /* Finish the function. */
15651 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15652 (inline_p ? 2 : 0));
15653 /* Generate code for it, if necessary. */
15654 expand_or_defer_fn (fn);
15655 /* Restore the saved values. */
15656 parser->in_unbraced_linkage_specification_p
15657 = saved_in_unbraced_linkage_specification_p;
15658 parser->num_template_parameter_lists
15659 = saved_num_template_parameter_lists;
15660 parser->in_function_body = saved_in_function_body;
15661
15662 return fn;
15663 }
15664
15665 /* Parse a template-declaration, assuming that the `export' (and
15666 `extern') keywords, if present, has already been scanned. MEMBER_P
15667 is as for cp_parser_template_declaration. */
15668
15669 static void
cp_parser_template_declaration_after_export(cp_parser * parser,bool member_p)15670 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15671 {
15672 tree decl = NULL_TREE;
15673 VEC (deferred_access_check,gc) *checks;
15674 tree parameter_list;
15675 bool friend_p = false;
15676 bool need_lang_pop;
15677
15678 /* Look for the `template' keyword. */
15679 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15680 return;
15681
15682 /* And the `<'. */
15683 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15684 return;
15685 if (at_class_scope_p () && current_function_decl)
15686 {
15687 /* 14.5.2.2 [temp.mem]
15688
15689 A local class shall not have member templates. */
15690 error ("invalid declaration of member template in local class");
15691 cp_parser_skip_to_end_of_block_or_statement (parser);
15692 return;
15693 }
15694 /* [temp]
15695
15696 A template ... shall not have C linkage. */
15697 if (current_lang_name == lang_name_c)
15698 {
15699 error ("template with C linkage");
15700 /* Give it C++ linkage to avoid confusing other parts of the
15701 front end. */
15702 push_lang_context (lang_name_cplusplus);
15703 need_lang_pop = true;
15704 }
15705 else
15706 need_lang_pop = false;
15707
15708 /* We cannot perform access checks on the template parameter
15709 declarations until we know what is being declared, just as we
15710 cannot check the decl-specifier list. */
15711 push_deferring_access_checks (dk_deferred);
15712
15713 /* If the next token is `>', then we have an invalid
15714 specialization. Rather than complain about an invalid template
15715 parameter, issue an error message here. */
15716 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15717 {
15718 cp_parser_error (parser, "invalid explicit specialization");
15719 begin_specialization ();
15720 parameter_list = NULL_TREE;
15721 }
15722 else
15723 /* Parse the template parameters. */
15724 parameter_list = cp_parser_template_parameter_list (parser);
15725
15726 /* Get the deferred access checks from the parameter list. These
15727 will be checked once we know what is being declared, as for a
15728 member template the checks must be performed in the scope of the
15729 class containing the member. */
15730 checks = get_deferred_access_checks ();
15731
15732 /* Look for the `>'. */
15733 cp_parser_skip_to_end_of_template_parameter_list (parser);
15734 /* We just processed one more parameter list. */
15735 ++parser->num_template_parameter_lists;
15736 /* If the next token is `template', there are more template
15737 parameters. */
15738 if (cp_lexer_next_token_is_keyword (parser->lexer,
15739 RID_TEMPLATE))
15740 cp_parser_template_declaration_after_export (parser, member_p);
15741 else
15742 {
15743 /* There are no access checks when parsing a template, as we do not
15744 know if a specialization will be a friend. */
15745 push_deferring_access_checks (dk_no_check);
15746 decl = cp_parser_single_declaration (parser,
15747 checks,
15748 member_p,
15749 &friend_p);
15750 pop_deferring_access_checks ();
15751
15752 /* If this is a member template declaration, let the front
15753 end know. */
15754 if (member_p && !friend_p && decl)
15755 {
15756 if (TREE_CODE (decl) == TYPE_DECL)
15757 cp_parser_check_access_in_redeclaration (decl);
15758
15759 decl = finish_member_template_decl (decl);
15760 }
15761 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15762 make_friend_class (current_class_type, TREE_TYPE (decl),
15763 /*complain=*/true);
15764 }
15765 /* We are done with the current parameter list. */
15766 --parser->num_template_parameter_lists;
15767
15768 pop_deferring_access_checks ();
15769
15770 /* Finish up. */
15771 finish_template_decl (parameter_list);
15772
15773 /* Register member declarations. */
15774 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15775 finish_member_declaration (decl);
15776 /* For the erroneous case of a template with C linkage, we pushed an
15777 implicit C++ linkage scope; exit that scope now. */
15778 if (need_lang_pop)
15779 pop_lang_context ();
15780 /* If DECL is a function template, we must return to parse it later.
15781 (Even though there is no definition, there might be default
15782 arguments that need handling.) */
15783 if (member_p && decl
15784 && (TREE_CODE (decl) == FUNCTION_DECL
15785 || DECL_FUNCTION_TEMPLATE_P (decl)))
15786 TREE_VALUE (parser->unparsed_functions_queues)
15787 = tree_cons (NULL_TREE, decl,
15788 TREE_VALUE (parser->unparsed_functions_queues));
15789 }
15790
15791 /* Perform the deferred access checks from a template-parameter-list.
15792 CHECKS is a TREE_LIST of access checks, as returned by
15793 get_deferred_access_checks. */
15794
15795 static void
cp_parser_perform_template_parameter_access_checks(VEC (deferred_access_check,gc)* checks)15796 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15797 {
15798 ++processing_template_parmlist;
15799 perform_access_checks (checks);
15800 --processing_template_parmlist;
15801 }
15802
15803 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15804 `function-definition' sequence. MEMBER_P is true, this declaration
15805 appears in a class scope.
15806
15807 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15808 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15809
15810 static tree
cp_parser_single_declaration(cp_parser * parser,VEC (deferred_access_check,gc)* checks,bool member_p,bool * friend_p)15811 cp_parser_single_declaration (cp_parser* parser,
15812 VEC (deferred_access_check,gc)* checks,
15813 bool member_p,
15814 bool* friend_p)
15815 {
15816 int declares_class_or_enum;
15817 tree decl = NULL_TREE;
15818 cp_decl_specifier_seq decl_specifiers;
15819 bool function_definition_p = false;
15820
15821 /* This function is only used when processing a template
15822 declaration. */
15823 gcc_assert (innermost_scope_kind () == sk_template_parms
15824 || innermost_scope_kind () == sk_template_spec);
15825
15826 /* Defer access checks until we know what is being declared. */
15827 push_deferring_access_checks (dk_deferred);
15828
15829 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15830 alternative. */
15831 cp_parser_decl_specifier_seq (parser,
15832 CP_PARSER_FLAGS_OPTIONAL,
15833 &decl_specifiers,
15834 &declares_class_or_enum);
15835 if (friend_p)
15836 *friend_p = cp_parser_friend_p (&decl_specifiers);
15837
15838 /* There are no template typedefs. */
15839 if (decl_specifiers.specs[(int) ds_typedef])
15840 {
15841 error ("template declaration of %qs", "typedef");
15842 decl = error_mark_node;
15843 }
15844
15845 /* Gather up the access checks that occurred the
15846 decl-specifier-seq. */
15847 stop_deferring_access_checks ();
15848
15849 /* Check for the declaration of a template class. */
15850 if (declares_class_or_enum)
15851 {
15852 if (cp_parser_declares_only_class_p (parser))
15853 {
15854 decl = shadow_tag (&decl_specifiers);
15855
15856 /* In this case:
15857
15858 struct C {
15859 friend template <typename T> struct A<T>::B;
15860 };
15861
15862 A<T>::B will be represented by a TYPENAME_TYPE, and
15863 therefore not recognized by shadow_tag. */
15864 if (friend_p && *friend_p
15865 && !decl
15866 && decl_specifiers.type
15867 && TYPE_P (decl_specifiers.type))
15868 decl = decl_specifiers.type;
15869
15870 if (decl && decl != error_mark_node)
15871 decl = TYPE_NAME (decl);
15872 else
15873 decl = error_mark_node;
15874
15875 /* Perform access checks for template parameters. */
15876 cp_parser_perform_template_parameter_access_checks (checks);
15877 }
15878 }
15879 /* If it's not a template class, try for a template function. If
15880 the next token is a `;', then this declaration does not declare
15881 anything. But, if there were errors in the decl-specifiers, then
15882 the error might well have come from an attempted class-specifier.
15883 In that case, there's no need to warn about a missing declarator. */
15884 if (!decl
15885 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15886 || decl_specifiers.type != error_mark_node))
15887 decl = cp_parser_init_declarator (parser,
15888 &decl_specifiers,
15889 checks,
15890 /*function_definition_allowed_p=*/true,
15891 member_p,
15892 declares_class_or_enum,
15893 &function_definition_p);
15894
15895 pop_deferring_access_checks ();
15896
15897 /* Clear any current qualification; whatever comes next is the start
15898 of something new. */
15899 parser->scope = NULL_TREE;
15900 parser->qualifying_scope = NULL_TREE;
15901 parser->object_scope = NULL_TREE;
15902 /* Look for a trailing `;' after the declaration. */
15903 if (!function_definition_p
15904 && (decl == error_mark_node
15905 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15906 cp_parser_skip_to_end_of_block_or_statement (parser);
15907
15908 return decl;
15909 }
15910
15911 /* Parse a cast-expression that is not the operand of a unary "&". */
15912
15913 static tree
cp_parser_simple_cast_expression(cp_parser * parser)15914 cp_parser_simple_cast_expression (cp_parser *parser)
15915 {
15916 return cp_parser_cast_expression (parser, /*address_p=*/false,
15917 /*cast_p=*/false);
15918 }
15919
15920 /* Parse a functional cast to TYPE. Returns an expression
15921 representing the cast. */
15922
15923 static tree
cp_parser_functional_cast(cp_parser * parser,tree type)15924 cp_parser_functional_cast (cp_parser* parser, tree type)
15925 {
15926 tree expression_list;
15927 tree cast;
15928
15929 expression_list
15930 = cp_parser_parenthesized_expression_list (parser, false,
15931 /*cast_p=*/true,
15932 /*non_constant_p=*/NULL);
15933
15934 cast = build_functional_cast (type, expression_list);
15935 /* [expr.const]/1: In an integral constant expression "only type
15936 conversions to integral or enumeration type can be used". */
15937 if (TREE_CODE (type) == TYPE_DECL)
15938 type = TREE_TYPE (type);
15939 if (cast != error_mark_node
15940 && !cast_valid_in_integral_constant_expression_p (type)
15941 && (cp_parser_non_integral_constant_expression
15942 (parser, "a call to a constructor")))
15943 return error_mark_node;
15944 return cast;
15945 }
15946
15947 /* Save the tokens that make up the body of a member function defined
15948 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15949 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15950 specifiers applied to the declaration. Returns the FUNCTION_DECL
15951 for the member function. */
15952
15953 static tree
cp_parser_save_member_function_body(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree attributes)15954 cp_parser_save_member_function_body (cp_parser* parser,
15955 cp_decl_specifier_seq *decl_specifiers,
15956 cp_declarator *declarator,
15957 tree attributes)
15958 {
15959 cp_token *first;
15960 cp_token *last;
15961 tree fn;
15962
15963 /* Create the function-declaration. */
15964 fn = start_method (decl_specifiers, declarator, attributes);
15965 /* If something went badly wrong, bail out now. */
15966 if (fn == error_mark_node)
15967 {
15968 /* If there's a function-body, skip it. */
15969 if (cp_parser_token_starts_function_definition_p
15970 (cp_lexer_peek_token (parser->lexer)))
15971 cp_parser_skip_to_end_of_block_or_statement (parser);
15972 return error_mark_node;
15973 }
15974
15975 /* Remember it, if there default args to post process. */
15976 cp_parser_save_default_args (parser, fn);
15977
15978 /* Save away the tokens that make up the body of the
15979 function. */
15980 first = parser->lexer->next_token;
15981 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15982 /* Handle function try blocks. */
15983 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15984 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15985 last = parser->lexer->next_token;
15986
15987 /* Save away the inline definition; we will process it when the
15988 class is complete. */
15989 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15990 DECL_PENDING_INLINE_P (fn) = 1;
15991
15992 /* We need to know that this was defined in the class, so that
15993 friend templates are handled correctly. */
15994 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15995
15996 /* We're done with the inline definition. */
15997 finish_method (fn);
15998
15999 /* Add FN to the queue of functions to be parsed later. */
16000 TREE_VALUE (parser->unparsed_functions_queues)
16001 = tree_cons (NULL_TREE, fn,
16002 TREE_VALUE (parser->unparsed_functions_queues));
16003
16004 return fn;
16005 }
16006
16007 /* Parse a template-argument-list, as well as the trailing ">" (but
16008 not the opening ">"). See cp_parser_template_argument_list for the
16009 return value. */
16010
16011 static tree
cp_parser_enclosed_template_argument_list(cp_parser * parser)16012 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16013 {
16014 tree arguments;
16015 tree saved_scope;
16016 tree saved_qualifying_scope;
16017 tree saved_object_scope;
16018 bool saved_greater_than_is_operator_p;
16019 bool saved_skip_evaluation;
16020
16021 /* [temp.names]
16022
16023 When parsing a template-id, the first non-nested `>' is taken as
16024 the end of the template-argument-list rather than a greater-than
16025 operator. */
16026 saved_greater_than_is_operator_p
16027 = parser->greater_than_is_operator_p;
16028 parser->greater_than_is_operator_p = false;
16029 /* Parsing the argument list may modify SCOPE, so we save it
16030 here. */
16031 saved_scope = parser->scope;
16032 saved_qualifying_scope = parser->qualifying_scope;
16033 saved_object_scope = parser->object_scope;
16034 /* We need to evaluate the template arguments, even though this
16035 template-id may be nested within a "sizeof". */
16036 saved_skip_evaluation = skip_evaluation;
16037 skip_evaluation = false;
16038 /* Parse the template-argument-list itself. */
16039 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16040 arguments = NULL_TREE;
16041 else
16042 arguments = cp_parser_template_argument_list (parser);
16043 /* Look for the `>' that ends the template-argument-list. If we find
16044 a '>>' instead, it's probably just a typo. */
16045 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16046 {
16047 if (!saved_greater_than_is_operator_p)
16048 {
16049 /* If we're in a nested template argument list, the '>>' has
16050 to be a typo for '> >'. We emit the error message, but we
16051 continue parsing and we push a '>' as next token, so that
16052 the argument list will be parsed correctly. Note that the
16053 global source location is still on the token before the
16054 '>>', so we need to say explicitly where we want it. */
16055 cp_token *token = cp_lexer_peek_token (parser->lexer);
16056 error ("%H%<>>%> should be %<> >%> "
16057 "within a nested template argument list",
16058 &token->location);
16059
16060 /* ??? Proper recovery should terminate two levels of
16061 template argument list here. */
16062 token->type = CPP_GREATER;
16063 }
16064 else
16065 {
16066 /* If this is not a nested template argument list, the '>>'
16067 is a typo for '>'. Emit an error message and continue.
16068 Same deal about the token location, but here we can get it
16069 right by consuming the '>>' before issuing the diagnostic. */
16070 cp_lexer_consume_token (parser->lexer);
16071 error ("spurious %<>>%>, use %<>%> to terminate "
16072 "a template argument list");
16073 }
16074 }
16075 else
16076 cp_parser_skip_to_end_of_template_parameter_list (parser);
16077 /* The `>' token might be a greater-than operator again now. */
16078 parser->greater_than_is_operator_p
16079 = saved_greater_than_is_operator_p;
16080 /* Restore the SAVED_SCOPE. */
16081 parser->scope = saved_scope;
16082 parser->qualifying_scope = saved_qualifying_scope;
16083 parser->object_scope = saved_object_scope;
16084 skip_evaluation = saved_skip_evaluation;
16085
16086 return arguments;
16087 }
16088
16089 /* MEMBER_FUNCTION is a member function, or a friend. If default
16090 arguments, or the body of the function have not yet been parsed,
16091 parse them now. */
16092
16093 static void
cp_parser_late_parsing_for_member(cp_parser * parser,tree member_function)16094 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16095 {
16096 /* If this member is a template, get the underlying
16097 FUNCTION_DECL. */
16098 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16099 member_function = DECL_TEMPLATE_RESULT (member_function);
16100
16101 /* There should not be any class definitions in progress at this
16102 point; the bodies of members are only parsed outside of all class
16103 definitions. */
16104 gcc_assert (parser->num_classes_being_defined == 0);
16105 /* While we're parsing the member functions we might encounter more
16106 classes. We want to handle them right away, but we don't want
16107 them getting mixed up with functions that are currently in the
16108 queue. */
16109 parser->unparsed_functions_queues
16110 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16111
16112 /* Make sure that any template parameters are in scope. */
16113 maybe_begin_member_template_processing (member_function);
16114
16115 /* If the body of the function has not yet been parsed, parse it
16116 now. */
16117 if (DECL_PENDING_INLINE_P (member_function))
16118 {
16119 tree function_scope;
16120 cp_token_cache *tokens;
16121
16122 /* The function is no longer pending; we are processing it. */
16123 tokens = DECL_PENDING_INLINE_INFO (member_function);
16124 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16125 DECL_PENDING_INLINE_P (member_function) = 0;
16126
16127 /* If this is a local class, enter the scope of the containing
16128 function. */
16129 function_scope = current_function_decl;
16130 if (function_scope)
16131 push_function_context_to (function_scope);
16132
16133
16134 /* Push the body of the function onto the lexer stack. */
16135 cp_parser_push_lexer_for_tokens (parser, tokens);
16136
16137 /* Let the front end know that we going to be defining this
16138 function. */
16139 start_preparsed_function (member_function, NULL_TREE,
16140 SF_PRE_PARSED | SF_INCLASS_INLINE);
16141
16142 /* Don't do access checking if it is a templated function. */
16143 if (processing_template_decl)
16144 push_deferring_access_checks (dk_no_check);
16145
16146 /* Now, parse the body of the function. */
16147 cp_parser_function_definition_after_declarator (parser,
16148 /*inline_p=*/true);
16149
16150 if (processing_template_decl)
16151 pop_deferring_access_checks ();
16152
16153 /* Leave the scope of the containing function. */
16154 if (function_scope)
16155 pop_function_context_from (function_scope);
16156 cp_parser_pop_lexer (parser);
16157 }
16158
16159 /* Remove any template parameters from the symbol table. */
16160 maybe_end_member_template_processing ();
16161
16162 /* Restore the queue. */
16163 parser->unparsed_functions_queues
16164 = TREE_CHAIN (parser->unparsed_functions_queues);
16165 }
16166
16167 /* If DECL contains any default args, remember it on the unparsed
16168 functions queue. */
16169
16170 static void
cp_parser_save_default_args(cp_parser * parser,tree decl)16171 cp_parser_save_default_args (cp_parser* parser, tree decl)
16172 {
16173 tree probe;
16174
16175 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16176 probe;
16177 probe = TREE_CHAIN (probe))
16178 if (TREE_PURPOSE (probe))
16179 {
16180 TREE_PURPOSE (parser->unparsed_functions_queues)
16181 = tree_cons (current_class_type, decl,
16182 TREE_PURPOSE (parser->unparsed_functions_queues));
16183 break;
16184 }
16185 }
16186
16187 /* FN is a FUNCTION_DECL which may contains a parameter with an
16188 unparsed DEFAULT_ARG. Parse the default args now. This function
16189 assumes that the current scope is the scope in which the default
16190 argument should be processed. */
16191
16192 static void
cp_parser_late_parsing_default_args(cp_parser * parser,tree fn)16193 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16194 {
16195 bool saved_local_variables_forbidden_p;
16196 tree parm;
16197
16198 /* While we're parsing the default args, we might (due to the
16199 statement expression extension) encounter more classes. We want
16200 to handle them right away, but we don't want them getting mixed
16201 up with default args that are currently in the queue. */
16202 parser->unparsed_functions_queues
16203 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16204
16205 /* Local variable names (and the `this' keyword) may not appear
16206 in a default argument. */
16207 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16208 parser->local_variables_forbidden_p = true;
16209
16210 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16211 parm;
16212 parm = TREE_CHAIN (parm))
16213 {
16214 cp_token_cache *tokens;
16215 tree default_arg = TREE_PURPOSE (parm);
16216 tree parsed_arg;
16217 VEC(tree,gc) *insts;
16218 tree copy;
16219 unsigned ix;
16220
16221 if (!default_arg)
16222 continue;
16223
16224 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16225 /* This can happen for a friend declaration for a function
16226 already declared with default arguments. */
16227 continue;
16228
16229 /* Push the saved tokens for the default argument onto the parser's
16230 lexer stack. */
16231 tokens = DEFARG_TOKENS (default_arg);
16232 cp_parser_push_lexer_for_tokens (parser, tokens);
16233
16234 /* Parse the assignment-expression. */
16235 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16236
16237 if (!processing_template_decl)
16238 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16239
16240 TREE_PURPOSE (parm) = parsed_arg;
16241
16242 /* Update any instantiations we've already created. */
16243 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16244 VEC_iterate (tree, insts, ix, copy); ix++)
16245 TREE_PURPOSE (copy) = parsed_arg;
16246
16247 /* If the token stream has not been completely used up, then
16248 there was extra junk after the end of the default
16249 argument. */
16250 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16251 cp_parser_error (parser, "expected %<,%>");
16252
16253 /* Revert to the main lexer. */
16254 cp_parser_pop_lexer (parser);
16255 }
16256
16257 /* Make sure no default arg is missing. */
16258 check_default_args (fn);
16259
16260 /* Restore the state of local_variables_forbidden_p. */
16261 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16262
16263 /* Restore the queue. */
16264 parser->unparsed_functions_queues
16265 = TREE_CHAIN (parser->unparsed_functions_queues);
16266 }
16267
16268 /* Parse the operand of `sizeof' (or a similar operator). Returns
16269 either a TYPE or an expression, depending on the form of the
16270 input. The KEYWORD indicates which kind of expression we have
16271 encountered. */
16272
16273 static tree
cp_parser_sizeof_operand(cp_parser * parser,enum rid keyword)16274 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16275 {
16276 static const char *format;
16277 tree expr = NULL_TREE;
16278 const char *saved_message;
16279 bool saved_integral_constant_expression_p;
16280 bool saved_non_integral_constant_expression_p;
16281
16282 /* Initialize FORMAT the first time we get here. */
16283 if (!format)
16284 format = "types may not be defined in '%s' expressions";
16285
16286 /* Types cannot be defined in a `sizeof' expression. Save away the
16287 old message. */
16288 saved_message = parser->type_definition_forbidden_message;
16289 /* And create the new one. */
16290 parser->type_definition_forbidden_message
16291 = XNEWVEC (const char, strlen (format)
16292 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16293 + 1 /* `\0' */);
16294 sprintf ((char *) parser->type_definition_forbidden_message,
16295 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16296
16297 /* The restrictions on constant-expressions do not apply inside
16298 sizeof expressions. */
16299 saved_integral_constant_expression_p
16300 = parser->integral_constant_expression_p;
16301 saved_non_integral_constant_expression_p
16302 = parser->non_integral_constant_expression_p;
16303 parser->integral_constant_expression_p = false;
16304
16305 /* Do not actually evaluate the expression. */
16306 ++skip_evaluation;
16307 /* If it's a `(', then we might be looking at the type-id
16308 construction. */
16309 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16310 {
16311 tree type;
16312 bool saved_in_type_id_in_expr_p;
16313
16314 /* We can't be sure yet whether we're looking at a type-id or an
16315 expression. */
16316 cp_parser_parse_tentatively (parser);
16317 /* Consume the `('. */
16318 cp_lexer_consume_token (parser->lexer);
16319 /* Parse the type-id. */
16320 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16321 parser->in_type_id_in_expr_p = true;
16322 type = cp_parser_type_id (parser);
16323 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16324 /* Now, look for the trailing `)'. */
16325 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16326 /* If all went well, then we're done. */
16327 if (cp_parser_parse_definitely (parser))
16328 {
16329 cp_decl_specifier_seq decl_specs;
16330
16331 /* Build a trivial decl-specifier-seq. */
16332 clear_decl_specs (&decl_specs);
16333 decl_specs.type = type;
16334
16335 /* Call grokdeclarator to figure out what type this is. */
16336 expr = grokdeclarator (NULL,
16337 &decl_specs,
16338 TYPENAME,
16339 /*initialized=*/0,
16340 /*attrlist=*/NULL);
16341 }
16342 }
16343
16344 /* If the type-id production did not work out, then we must be
16345 looking at the unary-expression production. */
16346 if (!expr)
16347 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16348 /*cast_p=*/false);
16349 /* Go back to evaluating expressions. */
16350 --skip_evaluation;
16351
16352 /* Free the message we created. */
16353 free ((char *) parser->type_definition_forbidden_message);
16354 /* And restore the old one. */
16355 parser->type_definition_forbidden_message = saved_message;
16356 parser->integral_constant_expression_p
16357 = saved_integral_constant_expression_p;
16358 parser->non_integral_constant_expression_p
16359 = saved_non_integral_constant_expression_p;
16360
16361 return expr;
16362 }
16363
16364 /* If the current declaration has no declarator, return true. */
16365
16366 static bool
cp_parser_declares_only_class_p(cp_parser * parser)16367 cp_parser_declares_only_class_p (cp_parser *parser)
16368 {
16369 /* If the next token is a `;' or a `,' then there is no
16370 declarator. */
16371 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16372 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16373 }
16374
16375 /* Update the DECL_SPECS to reflect the storage class indicated by
16376 KEYWORD. */
16377
16378 static void
cp_parser_set_storage_class(cp_parser * parser,cp_decl_specifier_seq * decl_specs,enum rid keyword)16379 cp_parser_set_storage_class (cp_parser *parser,
16380 cp_decl_specifier_seq *decl_specs,
16381 enum rid keyword)
16382 {
16383 cp_storage_class storage_class;
16384
16385 if (parser->in_unbraced_linkage_specification_p)
16386 {
16387 error ("invalid use of %qD in linkage specification",
16388 ridpointers[keyword]);
16389 return;
16390 }
16391 else if (decl_specs->storage_class != sc_none)
16392 {
16393 decl_specs->conflicting_specifiers_p = true;
16394 return;
16395 }
16396
16397 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16398 && decl_specs->specs[(int) ds_thread])
16399 {
16400 error ("%<__thread%> before %qD", ridpointers[keyword]);
16401 decl_specs->specs[(int) ds_thread] = 0;
16402 }
16403
16404 switch (keyword)
16405 {
16406 case RID_AUTO:
16407 storage_class = sc_auto;
16408 break;
16409 case RID_REGISTER:
16410 storage_class = sc_register;
16411 break;
16412 case RID_STATIC:
16413 storage_class = sc_static;
16414 break;
16415 case RID_EXTERN:
16416 storage_class = sc_extern;
16417 break;
16418 case RID_MUTABLE:
16419 storage_class = sc_mutable;
16420 break;
16421 default:
16422 gcc_unreachable ();
16423 }
16424 decl_specs->storage_class = storage_class;
16425
16426 /* A storage class specifier cannot be applied alongside a typedef
16427 specifier. If there is a typedef specifier present then set
16428 conflicting_specifiers_p which will trigger an error later
16429 on in grokdeclarator. */
16430 if (decl_specs->specs[(int)ds_typedef])
16431 decl_specs->conflicting_specifiers_p = true;
16432 }
16433
16434 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16435 is true, the type is a user-defined type; otherwise it is a
16436 built-in type specified by a keyword. */
16437
16438 static void
cp_parser_set_decl_spec_type(cp_decl_specifier_seq * decl_specs,tree type_spec,bool user_defined_p)16439 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16440 tree type_spec,
16441 bool user_defined_p)
16442 {
16443 decl_specs->any_specifiers_p = true;
16444
16445 /* If the user tries to redeclare bool or wchar_t (with, for
16446 example, in "typedef int wchar_t;") we remember that this is what
16447 happened. In system headers, we ignore these declarations so
16448 that G++ can work with system headers that are not C++-safe. */
16449 if (decl_specs->specs[(int) ds_typedef]
16450 && !user_defined_p
16451 && (type_spec == boolean_type_node
16452 || type_spec == wchar_type_node)
16453 && (decl_specs->type
16454 || decl_specs->specs[(int) ds_long]
16455 || decl_specs->specs[(int) ds_short]
16456 || decl_specs->specs[(int) ds_unsigned]
16457 || decl_specs->specs[(int) ds_signed]))
16458 {
16459 decl_specs->redefined_builtin_type = type_spec;
16460 if (!decl_specs->type)
16461 {
16462 decl_specs->type = type_spec;
16463 decl_specs->user_defined_type_p = false;
16464 }
16465 }
16466 else if (decl_specs->type)
16467 decl_specs->multiple_types_p = true;
16468 else
16469 {
16470 decl_specs->type = type_spec;
16471 decl_specs->user_defined_type_p = user_defined_p;
16472 decl_specs->redefined_builtin_type = NULL_TREE;
16473 }
16474 }
16475
16476 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16477 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16478
16479 static bool
cp_parser_friend_p(const cp_decl_specifier_seq * decl_specifiers)16480 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16481 {
16482 return decl_specifiers->specs[(int) ds_friend] != 0;
16483 }
16484
16485 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16486 issue an error message indicating that TOKEN_DESC was expected.
16487
16488 Returns the token consumed, if the token had the appropriate type.
16489 Otherwise, returns NULL. */
16490
16491 static cp_token *
cp_parser_require(cp_parser * parser,enum cpp_ttype type,const char * token_desc)16492 cp_parser_require (cp_parser* parser,
16493 enum cpp_ttype type,
16494 const char* token_desc)
16495 {
16496 if (cp_lexer_next_token_is (parser->lexer, type))
16497 return cp_lexer_consume_token (parser->lexer);
16498 else
16499 {
16500 /* Output the MESSAGE -- unless we're parsing tentatively. */
16501 if (!cp_parser_simulate_error (parser))
16502 {
16503 char *message = concat ("expected ", token_desc, NULL);
16504 cp_parser_error (parser, message);
16505 free (message);
16506 }
16507 return NULL;
16508 }
16509 }
16510
16511 /* An error message is produced if the next token is not '>'.
16512 All further tokens are skipped until the desired token is
16513 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16514
16515 static void
cp_parser_skip_to_end_of_template_parameter_list(cp_parser * parser)16516 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16517 {
16518 /* Current level of '< ... >'. */
16519 unsigned level = 0;
16520 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16521 unsigned nesting_depth = 0;
16522
16523 /* Are we ready, yet? If not, issue error message. */
16524 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16525 return;
16526
16527 /* Skip tokens until the desired token is found. */
16528 while (true)
16529 {
16530 /* Peek at the next token. */
16531 switch (cp_lexer_peek_token (parser->lexer)->type)
16532 {
16533 case CPP_LESS:
16534 if (!nesting_depth)
16535 ++level;
16536 break;
16537
16538 case CPP_GREATER:
16539 if (!nesting_depth && level-- == 0)
16540 {
16541 /* We've reached the token we want, consume it and stop. */
16542 cp_lexer_consume_token (parser->lexer);
16543 return;
16544 }
16545 break;
16546
16547 case CPP_OPEN_PAREN:
16548 case CPP_OPEN_SQUARE:
16549 ++nesting_depth;
16550 break;
16551
16552 case CPP_CLOSE_PAREN:
16553 case CPP_CLOSE_SQUARE:
16554 if (nesting_depth-- == 0)
16555 return;
16556 break;
16557
16558 case CPP_EOF:
16559 case CPP_PRAGMA_EOL:
16560 case CPP_SEMICOLON:
16561 case CPP_OPEN_BRACE:
16562 case CPP_CLOSE_BRACE:
16563 /* The '>' was probably forgotten, don't look further. */
16564 return;
16565
16566 default:
16567 break;
16568 }
16569
16570 /* Consume this token. */
16571 cp_lexer_consume_token (parser->lexer);
16572 }
16573 }
16574
16575 /* If the next token is the indicated keyword, consume it. Otherwise,
16576 issue an error message indicating that TOKEN_DESC was expected.
16577
16578 Returns the token consumed, if the token had the appropriate type.
16579 Otherwise, returns NULL. */
16580
16581 static cp_token *
cp_parser_require_keyword(cp_parser * parser,enum rid keyword,const char * token_desc)16582 cp_parser_require_keyword (cp_parser* parser,
16583 enum rid keyword,
16584 const char* token_desc)
16585 {
16586 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16587
16588 if (token && token->keyword != keyword)
16589 {
16590 dyn_string_t error_msg;
16591
16592 /* Format the error message. */
16593 error_msg = dyn_string_new (0);
16594 dyn_string_append_cstr (error_msg, "expected ");
16595 dyn_string_append_cstr (error_msg, token_desc);
16596 cp_parser_error (parser, error_msg->s);
16597 dyn_string_delete (error_msg);
16598 return NULL;
16599 }
16600
16601 return token;
16602 }
16603
16604 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16605 function-definition. */
16606
16607 static bool
cp_parser_token_starts_function_definition_p(cp_token * token)16608 cp_parser_token_starts_function_definition_p (cp_token* token)
16609 {
16610 return (/* An ordinary function-body begins with an `{'. */
16611 token->type == CPP_OPEN_BRACE
16612 /* A ctor-initializer begins with a `:'. */
16613 || token->type == CPP_COLON
16614 /* A function-try-block begins with `try'. */
16615 || token->keyword == RID_TRY
16616 /* The named return value extension begins with `return'. */
16617 || token->keyword == RID_RETURN);
16618 }
16619
16620 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16621 definition. */
16622
16623 static bool
cp_parser_next_token_starts_class_definition_p(cp_parser * parser)16624 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16625 {
16626 cp_token *token;
16627
16628 token = cp_lexer_peek_token (parser->lexer);
16629 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16630 }
16631
16632 /* Returns TRUE iff the next token is the "," or ">" ending a
16633 template-argument. */
16634
16635 static bool
cp_parser_next_token_ends_template_argument_p(cp_parser * parser)16636 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16637 {
16638 cp_token *token;
16639
16640 token = cp_lexer_peek_token (parser->lexer);
16641 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16642 }
16643
16644 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16645 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16646
16647 static bool
cp_parser_nth_token_starts_template_argument_list_p(cp_parser * parser,size_t n)16648 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16649 size_t n)
16650 {
16651 cp_token *token;
16652
16653 token = cp_lexer_peek_nth_token (parser->lexer, n);
16654 if (token->type == CPP_LESS)
16655 return true;
16656 /* Check for the sequence `<::' in the original code. It would be lexed as
16657 `[:', where `[' is a digraph, and there is no whitespace before
16658 `:'. */
16659 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16660 {
16661 cp_token *token2;
16662 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16663 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16664 return true;
16665 }
16666 return false;
16667 }
16668
16669 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16670 or none_type otherwise. */
16671
16672 static enum tag_types
cp_parser_token_is_class_key(cp_token * token)16673 cp_parser_token_is_class_key (cp_token* token)
16674 {
16675 switch (token->keyword)
16676 {
16677 case RID_CLASS:
16678 return class_type;
16679 case RID_STRUCT:
16680 return record_type;
16681 case RID_UNION:
16682 return union_type;
16683
16684 default:
16685 return none_type;
16686 }
16687 }
16688
16689 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16690
16691 static void
cp_parser_check_class_key(enum tag_types class_key,tree type)16692 cp_parser_check_class_key (enum tag_types class_key, tree type)
16693 {
16694 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16695 pedwarn ("%qs tag used in naming %q#T",
16696 class_key == union_type ? "union"
16697 : class_key == record_type ? "struct" : "class",
16698 type);
16699 }
16700
16701 /* Issue an error message if DECL is redeclared with different
16702 access than its original declaration [class.access.spec/3].
16703 This applies to nested classes and nested class templates.
16704 [class.mem/1]. */
16705
16706 static void
cp_parser_check_access_in_redeclaration(tree decl)16707 cp_parser_check_access_in_redeclaration (tree decl)
16708 {
16709 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16710 return;
16711
16712 if ((TREE_PRIVATE (decl)
16713 != (current_access_specifier == access_private_node))
16714 || (TREE_PROTECTED (decl)
16715 != (current_access_specifier == access_protected_node)))
16716 error ("%qD redeclared with different access", decl);
16717 }
16718
16719 /* Look for the `template' keyword, as a syntactic disambiguator.
16720 Return TRUE iff it is present, in which case it will be
16721 consumed. */
16722
16723 static bool
cp_parser_optional_template_keyword(cp_parser * parser)16724 cp_parser_optional_template_keyword (cp_parser *parser)
16725 {
16726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16727 {
16728 /* The `template' keyword can only be used within templates;
16729 outside templates the parser can always figure out what is a
16730 template and what is not. */
16731 if (!processing_template_decl)
16732 {
16733 error ("%<template%> (as a disambiguator) is only allowed "
16734 "within templates");
16735 /* If this part of the token stream is rescanned, the same
16736 error message would be generated. So, we purge the token
16737 from the stream. */
16738 cp_lexer_purge_token (parser->lexer);
16739 return false;
16740 }
16741 else
16742 {
16743 /* Consume the `template' keyword. */
16744 cp_lexer_consume_token (parser->lexer);
16745 return true;
16746 }
16747 }
16748
16749 return false;
16750 }
16751
16752 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16753 set PARSER->SCOPE, and perform other related actions. */
16754
16755 static void
cp_parser_pre_parsed_nested_name_specifier(cp_parser * parser)16756 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16757 {
16758 int i;
16759 struct tree_check *check_value;
16760 deferred_access_check *chk;
16761 VEC (deferred_access_check,gc) *checks;
16762
16763 /* Get the stored value. */
16764 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16765 /* Perform any access checks that were deferred. */
16766 checks = check_value->checks;
16767 if (checks)
16768 {
16769 for (i = 0 ;
16770 VEC_iterate (deferred_access_check, checks, i, chk) ;
16771 ++i)
16772 {
16773 perform_or_defer_access_check (chk->binfo,
16774 chk->decl,
16775 chk->diag_decl);
16776 }
16777 }
16778 /* Set the scope from the stored value. */
16779 parser->scope = check_value->value;
16780 parser->qualifying_scope = check_value->qualifying_scope;
16781 parser->object_scope = NULL_TREE;
16782 }
16783
16784 /* Consume tokens up through a non-nested END token. */
16785
16786 static void
cp_parser_cache_group(cp_parser * parser,enum cpp_ttype end,unsigned depth)16787 cp_parser_cache_group (cp_parser *parser,
16788 enum cpp_ttype end,
16789 unsigned depth)
16790 {
16791 while (true)
16792 {
16793 cp_token *token;
16794
16795 /* Abort a parenthesized expression if we encounter a brace. */
16796 if ((end == CPP_CLOSE_PAREN || depth == 0)
16797 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16798 return;
16799 /* If we've reached the end of the file, stop. */
16800 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16801 || (end != CPP_PRAGMA_EOL
16802 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16803 return;
16804 /* Consume the next token. */
16805 token = cp_lexer_consume_token (parser->lexer);
16806 /* See if it starts a new group. */
16807 if (token->type == CPP_OPEN_BRACE)
16808 {
16809 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16810 if (depth == 0)
16811 return;
16812 }
16813 else if (token->type == CPP_OPEN_PAREN)
16814 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16815 else if (token->type == CPP_PRAGMA)
16816 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16817 else if (token->type == end)
16818 return;
16819 }
16820 }
16821
16822 /* Begin parsing tentatively. We always save tokens while parsing
16823 tentatively so that if the tentative parsing fails we can restore the
16824 tokens. */
16825
16826 static void
cp_parser_parse_tentatively(cp_parser * parser)16827 cp_parser_parse_tentatively (cp_parser* parser)
16828 {
16829 /* Enter a new parsing context. */
16830 parser->context = cp_parser_context_new (parser->context);
16831 /* Begin saving tokens. */
16832 cp_lexer_save_tokens (parser->lexer);
16833 /* In order to avoid repetitive access control error messages,
16834 access checks are queued up until we are no longer parsing
16835 tentatively. */
16836 push_deferring_access_checks (dk_deferred);
16837 }
16838
16839 /* Commit to the currently active tentative parse. */
16840
16841 static void
cp_parser_commit_to_tentative_parse(cp_parser * parser)16842 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16843 {
16844 cp_parser_context *context;
16845 cp_lexer *lexer;
16846
16847 /* Mark all of the levels as committed. */
16848 lexer = parser->lexer;
16849 for (context = parser->context; context->next; context = context->next)
16850 {
16851 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16852 break;
16853 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16854 while (!cp_lexer_saving_tokens (lexer))
16855 lexer = lexer->next;
16856 cp_lexer_commit_tokens (lexer);
16857 }
16858 }
16859
16860 /* Abort the currently active tentative parse. All consumed tokens
16861 will be rolled back, and no diagnostics will be issued. */
16862
16863 static void
cp_parser_abort_tentative_parse(cp_parser * parser)16864 cp_parser_abort_tentative_parse (cp_parser* parser)
16865 {
16866 cp_parser_simulate_error (parser);
16867 /* Now, pretend that we want to see if the construct was
16868 successfully parsed. */
16869 cp_parser_parse_definitely (parser);
16870 }
16871
16872 /* Stop parsing tentatively. If a parse error has occurred, restore the
16873 token stream. Otherwise, commit to the tokens we have consumed.
16874 Returns true if no error occurred; false otherwise. */
16875
16876 static bool
cp_parser_parse_definitely(cp_parser * parser)16877 cp_parser_parse_definitely (cp_parser* parser)
16878 {
16879 bool error_occurred;
16880 cp_parser_context *context;
16881
16882 /* Remember whether or not an error occurred, since we are about to
16883 destroy that information. */
16884 error_occurred = cp_parser_error_occurred (parser);
16885 /* Remove the topmost context from the stack. */
16886 context = parser->context;
16887 parser->context = context->next;
16888 /* If no parse errors occurred, commit to the tentative parse. */
16889 if (!error_occurred)
16890 {
16891 /* Commit to the tokens read tentatively, unless that was
16892 already done. */
16893 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16894 cp_lexer_commit_tokens (parser->lexer);
16895
16896 pop_to_parent_deferring_access_checks ();
16897 }
16898 /* Otherwise, if errors occurred, roll back our state so that things
16899 are just as they were before we began the tentative parse. */
16900 else
16901 {
16902 cp_lexer_rollback_tokens (parser->lexer);
16903 pop_deferring_access_checks ();
16904 }
16905 /* Add the context to the front of the free list. */
16906 context->next = cp_parser_context_free_list;
16907 cp_parser_context_free_list = context;
16908
16909 return !error_occurred;
16910 }
16911
16912 /* Returns true if we are parsing tentatively and are not committed to
16913 this tentative parse. */
16914
16915 static bool
cp_parser_uncommitted_to_tentative_parse_p(cp_parser * parser)16916 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16917 {
16918 return (cp_parser_parsing_tentatively (parser)
16919 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16920 }
16921
16922 /* Returns nonzero iff an error has occurred during the most recent
16923 tentative parse. */
16924
16925 static bool
cp_parser_error_occurred(cp_parser * parser)16926 cp_parser_error_occurred (cp_parser* parser)
16927 {
16928 return (cp_parser_parsing_tentatively (parser)
16929 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16930 }
16931
16932 /* Returns nonzero if GNU extensions are allowed. */
16933
16934 static bool
cp_parser_allow_gnu_extensions_p(cp_parser * parser)16935 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16936 {
16937 return parser->allow_gnu_extensions_p;
16938 }
16939
16940 /* Objective-C++ Productions */
16941
16942
16943 /* Parse an Objective-C expression, which feeds into a primary-expression
16944 above.
16945
16946 objc-expression:
16947 objc-message-expression
16948 objc-string-literal
16949 objc-encode-expression
16950 objc-protocol-expression
16951 objc-selector-expression
16952
16953 Returns a tree representation of the expression. */
16954
16955 static tree
cp_parser_objc_expression(cp_parser * parser)16956 cp_parser_objc_expression (cp_parser* parser)
16957 {
16958 /* Try to figure out what kind of declaration is present. */
16959 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16960
16961 switch (kwd->type)
16962 {
16963 case CPP_OPEN_SQUARE:
16964 return cp_parser_objc_message_expression (parser);
16965
16966 case CPP_OBJC_STRING:
16967 kwd = cp_lexer_consume_token (parser->lexer);
16968 return objc_build_string_object (kwd->u.value);
16969
16970 case CPP_KEYWORD:
16971 switch (kwd->keyword)
16972 {
16973 case RID_AT_ENCODE:
16974 return cp_parser_objc_encode_expression (parser);
16975
16976 case RID_AT_PROTOCOL:
16977 return cp_parser_objc_protocol_expression (parser);
16978
16979 case RID_AT_SELECTOR:
16980 return cp_parser_objc_selector_expression (parser);
16981
16982 default:
16983 break;
16984 }
16985 default:
16986 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
16987 cp_parser_skip_to_end_of_block_or_statement (parser);
16988 }
16989
16990 return error_mark_node;
16991 }
16992
16993 /* Parse an Objective-C message expression.
16994
16995 objc-message-expression:
16996 [ objc-message-receiver objc-message-args ]
16997
16998 Returns a representation of an Objective-C message. */
16999
17000 static tree
cp_parser_objc_message_expression(cp_parser * parser)17001 cp_parser_objc_message_expression (cp_parser* parser)
17002 {
17003 tree receiver, messageargs;
17004
17005 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17006 receiver = cp_parser_objc_message_receiver (parser);
17007 messageargs = cp_parser_objc_message_args (parser);
17008 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17009
17010 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17011 }
17012
17013 /* Parse an objc-message-receiver.
17014
17015 objc-message-receiver:
17016 expression
17017 simple-type-specifier
17018
17019 Returns a representation of the type or expression. */
17020
17021 static tree
cp_parser_objc_message_receiver(cp_parser * parser)17022 cp_parser_objc_message_receiver (cp_parser* parser)
17023 {
17024 tree rcv;
17025
17026 /* An Objective-C message receiver may be either (1) a type
17027 or (2) an expression. */
17028 cp_parser_parse_tentatively (parser);
17029 rcv = cp_parser_expression (parser, false);
17030
17031 if (cp_parser_parse_definitely (parser))
17032 return rcv;
17033
17034 rcv = cp_parser_simple_type_specifier (parser,
17035 /*decl_specs=*/NULL,
17036 CP_PARSER_FLAGS_NONE);
17037
17038 return objc_get_class_reference (rcv);
17039 }
17040
17041 /* Parse the arguments and selectors comprising an Objective-C message.
17042
17043 objc-message-args:
17044 objc-selector
17045 objc-selector-args
17046 objc-selector-args , objc-comma-args
17047
17048 objc-selector-args:
17049 objc-selector [opt] : assignment-expression
17050 objc-selector-args objc-selector [opt] : assignment-expression
17051
17052 objc-comma-args:
17053 assignment-expression
17054 objc-comma-args , assignment-expression
17055
17056 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17057 selector arguments and TREE_VALUE containing a list of comma
17058 arguments. */
17059
17060 static tree
cp_parser_objc_message_args(cp_parser * parser)17061 cp_parser_objc_message_args (cp_parser* parser)
17062 {
17063 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17064 bool maybe_unary_selector_p = true;
17065 cp_token *token = cp_lexer_peek_token (parser->lexer);
17066
17067 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17068 {
17069 tree selector = NULL_TREE, arg;
17070
17071 if (token->type != CPP_COLON)
17072 selector = cp_parser_objc_selector (parser);
17073
17074 /* Detect if we have a unary selector. */
17075 if (maybe_unary_selector_p
17076 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17077 return build_tree_list (selector, NULL_TREE);
17078
17079 maybe_unary_selector_p = false;
17080 cp_parser_require (parser, CPP_COLON, "`:'");
17081 arg = cp_parser_assignment_expression (parser, false);
17082
17083 sel_args
17084 = chainon (sel_args,
17085 build_tree_list (selector, arg));
17086
17087 token = cp_lexer_peek_token (parser->lexer);
17088 }
17089
17090 /* Handle non-selector arguments, if any. */
17091 while (token->type == CPP_COMMA)
17092 {
17093 tree arg;
17094
17095 cp_lexer_consume_token (parser->lexer);
17096 arg = cp_parser_assignment_expression (parser, false);
17097
17098 addl_args
17099 = chainon (addl_args,
17100 build_tree_list (NULL_TREE, arg));
17101
17102 token = cp_lexer_peek_token (parser->lexer);
17103 }
17104
17105 return build_tree_list (sel_args, addl_args);
17106 }
17107
17108 /* Parse an Objective-C encode expression.
17109
17110 objc-encode-expression:
17111 @encode objc-typename
17112
17113 Returns an encoded representation of the type argument. */
17114
17115 static tree
cp_parser_objc_encode_expression(cp_parser * parser)17116 cp_parser_objc_encode_expression (cp_parser* parser)
17117 {
17118 tree type;
17119
17120 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17121 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17122 type = complete_type (cp_parser_type_id (parser));
17123 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17124
17125 if (!type)
17126 {
17127 error ("%<@encode%> must specify a type as an argument");
17128 return error_mark_node;
17129 }
17130
17131 return objc_build_encode_expr (type);
17132 }
17133
17134 /* Parse an Objective-C @defs expression. */
17135
17136 static tree
cp_parser_objc_defs_expression(cp_parser * parser)17137 cp_parser_objc_defs_expression (cp_parser *parser)
17138 {
17139 tree name;
17140
17141 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17142 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17143 name = cp_parser_identifier (parser);
17144 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17145
17146 return objc_get_class_ivars (name);
17147 }
17148
17149 /* Parse an Objective-C protocol expression.
17150
17151 objc-protocol-expression:
17152 @protocol ( identifier )
17153
17154 Returns a representation of the protocol expression. */
17155
17156 static tree
cp_parser_objc_protocol_expression(cp_parser * parser)17157 cp_parser_objc_protocol_expression (cp_parser* parser)
17158 {
17159 tree proto;
17160
17161 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17162 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17163 proto = cp_parser_identifier (parser);
17164 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17165
17166 return objc_build_protocol_expr (proto);
17167 }
17168
17169 /* Parse an Objective-C selector expression.
17170
17171 objc-selector-expression:
17172 @selector ( objc-method-signature )
17173
17174 objc-method-signature:
17175 objc-selector
17176 objc-selector-seq
17177
17178 objc-selector-seq:
17179 objc-selector :
17180 objc-selector-seq objc-selector :
17181
17182 Returns a representation of the method selector. */
17183
17184 static tree
cp_parser_objc_selector_expression(cp_parser * parser)17185 cp_parser_objc_selector_expression (cp_parser* parser)
17186 {
17187 tree sel_seq = NULL_TREE;
17188 bool maybe_unary_selector_p = true;
17189 cp_token *token;
17190
17191 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17192 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17193 token = cp_lexer_peek_token (parser->lexer);
17194
17195 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17196 || token->type == CPP_SCOPE)
17197 {
17198 tree selector = NULL_TREE;
17199
17200 if (token->type != CPP_COLON
17201 || token->type == CPP_SCOPE)
17202 selector = cp_parser_objc_selector (parser);
17203
17204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17205 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17206 {
17207 /* Detect if we have a unary selector. */
17208 if (maybe_unary_selector_p)
17209 {
17210 sel_seq = selector;
17211 goto finish_selector;
17212 }
17213 else
17214 {
17215 cp_parser_error (parser, "expected %<:%>");
17216 }
17217 }
17218 maybe_unary_selector_p = false;
17219 token = cp_lexer_consume_token (parser->lexer);
17220
17221 if (token->type == CPP_SCOPE)
17222 {
17223 sel_seq
17224 = chainon (sel_seq,
17225 build_tree_list (selector, NULL_TREE));
17226 sel_seq
17227 = chainon (sel_seq,
17228 build_tree_list (NULL_TREE, NULL_TREE));
17229 }
17230 else
17231 sel_seq
17232 = chainon (sel_seq,
17233 build_tree_list (selector, NULL_TREE));
17234
17235 token = cp_lexer_peek_token (parser->lexer);
17236 }
17237
17238 finish_selector:
17239 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17240
17241 return objc_build_selector_expr (sel_seq);
17242 }
17243
17244 /* Parse a list of identifiers.
17245
17246 objc-identifier-list:
17247 identifier
17248 objc-identifier-list , identifier
17249
17250 Returns a TREE_LIST of identifier nodes. */
17251
17252 static tree
cp_parser_objc_identifier_list(cp_parser * parser)17253 cp_parser_objc_identifier_list (cp_parser* parser)
17254 {
17255 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17256 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17257
17258 while (sep->type == CPP_COMMA)
17259 {
17260 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17261 list = chainon (list,
17262 build_tree_list (NULL_TREE,
17263 cp_parser_identifier (parser)));
17264 sep = cp_lexer_peek_token (parser->lexer);
17265 }
17266
17267 return list;
17268 }
17269
17270 /* Parse an Objective-C alias declaration.
17271
17272 objc-alias-declaration:
17273 @compatibility_alias identifier identifier ;
17274
17275 This function registers the alias mapping with the Objective-C front-end.
17276 It returns nothing. */
17277
17278 static void
cp_parser_objc_alias_declaration(cp_parser * parser)17279 cp_parser_objc_alias_declaration (cp_parser* parser)
17280 {
17281 tree alias, orig;
17282
17283 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17284 alias = cp_parser_identifier (parser);
17285 orig = cp_parser_identifier (parser);
17286 objc_declare_alias (alias, orig);
17287 cp_parser_consume_semicolon_at_end_of_statement (parser);
17288 }
17289
17290 /* Parse an Objective-C class forward-declaration.
17291
17292 objc-class-declaration:
17293 @class objc-identifier-list ;
17294
17295 The function registers the forward declarations with the Objective-C
17296 front-end. It returns nothing. */
17297
17298 static void
cp_parser_objc_class_declaration(cp_parser * parser)17299 cp_parser_objc_class_declaration (cp_parser* parser)
17300 {
17301 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17302 objc_declare_class (cp_parser_objc_identifier_list (parser));
17303 cp_parser_consume_semicolon_at_end_of_statement (parser);
17304 }
17305
17306 /* Parse a list of Objective-C protocol references.
17307
17308 objc-protocol-refs-opt:
17309 objc-protocol-refs [opt]
17310
17311 objc-protocol-refs:
17312 < objc-identifier-list >
17313
17314 Returns a TREE_LIST of identifiers, if any. */
17315
17316 static tree
cp_parser_objc_protocol_refs_opt(cp_parser * parser)17317 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17318 {
17319 tree protorefs = NULL_TREE;
17320
17321 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17322 {
17323 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17324 protorefs = cp_parser_objc_identifier_list (parser);
17325 cp_parser_require (parser, CPP_GREATER, "`>'");
17326 }
17327
17328 return protorefs;
17329 }
17330
17331 /* Parse a Objective-C visibility specification. */
17332
17333 static void
cp_parser_objc_visibility_spec(cp_parser * parser)17334 cp_parser_objc_visibility_spec (cp_parser* parser)
17335 {
17336 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17337
17338 switch (vis->keyword)
17339 {
17340 case RID_AT_PRIVATE:
17341 objc_set_visibility (2);
17342 break;
17343 case RID_AT_PROTECTED:
17344 objc_set_visibility (0);
17345 break;
17346 case RID_AT_PUBLIC:
17347 objc_set_visibility (1);
17348 break;
17349 default:
17350 return;
17351 }
17352
17353 /* Eat '@private'/'@protected'/'@public'. */
17354 cp_lexer_consume_token (parser->lexer);
17355 }
17356
17357 /* Parse an Objective-C method type. */
17358
17359 static void
cp_parser_objc_method_type(cp_parser * parser)17360 cp_parser_objc_method_type (cp_parser* parser)
17361 {
17362 objc_set_method_type
17363 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17364 ? PLUS_EXPR
17365 : MINUS_EXPR);
17366 }
17367
17368 /* Parse an Objective-C protocol qualifier. */
17369
17370 static tree
cp_parser_objc_protocol_qualifiers(cp_parser * parser)17371 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17372 {
17373 tree quals = NULL_TREE, node;
17374 cp_token *token = cp_lexer_peek_token (parser->lexer);
17375
17376 node = token->u.value;
17377
17378 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17379 && (node == ridpointers [(int) RID_IN]
17380 || node == ridpointers [(int) RID_OUT]
17381 || node == ridpointers [(int) RID_INOUT]
17382 || node == ridpointers [(int) RID_BYCOPY]
17383 || node == ridpointers [(int) RID_BYREF]
17384 || node == ridpointers [(int) RID_ONEWAY]))
17385 {
17386 quals = tree_cons (NULL_TREE, node, quals);
17387 cp_lexer_consume_token (parser->lexer);
17388 token = cp_lexer_peek_token (parser->lexer);
17389 node = token->u.value;
17390 }
17391
17392 return quals;
17393 }
17394
17395 /* Parse an Objective-C typename. */
17396
17397 static tree
cp_parser_objc_typename(cp_parser * parser)17398 cp_parser_objc_typename (cp_parser* parser)
17399 {
17400 tree typename = NULL_TREE;
17401
17402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17403 {
17404 tree proto_quals, cp_type = NULL_TREE;
17405
17406 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17407 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17408
17409 /* An ObjC type name may consist of just protocol qualifiers, in which
17410 case the type shall default to 'id'. */
17411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17412 cp_type = cp_parser_type_id (parser);
17413
17414 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17415 typename = build_tree_list (proto_quals, cp_type);
17416 }
17417
17418 return typename;
17419 }
17420
17421 /* Check to see if TYPE refers to an Objective-C selector name. */
17422
17423 static bool
cp_parser_objc_selector_p(enum cpp_ttype type)17424 cp_parser_objc_selector_p (enum cpp_ttype type)
17425 {
17426 return (type == CPP_NAME || type == CPP_KEYWORD
17427 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17428 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17429 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17430 || type == CPP_XOR || type == CPP_XOR_EQ);
17431 }
17432
17433 /* Parse an Objective-C selector. */
17434
17435 static tree
cp_parser_objc_selector(cp_parser * parser)17436 cp_parser_objc_selector (cp_parser* parser)
17437 {
17438 cp_token *token = cp_lexer_consume_token (parser->lexer);
17439
17440 if (!cp_parser_objc_selector_p (token->type))
17441 {
17442 error ("invalid Objective-C++ selector name");
17443 return error_mark_node;
17444 }
17445
17446 /* C++ operator names are allowed to appear in ObjC selectors. */
17447 switch (token->type)
17448 {
17449 case CPP_AND_AND: return get_identifier ("and");
17450 case CPP_AND_EQ: return get_identifier ("and_eq");
17451 case CPP_AND: return get_identifier ("bitand");
17452 case CPP_OR: return get_identifier ("bitor");
17453 case CPP_COMPL: return get_identifier ("compl");
17454 case CPP_NOT: return get_identifier ("not");
17455 case CPP_NOT_EQ: return get_identifier ("not_eq");
17456 case CPP_OR_OR: return get_identifier ("or");
17457 case CPP_OR_EQ: return get_identifier ("or_eq");
17458 case CPP_XOR: return get_identifier ("xor");
17459 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17460 default: return token->u.value;
17461 }
17462 }
17463
17464 /* Parse an Objective-C params list. */
17465
17466 static tree
cp_parser_objc_method_keyword_params(cp_parser * parser)17467 cp_parser_objc_method_keyword_params (cp_parser* parser)
17468 {
17469 tree params = NULL_TREE;
17470 bool maybe_unary_selector_p = true;
17471 cp_token *token = cp_lexer_peek_token (parser->lexer);
17472
17473 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17474 {
17475 tree selector = NULL_TREE, typename, identifier;
17476
17477 if (token->type != CPP_COLON)
17478 selector = cp_parser_objc_selector (parser);
17479
17480 /* Detect if we have a unary selector. */
17481 if (maybe_unary_selector_p
17482 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17483 return selector;
17484
17485 maybe_unary_selector_p = false;
17486 cp_parser_require (parser, CPP_COLON, "`:'");
17487 typename = cp_parser_objc_typename (parser);
17488 identifier = cp_parser_identifier (parser);
17489
17490 params
17491 = chainon (params,
17492 objc_build_keyword_decl (selector,
17493 typename,
17494 identifier));
17495
17496 token = cp_lexer_peek_token (parser->lexer);
17497 }
17498
17499 return params;
17500 }
17501
17502 /* Parse the non-keyword Objective-C params. */
17503
17504 static tree
cp_parser_objc_method_tail_params_opt(cp_parser * parser,bool * ellipsisp)17505 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17506 {
17507 tree params = make_node (TREE_LIST);
17508 cp_token *token = cp_lexer_peek_token (parser->lexer);
17509 *ellipsisp = false; /* Initially, assume no ellipsis. */
17510
17511 while (token->type == CPP_COMMA)
17512 {
17513 cp_parameter_declarator *parmdecl;
17514 tree parm;
17515
17516 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17517 token = cp_lexer_peek_token (parser->lexer);
17518
17519 if (token->type == CPP_ELLIPSIS)
17520 {
17521 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17522 *ellipsisp = true;
17523 break;
17524 }
17525
17526 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17527 parm = grokdeclarator (parmdecl->declarator,
17528 &parmdecl->decl_specifiers,
17529 PARM, /*initialized=*/0,
17530 /*attrlist=*/NULL);
17531
17532 chainon (params, build_tree_list (NULL_TREE, parm));
17533 token = cp_lexer_peek_token (parser->lexer);
17534 }
17535
17536 return params;
17537 }
17538
17539 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17540
17541 static void
cp_parser_objc_interstitial_code(cp_parser * parser)17542 cp_parser_objc_interstitial_code (cp_parser* parser)
17543 {
17544 cp_token *token = cp_lexer_peek_token (parser->lexer);
17545
17546 /* If the next token is `extern' and the following token is a string
17547 literal, then we have a linkage specification. */
17548 if (token->keyword == RID_EXTERN
17549 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17550 cp_parser_linkage_specification (parser);
17551 /* Handle #pragma, if any. */
17552 else if (token->type == CPP_PRAGMA)
17553 cp_parser_pragma (parser, pragma_external);
17554 /* Allow stray semicolons. */
17555 else if (token->type == CPP_SEMICOLON)
17556 cp_lexer_consume_token (parser->lexer);
17557 /* Finally, try to parse a block-declaration, or a function-definition. */
17558 else
17559 cp_parser_block_declaration (parser, /*statement_p=*/false);
17560 }
17561
17562 /* Parse a method signature. */
17563
17564 static tree
cp_parser_objc_method_signature(cp_parser * parser)17565 cp_parser_objc_method_signature (cp_parser* parser)
17566 {
17567 tree rettype, kwdparms, optparms;
17568 bool ellipsis = false;
17569
17570 cp_parser_objc_method_type (parser);
17571 rettype = cp_parser_objc_typename (parser);
17572 kwdparms = cp_parser_objc_method_keyword_params (parser);
17573 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17574
17575 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17576 }
17577
17578 /* Pars an Objective-C method prototype list. */
17579
17580 static void
cp_parser_objc_method_prototype_list(cp_parser * parser)17581 cp_parser_objc_method_prototype_list (cp_parser* parser)
17582 {
17583 cp_token *token = cp_lexer_peek_token (parser->lexer);
17584
17585 while (token->keyword != RID_AT_END)
17586 {
17587 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17588 {
17589 objc_add_method_declaration
17590 (cp_parser_objc_method_signature (parser));
17591 cp_parser_consume_semicolon_at_end_of_statement (parser);
17592 }
17593 else
17594 /* Allow for interspersed non-ObjC++ code. */
17595 cp_parser_objc_interstitial_code (parser);
17596
17597 token = cp_lexer_peek_token (parser->lexer);
17598 }
17599
17600 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17601 objc_finish_interface ();
17602 }
17603
17604 /* Parse an Objective-C method definition list. */
17605
17606 static void
cp_parser_objc_method_definition_list(cp_parser * parser)17607 cp_parser_objc_method_definition_list (cp_parser* parser)
17608 {
17609 cp_token *token = cp_lexer_peek_token (parser->lexer);
17610
17611 while (token->keyword != RID_AT_END)
17612 {
17613 tree meth;
17614
17615 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17616 {
17617 push_deferring_access_checks (dk_deferred);
17618 objc_start_method_definition
17619 (cp_parser_objc_method_signature (parser));
17620
17621 /* For historical reasons, we accept an optional semicolon. */
17622 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17623 cp_lexer_consume_token (parser->lexer);
17624
17625 perform_deferred_access_checks ();
17626 stop_deferring_access_checks ();
17627 meth = cp_parser_function_definition_after_declarator (parser,
17628 false);
17629 pop_deferring_access_checks ();
17630 objc_finish_method_definition (meth);
17631 }
17632 else
17633 /* Allow for interspersed non-ObjC++ code. */
17634 cp_parser_objc_interstitial_code (parser);
17635
17636 token = cp_lexer_peek_token (parser->lexer);
17637 }
17638
17639 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17640 objc_finish_implementation ();
17641 }
17642
17643 /* Parse Objective-C ivars. */
17644
17645 static void
cp_parser_objc_class_ivars(cp_parser * parser)17646 cp_parser_objc_class_ivars (cp_parser* parser)
17647 {
17648 cp_token *token = cp_lexer_peek_token (parser->lexer);
17649
17650 if (token->type != CPP_OPEN_BRACE)
17651 return; /* No ivars specified. */
17652
17653 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17654 token = cp_lexer_peek_token (parser->lexer);
17655
17656 while (token->type != CPP_CLOSE_BRACE)
17657 {
17658 cp_decl_specifier_seq declspecs;
17659 int decl_class_or_enum_p;
17660 tree prefix_attributes;
17661
17662 cp_parser_objc_visibility_spec (parser);
17663
17664 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17665 break;
17666
17667 cp_parser_decl_specifier_seq (parser,
17668 CP_PARSER_FLAGS_OPTIONAL,
17669 &declspecs,
17670 &decl_class_or_enum_p);
17671 prefix_attributes = declspecs.attributes;
17672 declspecs.attributes = NULL_TREE;
17673
17674 /* Keep going until we hit the `;' at the end of the
17675 declaration. */
17676 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17677 {
17678 tree width = NULL_TREE, attributes, first_attribute, decl;
17679 cp_declarator *declarator = NULL;
17680 int ctor_dtor_or_conv_p;
17681
17682 /* Check for a (possibly unnamed) bitfield declaration. */
17683 token = cp_lexer_peek_token (parser->lexer);
17684 if (token->type == CPP_COLON)
17685 goto eat_colon;
17686
17687 if (token->type == CPP_NAME
17688 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17689 == CPP_COLON))
17690 {
17691 /* Get the name of the bitfield. */
17692 declarator = make_id_declarator (NULL_TREE,
17693 cp_parser_identifier (parser),
17694 sfk_none);
17695
17696 eat_colon:
17697 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17698 /* Get the width of the bitfield. */
17699 width
17700 = cp_parser_constant_expression (parser,
17701 /*allow_non_constant=*/false,
17702 NULL);
17703 }
17704 else
17705 {
17706 /* Parse the declarator. */
17707 declarator
17708 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17709 &ctor_dtor_or_conv_p,
17710 /*parenthesized_p=*/NULL,
17711 /*member_p=*/false);
17712 }
17713
17714 /* Look for attributes that apply to the ivar. */
17715 attributes = cp_parser_attributes_opt (parser);
17716 /* Remember which attributes are prefix attributes and
17717 which are not. */
17718 first_attribute = attributes;
17719 /* Combine the attributes. */
17720 attributes = chainon (prefix_attributes, attributes);
17721
17722 if (width)
17723 {
17724 /* Create the bitfield declaration. */
17725 decl = grokbitfield (declarator, &declspecs, width);
17726 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17727 }
17728 else
17729 decl = grokfield (declarator, &declspecs,
17730 NULL_TREE, /*init_const_expr_p=*/false,
17731 NULL_TREE, attributes);
17732
17733 /* Add the instance variable. */
17734 objc_add_instance_variable (decl);
17735
17736 /* Reset PREFIX_ATTRIBUTES. */
17737 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17738 attributes = TREE_CHAIN (attributes);
17739 if (attributes)
17740 TREE_CHAIN (attributes) = NULL_TREE;
17741
17742 token = cp_lexer_peek_token (parser->lexer);
17743
17744 if (token->type == CPP_COMMA)
17745 {
17746 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17747 continue;
17748 }
17749 break;
17750 }
17751
17752 cp_parser_consume_semicolon_at_end_of_statement (parser);
17753 token = cp_lexer_peek_token (parser->lexer);
17754 }
17755
17756 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17757 /* For historical reasons, we accept an optional semicolon. */
17758 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17759 cp_lexer_consume_token (parser->lexer);
17760 }
17761
17762 /* Parse an Objective-C protocol declaration. */
17763
17764 static void
cp_parser_objc_protocol_declaration(cp_parser * parser)17765 cp_parser_objc_protocol_declaration (cp_parser* parser)
17766 {
17767 tree proto, protorefs;
17768 cp_token *tok;
17769
17770 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17772 {
17773 error ("identifier expected after %<@protocol%>");
17774 goto finish;
17775 }
17776
17777 /* See if we have a forward declaration or a definition. */
17778 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17779
17780 /* Try a forward declaration first. */
17781 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17782 {
17783 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17784 finish:
17785 cp_parser_consume_semicolon_at_end_of_statement (parser);
17786 }
17787
17788 /* Ok, we got a full-fledged definition (or at least should). */
17789 else
17790 {
17791 proto = cp_parser_identifier (parser);
17792 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17793 objc_start_protocol (proto, protorefs);
17794 cp_parser_objc_method_prototype_list (parser);
17795 }
17796 }
17797
17798 /* Parse an Objective-C superclass or category. */
17799
17800 static void
cp_parser_objc_superclass_or_category(cp_parser * parser,tree * super,tree * categ)17801 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17802 tree *categ)
17803 {
17804 cp_token *next = cp_lexer_peek_token (parser->lexer);
17805
17806 *super = *categ = NULL_TREE;
17807 if (next->type == CPP_COLON)
17808 {
17809 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17810 *super = cp_parser_identifier (parser);
17811 }
17812 else if (next->type == CPP_OPEN_PAREN)
17813 {
17814 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17815 *categ = cp_parser_identifier (parser);
17816 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17817 }
17818 }
17819
17820 /* Parse an Objective-C class interface. */
17821
17822 static void
cp_parser_objc_class_interface(cp_parser * parser)17823 cp_parser_objc_class_interface (cp_parser* parser)
17824 {
17825 tree name, super, categ, protos;
17826
17827 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17828 name = cp_parser_identifier (parser);
17829 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17830 protos = cp_parser_objc_protocol_refs_opt (parser);
17831
17832 /* We have either a class or a category on our hands. */
17833 if (categ)
17834 objc_start_category_interface (name, categ, protos);
17835 else
17836 {
17837 objc_start_class_interface (name, super, protos);
17838 /* Handle instance variable declarations, if any. */
17839 cp_parser_objc_class_ivars (parser);
17840 objc_continue_interface ();
17841 }
17842
17843 cp_parser_objc_method_prototype_list (parser);
17844 }
17845
17846 /* Parse an Objective-C class implementation. */
17847
17848 static void
cp_parser_objc_class_implementation(cp_parser * parser)17849 cp_parser_objc_class_implementation (cp_parser* parser)
17850 {
17851 tree name, super, categ;
17852
17853 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17854 name = cp_parser_identifier (parser);
17855 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17856
17857 /* We have either a class or a category on our hands. */
17858 if (categ)
17859 objc_start_category_implementation (name, categ);
17860 else
17861 {
17862 objc_start_class_implementation (name, super);
17863 /* Handle instance variable declarations, if any. */
17864 cp_parser_objc_class_ivars (parser);
17865 objc_continue_implementation ();
17866 }
17867
17868 cp_parser_objc_method_definition_list (parser);
17869 }
17870
17871 /* Consume the @end token and finish off the implementation. */
17872
17873 static void
cp_parser_objc_end_implementation(cp_parser * parser)17874 cp_parser_objc_end_implementation (cp_parser* parser)
17875 {
17876 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17877 objc_finish_implementation ();
17878 }
17879
17880 /* Parse an Objective-C declaration. */
17881
17882 static void
cp_parser_objc_declaration(cp_parser * parser)17883 cp_parser_objc_declaration (cp_parser* parser)
17884 {
17885 /* Try to figure out what kind of declaration is present. */
17886 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17887
17888 switch (kwd->keyword)
17889 {
17890 case RID_AT_ALIAS:
17891 cp_parser_objc_alias_declaration (parser);
17892 break;
17893 case RID_AT_CLASS:
17894 cp_parser_objc_class_declaration (parser);
17895 break;
17896 case RID_AT_PROTOCOL:
17897 cp_parser_objc_protocol_declaration (parser);
17898 break;
17899 case RID_AT_INTERFACE:
17900 cp_parser_objc_class_interface (parser);
17901 break;
17902 case RID_AT_IMPLEMENTATION:
17903 cp_parser_objc_class_implementation (parser);
17904 break;
17905 case RID_AT_END:
17906 cp_parser_objc_end_implementation (parser);
17907 break;
17908 default:
17909 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17910 cp_parser_skip_to_end_of_block_or_statement (parser);
17911 }
17912 }
17913
17914 /* Parse an Objective-C try-catch-finally statement.
17915
17916 objc-try-catch-finally-stmt:
17917 @try compound-statement objc-catch-clause-seq [opt]
17918 objc-finally-clause [opt]
17919
17920 objc-catch-clause-seq:
17921 objc-catch-clause objc-catch-clause-seq [opt]
17922
17923 objc-catch-clause:
17924 @catch ( exception-declaration ) compound-statement
17925
17926 objc-finally-clause
17927 @finally compound-statement
17928
17929 Returns NULL_TREE. */
17930
17931 static tree
cp_parser_objc_try_catch_finally_statement(cp_parser * parser)17932 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17933 location_t location;
17934 tree stmt;
17935
17936 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17937 location = cp_lexer_peek_token (parser->lexer)->location;
17938 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17939 node, lest it get absorbed into the surrounding block. */
17940 stmt = push_stmt_list ();
17941 cp_parser_compound_statement (parser, NULL, false);
17942 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17943
17944 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17945 {
17946 cp_parameter_declarator *parmdecl;
17947 tree parm;
17948
17949 cp_lexer_consume_token (parser->lexer);
17950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17951 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17952 parm = grokdeclarator (parmdecl->declarator,
17953 &parmdecl->decl_specifiers,
17954 PARM, /*initialized=*/0,
17955 /*attrlist=*/NULL);
17956 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17957 objc_begin_catch_clause (parm);
17958 cp_parser_compound_statement (parser, NULL, false);
17959 objc_finish_catch_clause ();
17960 }
17961
17962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17963 {
17964 cp_lexer_consume_token (parser->lexer);
17965 location = cp_lexer_peek_token (parser->lexer)->location;
17966 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17967 node, lest it get absorbed into the surrounding block. */
17968 stmt = push_stmt_list ();
17969 cp_parser_compound_statement (parser, NULL, false);
17970 objc_build_finally_clause (location, pop_stmt_list (stmt));
17971 }
17972
17973 return objc_finish_try_stmt ();
17974 }
17975
17976 /* Parse an Objective-C synchronized statement.
17977
17978 objc-synchronized-stmt:
17979 @synchronized ( expression ) compound-statement
17980
17981 Returns NULL_TREE. */
17982
17983 static tree
cp_parser_objc_synchronized_statement(cp_parser * parser)17984 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17985 location_t location;
17986 tree lock, stmt;
17987
17988 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17989
17990 location = cp_lexer_peek_token (parser->lexer)->location;
17991 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17992 lock = cp_parser_expression (parser, false);
17993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17994
17995 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17996 node, lest it get absorbed into the surrounding block. */
17997 stmt = push_stmt_list ();
17998 cp_parser_compound_statement (parser, NULL, false);
17999
18000 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18001 }
18002
18003 /* Parse an Objective-C throw statement.
18004
18005 objc-throw-stmt:
18006 @throw assignment-expression [opt] ;
18007
18008 Returns a constructed '@throw' statement. */
18009
18010 static tree
cp_parser_objc_throw_statement(cp_parser * parser)18011 cp_parser_objc_throw_statement (cp_parser *parser) {
18012 tree expr = NULL_TREE;
18013
18014 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18015
18016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18017 expr = cp_parser_assignment_expression (parser, false);
18018
18019 cp_parser_consume_semicolon_at_end_of_statement (parser);
18020
18021 return objc_build_throw_stmt (expr);
18022 }
18023
18024 /* Parse an Objective-C statement. */
18025
18026 static tree
cp_parser_objc_statement(cp_parser * parser)18027 cp_parser_objc_statement (cp_parser * parser) {
18028 /* Try to figure out what kind of declaration is present. */
18029 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18030
18031 switch (kwd->keyword)
18032 {
18033 case RID_AT_TRY:
18034 return cp_parser_objc_try_catch_finally_statement (parser);
18035 case RID_AT_SYNCHRONIZED:
18036 return cp_parser_objc_synchronized_statement (parser);
18037 case RID_AT_THROW:
18038 return cp_parser_objc_throw_statement (parser);
18039 default:
18040 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18041 cp_parser_skip_to_end_of_block_or_statement (parser);
18042 }
18043
18044 return error_mark_node;
18045 }
18046
18047 /* OpenMP 2.5 parsing routines. */
18048
18049 /* All OpenMP clauses. OpenMP 2.5. */
18050 typedef enum pragma_omp_clause {
18051 PRAGMA_OMP_CLAUSE_NONE = 0,
18052
18053 PRAGMA_OMP_CLAUSE_COPYIN,
18054 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
18055 PRAGMA_OMP_CLAUSE_DEFAULT,
18056 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
18057 PRAGMA_OMP_CLAUSE_IF,
18058 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
18059 PRAGMA_OMP_CLAUSE_NOWAIT,
18060 PRAGMA_OMP_CLAUSE_NUM_THREADS,
18061 PRAGMA_OMP_CLAUSE_ORDERED,
18062 PRAGMA_OMP_CLAUSE_PRIVATE,
18063 PRAGMA_OMP_CLAUSE_REDUCTION,
18064 PRAGMA_OMP_CLAUSE_SCHEDULE,
18065 PRAGMA_OMP_CLAUSE_SHARED
18066 } pragma_omp_clause;
18067
18068 /* Returns name of the next clause.
18069 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18070 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18071 returned and the token is consumed. */
18072
18073 static pragma_omp_clause
cp_parser_omp_clause_name(cp_parser * parser)18074 cp_parser_omp_clause_name (cp_parser *parser)
18075 {
18076 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18077
18078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18079 result = PRAGMA_OMP_CLAUSE_IF;
18080 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18081 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18082 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18083 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18084 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18085 {
18086 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18087 const char *p = IDENTIFIER_POINTER (id);
18088
18089 switch (p[0])
18090 {
18091 case 'c':
18092 if (!strcmp ("copyin", p))
18093 result = PRAGMA_OMP_CLAUSE_COPYIN;
18094 else if (!strcmp ("copyprivate", p))
18095 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18096 break;
18097 case 'f':
18098 if (!strcmp ("firstprivate", p))
18099 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18100 break;
18101 case 'l':
18102 if (!strcmp ("lastprivate", p))
18103 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18104 break;
18105 case 'n':
18106 if (!strcmp ("nowait", p))
18107 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18108 else if (!strcmp ("num_threads", p))
18109 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18110 break;
18111 case 'o':
18112 if (!strcmp ("ordered", p))
18113 result = PRAGMA_OMP_CLAUSE_ORDERED;
18114 break;
18115 case 'r':
18116 if (!strcmp ("reduction", p))
18117 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18118 break;
18119 case 's':
18120 if (!strcmp ("schedule", p))
18121 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18122 else if (!strcmp ("shared", p))
18123 result = PRAGMA_OMP_CLAUSE_SHARED;
18124 break;
18125 }
18126 }
18127
18128 if (result != PRAGMA_OMP_CLAUSE_NONE)
18129 cp_lexer_consume_token (parser->lexer);
18130
18131 return result;
18132 }
18133
18134 /* Validate that a clause of the given type does not already exist. */
18135
18136 static void
check_no_duplicate_clause(tree clauses,enum tree_code code,const char * name)18137 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18138 {
18139 tree c;
18140
18141 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18142 if (OMP_CLAUSE_CODE (c) == code)
18143 {
18144 error ("too many %qs clauses", name);
18145 break;
18146 }
18147 }
18148
18149 /* OpenMP 2.5:
18150 variable-list:
18151 identifier
18152 variable-list , identifier
18153
18154 In addition, we match a closing parenthesis. An opening parenthesis
18155 will have been consumed by the caller.
18156
18157 If KIND is nonzero, create the appropriate node and install the decl
18158 in OMP_CLAUSE_DECL and add the node to the head of the list.
18159
18160 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18161 return the list created. */
18162
18163 static tree
cp_parser_omp_var_list_no_open(cp_parser * parser,enum omp_clause_code kind,tree list)18164 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18165 tree list)
18166 {
18167 while (1)
18168 {
18169 tree name, decl;
18170
18171 name = cp_parser_id_expression (parser, /*template_p=*/false,
18172 /*check_dependency_p=*/true,
18173 /*template_p=*/NULL,
18174 /*declarator_p=*/false,
18175 /*optional_p=*/false);
18176 if (name == error_mark_node)
18177 goto skip_comma;
18178
18179 decl = cp_parser_lookup_name_simple (parser, name);
18180 if (decl == error_mark_node)
18181 cp_parser_name_lookup_error (parser, name, decl, NULL);
18182 else if (kind != 0)
18183 {
18184 tree u = build_omp_clause (kind);
18185 OMP_CLAUSE_DECL (u) = decl;
18186 OMP_CLAUSE_CHAIN (u) = list;
18187 list = u;
18188 }
18189 else
18190 list = tree_cons (decl, NULL_TREE, list);
18191
18192 get_comma:
18193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18194 break;
18195 cp_lexer_consume_token (parser->lexer);
18196 }
18197
18198 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18199 {
18200 int ending;
18201
18202 /* Try to resync to an unnested comma. Copied from
18203 cp_parser_parenthesized_expression_list. */
18204 skip_comma:
18205 ending = cp_parser_skip_to_closing_parenthesis (parser,
18206 /*recovering=*/true,
18207 /*or_comma=*/true,
18208 /*consume_paren=*/true);
18209 if (ending < 0)
18210 goto get_comma;
18211 }
18212
18213 return list;
18214 }
18215
18216 /* Similarly, but expect leading and trailing parenthesis. This is a very
18217 common case for omp clauses. */
18218
18219 static tree
cp_parser_omp_var_list(cp_parser * parser,enum omp_clause_code kind,tree list)18220 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18221 {
18222 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18223 return cp_parser_omp_var_list_no_open (parser, kind, list);
18224 return list;
18225 }
18226
18227 /* OpenMP 2.5:
18228 default ( shared | none ) */
18229
18230 static tree
cp_parser_omp_clause_default(cp_parser * parser,tree list)18231 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18232 {
18233 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18234 tree c;
18235
18236 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18237 return list;
18238 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18239 {
18240 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18241 const char *p = IDENTIFIER_POINTER (id);
18242
18243 switch (p[0])
18244 {
18245 case 'n':
18246 if (strcmp ("none", p) != 0)
18247 goto invalid_kind;
18248 kind = OMP_CLAUSE_DEFAULT_NONE;
18249 break;
18250
18251 case 's':
18252 if (strcmp ("shared", p) != 0)
18253 goto invalid_kind;
18254 kind = OMP_CLAUSE_DEFAULT_SHARED;
18255 break;
18256
18257 default:
18258 goto invalid_kind;
18259 }
18260
18261 cp_lexer_consume_token (parser->lexer);
18262 }
18263 else
18264 {
18265 invalid_kind:
18266 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18267 }
18268
18269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18270 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18271 /*or_comma=*/false,
18272 /*consume_paren=*/true);
18273
18274 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18275 return list;
18276
18277 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18278 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18279 OMP_CLAUSE_CHAIN (c) = list;
18280 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18281
18282 return c;
18283 }
18284
18285 /* OpenMP 2.5:
18286 if ( expression ) */
18287
18288 static tree
cp_parser_omp_clause_if(cp_parser * parser,tree list)18289 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18290 {
18291 tree t, c;
18292
18293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18294 return list;
18295
18296 t = cp_parser_condition (parser);
18297
18298 if (t == error_mark_node
18299 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18301 /*or_comma=*/false,
18302 /*consume_paren=*/true);
18303
18304 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18305
18306 c = build_omp_clause (OMP_CLAUSE_IF);
18307 OMP_CLAUSE_IF_EXPR (c) = t;
18308 OMP_CLAUSE_CHAIN (c) = list;
18309
18310 return c;
18311 }
18312
18313 /* OpenMP 2.5:
18314 nowait */
18315
18316 static tree
cp_parser_omp_clause_nowait(cp_parser * parser ATTRIBUTE_UNUSED,tree list)18317 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18318 {
18319 tree c;
18320
18321 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18322
18323 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18324 OMP_CLAUSE_CHAIN (c) = list;
18325 return c;
18326 }
18327
18328 /* OpenMP 2.5:
18329 num_threads ( expression ) */
18330
18331 static tree
cp_parser_omp_clause_num_threads(cp_parser * parser,tree list)18332 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18333 {
18334 tree t, c;
18335
18336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18337 return list;
18338
18339 t = cp_parser_expression (parser, false);
18340
18341 if (t == error_mark_node
18342 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18344 /*or_comma=*/false,
18345 /*consume_paren=*/true);
18346
18347 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18348
18349 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18350 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18351 OMP_CLAUSE_CHAIN (c) = list;
18352
18353 return c;
18354 }
18355
18356 /* OpenMP 2.5:
18357 ordered */
18358
18359 static tree
cp_parser_omp_clause_ordered(cp_parser * parser ATTRIBUTE_UNUSED,tree list)18360 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18361 {
18362 tree c;
18363
18364 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18365
18366 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18367 OMP_CLAUSE_CHAIN (c) = list;
18368 return c;
18369 }
18370
18371 /* OpenMP 2.5:
18372 reduction ( reduction-operator : variable-list )
18373
18374 reduction-operator:
18375 One of: + * - & ^ | && || */
18376
18377 static tree
cp_parser_omp_clause_reduction(cp_parser * parser,tree list)18378 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18379 {
18380 enum tree_code code;
18381 tree nlist, c;
18382
18383 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18384 return list;
18385
18386 switch (cp_lexer_peek_token (parser->lexer)->type)
18387 {
18388 case CPP_PLUS:
18389 code = PLUS_EXPR;
18390 break;
18391 case CPP_MULT:
18392 code = MULT_EXPR;
18393 break;
18394 case CPP_MINUS:
18395 code = MINUS_EXPR;
18396 break;
18397 case CPP_AND:
18398 code = BIT_AND_EXPR;
18399 break;
18400 case CPP_XOR:
18401 code = BIT_XOR_EXPR;
18402 break;
18403 case CPP_OR:
18404 code = BIT_IOR_EXPR;
18405 break;
18406 case CPP_AND_AND:
18407 code = TRUTH_ANDIF_EXPR;
18408 break;
18409 case CPP_OR_OR:
18410 code = TRUTH_ORIF_EXPR;
18411 break;
18412 default:
18413 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18414 resync_fail:
18415 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18416 /*or_comma=*/false,
18417 /*consume_paren=*/true);
18418 return list;
18419 }
18420 cp_lexer_consume_token (parser->lexer);
18421
18422 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18423 goto resync_fail;
18424
18425 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18426 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18427 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18428
18429 return nlist;
18430 }
18431
18432 /* OpenMP 2.5:
18433 schedule ( schedule-kind )
18434 schedule ( schedule-kind , expression )
18435
18436 schedule-kind:
18437 static | dynamic | guided | runtime */
18438
18439 static tree
cp_parser_omp_clause_schedule(cp_parser * parser,tree list)18440 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18441 {
18442 tree c, t;
18443
18444 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18445 return list;
18446
18447 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18448
18449 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18450 {
18451 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18452 const char *p = IDENTIFIER_POINTER (id);
18453
18454 switch (p[0])
18455 {
18456 case 'd':
18457 if (strcmp ("dynamic", p) != 0)
18458 goto invalid_kind;
18459 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18460 break;
18461
18462 case 'g':
18463 if (strcmp ("guided", p) != 0)
18464 goto invalid_kind;
18465 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18466 break;
18467
18468 case 'r':
18469 if (strcmp ("runtime", p) != 0)
18470 goto invalid_kind;
18471 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18472 break;
18473
18474 default:
18475 goto invalid_kind;
18476 }
18477 }
18478 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18479 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18480 else
18481 goto invalid_kind;
18482 cp_lexer_consume_token (parser->lexer);
18483
18484 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18485 {
18486 cp_lexer_consume_token (parser->lexer);
18487
18488 t = cp_parser_assignment_expression (parser, false);
18489
18490 if (t == error_mark_node)
18491 goto resync_fail;
18492 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18493 error ("schedule %<runtime%> does not take "
18494 "a %<chunk_size%> parameter");
18495 else
18496 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18497
18498 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18499 goto resync_fail;
18500 }
18501 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18502 goto resync_fail;
18503
18504 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18505 OMP_CLAUSE_CHAIN (c) = list;
18506 return c;
18507
18508 invalid_kind:
18509 cp_parser_error (parser, "invalid schedule kind");
18510 resync_fail:
18511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18512 /*or_comma=*/false,
18513 /*consume_paren=*/true);
18514 return list;
18515 }
18516
18517 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18518 is a bitmask in MASK. Return the list of clauses found; the result
18519 of clause default goes in *pdefault. */
18520
18521 static tree
cp_parser_omp_all_clauses(cp_parser * parser,unsigned int mask,const char * where,cp_token * pragma_tok)18522 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18523 const char *where, cp_token *pragma_tok)
18524 {
18525 tree clauses = NULL;
18526
18527 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18528 {
18529 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18530 const char *c_name;
18531 tree prev = clauses;
18532
18533 switch (c_kind)
18534 {
18535 case PRAGMA_OMP_CLAUSE_COPYIN:
18536 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18537 c_name = "copyin";
18538 break;
18539 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18540 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18541 clauses);
18542 c_name = "copyprivate";
18543 break;
18544 case PRAGMA_OMP_CLAUSE_DEFAULT:
18545 clauses = cp_parser_omp_clause_default (parser, clauses);
18546 c_name = "default";
18547 break;
18548 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18549 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18550 clauses);
18551 c_name = "firstprivate";
18552 break;
18553 case PRAGMA_OMP_CLAUSE_IF:
18554 clauses = cp_parser_omp_clause_if (parser, clauses);
18555 c_name = "if";
18556 break;
18557 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18558 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18559 clauses);
18560 c_name = "lastprivate";
18561 break;
18562 case PRAGMA_OMP_CLAUSE_NOWAIT:
18563 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18564 c_name = "nowait";
18565 break;
18566 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18567 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18568 c_name = "num_threads";
18569 break;
18570 case PRAGMA_OMP_CLAUSE_ORDERED:
18571 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18572 c_name = "ordered";
18573 break;
18574 case PRAGMA_OMP_CLAUSE_PRIVATE:
18575 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18576 clauses);
18577 c_name = "private";
18578 break;
18579 case PRAGMA_OMP_CLAUSE_REDUCTION:
18580 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18581 c_name = "reduction";
18582 break;
18583 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18584 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18585 c_name = "schedule";
18586 break;
18587 case PRAGMA_OMP_CLAUSE_SHARED:
18588 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18589 clauses);
18590 c_name = "shared";
18591 break;
18592 default:
18593 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18594 goto saw_error;
18595 }
18596
18597 if (((mask >> c_kind) & 1) == 0)
18598 {
18599 /* Remove the invalid clause(s) from the list to avoid
18600 confusing the rest of the compiler. */
18601 clauses = prev;
18602 error ("%qs is not valid for %qs", c_name, where);
18603 }
18604 }
18605 saw_error:
18606 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18607 return finish_omp_clauses (clauses);
18608 }
18609
18610 /* OpenMP 2.5:
18611 structured-block:
18612 statement
18613
18614 In practice, we're also interested in adding the statement to an
18615 outer node. So it is convenient if we work around the fact that
18616 cp_parser_statement calls add_stmt. */
18617
18618 static unsigned
cp_parser_begin_omp_structured_block(cp_parser * parser)18619 cp_parser_begin_omp_structured_block (cp_parser *parser)
18620 {
18621 unsigned save = parser->in_statement;
18622
18623 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18624 This preserves the "not within loop or switch" style error messages
18625 for nonsense cases like
18626 void foo() {
18627 #pragma omp single
18628 break;
18629 }
18630 */
18631 if (parser->in_statement)
18632 parser->in_statement = IN_OMP_BLOCK;
18633
18634 return save;
18635 }
18636
18637 static void
cp_parser_end_omp_structured_block(cp_parser * parser,unsigned save)18638 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18639 {
18640 parser->in_statement = save;
18641 }
18642
18643 static tree
cp_parser_omp_structured_block(cp_parser * parser)18644 cp_parser_omp_structured_block (cp_parser *parser)
18645 {
18646 tree stmt = begin_omp_structured_block ();
18647 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18648
18649 cp_parser_statement (parser, NULL_TREE, false);
18650
18651 cp_parser_end_omp_structured_block (parser, save);
18652 return finish_omp_structured_block (stmt);
18653 }
18654
18655 /* OpenMP 2.5:
18656 # pragma omp atomic new-line
18657 expression-stmt
18658
18659 expression-stmt:
18660 x binop= expr | x++ | ++x | x-- | --x
18661 binop:
18662 +, *, -, /, &, ^, |, <<, >>
18663
18664 where x is an lvalue expression with scalar type. */
18665
18666 static void
cp_parser_omp_atomic(cp_parser * parser,cp_token * pragma_tok)18667 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18668 {
18669 tree lhs, rhs;
18670 enum tree_code code;
18671
18672 cp_parser_require_pragma_eol (parser, pragma_tok);
18673
18674 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18675 /*cast_p=*/false);
18676 switch (TREE_CODE (lhs))
18677 {
18678 case ERROR_MARK:
18679 goto saw_error;
18680
18681 case PREINCREMENT_EXPR:
18682 case POSTINCREMENT_EXPR:
18683 lhs = TREE_OPERAND (lhs, 0);
18684 code = PLUS_EXPR;
18685 rhs = integer_one_node;
18686 break;
18687
18688 case PREDECREMENT_EXPR:
18689 case POSTDECREMENT_EXPR:
18690 lhs = TREE_OPERAND (lhs, 0);
18691 code = MINUS_EXPR;
18692 rhs = integer_one_node;
18693 break;
18694
18695 default:
18696 switch (cp_lexer_peek_token (parser->lexer)->type)
18697 {
18698 case CPP_MULT_EQ:
18699 code = MULT_EXPR;
18700 break;
18701 case CPP_DIV_EQ:
18702 code = TRUNC_DIV_EXPR;
18703 break;
18704 case CPP_PLUS_EQ:
18705 code = PLUS_EXPR;
18706 break;
18707 case CPP_MINUS_EQ:
18708 code = MINUS_EXPR;
18709 break;
18710 case CPP_LSHIFT_EQ:
18711 code = LSHIFT_EXPR;
18712 break;
18713 case CPP_RSHIFT_EQ:
18714 code = RSHIFT_EXPR;
18715 break;
18716 case CPP_AND_EQ:
18717 code = BIT_AND_EXPR;
18718 break;
18719 case CPP_OR_EQ:
18720 code = BIT_IOR_EXPR;
18721 break;
18722 case CPP_XOR_EQ:
18723 code = BIT_XOR_EXPR;
18724 break;
18725 default:
18726 cp_parser_error (parser,
18727 "invalid operator for %<#pragma omp atomic%>");
18728 goto saw_error;
18729 }
18730 cp_lexer_consume_token (parser->lexer);
18731
18732 rhs = cp_parser_expression (parser, false);
18733 if (rhs == error_mark_node)
18734 goto saw_error;
18735 break;
18736 }
18737 finish_omp_atomic (code, lhs, rhs);
18738 cp_parser_consume_semicolon_at_end_of_statement (parser);
18739 return;
18740
18741 saw_error:
18742 cp_parser_skip_to_end_of_block_or_statement (parser);
18743 }
18744
18745
18746 /* OpenMP 2.5:
18747 # pragma omp barrier new-line */
18748
18749 static void
cp_parser_omp_barrier(cp_parser * parser,cp_token * pragma_tok)18750 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18751 {
18752 cp_parser_require_pragma_eol (parser, pragma_tok);
18753 finish_omp_barrier ();
18754 }
18755
18756 /* OpenMP 2.5:
18757 # pragma omp critical [(name)] new-line
18758 structured-block */
18759
18760 static tree
cp_parser_omp_critical(cp_parser * parser,cp_token * pragma_tok)18761 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18762 {
18763 tree stmt, name = NULL;
18764
18765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18766 {
18767 cp_lexer_consume_token (parser->lexer);
18768
18769 name = cp_parser_identifier (parser);
18770
18771 if (name == error_mark_node
18772 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18774 /*or_comma=*/false,
18775 /*consume_paren=*/true);
18776 if (name == error_mark_node)
18777 name = NULL;
18778 }
18779 cp_parser_require_pragma_eol (parser, pragma_tok);
18780
18781 stmt = cp_parser_omp_structured_block (parser);
18782 return c_finish_omp_critical (stmt, name);
18783 }
18784
18785 /* OpenMP 2.5:
18786 # pragma omp flush flush-vars[opt] new-line
18787
18788 flush-vars:
18789 ( variable-list ) */
18790
18791 static void
cp_parser_omp_flush(cp_parser * parser,cp_token * pragma_tok)18792 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18793 {
18794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18795 (void) cp_parser_omp_var_list (parser, 0, NULL);
18796 cp_parser_require_pragma_eol (parser, pragma_tok);
18797
18798 finish_omp_flush ();
18799 }
18800
18801 /* Parse the restricted form of the for statment allowed by OpenMP. */
18802
18803 static tree
cp_parser_omp_for_loop(cp_parser * parser)18804 cp_parser_omp_for_loop (cp_parser *parser)
18805 {
18806 tree init, cond, incr, body, decl, pre_body;
18807 location_t loc;
18808
18809 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18810 {
18811 cp_parser_error (parser, "for statement expected");
18812 return NULL;
18813 }
18814 loc = cp_lexer_consume_token (parser->lexer)->location;
18815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18816 return NULL;
18817
18818 init = decl = NULL;
18819 pre_body = push_stmt_list ();
18820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18821 {
18822 cp_decl_specifier_seq type_specifiers;
18823
18824 /* First, try to parse as an initialized declaration. See
18825 cp_parser_condition, from whence the bulk of this is copied. */
18826
18827 cp_parser_parse_tentatively (parser);
18828 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18829 &type_specifiers);
18830 if (!cp_parser_error_occurred (parser))
18831 {
18832 tree asm_specification, attributes;
18833 cp_declarator *declarator;
18834
18835 declarator = cp_parser_declarator (parser,
18836 CP_PARSER_DECLARATOR_NAMED,
18837 /*ctor_dtor_or_conv_p=*/NULL,
18838 /*parenthesized_p=*/NULL,
18839 /*member_p=*/false);
18840 attributes = cp_parser_attributes_opt (parser);
18841 asm_specification = cp_parser_asm_specification_opt (parser);
18842
18843 cp_parser_require (parser, CPP_EQ, "`='");
18844 if (cp_parser_parse_definitely (parser))
18845 {
18846 tree pushed_scope;
18847
18848 decl = start_decl (declarator, &type_specifiers,
18849 /*initialized_p=*/false, attributes,
18850 /*prefix_attributes=*/NULL_TREE,
18851 &pushed_scope);
18852
18853 init = cp_parser_assignment_expression (parser, false);
18854
18855 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18856 asm_specification, LOOKUP_ONLYCONVERTING);
18857
18858 if (pushed_scope)
18859 pop_scope (pushed_scope);
18860 }
18861 }
18862 else
18863 cp_parser_abort_tentative_parse (parser);
18864
18865 /* If parsing as an initialized declaration failed, try again as
18866 a simple expression. */
18867 if (decl == NULL)
18868 init = cp_parser_expression (parser, false);
18869 }
18870 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18871 pre_body = pop_stmt_list (pre_body);
18872
18873 cond = NULL;
18874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18875 cond = cp_parser_condition (parser);
18876 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18877
18878 incr = NULL;
18879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18880 incr = cp_parser_expression (parser, false);
18881
18882 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18884 /*or_comma=*/false,
18885 /*consume_paren=*/true);
18886
18887 /* Note that we saved the original contents of this flag when we entered
18888 the structured block, and so we don't need to re-save it here. */
18889 parser->in_statement = IN_OMP_FOR;
18890
18891 /* Note that the grammar doesn't call for a structured block here,
18892 though the loop as a whole is a structured block. */
18893 body = push_stmt_list ();
18894 cp_parser_statement (parser, NULL_TREE, false);
18895 body = pop_stmt_list (body);
18896
18897 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18898 }
18899
18900 /* OpenMP 2.5:
18901 #pragma omp for for-clause[optseq] new-line
18902 for-loop */
18903
18904 #define OMP_FOR_CLAUSE_MASK \
18905 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18906 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18907 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18908 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18909 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18910 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18911 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18912
18913 static tree
cp_parser_omp_for(cp_parser * parser,cp_token * pragma_tok)18914 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18915 {
18916 tree clauses, sb, ret;
18917 unsigned int save;
18918
18919 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18920 "#pragma omp for", pragma_tok);
18921
18922 sb = begin_omp_structured_block ();
18923 save = cp_parser_begin_omp_structured_block (parser);
18924
18925 ret = cp_parser_omp_for_loop (parser);
18926 if (ret)
18927 OMP_FOR_CLAUSES (ret) = clauses;
18928
18929 cp_parser_end_omp_structured_block (parser, save);
18930 add_stmt (finish_omp_structured_block (sb));
18931
18932 return ret;
18933 }
18934
18935 /* OpenMP 2.5:
18936 # pragma omp master new-line
18937 structured-block */
18938
18939 static tree
cp_parser_omp_master(cp_parser * parser,cp_token * pragma_tok)18940 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18941 {
18942 cp_parser_require_pragma_eol (parser, pragma_tok);
18943 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18944 }
18945
18946 /* OpenMP 2.5:
18947 # pragma omp ordered new-line
18948 structured-block */
18949
18950 static tree
cp_parser_omp_ordered(cp_parser * parser,cp_token * pragma_tok)18951 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18952 {
18953 cp_parser_require_pragma_eol (parser, pragma_tok);
18954 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18955 }
18956
18957 /* OpenMP 2.5:
18958
18959 section-scope:
18960 { section-sequence }
18961
18962 section-sequence:
18963 section-directive[opt] structured-block
18964 section-sequence section-directive structured-block */
18965
18966 static tree
cp_parser_omp_sections_scope(cp_parser * parser)18967 cp_parser_omp_sections_scope (cp_parser *parser)
18968 {
18969 tree stmt, substmt;
18970 bool error_suppress = false;
18971 cp_token *tok;
18972
18973 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18974 return NULL_TREE;
18975
18976 stmt = push_stmt_list ();
18977
18978 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18979 {
18980 unsigned save;
18981
18982 substmt = begin_omp_structured_block ();
18983 save = cp_parser_begin_omp_structured_block (parser);
18984
18985 while (1)
18986 {
18987 cp_parser_statement (parser, NULL_TREE, false);
18988
18989 tok = cp_lexer_peek_token (parser->lexer);
18990 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18991 break;
18992 if (tok->type == CPP_CLOSE_BRACE)
18993 break;
18994 if (tok->type == CPP_EOF)
18995 break;
18996 }
18997
18998 cp_parser_end_omp_structured_block (parser, save);
18999 substmt = finish_omp_structured_block (substmt);
19000 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19001 add_stmt (substmt);
19002 }
19003
19004 while (1)
19005 {
19006 tok = cp_lexer_peek_token (parser->lexer);
19007 if (tok->type == CPP_CLOSE_BRACE)
19008 break;
19009 if (tok->type == CPP_EOF)
19010 break;
19011
19012 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19013 {
19014 cp_lexer_consume_token (parser->lexer);
19015 cp_parser_require_pragma_eol (parser, tok);
19016 error_suppress = false;
19017 }
19018 else if (!error_suppress)
19019 {
19020 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19021 error_suppress = true;
19022 }
19023
19024 substmt = cp_parser_omp_structured_block (parser);
19025 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19026 add_stmt (substmt);
19027 }
19028 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19029
19030 substmt = pop_stmt_list (stmt);
19031
19032 stmt = make_node (OMP_SECTIONS);
19033 TREE_TYPE (stmt) = void_type_node;
19034 OMP_SECTIONS_BODY (stmt) = substmt;
19035
19036 add_stmt (stmt);
19037 return stmt;
19038 }
19039
19040 /* OpenMP 2.5:
19041 # pragma omp sections sections-clause[optseq] newline
19042 sections-scope */
19043
19044 #define OMP_SECTIONS_CLAUSE_MASK \
19045 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19046 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19047 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19048 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19049 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19050
19051 static tree
cp_parser_omp_sections(cp_parser * parser,cp_token * pragma_tok)19052 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19053 {
19054 tree clauses, ret;
19055
19056 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19057 "#pragma omp sections", pragma_tok);
19058
19059 ret = cp_parser_omp_sections_scope (parser);
19060 if (ret)
19061 OMP_SECTIONS_CLAUSES (ret) = clauses;
19062
19063 return ret;
19064 }
19065
19066 /* OpenMP 2.5:
19067 # pragma parallel parallel-clause new-line
19068 # pragma parallel for parallel-for-clause new-line
19069 # pragma parallel sections parallel-sections-clause new-line */
19070
19071 #define OMP_PARALLEL_CLAUSE_MASK \
19072 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19073 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19074 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19075 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19076 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19077 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19078 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19079 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19080
19081 static tree
cp_parser_omp_parallel(cp_parser * parser,cp_token * pragma_tok)19082 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19083 {
19084 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19085 const char *p_name = "#pragma omp parallel";
19086 tree stmt, clauses, par_clause, ws_clause, block;
19087 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19088 unsigned int save;
19089
19090 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19091 {
19092 cp_lexer_consume_token (parser->lexer);
19093 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19094 p_name = "#pragma omp parallel for";
19095 mask |= OMP_FOR_CLAUSE_MASK;
19096 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19097 }
19098 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19099 {
19100 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19101 const char *p = IDENTIFIER_POINTER (id);
19102 if (strcmp (p, "sections") == 0)
19103 {
19104 cp_lexer_consume_token (parser->lexer);
19105 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19106 p_name = "#pragma omp parallel sections";
19107 mask |= OMP_SECTIONS_CLAUSE_MASK;
19108 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19109 }
19110 }
19111
19112 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19113 block = begin_omp_parallel ();
19114 save = cp_parser_begin_omp_structured_block (parser);
19115
19116 switch (p_kind)
19117 {
19118 case PRAGMA_OMP_PARALLEL:
19119 cp_parser_already_scoped_statement (parser);
19120 par_clause = clauses;
19121 break;
19122
19123 case PRAGMA_OMP_PARALLEL_FOR:
19124 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19125 stmt = cp_parser_omp_for_loop (parser);
19126 if (stmt)
19127 OMP_FOR_CLAUSES (stmt) = ws_clause;
19128 break;
19129
19130 case PRAGMA_OMP_PARALLEL_SECTIONS:
19131 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19132 stmt = cp_parser_omp_sections_scope (parser);
19133 if (stmt)
19134 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19135 break;
19136
19137 default:
19138 gcc_unreachable ();
19139 }
19140
19141 cp_parser_end_omp_structured_block (parser, save);
19142 stmt = finish_omp_parallel (par_clause, block);
19143 if (p_kind != PRAGMA_OMP_PARALLEL)
19144 OMP_PARALLEL_COMBINED (stmt) = 1;
19145 return stmt;
19146 }
19147
19148 /* OpenMP 2.5:
19149 # pragma omp single single-clause[optseq] new-line
19150 structured-block */
19151
19152 #define OMP_SINGLE_CLAUSE_MASK \
19153 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19154 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19155 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19156 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19157
19158 static tree
cp_parser_omp_single(cp_parser * parser,cp_token * pragma_tok)19159 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19160 {
19161 tree stmt = make_node (OMP_SINGLE);
19162 TREE_TYPE (stmt) = void_type_node;
19163
19164 OMP_SINGLE_CLAUSES (stmt)
19165 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19166 "#pragma omp single", pragma_tok);
19167 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19168
19169 return add_stmt (stmt);
19170 }
19171
19172 /* OpenMP 2.5:
19173 # pragma omp threadprivate (variable-list) */
19174
19175 static void
cp_parser_omp_threadprivate(cp_parser * parser,cp_token * pragma_tok)19176 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19177 {
19178 tree vars;
19179
19180 vars = cp_parser_omp_var_list (parser, 0, NULL);
19181 cp_parser_require_pragma_eol (parser, pragma_tok);
19182
19183 if (!targetm.have_tls)
19184 sorry ("threadprivate variables not supported in this target");
19185
19186 finish_omp_threadprivate (vars);
19187 }
19188
19189 /* Main entry point to OpenMP statement pragmas. */
19190
19191 static void
cp_parser_omp_construct(cp_parser * parser,cp_token * pragma_tok)19192 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19193 {
19194 tree stmt;
19195
19196 switch (pragma_tok->pragma_kind)
19197 {
19198 case PRAGMA_OMP_ATOMIC:
19199 cp_parser_omp_atomic (parser, pragma_tok);
19200 return;
19201 case PRAGMA_OMP_CRITICAL:
19202 stmt = cp_parser_omp_critical (parser, pragma_tok);
19203 break;
19204 case PRAGMA_OMP_FOR:
19205 stmt = cp_parser_omp_for (parser, pragma_tok);
19206 break;
19207 case PRAGMA_OMP_MASTER:
19208 stmt = cp_parser_omp_master (parser, pragma_tok);
19209 break;
19210 case PRAGMA_OMP_ORDERED:
19211 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19212 break;
19213 case PRAGMA_OMP_PARALLEL:
19214 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19215 break;
19216 case PRAGMA_OMP_SECTIONS:
19217 stmt = cp_parser_omp_sections (parser, pragma_tok);
19218 break;
19219 case PRAGMA_OMP_SINGLE:
19220 stmt = cp_parser_omp_single (parser, pragma_tok);
19221 break;
19222 default:
19223 gcc_unreachable ();
19224 }
19225
19226 if (stmt)
19227 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19228 }
19229
19230 /* The parser. */
19231
19232 static GTY (()) cp_parser *the_parser;
19233
19234
19235 /* Special handling for the first token or line in the file. The first
19236 thing in the file might be #pragma GCC pch_preprocess, which loads a
19237 PCH file, which is a GC collection point. So we need to handle this
19238 first pragma without benefit of an existing lexer structure.
19239
19240 Always returns one token to the caller in *FIRST_TOKEN. This is
19241 either the true first token of the file, or the first token after
19242 the initial pragma. */
19243
19244 static void
cp_parser_initial_pragma(cp_token * first_token)19245 cp_parser_initial_pragma (cp_token *first_token)
19246 {
19247 tree name = NULL;
19248
19249 cp_lexer_get_preprocessor_token (NULL, first_token);
19250 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19251 return;
19252
19253 cp_lexer_get_preprocessor_token (NULL, first_token);
19254 if (first_token->type == CPP_STRING)
19255 {
19256 name = first_token->u.value;
19257
19258 cp_lexer_get_preprocessor_token (NULL, first_token);
19259 if (first_token->type != CPP_PRAGMA_EOL)
19260 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19261 }
19262 else
19263 error ("expected string literal");
19264
19265 /* Skip to the end of the pragma. */
19266 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19267 cp_lexer_get_preprocessor_token (NULL, first_token);
19268
19269 /* Now actually load the PCH file. */
19270 if (name)
19271 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19272
19273 /* Read one more token to return to our caller. We have to do this
19274 after reading the PCH file in, since its pointers have to be
19275 live. */
19276 cp_lexer_get_preprocessor_token (NULL, first_token);
19277 }
19278
19279 /* Normal parsing of a pragma token. Here we can (and must) use the
19280 regular lexer. */
19281
19282 static bool
cp_parser_pragma(cp_parser * parser,enum pragma_context context)19283 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19284 {
19285 cp_token *pragma_tok;
19286 unsigned int id;
19287
19288 pragma_tok = cp_lexer_consume_token (parser->lexer);
19289 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19290 parser->lexer->in_pragma = true;
19291
19292 id = pragma_tok->pragma_kind;
19293 switch (id)
19294 {
19295 case PRAGMA_GCC_PCH_PREPROCESS:
19296 error ("%<#pragma GCC pch_preprocess%> must be first");
19297 break;
19298
19299 case PRAGMA_OMP_BARRIER:
19300 switch (context)
19301 {
19302 case pragma_compound:
19303 cp_parser_omp_barrier (parser, pragma_tok);
19304 return false;
19305 case pragma_stmt:
19306 error ("%<#pragma omp barrier%> may only be "
19307 "used in compound statements");
19308 break;
19309 default:
19310 goto bad_stmt;
19311 }
19312 break;
19313
19314 case PRAGMA_OMP_FLUSH:
19315 switch (context)
19316 {
19317 case pragma_compound:
19318 cp_parser_omp_flush (parser, pragma_tok);
19319 return false;
19320 case pragma_stmt:
19321 error ("%<#pragma omp flush%> may only be "
19322 "used in compound statements");
19323 break;
19324 default:
19325 goto bad_stmt;
19326 }
19327 break;
19328
19329 case PRAGMA_OMP_THREADPRIVATE:
19330 cp_parser_omp_threadprivate (parser, pragma_tok);
19331 return false;
19332
19333 case PRAGMA_OMP_ATOMIC:
19334 case PRAGMA_OMP_CRITICAL:
19335 case PRAGMA_OMP_FOR:
19336 case PRAGMA_OMP_MASTER:
19337 case PRAGMA_OMP_ORDERED:
19338 case PRAGMA_OMP_PARALLEL:
19339 case PRAGMA_OMP_SECTIONS:
19340 case PRAGMA_OMP_SINGLE:
19341 if (context == pragma_external)
19342 goto bad_stmt;
19343 cp_parser_omp_construct (parser, pragma_tok);
19344 return true;
19345
19346 case PRAGMA_OMP_SECTION:
19347 error ("%<#pragma omp section%> may only be used in "
19348 "%<#pragma omp sections%> construct");
19349 break;
19350
19351 default:
19352 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19353 c_invoke_pragma_handler (id);
19354 break;
19355
19356 bad_stmt:
19357 cp_parser_error (parser, "expected declaration specifiers");
19358 break;
19359 }
19360
19361 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19362 return false;
19363 }
19364
19365 /* The interface the pragma parsers have to the lexer. */
19366
19367 enum cpp_ttype
pragma_lex(tree * value)19368 pragma_lex (tree *value)
19369 {
19370 cp_token *tok;
19371 enum cpp_ttype ret;
19372
19373 tok = cp_lexer_peek_token (the_parser->lexer);
19374
19375 ret = tok->type;
19376 *value = tok->u.value;
19377
19378 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19379 ret = CPP_EOF;
19380 else if (ret == CPP_STRING)
19381 *value = cp_parser_string_literal (the_parser, false, false);
19382 else
19383 {
19384 cp_lexer_consume_token (the_parser->lexer);
19385 if (ret == CPP_KEYWORD)
19386 ret = CPP_NAME;
19387 }
19388
19389 return ret;
19390 }
19391
19392
19393 /* External interface. */
19394
19395 /* Parse one entire translation unit. */
19396
19397 void
c_parse_file(void)19398 c_parse_file (void)
19399 {
19400 bool error_occurred;
19401 static bool already_called = false;
19402
19403 if (already_called)
19404 {
19405 sorry ("inter-module optimizations not implemented for C++");
19406 return;
19407 }
19408 already_called = true;
19409
19410 the_parser = cp_parser_new ();
19411 push_deferring_access_checks (flag_access_control
19412 ? dk_no_deferred : dk_no_check);
19413 error_occurred = cp_parser_translation_unit (the_parser);
19414 the_parser = NULL;
19415 }
19416
19417 /* This variable must be provided by every front end. */
19418
19419 int yydebug;
19420
19421 #include "gt-cp-parser.h"
19422