1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37
38
39 /* The lexer. */
40
41 /* Overview
42 --------
43
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
46
47 Methodology
48 -----------
49
50 We use a circular buffer to store incoming tokens.
51
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
58
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
61
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
65
66 /* A C++ token. */
67
68 typedef struct cp_token GTY (())
69 {
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
75 /* Token flags. */
76 unsigned char flags;
77 /* The value associated with this token, if any. */
78 tree value;
79 /* The location at which this token was found. */
80 location_t location;
81 } cp_token;
82
83 /* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
85
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
87
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
92 complete.)
93
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to constraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
98
99 typedef struct cp_token_block GTY (())
100 {
101 /* The tokens. */
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
104 size_t num_tokens;
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
109 } cp_token_block;
110
111 typedef struct cp_token_cache GTY (())
112 {
113 /* The first block in the cache. NULL if there are no tokens in the
114 cache. */
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
117 cache. */
118 cp_token_block *last;
119 } cp_token_cache;
120
121 /* Prototypes. */
122
123 static cp_token_cache *cp_token_cache_new
124 (void);
125 static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
127
128 /* Create a new cp_token_cache. */
129
130 static cp_token_cache *
cp_token_cache_new(void)131 cp_token_cache_new (void)
132 {
133 return ggc_alloc_cleared (sizeof (cp_token_cache));
134 }
135
136 /* Add *TOKEN to *CACHE. */
137
138 static void
cp_token_cache_push_token(cp_token_cache * cache,cp_token * token)139 cp_token_cache_push_token (cp_token_cache *cache,
140 cp_token *token)
141 {
142 cp_token_block *b = cache->last;
143
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146 {
147 b = ggc_alloc_cleared (sizeof (cp_token_block));
148 b->prev = cache->last;
149 if (cache->last)
150 {
151 cache->last->next = b;
152 cache->last = b;
153 }
154 else
155 cache->first = cache->last = b;
156 }
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
159 }
160
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
163 it to the parser. */
164
165 typedef struct cp_lexer GTY (())
166 {
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip (""))) last_token;
183
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
188
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
193
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
198
199 /* The STRING_CST tokens encountered while processing the current
200 string literal. */
201 varray_type string_tokens;
202
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
205 bool main_lexer_p;
206
207 /* True if we should output debugging information. */
208 bool debugging_p;
209
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
212 } cp_lexer;
213
214 /* Prototypes. */
215
216 static cp_lexer *cp_lexer_new_main
217 (void);
218 static cp_lexer *cp_lexer_new_from_tokens
219 (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
221 (const cp_lexer *);
222 static cp_token *cp_lexer_next_token
223 (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
229 (cp_lexer *);
230 static void cp_lexer_maybe_grow_buffer
231 (cp_lexer *);
232 static void cp_lexer_get_preprocessor_token
233 (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
235 (cp_lexer *);
236 static cp_token *cp_lexer_peek_nth_token
237 (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241 (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243 (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token
245 (cp_lexer *);
246 static void cp_lexer_purge_token
247 (cp_lexer *);
248 static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
251 (cp_lexer *);
252 static void cp_lexer_commit_tokens
253 (cp_lexer *);
254 static void cp_lexer_rollback_tokens
255 (cp_lexer *);
256 static inline void cp_lexer_set_source_position_from_token
257 (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259 (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p
261 (cp_lexer *);
262 static void cp_lexer_start_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265 (cp_lexer *) ATTRIBUTE_UNUSED;
266
267 /* Manifest constants. */
268
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
271
272 /* A token type for keywords, as opposed to ordinary identifiers. */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
274
275 /* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
280
281 /* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
287
288 /* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
291
292 /* Variables. */
293
294 /* The stream to which debugging output should be written. */
295 static FILE *cp_lexer_debug_stream;
296
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
298 preprocessor. */
299
300 static cp_lexer *
cp_lexer_new_main(void)301 cp_lexer_new_main (void)
302 {
303 cp_lexer *lexer;
304 cp_token first_token;
305
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL, &first_token);
310 c_common_no_more_pch ();
311
312 /* Allocate the memory. */
313 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
314
315 /* Create the circular buffer. */
316 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
318
319 /* There is one token in the buffer. */
320 lexer->last_token = lexer->buffer + 1;
321 lexer->first_token = lexer->buffer;
322 lexer->next_token = lexer->buffer;
323 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
324
325 /* This lexer obtains more tokens by calling c_lex. */
326 lexer->main_lexer_p = true;
327
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
330
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
333
334 /* Assume we are not debugging. */
335 lexer->debugging_p = false;
336
337 return lexer;
338 }
339
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
342
343 static cp_lexer *
cp_lexer_new_from_tokens(cp_token_cache * tokens)344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
345 {
346 cp_lexer *lexer;
347 cp_token *token;
348 cp_token_block *block;
349 ptrdiff_t num_tokens;
350
351 /* Allocate the memory. */
352 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
353
354 /* Create a new buffer, appropriately sized. */
355 num_tokens = 0;
356 for (block = tokens->first; block != NULL; block = block->next)
357 num_tokens += block->num_tokens;
358 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359 lexer->buffer_end = lexer->buffer + num_tokens;
360
361 /* Install the tokens. */
362 token = lexer->buffer;
363 for (block = tokens->first; block != NULL; block = block->next)
364 {
365 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366 token += block->num_tokens;
367 }
368
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer->first_token = lexer->buffer;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer->next_token = lexer->buffer;
373 /* The buffer is full. */
374 lexer->last_token = lexer->first_token;
375
376 /* This lexer doesn't obtain more tokens. */
377 lexer->main_lexer_p = false;
378
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
381
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
384
385 /* Assume we are not debugging. */
386 lexer->debugging_p = false;
387
388 return lexer;
389 }
390
391 /* Returns nonzero if debugging information should be output. */
392
393 static inline bool
cp_lexer_debugging_p(cp_lexer * lexer)394 cp_lexer_debugging_p (cp_lexer *lexer)
395 {
396 return lexer->debugging_p;
397 }
398
399 /* Set the current source position from the information stored in
400 TOKEN. */
401
402 static inline void
cp_lexer_set_source_position_from_token(cp_lexer * lexer ATTRIBUTE_UNUSED,const cp_token * token)403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404 const cp_token *token)
405 {
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
408
409 /* Update the line number. */
410 if (token->type != CPP_EOF)
411 input_location = token->location;
412 }
413
414 /* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
416
417 static inline cp_token *
cp_lexer_next_token(cp_lexer * lexer,cp_token * token)418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
419 {
420 token++;
421 if (token == lexer->buffer_end)
422 token = lexer->buffer;
423 return token;
424 }
425
426 /* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
428
429 static inline cp_token *
cp_lexer_prev_token(cp_lexer * lexer,cp_token * token)430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
431 {
432 if (token == lexer->buffer)
433 token = lexer->buffer_end;
434 return token - 1;
435 }
436
437 /* nonzero if we are presently saving tokens. */
438
439 static int
cp_lexer_saving_tokens(const cp_lexer * lexer)440 cp_lexer_saving_tokens (const cp_lexer* lexer)
441 {
442 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443 }
444
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
446 buffer. */
447
448 static cp_token *
cp_lexer_advance_token(cp_lexer * lexer,cp_token * token,ptrdiff_t n)449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
450 {
451 token += n;
452 if (token >= lexer->buffer_end)
453 token = lexer->buffer + (token - lexer->buffer_end);
454 return token;
455 }
456
457 /* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
459
460 static ptrdiff_t
cp_lexer_token_difference(cp_lexer * lexer,cp_token * start,cp_token * finish)461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
462 {
463 if (finish >= start)
464 return finish - start;
465 else
466 return ((lexer->buffer_end - lexer->buffer)
467 - (start - finish));
468 }
469
470 /* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
472
473 static cp_token *
cp_lexer_read_token(cp_lexer * lexer)474 cp_lexer_read_token (cp_lexer* lexer)
475 {
476 cp_token *token;
477
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer);
480
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer->first_token)
483 lexer->first_token = lexer->last_token;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer->next_token)
486 lexer->next_token = lexer->last_token;
487
488 /* Figure out where we're going to store the new token. */
489 token = lexer->last_token;
490
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer, token);
493
494 /* Increment LAST_TOKEN. */
495 lexer->last_token = cp_lexer_next_token (lexer, token);
496
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
499 constant elements.
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502 && flag_const_strings)
503 {
504 tree type;
505
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
513 }
514
515 return token;
516 }
517
518 /* If the circular buffer is full, make it bigger. */
519
520 static void
cp_lexer_maybe_grow_buffer(cp_lexer * lexer)521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
522 {
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
525 {
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
531
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
534 value. */
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
539 new_buffer = ggc_realloc (lexer->buffer,
540 2 * buffer_length * sizeof (cp_token));
541
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
546 buffer. */
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
549 new_buffer,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555
556 /* Now recompute all of the buffer pointers. */
557 new_first_token
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
560 {
561 ptrdiff_t next_token_delta;
562
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
565 else
566 next_token_delta =
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
569 }
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
574 }
575 }
576
577 /* Store the next token from the preprocessor in *TOKEN. */
578
579 static void
cp_lexer_get_preprocessor_token(cp_lexer * lexer ATTRIBUTE_UNUSED,cp_token * token)580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581 cp_token *token)
582 {
583 bool done;
584
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (lexer != NULL && !lexer->main_lexer_p)
587 {
588 token->type = CPP_EOF;
589 token->location.line = 0;
590 token->location.file = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
593
594 return;
595 }
596
597 done = false;
598 /* Keep going until we get a token we like. */
599 while (!done)
600 {
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex_with_flags (&token->value, &token->flags);
603 /* Issue messages about tokens we cannot process. */
604 switch (token->type)
605 {
606 case CPP_ATSIGN:
607 case CPP_HASH:
608 case CPP_PASTE:
609 error ("invalid token");
610 break;
611
612 default:
613 /* This is a good token, so we exit the loop. */
614 done = true;
615 break;
616 }
617 }
618 /* Now we've got our token. */
619 token->location = input_location;
620
621 /* Check to see if this token is a keyword. */
622 if (token->type == CPP_NAME
623 && C_IS_RESERVED_WORD (token->value))
624 {
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
634 }
635 else
636 token->keyword = RID_MAX;
637 }
638
639 /* Return a pointer to the next token in the token stream, but do not
640 consume it. */
641
642 static cp_token *
cp_lexer_peek_token(cp_lexer * lexer)643 cp_lexer_peek_token (cp_lexer* lexer)
644 {
645 cp_token *token;
646
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
650
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
653 {
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
657 }
658
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
661 return token;
662 }
663
664 /* Return true if the next token has the indicated TYPE. */
665
666 static bool
cp_lexer_next_token_is(cp_lexer * lexer,enum cpp_ttype type)667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
668 {
669 cp_token *token;
670
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
675 }
676
677 /* Return true if the next token does not have the indicated TYPE. */
678
679 static bool
cp_lexer_next_token_is_not(cp_lexer * lexer,enum cpp_ttype type)680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
681 {
682 return !cp_lexer_next_token_is (lexer, type);
683 }
684
685 /* Return true if the next token is the indicated KEYWORD. */
686
687 static bool
cp_lexer_next_token_is_keyword(cp_lexer * lexer,enum rid keyword)688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
689 {
690 cp_token *token;
691
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
696 }
697
698 /* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
700
701 static cp_token *
cp_lexer_peek_nth_token(cp_lexer * lexer,size_t n)702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
703 {
704 cp_token *token;
705
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
708
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
712 if (!token)
713 {
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
716 }
717
718 /* Now, read tokens until we have enough. */
719 while (--n > 0)
720 {
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
726 }
727
728 return token;
729 }
730
731 /* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
734 time. */
735
736 static cp_token *
cp_lexer_consume_token(cp_lexer * lexer)737 cp_lexer_consume_token (cp_lexer* lexer)
738 {
739 cp_token *token;
740
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
744
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
747
748 /* Increment NEXT_TOKEN. */
749 lexer->next_token = cp_lexer_next_token (lexer,
750 lexer->next_token);
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
754
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
757 {
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
761 else
762 lexer->first_token = lexer->next_token;
763 }
764
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
767 {
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
771 }
772
773 return token;
774 }
775
776 /* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
779
780 static void
cp_lexer_purge_token(cp_lexer * lexer)781 cp_lexer_purge_token (cp_lexer *lexer)
782 {
783 cp_token *token;
784 cp_token *next_token;
785
786 token = lexer->next_token;
787 while (true)
788 {
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
791 break;
792 *token = *next_token;
793 token = next_token;
794 }
795
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
798 clear NEXT_TOKEN. */
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
801 }
802
803 /* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
806
807 static void
cp_lexer_purge_tokens_after(cp_lexer * lexer,cp_token * token)808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
809 {
810 cp_token *peek;
811 cp_token *t1;
812 cp_token *t2;
813
814 if (lexer->next_token)
815 {
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
822 {
823 *t1 = *t2;
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
826 }
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
831 }
832 else
833 {
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
837 }
838 }
839
840 /* Begin saving tokens. All tokens consumed after this point will be
841 preserved. */
842
843 static void
cp_lexer_save_tokens(cp_lexer * lexer)844 cp_lexer_save_tokens (cp_lexer* lexer)
845 {
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
854
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
857 lexer->first_token,
858 lexer->next_token));
859 }
860
861 /* Commit to the portion of the token stream most recently saved. */
862
863 static void
cp_lexer_commit_tokens(cp_lexer * lexer)864 cp_lexer_commit_tokens (cp_lexer* lexer)
865 {
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
869
870 VARRAY_POP (lexer->saved_tokens);
871 }
872
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
875
876 static void
cp_lexer_rollback_tokens(cp_lexer * lexer)877 cp_lexer_rollback_tokens (cp_lexer* lexer)
878 {
879 size_t delta;
880
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
884
885 /* Find the token that was the NEXT_TOKEN when we started saving
886 tokens. */
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
890 lexer->first_token,
891 delta);
892 /* It might be the case that there were no tokens when we started
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
896
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
899 }
900
901 /* Print a representation of the TOKEN on the STREAM. */
902
903 static void
cp_lexer_print_token(FILE * stream,cp_token * token)904 cp_lexer_print_token (FILE * stream, cp_token* token)
905 {
906 const char *token_type = NULL;
907
908 /* Figure out what kind of token this is. */
909 switch (token->type)
910 {
911 case CPP_EQ:
912 token_type = "EQ";
913 break;
914
915 case CPP_COMMA:
916 token_type = "COMMA";
917 break;
918
919 case CPP_OPEN_PAREN:
920 token_type = "OPEN_PAREN";
921 break;
922
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
925 break;
926
927 case CPP_OPEN_BRACE:
928 token_type = "OPEN_BRACE";
929 break;
930
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
933 break;
934
935 case CPP_SEMICOLON:
936 token_type = "SEMICOLON";
937 break;
938
939 case CPP_NAME:
940 token_type = "NAME";
941 break;
942
943 case CPP_EOF:
944 token_type = "EOF";
945 break;
946
947 case CPP_KEYWORD:
948 token_type = "keyword";
949 break;
950
951 /* This is not a token that we know how to handle yet. */
952 default:
953 break;
954 }
955
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
958 if (token_type)
959 fprintf (stream, "%s", token_type);
960 else
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
963 if (token->type == CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token->type == CPP_KEYWORD
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969 }
970
971 /* Start emitting debugging information. */
972
973 static void
cp_lexer_start_debugging(cp_lexer * lexer)974 cp_lexer_start_debugging (cp_lexer* lexer)
975 {
976 ++lexer->debugging_p;
977 }
978
979 /* Stop emitting debugging information. */
980
981 static void
cp_lexer_stop_debugging(cp_lexer * lexer)982 cp_lexer_stop_debugging (cp_lexer* lexer)
983 {
984 --lexer->debugging_p;
985 }
986
987
988 /* The parser. */
989
990 /* Overview
991 --------
992
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1000
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1003 code processed.
1004
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1013
1014 Methodology
1015 -----------
1016
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1033
1034 Future Improvements
1035 -------------------
1036
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1039
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1044
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1049
1050 /* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1052
1053 typedef enum cp_parser_flags
1054 {
1055 /* No flags. */
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062 } cp_parser_flags;
1063
1064 /* The different kinds of declarators we want to parse. */
1065
1066 typedef enum cp_parser_declarator_kind
1067 {
1068 /* We want an abstract declartor. */
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
1072 /* We don't mind, but the name must be an unqualified-id. */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1075
1076 /* A mapping from a token type to a corresponding tree node type. */
1077
1078 typedef struct cp_parser_token_tree_map_node
1079 {
1080 /* The token type. */
1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082 /* The corresponding tree code. */
1083 ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1085
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1088
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1090
1091 /* The status of a tentative parse. */
1092
1093 typedef enum cp_parser_status_kind
1094 {
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1100 has occurred. */
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1103
1104 /* Context that is saved and restored when parsing tentatively. */
1105
1106 typedef struct cp_parser_context GTY (())
1107 {
1108 /* If this is a tentative parsing context, the status of the
1109 tentative parse. */
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1115 tree object_type;
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118 } cp_parser_context;
1119
1120 /* Prototypes. */
1121
1122 /* Constructors and destructors. */
1123
1124 static cp_parser_context *cp_parser_context_new
1125 (cp_parser_context *);
1126
1127 /* Class variables. */
1128
1129 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1130
1131 /* Constructors and destructors. */
1132
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1135
1136 static cp_parser_context *
cp_parser_context_new(cp_parser_context * next)1137 cp_parser_context_new (cp_parser_context* next)
1138 {
1139 cp_parser_context *context;
1140
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list != NULL)
1143 {
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
1147 memset (context, 0, sizeof (*context));
1148 }
1149 else
1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1155 if (next)
1156 {
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
1160 /* Thread the stack. */
1161 context->next = next;
1162 }
1163
1164 return context;
1165 }
1166
1167 /* The cp_parser structure represents the C++ parser. */
1168
1169 typedef struct cp_parser GTY(())
1170 {
1171 /* The lexer from which we are obtaining tokens. */
1172 cp_lexer *lexer;
1173
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1178
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1186 tree scope;
1187
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1193 tree object_scope;
1194 tree qualifying_scope;
1195
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1198
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1206
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1210
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1215
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
1218 them permissible. */
1219 bool default_arg_ok_p;
1220
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
1223 bool integral_constant_expression_p;
1224
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
1229 bool allow_non_integral_constant_expression_p;
1230
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
1233 bool non_integral_constant_expression_p;
1234
1235 /* TRUE if we are parsing the argument to "__offsetof__". */
1236 bool in_offsetof_p;
1237
1238 /* TRUE if local variable names and `this' are forbidden in the
1239 current context. */
1240 bool local_variables_forbidden_p;
1241
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1244 declaration'. */
1245 bool in_unbraced_linkage_specification_p;
1246
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1250
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1253
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1257
1258 /* TRUE if we are presently parsing the body of a switch
1259 statement. */
1260 bool in_switch_statement_p;
1261
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1264 alternatives. */
1265 bool in_type_id_in_expr_p;
1266
1267 /* If non-NULL, then we are parsing a construct where new type
1268 definitions are not permitted. The string stored here will be
1269 issued as an error message if a type is defined. */
1270 const char *type_definition_forbidden_message;
1271
1272 /* A list of lists. The outer list is a stack, used for member
1273 functions of local classes. At each level there are two sub-list,
1274 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276 TREE_VALUE's. The functions are chained in reverse declaration
1277 order.
1278
1279 The TREE_PURPOSE sublist contains those functions with default
1280 arguments that need post processing, and the TREE_VALUE sublist
1281 contains those functions with definitions that need post
1282 processing.
1283
1284 These lists can only be processed once the outermost class being
1285 defined is complete. */
1286 tree unparsed_functions_queues;
1287
1288 /* The number of classes whose definitions are currently in
1289 progress. */
1290 unsigned num_classes_being_defined;
1291
1292 /* The number of template parameter lists that apply directly to the
1293 current declaration. */
1294 unsigned num_template_parameter_lists;
1295 } cp_parser;
1296
1297 /* The type of a function that parses some kind of expression. */
1298 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1299
1300 /* Prototypes. */
1301
1302 /* Constructors and destructors. */
1303
1304 static cp_parser *cp_parser_new
1305 (void);
1306
1307 /* Routines to parse various constructs.
1308
1309 Those that return `tree' will return the error_mark_node (rather
1310 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311 Sometimes, they will return an ordinary node if error-recovery was
1312 attempted, even though a parse error occurred. So, to check
1313 whether or not a parse error occurred, you should always use
1314 cp_parser_error_occurred. If the construct is optional (indicated
1315 either by an `_opt' in the name of the function that does the
1316 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317 the construct is not present. */
1318
1319 /* Lexical conventions [gram.lex] */
1320
1321 static tree cp_parser_identifier
1322 (cp_parser *);
1323
1324 /* Basic concepts [gram.basic] */
1325
1326 static bool cp_parser_translation_unit
1327 (cp_parser *);
1328
1329 /* Expressions [gram.expr] */
1330
1331 static tree cp_parser_primary_expression
1332 (cp_parser *, cp_id_kind *, tree *);
1333 static tree cp_parser_id_expression
1334 (cp_parser *, bool, bool, bool *, bool);
1335 static tree cp_parser_unqualified_id
1336 (cp_parser *, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier_opt
1338 (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_nested_name_specifier
1340 (cp_parser *, bool, bool, bool, bool);
1341 static tree cp_parser_class_or_namespace_name
1342 (cp_parser *, bool, bool, bool, bool, bool);
1343 static tree cp_parser_postfix_expression
1344 (cp_parser *, bool);
1345 static tree cp_parser_parenthesized_expression_list
1346 (cp_parser *, bool, bool *);
1347 static void cp_parser_pseudo_destructor_name
1348 (cp_parser *, tree *, tree *);
1349 static tree cp_parser_unary_expression
1350 (cp_parser *, bool);
1351 static enum tree_code cp_parser_unary_operator
1352 (cp_token *);
1353 static tree cp_parser_new_expression
1354 (cp_parser *);
1355 static tree cp_parser_new_placement
1356 (cp_parser *);
1357 static tree cp_parser_new_type_id
1358 (cp_parser *);
1359 static tree cp_parser_new_declarator_opt
1360 (cp_parser *);
1361 static tree cp_parser_direct_new_declarator
1362 (cp_parser *);
1363 static tree cp_parser_new_initializer
1364 (cp_parser *);
1365 static tree cp_parser_delete_expression
1366 (cp_parser *);
1367 static tree cp_parser_cast_expression
1368 (cp_parser *, bool);
1369 static tree cp_parser_pm_expression
1370 (cp_parser *);
1371 static tree cp_parser_multiplicative_expression
1372 (cp_parser *);
1373 static tree cp_parser_additive_expression
1374 (cp_parser *);
1375 static tree cp_parser_shift_expression
1376 (cp_parser *);
1377 static tree cp_parser_relational_expression
1378 (cp_parser *);
1379 static tree cp_parser_equality_expression
1380 (cp_parser *);
1381 static tree cp_parser_and_expression
1382 (cp_parser *);
1383 static tree cp_parser_exclusive_or_expression
1384 (cp_parser *);
1385 static tree cp_parser_inclusive_or_expression
1386 (cp_parser *);
1387 static tree cp_parser_logical_and_expression
1388 (cp_parser *);
1389 static tree cp_parser_logical_or_expression
1390 (cp_parser *);
1391 static tree cp_parser_question_colon_clause
1392 (cp_parser *, tree);
1393 static tree cp_parser_assignment_expression
1394 (cp_parser *);
1395 static enum tree_code cp_parser_assignment_operator_opt
1396 (cp_parser *);
1397 static tree cp_parser_expression
1398 (cp_parser *);
1399 static tree cp_parser_constant_expression
1400 (cp_parser *, bool, bool *);
1401
1402 /* Statements [gram.stmt.stmt] */
1403
1404 static void cp_parser_statement
1405 (cp_parser *, bool);
1406 static tree cp_parser_labeled_statement
1407 (cp_parser *, bool);
1408 static tree cp_parser_expression_statement
1409 (cp_parser *, bool);
1410 static tree cp_parser_compound_statement
1411 (cp_parser *, bool);
1412 static void cp_parser_statement_seq_opt
1413 (cp_parser *, bool);
1414 static tree cp_parser_selection_statement
1415 (cp_parser *);
1416 static tree cp_parser_condition
1417 (cp_parser *);
1418 static tree cp_parser_iteration_statement
1419 (cp_parser *);
1420 static void cp_parser_for_init_statement
1421 (cp_parser *);
1422 static tree cp_parser_jump_statement
1423 (cp_parser *);
1424 static void cp_parser_declaration_statement
1425 (cp_parser *);
1426
1427 static tree cp_parser_implicitly_scoped_statement
1428 (cp_parser *);
1429 static void cp_parser_already_scoped_statement
1430 (cp_parser *);
1431
1432 /* Declarations [gram.dcl.dcl] */
1433
1434 static void cp_parser_declaration_seq_opt
1435 (cp_parser *);
1436 static void cp_parser_declaration
1437 (cp_parser *);
1438 static void cp_parser_block_declaration
1439 (cp_parser *, bool);
1440 static void cp_parser_simple_declaration
1441 (cp_parser *, bool);
1442 static tree cp_parser_decl_specifier_seq
1443 (cp_parser *, cp_parser_flags, tree *, int *);
1444 static tree cp_parser_storage_class_specifier_opt
1445 (cp_parser *);
1446 static tree cp_parser_function_specifier_opt
1447 (cp_parser *);
1448 static tree cp_parser_type_specifier
1449 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450 static tree cp_parser_simple_type_specifier
1451 (cp_parser *, cp_parser_flags, bool);
1452 static tree cp_parser_type_name
1453 (cp_parser *);
1454 static tree cp_parser_elaborated_type_specifier
1455 (cp_parser *, bool, bool);
1456 static tree cp_parser_enum_specifier
1457 (cp_parser *);
1458 static void cp_parser_enumerator_list
1459 (cp_parser *, tree);
1460 static void cp_parser_enumerator_definition
1461 (cp_parser *, tree);
1462 static tree cp_parser_namespace_name
1463 (cp_parser *);
1464 static void cp_parser_namespace_definition
1465 (cp_parser *);
1466 static void cp_parser_namespace_body
1467 (cp_parser *);
1468 static tree cp_parser_qualified_namespace_specifier
1469 (cp_parser *);
1470 static void cp_parser_namespace_alias_definition
1471 (cp_parser *);
1472 static void cp_parser_using_declaration
1473 (cp_parser *);
1474 static void cp_parser_using_directive
1475 (cp_parser *);
1476 static void cp_parser_asm_definition
1477 (cp_parser *);
1478 static void cp_parser_linkage_specification
1479 (cp_parser *);
1480
1481 /* Declarators [gram.dcl.decl] */
1482
1483 static tree cp_parser_init_declarator
1484 (cp_parser *, tree, tree, bool, bool, int, bool *);
1485 static tree cp_parser_declarator
1486 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1487 static tree cp_parser_direct_declarator
1488 (cp_parser *, cp_parser_declarator_kind, int *);
1489 static enum tree_code cp_parser_ptr_operator
1490 (cp_parser *, tree *, tree *);
1491 static tree cp_parser_cv_qualifier_seq_opt
1492 (cp_parser *);
1493 static tree cp_parser_cv_qualifier_opt
1494 (cp_parser *);
1495 static tree cp_parser_declarator_id
1496 (cp_parser *);
1497 static tree cp_parser_type_id
1498 (cp_parser *);
1499 static tree cp_parser_type_specifier_seq
1500 (cp_parser *);
1501 static tree cp_parser_parameter_declaration_clause
1502 (cp_parser *);
1503 static tree cp_parser_parameter_declaration_list
1504 (cp_parser *);
1505 static tree cp_parser_parameter_declaration
1506 (cp_parser *, bool, bool *);
1507 static void cp_parser_function_body
1508 (cp_parser *);
1509 static tree cp_parser_initializer
1510 (cp_parser *, bool *, bool *);
1511 static tree cp_parser_initializer_clause
1512 (cp_parser *, bool *);
1513 static tree cp_parser_initializer_list
1514 (cp_parser *, bool *);
1515
1516 static bool cp_parser_ctor_initializer_opt_and_function_body
1517 (cp_parser *);
1518
1519 /* Classes [gram.class] */
1520
1521 static tree cp_parser_class_name
1522 (cp_parser *, bool, bool, bool, bool, bool, bool);
1523 static tree cp_parser_class_specifier
1524 (cp_parser *);
1525 static tree cp_parser_class_head
1526 (cp_parser *, bool *, tree *);
1527 static enum tag_types cp_parser_class_key
1528 (cp_parser *);
1529 static void cp_parser_member_specification_opt
1530 (cp_parser *);
1531 static void cp_parser_member_declaration
1532 (cp_parser *);
1533 static tree cp_parser_pure_specifier
1534 (cp_parser *);
1535 static tree cp_parser_constant_initializer
1536 (cp_parser *);
1537
1538 /* Derived classes [gram.class.derived] */
1539
1540 static tree cp_parser_base_clause
1541 (cp_parser *);
1542 static tree cp_parser_base_specifier
1543 (cp_parser *);
1544
1545 /* Special member functions [gram.special] */
1546
1547 static tree cp_parser_conversion_function_id
1548 (cp_parser *);
1549 static tree cp_parser_conversion_type_id
1550 (cp_parser *);
1551 static tree cp_parser_conversion_declarator_opt
1552 (cp_parser *);
1553 static bool cp_parser_ctor_initializer_opt
1554 (cp_parser *);
1555 static void cp_parser_mem_initializer_list
1556 (cp_parser *);
1557 static tree cp_parser_mem_initializer
1558 (cp_parser *);
1559 static tree cp_parser_mem_initializer_id
1560 (cp_parser *);
1561
1562 /* Overloading [gram.over] */
1563
1564 static tree cp_parser_operator_function_id
1565 (cp_parser *);
1566 static tree cp_parser_operator
1567 (cp_parser *);
1568
1569 /* Templates [gram.temp] */
1570
1571 static void cp_parser_template_declaration
1572 (cp_parser *, bool);
1573 static tree cp_parser_template_parameter_list
1574 (cp_parser *);
1575 static tree cp_parser_template_parameter
1576 (cp_parser *);
1577 static tree cp_parser_type_parameter
1578 (cp_parser *);
1579 static tree cp_parser_template_id
1580 (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_template_name
1582 (cp_parser *, bool, bool, bool, bool *);
1583 static tree cp_parser_template_argument_list
1584 (cp_parser *);
1585 static tree cp_parser_template_argument
1586 (cp_parser *);
1587 static void cp_parser_explicit_instantiation
1588 (cp_parser *);
1589 static void cp_parser_explicit_specialization
1590 (cp_parser *);
1591
1592 /* Exception handling [gram.exception] */
1593
1594 static tree cp_parser_try_block
1595 (cp_parser *);
1596 static bool cp_parser_function_try_block
1597 (cp_parser *);
1598 static void cp_parser_handler_seq
1599 (cp_parser *);
1600 static void cp_parser_handler
1601 (cp_parser *);
1602 static tree cp_parser_exception_declaration
1603 (cp_parser *);
1604 static tree cp_parser_throw_expression
1605 (cp_parser *);
1606 static tree cp_parser_exception_specification_opt
1607 (cp_parser *);
1608 static tree cp_parser_type_id_list
1609 (cp_parser *);
1610
1611 /* GNU Extensions */
1612
1613 static tree cp_parser_asm_specification_opt
1614 (cp_parser *);
1615 static tree cp_parser_asm_operand_list
1616 (cp_parser *);
1617 static tree cp_parser_asm_clobber_list
1618 (cp_parser *);
1619 static tree cp_parser_attributes_opt
1620 (cp_parser *);
1621 static tree cp_parser_attribute_list
1622 (cp_parser *);
1623 static bool cp_parser_extension_opt
1624 (cp_parser *, int *);
1625 static void cp_parser_label_declaration
1626 (cp_parser *);
1627
1628 /* Utility Routines */
1629
1630 static tree cp_parser_lookup_name
1631 (cp_parser *, tree, bool, bool, bool, bool);
1632 static tree cp_parser_lookup_name_simple
1633 (cp_parser *, tree);
1634 static tree cp_parser_maybe_treat_template_as_class
1635 (tree, bool);
1636 static bool cp_parser_check_declarator_template_parameters
1637 (cp_parser *, tree);
1638 static bool cp_parser_check_template_parameters
1639 (cp_parser *, unsigned);
1640 static tree cp_parser_simple_cast_expression
1641 (cp_parser *);
1642 static tree cp_parser_binary_expression
1643 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644 static tree cp_parser_global_scope_opt
1645 (cp_parser *, bool);
1646 static bool cp_parser_constructor_declarator_p
1647 (cp_parser *, bool);
1648 static tree cp_parser_function_definition_from_specifiers_and_declarator
1649 (cp_parser *, tree, tree, tree);
1650 static tree cp_parser_function_definition_after_declarator
1651 (cp_parser *, bool);
1652 static void cp_parser_template_declaration_after_export
1653 (cp_parser *, bool);
1654 static tree cp_parser_single_declaration
1655 (cp_parser *, bool, bool *);
1656 static tree cp_parser_functional_cast
1657 (cp_parser *, tree);
1658 static tree cp_parser_save_member_function_body
1659 (cp_parser *, tree, tree, tree);
1660 static tree cp_parser_enclosed_template_argument_list
1661 (cp_parser *);
1662 static void cp_parser_save_default_args
1663 (cp_parser *, tree);
1664 static void cp_parser_late_parsing_for_member
1665 (cp_parser *, tree);
1666 static void cp_parser_late_parsing_default_args
1667 (cp_parser *, tree);
1668 static tree cp_parser_sizeof_operand
1669 (cp_parser *, enum rid);
1670 static bool cp_parser_declares_only_class_p
1671 (cp_parser *);
1672 static bool cp_parser_friend_p
1673 (tree);
1674 static cp_token *cp_parser_require
1675 (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677 (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1679 (cp_token *);
1680 static bool cp_parser_next_token_starts_class_definition_p
1681 (cp_parser *);
1682 static bool cp_parser_next_token_ends_template_argument_p
1683 (cp_parser *);
1684 static bool cp_parser_nth_token_starts_template_argument_list_p
1685 (cp_parser *, size_t);
1686 static enum tag_types cp_parser_token_is_class_key
1687 (cp_token *);
1688 static void cp_parser_check_class_key
1689 (enum tag_types, tree type);
1690 static void cp_parser_check_access_in_redeclaration
1691 (tree type);
1692 static bool cp_parser_optional_template_keyword
1693 (cp_parser *);
1694 static void cp_parser_pre_parsed_nested_name_specifier
1695 (cp_parser *);
1696 static void cp_parser_cache_group
1697 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698 static void cp_parser_parse_tentatively
1699 (cp_parser *);
1700 static void cp_parser_commit_to_tentative_parse
1701 (cp_parser *);
1702 static void cp_parser_abort_tentative_parse
1703 (cp_parser *);
1704 static bool cp_parser_parse_definitely
1705 (cp_parser *);
1706 static inline bool cp_parser_parsing_tentatively
1707 (cp_parser *);
1708 static bool cp_parser_committed_to_tentative_parse
1709 (cp_parser *);
1710 static void cp_parser_error
1711 (cp_parser *, const char *);
1712 static void cp_parser_name_lookup_error
1713 (cp_parser *, tree, tree, const char *);
1714 static bool cp_parser_simulate_error
1715 (cp_parser *);
1716 static void cp_parser_check_type_definition
1717 (cp_parser *);
1718 static void cp_parser_check_for_definition_in_return_type
1719 (tree, int);
1720 static void cp_parser_check_for_invalid_template_id
1721 (cp_parser *, tree);
1722 static bool cp_parser_non_integral_constant_expression
1723 (cp_parser *, const char *);
1724 static bool cp_parser_diagnose_invalid_type_name
1725 (cp_parser *);
1726 static int cp_parser_skip_to_closing_parenthesis
1727 (cp_parser *, bool, bool, bool);
1728 static void cp_parser_skip_to_end_of_statement
1729 (cp_parser *);
1730 static void cp_parser_consume_semicolon_at_end_of_statement
1731 (cp_parser *);
1732 static void cp_parser_skip_to_end_of_block_or_statement
1733 (cp_parser *);
1734 static void cp_parser_skip_to_closing_brace
1735 (cp_parser *);
1736 static void cp_parser_skip_until_found
1737 (cp_parser *, enum cpp_ttype, const char *);
1738 static bool cp_parser_error_occurred
1739 (cp_parser *);
1740 static bool cp_parser_allow_gnu_extensions_p
1741 (cp_parser *);
1742 static bool cp_parser_is_string_literal
1743 (cp_token *);
1744 static bool cp_parser_is_keyword
1745 (cp_token *, enum rid);
1746
1747 /* Returns nonzero if we are parsing tentatively. */
1748
1749 static inline bool
cp_parser_parsing_tentatively(cp_parser * parser)1750 cp_parser_parsing_tentatively (cp_parser* parser)
1751 {
1752 return parser->context->next != NULL;
1753 }
1754
1755 /* Returns nonzero if TOKEN is a string literal. */
1756
1757 static bool
cp_parser_is_string_literal(cp_token * token)1758 cp_parser_is_string_literal (cp_token* token)
1759 {
1760 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1761 }
1762
1763 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1764
1765 static bool
cp_parser_is_keyword(cp_token * token,enum rid keyword)1766 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1767 {
1768 return token->keyword == keyword;
1769 }
1770
1771 /* Issue the indicated error MESSAGE. */
1772
1773 static void
cp_parser_error(cp_parser * parser,const char * message)1774 cp_parser_error (cp_parser* parser, const char* message)
1775 {
1776 /* Output the MESSAGE -- unless we're parsing tentatively. */
1777 if (!cp_parser_simulate_error (parser))
1778 {
1779 cp_token *token;
1780 token = cp_lexer_peek_token (parser->lexer);
1781 c_parse_error (message,
1782 /* Because c_parser_error does not understand
1783 CPP_KEYWORD, keywords are treated like
1784 identifiers. */
1785 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1786 token->value);
1787 }
1788 }
1789
1790 /* Issue an error about name-lookup failing. NAME is the
1791 IDENTIFIER_NODE DECL is the result of
1792 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1793 the thing that we hoped to find. */
1794
1795 static void
cp_parser_name_lookup_error(cp_parser * parser,tree name,tree decl,const char * desired)1796 cp_parser_name_lookup_error (cp_parser* parser,
1797 tree name,
1798 tree decl,
1799 const char* desired)
1800 {
1801 /* If name lookup completely failed, tell the user that NAME was not
1802 declared. */
1803 if (decl == error_mark_node)
1804 {
1805 if (parser->scope && parser->scope != global_namespace)
1806 error ("`%D::%D' has not been declared",
1807 parser->scope, name);
1808 else if (parser->scope == global_namespace)
1809 error ("`::%D' has not been declared", name);
1810 else
1811 error ("`%D' has not been declared", name);
1812 }
1813 else if (parser->scope && parser->scope != global_namespace)
1814 error ("`%D::%D' %s", parser->scope, name, desired);
1815 else if (parser->scope == global_namespace)
1816 error ("`::%D' %s", name, desired);
1817 else
1818 error ("`%D' %s", name, desired);
1819 }
1820
1821 /* If we are parsing tentatively, remember that an error has occurred
1822 during this tentative parse. Returns true if the error was
1823 simulated; false if a messgae should be issued by the caller. */
1824
1825 static bool
cp_parser_simulate_error(cp_parser * parser)1826 cp_parser_simulate_error (cp_parser* parser)
1827 {
1828 if (cp_parser_parsing_tentatively (parser)
1829 && !cp_parser_committed_to_tentative_parse (parser))
1830 {
1831 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1832 return true;
1833 }
1834 return false;
1835 }
1836
1837 /* This function is called when a type is defined. If type
1838 definitions are forbidden at this point, an error message is
1839 issued. */
1840
1841 static void
cp_parser_check_type_definition(cp_parser * parser)1842 cp_parser_check_type_definition (cp_parser* parser)
1843 {
1844 /* If types are forbidden here, issue a message. */
1845 if (parser->type_definition_forbidden_message)
1846 /* Use `%s' to print the string in case there are any escape
1847 characters in the message. */
1848 error ("%s", parser->type_definition_forbidden_message);
1849 }
1850
1851 /* This function is called when a declaration is parsed. If
1852 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1853 indicates that a type was defined in the decl-specifiers for DECL,
1854 then an error is issued. */
1855
1856 static void
cp_parser_check_for_definition_in_return_type(tree declarator,int declares_class_or_enum)1857 cp_parser_check_for_definition_in_return_type (tree declarator,
1858 int declares_class_or_enum)
1859 {
1860 /* [dcl.fct] forbids type definitions in return types.
1861 Unfortunately, it's not easy to know whether or not we are
1862 processing a return type until after the fact. */
1863 while (declarator
1864 && (TREE_CODE (declarator) == INDIRECT_REF
1865 || TREE_CODE (declarator) == ADDR_EXPR))
1866 declarator = TREE_OPERAND (declarator, 0);
1867 if (declarator
1868 && TREE_CODE (declarator) == CALL_EXPR
1869 && declares_class_or_enum & 2)
1870 error ("new types may not be defined in a return type");
1871 }
1872
1873 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1874 "<" in any valid C++ program. If the next token is indeed "<",
1875 issue a message warning the user about what appears to be an
1876 invalid attempt to form a template-id. */
1877
1878 static void
cp_parser_check_for_invalid_template_id(cp_parser * parser,tree type)1879 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1880 tree type)
1881 {
1882 ptrdiff_t start;
1883 cp_token *token;
1884
1885 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1886 {
1887 if (TYPE_P (type))
1888 error ("`%T' is not a template", type);
1889 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1890 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1891 else
1892 error ("invalid template-id");
1893 /* Remember the location of the invalid "<". */
1894 if (cp_parser_parsing_tentatively (parser)
1895 && !cp_parser_committed_to_tentative_parse (parser))
1896 {
1897 token = cp_lexer_peek_token (parser->lexer);
1898 token = cp_lexer_prev_token (parser->lexer, token);
1899 start = cp_lexer_token_difference (parser->lexer,
1900 parser->lexer->first_token,
1901 token);
1902 }
1903 else
1904 start = -1;
1905 /* Consume the "<". */
1906 cp_lexer_consume_token (parser->lexer);
1907 /* Parse the template arguments. */
1908 cp_parser_enclosed_template_argument_list (parser);
1909 /* Permanently remove the invalid template arguments so that
1910 this error message is not issued again. */
1911 if (start >= 0)
1912 {
1913 token = cp_lexer_advance_token (parser->lexer,
1914 parser->lexer->first_token,
1915 start);
1916 cp_lexer_purge_tokens_after (parser->lexer, token);
1917 }
1918 }
1919 }
1920
1921 /* If parsing an integral constant-expression, issue an error message
1922 about the fact that THING appeared and return true. Otherwise,
1923 return false, marking the current expression as non-constant. */
1924
1925 static bool
cp_parser_non_integral_constant_expression(cp_parser * parser,const char * thing)1926 cp_parser_non_integral_constant_expression (cp_parser *parser,
1927 const char *thing)
1928 {
1929 if (parser->integral_constant_expression_p)
1930 {
1931 if (!parser->allow_non_integral_constant_expression_p)
1932 {
1933 error ("%s cannot appear in a constant-expression", thing);
1934 return true;
1935 }
1936 parser->non_integral_constant_expression_p = true;
1937 }
1938 return false;
1939 }
1940
1941 /* Check for a common situation where a type-name should be present,
1942 but is not, and issue a sensible error message. Returns true if an
1943 invalid type-name was detected. */
1944
1945 static bool
cp_parser_diagnose_invalid_type_name(cp_parser * parser)1946 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1947 {
1948 /* If the next two tokens are both identifiers, the code is
1949 erroneous. The usual cause of this situation is code like:
1950
1951 T t;
1952
1953 where "T" should name a type -- but does not. */
1954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1955 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1956 {
1957 tree name;
1958
1959 /* If parsing tentatively, we should commit; we really are
1960 looking at a declaration. */
1961 /* Consume the first identifier. */
1962 name = cp_lexer_consume_token (parser->lexer)->value;
1963 /* Issue an error message. */
1964 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1965 /* If we're in a template class, it's possible that the user was
1966 referring to a type from a base class. For example:
1967
1968 template <typename T> struct A { typedef T X; };
1969 template <typename T> struct B : public A<T> { X x; };
1970
1971 The user should have said "typename A<T>::X". */
1972 if (processing_template_decl && current_class_type)
1973 {
1974 tree b;
1975
1976 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1977 b;
1978 b = TREE_CHAIN (b))
1979 {
1980 tree base_type = BINFO_TYPE (b);
1981 if (CLASS_TYPE_P (base_type)
1982 && dependent_type_p (base_type))
1983 {
1984 tree field;
1985 /* Go from a particular instantiation of the
1986 template (which will have an empty TYPE_FIELDs),
1987 to the main version. */
1988 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1989 for (field = TYPE_FIELDS (base_type);
1990 field;
1991 field = TREE_CHAIN (field))
1992 if (TREE_CODE (field) == TYPE_DECL
1993 && DECL_NAME (field) == name)
1994 {
1995 error ("(perhaps `typename %T::%s' was intended)",
1996 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1997 break;
1998 }
1999 if (field)
2000 break;
2001 }
2002 }
2003 }
2004 /* Skip to the end of the declaration; there's no point in
2005 trying to process it. */
2006 cp_parser_skip_to_end_of_statement (parser);
2007
2008 return true;
2009 }
2010
2011 return false;
2012 }
2013
2014 /* Consume tokens up to, and including, the next non-nested closing `)'.
2015 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2016 are doing error recovery. Returns -1 if OR_COMMA is true and we
2017 found an unnested comma. */
2018
2019 static int
cp_parser_skip_to_closing_parenthesis(cp_parser * parser,bool recovering,bool or_comma,bool consume_paren)2020 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2021 bool recovering,
2022 bool or_comma,
2023 bool consume_paren)
2024 {
2025 unsigned paren_depth = 0;
2026 unsigned brace_depth = 0;
2027
2028 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2029 && !cp_parser_committed_to_tentative_parse (parser))
2030 return 0;
2031
2032 while (true)
2033 {
2034 cp_token *token;
2035
2036 /* If we've run out of tokens, then there is no closing `)'. */
2037 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2038 return 0;
2039
2040 token = cp_lexer_peek_token (parser->lexer);
2041
2042 /* This matches the processing in skip_to_end_of_statement. */
2043 if (token->type == CPP_SEMICOLON && !brace_depth)
2044 return 0;
2045 if (token->type == CPP_OPEN_BRACE)
2046 ++brace_depth;
2047 if (token->type == CPP_CLOSE_BRACE)
2048 {
2049 if (!brace_depth--)
2050 return 0;
2051 }
2052 if (recovering && or_comma && token->type == CPP_COMMA
2053 && !brace_depth && !paren_depth)
2054 return -1;
2055
2056 if (!brace_depth)
2057 {
2058 /* If it is an `(', we have entered another level of nesting. */
2059 if (token->type == CPP_OPEN_PAREN)
2060 ++paren_depth;
2061 /* If it is a `)', then we might be done. */
2062 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2063 {
2064 if (consume_paren)
2065 cp_lexer_consume_token (parser->lexer);
2066 return 1;
2067 }
2068 }
2069
2070 /* Consume the token. */
2071 cp_lexer_consume_token (parser->lexer);
2072 }
2073 }
2074
2075 /* Consume tokens until we reach the end of the current statement.
2076 Normally, that will be just before consuming a `;'. However, if a
2077 non-nested `}' comes first, then we stop before consuming that. */
2078
2079 static void
cp_parser_skip_to_end_of_statement(cp_parser * parser)2080 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2081 {
2082 unsigned nesting_depth = 0;
2083
2084 while (true)
2085 {
2086 cp_token *token;
2087
2088 /* Peek at the next token. */
2089 token = cp_lexer_peek_token (parser->lexer);
2090 /* If we've run out of tokens, stop. */
2091 if (token->type == CPP_EOF)
2092 break;
2093 /* If the next token is a `;', we have reached the end of the
2094 statement. */
2095 if (token->type == CPP_SEMICOLON && !nesting_depth)
2096 break;
2097 /* If the next token is a non-nested `}', then we have reached
2098 the end of the current block. */
2099 if (token->type == CPP_CLOSE_BRACE)
2100 {
2101 /* If this is a non-nested `}', stop before consuming it.
2102 That way, when confronted with something like:
2103
2104 { 3 + }
2105
2106 we stop before consuming the closing `}', even though we
2107 have not yet reached a `;'. */
2108 if (nesting_depth == 0)
2109 break;
2110 /* If it is the closing `}' for a block that we have
2111 scanned, stop -- but only after consuming the token.
2112 That way given:
2113
2114 void f g () { ... }
2115 typedef int I;
2116
2117 we will stop after the body of the erroneously declared
2118 function, but before consuming the following `typedef'
2119 declaration. */
2120 if (--nesting_depth == 0)
2121 {
2122 cp_lexer_consume_token (parser->lexer);
2123 break;
2124 }
2125 }
2126 /* If it the next token is a `{', then we are entering a new
2127 block. Consume the entire block. */
2128 else if (token->type == CPP_OPEN_BRACE)
2129 ++nesting_depth;
2130 /* Consume the token. */
2131 cp_lexer_consume_token (parser->lexer);
2132 }
2133 }
2134
2135 /* This function is called at the end of a statement or declaration.
2136 If the next token is a semicolon, it is consumed; otherwise, error
2137 recovery is attempted. */
2138
2139 static void
cp_parser_consume_semicolon_at_end_of_statement(cp_parser * parser)2140 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2141 {
2142 /* Look for the trailing `;'. */
2143 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2144 {
2145 /* If there is additional (erroneous) input, skip to the end of
2146 the statement. */
2147 cp_parser_skip_to_end_of_statement (parser);
2148 /* If the next token is now a `;', consume it. */
2149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2150 cp_lexer_consume_token (parser->lexer);
2151 }
2152 }
2153
2154 /* Skip tokens until we have consumed an entire block, or until we
2155 have consumed a non-nested `;'. */
2156
2157 static void
cp_parser_skip_to_end_of_block_or_statement(cp_parser * parser)2158 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2159 {
2160 unsigned nesting_depth = 0;
2161
2162 while (true)
2163 {
2164 cp_token *token;
2165
2166 /* Peek at the next token. */
2167 token = cp_lexer_peek_token (parser->lexer);
2168 /* If we've run out of tokens, stop. */
2169 if (token->type == CPP_EOF)
2170 break;
2171 /* If the next token is a `;', we have reached the end of the
2172 statement. */
2173 if (token->type == CPP_SEMICOLON && !nesting_depth)
2174 {
2175 /* Consume the `;'. */
2176 cp_lexer_consume_token (parser->lexer);
2177 break;
2178 }
2179 /* Consume the token. */
2180 token = cp_lexer_consume_token (parser->lexer);
2181 /* If the next token is a non-nested `}', then we have reached
2182 the end of the current block. */
2183 if (token->type == CPP_CLOSE_BRACE
2184 && (nesting_depth == 0 || --nesting_depth == 0))
2185 break;
2186 /* If it the next token is a `{', then we are entering a new
2187 block. Consume the entire block. */
2188 if (token->type == CPP_OPEN_BRACE)
2189 ++nesting_depth;
2190 }
2191 }
2192
2193 /* Skip tokens until a non-nested closing curly brace is the next
2194 token. */
2195
2196 static void
cp_parser_skip_to_closing_brace(cp_parser * parser)2197 cp_parser_skip_to_closing_brace (cp_parser *parser)
2198 {
2199 unsigned nesting_depth = 0;
2200
2201 while (true)
2202 {
2203 cp_token *token;
2204
2205 /* Peek at the next token. */
2206 token = cp_lexer_peek_token (parser->lexer);
2207 /* If we've run out of tokens, stop. */
2208 if (token->type == CPP_EOF)
2209 break;
2210 /* If the next token is a non-nested `}', then we have reached
2211 the end of the current block. */
2212 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2213 break;
2214 /* If it the next token is a `{', then we are entering a new
2215 block. Consume the entire block. */
2216 else if (token->type == CPP_OPEN_BRACE)
2217 ++nesting_depth;
2218 /* Consume the token. */
2219 cp_lexer_consume_token (parser->lexer);
2220 }
2221 }
2222
2223 /* Create a new C++ parser. */
2224
2225 static cp_parser *
cp_parser_new(void)2226 cp_parser_new (void)
2227 {
2228 cp_parser *parser;
2229 cp_lexer *lexer;
2230
2231 /* cp_lexer_new_main is called before calling ggc_alloc because
2232 cp_lexer_new_main might load a PCH file. */
2233 lexer = cp_lexer_new_main ();
2234
2235 parser = ggc_alloc_cleared (sizeof (cp_parser));
2236 parser->lexer = lexer;
2237 parser->context = cp_parser_context_new (NULL);
2238
2239 /* For now, we always accept GNU extensions. */
2240 parser->allow_gnu_extensions_p = 1;
2241
2242 /* The `>' token is a greater-than operator, not the end of a
2243 template-id. */
2244 parser->greater_than_is_operator_p = true;
2245
2246 parser->default_arg_ok_p = true;
2247
2248 /* We are not parsing a constant-expression. */
2249 parser->integral_constant_expression_p = false;
2250 parser->allow_non_integral_constant_expression_p = false;
2251 parser->non_integral_constant_expression_p = false;
2252
2253 /* We are not parsing offsetof. */
2254 parser->in_offsetof_p = false;
2255
2256 /* Local variable names are not forbidden. */
2257 parser->local_variables_forbidden_p = false;
2258
2259 /* We are not processing an `extern "C"' declaration. */
2260 parser->in_unbraced_linkage_specification_p = false;
2261
2262 /* We are not processing a declarator. */
2263 parser->in_declarator_p = false;
2264
2265 /* We are not processing a template-argument-list. */
2266 parser->in_template_argument_list_p = false;
2267
2268 /* We are not in an iteration statement. */
2269 parser->in_iteration_statement_p = false;
2270
2271 /* We are not in a switch statement. */
2272 parser->in_switch_statement_p = false;
2273
2274 /* We are not parsing a type-id inside an expression. */
2275 parser->in_type_id_in_expr_p = false;
2276
2277 /* The unparsed function queue is empty. */
2278 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2279
2280 /* There are no classes being defined. */
2281 parser->num_classes_being_defined = 0;
2282
2283 /* No template parameters apply. */
2284 parser->num_template_parameter_lists = 0;
2285
2286 return parser;
2287 }
2288
2289 /* Lexical conventions [gram.lex] */
2290
2291 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2292 identifier. */
2293
2294 static tree
cp_parser_identifier(cp_parser * parser)2295 cp_parser_identifier (cp_parser* parser)
2296 {
2297 cp_token *token;
2298
2299 /* Look for the identifier. */
2300 token = cp_parser_require (parser, CPP_NAME, "identifier");
2301 /* Return the value. */
2302 return token ? token->value : error_mark_node;
2303 }
2304
2305 /* Basic concepts [gram.basic] */
2306
2307 /* Parse a translation-unit.
2308
2309 translation-unit:
2310 declaration-seq [opt]
2311
2312 Returns TRUE if all went well. */
2313
2314 static bool
cp_parser_translation_unit(cp_parser * parser)2315 cp_parser_translation_unit (cp_parser* parser)
2316 {
2317 while (true)
2318 {
2319 cp_parser_declaration_seq_opt (parser);
2320
2321 /* If there are no tokens left then all went well. */
2322 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2323 break;
2324
2325 /* Otherwise, issue an error message. */
2326 cp_parser_error (parser, "expected declaration");
2327 return false;
2328 }
2329
2330 /* Consume the EOF token. */
2331 cp_parser_require (parser, CPP_EOF, "end-of-file");
2332
2333 /* Finish up. */
2334 finish_translation_unit ();
2335
2336 /* All went well. */
2337 return true;
2338 }
2339
2340 /* Expressions [gram.expr] */
2341
2342 /* Parse a primary-expression.
2343
2344 primary-expression:
2345 literal
2346 this
2347 ( expression )
2348 id-expression
2349
2350 GNU Extensions:
2351
2352 primary-expression:
2353 ( compound-statement )
2354 __builtin_va_arg ( assignment-expression , type-id )
2355
2356 literal:
2357 __null
2358
2359 Returns a representation of the expression.
2360
2361 *IDK indicates what kind of id-expression (if any) was present.
2362
2363 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2364 used as the operand of a pointer-to-member. In that case,
2365 *QUALIFYING_CLASS gives the class that is used as the qualifying
2366 class in the pointer-to-member. */
2367
2368 static tree
cp_parser_primary_expression(cp_parser * parser,cp_id_kind * idk,tree * qualifying_class)2369 cp_parser_primary_expression (cp_parser *parser,
2370 cp_id_kind *idk,
2371 tree *qualifying_class)
2372 {
2373 cp_token *token;
2374
2375 /* Assume the primary expression is not an id-expression. */
2376 *idk = CP_ID_KIND_NONE;
2377 /* And that it cannot be used as pointer-to-member. */
2378 *qualifying_class = NULL_TREE;
2379
2380 /* Peek at the next token. */
2381 token = cp_lexer_peek_token (parser->lexer);
2382 switch (token->type)
2383 {
2384 /* literal:
2385 integer-literal
2386 character-literal
2387 floating-literal
2388 string-literal
2389 boolean-literal */
2390 case CPP_CHAR:
2391 case CPP_WCHAR:
2392 case CPP_STRING:
2393 case CPP_WSTRING:
2394 case CPP_NUMBER:
2395 token = cp_lexer_consume_token (parser->lexer);
2396 return token->value;
2397
2398 case CPP_OPEN_PAREN:
2399 {
2400 tree expr;
2401 bool saved_greater_than_is_operator_p;
2402
2403 /* Consume the `('. */
2404 cp_lexer_consume_token (parser->lexer);
2405 /* Within a parenthesized expression, a `>' token is always
2406 the greater-than operator. */
2407 saved_greater_than_is_operator_p
2408 = parser->greater_than_is_operator_p;
2409 parser->greater_than_is_operator_p = true;
2410 /* If we see `( { ' then we are looking at the beginning of
2411 a GNU statement-expression. */
2412 if (cp_parser_allow_gnu_extensions_p (parser)
2413 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2414 {
2415 /* Statement-expressions are not allowed by the standard. */
2416 if (pedantic)
2417 pedwarn ("ISO C++ forbids braced-groups within expressions");
2418
2419 /* And they're not allowed outside of a function-body; you
2420 cannot, for example, write:
2421
2422 int i = ({ int j = 3; j + 1; });
2423
2424 at class or namespace scope. */
2425 if (!at_function_scope_p ())
2426 error ("statement-expressions are allowed only inside functions");
2427 /* Start the statement-expression. */
2428 expr = begin_stmt_expr ();
2429 /* Parse the compound-statement. */
2430 cp_parser_compound_statement (parser, true);
2431 /* Finish up. */
2432 expr = finish_stmt_expr (expr, false);
2433 }
2434 else
2435 {
2436 /* Parse the parenthesized expression. */
2437 expr = cp_parser_expression (parser);
2438 /* Let the front end know that this expression was
2439 enclosed in parentheses. This matters in case, for
2440 example, the expression is of the form `A::B', since
2441 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2442 not. */
2443 finish_parenthesized_expr (expr);
2444 }
2445 /* The `>' token might be the end of a template-id or
2446 template-parameter-list now. */
2447 parser->greater_than_is_operator_p
2448 = saved_greater_than_is_operator_p;
2449 /* Consume the `)'. */
2450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2451 cp_parser_skip_to_end_of_statement (parser);
2452
2453 return expr;
2454 }
2455
2456 case CPP_KEYWORD:
2457 switch (token->keyword)
2458 {
2459 /* These two are the boolean literals. */
2460 case RID_TRUE:
2461 cp_lexer_consume_token (parser->lexer);
2462 return boolean_true_node;
2463 case RID_FALSE:
2464 cp_lexer_consume_token (parser->lexer);
2465 return boolean_false_node;
2466
2467 /* The `__null' literal. */
2468 case RID_NULL:
2469 cp_lexer_consume_token (parser->lexer);
2470 return null_node;
2471
2472 /* Recognize the `this' keyword. */
2473 case RID_THIS:
2474 cp_lexer_consume_token (parser->lexer);
2475 if (parser->local_variables_forbidden_p)
2476 {
2477 error ("`this' may not be used in this context");
2478 return error_mark_node;
2479 }
2480 /* Pointers cannot appear in constant-expressions. */
2481 if (cp_parser_non_integral_constant_expression (parser,
2482 "`this'"))
2483 return error_mark_node;
2484 return finish_this_expr ();
2485
2486 /* The `operator' keyword can be the beginning of an
2487 id-expression. */
2488 case RID_OPERATOR:
2489 goto id_expression;
2490
2491 case RID_FUNCTION_NAME:
2492 case RID_PRETTY_FUNCTION_NAME:
2493 case RID_C99_FUNCTION_NAME:
2494 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2495 __func__ are the names of variables -- but they are
2496 treated specially. Therefore, they are handled here,
2497 rather than relying on the generic id-expression logic
2498 below. Grammatically, these names are id-expressions.
2499
2500 Consume the token. */
2501 token = cp_lexer_consume_token (parser->lexer);
2502 /* Look up the name. */
2503 return finish_fname (token->value);
2504
2505 case RID_VA_ARG:
2506 {
2507 tree expression;
2508 tree type;
2509
2510 /* The `__builtin_va_arg' construct is used to handle
2511 `va_arg'. Consume the `__builtin_va_arg' token. */
2512 cp_lexer_consume_token (parser->lexer);
2513 /* Look for the opening `('. */
2514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2515 /* Now, parse the assignment-expression. */
2516 expression = cp_parser_assignment_expression (parser);
2517 /* Look for the `,'. */
2518 cp_parser_require (parser, CPP_COMMA, "`,'");
2519 /* Parse the type-id. */
2520 type = cp_parser_type_id (parser);
2521 /* Look for the closing `)'. */
2522 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2523 /* Using `va_arg' in a constant-expression is not
2524 allowed. */
2525 if (cp_parser_non_integral_constant_expression (parser,
2526 "`va_arg'"))
2527 return error_mark_node;
2528 return build_x_va_arg (expression, type);
2529 }
2530
2531 case RID_OFFSETOF:
2532 {
2533 tree expression;
2534 bool saved_in_offsetof_p;
2535
2536 /* Consume the "__offsetof__" token. */
2537 cp_lexer_consume_token (parser->lexer);
2538 /* Consume the opening `('. */
2539 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2540 /* Parse the parenthesized (almost) constant-expression. */
2541 saved_in_offsetof_p = parser->in_offsetof_p;
2542 parser->in_offsetof_p = true;
2543 expression
2544 = cp_parser_constant_expression (parser,
2545 /*allow_non_constant_p=*/false,
2546 /*non_constant_p=*/NULL);
2547 parser->in_offsetof_p = saved_in_offsetof_p;
2548 /* Consume the closing ')'. */
2549 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2550
2551 return expression;
2552 }
2553
2554 default:
2555 cp_parser_error (parser, "expected primary-expression");
2556 return error_mark_node;
2557 }
2558
2559 /* An id-expression can start with either an identifier, a
2560 `::' as the beginning of a qualified-id, or the "operator"
2561 keyword. */
2562 case CPP_NAME:
2563 case CPP_SCOPE:
2564 case CPP_TEMPLATE_ID:
2565 case CPP_NESTED_NAME_SPECIFIER:
2566 {
2567 tree id_expression;
2568 tree decl;
2569 const char *error_msg;
2570
2571 id_expression:
2572 /* Parse the id-expression. */
2573 id_expression
2574 = cp_parser_id_expression (parser,
2575 /*template_keyword_p=*/false,
2576 /*check_dependency_p=*/true,
2577 /*template_p=*/NULL,
2578 /*declarator_p=*/false);
2579 if (id_expression == error_mark_node)
2580 return error_mark_node;
2581 /* If we have a template-id, then no further lookup is
2582 required. If the template-id was for a template-class, we
2583 will sometimes have a TYPE_DECL at this point. */
2584 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2585 || TREE_CODE (id_expression) == TYPE_DECL)
2586 decl = id_expression;
2587 /* Look up the name. */
2588 else
2589 {
2590 decl = cp_parser_lookup_name_simple (parser, id_expression);
2591 /* If name lookup gives us a SCOPE_REF, then the
2592 qualifying scope was dependent. Just propagate the
2593 name. */
2594 if (TREE_CODE (decl) == SCOPE_REF)
2595 {
2596 if (TYPE_P (TREE_OPERAND (decl, 0)))
2597 *qualifying_class = TREE_OPERAND (decl, 0);
2598 return decl;
2599 }
2600 /* Check to see if DECL is a local variable in a context
2601 where that is forbidden. */
2602 if (parser->local_variables_forbidden_p
2603 && local_variable_p (decl))
2604 {
2605 /* It might be that we only found DECL because we are
2606 trying to be generous with pre-ISO scoping rules.
2607 For example, consider:
2608
2609 int i;
2610 void g() {
2611 for (int i = 0; i < 10; ++i) {}
2612 extern void f(int j = i);
2613 }
2614
2615 Here, name look up will originally find the out
2616 of scope `i'. We need to issue a warning message,
2617 but then use the global `i'. */
2618 decl = check_for_out_of_scope_variable (decl);
2619 if (local_variable_p (decl))
2620 {
2621 error ("local variable `%D' may not appear in this context",
2622 decl);
2623 return error_mark_node;
2624 }
2625 }
2626 }
2627
2628 decl = finish_id_expression (id_expression, decl, parser->scope,
2629 idk, qualifying_class,
2630 parser->integral_constant_expression_p,
2631 parser->allow_non_integral_constant_expression_p,
2632 &parser->non_integral_constant_expression_p,
2633 &error_msg);
2634 if (error_msg)
2635 cp_parser_error (parser, error_msg);
2636 return decl;
2637 }
2638
2639 /* Anything else is an error. */
2640 default:
2641 cp_parser_error (parser, "expected primary-expression");
2642 return error_mark_node;
2643 }
2644 }
2645
2646 /* Parse an id-expression.
2647
2648 id-expression:
2649 unqualified-id
2650 qualified-id
2651
2652 qualified-id:
2653 :: [opt] nested-name-specifier template [opt] unqualified-id
2654 :: identifier
2655 :: operator-function-id
2656 :: template-id
2657
2658 Return a representation of the unqualified portion of the
2659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2660 a `::' or nested-name-specifier.
2661
2662 Often, if the id-expression was a qualified-id, the caller will
2663 want to make a SCOPE_REF to represent the qualified-id. This
2664 function does not do this in order to avoid wastefully creating
2665 SCOPE_REFs when they are not required.
2666
2667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2668 `template' keyword.
2669
2670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2671 uninstantiated templates.
2672
2673 If *TEMPLATE_P is non-NULL, it is set to true iff the
2674 `template' keyword is used to explicitly indicate that the entity
2675 named is a template.
2676
2677 If DECLARATOR_P is true, the id-expression is appearing as part of
2678 a declarator, rather than as part of an expression. */
2679
2680 static tree
cp_parser_id_expression(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool * template_p,bool declarator_p)2681 cp_parser_id_expression (cp_parser *parser,
2682 bool template_keyword_p,
2683 bool check_dependency_p,
2684 bool *template_p,
2685 bool declarator_p)
2686 {
2687 bool global_scope_p;
2688 bool nested_name_specifier_p;
2689
2690 /* Assume the `template' keyword was not used. */
2691 if (template_p)
2692 *template_p = false;
2693
2694 /* Look for the optional `::' operator. */
2695 global_scope_p
2696 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2697 != NULL_TREE);
2698 /* Look for the optional nested-name-specifier. */
2699 nested_name_specifier_p
2700 = (cp_parser_nested_name_specifier_opt (parser,
2701 /*typename_keyword_p=*/false,
2702 check_dependency_p,
2703 /*type_p=*/false,
2704 /*is_declarator=*/false)
2705 != NULL_TREE);
2706 /* If there is a nested-name-specifier, then we are looking at
2707 the first qualified-id production. */
2708 if (nested_name_specifier_p)
2709 {
2710 tree saved_scope;
2711 tree saved_object_scope;
2712 tree saved_qualifying_scope;
2713 tree unqualified_id;
2714 bool is_template;
2715
2716 /* See if the next token is the `template' keyword. */
2717 if (!template_p)
2718 template_p = &is_template;
2719 *template_p = cp_parser_optional_template_keyword (parser);
2720 /* Name lookup we do during the processing of the
2721 unqualified-id might obliterate SCOPE. */
2722 saved_scope = parser->scope;
2723 saved_object_scope = parser->object_scope;
2724 saved_qualifying_scope = parser->qualifying_scope;
2725 /* Process the final unqualified-id. */
2726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2727 check_dependency_p,
2728 declarator_p);
2729 /* Restore the SAVED_SCOPE for our caller. */
2730 parser->scope = saved_scope;
2731 parser->object_scope = saved_object_scope;
2732 parser->qualifying_scope = saved_qualifying_scope;
2733
2734 return unqualified_id;
2735 }
2736 /* Otherwise, if we are in global scope, then we are looking at one
2737 of the other qualified-id productions. */
2738 else if (global_scope_p)
2739 {
2740 cp_token *token;
2741 tree id;
2742
2743 /* Peek at the next token. */
2744 token = cp_lexer_peek_token (parser->lexer);
2745
2746 /* If it's an identifier, and the next token is not a "<", then
2747 we can avoid the template-id case. This is an optimization
2748 for this common case. */
2749 if (token->type == CPP_NAME
2750 && !cp_parser_nth_token_starts_template_argument_list_p
2751 (parser, 2))
2752 return cp_parser_identifier (parser);
2753
2754 cp_parser_parse_tentatively (parser);
2755 /* Try a template-id. */
2756 id = cp_parser_template_id (parser,
2757 /*template_keyword_p=*/false,
2758 /*check_dependency_p=*/true,
2759 declarator_p);
2760 /* If that worked, we're done. */
2761 if (cp_parser_parse_definitely (parser))
2762 return id;
2763
2764 /* Peek at the next token. (Changes in the token buffer may
2765 have invalidated the pointer obtained above.) */
2766 token = cp_lexer_peek_token (parser->lexer);
2767
2768 switch (token->type)
2769 {
2770 case CPP_NAME:
2771 return cp_parser_identifier (parser);
2772
2773 case CPP_KEYWORD:
2774 if (token->keyword == RID_OPERATOR)
2775 return cp_parser_operator_function_id (parser);
2776 /* Fall through. */
2777
2778 default:
2779 cp_parser_error (parser, "expected id-expression");
2780 return error_mark_node;
2781 }
2782 }
2783 else
2784 return cp_parser_unqualified_id (parser, template_keyword_p,
2785 /*check_dependency_p=*/true,
2786 declarator_p);
2787 }
2788
2789 /* Parse an unqualified-id.
2790
2791 unqualified-id:
2792 identifier
2793 operator-function-id
2794 conversion-function-id
2795 ~ class-name
2796 template-id
2797
2798 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2799 keyword, in a construct like `A::template ...'.
2800
2801 Returns a representation of unqualified-id. For the `identifier'
2802 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2803 production a BIT_NOT_EXPR is returned; the operand of the
2804 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2805 other productions, see the documentation accompanying the
2806 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2807 names are looked up in uninstantiated templates. If DECLARATOR_P
2808 is true, the unqualified-id is appearing as part of a declarator,
2809 rather than as part of an expression. */
2810
2811 static tree
cp_parser_unqualified_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool declarator_p)2812 cp_parser_unqualified_id (cp_parser* parser,
2813 bool template_keyword_p,
2814 bool check_dependency_p,
2815 bool declarator_p)
2816 {
2817 cp_token *token;
2818
2819 /* Peek at the next token. */
2820 token = cp_lexer_peek_token (parser->lexer);
2821
2822 switch (token->type)
2823 {
2824 case CPP_NAME:
2825 {
2826 tree id;
2827
2828 /* We don't know yet whether or not this will be a
2829 template-id. */
2830 cp_parser_parse_tentatively (parser);
2831 /* Try a template-id. */
2832 id = cp_parser_template_id (parser, template_keyword_p,
2833 check_dependency_p,
2834 declarator_p);
2835 /* If it worked, we're done. */
2836 if (cp_parser_parse_definitely (parser))
2837 return id;
2838 /* Otherwise, it's an ordinary identifier. */
2839 return cp_parser_identifier (parser);
2840 }
2841
2842 case CPP_TEMPLATE_ID:
2843 return cp_parser_template_id (parser, template_keyword_p,
2844 check_dependency_p,
2845 declarator_p);
2846
2847 case CPP_COMPL:
2848 {
2849 tree type_decl;
2850 tree qualifying_scope;
2851 tree object_scope;
2852 tree scope;
2853
2854 /* Consume the `~' token. */
2855 cp_lexer_consume_token (parser->lexer);
2856 /* Parse the class-name. The standard, as written, seems to
2857 say that:
2858
2859 template <typename T> struct S { ~S (); };
2860 template <typename T> S<T>::~S() {}
2861
2862 is invalid, since `~' must be followed by a class-name, but
2863 `S<T>' is dependent, and so not known to be a class.
2864 That's not right; we need to look in uninstantiated
2865 templates. A further complication arises from:
2866
2867 template <typename T> void f(T t) {
2868 t.T::~T();
2869 }
2870
2871 Here, it is not possible to look up `T' in the scope of `T'
2872 itself. We must look in both the current scope, and the
2873 scope of the containing complete expression.
2874
2875 Yet another issue is:
2876
2877 struct S {
2878 int S;
2879 ~S();
2880 };
2881
2882 S::~S() {}
2883
2884 The standard does not seem to say that the `S' in `~S'
2885 should refer to the type `S' and not the data member
2886 `S::S'. */
2887
2888 /* DR 244 says that we look up the name after the "~" in the
2889 same scope as we looked up the qualifying name. That idea
2890 isn't fully worked out; it's more complicated than that. */
2891 scope = parser->scope;
2892 object_scope = parser->object_scope;
2893 qualifying_scope = parser->qualifying_scope;
2894
2895 /* If the name is of the form "X::~X" it's OK. */
2896 if (scope && TYPE_P (scope)
2897 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2898 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2899 == CPP_OPEN_PAREN)
2900 && (cp_lexer_peek_token (parser->lexer)->value
2901 == TYPE_IDENTIFIER (scope)))
2902 {
2903 cp_lexer_consume_token (parser->lexer);
2904 return build_nt (BIT_NOT_EXPR, scope);
2905 }
2906
2907 /* If there was an explicit qualification (S::~T), first look
2908 in the scope given by the qualification (i.e., S). */
2909 if (scope)
2910 {
2911 cp_parser_parse_tentatively (parser);
2912 type_decl = cp_parser_class_name (parser,
2913 /*typename_keyword_p=*/false,
2914 /*template_keyword_p=*/false,
2915 /*type_p=*/false,
2916 /*check_dependency=*/false,
2917 /*class_head_p=*/false,
2918 declarator_p);
2919 if (cp_parser_parse_definitely (parser))
2920 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2921 }
2922 /* In "N::S::~S", look in "N" as well. */
2923 if (scope && qualifying_scope)
2924 {
2925 cp_parser_parse_tentatively (parser);
2926 parser->scope = qualifying_scope;
2927 parser->object_scope = NULL_TREE;
2928 parser->qualifying_scope = NULL_TREE;
2929 type_decl
2930 = cp_parser_class_name (parser,
2931 /*typename_keyword_p=*/false,
2932 /*template_keyword_p=*/false,
2933 /*type_p=*/false,
2934 /*check_dependency=*/false,
2935 /*class_head_p=*/false,
2936 declarator_p);
2937 if (cp_parser_parse_definitely (parser))
2938 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2939 }
2940 /* In "p->S::~T", look in the scope given by "*p" as well. */
2941 else if (object_scope)
2942 {
2943 cp_parser_parse_tentatively (parser);
2944 parser->scope = object_scope;
2945 parser->object_scope = NULL_TREE;
2946 parser->qualifying_scope = NULL_TREE;
2947 type_decl
2948 = cp_parser_class_name (parser,
2949 /*typename_keyword_p=*/false,
2950 /*template_keyword_p=*/false,
2951 /*type_p=*/false,
2952 /*check_dependency=*/false,
2953 /*class_head_p=*/false,
2954 declarator_p);
2955 if (cp_parser_parse_definitely (parser))
2956 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2957 }
2958 /* Look in the surrounding context. */
2959 parser->scope = NULL_TREE;
2960 parser->object_scope = NULL_TREE;
2961 parser->qualifying_scope = NULL_TREE;
2962 type_decl
2963 = cp_parser_class_name (parser,
2964 /*typename_keyword_p=*/false,
2965 /*template_keyword_p=*/false,
2966 /*type_p=*/false,
2967 /*check_dependency=*/false,
2968 /*class_head_p=*/false,
2969 declarator_p);
2970 /* If an error occurred, assume that the name of the
2971 destructor is the same as the name of the qualifying
2972 class. That allows us to keep parsing after running
2973 into ill-formed destructor names. */
2974 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2975 return build_nt (BIT_NOT_EXPR, scope);
2976 else if (type_decl == error_mark_node)
2977 return error_mark_node;
2978
2979 /* [class.dtor]
2980
2981 A typedef-name that names a class shall not be used as the
2982 identifier in the declarator for a destructor declaration. */
2983 if (declarator_p
2984 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2985 && !DECL_SELF_REFERENCE_P (type_decl))
2986 error ("typedef-name `%D' used as destructor declarator",
2987 type_decl);
2988
2989 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2990 }
2991
2992 case CPP_KEYWORD:
2993 if (token->keyword == RID_OPERATOR)
2994 {
2995 tree id;
2996
2997 /* This could be a template-id, so we try that first. */
2998 cp_parser_parse_tentatively (parser);
2999 /* Try a template-id. */
3000 id = cp_parser_template_id (parser, template_keyword_p,
3001 /*check_dependency_p=*/true,
3002 declarator_p);
3003 /* If that worked, we're done. */
3004 if (cp_parser_parse_definitely (parser))
3005 return id;
3006 /* We still don't know whether we're looking at an
3007 operator-function-id or a conversion-function-id. */
3008 cp_parser_parse_tentatively (parser);
3009 /* Try an operator-function-id. */
3010 id = cp_parser_operator_function_id (parser);
3011 /* If that didn't work, try a conversion-function-id. */
3012 if (!cp_parser_parse_definitely (parser))
3013 id = cp_parser_conversion_function_id (parser);
3014
3015 return id;
3016 }
3017 /* Fall through. */
3018
3019 default:
3020 cp_parser_error (parser, "expected unqualified-id");
3021 return error_mark_node;
3022 }
3023 }
3024
3025 /* Parse an (optional) nested-name-specifier.
3026
3027 nested-name-specifier:
3028 class-or-namespace-name :: nested-name-specifier [opt]
3029 class-or-namespace-name :: template nested-name-specifier [opt]
3030
3031 PARSER->SCOPE should be set appropriately before this function is
3032 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3033 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3034 in name lookups.
3035
3036 Sets PARSER->SCOPE to the class (TYPE) or namespace
3037 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3038 it unchanged if there is no nested-name-specifier. Returns the new
3039 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3040
3041 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3042 part of a declaration and/or decl-specifier. */
3043
3044 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)3045 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3046 bool typename_keyword_p,
3047 bool check_dependency_p,
3048 bool type_p,
3049 bool is_declaration)
3050 {
3051 bool success = false;
3052 tree access_check = NULL_TREE;
3053 ptrdiff_t start;
3054 cp_token* token;
3055
3056 /* If the next token corresponds to a nested name specifier, there
3057 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3058 false, it may have been true before, in which case something
3059 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3060 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3061 CHECK_DEPENDENCY_P is false, we have to fall through into the
3062 main loop. */
3063 if (check_dependency_p
3064 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3065 {
3066 cp_parser_pre_parsed_nested_name_specifier (parser);
3067 return parser->scope;
3068 }
3069
3070 /* Remember where the nested-name-specifier starts. */
3071 if (cp_parser_parsing_tentatively (parser)
3072 && !cp_parser_committed_to_tentative_parse (parser))
3073 {
3074 token = cp_lexer_peek_token (parser->lexer);
3075 start = cp_lexer_token_difference (parser->lexer,
3076 parser->lexer->first_token,
3077 token);
3078 }
3079 else
3080 start = -1;
3081
3082 push_deferring_access_checks (dk_deferred);
3083
3084 while (true)
3085 {
3086 tree new_scope;
3087 tree old_scope;
3088 tree saved_qualifying_scope;
3089 bool template_keyword_p;
3090
3091 /* Spot cases that cannot be the beginning of a
3092 nested-name-specifier. */
3093 token = cp_lexer_peek_token (parser->lexer);
3094
3095 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3096 the already parsed nested-name-specifier. */
3097 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3098 {
3099 /* Grab the nested-name-specifier and continue the loop. */
3100 cp_parser_pre_parsed_nested_name_specifier (parser);
3101 success = true;
3102 continue;
3103 }
3104
3105 /* Spot cases that cannot be the beginning of a
3106 nested-name-specifier. On the second and subsequent times
3107 through the loop, we look for the `template' keyword. */
3108 if (success && token->keyword == RID_TEMPLATE)
3109 ;
3110 /* A template-id can start a nested-name-specifier. */
3111 else if (token->type == CPP_TEMPLATE_ID)
3112 ;
3113 else
3114 {
3115 /* If the next token is not an identifier, then it is
3116 definitely not a class-or-namespace-name. */
3117 if (token->type != CPP_NAME)
3118 break;
3119 /* If the following token is neither a `<' (to begin a
3120 template-id), nor a `::', then we are not looking at a
3121 nested-name-specifier. */
3122 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3123 if (token->type != CPP_SCOPE
3124 && !cp_parser_nth_token_starts_template_argument_list_p
3125 (parser, 2))
3126 break;
3127 }
3128
3129 /* The nested-name-specifier is optional, so we parse
3130 tentatively. */
3131 cp_parser_parse_tentatively (parser);
3132
3133 /* Look for the optional `template' keyword, if this isn't the
3134 first time through the loop. */
3135 if (success)
3136 template_keyword_p = cp_parser_optional_template_keyword (parser);
3137 else
3138 template_keyword_p = false;
3139
3140 /* Save the old scope since the name lookup we are about to do
3141 might destroy it. */
3142 old_scope = parser->scope;
3143 saved_qualifying_scope = parser->qualifying_scope;
3144 /* Parse the qualifying entity. */
3145 new_scope
3146 = cp_parser_class_or_namespace_name (parser,
3147 typename_keyword_p,
3148 template_keyword_p,
3149 check_dependency_p,
3150 type_p,
3151 is_declaration);
3152 /* Look for the `::' token. */
3153 cp_parser_require (parser, CPP_SCOPE, "`::'");
3154
3155 /* If we found what we wanted, we keep going; otherwise, we're
3156 done. */
3157 if (!cp_parser_parse_definitely (parser))
3158 {
3159 bool error_p = false;
3160
3161 /* Restore the OLD_SCOPE since it was valid before the
3162 failed attempt at finding the last
3163 class-or-namespace-name. */
3164 parser->scope = old_scope;
3165 parser->qualifying_scope = saved_qualifying_scope;
3166 /* If the next token is an identifier, and the one after
3167 that is a `::', then any valid interpretation would have
3168 found a class-or-namespace-name. */
3169 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3170 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3171 == CPP_SCOPE)
3172 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3173 != CPP_COMPL))
3174 {
3175 token = cp_lexer_consume_token (parser->lexer);
3176 if (!error_p)
3177 {
3178 tree decl;
3179
3180 decl = cp_parser_lookup_name_simple (parser, token->value);
3181 if (TREE_CODE (decl) == TEMPLATE_DECL)
3182 error ("`%D' used without template parameters",
3183 decl);
3184 else
3185 cp_parser_name_lookup_error
3186 (parser, token->value, decl,
3187 "is not a class or namespace");
3188 parser->scope = NULL_TREE;
3189 error_p = true;
3190 /* Treat this as a successful nested-name-specifier
3191 due to:
3192
3193 [basic.lookup.qual]
3194
3195 If the name found is not a class-name (clause
3196 _class_) or namespace-name (_namespace.def_), the
3197 program is ill-formed. */
3198 success = true;
3199 }
3200 cp_lexer_consume_token (parser->lexer);
3201 }
3202 break;
3203 }
3204
3205 /* We've found one valid nested-name-specifier. */
3206 success = true;
3207 /* Make sure we look in the right scope the next time through
3208 the loop. */
3209 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3210 ? TREE_TYPE (new_scope)
3211 : new_scope);
3212 /* If it is a class scope, try to complete it; we are about to
3213 be looking up names inside the class. */
3214 if (TYPE_P (parser->scope)
3215 /* Since checking types for dependency can be expensive,
3216 avoid doing it if the type is already complete. */
3217 && !COMPLETE_TYPE_P (parser->scope)
3218 /* Do not try to complete dependent types. */
3219 && !dependent_type_p (parser->scope))
3220 complete_type (parser->scope);
3221 }
3222
3223 /* Retrieve any deferred checks. Do not pop this access checks yet
3224 so the memory will not be reclaimed during token replacing below. */
3225 access_check = get_deferred_access_checks ();
3226
3227 /* If parsing tentatively, replace the sequence of tokens that makes
3228 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3229 token. That way, should we re-parse the token stream, we will
3230 not have to repeat the effort required to do the parse, nor will
3231 we issue duplicate error messages. */
3232 if (success && start >= 0)
3233 {
3234 /* Find the token that corresponds to the start of the
3235 template-id. */
3236 token = cp_lexer_advance_token (parser->lexer,
3237 parser->lexer->first_token,
3238 start);
3239
3240 /* Reset the contents of the START token. */
3241 token->type = CPP_NESTED_NAME_SPECIFIER;
3242 token->value = build_tree_list (access_check, parser->scope);
3243 TREE_TYPE (token->value) = parser->qualifying_scope;
3244 token->keyword = RID_MAX;
3245 /* Purge all subsequent tokens. */
3246 cp_lexer_purge_tokens_after (parser->lexer, token);
3247 }
3248
3249 pop_deferring_access_checks ();
3250 return success ? parser->scope : NULL_TREE;
3251 }
3252
3253 /* Parse a nested-name-specifier. See
3254 cp_parser_nested_name_specifier_opt for details. This function
3255 behaves identically, except that it will an issue an error if no
3256 nested-name-specifier is present, and it will return
3257 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3258 is present. */
3259
3260 static tree
cp_parser_nested_name_specifier(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)3261 cp_parser_nested_name_specifier (cp_parser *parser,
3262 bool typename_keyword_p,
3263 bool check_dependency_p,
3264 bool type_p,
3265 bool is_declaration)
3266 {
3267 tree scope;
3268
3269 /* Look for the nested-name-specifier. */
3270 scope = cp_parser_nested_name_specifier_opt (parser,
3271 typename_keyword_p,
3272 check_dependency_p,
3273 type_p,
3274 is_declaration);
3275 /* If it was not present, issue an error message. */
3276 if (!scope)
3277 {
3278 cp_parser_error (parser, "expected nested-name-specifier");
3279 parser->scope = NULL_TREE;
3280 return error_mark_node;
3281 }
3282
3283 return scope;
3284 }
3285
3286 /* Parse a class-or-namespace-name.
3287
3288 class-or-namespace-name:
3289 class-name
3290 namespace-name
3291
3292 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3293 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3294 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3295 TYPE_P is TRUE iff the next name should be taken as a class-name,
3296 even the same name is declared to be another entity in the same
3297 scope.
3298
3299 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3300 specified by the class-or-namespace-name. If neither is found the
3301 ERROR_MARK_NODE is returned. */
3302
3303 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)3304 cp_parser_class_or_namespace_name (cp_parser *parser,
3305 bool typename_keyword_p,
3306 bool template_keyword_p,
3307 bool check_dependency_p,
3308 bool type_p,
3309 bool is_declaration)
3310 {
3311 tree saved_scope;
3312 tree saved_qualifying_scope;
3313 tree saved_object_scope;
3314 tree scope;
3315 bool only_class_p;
3316
3317 /* Before we try to parse the class-name, we must save away the
3318 current PARSER->SCOPE since cp_parser_class_name will destroy
3319 it. */
3320 saved_scope = parser->scope;
3321 saved_qualifying_scope = parser->qualifying_scope;
3322 saved_object_scope = parser->object_scope;
3323 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3324 there is no need to look for a namespace-name. */
3325 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3326 if (!only_class_p)
3327 cp_parser_parse_tentatively (parser);
3328 scope = cp_parser_class_name (parser,
3329 typename_keyword_p,
3330 template_keyword_p,
3331 type_p,
3332 check_dependency_p,
3333 /*class_head_p=*/false,
3334 is_declaration);
3335 /* If that didn't work, try for a namespace-name. */
3336 if (!only_class_p && !cp_parser_parse_definitely (parser))
3337 {
3338 /* Restore the saved scope. */
3339 parser->scope = saved_scope;
3340 parser->qualifying_scope = saved_qualifying_scope;
3341 parser->object_scope = saved_object_scope;
3342 /* If we are not looking at an identifier followed by the scope
3343 resolution operator, then this is not part of a
3344 nested-name-specifier. (Note that this function is only used
3345 to parse the components of a nested-name-specifier.) */
3346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3347 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3348 return error_mark_node;
3349 scope = cp_parser_namespace_name (parser);
3350 }
3351
3352 return scope;
3353 }
3354
3355 /* Parse a postfix-expression.
3356
3357 postfix-expression:
3358 primary-expression
3359 postfix-expression [ expression ]
3360 postfix-expression ( expression-list [opt] )
3361 simple-type-specifier ( expression-list [opt] )
3362 typename :: [opt] nested-name-specifier identifier
3363 ( expression-list [opt] )
3364 typename :: [opt] nested-name-specifier template [opt] template-id
3365 ( expression-list [opt] )
3366 postfix-expression . template [opt] id-expression
3367 postfix-expression -> template [opt] id-expression
3368 postfix-expression . pseudo-destructor-name
3369 postfix-expression -> pseudo-destructor-name
3370 postfix-expression ++
3371 postfix-expression --
3372 dynamic_cast < type-id > ( expression )
3373 static_cast < type-id > ( expression )
3374 reinterpret_cast < type-id > ( expression )
3375 const_cast < type-id > ( expression )
3376 typeid ( expression )
3377 typeid ( type-id )
3378
3379 GNU Extension:
3380
3381 postfix-expression:
3382 ( type-id ) { initializer-list , [opt] }
3383
3384 This extension is a GNU version of the C99 compound-literal
3385 construct. (The C99 grammar uses `type-name' instead of `type-id',
3386 but they are essentially the same concept.)
3387
3388 If ADDRESS_P is true, the postfix expression is the operand of the
3389 `&' operator.
3390
3391 Returns a representation of the expression. */
3392
3393 static tree
cp_parser_postfix_expression(cp_parser * parser,bool address_p)3394 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3395 {
3396 cp_token *token;
3397 enum rid keyword;
3398 cp_id_kind idk = CP_ID_KIND_NONE;
3399 tree postfix_expression = NULL_TREE;
3400 /* Non-NULL only if the current postfix-expression can be used to
3401 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3402 class used to qualify the member. */
3403 tree qualifying_class = NULL_TREE;
3404
3405 /* Peek at the next token. */
3406 token = cp_lexer_peek_token (parser->lexer);
3407 /* Some of the productions are determined by keywords. */
3408 keyword = token->keyword;
3409 switch (keyword)
3410 {
3411 case RID_DYNCAST:
3412 case RID_STATCAST:
3413 case RID_REINTCAST:
3414 case RID_CONSTCAST:
3415 {
3416 tree type;
3417 tree expression;
3418 const char *saved_message;
3419
3420 /* All of these can be handled in the same way from the point
3421 of view of parsing. Begin by consuming the token
3422 identifying the cast. */
3423 cp_lexer_consume_token (parser->lexer);
3424
3425 /* New types cannot be defined in the cast. */
3426 saved_message = parser->type_definition_forbidden_message;
3427 parser->type_definition_forbidden_message
3428 = "types may not be defined in casts";
3429
3430 /* Look for the opening `<'. */
3431 cp_parser_require (parser, CPP_LESS, "`<'");
3432 /* Parse the type to which we are casting. */
3433 type = cp_parser_type_id (parser);
3434 /* Look for the closing `>'. */
3435 cp_parser_require (parser, CPP_GREATER, "`>'");
3436 /* Restore the old message. */
3437 parser->type_definition_forbidden_message = saved_message;
3438
3439 /* And the expression which is being cast. */
3440 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3441 expression = cp_parser_expression (parser);
3442 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3443
3444 /* Only type conversions to integral or enumeration types
3445 can be used in constant-expressions. */
3446 if (parser->integral_constant_expression_p
3447 && !dependent_type_p (type)
3448 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3449 /* A cast to pointer or reference type is allowed in the
3450 implementation of "offsetof". */
3451 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3452 && (cp_parser_non_integral_constant_expression
3453 (parser,
3454 "a cast to a type other than an integral or "
3455 "enumeration type")))
3456 return error_mark_node;
3457
3458 switch (keyword)
3459 {
3460 case RID_DYNCAST:
3461 postfix_expression
3462 = build_dynamic_cast (type, expression);
3463 break;
3464 case RID_STATCAST:
3465 postfix_expression
3466 = build_static_cast (type, expression);
3467 break;
3468 case RID_REINTCAST:
3469 postfix_expression
3470 = build_reinterpret_cast (type, expression);
3471 break;
3472 case RID_CONSTCAST:
3473 postfix_expression
3474 = build_const_cast (type, expression);
3475 break;
3476 default:
3477 abort ();
3478 }
3479 }
3480 break;
3481
3482 case RID_TYPEID:
3483 {
3484 tree type;
3485 const char *saved_message;
3486 bool saved_in_type_id_in_expr_p;
3487
3488 /* Consume the `typeid' token. */
3489 cp_lexer_consume_token (parser->lexer);
3490 /* Look for the `(' token. */
3491 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3492 /* Types cannot be defined in a `typeid' expression. */
3493 saved_message = parser->type_definition_forbidden_message;
3494 parser->type_definition_forbidden_message
3495 = "types may not be defined in a `typeid\' expression";
3496 /* We can't be sure yet whether we're looking at a type-id or an
3497 expression. */
3498 cp_parser_parse_tentatively (parser);
3499 /* Try a type-id first. */
3500 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3501 parser->in_type_id_in_expr_p = true;
3502 type = cp_parser_type_id (parser);
3503 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3504 /* Look for the `)' token. Otherwise, we can't be sure that
3505 we're not looking at an expression: consider `typeid (int
3506 (3))', for example. */
3507 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3508 /* If all went well, simply lookup the type-id. */
3509 if (cp_parser_parse_definitely (parser))
3510 postfix_expression = get_typeid (type);
3511 /* Otherwise, fall back to the expression variant. */
3512 else
3513 {
3514 tree expression;
3515
3516 /* Look for an expression. */
3517 expression = cp_parser_expression (parser);
3518 /* Compute its typeid. */
3519 postfix_expression = build_typeid (expression);
3520 /* Look for the `)' token. */
3521 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3522 }
3523 /* `typeid' may not appear in an integral constant expression. */
3524 if (cp_parser_non_integral_constant_expression(parser,
3525 "`typeid' operator"))
3526 return error_mark_node;
3527 /* Restore the saved message. */
3528 parser->type_definition_forbidden_message = saved_message;
3529 }
3530 break;
3531
3532 case RID_TYPENAME:
3533 {
3534 bool template_p = false;
3535 tree id;
3536 tree type;
3537
3538 /* Consume the `typename' token. */
3539 cp_lexer_consume_token (parser->lexer);
3540 /* Look for the optional `::' operator. */
3541 cp_parser_global_scope_opt (parser,
3542 /*current_scope_valid_p=*/false);
3543 /* Look for the nested-name-specifier. */
3544 cp_parser_nested_name_specifier (parser,
3545 /*typename_keyword_p=*/true,
3546 /*check_dependency_p=*/true,
3547 /*type_p=*/true,
3548 /*is_declaration=*/true);
3549 /* Look for the optional `template' keyword. */
3550 template_p = cp_parser_optional_template_keyword (parser);
3551 /* We don't know whether we're looking at a template-id or an
3552 identifier. */
3553 cp_parser_parse_tentatively (parser);
3554 /* Try a template-id. */
3555 id = cp_parser_template_id (parser, template_p,
3556 /*check_dependency_p=*/true,
3557 /*is_declaration=*/true);
3558 /* If that didn't work, try an identifier. */
3559 if (!cp_parser_parse_definitely (parser))
3560 id = cp_parser_identifier (parser);
3561 /* If we look up a template-id in a non-dependent qualifying
3562 scope, there's no need to create a dependent type. */
3563 if (TREE_CODE (id) == TYPE_DECL
3564 && !dependent_type_p (parser->scope))
3565 type = TREE_TYPE (id);
3566 /* Create a TYPENAME_TYPE to represent the type to which the
3567 functional cast is being performed. */
3568 else
3569 type = make_typename_type (parser->scope, id,
3570 /*complain=*/1);
3571
3572 postfix_expression = cp_parser_functional_cast (parser, type);
3573 }
3574 break;
3575
3576 default:
3577 {
3578 tree type;
3579
3580 /* If the next thing is a simple-type-specifier, we may be
3581 looking at a functional cast. We could also be looking at
3582 an id-expression. So, we try the functional cast, and if
3583 that doesn't work we fall back to the primary-expression. */
3584 cp_parser_parse_tentatively (parser);
3585 /* Look for the simple-type-specifier. */
3586 type = cp_parser_simple_type_specifier (parser,
3587 CP_PARSER_FLAGS_NONE,
3588 /*identifier_p=*/false);
3589 /* Parse the cast itself. */
3590 if (!cp_parser_error_occurred (parser))
3591 postfix_expression
3592 = cp_parser_functional_cast (parser, type);
3593 /* If that worked, we're done. */
3594 if (cp_parser_parse_definitely (parser))
3595 break;
3596
3597 /* If the functional-cast didn't work out, try a
3598 compound-literal. */
3599 if (cp_parser_allow_gnu_extensions_p (parser)
3600 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3601 {
3602 tree initializer_list = NULL_TREE;
3603 bool saved_in_type_id_in_expr_p;
3604
3605 cp_parser_parse_tentatively (parser);
3606 /* Consume the `('. */
3607 cp_lexer_consume_token (parser->lexer);
3608 /* Parse the type. */
3609 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3610 parser->in_type_id_in_expr_p = true;
3611 type = cp_parser_type_id (parser);
3612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3613 /* Look for the `)'. */
3614 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3615 /* Look for the `{'. */
3616 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3617 /* If things aren't going well, there's no need to
3618 keep going. */
3619 if (!cp_parser_error_occurred (parser))
3620 {
3621 bool non_constant_p;
3622 /* Parse the initializer-list. */
3623 initializer_list
3624 = cp_parser_initializer_list (parser, &non_constant_p);
3625 /* Allow a trailing `,'. */
3626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3627 cp_lexer_consume_token (parser->lexer);
3628 /* Look for the final `}'. */
3629 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3630 }
3631 /* If that worked, we're definitely looking at a
3632 compound-literal expression. */
3633 if (cp_parser_parse_definitely (parser))
3634 {
3635 /* Warn the user that a compound literal is not
3636 allowed in standard C++. */
3637 if (pedantic)
3638 pedwarn ("ISO C++ forbids compound-literals");
3639 /* Form the representation of the compound-literal. */
3640 postfix_expression
3641 = finish_compound_literal (type, initializer_list);
3642 break;
3643 }
3644 }
3645
3646 /* It must be a primary-expression. */
3647 postfix_expression = cp_parser_primary_expression (parser,
3648 &idk,
3649 &qualifying_class);
3650 }
3651 break;
3652 }
3653
3654 /* If we were avoiding committing to the processing of a
3655 qualified-id until we knew whether or not we had a
3656 pointer-to-member, we now know. */
3657 if (qualifying_class)
3658 {
3659 bool done;
3660
3661 /* Peek at the next token. */
3662 token = cp_lexer_peek_token (parser->lexer);
3663 done = (token->type != CPP_OPEN_SQUARE
3664 && token->type != CPP_OPEN_PAREN
3665 && token->type != CPP_DOT
3666 && token->type != CPP_DEREF
3667 && token->type != CPP_PLUS_PLUS
3668 && token->type != CPP_MINUS_MINUS);
3669
3670 postfix_expression = finish_qualified_id_expr (qualifying_class,
3671 postfix_expression,
3672 done,
3673 address_p);
3674 if (done)
3675 return postfix_expression;
3676 }
3677
3678 /* Keep looping until the postfix-expression is complete. */
3679 while (true)
3680 {
3681 if (idk == CP_ID_KIND_UNQUALIFIED
3682 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3683 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3684 /* It is not a Koenig lookup function call. */
3685 postfix_expression
3686 = unqualified_name_lookup_error (postfix_expression);
3687
3688 /* Peek at the next token. */
3689 token = cp_lexer_peek_token (parser->lexer);
3690
3691 switch (token->type)
3692 {
3693 case CPP_OPEN_SQUARE:
3694 /* postfix-expression [ expression ] */
3695 {
3696 tree index;
3697
3698 /* Consume the `[' token. */
3699 cp_lexer_consume_token (parser->lexer);
3700 /* Parse the index expression. */
3701 index = cp_parser_expression (parser);
3702 /* Look for the closing `]'. */
3703 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3704
3705 /* Build the ARRAY_REF. */
3706 postfix_expression
3707 = grok_array_decl (postfix_expression, index);
3708 idk = CP_ID_KIND_NONE;
3709 /* Array references are not permitted in
3710 constant-expressions (but they are allowed
3711 in offsetof). */
3712 if (!parser->in_offsetof_p
3713 && cp_parser_non_integral_constant_expression
3714 (parser, "an array reference"))
3715 postfix_expression = error_mark_node;
3716 }
3717 break;
3718
3719 case CPP_OPEN_PAREN:
3720 /* postfix-expression ( expression-list [opt] ) */
3721 {
3722 bool koenig_p;
3723 tree args = (cp_parser_parenthesized_expression_list
3724 (parser, false, /*non_constant_p=*/NULL));
3725
3726 if (args == error_mark_node)
3727 {
3728 postfix_expression = error_mark_node;
3729 break;
3730 }
3731
3732 /* Function calls are not permitted in
3733 constant-expressions. */
3734 if (cp_parser_non_integral_constant_expression (parser,
3735 "a function call"))
3736 {
3737 postfix_expression = error_mark_node;
3738 break;
3739 }
3740
3741 koenig_p = false;
3742 if (idk == CP_ID_KIND_UNQUALIFIED)
3743 {
3744 /* We do not perform argument-dependent lookup if
3745 normal lookup finds a non-function, in accordance
3746 with the expected resolution of DR 218. */
3747 if (args
3748 && (is_overloaded_fn (postfix_expression)
3749 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3750 {
3751 koenig_p = true;
3752 postfix_expression
3753 = perform_koenig_lookup (postfix_expression, args);
3754 }
3755 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3756 postfix_expression
3757 = unqualified_fn_lookup_error (postfix_expression);
3758 }
3759
3760 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3761 {
3762 tree instance = TREE_OPERAND (postfix_expression, 0);
3763 tree fn = TREE_OPERAND (postfix_expression, 1);
3764
3765 if (processing_template_decl
3766 && (type_dependent_expression_p (instance)
3767 || (!BASELINK_P (fn)
3768 && TREE_CODE (fn) != FIELD_DECL)
3769 || type_dependent_expression_p (fn)
3770 || any_type_dependent_arguments_p (args)))
3771 {
3772 postfix_expression
3773 = build_min_nt (CALL_EXPR, postfix_expression, args);
3774 break;
3775 }
3776
3777 if (BASELINK_P (fn))
3778 postfix_expression
3779 = (build_new_method_call
3780 (instance, fn, args, NULL_TREE,
3781 (idk == CP_ID_KIND_QUALIFIED
3782 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3783 else
3784 postfix_expression
3785 = finish_call_expr (postfix_expression, args,
3786 /*disallow_virtual=*/false,
3787 /*koenig_p=*/false);
3788 }
3789 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3790 || TREE_CODE (postfix_expression) == MEMBER_REF
3791 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3792 postfix_expression = (build_offset_ref_call_from_tree
3793 (postfix_expression, args));
3794 else if (idk == CP_ID_KIND_QUALIFIED)
3795 /* A call to a static class member, or a namespace-scope
3796 function. */
3797 postfix_expression
3798 = finish_call_expr (postfix_expression, args,
3799 /*disallow_virtual=*/true,
3800 koenig_p);
3801 else
3802 /* All other function calls. */
3803 postfix_expression
3804 = finish_call_expr (postfix_expression, args,
3805 /*disallow_virtual=*/false,
3806 koenig_p);
3807
3808 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3809 idk = CP_ID_KIND_NONE;
3810 }
3811 break;
3812
3813 case CPP_DOT:
3814 case CPP_DEREF:
3815 /* postfix-expression . template [opt] id-expression
3816 postfix-expression . pseudo-destructor-name
3817 postfix-expression -> template [opt] id-expression
3818 postfix-expression -> pseudo-destructor-name */
3819 {
3820 tree name;
3821 bool dependent_p;
3822 bool template_p;
3823 tree scope = NULL_TREE;
3824 enum cpp_ttype token_type = token->type;
3825
3826 /* If this is a `->' operator, dereference the pointer. */
3827 if (token->type == CPP_DEREF)
3828 postfix_expression = build_x_arrow (postfix_expression);
3829 /* Check to see whether or not the expression is
3830 type-dependent. */
3831 dependent_p = type_dependent_expression_p (postfix_expression);
3832 /* The identifier following the `->' or `.' is not
3833 qualified. */
3834 parser->scope = NULL_TREE;
3835 parser->qualifying_scope = NULL_TREE;
3836 parser->object_scope = NULL_TREE;
3837 idk = CP_ID_KIND_NONE;
3838 /* Enter the scope corresponding to the type of the object
3839 given by the POSTFIX_EXPRESSION. */
3840 if (!dependent_p
3841 && TREE_TYPE (postfix_expression) != NULL_TREE)
3842 {
3843 scope = TREE_TYPE (postfix_expression);
3844 /* According to the standard, no expression should
3845 ever have reference type. Unfortunately, we do not
3846 currently match the standard in this respect in
3847 that our internal representation of an expression
3848 may have reference type even when the standard says
3849 it does not. Therefore, we have to manually obtain
3850 the underlying type here. */
3851 scope = non_reference (scope);
3852 /* The type of the POSTFIX_EXPRESSION must be
3853 complete. */
3854 scope = complete_type_or_else (scope, NULL_TREE);
3855 /* Let the name lookup machinery know that we are
3856 processing a class member access expression. */
3857 parser->context->object_type = scope;
3858 /* If something went wrong, we want to be able to
3859 discern that case, as opposed to the case where
3860 there was no SCOPE due to the type of expression
3861 being dependent. */
3862 if (!scope)
3863 scope = error_mark_node;
3864 /* If the SCOPE was erroneous, make the various
3865 semantic analysis functions exit quickly -- and
3866 without issuing additional error messages. */
3867 if (scope == error_mark_node)
3868 postfix_expression = error_mark_node;
3869 }
3870
3871 /* Consume the `.' or `->' operator. */
3872 cp_lexer_consume_token (parser->lexer);
3873 /* If the SCOPE is not a scalar type, we are looking at an
3874 ordinary class member access expression, rather than a
3875 pseudo-destructor-name. */
3876 if (!scope || !SCALAR_TYPE_P (scope))
3877 {
3878 template_p = cp_parser_optional_template_keyword (parser);
3879 /* Parse the id-expression. */
3880 name = cp_parser_id_expression (parser,
3881 template_p,
3882 /*check_dependency_p=*/true,
3883 /*template_p=*/NULL,
3884 /*declarator_p=*/false);
3885 /* In general, build a SCOPE_REF if the member name is
3886 qualified. However, if the name was not dependent
3887 and has already been resolved; there is no need to
3888 build the SCOPE_REF. For example;
3889
3890 struct X { void f(); };
3891 template <typename T> void f(T* t) { t->X::f(); }
3892
3893 Even though "t" is dependent, "X::f" is not and has
3894 been resolved to a BASELINK; there is no need to
3895 include scope information. */
3896
3897 /* But we do need to remember that there was an explicit
3898 scope for virtual function calls. */
3899 if (parser->scope)
3900 idk = CP_ID_KIND_QUALIFIED;
3901
3902 if (name != error_mark_node
3903 && !BASELINK_P (name)
3904 && parser->scope)
3905 {
3906 name = build_nt (SCOPE_REF, parser->scope, name);
3907 parser->scope = NULL_TREE;
3908 parser->qualifying_scope = NULL_TREE;
3909 parser->object_scope = NULL_TREE;
3910 }
3911 if (scope && name && BASELINK_P (name))
3912 adjust_result_of_qualified_name_lookup
3913 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3914 postfix_expression
3915 = finish_class_member_access_expr (postfix_expression, name);
3916 }
3917 /* Otherwise, try the pseudo-destructor-name production. */
3918 else
3919 {
3920 tree s = NULL_TREE;
3921 tree type;
3922
3923 /* Parse the pseudo-destructor-name. */
3924 cp_parser_pseudo_destructor_name (parser, &s, &type);
3925 /* Form the call. */
3926 postfix_expression
3927 = finish_pseudo_destructor_expr (postfix_expression,
3928 s, TREE_TYPE (type));
3929 }
3930
3931 /* We no longer need to look up names in the scope of the
3932 object on the left-hand side of the `.' or `->'
3933 operator. */
3934 parser->context->object_type = NULL_TREE;
3935 /* These operators may not appear in constant-expressions. */
3936 if (/* The "->" operator is allowed in the implementation
3937 of "offsetof". The "." operator may appear in the
3938 name of the member. */
3939 !parser->in_offsetof_p
3940 && (cp_parser_non_integral_constant_expression
3941 (parser,
3942 token_type == CPP_DEREF ? "'->'" : "`.'")))
3943 postfix_expression = error_mark_node;
3944 }
3945 break;
3946
3947 case CPP_PLUS_PLUS:
3948 /* postfix-expression ++ */
3949 /* Consume the `++' token. */
3950 cp_lexer_consume_token (parser->lexer);
3951 /* Generate a representation for the complete expression. */
3952 postfix_expression
3953 = finish_increment_expr (postfix_expression,
3954 POSTINCREMENT_EXPR);
3955 /* Increments may not appear in constant-expressions. */
3956 if (cp_parser_non_integral_constant_expression (parser,
3957 "an increment"))
3958 postfix_expression = error_mark_node;
3959 idk = CP_ID_KIND_NONE;
3960 break;
3961
3962 case CPP_MINUS_MINUS:
3963 /* postfix-expression -- */
3964 /* Consume the `--' token. */
3965 cp_lexer_consume_token (parser->lexer);
3966 /* Generate a representation for the complete expression. */
3967 postfix_expression
3968 = finish_increment_expr (postfix_expression,
3969 POSTDECREMENT_EXPR);
3970 /* Decrements may not appear in constant-expressions. */
3971 if (cp_parser_non_integral_constant_expression (parser,
3972 "a decrement"))
3973 postfix_expression = error_mark_node;
3974 idk = CP_ID_KIND_NONE;
3975 break;
3976
3977 default:
3978 return postfix_expression;
3979 }
3980 }
3981
3982 /* We should never get here. */
3983 abort ();
3984 return error_mark_node;
3985 }
3986
3987 /* Parse a parenthesized expression-list.
3988
3989 expression-list:
3990 assignment-expression
3991 expression-list, assignment-expression
3992
3993 attribute-list:
3994 expression-list
3995 identifier
3996 identifier, expression-list
3997
3998 Returns a TREE_LIST. The TREE_VALUE of each node is a
3999 representation of an assignment-expression. Note that a TREE_LIST
4000 is returned even if there is only a single expression in the list.
4001 error_mark_node is returned if the ( and or ) are
4002 missing. NULL_TREE is returned on no expressions. The parentheses
4003 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4004 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4005 indicates whether or not all of the expressions in the list were
4006 constant. */
4007
4008 static tree
cp_parser_parenthesized_expression_list(cp_parser * parser,bool is_attribute_list,bool * non_constant_p)4009 cp_parser_parenthesized_expression_list (cp_parser* parser,
4010 bool is_attribute_list,
4011 bool *non_constant_p)
4012 {
4013 tree expression_list = NULL_TREE;
4014 tree identifier = NULL_TREE;
4015
4016 /* Assume all the expressions will be constant. */
4017 if (non_constant_p)
4018 *non_constant_p = false;
4019
4020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4021 return error_mark_node;
4022
4023 /* Consume expressions until there are no more. */
4024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4025 while (true)
4026 {
4027 tree expr;
4028
4029 /* At the beginning of attribute lists, check to see if the
4030 next token is an identifier. */
4031 if (is_attribute_list
4032 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4033 {
4034 cp_token *token;
4035
4036 /* Consume the identifier. */
4037 token = cp_lexer_consume_token (parser->lexer);
4038 /* Save the identifier. */
4039 identifier = token->value;
4040 }
4041 else
4042 {
4043 /* Parse the next assignment-expression. */
4044 if (non_constant_p)
4045 {
4046 bool expr_non_constant_p;
4047 expr = (cp_parser_constant_expression
4048 (parser, /*allow_non_constant_p=*/true,
4049 &expr_non_constant_p));
4050 if (expr_non_constant_p)
4051 *non_constant_p = true;
4052 }
4053 else
4054 expr = cp_parser_assignment_expression (parser);
4055
4056 /* Add it to the list. We add error_mark_node
4057 expressions to the list, so that we can still tell if
4058 the correct form for a parenthesized expression-list
4059 is found. That gives better errors. */
4060 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4061
4062 if (expr == error_mark_node)
4063 goto skip_comma;
4064 }
4065
4066 /* After the first item, attribute lists look the same as
4067 expression lists. */
4068 is_attribute_list = false;
4069
4070 get_comma:;
4071 /* If the next token isn't a `,', then we are done. */
4072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4073 break;
4074
4075 /* Otherwise, consume the `,' and keep going. */
4076 cp_lexer_consume_token (parser->lexer);
4077 }
4078
4079 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4080 {
4081 int ending;
4082
4083 skip_comma:;
4084 /* We try and resync to an unnested comma, as that will give the
4085 user better diagnostics. */
4086 ending = cp_parser_skip_to_closing_parenthesis (parser,
4087 /*recovering=*/true,
4088 /*or_comma=*/true,
4089 /*consume_paren=*/true);
4090 if (ending < 0)
4091 goto get_comma;
4092 if (!ending)
4093 return error_mark_node;
4094 }
4095
4096 /* We built up the list in reverse order so we must reverse it now. */
4097 expression_list = nreverse (expression_list);
4098 if (identifier)
4099 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4100
4101 return expression_list;
4102 }
4103
4104 /* Parse a pseudo-destructor-name.
4105
4106 pseudo-destructor-name:
4107 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4108 :: [opt] nested-name-specifier template template-id :: ~ type-name
4109 :: [opt] nested-name-specifier [opt] ~ type-name
4110
4111 If either of the first two productions is used, sets *SCOPE to the
4112 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4113 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4114 or ERROR_MARK_NODE if the parse fails. */
4115
4116 static void
cp_parser_pseudo_destructor_name(cp_parser * parser,tree * scope,tree * type)4117 cp_parser_pseudo_destructor_name (cp_parser* parser,
4118 tree* scope,
4119 tree* type)
4120 {
4121 bool nested_name_specifier_p;
4122
4123 /* Look for the optional `::' operator. */
4124 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4125 /* Look for the optional nested-name-specifier. */
4126 nested_name_specifier_p
4127 = (cp_parser_nested_name_specifier_opt (parser,
4128 /*typename_keyword_p=*/false,
4129 /*check_dependency_p=*/true,
4130 /*type_p=*/false,
4131 /*is_declaration=*/true)
4132 != NULL_TREE);
4133 /* Now, if we saw a nested-name-specifier, we might be doing the
4134 second production. */
4135 if (nested_name_specifier_p
4136 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4137 {
4138 /* Consume the `template' keyword. */
4139 cp_lexer_consume_token (parser->lexer);
4140 /* Parse the template-id. */
4141 cp_parser_template_id (parser,
4142 /*template_keyword_p=*/true,
4143 /*check_dependency_p=*/false,
4144 /*is_declaration=*/true);
4145 /* Look for the `::' token. */
4146 cp_parser_require (parser, CPP_SCOPE, "`::'");
4147 }
4148 /* If the next token is not a `~', then there might be some
4149 additional qualification. */
4150 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4151 {
4152 /* Look for the type-name. */
4153 *scope = TREE_TYPE (cp_parser_type_name (parser));
4154
4155 /* If we didn't get an aggregate type, or we don't have ::~,
4156 then something has gone wrong. Since the only caller of this
4157 function is looking for something after `.' or `->' after a
4158 scalar type, most likely the program is trying to get a
4159 member of a non-aggregate type. */
4160 if (*scope == error_mark_node
4161 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4162 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4163 {
4164 cp_parser_error (parser, "request for member of non-aggregate type");
4165 *type = error_mark_node;
4166 return;
4167 }
4168
4169 /* Look for the `::' token. */
4170 cp_parser_require (parser, CPP_SCOPE, "`::'");
4171 }
4172 else
4173 *scope = NULL_TREE;
4174
4175 /* Look for the `~'. */
4176 cp_parser_require (parser, CPP_COMPL, "`~'");
4177 /* Look for the type-name again. We are not responsible for
4178 checking that it matches the first type-name. */
4179 *type = cp_parser_type_name (parser);
4180 }
4181
4182 /* Parse a unary-expression.
4183
4184 unary-expression:
4185 postfix-expression
4186 ++ cast-expression
4187 -- cast-expression
4188 unary-operator cast-expression
4189 sizeof unary-expression
4190 sizeof ( type-id )
4191 new-expression
4192 delete-expression
4193
4194 GNU Extensions:
4195
4196 unary-expression:
4197 __extension__ cast-expression
4198 __alignof__ unary-expression
4199 __alignof__ ( type-id )
4200 __real__ cast-expression
4201 __imag__ cast-expression
4202 && identifier
4203
4204 ADDRESS_P is true iff the unary-expression is appearing as the
4205 operand of the `&' operator.
4206
4207 Returns a representation of the expression. */
4208
4209 static tree
cp_parser_unary_expression(cp_parser * parser,bool address_p)4210 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4211 {
4212 cp_token *token;
4213 enum tree_code unary_operator;
4214
4215 /* Peek at the next token. */
4216 token = cp_lexer_peek_token (parser->lexer);
4217 /* Some keywords give away the kind of expression. */
4218 if (token->type == CPP_KEYWORD)
4219 {
4220 enum rid keyword = token->keyword;
4221
4222 switch (keyword)
4223 {
4224 case RID_ALIGNOF:
4225 case RID_SIZEOF:
4226 {
4227 tree operand;
4228 enum tree_code op;
4229
4230 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4231 /* Consume the token. */
4232 cp_lexer_consume_token (parser->lexer);
4233 /* Parse the operand. */
4234 operand = cp_parser_sizeof_operand (parser, keyword);
4235
4236 if (TYPE_P (operand))
4237 return cxx_sizeof_or_alignof_type (operand, op, true);
4238 else
4239 return cxx_sizeof_or_alignof_expr (operand, op);
4240 }
4241
4242 case RID_NEW:
4243 return cp_parser_new_expression (parser);
4244
4245 case RID_DELETE:
4246 return cp_parser_delete_expression (parser);
4247
4248 case RID_EXTENSION:
4249 {
4250 /* The saved value of the PEDANTIC flag. */
4251 int saved_pedantic;
4252 tree expr;
4253
4254 /* Save away the PEDANTIC flag. */
4255 cp_parser_extension_opt (parser, &saved_pedantic);
4256 /* Parse the cast-expression. */
4257 expr = cp_parser_simple_cast_expression (parser);
4258 /* Restore the PEDANTIC flag. */
4259 pedantic = saved_pedantic;
4260
4261 return expr;
4262 }
4263
4264 case RID_REALPART:
4265 case RID_IMAGPART:
4266 {
4267 tree expression;
4268
4269 /* Consume the `__real__' or `__imag__' token. */
4270 cp_lexer_consume_token (parser->lexer);
4271 /* Parse the cast-expression. */
4272 expression = cp_parser_simple_cast_expression (parser);
4273 /* Create the complete representation. */
4274 return build_x_unary_op ((keyword == RID_REALPART
4275 ? REALPART_EXPR : IMAGPART_EXPR),
4276 expression);
4277 }
4278 break;
4279
4280 default:
4281 break;
4282 }
4283 }
4284
4285 /* Look for the `:: new' and `:: delete', which also signal the
4286 beginning of a new-expression, or delete-expression,
4287 respectively. If the next token is `::', then it might be one of
4288 these. */
4289 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4290 {
4291 enum rid keyword;
4292
4293 /* See if the token after the `::' is one of the keywords in
4294 which we're interested. */
4295 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4296 /* If it's `new', we have a new-expression. */
4297 if (keyword == RID_NEW)
4298 return cp_parser_new_expression (parser);
4299 /* Similarly, for `delete'. */
4300 else if (keyword == RID_DELETE)
4301 return cp_parser_delete_expression (parser);
4302 }
4303
4304 /* Look for a unary operator. */
4305 unary_operator = cp_parser_unary_operator (token);
4306 /* The `++' and `--' operators can be handled similarly, even though
4307 they are not technically unary-operators in the grammar. */
4308 if (unary_operator == ERROR_MARK)
4309 {
4310 if (token->type == CPP_PLUS_PLUS)
4311 unary_operator = PREINCREMENT_EXPR;
4312 else if (token->type == CPP_MINUS_MINUS)
4313 unary_operator = PREDECREMENT_EXPR;
4314 /* Handle the GNU address-of-label extension. */
4315 else if (cp_parser_allow_gnu_extensions_p (parser)
4316 && token->type == CPP_AND_AND)
4317 {
4318 tree identifier;
4319
4320 /* Consume the '&&' token. */
4321 cp_lexer_consume_token (parser->lexer);
4322 /* Look for the identifier. */
4323 identifier = cp_parser_identifier (parser);
4324 /* Create an expression representing the address. */
4325 return finish_label_address_expr (identifier);
4326 }
4327 }
4328 if (unary_operator != ERROR_MARK)
4329 {
4330 tree cast_expression;
4331 tree expression = error_mark_node;
4332 const char *non_constant_p = NULL;
4333
4334 /* Consume the operator token. */
4335 token = cp_lexer_consume_token (parser->lexer);
4336 /* Parse the cast-expression. */
4337 cast_expression
4338 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4339 /* Now, build an appropriate representation. */
4340 switch (unary_operator)
4341 {
4342 case INDIRECT_REF:
4343 non_constant_p = "`*'";
4344 expression = build_x_indirect_ref (cast_expression, "unary *");
4345 break;
4346
4347 case ADDR_EXPR:
4348 /* The "&" operator is allowed in the implementation of
4349 "offsetof". */
4350 if (!parser->in_offsetof_p)
4351 non_constant_p = "`&'";
4352 /* Fall through. */
4353 case BIT_NOT_EXPR:
4354 expression = build_x_unary_op (unary_operator, cast_expression);
4355 break;
4356
4357 case PREINCREMENT_EXPR:
4358 case PREDECREMENT_EXPR:
4359 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4360 ? "`++'" : "`--'");
4361 /* Fall through. */
4362 case CONVERT_EXPR:
4363 case NEGATE_EXPR:
4364 case TRUTH_NOT_EXPR:
4365 expression = finish_unary_op_expr (unary_operator, cast_expression);
4366 break;
4367
4368 default:
4369 abort ();
4370 }
4371
4372 if (non_constant_p
4373 && cp_parser_non_integral_constant_expression (parser,
4374 non_constant_p))
4375 expression = error_mark_node;
4376
4377 return expression;
4378 }
4379
4380 return cp_parser_postfix_expression (parser, address_p);
4381 }
4382
4383 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4384 unary-operator, the corresponding tree code is returned. */
4385
4386 static enum tree_code
cp_parser_unary_operator(cp_token * token)4387 cp_parser_unary_operator (cp_token* token)
4388 {
4389 switch (token->type)
4390 {
4391 case CPP_MULT:
4392 return INDIRECT_REF;
4393
4394 case CPP_AND:
4395 return ADDR_EXPR;
4396
4397 case CPP_PLUS:
4398 return CONVERT_EXPR;
4399
4400 case CPP_MINUS:
4401 return NEGATE_EXPR;
4402
4403 case CPP_NOT:
4404 return TRUTH_NOT_EXPR;
4405
4406 case CPP_COMPL:
4407 return BIT_NOT_EXPR;
4408
4409 default:
4410 return ERROR_MARK;
4411 }
4412 }
4413
4414 /* Parse a new-expression.
4415
4416 new-expression:
4417 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4418 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4419
4420 Returns a representation of the expression. */
4421
4422 static tree
cp_parser_new_expression(cp_parser * parser)4423 cp_parser_new_expression (cp_parser* parser)
4424 {
4425 bool global_scope_p;
4426 tree placement;
4427 tree type;
4428 tree initializer;
4429
4430 /* Look for the optional `::' operator. */
4431 global_scope_p
4432 = (cp_parser_global_scope_opt (parser,
4433 /*current_scope_valid_p=*/false)
4434 != NULL_TREE);
4435 /* Look for the `new' operator. */
4436 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4437 /* There's no easy way to tell a new-placement from the
4438 `( type-id )' construct. */
4439 cp_parser_parse_tentatively (parser);
4440 /* Look for a new-placement. */
4441 placement = cp_parser_new_placement (parser);
4442 /* If that didn't work out, there's no new-placement. */
4443 if (!cp_parser_parse_definitely (parser))
4444 placement = NULL_TREE;
4445
4446 /* If the next token is a `(', then we have a parenthesized
4447 type-id. */
4448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4449 {
4450 /* Consume the `('. */
4451 cp_lexer_consume_token (parser->lexer);
4452 /* Parse the type-id. */
4453 type = cp_parser_type_id (parser);
4454 /* Look for the closing `)'. */
4455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4456 /* There should not be a direct-new-declarator in this production,
4457 but GCC used to allowed this, so we check and emit a sensible error
4458 message for this case. */
4459 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4460 {
4461 error ("array bound forbidden after parenthesized type-id");
4462 inform ("try removing the parentheses around the type-id");
4463 cp_parser_direct_new_declarator (parser);
4464 }
4465 }
4466 /* Otherwise, there must be a new-type-id. */
4467 else
4468 type = cp_parser_new_type_id (parser);
4469
4470 /* If the next token is a `(', then we have a new-initializer. */
4471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4472 initializer = cp_parser_new_initializer (parser);
4473 else
4474 initializer = NULL_TREE;
4475
4476 /* A new-expression may not appear in an integral constant
4477 expression. */
4478 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4479 return error_mark_node;
4480
4481 /* Create a representation of the new-expression. */
4482 return build_new (placement, type, initializer, global_scope_p);
4483 }
4484
4485 /* Parse a new-placement.
4486
4487 new-placement:
4488 ( expression-list )
4489
4490 Returns the same representation as for an expression-list. */
4491
4492 static tree
cp_parser_new_placement(cp_parser * parser)4493 cp_parser_new_placement (cp_parser* parser)
4494 {
4495 tree expression_list;
4496
4497 /* Parse the expression-list. */
4498 expression_list = (cp_parser_parenthesized_expression_list
4499 (parser, false, /*non_constant_p=*/NULL));
4500
4501 return expression_list;
4502 }
4503
4504 /* Parse a new-type-id.
4505
4506 new-type-id:
4507 type-specifier-seq new-declarator [opt]
4508
4509 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4510 and whose TREE_VALUE is the new-declarator. */
4511
4512 static tree
cp_parser_new_type_id(cp_parser * parser)4513 cp_parser_new_type_id (cp_parser* parser)
4514 {
4515 tree type_specifier_seq;
4516 tree declarator;
4517 const char *saved_message;
4518
4519 /* The type-specifier sequence must not contain type definitions.
4520 (It cannot contain declarations of new types either, but if they
4521 are not definitions we will catch that because they are not
4522 complete.) */
4523 saved_message = parser->type_definition_forbidden_message;
4524 parser->type_definition_forbidden_message
4525 = "types may not be defined in a new-type-id";
4526 /* Parse the type-specifier-seq. */
4527 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4528 /* Restore the old message. */
4529 parser->type_definition_forbidden_message = saved_message;
4530 /* Parse the new-declarator. */
4531 declarator = cp_parser_new_declarator_opt (parser);
4532
4533 return build_tree_list (type_specifier_seq, declarator);
4534 }
4535
4536 /* Parse an (optional) new-declarator.
4537
4538 new-declarator:
4539 ptr-operator new-declarator [opt]
4540 direct-new-declarator
4541
4542 Returns a representation of the declarator. See
4543 cp_parser_declarator for the representations used. */
4544
4545 static tree
cp_parser_new_declarator_opt(cp_parser * parser)4546 cp_parser_new_declarator_opt (cp_parser* parser)
4547 {
4548 enum tree_code code;
4549 tree type;
4550 tree cv_qualifier_seq;
4551
4552 /* We don't know if there's a ptr-operator next, or not. */
4553 cp_parser_parse_tentatively (parser);
4554 /* Look for a ptr-operator. */
4555 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4556 /* If that worked, look for more new-declarators. */
4557 if (cp_parser_parse_definitely (parser))
4558 {
4559 tree declarator;
4560
4561 /* Parse another optional declarator. */
4562 declarator = cp_parser_new_declarator_opt (parser);
4563
4564 /* Create the representation of the declarator. */
4565 if (code == INDIRECT_REF)
4566 declarator = make_pointer_declarator (cv_qualifier_seq,
4567 declarator);
4568 else
4569 declarator = make_reference_declarator (cv_qualifier_seq,
4570 declarator);
4571
4572 /* Handle the pointer-to-member case. */
4573 if (type)
4574 declarator = build_nt (SCOPE_REF, type, declarator);
4575
4576 return declarator;
4577 }
4578
4579 /* If the next token is a `[', there is a direct-new-declarator. */
4580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4581 return cp_parser_direct_new_declarator (parser);
4582
4583 return NULL_TREE;
4584 }
4585
4586 /* Parse a direct-new-declarator.
4587
4588 direct-new-declarator:
4589 [ expression ]
4590 direct-new-declarator [constant-expression]
4591
4592 Returns an ARRAY_REF, following the same conventions as are
4593 documented for cp_parser_direct_declarator. */
4594
4595 static tree
cp_parser_direct_new_declarator(cp_parser * parser)4596 cp_parser_direct_new_declarator (cp_parser* parser)
4597 {
4598 tree declarator = NULL_TREE;
4599
4600 while (true)
4601 {
4602 tree expression;
4603
4604 /* Look for the opening `['. */
4605 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4606 /* The first expression is not required to be constant. */
4607 if (!declarator)
4608 {
4609 expression = cp_parser_expression (parser);
4610 /* The standard requires that the expression have integral
4611 type. DR 74 adds enumeration types. We believe that the
4612 real intent is that these expressions be handled like the
4613 expression in a `switch' condition, which also allows
4614 classes with a single conversion to integral or
4615 enumeration type. */
4616 if (!processing_template_decl)
4617 {
4618 expression
4619 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4620 expression,
4621 /*complain=*/true);
4622 if (!expression)
4623 {
4624 error ("expression in new-declarator must have integral or enumeration type");
4625 expression = error_mark_node;
4626 }
4627 }
4628 }
4629 /* But all the other expressions must be. */
4630 else
4631 expression
4632 = cp_parser_constant_expression (parser,
4633 /*allow_non_constant=*/false,
4634 NULL);
4635 /* Look for the closing `]'. */
4636 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4637
4638 /* Add this bound to the declarator. */
4639 declarator = build_nt (ARRAY_REF, declarator, expression);
4640
4641 /* If the next token is not a `[', then there are no more
4642 bounds. */
4643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4644 break;
4645 }
4646
4647 return declarator;
4648 }
4649
4650 /* Parse a new-initializer.
4651
4652 new-initializer:
4653 ( expression-list [opt] )
4654
4655 Returns a representation of the expression-list. If there is no
4656 expression-list, VOID_ZERO_NODE is returned. */
4657
4658 static tree
cp_parser_new_initializer(cp_parser * parser)4659 cp_parser_new_initializer (cp_parser* parser)
4660 {
4661 tree expression_list;
4662
4663 expression_list = (cp_parser_parenthesized_expression_list
4664 (parser, false, /*non_constant_p=*/NULL));
4665 if (!expression_list)
4666 expression_list = void_zero_node;
4667
4668 return expression_list;
4669 }
4670
4671 /* Parse a delete-expression.
4672
4673 delete-expression:
4674 :: [opt] delete cast-expression
4675 :: [opt] delete [ ] cast-expression
4676
4677 Returns a representation of the expression. */
4678
4679 static tree
cp_parser_delete_expression(cp_parser * parser)4680 cp_parser_delete_expression (cp_parser* parser)
4681 {
4682 bool global_scope_p;
4683 bool array_p;
4684 tree expression;
4685
4686 /* Look for the optional `::' operator. */
4687 global_scope_p
4688 = (cp_parser_global_scope_opt (parser,
4689 /*current_scope_valid_p=*/false)
4690 != NULL_TREE);
4691 /* Look for the `delete' keyword. */
4692 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4693 /* See if the array syntax is in use. */
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4695 {
4696 /* Consume the `[' token. */
4697 cp_lexer_consume_token (parser->lexer);
4698 /* Look for the `]' token. */
4699 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4700 /* Remember that this is the `[]' construct. */
4701 array_p = true;
4702 }
4703 else
4704 array_p = false;
4705
4706 /* Parse the cast-expression. */
4707 expression = cp_parser_simple_cast_expression (parser);
4708
4709 /* A delete-expression may not appear in an integral constant
4710 expression. */
4711 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4712 return error_mark_node;
4713
4714 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4715 }
4716
4717 /* Parse a cast-expression.
4718
4719 cast-expression:
4720 unary-expression
4721 ( type-id ) cast-expression
4722
4723 Returns a representation of the expression. */
4724
4725 static tree
cp_parser_cast_expression(cp_parser * parser,bool address_p)4726 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4727 {
4728 /* If it's a `(', then we might be looking at a cast. */
4729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4730 {
4731 tree type = NULL_TREE;
4732 tree expr = NULL_TREE;
4733 bool compound_literal_p;
4734 const char *saved_message;
4735
4736 /* There's no way to know yet whether or not this is a cast.
4737 For example, `(int (3))' is a unary-expression, while `(int)
4738 3' is a cast. So, we resort to parsing tentatively. */
4739 cp_parser_parse_tentatively (parser);
4740 /* Types may not be defined in a cast. */
4741 saved_message = parser->type_definition_forbidden_message;
4742 parser->type_definition_forbidden_message
4743 = "types may not be defined in casts";
4744 /* Consume the `('. */
4745 cp_lexer_consume_token (parser->lexer);
4746 /* A very tricky bit is that `(struct S) { 3 }' is a
4747 compound-literal (which we permit in C++ as an extension).
4748 But, that construct is not a cast-expression -- it is a
4749 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4750 is legal; if the compound-literal were a cast-expression,
4751 you'd need an extra set of parentheses.) But, if we parse
4752 the type-id, and it happens to be a class-specifier, then we
4753 will commit to the parse at that point, because we cannot
4754 undo the action that is done when creating a new class. So,
4755 then we cannot back up and do a postfix-expression.
4756
4757 Therefore, we scan ahead to the closing `)', and check to see
4758 if the token after the `)' is a `{'. If so, we are not
4759 looking at a cast-expression.
4760
4761 Save tokens so that we can put them back. */
4762 cp_lexer_save_tokens (parser->lexer);
4763 /* Skip tokens until the next token is a closing parenthesis.
4764 If we find the closing `)', and the next token is a `{', then
4765 we are looking at a compound-literal. */
4766 compound_literal_p
4767 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4768 /*consume_paren=*/true)
4769 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4770 /* Roll back the tokens we skipped. */
4771 cp_lexer_rollback_tokens (parser->lexer);
4772 /* If we were looking at a compound-literal, simulate an error
4773 so that the call to cp_parser_parse_definitely below will
4774 fail. */
4775 if (compound_literal_p)
4776 cp_parser_simulate_error (parser);
4777 else
4778 {
4779 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4780 parser->in_type_id_in_expr_p = true;
4781 /* Look for the type-id. */
4782 type = cp_parser_type_id (parser);
4783 /* Look for the closing `)'. */
4784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4785 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4786 }
4787
4788 /* Restore the saved message. */
4789 parser->type_definition_forbidden_message = saved_message;
4790
4791 /* If ok so far, parse the dependent expression. We cannot be
4792 sure it is a cast. Consider `(T ())'. It is a parenthesized
4793 ctor of T, but looks like a cast to function returning T
4794 without a dependent expression. */
4795 if (!cp_parser_error_occurred (parser))
4796 expr = cp_parser_simple_cast_expression (parser);
4797
4798 if (cp_parser_parse_definitely (parser))
4799 {
4800 /* Warn about old-style casts, if so requested. */
4801 if (warn_old_style_cast
4802 && !in_system_header
4803 && !VOID_TYPE_P (type)
4804 && current_lang_name != lang_name_c)
4805 warning ("use of old-style cast");
4806
4807 /* Only type conversions to integral or enumeration types
4808 can be used in constant-expressions. */
4809 if (parser->integral_constant_expression_p
4810 && !dependent_type_p (type)
4811 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4812 && (cp_parser_non_integral_constant_expression
4813 (parser,
4814 "a casts to a type other than an integral or "
4815 "enumeration type")))
4816 return error_mark_node;
4817
4818 /* Perform the cast. */
4819 expr = build_c_cast (type, expr);
4820 return expr;
4821 }
4822 }
4823
4824 /* If we get here, then it's not a cast, so it must be a
4825 unary-expression. */
4826 return cp_parser_unary_expression (parser, address_p);
4827 }
4828
4829 /* Parse a pm-expression.
4830
4831 pm-expression:
4832 cast-expression
4833 pm-expression .* cast-expression
4834 pm-expression ->* cast-expression
4835
4836 Returns a representation of the expression. */
4837
4838 static tree
cp_parser_pm_expression(cp_parser * parser)4839 cp_parser_pm_expression (cp_parser* parser)
4840 {
4841 static const cp_parser_token_tree_map map = {
4842 { CPP_DEREF_STAR, MEMBER_REF },
4843 { CPP_DOT_STAR, DOTSTAR_EXPR },
4844 { CPP_EOF, ERROR_MARK }
4845 };
4846
4847 return cp_parser_binary_expression (parser, map,
4848 cp_parser_simple_cast_expression);
4849 }
4850
4851 /* Parse a multiplicative-expression.
4852
4853 mulitplicative-expression:
4854 pm-expression
4855 multiplicative-expression * pm-expression
4856 multiplicative-expression / pm-expression
4857 multiplicative-expression % pm-expression
4858
4859 Returns a representation of the expression. */
4860
4861 static tree
cp_parser_multiplicative_expression(cp_parser * parser)4862 cp_parser_multiplicative_expression (cp_parser* parser)
4863 {
4864 static const cp_parser_token_tree_map map = {
4865 { CPP_MULT, MULT_EXPR },
4866 { CPP_DIV, TRUNC_DIV_EXPR },
4867 { CPP_MOD, TRUNC_MOD_EXPR },
4868 { CPP_EOF, ERROR_MARK }
4869 };
4870
4871 return cp_parser_binary_expression (parser,
4872 map,
4873 cp_parser_pm_expression);
4874 }
4875
4876 /* Parse an additive-expression.
4877
4878 additive-expression:
4879 multiplicative-expression
4880 additive-expression + multiplicative-expression
4881 additive-expression - multiplicative-expression
4882
4883 Returns a representation of the expression. */
4884
4885 static tree
cp_parser_additive_expression(cp_parser * parser)4886 cp_parser_additive_expression (cp_parser* parser)
4887 {
4888 static const cp_parser_token_tree_map map = {
4889 { CPP_PLUS, PLUS_EXPR },
4890 { CPP_MINUS, MINUS_EXPR },
4891 { CPP_EOF, ERROR_MARK }
4892 };
4893
4894 return cp_parser_binary_expression (parser,
4895 map,
4896 cp_parser_multiplicative_expression);
4897 }
4898
4899 /* Parse a shift-expression.
4900
4901 shift-expression:
4902 additive-expression
4903 shift-expression << additive-expression
4904 shift-expression >> additive-expression
4905
4906 Returns a representation of the expression. */
4907
4908 static tree
cp_parser_shift_expression(cp_parser * parser)4909 cp_parser_shift_expression (cp_parser* parser)
4910 {
4911 static const cp_parser_token_tree_map map = {
4912 { CPP_LSHIFT, LSHIFT_EXPR },
4913 { CPP_RSHIFT, RSHIFT_EXPR },
4914 { CPP_EOF, ERROR_MARK }
4915 };
4916
4917 return cp_parser_binary_expression (parser,
4918 map,
4919 cp_parser_additive_expression);
4920 }
4921
4922 /* Parse a relational-expression.
4923
4924 relational-expression:
4925 shift-expression
4926 relational-expression < shift-expression
4927 relational-expression > shift-expression
4928 relational-expression <= shift-expression
4929 relational-expression >= shift-expression
4930
4931 GNU Extension:
4932
4933 relational-expression:
4934 relational-expression <? shift-expression
4935 relational-expression >? shift-expression
4936
4937 Returns a representation of the expression. */
4938
4939 static tree
cp_parser_relational_expression(cp_parser * parser)4940 cp_parser_relational_expression (cp_parser* parser)
4941 {
4942 static const cp_parser_token_tree_map map = {
4943 { CPP_LESS, LT_EXPR },
4944 { CPP_GREATER, GT_EXPR },
4945 { CPP_LESS_EQ, LE_EXPR },
4946 { CPP_GREATER_EQ, GE_EXPR },
4947 { CPP_MIN, MIN_EXPR },
4948 { CPP_MAX, MAX_EXPR },
4949 { CPP_EOF, ERROR_MARK }
4950 };
4951
4952 return cp_parser_binary_expression (parser,
4953 map,
4954 cp_parser_shift_expression);
4955 }
4956
4957 /* Parse an equality-expression.
4958
4959 equality-expression:
4960 relational-expression
4961 equality-expression == relational-expression
4962 equality-expression != relational-expression
4963
4964 Returns a representation of the expression. */
4965
4966 static tree
cp_parser_equality_expression(cp_parser * parser)4967 cp_parser_equality_expression (cp_parser* parser)
4968 {
4969 static const cp_parser_token_tree_map map = {
4970 { CPP_EQ_EQ, EQ_EXPR },
4971 { CPP_NOT_EQ, NE_EXPR },
4972 { CPP_EOF, ERROR_MARK }
4973 };
4974
4975 return cp_parser_binary_expression (parser,
4976 map,
4977 cp_parser_relational_expression);
4978 }
4979
4980 /* Parse an and-expression.
4981
4982 and-expression:
4983 equality-expression
4984 and-expression & equality-expression
4985
4986 Returns a representation of the expression. */
4987
4988 static tree
cp_parser_and_expression(cp_parser * parser)4989 cp_parser_and_expression (cp_parser* parser)
4990 {
4991 static const cp_parser_token_tree_map map = {
4992 { CPP_AND, BIT_AND_EXPR },
4993 { CPP_EOF, ERROR_MARK }
4994 };
4995
4996 return cp_parser_binary_expression (parser,
4997 map,
4998 cp_parser_equality_expression);
4999 }
5000
5001 /* Parse an exclusive-or-expression.
5002
5003 exclusive-or-expression:
5004 and-expression
5005 exclusive-or-expression ^ and-expression
5006
5007 Returns a representation of the expression. */
5008
5009 static tree
cp_parser_exclusive_or_expression(cp_parser * parser)5010 cp_parser_exclusive_or_expression (cp_parser* parser)
5011 {
5012 static const cp_parser_token_tree_map map = {
5013 { CPP_XOR, BIT_XOR_EXPR },
5014 { CPP_EOF, ERROR_MARK }
5015 };
5016
5017 return cp_parser_binary_expression (parser,
5018 map,
5019 cp_parser_and_expression);
5020 }
5021
5022
5023 /* Parse an inclusive-or-expression.
5024
5025 inclusive-or-expression:
5026 exclusive-or-expression
5027 inclusive-or-expression | exclusive-or-expression
5028
5029 Returns a representation of the expression. */
5030
5031 static tree
cp_parser_inclusive_or_expression(cp_parser * parser)5032 cp_parser_inclusive_or_expression (cp_parser* parser)
5033 {
5034 static const cp_parser_token_tree_map map = {
5035 { CPP_OR, BIT_IOR_EXPR },
5036 { CPP_EOF, ERROR_MARK }
5037 };
5038
5039 return cp_parser_binary_expression (parser,
5040 map,
5041 cp_parser_exclusive_or_expression);
5042 }
5043
5044 /* Parse a logical-and-expression.
5045
5046 logical-and-expression:
5047 inclusive-or-expression
5048 logical-and-expression && inclusive-or-expression
5049
5050 Returns a representation of the expression. */
5051
5052 static tree
cp_parser_logical_and_expression(cp_parser * parser)5053 cp_parser_logical_and_expression (cp_parser* parser)
5054 {
5055 static const cp_parser_token_tree_map map = {
5056 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5057 { CPP_EOF, ERROR_MARK }
5058 };
5059
5060 return cp_parser_binary_expression (parser,
5061 map,
5062 cp_parser_inclusive_or_expression);
5063 }
5064
5065 /* Parse a logical-or-expression.
5066
5067 logical-or-expression:
5068 logical-and-expression
5069 logical-or-expression || logical-and-expression
5070
5071 Returns a representation of the expression. */
5072
5073 static tree
cp_parser_logical_or_expression(cp_parser * parser)5074 cp_parser_logical_or_expression (cp_parser* parser)
5075 {
5076 static const cp_parser_token_tree_map map = {
5077 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5078 { CPP_EOF, ERROR_MARK }
5079 };
5080
5081 return cp_parser_binary_expression (parser,
5082 map,
5083 cp_parser_logical_and_expression);
5084 }
5085
5086 /* Parse the `? expression : assignment-expression' part of a
5087 conditional-expression. The LOGICAL_OR_EXPR is the
5088 logical-or-expression that started the conditional-expression.
5089 Returns a representation of the entire conditional-expression.
5090
5091 This routine is used by cp_parser_assignment_expression.
5092
5093 ? expression : assignment-expression
5094
5095 GNU Extensions:
5096
5097 ? : assignment-expression */
5098
5099 static tree
cp_parser_question_colon_clause(cp_parser * parser,tree logical_or_expr)5100 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5101 {
5102 tree expr;
5103 tree assignment_expr;
5104
5105 /* Consume the `?' token. */
5106 cp_lexer_consume_token (parser->lexer);
5107 if (cp_parser_allow_gnu_extensions_p (parser)
5108 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5109 /* Implicit true clause. */
5110 expr = NULL_TREE;
5111 else
5112 /* Parse the expression. */
5113 expr = cp_parser_expression (parser);
5114
5115 /* The next token should be a `:'. */
5116 cp_parser_require (parser, CPP_COLON, "`:'");
5117 /* Parse the assignment-expression. */
5118 assignment_expr = cp_parser_assignment_expression (parser);
5119
5120 /* Build the conditional-expression. */
5121 return build_x_conditional_expr (logical_or_expr,
5122 expr,
5123 assignment_expr);
5124 }
5125
5126 /* Parse an assignment-expression.
5127
5128 assignment-expression:
5129 conditional-expression
5130 logical-or-expression assignment-operator assignment_expression
5131 throw-expression
5132
5133 Returns a representation for the expression. */
5134
5135 static tree
cp_parser_assignment_expression(cp_parser * parser)5136 cp_parser_assignment_expression (cp_parser* parser)
5137 {
5138 tree expr;
5139
5140 /* If the next token is the `throw' keyword, then we're looking at
5141 a throw-expression. */
5142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5143 expr = cp_parser_throw_expression (parser);
5144 /* Otherwise, it must be that we are looking at a
5145 logical-or-expression. */
5146 else
5147 {
5148 /* Parse the logical-or-expression. */
5149 expr = cp_parser_logical_or_expression (parser);
5150 /* If the next token is a `?' then we're actually looking at a
5151 conditional-expression. */
5152 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5153 return cp_parser_question_colon_clause (parser, expr);
5154 else
5155 {
5156 enum tree_code assignment_operator;
5157
5158 /* If it's an assignment-operator, we're using the second
5159 production. */
5160 assignment_operator
5161 = cp_parser_assignment_operator_opt (parser);
5162 if (assignment_operator != ERROR_MARK)
5163 {
5164 tree rhs;
5165
5166 /* Parse the right-hand side of the assignment. */
5167 rhs = cp_parser_assignment_expression (parser);
5168 /* An assignment may not appear in a
5169 constant-expression. */
5170 if (cp_parser_non_integral_constant_expression (parser,
5171 "an assignment"))
5172 return error_mark_node;
5173 /* Build the assignment expression. */
5174 expr = build_x_modify_expr (expr,
5175 assignment_operator,
5176 rhs);
5177 }
5178 }
5179 }
5180
5181 return expr;
5182 }
5183
5184 /* Parse an (optional) assignment-operator.
5185
5186 assignment-operator: one of
5187 = *= /= %= += -= >>= <<= &= ^= |=
5188
5189 GNU Extension:
5190
5191 assignment-operator: one of
5192 <?= >?=
5193
5194 If the next token is an assignment operator, the corresponding tree
5195 code is returned, and the token is consumed. For example, for
5196 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5197 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5198 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5199 operator, ERROR_MARK is returned. */
5200
5201 static enum tree_code
cp_parser_assignment_operator_opt(cp_parser * parser)5202 cp_parser_assignment_operator_opt (cp_parser* parser)
5203 {
5204 enum tree_code op;
5205 cp_token *token;
5206
5207 /* Peek at the next toen. */
5208 token = cp_lexer_peek_token (parser->lexer);
5209
5210 switch (token->type)
5211 {
5212 case CPP_EQ:
5213 op = NOP_EXPR;
5214 break;
5215
5216 case CPP_MULT_EQ:
5217 op = MULT_EXPR;
5218 break;
5219
5220 case CPP_DIV_EQ:
5221 op = TRUNC_DIV_EXPR;
5222 break;
5223
5224 case CPP_MOD_EQ:
5225 op = TRUNC_MOD_EXPR;
5226 break;
5227
5228 case CPP_PLUS_EQ:
5229 op = PLUS_EXPR;
5230 break;
5231
5232 case CPP_MINUS_EQ:
5233 op = MINUS_EXPR;
5234 break;
5235
5236 case CPP_RSHIFT_EQ:
5237 op = RSHIFT_EXPR;
5238 break;
5239
5240 case CPP_LSHIFT_EQ:
5241 op = LSHIFT_EXPR;
5242 break;
5243
5244 case CPP_AND_EQ:
5245 op = BIT_AND_EXPR;
5246 break;
5247
5248 case CPP_XOR_EQ:
5249 op = BIT_XOR_EXPR;
5250 break;
5251
5252 case CPP_OR_EQ:
5253 op = BIT_IOR_EXPR;
5254 break;
5255
5256 case CPP_MIN_EQ:
5257 op = MIN_EXPR;
5258 break;
5259
5260 case CPP_MAX_EQ:
5261 op = MAX_EXPR;
5262 break;
5263
5264 default:
5265 /* Nothing else is an assignment operator. */
5266 op = ERROR_MARK;
5267 }
5268
5269 /* If it was an assignment operator, consume it. */
5270 if (op != ERROR_MARK)
5271 cp_lexer_consume_token (parser->lexer);
5272
5273 return op;
5274 }
5275
5276 /* Parse an expression.
5277
5278 expression:
5279 assignment-expression
5280 expression , assignment-expression
5281
5282 Returns a representation of the expression. */
5283
5284 static tree
cp_parser_expression(cp_parser * parser)5285 cp_parser_expression (cp_parser* parser)
5286 {
5287 tree expression = NULL_TREE;
5288
5289 while (true)
5290 {
5291 tree assignment_expression;
5292
5293 /* Parse the next assignment-expression. */
5294 assignment_expression
5295 = cp_parser_assignment_expression (parser);
5296 /* If this is the first assignment-expression, we can just
5297 save it away. */
5298 if (!expression)
5299 expression = assignment_expression;
5300 else
5301 expression = build_x_compound_expr (expression,
5302 assignment_expression);
5303 /* If the next token is not a comma, then we are done with the
5304 expression. */
5305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5306 break;
5307 /* Consume the `,'. */
5308 cp_lexer_consume_token (parser->lexer);
5309 /* A comma operator cannot appear in a constant-expression. */
5310 if (cp_parser_non_integral_constant_expression (parser,
5311 "a comma operator"))
5312 expression = error_mark_node;
5313 }
5314
5315 return expression;
5316 }
5317
5318 /* Parse a constant-expression.
5319
5320 constant-expression:
5321 conditional-expression
5322
5323 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5324 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5325 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5326 is false, NON_CONSTANT_P should be NULL. */
5327
5328 static tree
cp_parser_constant_expression(cp_parser * parser,bool allow_non_constant_p,bool * non_constant_p)5329 cp_parser_constant_expression (cp_parser* parser,
5330 bool allow_non_constant_p,
5331 bool *non_constant_p)
5332 {
5333 bool saved_integral_constant_expression_p;
5334 bool saved_allow_non_integral_constant_expression_p;
5335 bool saved_non_integral_constant_expression_p;
5336 tree expression;
5337
5338 /* It might seem that we could simply parse the
5339 conditional-expression, and then check to see if it were
5340 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5341 one that the compiler can figure out is constant, possibly after
5342 doing some simplifications or optimizations. The standard has a
5343 precise definition of constant-expression, and we must honor
5344 that, even though it is somewhat more restrictive.
5345
5346 For example:
5347
5348 int i[(2, 3)];
5349
5350 is not a legal declaration, because `(2, 3)' is not a
5351 constant-expression. The `,' operator is forbidden in a
5352 constant-expression. However, GCC's constant-folding machinery
5353 will fold this operation to an INTEGER_CST for `3'. */
5354
5355 /* Save the old settings. */
5356 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5357 saved_allow_non_integral_constant_expression_p
5358 = parser->allow_non_integral_constant_expression_p;
5359 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5360 /* We are now parsing a constant-expression. */
5361 parser->integral_constant_expression_p = true;
5362 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5363 parser->non_integral_constant_expression_p = false;
5364 /* Although the grammar says "conditional-expression", we parse an
5365 "assignment-expression", which also permits "throw-expression"
5366 and the use of assignment operators. In the case that
5367 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5368 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5369 actually essential that we look for an assignment-expression.
5370 For example, cp_parser_initializer_clauses uses this function to
5371 determine whether a particular assignment-expression is in fact
5372 constant. */
5373 expression = cp_parser_assignment_expression (parser);
5374 /* Restore the old settings. */
5375 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5376 parser->allow_non_integral_constant_expression_p
5377 = saved_allow_non_integral_constant_expression_p;
5378 if (allow_non_constant_p)
5379 *non_constant_p = parser->non_integral_constant_expression_p;
5380 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5381
5382 return expression;
5383 }
5384
5385 /* Statements [gram.stmt.stmt] */
5386
5387 /* Parse a statement.
5388
5389 statement:
5390 labeled-statement
5391 expression-statement
5392 compound-statement
5393 selection-statement
5394 iteration-statement
5395 jump-statement
5396 declaration-statement
5397 try-block */
5398
5399 static void
cp_parser_statement(cp_parser * parser,bool in_statement_expr_p)5400 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5401 {
5402 tree statement;
5403 cp_token *token;
5404 int statement_line_number;
5405
5406 /* There is no statement yet. */
5407 statement = NULL_TREE;
5408 /* Peek at the next token. */
5409 token = cp_lexer_peek_token (parser->lexer);
5410 /* Remember the line number of the first token in the statement. */
5411 statement_line_number = token->location.line;
5412 /* If this is a keyword, then that will often determine what kind of
5413 statement we have. */
5414 if (token->type == CPP_KEYWORD)
5415 {
5416 enum rid keyword = token->keyword;
5417
5418 switch (keyword)
5419 {
5420 case RID_CASE:
5421 case RID_DEFAULT:
5422 statement = cp_parser_labeled_statement (parser,
5423 in_statement_expr_p);
5424 break;
5425
5426 case RID_IF:
5427 case RID_SWITCH:
5428 statement = cp_parser_selection_statement (parser);
5429 break;
5430
5431 case RID_WHILE:
5432 case RID_DO:
5433 case RID_FOR:
5434 statement = cp_parser_iteration_statement (parser);
5435 break;
5436
5437 case RID_BREAK:
5438 case RID_CONTINUE:
5439 case RID_RETURN:
5440 case RID_GOTO:
5441 statement = cp_parser_jump_statement (parser);
5442 break;
5443
5444 case RID_TRY:
5445 statement = cp_parser_try_block (parser);
5446 break;
5447
5448 default:
5449 /* It might be a keyword like `int' that can start a
5450 declaration-statement. */
5451 break;
5452 }
5453 }
5454 else if (token->type == CPP_NAME)
5455 {
5456 /* If the next token is a `:', then we are looking at a
5457 labeled-statement. */
5458 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5459 if (token->type == CPP_COLON)
5460 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5461 }
5462 /* Anything that starts with a `{' must be a compound-statement. */
5463 else if (token->type == CPP_OPEN_BRACE)
5464 statement = cp_parser_compound_statement (parser, false);
5465
5466 /* Everything else must be a declaration-statement or an
5467 expression-statement. Try for the declaration-statement
5468 first, unless we are looking at a `;', in which case we know that
5469 we have an expression-statement. */
5470 if (!statement)
5471 {
5472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5473 {
5474 cp_parser_parse_tentatively (parser);
5475 /* Try to parse the declaration-statement. */
5476 cp_parser_declaration_statement (parser);
5477 /* If that worked, we're done. */
5478 if (cp_parser_parse_definitely (parser))
5479 return;
5480 }
5481 /* Look for an expression-statement instead. */
5482 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5483 }
5484
5485 /* Set the line number for the statement. */
5486 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5487 STMT_LINENO (statement) = statement_line_number;
5488 }
5489
5490 /* Parse a labeled-statement.
5491
5492 labeled-statement:
5493 identifier : statement
5494 case constant-expression : statement
5495 default : statement
5496
5497 GNU Extension:
5498
5499 labeled-statement:
5500 case constant-expression ... constant-expression : statement
5501
5502 Returns the new CASE_LABEL, for a `case' or `default' label. For
5503 an ordinary label, returns a LABEL_STMT. */
5504
5505 static tree
cp_parser_labeled_statement(cp_parser * parser,bool in_statement_expr_p)5506 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5507 {
5508 cp_token *token;
5509 tree statement = error_mark_node;
5510
5511 /* The next token should be an identifier. */
5512 token = cp_lexer_peek_token (parser->lexer);
5513 if (token->type != CPP_NAME
5514 && token->type != CPP_KEYWORD)
5515 {
5516 cp_parser_error (parser, "expected labeled-statement");
5517 return error_mark_node;
5518 }
5519
5520 switch (token->keyword)
5521 {
5522 case RID_CASE:
5523 {
5524 tree expr, expr_hi;
5525 cp_token *ellipsis;
5526
5527 /* Consume the `case' token. */
5528 cp_lexer_consume_token (parser->lexer);
5529 /* Parse the constant-expression. */
5530 expr = cp_parser_constant_expression (parser,
5531 /*allow_non_constant_p=*/false,
5532 NULL);
5533
5534 ellipsis = cp_lexer_peek_token (parser->lexer);
5535 if (ellipsis->type == CPP_ELLIPSIS)
5536 {
5537 /* Consume the `...' token. */
5538 cp_lexer_consume_token (parser->lexer);
5539 expr_hi =
5540 cp_parser_constant_expression (parser,
5541 /*allow_non_constant_p=*/false,
5542 NULL);
5543 /* We don't need to emit warnings here, as the common code
5544 will do this for us. */
5545 }
5546 else
5547 expr_hi = NULL_TREE;
5548
5549 if (!parser->in_switch_statement_p)
5550 error ("case label `%E' not within a switch statement", expr);
5551 else
5552 statement = finish_case_label (expr, expr_hi);
5553 }
5554 break;
5555
5556 case RID_DEFAULT:
5557 /* Consume the `default' token. */
5558 cp_lexer_consume_token (parser->lexer);
5559 if (!parser->in_switch_statement_p)
5560 error ("case label not within a switch statement");
5561 else
5562 statement = finish_case_label (NULL_TREE, NULL_TREE);
5563 break;
5564
5565 default:
5566 /* Anything else must be an ordinary label. */
5567 statement = finish_label_stmt (cp_parser_identifier (parser));
5568 break;
5569 }
5570
5571 /* Require the `:' token. */
5572 cp_parser_require (parser, CPP_COLON, "`:'");
5573 /* Parse the labeled statement. */
5574 cp_parser_statement (parser, in_statement_expr_p);
5575
5576 /* Return the label, in the case of a `case' or `default' label. */
5577 return statement;
5578 }
5579
5580 /* Parse an expression-statement.
5581
5582 expression-statement:
5583 expression [opt] ;
5584
5585 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5586 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5587 indicates whether this expression-statement is part of an
5588 expression statement. */
5589
5590 static tree
cp_parser_expression_statement(cp_parser * parser,bool in_statement_expr_p)5591 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5592 {
5593 tree statement = NULL_TREE;
5594
5595 /* If the next token is a ';', then there is no expression
5596 statement. */
5597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5598 statement = cp_parser_expression (parser);
5599
5600 /* Consume the final `;'. */
5601 cp_parser_consume_semicolon_at_end_of_statement (parser);
5602
5603 if (in_statement_expr_p
5604 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5605 {
5606 /* This is the final expression statement of a statement
5607 expression. */
5608 statement = finish_stmt_expr_expr (statement);
5609 }
5610 else if (statement)
5611 statement = finish_expr_stmt (statement);
5612 else
5613 finish_stmt ();
5614
5615 return statement;
5616 }
5617
5618 /* Parse a compound-statement.
5619
5620 compound-statement:
5621 { statement-seq [opt] }
5622
5623 Returns a COMPOUND_STMT representing the statement. */
5624
5625 static tree
cp_parser_compound_statement(cp_parser * parser,bool in_statement_expr_p)5626 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5627 {
5628 tree compound_stmt;
5629
5630 /* Consume the `{'. */
5631 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5632 return error_mark_node;
5633 /* Begin the compound-statement. */
5634 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5635 /* Parse an (optional) statement-seq. */
5636 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5637 /* Finish the compound-statement. */
5638 finish_compound_stmt (compound_stmt);
5639 /* Consume the `}'. */
5640 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5641
5642 return compound_stmt;
5643 }
5644
5645 /* Parse an (optional) statement-seq.
5646
5647 statement-seq:
5648 statement
5649 statement-seq [opt] statement */
5650
5651 static void
cp_parser_statement_seq_opt(cp_parser * parser,bool in_statement_expr_p)5652 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5653 {
5654 /* Scan statements until there aren't any more. */
5655 while (true)
5656 {
5657 /* If we're looking at a `}', then we've run out of statements. */
5658 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5659 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5660 break;
5661
5662 /* Parse the statement. */
5663 cp_parser_statement (parser, in_statement_expr_p);
5664 }
5665 }
5666
5667 /* Parse a selection-statement.
5668
5669 selection-statement:
5670 if ( condition ) statement
5671 if ( condition ) statement else statement
5672 switch ( condition ) statement
5673
5674 Returns the new IF_STMT or SWITCH_STMT. */
5675
5676 static tree
cp_parser_selection_statement(cp_parser * parser)5677 cp_parser_selection_statement (cp_parser* parser)
5678 {
5679 cp_token *token;
5680 enum rid keyword;
5681
5682 /* Peek at the next token. */
5683 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5684
5685 /* See what kind of keyword it is. */
5686 keyword = token->keyword;
5687 switch (keyword)
5688 {
5689 case RID_IF:
5690 case RID_SWITCH:
5691 {
5692 tree statement;
5693 tree condition;
5694
5695 /* Look for the `('. */
5696 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5697 {
5698 cp_parser_skip_to_end_of_statement (parser);
5699 return error_mark_node;
5700 }
5701
5702 /* Begin the selection-statement. */
5703 if (keyword == RID_IF)
5704 statement = begin_if_stmt ();
5705 else
5706 statement = begin_switch_stmt ();
5707
5708 /* Parse the condition. */
5709 condition = cp_parser_condition (parser);
5710 /* Look for the `)'. */
5711 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5712 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5713 /*consume_paren=*/true);
5714
5715 if (keyword == RID_IF)
5716 {
5717 tree then_stmt;
5718
5719 /* Add the condition. */
5720 finish_if_stmt_cond (condition, statement);
5721
5722 /* Parse the then-clause. */
5723 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5724 finish_then_clause (statement);
5725
5726 /* If the next token is `else', parse the else-clause. */
5727 if (cp_lexer_next_token_is_keyword (parser->lexer,
5728 RID_ELSE))
5729 {
5730 tree else_stmt;
5731
5732 /* Consume the `else' keyword. */
5733 cp_lexer_consume_token (parser->lexer);
5734 /* Parse the else-clause. */
5735 else_stmt
5736 = cp_parser_implicitly_scoped_statement (parser);
5737 finish_else_clause (statement);
5738 }
5739
5740 /* Now we're all done with the if-statement. */
5741 finish_if_stmt ();
5742 }
5743 else
5744 {
5745 tree body;
5746 bool in_switch_statement_p;
5747
5748 /* Add the condition. */
5749 finish_switch_cond (condition, statement);
5750
5751 /* Parse the body of the switch-statement. */
5752 in_switch_statement_p = parser->in_switch_statement_p;
5753 parser->in_switch_statement_p = true;
5754 body = cp_parser_implicitly_scoped_statement (parser);
5755 parser->in_switch_statement_p = in_switch_statement_p;
5756
5757 /* Now we're all done with the switch-statement. */
5758 finish_switch_stmt (statement);
5759 }
5760
5761 return statement;
5762 }
5763 break;
5764
5765 default:
5766 cp_parser_error (parser, "expected selection-statement");
5767 return error_mark_node;
5768 }
5769 }
5770
5771 /* Parse a condition.
5772
5773 condition:
5774 expression
5775 type-specifier-seq declarator = assignment-expression
5776
5777 GNU Extension:
5778
5779 condition:
5780 type-specifier-seq declarator asm-specification [opt]
5781 attributes [opt] = assignment-expression
5782
5783 Returns the expression that should be tested. */
5784
5785 static tree
cp_parser_condition(cp_parser * parser)5786 cp_parser_condition (cp_parser* parser)
5787 {
5788 tree type_specifiers;
5789 const char *saved_message;
5790
5791 /* Try the declaration first. */
5792 cp_parser_parse_tentatively (parser);
5793 /* New types are not allowed in the type-specifier-seq for a
5794 condition. */
5795 saved_message = parser->type_definition_forbidden_message;
5796 parser->type_definition_forbidden_message
5797 = "types may not be defined in conditions";
5798 /* Parse the type-specifier-seq. */
5799 type_specifiers = cp_parser_type_specifier_seq (parser);
5800 /* Restore the saved message. */
5801 parser->type_definition_forbidden_message = saved_message;
5802 /* If all is well, we might be looking at a declaration. */
5803 if (!cp_parser_error_occurred (parser))
5804 {
5805 tree decl;
5806 tree asm_specification;
5807 tree attributes;
5808 tree declarator;
5809 tree initializer = NULL_TREE;
5810
5811 /* Parse the declarator. */
5812 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5813 /*ctor_dtor_or_conv_p=*/NULL,
5814 /*parenthesized_p=*/NULL);
5815 /* Parse the attributes. */
5816 attributes = cp_parser_attributes_opt (parser);
5817 /* Parse the asm-specification. */
5818 asm_specification = cp_parser_asm_specification_opt (parser);
5819 /* If the next token is not an `=', then we might still be
5820 looking at an expression. For example:
5821
5822 if (A(a).x)
5823
5824 looks like a decl-specifier-seq and a declarator -- but then
5825 there is no `=', so this is an expression. */
5826 cp_parser_require (parser, CPP_EQ, "`='");
5827 /* If we did see an `=', then we are looking at a declaration
5828 for sure. */
5829 if (cp_parser_parse_definitely (parser))
5830 {
5831 /* Create the declaration. */
5832 decl = start_decl (declarator, type_specifiers,
5833 /*initialized_p=*/true,
5834 attributes, /*prefix_attributes=*/NULL_TREE);
5835 /* Parse the assignment-expression. */
5836 initializer = cp_parser_assignment_expression (parser);
5837
5838 /* Process the initializer. */
5839 cp_finish_decl (decl,
5840 initializer,
5841 asm_specification,
5842 LOOKUP_ONLYCONVERTING);
5843
5844 return convert_from_reference (decl);
5845 }
5846 }
5847 /* If we didn't even get past the declarator successfully, we are
5848 definitely not looking at a declaration. */
5849 else
5850 cp_parser_abort_tentative_parse (parser);
5851
5852 /* Otherwise, we are looking at an expression. */
5853 return cp_parser_expression (parser);
5854 }
5855
5856 /* Parse an iteration-statement.
5857
5858 iteration-statement:
5859 while ( condition ) statement
5860 do statement while ( expression ) ;
5861 for ( for-init-statement condition [opt] ; expression [opt] )
5862 statement
5863
5864 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5865
5866 static tree
cp_parser_iteration_statement(cp_parser * parser)5867 cp_parser_iteration_statement (cp_parser* parser)
5868 {
5869 cp_token *token;
5870 enum rid keyword;
5871 tree statement;
5872 bool in_iteration_statement_p;
5873
5874
5875 /* Peek at the next token. */
5876 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5877 if (!token)
5878 return error_mark_node;
5879
5880 /* Remember whether or not we are already within an iteration
5881 statement. */
5882 in_iteration_statement_p = parser->in_iteration_statement_p;
5883
5884 /* See what kind of keyword it is. */
5885 keyword = token->keyword;
5886 switch (keyword)
5887 {
5888 case RID_WHILE:
5889 {
5890 tree condition;
5891
5892 /* Begin the while-statement. */
5893 statement = begin_while_stmt ();
5894 /* Look for the `('. */
5895 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5896 /* Parse the condition. */
5897 condition = cp_parser_condition (parser);
5898 finish_while_stmt_cond (condition, statement);
5899 /* Look for the `)'. */
5900 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5901 /* Parse the dependent statement. */
5902 parser->in_iteration_statement_p = true;
5903 cp_parser_already_scoped_statement (parser);
5904 parser->in_iteration_statement_p = in_iteration_statement_p;
5905 /* We're done with the while-statement. */
5906 finish_while_stmt (statement);
5907 }
5908 break;
5909
5910 case RID_DO:
5911 {
5912 tree expression;
5913
5914 /* Begin the do-statement. */
5915 statement = begin_do_stmt ();
5916 /* Parse the body of the do-statement. */
5917 parser->in_iteration_statement_p = true;
5918 cp_parser_implicitly_scoped_statement (parser);
5919 parser->in_iteration_statement_p = in_iteration_statement_p;
5920 finish_do_body (statement);
5921 /* Look for the `while' keyword. */
5922 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5923 /* Look for the `('. */
5924 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5925 /* Parse the expression. */
5926 expression = cp_parser_expression (parser);
5927 /* We're done with the do-statement. */
5928 finish_do_stmt (expression, statement);
5929 /* Look for the `)'. */
5930 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5931 /* Look for the `;'. */
5932 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5933 }
5934 break;
5935
5936 case RID_FOR:
5937 {
5938 tree condition = NULL_TREE;
5939 tree expression = NULL_TREE;
5940
5941 /* Begin the for-statement. */
5942 statement = begin_for_stmt ();
5943 /* Look for the `('. */
5944 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5945 /* Parse the initialization. */
5946 cp_parser_for_init_statement (parser);
5947 finish_for_init_stmt (statement);
5948
5949 /* If there's a condition, process it. */
5950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5951 condition = cp_parser_condition (parser);
5952 finish_for_cond (condition, statement);
5953 /* Look for the `;'. */
5954 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5955
5956 /* If there's an expression, process it. */
5957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5958 expression = cp_parser_expression (parser);
5959 finish_for_expr (expression, statement);
5960 /* Look for the `)'. */
5961 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5962
5963 /* Parse the body of the for-statement. */
5964 parser->in_iteration_statement_p = true;
5965 cp_parser_already_scoped_statement (parser);
5966 parser->in_iteration_statement_p = in_iteration_statement_p;
5967
5968 /* We're done with the for-statement. */
5969 finish_for_stmt (statement);
5970 }
5971 break;
5972
5973 default:
5974 cp_parser_error (parser, "expected iteration-statement");
5975 statement = error_mark_node;
5976 break;
5977 }
5978
5979 return statement;
5980 }
5981
5982 /* Parse a for-init-statement.
5983
5984 for-init-statement:
5985 expression-statement
5986 simple-declaration */
5987
5988 static void
cp_parser_for_init_statement(cp_parser * parser)5989 cp_parser_for_init_statement (cp_parser* parser)
5990 {
5991 /* If the next token is a `;', then we have an empty
5992 expression-statement. Grammatically, this is also a
5993 simple-declaration, but an invalid one, because it does not
5994 declare anything. Therefore, if we did not handle this case
5995 specially, we would issue an error message about an invalid
5996 declaration. */
5997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5998 {
5999 /* We're going to speculatively look for a declaration, falling back
6000 to an expression, if necessary. */
6001 cp_parser_parse_tentatively (parser);
6002 /* Parse the declaration. */
6003 cp_parser_simple_declaration (parser,
6004 /*function_definition_allowed_p=*/false);
6005 /* If the tentative parse failed, then we shall need to look for an
6006 expression-statement. */
6007 if (cp_parser_parse_definitely (parser))
6008 return;
6009 }
6010
6011 cp_parser_expression_statement (parser, false);
6012 }
6013
6014 /* Parse a jump-statement.
6015
6016 jump-statement:
6017 break ;
6018 continue ;
6019 return expression [opt] ;
6020 goto identifier ;
6021
6022 GNU extension:
6023
6024 jump-statement:
6025 goto * expression ;
6026
6027 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6028 GOTO_STMT. */
6029
6030 static tree
cp_parser_jump_statement(cp_parser * parser)6031 cp_parser_jump_statement (cp_parser* parser)
6032 {
6033 tree statement = error_mark_node;
6034 cp_token *token;
6035 enum rid keyword;
6036
6037 /* Peek at the next token. */
6038 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6039 if (!token)
6040 return error_mark_node;
6041
6042 /* See what kind of keyword it is. */
6043 keyword = token->keyword;
6044 switch (keyword)
6045 {
6046 case RID_BREAK:
6047 if (!parser->in_switch_statement_p
6048 && !parser->in_iteration_statement_p)
6049 {
6050 error ("break statement not within loop or switch");
6051 statement = error_mark_node;
6052 }
6053 else
6054 statement = finish_break_stmt ();
6055 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6056 break;
6057
6058 case RID_CONTINUE:
6059 if (!parser->in_iteration_statement_p)
6060 {
6061 error ("continue statement not within a loop");
6062 statement = error_mark_node;
6063 }
6064 else
6065 statement = finish_continue_stmt ();
6066 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6067 break;
6068
6069 case RID_RETURN:
6070 {
6071 tree expr;
6072
6073 /* If the next token is a `;', then there is no
6074 expression. */
6075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6076 expr = cp_parser_expression (parser);
6077 else
6078 expr = NULL_TREE;
6079 /* Build the return-statement. */
6080 statement = finish_return_stmt (expr);
6081 /* Look for the final `;'. */
6082 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6083 }
6084 break;
6085
6086 case RID_GOTO:
6087 /* Create the goto-statement. */
6088 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6089 {
6090 /* Issue a warning about this use of a GNU extension. */
6091 if (pedantic)
6092 pedwarn ("ISO C++ forbids computed gotos");
6093 /* Consume the '*' token. */
6094 cp_lexer_consume_token (parser->lexer);
6095 /* Parse the dependent expression. */
6096 finish_goto_stmt (cp_parser_expression (parser));
6097 }
6098 else
6099 finish_goto_stmt (cp_parser_identifier (parser));
6100 /* Look for the final `;'. */
6101 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6102 break;
6103
6104 default:
6105 cp_parser_error (parser, "expected jump-statement");
6106 break;
6107 }
6108
6109 return statement;
6110 }
6111
6112 /* Parse a declaration-statement.
6113
6114 declaration-statement:
6115 block-declaration */
6116
6117 static void
cp_parser_declaration_statement(cp_parser * parser)6118 cp_parser_declaration_statement (cp_parser* parser)
6119 {
6120 /* Parse the block-declaration. */
6121 cp_parser_block_declaration (parser, /*statement_p=*/true);
6122
6123 /* Finish off the statement. */
6124 finish_stmt ();
6125 }
6126
6127 /* Some dependent statements (like `if (cond) statement'), are
6128 implicitly in their own scope. In other words, if the statement is
6129 a single statement (as opposed to a compound-statement), it is
6130 none-the-less treated as if it were enclosed in braces. Any
6131 declarations appearing in the dependent statement are out of scope
6132 after control passes that point. This function parses a statement,
6133 but ensures that is in its own scope, even if it is not a
6134 compound-statement.
6135
6136 Returns the new statement. */
6137
6138 static tree
cp_parser_implicitly_scoped_statement(cp_parser * parser)6139 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6140 {
6141 tree statement;
6142
6143 /* If the token is not a `{', then we must take special action. */
6144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6145 {
6146 /* Create a compound-statement. */
6147 statement = begin_compound_stmt (/*has_no_scope=*/false);
6148 /* Parse the dependent-statement. */
6149 cp_parser_statement (parser, false);
6150 /* Finish the dummy compound-statement. */
6151 finish_compound_stmt (statement);
6152 }
6153 /* Otherwise, we simply parse the statement directly. */
6154 else
6155 statement = cp_parser_compound_statement (parser, false);
6156
6157 /* Return the statement. */
6158 return statement;
6159 }
6160
6161 /* For some dependent statements (like `while (cond) statement'), we
6162 have already created a scope. Therefore, even if the dependent
6163 statement is a compound-statement, we do not want to create another
6164 scope. */
6165
6166 static void
cp_parser_already_scoped_statement(cp_parser * parser)6167 cp_parser_already_scoped_statement (cp_parser* parser)
6168 {
6169 /* If the token is not a `{', then we must take special action. */
6170 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6171 {
6172 tree statement;
6173
6174 /* Create a compound-statement. */
6175 statement = begin_compound_stmt (/*has_no_scope=*/true);
6176 /* Parse the dependent-statement. */
6177 cp_parser_statement (parser, false);
6178 /* Finish the dummy compound-statement. */
6179 finish_compound_stmt (statement);
6180 }
6181 /* Otherwise, we simply parse the statement directly. */
6182 else
6183 cp_parser_statement (parser, false);
6184 }
6185
6186 /* Declarations [gram.dcl.dcl] */
6187
6188 /* Parse an optional declaration-sequence.
6189
6190 declaration-seq:
6191 declaration
6192 declaration-seq declaration */
6193
6194 static void
cp_parser_declaration_seq_opt(cp_parser * parser)6195 cp_parser_declaration_seq_opt (cp_parser* parser)
6196 {
6197 while (true)
6198 {
6199 cp_token *token;
6200
6201 token = cp_lexer_peek_token (parser->lexer);
6202
6203 if (token->type == CPP_CLOSE_BRACE
6204 || token->type == CPP_EOF)
6205 break;
6206
6207 if (token->type == CPP_SEMICOLON)
6208 {
6209 /* A declaration consisting of a single semicolon is
6210 invalid. Allow it unless we're being pedantic. */
6211 if (pedantic && !in_system_header)
6212 pedwarn ("extra `;'");
6213 cp_lexer_consume_token (parser->lexer);
6214 continue;
6215 }
6216
6217 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6218 parser to enter or exit implicit `extern "C"' blocks. */
6219 while (pending_lang_change > 0)
6220 {
6221 push_lang_context (lang_name_c);
6222 --pending_lang_change;
6223 }
6224 while (pending_lang_change < 0)
6225 {
6226 pop_lang_context ();
6227 ++pending_lang_change;
6228 }
6229
6230 /* Parse the declaration itself. */
6231 cp_parser_declaration (parser);
6232 }
6233 }
6234
6235 /* Parse a declaration.
6236
6237 declaration:
6238 block-declaration
6239 function-definition
6240 template-declaration
6241 explicit-instantiation
6242 explicit-specialization
6243 linkage-specification
6244 namespace-definition
6245
6246 GNU extension:
6247
6248 declaration:
6249 __extension__ declaration */
6250
6251 static void
cp_parser_declaration(cp_parser * parser)6252 cp_parser_declaration (cp_parser* parser)
6253 {
6254 cp_token token1;
6255 cp_token token2;
6256 int saved_pedantic;
6257
6258 /* Check for the `__extension__' keyword. */
6259 if (cp_parser_extension_opt (parser, &saved_pedantic))
6260 {
6261 /* Parse the qualified declaration. */
6262 cp_parser_declaration (parser);
6263 /* Restore the PEDANTIC flag. */
6264 pedantic = saved_pedantic;
6265
6266 return;
6267 }
6268
6269 /* Try to figure out what kind of declaration is present. */
6270 token1 = *cp_lexer_peek_token (parser->lexer);
6271 if (token1.type != CPP_EOF)
6272 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6273
6274 /* If the next token is `extern' and the following token is a string
6275 literal, then we have a linkage specification. */
6276 if (token1.keyword == RID_EXTERN
6277 && cp_parser_is_string_literal (&token2))
6278 cp_parser_linkage_specification (parser);
6279 /* If the next token is `template', then we have either a template
6280 declaration, an explicit instantiation, or an explicit
6281 specialization. */
6282 else if (token1.keyword == RID_TEMPLATE)
6283 {
6284 /* `template <>' indicates a template specialization. */
6285 if (token2.type == CPP_LESS
6286 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6287 cp_parser_explicit_specialization (parser);
6288 /* `template <' indicates a template declaration. */
6289 else if (token2.type == CPP_LESS)
6290 cp_parser_template_declaration (parser, /*member_p=*/false);
6291 /* Anything else must be an explicit instantiation. */
6292 else
6293 cp_parser_explicit_instantiation (parser);
6294 }
6295 /* If the next token is `export', then we have a template
6296 declaration. */
6297 else if (token1.keyword == RID_EXPORT)
6298 cp_parser_template_declaration (parser, /*member_p=*/false);
6299 /* If the next token is `extern', 'static' or 'inline' and the one
6300 after that is `template', we have a GNU extended explicit
6301 instantiation directive. */
6302 else if (cp_parser_allow_gnu_extensions_p (parser)
6303 && (token1.keyword == RID_EXTERN
6304 || token1.keyword == RID_STATIC
6305 || token1.keyword == RID_INLINE)
6306 && token2.keyword == RID_TEMPLATE)
6307 cp_parser_explicit_instantiation (parser);
6308 /* If the next token is `namespace', check for a named or unnamed
6309 namespace definition. */
6310 else if (token1.keyword == RID_NAMESPACE
6311 && (/* A named namespace definition. */
6312 (token2.type == CPP_NAME
6313 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6314 == CPP_OPEN_BRACE))
6315 /* An unnamed namespace definition. */
6316 || token2.type == CPP_OPEN_BRACE))
6317 cp_parser_namespace_definition (parser);
6318 /* We must have either a block declaration or a function
6319 definition. */
6320 else
6321 /* Try to parse a block-declaration, or a function-definition. */
6322 cp_parser_block_declaration (parser, /*statement_p=*/false);
6323 }
6324
6325 /* Parse a block-declaration.
6326
6327 block-declaration:
6328 simple-declaration
6329 asm-definition
6330 namespace-alias-definition
6331 using-declaration
6332 using-directive
6333
6334 GNU Extension:
6335
6336 block-declaration:
6337 __extension__ block-declaration
6338 label-declaration
6339
6340 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6341 part of a declaration-statement. */
6342
6343 static void
cp_parser_block_declaration(cp_parser * parser,bool statement_p)6344 cp_parser_block_declaration (cp_parser *parser,
6345 bool statement_p)
6346 {
6347 cp_token *token1;
6348 int saved_pedantic;
6349
6350 /* Check for the `__extension__' keyword. */
6351 if (cp_parser_extension_opt (parser, &saved_pedantic))
6352 {
6353 /* Parse the qualified declaration. */
6354 cp_parser_block_declaration (parser, statement_p);
6355 /* Restore the PEDANTIC flag. */
6356 pedantic = saved_pedantic;
6357
6358 return;
6359 }
6360
6361 /* Peek at the next token to figure out which kind of declaration is
6362 present. */
6363 token1 = cp_lexer_peek_token (parser->lexer);
6364
6365 /* If the next keyword is `asm', we have an asm-definition. */
6366 if (token1->keyword == RID_ASM)
6367 {
6368 if (statement_p)
6369 cp_parser_commit_to_tentative_parse (parser);
6370 cp_parser_asm_definition (parser);
6371 }
6372 /* If the next keyword is `namespace', we have a
6373 namespace-alias-definition. */
6374 else if (token1->keyword == RID_NAMESPACE)
6375 cp_parser_namespace_alias_definition (parser);
6376 /* If the next keyword is `using', we have either a
6377 using-declaration or a using-directive. */
6378 else if (token1->keyword == RID_USING)
6379 {
6380 cp_token *token2;
6381
6382 if (statement_p)
6383 cp_parser_commit_to_tentative_parse (parser);
6384 /* If the token after `using' is `namespace', then we have a
6385 using-directive. */
6386 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6387 if (token2->keyword == RID_NAMESPACE)
6388 cp_parser_using_directive (parser);
6389 /* Otherwise, it's a using-declaration. */
6390 else
6391 cp_parser_using_declaration (parser);
6392 }
6393 /* If the next keyword is `__label__' we have a label declaration. */
6394 else if (token1->keyword == RID_LABEL)
6395 {
6396 if (statement_p)
6397 cp_parser_commit_to_tentative_parse (parser);
6398 cp_parser_label_declaration (parser);
6399 }
6400 /* Anything else must be a simple-declaration. */
6401 else
6402 cp_parser_simple_declaration (parser, !statement_p);
6403 }
6404
6405 /* Parse a simple-declaration.
6406
6407 simple-declaration:
6408 decl-specifier-seq [opt] init-declarator-list [opt] ;
6409
6410 init-declarator-list:
6411 init-declarator
6412 init-declarator-list , init-declarator
6413
6414 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6415 function-definition as a simple-declaration. */
6416
6417 static void
cp_parser_simple_declaration(cp_parser * parser,bool function_definition_allowed_p)6418 cp_parser_simple_declaration (cp_parser* parser,
6419 bool function_definition_allowed_p)
6420 {
6421 tree decl_specifiers;
6422 tree attributes;
6423 int declares_class_or_enum;
6424 bool saw_declarator;
6425
6426 /* Defer access checks until we know what is being declared; the
6427 checks for names appearing in the decl-specifier-seq should be
6428 done as if we were in the scope of the thing being declared. */
6429 push_deferring_access_checks (dk_deferred);
6430
6431 /* Parse the decl-specifier-seq. We have to keep track of whether
6432 or not the decl-specifier-seq declares a named class or
6433 enumeration type, since that is the only case in which the
6434 init-declarator-list is allowed to be empty.
6435
6436 [dcl.dcl]
6437
6438 In a simple-declaration, the optional init-declarator-list can be
6439 omitted only when declaring a class or enumeration, that is when
6440 the decl-specifier-seq contains either a class-specifier, an
6441 elaborated-type-specifier, or an enum-specifier. */
6442 decl_specifiers
6443 = cp_parser_decl_specifier_seq (parser,
6444 CP_PARSER_FLAGS_OPTIONAL,
6445 &attributes,
6446 &declares_class_or_enum);
6447 /* We no longer need to defer access checks. */
6448 stop_deferring_access_checks ();
6449
6450 /* In a block scope, a valid declaration must always have a
6451 decl-specifier-seq. By not trying to parse declarators, we can
6452 resolve the declaration/expression ambiguity more quickly. */
6453 if (!function_definition_allowed_p && !decl_specifiers)
6454 {
6455 cp_parser_error (parser, "expected declaration");
6456 goto done;
6457 }
6458
6459 /* If the next two tokens are both identifiers, the code is
6460 erroneous. The usual cause of this situation is code like:
6461
6462 T t;
6463
6464 where "T" should name a type -- but does not. */
6465 if (cp_parser_diagnose_invalid_type_name (parser))
6466 {
6467 /* If parsing tentatively, we should commit; we really are
6468 looking at a declaration. */
6469 cp_parser_commit_to_tentative_parse (parser);
6470 /* Give up. */
6471 goto done;
6472 }
6473
6474 /* Keep going until we hit the `;' at the end of the simple
6475 declaration. */
6476 saw_declarator = false;
6477 while (cp_lexer_next_token_is_not (parser->lexer,
6478 CPP_SEMICOLON))
6479 {
6480 cp_token *token;
6481 bool function_definition_p;
6482 tree decl;
6483
6484 saw_declarator = true;
6485 /* Parse the init-declarator. */
6486 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6487 function_definition_allowed_p,
6488 /*member_p=*/false,
6489 declares_class_or_enum,
6490 &function_definition_p);
6491 /* If an error occurred while parsing tentatively, exit quickly.
6492 (That usually happens when in the body of a function; each
6493 statement is treated as a declaration-statement until proven
6494 otherwise.) */
6495 if (cp_parser_error_occurred (parser))
6496 goto done;
6497 /* Handle function definitions specially. */
6498 if (function_definition_p)
6499 {
6500 /* If the next token is a `,', then we are probably
6501 processing something like:
6502
6503 void f() {}, *p;
6504
6505 which is erroneous. */
6506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6507 error ("mixing declarations and function-definitions is forbidden");
6508 /* Otherwise, we're done with the list of declarators. */
6509 else
6510 {
6511 pop_deferring_access_checks ();
6512 return;
6513 }
6514 }
6515 /* The next token should be either a `,' or a `;'. */
6516 token = cp_lexer_peek_token (parser->lexer);
6517 /* If it's a `,', there are more declarators to come. */
6518 if (token->type == CPP_COMMA)
6519 cp_lexer_consume_token (parser->lexer);
6520 /* If it's a `;', we are done. */
6521 else if (token->type == CPP_SEMICOLON)
6522 break;
6523 /* Anything else is an error. */
6524 else
6525 {
6526 cp_parser_error (parser, "expected `,' or `;'");
6527 /* Skip tokens until we reach the end of the statement. */
6528 cp_parser_skip_to_end_of_statement (parser);
6529 /* If the next token is now a `;', consume it. */
6530 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6531 cp_lexer_consume_token (parser->lexer);
6532 goto done;
6533 }
6534 /* After the first time around, a function-definition is not
6535 allowed -- even if it was OK at first. For example:
6536
6537 int i, f() {}
6538
6539 is not valid. */
6540 function_definition_allowed_p = false;
6541 }
6542
6543 /* Issue an error message if no declarators are present, and the
6544 decl-specifier-seq does not itself declare a class or
6545 enumeration. */
6546 if (!saw_declarator)
6547 {
6548 if (cp_parser_declares_only_class_p (parser))
6549 shadow_tag (decl_specifiers);
6550 /* Perform any deferred access checks. */
6551 perform_deferred_access_checks ();
6552 }
6553
6554 /* Consume the `;'. */
6555 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6556
6557 done:
6558 pop_deferring_access_checks ();
6559 }
6560
6561 /* Parse a decl-specifier-seq.
6562
6563 decl-specifier-seq:
6564 decl-specifier-seq [opt] decl-specifier
6565
6566 decl-specifier:
6567 storage-class-specifier
6568 type-specifier
6569 function-specifier
6570 friend
6571 typedef
6572
6573 GNU Extension:
6574
6575 decl-specifier:
6576 attributes
6577
6578 Returns a TREE_LIST, giving the decl-specifiers in the order they
6579 appear in the source code. The TREE_VALUE of each node is the
6580 decl-specifier. For a keyword (such as `auto' or `friend'), the
6581 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6582 representation of a type-specifier, see cp_parser_type_specifier.
6583
6584 If there are attributes, they will be stored in *ATTRIBUTES,
6585 represented as described above cp_parser_attributes.
6586
6587 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6588 appears, and the entity that will be a friend is not going to be a
6589 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6590 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6591 friendship is granted might not be a class.
6592
6593 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6594 flags:
6595
6596 1: one of the decl-specifiers is an elaborated-type-specifier
6597 (i.e., a type declaration)
6598 2: one of the decl-specifiers is an enum-specifier or a
6599 class-specifier (i.e., a type definition)
6600
6601 */
6602
6603 static tree
cp_parser_decl_specifier_seq(cp_parser * parser,cp_parser_flags flags,tree * attributes,int * declares_class_or_enum)6604 cp_parser_decl_specifier_seq (cp_parser* parser,
6605 cp_parser_flags flags,
6606 tree* attributes,
6607 int* declares_class_or_enum)
6608 {
6609 tree decl_specs = NULL_TREE;
6610 bool friend_p = false;
6611 bool constructor_possible_p = !parser->in_declarator_p;
6612
6613 /* Assume no class or enumeration type is declared. */
6614 *declares_class_or_enum = 0;
6615
6616 /* Assume there are no attributes. */
6617 *attributes = NULL_TREE;
6618
6619 /* Keep reading specifiers until there are no more to read. */
6620 while (true)
6621 {
6622 tree decl_spec = NULL_TREE;
6623 bool constructor_p;
6624 cp_token *token;
6625
6626 /* Peek at the next token. */
6627 token = cp_lexer_peek_token (parser->lexer);
6628 /* Handle attributes. */
6629 if (token->keyword == RID_ATTRIBUTE)
6630 {
6631 /* Parse the attributes. */
6632 decl_spec = cp_parser_attributes_opt (parser);
6633 /* Add them to the list. */
6634 *attributes = chainon (*attributes, decl_spec);
6635 continue;
6636 }
6637 /* If the next token is an appropriate keyword, we can simply
6638 add it to the list. */
6639 switch (token->keyword)
6640 {
6641 case RID_FRIEND:
6642 /* decl-specifier:
6643 friend */
6644 if (friend_p)
6645 error ("duplicate `friend'");
6646 else
6647 friend_p = true;
6648 /* The representation of the specifier is simply the
6649 appropriate TREE_IDENTIFIER node. */
6650 decl_spec = token->value;
6651 /* Consume the token. */
6652 cp_lexer_consume_token (parser->lexer);
6653 break;
6654
6655 /* function-specifier:
6656 inline
6657 virtual
6658 explicit */
6659 case RID_INLINE:
6660 case RID_VIRTUAL:
6661 case RID_EXPLICIT:
6662 decl_spec = cp_parser_function_specifier_opt (parser);
6663 break;
6664
6665 /* decl-specifier:
6666 typedef */
6667 case RID_TYPEDEF:
6668 /* The representation of the specifier is simply the
6669 appropriate TREE_IDENTIFIER node. */
6670 decl_spec = token->value;
6671 /* Consume the token. */
6672 cp_lexer_consume_token (parser->lexer);
6673 /* A constructor declarator cannot appear in a typedef. */
6674 constructor_possible_p = false;
6675 /* The "typedef" keyword can only occur in a declaration; we
6676 may as well commit at this point. */
6677 cp_parser_commit_to_tentative_parse (parser);
6678 break;
6679
6680 /* storage-class-specifier:
6681 auto
6682 register
6683 static
6684 extern
6685 mutable
6686
6687 GNU Extension:
6688 thread */
6689 case RID_AUTO:
6690 case RID_REGISTER:
6691 case RID_STATIC:
6692 case RID_EXTERN:
6693 case RID_MUTABLE:
6694 case RID_THREAD:
6695 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6696 break;
6697
6698 default:
6699 break;
6700 }
6701
6702 /* Constructors are a special case. The `S' in `S()' is not a
6703 decl-specifier; it is the beginning of the declarator. */
6704 constructor_p = (!decl_spec
6705 && constructor_possible_p
6706 && cp_parser_constructor_declarator_p (parser,
6707 friend_p));
6708
6709 /* If we don't have a DECL_SPEC yet, then we must be looking at
6710 a type-specifier. */
6711 if (!decl_spec && !constructor_p)
6712 {
6713 int decl_spec_declares_class_or_enum;
6714 bool is_cv_qualifier;
6715
6716 decl_spec
6717 = cp_parser_type_specifier (parser, flags,
6718 friend_p,
6719 /*is_declaration=*/true,
6720 &decl_spec_declares_class_or_enum,
6721 &is_cv_qualifier);
6722
6723 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6724
6725 /* If this type-specifier referenced a user-defined type
6726 (a typedef, class-name, etc.), then we can't allow any
6727 more such type-specifiers henceforth.
6728
6729 [dcl.spec]
6730
6731 The longest sequence of decl-specifiers that could
6732 possibly be a type name is taken as the
6733 decl-specifier-seq of a declaration. The sequence shall
6734 be self-consistent as described below.
6735
6736 [dcl.type]
6737
6738 As a general rule, at most one type-specifier is allowed
6739 in the complete decl-specifier-seq of a declaration. The
6740 only exceptions are the following:
6741
6742 -- const or volatile can be combined with any other
6743 type-specifier.
6744
6745 -- signed or unsigned can be combined with char, long,
6746 short, or int.
6747
6748 -- ..
6749
6750 Example:
6751
6752 typedef char* Pc;
6753 void g (const int Pc);
6754
6755 Here, Pc is *not* part of the decl-specifier seq; it's
6756 the declarator. Therefore, once we see a type-specifier
6757 (other than a cv-qualifier), we forbid any additional
6758 user-defined types. We *do* still allow things like `int
6759 int' to be considered a decl-specifier-seq, and issue the
6760 error message later. */
6761 if (decl_spec && !is_cv_qualifier)
6762 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6763 /* A constructor declarator cannot follow a type-specifier. */
6764 if (decl_spec)
6765 constructor_possible_p = false;
6766 }
6767
6768 /* If we still do not have a DECL_SPEC, then there are no more
6769 decl-specifiers. */
6770 if (!decl_spec)
6771 {
6772 /* Issue an error message, unless the entire construct was
6773 optional. */
6774 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6775 {
6776 cp_parser_error (parser, "expected decl specifier");
6777 return error_mark_node;
6778 }
6779
6780 break;
6781 }
6782
6783 /* Add the DECL_SPEC to the list of specifiers. */
6784 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6785 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6786
6787 /* After we see one decl-specifier, further decl-specifiers are
6788 always optional. */
6789 flags |= CP_PARSER_FLAGS_OPTIONAL;
6790 }
6791
6792 /* Don't allow a friend specifier with a class definition. */
6793 if (friend_p && (*declares_class_or_enum & 2))
6794 error ("class definition may not be declared a friend");
6795
6796 /* We have built up the DECL_SPECS in reverse order. Return them in
6797 the correct order. */
6798 return nreverse (decl_specs);
6799 }
6800
6801 /* Parse an (optional) storage-class-specifier.
6802
6803 storage-class-specifier:
6804 auto
6805 register
6806 static
6807 extern
6808 mutable
6809
6810 GNU Extension:
6811
6812 storage-class-specifier:
6813 thread
6814
6815 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6816
6817 static tree
cp_parser_storage_class_specifier_opt(cp_parser * parser)6818 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6819 {
6820 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6821 {
6822 case RID_AUTO:
6823 case RID_REGISTER:
6824 case RID_STATIC:
6825 case RID_EXTERN:
6826 case RID_MUTABLE:
6827 case RID_THREAD:
6828 /* Consume the token. */
6829 return cp_lexer_consume_token (parser->lexer)->value;
6830
6831 default:
6832 return NULL_TREE;
6833 }
6834 }
6835
6836 /* Parse an (optional) function-specifier.
6837
6838 function-specifier:
6839 inline
6840 virtual
6841 explicit
6842
6843 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6844
6845 static tree
cp_parser_function_specifier_opt(cp_parser * parser)6846 cp_parser_function_specifier_opt (cp_parser* parser)
6847 {
6848 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6849 {
6850 case RID_INLINE:
6851 case RID_VIRTUAL:
6852 case RID_EXPLICIT:
6853 /* Consume the token. */
6854 return cp_lexer_consume_token (parser->lexer)->value;
6855
6856 default:
6857 return NULL_TREE;
6858 }
6859 }
6860
6861 /* Parse a linkage-specification.
6862
6863 linkage-specification:
6864 extern string-literal { declaration-seq [opt] }
6865 extern string-literal declaration */
6866
6867 static void
cp_parser_linkage_specification(cp_parser * parser)6868 cp_parser_linkage_specification (cp_parser* parser)
6869 {
6870 cp_token *token;
6871 tree linkage;
6872
6873 /* Look for the `extern' keyword. */
6874 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6875
6876 /* Peek at the next token. */
6877 token = cp_lexer_peek_token (parser->lexer);
6878 /* If it's not a string-literal, then there's a problem. */
6879 if (!cp_parser_is_string_literal (token))
6880 {
6881 cp_parser_error (parser, "expected language-name");
6882 return;
6883 }
6884 /* Consume the token. */
6885 cp_lexer_consume_token (parser->lexer);
6886
6887 /* Transform the literal into an identifier. If the literal is a
6888 wide-character string, or contains embedded NULs, then we can't
6889 handle it as the user wants. */
6890 if (token->type == CPP_WSTRING
6891 || (strlen (TREE_STRING_POINTER (token->value))
6892 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6893 {
6894 cp_parser_error (parser, "invalid linkage-specification");
6895 /* Assume C++ linkage. */
6896 linkage = get_identifier ("c++");
6897 }
6898 /* If it's a simple string constant, things are easier. */
6899 else
6900 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6901
6902 /* We're now using the new linkage. */
6903 push_lang_context (linkage);
6904
6905 /* If the next token is a `{', then we're using the first
6906 production. */
6907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6908 {
6909 /* Consume the `{' token. */
6910 cp_lexer_consume_token (parser->lexer);
6911 /* Parse the declarations. */
6912 cp_parser_declaration_seq_opt (parser);
6913 /* Look for the closing `}'. */
6914 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6915 }
6916 /* Otherwise, there's just one declaration. */
6917 else
6918 {
6919 bool saved_in_unbraced_linkage_specification_p;
6920
6921 saved_in_unbraced_linkage_specification_p
6922 = parser->in_unbraced_linkage_specification_p;
6923 parser->in_unbraced_linkage_specification_p = true;
6924 have_extern_spec = true;
6925 cp_parser_declaration (parser);
6926 have_extern_spec = false;
6927 parser->in_unbraced_linkage_specification_p
6928 = saved_in_unbraced_linkage_specification_p;
6929 }
6930
6931 /* We're done with the linkage-specification. */
6932 pop_lang_context ();
6933 }
6934
6935 /* Special member functions [gram.special] */
6936
6937 /* Parse a conversion-function-id.
6938
6939 conversion-function-id:
6940 operator conversion-type-id
6941
6942 Returns an IDENTIFIER_NODE representing the operator. */
6943
6944 static tree
cp_parser_conversion_function_id(cp_parser * parser)6945 cp_parser_conversion_function_id (cp_parser* parser)
6946 {
6947 tree type;
6948 tree saved_scope;
6949 tree saved_qualifying_scope;
6950 tree saved_object_scope;
6951 bool pop_p = false;
6952
6953 /* Look for the `operator' token. */
6954 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6955 return error_mark_node;
6956 /* When we parse the conversion-type-id, the current scope will be
6957 reset. However, we need that information in able to look up the
6958 conversion function later, so we save it here. */
6959 saved_scope = parser->scope;
6960 saved_qualifying_scope = parser->qualifying_scope;
6961 saved_object_scope = parser->object_scope;
6962 /* We must enter the scope of the class so that the names of
6963 entities declared within the class are available in the
6964 conversion-type-id. For example, consider:
6965
6966 struct S {
6967 typedef int I;
6968 operator I();
6969 };
6970
6971 S::operator I() { ... }
6972
6973 In order to see that `I' is a type-name in the definition, we
6974 must be in the scope of `S'. */
6975 if (saved_scope)
6976 pop_p = push_scope (saved_scope);
6977 /* Parse the conversion-type-id. */
6978 type = cp_parser_conversion_type_id (parser);
6979 /* Leave the scope of the class, if any. */
6980 if (pop_p)
6981 pop_scope (saved_scope);
6982 /* Restore the saved scope. */
6983 parser->scope = saved_scope;
6984 parser->qualifying_scope = saved_qualifying_scope;
6985 parser->object_scope = saved_object_scope;
6986 /* If the TYPE is invalid, indicate failure. */
6987 if (type == error_mark_node)
6988 return error_mark_node;
6989 return mangle_conv_op_name_for_type (type);
6990 }
6991
6992 /* Parse a conversion-type-id:
6993
6994 conversion-type-id:
6995 type-specifier-seq conversion-declarator [opt]
6996
6997 Returns the TYPE specified. */
6998
6999 static tree
cp_parser_conversion_type_id(cp_parser * parser)7000 cp_parser_conversion_type_id (cp_parser* parser)
7001 {
7002 tree attributes;
7003 tree type_specifiers;
7004 tree declarator;
7005
7006 /* Parse the attributes. */
7007 attributes = cp_parser_attributes_opt (parser);
7008 /* Parse the type-specifiers. */
7009 type_specifiers = cp_parser_type_specifier_seq (parser);
7010 /* If that didn't work, stop. */
7011 if (type_specifiers == error_mark_node)
7012 return error_mark_node;
7013 /* Parse the conversion-declarator. */
7014 declarator = cp_parser_conversion_declarator_opt (parser);
7015
7016 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7017 /*initialized=*/0, &attributes);
7018 }
7019
7020 /* Parse an (optional) conversion-declarator.
7021
7022 conversion-declarator:
7023 ptr-operator conversion-declarator [opt]
7024
7025 Returns a representation of the declarator. See
7026 cp_parser_declarator for details. */
7027
7028 static tree
cp_parser_conversion_declarator_opt(cp_parser * parser)7029 cp_parser_conversion_declarator_opt (cp_parser* parser)
7030 {
7031 enum tree_code code;
7032 tree class_type;
7033 tree cv_qualifier_seq;
7034
7035 /* We don't know if there's a ptr-operator next, or not. */
7036 cp_parser_parse_tentatively (parser);
7037 /* Try the ptr-operator. */
7038 code = cp_parser_ptr_operator (parser, &class_type,
7039 &cv_qualifier_seq);
7040 /* If it worked, look for more conversion-declarators. */
7041 if (cp_parser_parse_definitely (parser))
7042 {
7043 tree declarator;
7044
7045 /* Parse another optional declarator. */
7046 declarator = cp_parser_conversion_declarator_opt (parser);
7047
7048 /* Create the representation of the declarator. */
7049 if (code == INDIRECT_REF)
7050 declarator = make_pointer_declarator (cv_qualifier_seq,
7051 declarator);
7052 else
7053 declarator = make_reference_declarator (cv_qualifier_seq,
7054 declarator);
7055
7056 /* Handle the pointer-to-member case. */
7057 if (class_type)
7058 declarator = build_nt (SCOPE_REF, class_type, declarator);
7059
7060 return declarator;
7061 }
7062
7063 return NULL_TREE;
7064 }
7065
7066 /* Parse an (optional) ctor-initializer.
7067
7068 ctor-initializer:
7069 : mem-initializer-list
7070
7071 Returns TRUE iff the ctor-initializer was actually present. */
7072
7073 static bool
cp_parser_ctor_initializer_opt(cp_parser * parser)7074 cp_parser_ctor_initializer_opt (cp_parser* parser)
7075 {
7076 /* If the next token is not a `:', then there is no
7077 ctor-initializer. */
7078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7079 {
7080 /* Do default initialization of any bases and members. */
7081 if (DECL_CONSTRUCTOR_P (current_function_decl))
7082 finish_mem_initializers (NULL_TREE);
7083
7084 return false;
7085 }
7086
7087 /* Consume the `:' token. */
7088 cp_lexer_consume_token (parser->lexer);
7089 /* And the mem-initializer-list. */
7090 cp_parser_mem_initializer_list (parser);
7091
7092 return true;
7093 }
7094
7095 /* Parse a mem-initializer-list.
7096
7097 mem-initializer-list:
7098 mem-initializer
7099 mem-initializer , mem-initializer-list */
7100
7101 static void
cp_parser_mem_initializer_list(cp_parser * parser)7102 cp_parser_mem_initializer_list (cp_parser* parser)
7103 {
7104 tree mem_initializer_list = NULL_TREE;
7105
7106 /* Let the semantic analysis code know that we are starting the
7107 mem-initializer-list. */
7108 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7109 error ("only constructors take base initializers");
7110
7111 /* Loop through the list. */
7112 while (true)
7113 {
7114 tree mem_initializer;
7115
7116 /* Parse the mem-initializer. */
7117 mem_initializer = cp_parser_mem_initializer (parser);
7118 /* Add it to the list, unless it was erroneous. */
7119 if (mem_initializer)
7120 {
7121 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7122 mem_initializer_list = mem_initializer;
7123 }
7124 /* If the next token is not a `,', we're done. */
7125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7126 break;
7127 /* Consume the `,' token. */
7128 cp_lexer_consume_token (parser->lexer);
7129 }
7130
7131 /* Perform semantic analysis. */
7132 if (DECL_CONSTRUCTOR_P (current_function_decl))
7133 finish_mem_initializers (mem_initializer_list);
7134 }
7135
7136 /* Parse a mem-initializer.
7137
7138 mem-initializer:
7139 mem-initializer-id ( expression-list [opt] )
7140
7141 GNU extension:
7142
7143 mem-initializer:
7144 ( expression-list [opt] )
7145
7146 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7147 class) or FIELD_DECL (for a non-static data member) to initialize;
7148 the TREE_VALUE is the expression-list. */
7149
7150 static tree
cp_parser_mem_initializer(cp_parser * parser)7151 cp_parser_mem_initializer (cp_parser* parser)
7152 {
7153 tree mem_initializer_id;
7154 tree expression_list;
7155 tree member;
7156
7157 /* Find out what is being initialized. */
7158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7159 {
7160 pedwarn ("anachronistic old-style base class initializer");
7161 mem_initializer_id = NULL_TREE;
7162 }
7163 else
7164 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7165 member = expand_member_init (mem_initializer_id);
7166 if (member && !DECL_P (member))
7167 in_base_initializer = 1;
7168
7169 expression_list
7170 = cp_parser_parenthesized_expression_list (parser, false,
7171 /*non_constant_p=*/NULL);
7172 if (!expression_list)
7173 expression_list = void_type_node;
7174
7175 in_base_initializer = 0;
7176
7177 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7178 }
7179
7180 /* Parse a mem-initializer-id.
7181
7182 mem-initializer-id:
7183 :: [opt] nested-name-specifier [opt] class-name
7184 identifier
7185
7186 Returns a TYPE indicating the class to be initializer for the first
7187 production. Returns an IDENTIFIER_NODE indicating the data member
7188 to be initialized for the second production. */
7189
7190 static tree
cp_parser_mem_initializer_id(cp_parser * parser)7191 cp_parser_mem_initializer_id (cp_parser* parser)
7192 {
7193 bool global_scope_p;
7194 bool nested_name_specifier_p;
7195 bool template_p = false;
7196 tree id;
7197
7198 /* `typename' is not allowed in this context ([temp.res]). */
7199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7200 {
7201 error ("keyword `typename' not allowed in this context (a qualified "
7202 "member initializer is implicitly a type)");
7203 cp_lexer_consume_token (parser->lexer);
7204 }
7205 /* Look for the optional `::' operator. */
7206 global_scope_p
7207 = (cp_parser_global_scope_opt (parser,
7208 /*current_scope_valid_p=*/false)
7209 != NULL_TREE);
7210 /* Look for the optional nested-name-specifier. The simplest way to
7211 implement:
7212
7213 [temp.res]
7214
7215 The keyword `typename' is not permitted in a base-specifier or
7216 mem-initializer; in these contexts a qualified name that
7217 depends on a template-parameter is implicitly assumed to be a
7218 type name.
7219
7220 is to assume that we have seen the `typename' keyword at this
7221 point. */
7222 nested_name_specifier_p
7223 = (cp_parser_nested_name_specifier_opt (parser,
7224 /*typename_keyword_p=*/true,
7225 /*check_dependency_p=*/true,
7226 /*type_p=*/true,
7227 /*is_declaration=*/true)
7228 != NULL_TREE);
7229 if (nested_name_specifier_p)
7230 template_p = cp_parser_optional_template_keyword (parser);
7231 /* If there is a `::' operator or a nested-name-specifier, then we
7232 are definitely looking for a class-name. */
7233 if (global_scope_p || nested_name_specifier_p)
7234 return cp_parser_class_name (parser,
7235 /*typename_keyword_p=*/true,
7236 /*template_keyword_p=*/template_p,
7237 /*type_p=*/false,
7238 /*check_dependency_p=*/true,
7239 /*class_head_p=*/false,
7240 /*is_declaration=*/true);
7241 /* Otherwise, we could also be looking for an ordinary identifier. */
7242 cp_parser_parse_tentatively (parser);
7243 /* Try a class-name. */
7244 id = cp_parser_class_name (parser,
7245 /*typename_keyword_p=*/true,
7246 /*template_keyword_p=*/false,
7247 /*type_p=*/false,
7248 /*check_dependency_p=*/true,
7249 /*class_head_p=*/false,
7250 /*is_declaration=*/true);
7251 /* If we found one, we're done. */
7252 if (cp_parser_parse_definitely (parser))
7253 return id;
7254 /* Otherwise, look for an ordinary identifier. */
7255 return cp_parser_identifier (parser);
7256 }
7257
7258 /* Overloading [gram.over] */
7259
7260 /* Parse an operator-function-id.
7261
7262 operator-function-id:
7263 operator operator
7264
7265 Returns an IDENTIFIER_NODE for the operator which is a
7266 human-readable spelling of the identifier, e.g., `operator +'. */
7267
7268 static tree
cp_parser_operator_function_id(cp_parser * parser)7269 cp_parser_operator_function_id (cp_parser* parser)
7270 {
7271 /* Look for the `operator' keyword. */
7272 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7273 return error_mark_node;
7274 /* And then the name of the operator itself. */
7275 return cp_parser_operator (parser);
7276 }
7277
7278 /* Parse an operator.
7279
7280 operator:
7281 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7282 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7283 || ++ -- , ->* -> () []
7284
7285 GNU Extensions:
7286
7287 operator:
7288 <? >? <?= >?=
7289
7290 Returns an IDENTIFIER_NODE for the operator which is a
7291 human-readable spelling of the identifier, e.g., `operator +'. */
7292
7293 static tree
cp_parser_operator(cp_parser * parser)7294 cp_parser_operator (cp_parser* parser)
7295 {
7296 tree id = NULL_TREE;
7297 cp_token *token;
7298
7299 /* Peek at the next token. */
7300 token = cp_lexer_peek_token (parser->lexer);
7301 /* Figure out which operator we have. */
7302 switch (token->type)
7303 {
7304 case CPP_KEYWORD:
7305 {
7306 enum tree_code op;
7307
7308 /* The keyword should be either `new' or `delete'. */
7309 if (token->keyword == RID_NEW)
7310 op = NEW_EXPR;
7311 else if (token->keyword == RID_DELETE)
7312 op = DELETE_EXPR;
7313 else
7314 break;
7315
7316 /* Consume the `new' or `delete' token. */
7317 cp_lexer_consume_token (parser->lexer);
7318
7319 /* Peek at the next token. */
7320 token = cp_lexer_peek_token (parser->lexer);
7321 /* If it's a `[' token then this is the array variant of the
7322 operator. */
7323 if (token->type == CPP_OPEN_SQUARE)
7324 {
7325 /* Consume the `[' token. */
7326 cp_lexer_consume_token (parser->lexer);
7327 /* Look for the `]' token. */
7328 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7329 id = ansi_opname (op == NEW_EXPR
7330 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7331 }
7332 /* Otherwise, we have the non-array variant. */
7333 else
7334 id = ansi_opname (op);
7335
7336 return id;
7337 }
7338
7339 case CPP_PLUS:
7340 id = ansi_opname (PLUS_EXPR);
7341 break;
7342
7343 case CPP_MINUS:
7344 id = ansi_opname (MINUS_EXPR);
7345 break;
7346
7347 case CPP_MULT:
7348 id = ansi_opname (MULT_EXPR);
7349 break;
7350
7351 case CPP_DIV:
7352 id = ansi_opname (TRUNC_DIV_EXPR);
7353 break;
7354
7355 case CPP_MOD:
7356 id = ansi_opname (TRUNC_MOD_EXPR);
7357 break;
7358
7359 case CPP_XOR:
7360 id = ansi_opname (BIT_XOR_EXPR);
7361 break;
7362
7363 case CPP_AND:
7364 id = ansi_opname (BIT_AND_EXPR);
7365 break;
7366
7367 case CPP_OR:
7368 id = ansi_opname (BIT_IOR_EXPR);
7369 break;
7370
7371 case CPP_COMPL:
7372 id = ansi_opname (BIT_NOT_EXPR);
7373 break;
7374
7375 case CPP_NOT:
7376 id = ansi_opname (TRUTH_NOT_EXPR);
7377 break;
7378
7379 case CPP_EQ:
7380 id = ansi_assopname (NOP_EXPR);
7381 break;
7382
7383 case CPP_LESS:
7384 id = ansi_opname (LT_EXPR);
7385 break;
7386
7387 case CPP_GREATER:
7388 id = ansi_opname (GT_EXPR);
7389 break;
7390
7391 case CPP_PLUS_EQ:
7392 id = ansi_assopname (PLUS_EXPR);
7393 break;
7394
7395 case CPP_MINUS_EQ:
7396 id = ansi_assopname (MINUS_EXPR);
7397 break;
7398
7399 case CPP_MULT_EQ:
7400 id = ansi_assopname (MULT_EXPR);
7401 break;
7402
7403 case CPP_DIV_EQ:
7404 id = ansi_assopname (TRUNC_DIV_EXPR);
7405 break;
7406
7407 case CPP_MOD_EQ:
7408 id = ansi_assopname (TRUNC_MOD_EXPR);
7409 break;
7410
7411 case CPP_XOR_EQ:
7412 id = ansi_assopname (BIT_XOR_EXPR);
7413 break;
7414
7415 case CPP_AND_EQ:
7416 id = ansi_assopname (BIT_AND_EXPR);
7417 break;
7418
7419 case CPP_OR_EQ:
7420 id = ansi_assopname (BIT_IOR_EXPR);
7421 break;
7422
7423 case CPP_LSHIFT:
7424 id = ansi_opname (LSHIFT_EXPR);
7425 break;
7426
7427 case CPP_RSHIFT:
7428 id = ansi_opname (RSHIFT_EXPR);
7429 break;
7430
7431 case CPP_LSHIFT_EQ:
7432 id = ansi_assopname (LSHIFT_EXPR);
7433 break;
7434
7435 case CPP_RSHIFT_EQ:
7436 id = ansi_assopname (RSHIFT_EXPR);
7437 break;
7438
7439 case CPP_EQ_EQ:
7440 id = ansi_opname (EQ_EXPR);
7441 break;
7442
7443 case CPP_NOT_EQ:
7444 id = ansi_opname (NE_EXPR);
7445 break;
7446
7447 case CPP_LESS_EQ:
7448 id = ansi_opname (LE_EXPR);
7449 break;
7450
7451 case CPP_GREATER_EQ:
7452 id = ansi_opname (GE_EXPR);
7453 break;
7454
7455 case CPP_AND_AND:
7456 id = ansi_opname (TRUTH_ANDIF_EXPR);
7457 break;
7458
7459 case CPP_OR_OR:
7460 id = ansi_opname (TRUTH_ORIF_EXPR);
7461 break;
7462
7463 case CPP_PLUS_PLUS:
7464 id = ansi_opname (POSTINCREMENT_EXPR);
7465 break;
7466
7467 case CPP_MINUS_MINUS:
7468 id = ansi_opname (PREDECREMENT_EXPR);
7469 break;
7470
7471 case CPP_COMMA:
7472 id = ansi_opname (COMPOUND_EXPR);
7473 break;
7474
7475 case CPP_DEREF_STAR:
7476 id = ansi_opname (MEMBER_REF);
7477 break;
7478
7479 case CPP_DEREF:
7480 id = ansi_opname (COMPONENT_REF);
7481 break;
7482
7483 case CPP_OPEN_PAREN:
7484 /* Consume the `('. */
7485 cp_lexer_consume_token (parser->lexer);
7486 /* Look for the matching `)'. */
7487 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7488 return ansi_opname (CALL_EXPR);
7489
7490 case CPP_OPEN_SQUARE:
7491 /* Consume the `['. */
7492 cp_lexer_consume_token (parser->lexer);
7493 /* Look for the matching `]'. */
7494 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7495 return ansi_opname (ARRAY_REF);
7496
7497 /* Extensions. */
7498 case CPP_MIN:
7499 id = ansi_opname (MIN_EXPR);
7500 break;
7501
7502 case CPP_MAX:
7503 id = ansi_opname (MAX_EXPR);
7504 break;
7505
7506 case CPP_MIN_EQ:
7507 id = ansi_assopname (MIN_EXPR);
7508 break;
7509
7510 case CPP_MAX_EQ:
7511 id = ansi_assopname (MAX_EXPR);
7512 break;
7513
7514 default:
7515 /* Anything else is an error. */
7516 break;
7517 }
7518
7519 /* If we have selected an identifier, we need to consume the
7520 operator token. */
7521 if (id)
7522 cp_lexer_consume_token (parser->lexer);
7523 /* Otherwise, no valid operator name was present. */
7524 else
7525 {
7526 cp_parser_error (parser, "expected operator");
7527 id = error_mark_node;
7528 }
7529
7530 return id;
7531 }
7532
7533 /* Parse a template-declaration.
7534
7535 template-declaration:
7536 export [opt] template < template-parameter-list > declaration
7537
7538 If MEMBER_P is TRUE, this template-declaration occurs within a
7539 class-specifier.
7540
7541 The grammar rule given by the standard isn't correct. What
7542 is really meant is:
7543
7544 template-declaration:
7545 export [opt] template-parameter-list-seq
7546 decl-specifier-seq [opt] init-declarator [opt] ;
7547 export [opt] template-parameter-list-seq
7548 function-definition
7549
7550 template-parameter-list-seq:
7551 template-parameter-list-seq [opt]
7552 template < template-parameter-list > */
7553
7554 static void
cp_parser_template_declaration(cp_parser * parser,bool member_p)7555 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7556 {
7557 /* Check for `export'. */
7558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7559 {
7560 /* Consume the `export' token. */
7561 cp_lexer_consume_token (parser->lexer);
7562 /* Warn that we do not support `export'. */
7563 warning ("keyword `export' not implemented, and will be ignored");
7564 }
7565
7566 cp_parser_template_declaration_after_export (parser, member_p);
7567 }
7568
7569 /* Parse a template-parameter-list.
7570
7571 template-parameter-list:
7572 template-parameter
7573 template-parameter-list , template-parameter
7574
7575 Returns a TREE_LIST. Each node represents a template parameter.
7576 The nodes are connected via their TREE_CHAINs. */
7577
7578 static tree
cp_parser_template_parameter_list(cp_parser * parser)7579 cp_parser_template_parameter_list (cp_parser* parser)
7580 {
7581 tree parameter_list = NULL_TREE;
7582
7583 while (true)
7584 {
7585 tree parameter;
7586 cp_token *token;
7587
7588 /* Parse the template-parameter. */
7589 parameter = cp_parser_template_parameter (parser);
7590 /* Add it to the list. */
7591 parameter_list = process_template_parm (parameter_list,
7592 parameter);
7593
7594 /* Peek at the next token. */
7595 token = cp_lexer_peek_token (parser->lexer);
7596 /* If it's not a `,', we're done. */
7597 if (token->type != CPP_COMMA)
7598 break;
7599 /* Otherwise, consume the `,' token. */
7600 cp_lexer_consume_token (parser->lexer);
7601 }
7602
7603 return parameter_list;
7604 }
7605
7606 /* Parse a template-parameter.
7607
7608 template-parameter:
7609 type-parameter
7610 parameter-declaration
7611
7612 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7613 TREE_PURPOSE is the default value, if any. */
7614
7615 static tree
cp_parser_template_parameter(cp_parser * parser)7616 cp_parser_template_parameter (cp_parser* parser)
7617 {
7618 cp_token *token;
7619
7620 /* Peek at the next token. */
7621 token = cp_lexer_peek_token (parser->lexer);
7622 /* If it is `class' or `template', we have a type-parameter. */
7623 if (token->keyword == RID_TEMPLATE)
7624 return cp_parser_type_parameter (parser);
7625 /* If it is `class' or `typename' we do not know yet whether it is a
7626 type parameter or a non-type parameter. Consider:
7627
7628 template <typename T, typename T::X X> ...
7629
7630 or:
7631
7632 template <class C, class D*> ...
7633
7634 Here, the first parameter is a type parameter, and the second is
7635 a non-type parameter. We can tell by looking at the token after
7636 the identifier -- if it is a `,', `=', or `>' then we have a type
7637 parameter. */
7638 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7639 {
7640 /* Peek at the token after `class' or `typename'. */
7641 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7642 /* If it's an identifier, skip it. */
7643 if (token->type == CPP_NAME)
7644 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7645 /* Now, see if the token looks like the end of a template
7646 parameter. */
7647 if (token->type == CPP_COMMA
7648 || token->type == CPP_EQ
7649 || token->type == CPP_GREATER)
7650 return cp_parser_type_parameter (parser);
7651 }
7652
7653 /* Otherwise, it is a non-type parameter.
7654
7655 [temp.param]
7656
7657 When parsing a default template-argument for a non-type
7658 template-parameter, the first non-nested `>' is taken as the end
7659 of the template parameter-list rather than a greater-than
7660 operator. */
7661 return
7662 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7663 /*parenthesized_p=*/NULL);
7664 }
7665
7666 /* Parse a type-parameter.
7667
7668 type-parameter:
7669 class identifier [opt]
7670 class identifier [opt] = type-id
7671 typename identifier [opt]
7672 typename identifier [opt] = type-id
7673 template < template-parameter-list > class identifier [opt]
7674 template < template-parameter-list > class identifier [opt]
7675 = id-expression
7676
7677 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7678 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7679 the declaration of the parameter. */
7680
7681 static tree
cp_parser_type_parameter(cp_parser * parser)7682 cp_parser_type_parameter (cp_parser* parser)
7683 {
7684 cp_token *token;
7685 tree parameter;
7686
7687 /* Look for a keyword to tell us what kind of parameter this is. */
7688 token = cp_parser_require (parser, CPP_KEYWORD,
7689 "`class', `typename', or `template'");
7690 if (!token)
7691 return error_mark_node;
7692
7693 switch (token->keyword)
7694 {
7695 case RID_CLASS:
7696 case RID_TYPENAME:
7697 {
7698 tree identifier;
7699 tree default_argument;
7700
7701 /* If the next token is an identifier, then it names the
7702 parameter. */
7703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7704 identifier = cp_parser_identifier (parser);
7705 else
7706 identifier = NULL_TREE;
7707
7708 /* Create the parameter. */
7709 parameter = finish_template_type_parm (class_type_node, identifier);
7710
7711 /* If the next token is an `=', we have a default argument. */
7712 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7713 {
7714 /* Consume the `=' token. */
7715 cp_lexer_consume_token (parser->lexer);
7716 /* Parse the default-argument. */
7717 default_argument = cp_parser_type_id (parser);
7718 }
7719 else
7720 default_argument = NULL_TREE;
7721
7722 /* Create the combined representation of the parameter and the
7723 default argument. */
7724 parameter = build_tree_list (default_argument, parameter);
7725 }
7726 break;
7727
7728 case RID_TEMPLATE:
7729 {
7730 tree parameter_list;
7731 tree identifier;
7732 tree default_argument;
7733
7734 /* Look for the `<'. */
7735 cp_parser_require (parser, CPP_LESS, "`<'");
7736 /* Parse the template-parameter-list. */
7737 begin_template_parm_list ();
7738 parameter_list
7739 = cp_parser_template_parameter_list (parser);
7740 parameter_list = end_template_parm_list (parameter_list);
7741 /* Look for the `>'. */
7742 cp_parser_require (parser, CPP_GREATER, "`>'");
7743 /* Look for the `class' keyword. */
7744 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7745 /* If the next token is an `=', then there is a
7746 default-argument. If the next token is a `>', we are at
7747 the end of the parameter-list. If the next token is a `,',
7748 then we are at the end of this parameter. */
7749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7750 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7751 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7752 identifier = cp_parser_identifier (parser);
7753 else
7754 identifier = NULL_TREE;
7755 /* Create the template parameter. */
7756 parameter = finish_template_template_parm (class_type_node,
7757 identifier);
7758
7759 /* If the next token is an `=', then there is a
7760 default-argument. */
7761 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7762 {
7763 bool is_template;
7764
7765 /* Consume the `='. */
7766 cp_lexer_consume_token (parser->lexer);
7767 /* Parse the id-expression. */
7768 default_argument
7769 = cp_parser_id_expression (parser,
7770 /*template_keyword_p=*/false,
7771 /*check_dependency_p=*/true,
7772 /*template_p=*/&is_template,
7773 /*declarator_p=*/false);
7774 if (TREE_CODE (default_argument) == TYPE_DECL)
7775 /* If the id-expression was a template-id that refers to
7776 a template-class, we already have the declaration here,
7777 so no further lookup is needed. */
7778 ;
7779 else
7780 /* Look up the name. */
7781 default_argument
7782 = cp_parser_lookup_name (parser, default_argument,
7783 /*is_type=*/false,
7784 /*is_template=*/is_template,
7785 /*is_namespace=*/false,
7786 /*check_dependency=*/true);
7787 /* See if the default argument is valid. */
7788 default_argument
7789 = check_template_template_default_arg (default_argument);
7790 }
7791 else
7792 default_argument = NULL_TREE;
7793
7794 /* Create the combined representation of the parameter and the
7795 default argument. */
7796 parameter = build_tree_list (default_argument, parameter);
7797 }
7798 break;
7799
7800 default:
7801 /* Anything else is an error. */
7802 cp_parser_error (parser,
7803 "expected `class', `typename', or `template'");
7804 parameter = error_mark_node;
7805 }
7806
7807 return parameter;
7808 }
7809
7810 /* Parse a template-id.
7811
7812 template-id:
7813 template-name < template-argument-list [opt] >
7814
7815 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7816 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7817 returned. Otherwise, if the template-name names a function, or set
7818 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7819 names a class, returns a TYPE_DECL for the specialization.
7820
7821 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7822 uninstantiated templates. */
7823
7824 static tree
cp_parser_template_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool is_declaration)7825 cp_parser_template_id (cp_parser *parser,
7826 bool template_keyword_p,
7827 bool check_dependency_p,
7828 bool is_declaration)
7829 {
7830 tree template;
7831 tree arguments;
7832 tree template_id;
7833 ptrdiff_t start_of_id;
7834 tree access_check = NULL_TREE;
7835 cp_token *next_token, *next_token_2;
7836 bool is_identifier;
7837
7838 /* If the next token corresponds to a template-id, there is no need
7839 to reparse it. */
7840 next_token = cp_lexer_peek_token (parser->lexer);
7841 if (next_token->type == CPP_TEMPLATE_ID)
7842 {
7843 tree value;
7844 tree check;
7845
7846 /* Get the stored value. */
7847 value = cp_lexer_consume_token (parser->lexer)->value;
7848 /* Perform any access checks that were deferred. */
7849 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7850 perform_or_defer_access_check (TREE_PURPOSE (check),
7851 TREE_VALUE (check));
7852 /* Return the stored value. */
7853 return TREE_VALUE (value);
7854 }
7855
7856 /* Avoid performing name lookup if there is no possibility of
7857 finding a template-id. */
7858 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7859 || (next_token->type == CPP_NAME
7860 && !cp_parser_nth_token_starts_template_argument_list_p
7861 (parser, 2)))
7862 {
7863 cp_parser_error (parser, "expected template-id");
7864 return error_mark_node;
7865 }
7866
7867 /* Remember where the template-id starts. */
7868 if (cp_parser_parsing_tentatively (parser)
7869 && !cp_parser_committed_to_tentative_parse (parser))
7870 {
7871 next_token = cp_lexer_peek_token (parser->lexer);
7872 start_of_id = cp_lexer_token_difference (parser->lexer,
7873 parser->lexer->first_token,
7874 next_token);
7875 }
7876 else
7877 start_of_id = -1;
7878
7879 push_deferring_access_checks (dk_deferred);
7880
7881 /* Parse the template-name. */
7882 is_identifier = false;
7883 template = cp_parser_template_name (parser, template_keyword_p,
7884 check_dependency_p,
7885 is_declaration,
7886 &is_identifier);
7887 if (template == error_mark_node || is_identifier)
7888 {
7889 pop_deferring_access_checks ();
7890 return template;
7891 }
7892
7893 /* If we find the sequence `[:' after a template-name, it's probably
7894 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7895 parse correctly the argument list. */
7896 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7897 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7898 if (next_token->type == CPP_OPEN_SQUARE
7899 && next_token->flags & DIGRAPH
7900 && next_token_2->type == CPP_COLON
7901 && !(next_token_2->flags & PREV_WHITE))
7902 {
7903 cp_parser_parse_tentatively (parser);
7904 /* Change `:' into `::'. */
7905 next_token_2->type = CPP_SCOPE;
7906 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7907 CPP_LESS. */
7908 cp_lexer_consume_token (parser->lexer);
7909 /* Parse the arguments. */
7910 arguments = cp_parser_enclosed_template_argument_list (parser);
7911 if (!cp_parser_parse_definitely (parser))
7912 {
7913 /* If we couldn't parse an argument list, then we revert our changes
7914 and return simply an error. Maybe this is not a template-id
7915 after all. */
7916 next_token_2->type = CPP_COLON;
7917 cp_parser_error (parser, "expected `<'");
7918 pop_deferring_access_checks ();
7919 return error_mark_node;
7920 }
7921 /* Otherwise, emit an error about the invalid digraph, but continue
7922 parsing because we got our argument list. */
7923 pedwarn ("`<::' cannot begin a template-argument list");
7924 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7925 "between `<' and `::'");
7926 if (!flag_permissive)
7927 {
7928 static bool hint;
7929 if (!hint)
7930 {
7931 inform ("(if you use `-fpermissive' G++ will accept your code)");
7932 hint = true;
7933 }
7934 }
7935 }
7936 else
7937 {
7938 /* Look for the `<' that starts the template-argument-list. */
7939 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7940 {
7941 pop_deferring_access_checks ();
7942 return error_mark_node;
7943 }
7944 /* Parse the arguments. */
7945 arguments = cp_parser_enclosed_template_argument_list (parser);
7946 }
7947
7948 /* Build a representation of the specialization. */
7949 if (TREE_CODE (template) == IDENTIFIER_NODE)
7950 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7951 else if (DECL_CLASS_TEMPLATE_P (template)
7952 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7953 template_id
7954 = finish_template_type (template, arguments,
7955 cp_lexer_next_token_is (parser->lexer,
7956 CPP_SCOPE));
7957 else
7958 {
7959 /* If it's not a class-template or a template-template, it should be
7960 a function-template. */
7961 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7962 || TREE_CODE (template) == OVERLOAD
7963 || BASELINK_P (template)),
7964 20010716);
7965
7966 template_id = lookup_template_function (template, arguments);
7967 }
7968
7969 /* Retrieve any deferred checks. Do not pop this access checks yet
7970 so the memory will not be reclaimed during token replacing below. */
7971 access_check = get_deferred_access_checks ();
7972
7973 /* If parsing tentatively, replace the sequence of tokens that makes
7974 up the template-id with a CPP_TEMPLATE_ID token. That way,
7975 should we re-parse the token stream, we will not have to repeat
7976 the effort required to do the parse, nor will we issue duplicate
7977 error messages about problems during instantiation of the
7978 template. */
7979 if (start_of_id >= 0)
7980 {
7981 cp_token *token;
7982
7983 /* Find the token that corresponds to the start of the
7984 template-id. */
7985 token = cp_lexer_advance_token (parser->lexer,
7986 parser->lexer->first_token,
7987 start_of_id);
7988
7989 /* Reset the contents of the START_OF_ID token. */
7990 token->type = CPP_TEMPLATE_ID;
7991 token->value = build_tree_list (access_check, template_id);
7992 token->keyword = RID_MAX;
7993 /* Purge all subsequent tokens. */
7994 cp_lexer_purge_tokens_after (parser->lexer, token);
7995 }
7996
7997 pop_deferring_access_checks ();
7998 return template_id;
7999 }
8000
8001 /* Parse a template-name.
8002
8003 template-name:
8004 identifier
8005
8006 The standard should actually say:
8007
8008 template-name:
8009 identifier
8010 operator-function-id
8011
8012 A defect report has been filed about this issue.
8013
8014 A conversion-function-id cannot be a template name because they cannot
8015 be part of a template-id. In fact, looking at this code:
8016
8017 a.operator K<int>()
8018
8019 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8020 It is impossible to call a templated conversion-function-id with an
8021 explicit argument list, since the only allowed template parameter is
8022 the type to which it is converting.
8023
8024 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8025 `template' keyword, in a construction like:
8026
8027 T::template f<3>()
8028
8029 In that case `f' is taken to be a template-name, even though there
8030 is no way of knowing for sure.
8031
8032 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8033 name refers to a set of overloaded functions, at least one of which
8034 is a template, or an IDENTIFIER_NODE with the name of the template,
8035 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8036 names are looked up inside uninstantiated templates. */
8037
8038 static tree
cp_parser_template_name(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool is_declaration,bool * is_identifier)8039 cp_parser_template_name (cp_parser* parser,
8040 bool template_keyword_p,
8041 bool check_dependency_p,
8042 bool is_declaration,
8043 bool *is_identifier)
8044 {
8045 tree identifier;
8046 tree decl;
8047 tree fns;
8048
8049 /* If the next token is `operator', then we have either an
8050 operator-function-id or a conversion-function-id. */
8051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8052 {
8053 /* We don't know whether we're looking at an
8054 operator-function-id or a conversion-function-id. */
8055 cp_parser_parse_tentatively (parser);
8056 /* Try an operator-function-id. */
8057 identifier = cp_parser_operator_function_id (parser);
8058 /* If that didn't work, try a conversion-function-id. */
8059 if (!cp_parser_parse_definitely (parser))
8060 {
8061 cp_parser_error (parser, "expected template-name");
8062 return error_mark_node;
8063 }
8064 }
8065 /* Look for the identifier. */
8066 else
8067 identifier = cp_parser_identifier (parser);
8068
8069 /* If we didn't find an identifier, we don't have a template-id. */
8070 if (identifier == error_mark_node)
8071 return error_mark_node;
8072
8073 /* If the name immediately followed the `template' keyword, then it
8074 is a template-name. However, if the next token is not `<', then
8075 we do not treat it as a template-name, since it is not being used
8076 as part of a template-id. This enables us to handle constructs
8077 like:
8078
8079 template <typename T> struct S { S(); };
8080 template <typename T> S<T>::S();
8081
8082 correctly. We would treat `S' as a template -- if it were `S<T>'
8083 -- but we do not if there is no `<'. */
8084
8085 if (processing_template_decl
8086 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8087 {
8088 /* In a declaration, in a dependent context, we pretend that the
8089 "template" keyword was present in order to improve error
8090 recovery. For example, given:
8091
8092 template <typename T> void f(T::X<int>);
8093
8094 we want to treat "X<int>" as a template-id. */
8095 if (is_declaration
8096 && !template_keyword_p
8097 && parser->scope && TYPE_P (parser->scope)
8098 && dependent_type_p (parser->scope)
8099 /* Do not do this for dtors (or ctors), since they never
8100 need the template keyword before their name. */
8101 && !constructor_name_p (identifier, parser->scope))
8102 {
8103 ptrdiff_t start;
8104 cp_token* token;
8105 /* Explain what went wrong. */
8106 error ("non-template `%D' used as template", identifier);
8107 inform ("use `%T::template %D' to indicate that it is a template",
8108 parser->scope, identifier);
8109 /* If parsing tentatively, find the location of the "<"
8110 token. */
8111 if (cp_parser_parsing_tentatively (parser)
8112 && !cp_parser_committed_to_tentative_parse (parser))
8113 {
8114 cp_parser_simulate_error (parser);
8115 token = cp_lexer_peek_token (parser->lexer);
8116 token = cp_lexer_prev_token (parser->lexer, token);
8117 start = cp_lexer_token_difference (parser->lexer,
8118 parser->lexer->first_token,
8119 token);
8120 }
8121 else
8122 start = -1;
8123 /* Parse the template arguments so that we can issue error
8124 messages about them. */
8125 cp_lexer_consume_token (parser->lexer);
8126 cp_parser_enclosed_template_argument_list (parser);
8127 /* Skip tokens until we find a good place from which to
8128 continue parsing. */
8129 cp_parser_skip_to_closing_parenthesis (parser,
8130 /*recovering=*/true,
8131 /*or_comma=*/true,
8132 /*consume_paren=*/false);
8133 /* If parsing tentatively, permanently remove the
8134 template argument list. That will prevent duplicate
8135 error messages from being issued about the missing
8136 "template" keyword. */
8137 if (start >= 0)
8138 {
8139 token = cp_lexer_advance_token (parser->lexer,
8140 parser->lexer->first_token,
8141 start);
8142 cp_lexer_purge_tokens_after (parser->lexer, token);
8143 }
8144 if (is_identifier)
8145 *is_identifier = true;
8146 return identifier;
8147 }
8148
8149 /* If the "template" keyword is present, then there is generally
8150 no point in doing name-lookup, so we just return IDENTIFIER.
8151 But, if the qualifying scope is non-dependent then we can
8152 (and must) do name-lookup normally. */
8153 if (template_keyword_p
8154 && (!parser->scope
8155 || (TYPE_P (parser->scope)
8156 && dependent_type_p (parser->scope))))
8157 return identifier;
8158 }
8159
8160 /* Look up the name. */
8161 decl = cp_parser_lookup_name (parser, identifier,
8162 /*is_type=*/false,
8163 /*is_template=*/false,
8164 /*is_namespace=*/false,
8165 check_dependency_p);
8166 decl = maybe_get_template_decl_from_type_decl (decl);
8167
8168 /* If DECL is a template, then the name was a template-name. */
8169 if (TREE_CODE (decl) == TEMPLATE_DECL)
8170 ;
8171 else
8172 {
8173 /* The standard does not explicitly indicate whether a name that
8174 names a set of overloaded declarations, some of which are
8175 templates, is a template-name. However, such a name should
8176 be a template-name; otherwise, there is no way to form a
8177 template-id for the overloaded templates. */
8178 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8179 if (TREE_CODE (fns) == OVERLOAD)
8180 {
8181 tree fn;
8182
8183 for (fn = fns; fn; fn = OVL_NEXT (fn))
8184 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8185 break;
8186 }
8187 else
8188 {
8189 /* Otherwise, the name does not name a template. */
8190 cp_parser_error (parser, "expected template-name");
8191 return error_mark_node;
8192 }
8193 }
8194
8195 /* If DECL is dependent, and refers to a function, then just return
8196 its name; we will look it up again during template instantiation. */
8197 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8198 {
8199 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8200 if (TYPE_P (scope) && dependent_type_p (scope))
8201 return identifier;
8202 }
8203
8204 return decl;
8205 }
8206
8207 /* Parse a template-argument-list.
8208
8209 template-argument-list:
8210 template-argument
8211 template-argument-list , template-argument
8212
8213 Returns a TREE_VEC containing the arguments. */
8214
8215 static tree
cp_parser_template_argument_list(cp_parser * parser)8216 cp_parser_template_argument_list (cp_parser* parser)
8217 {
8218 tree fixed_args[10];
8219 unsigned n_args = 0;
8220 unsigned alloced = 10;
8221 tree *arg_ary = fixed_args;
8222 tree vec;
8223 bool saved_in_template_argument_list_p;
8224
8225 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8226 parser->in_template_argument_list_p = true;
8227 do
8228 {
8229 tree argument;
8230
8231 if (n_args)
8232 /* Consume the comma. */
8233 cp_lexer_consume_token (parser->lexer);
8234
8235 /* Parse the template-argument. */
8236 argument = cp_parser_template_argument (parser);
8237 if (n_args == alloced)
8238 {
8239 alloced *= 2;
8240
8241 if (arg_ary == fixed_args)
8242 {
8243 arg_ary = xmalloc (sizeof (tree) * alloced);
8244 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8245 }
8246 else
8247 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8248 }
8249 arg_ary[n_args++] = argument;
8250 }
8251 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8252
8253 vec = make_tree_vec (n_args);
8254
8255 while (n_args--)
8256 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8257
8258 if (arg_ary != fixed_args)
8259 free (arg_ary);
8260 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8261 return vec;
8262 }
8263
8264 /* Parse a template-argument.
8265
8266 template-argument:
8267 assignment-expression
8268 type-id
8269 id-expression
8270
8271 The representation is that of an assignment-expression, type-id, or
8272 id-expression -- except that the qualified id-expression is
8273 evaluated, so that the value returned is either a DECL or an
8274 OVERLOAD.
8275
8276 Although the standard says "assignment-expression", it forbids
8277 throw-expressions or assignments in the template argument.
8278 Therefore, we use "conditional-expression" instead. */
8279
8280 static tree
cp_parser_template_argument(cp_parser * parser)8281 cp_parser_template_argument (cp_parser* parser)
8282 {
8283 tree argument;
8284 bool template_p;
8285 bool address_p;
8286 bool maybe_type_id = false;
8287 cp_token *token;
8288 cp_id_kind idk;
8289 tree qualifying_class;
8290
8291 /* There's really no way to know what we're looking at, so we just
8292 try each alternative in order.
8293
8294 [temp.arg]
8295
8296 In a template-argument, an ambiguity between a type-id and an
8297 expression is resolved to a type-id, regardless of the form of
8298 the corresponding template-parameter.
8299
8300 Therefore, we try a type-id first. */
8301 cp_parser_parse_tentatively (parser);
8302 argument = cp_parser_type_id (parser);
8303 /* If there was no error parsing the type-id but the next token is a '>>',
8304 we probably found a typo for '> >'. But there are type-id which are
8305 also valid expressions. For instance:
8306
8307 struct X { int operator >> (int); };
8308 template <int V> struct Foo {};
8309 Foo<X () >> 5> r;
8310
8311 Here 'X()' is a valid type-id of a function type, but the user just
8312 wanted to write the expression "X() >> 5". Thus, we remember that we
8313 found a valid type-id, but we still try to parse the argument as an
8314 expression to see what happens. */
8315 if (!cp_parser_error_occurred (parser)
8316 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8317 {
8318 maybe_type_id = true;
8319 cp_parser_abort_tentative_parse (parser);
8320 }
8321 else
8322 {
8323 /* If the next token isn't a `,' or a `>', then this argument wasn't
8324 really finished. This means that the argument is not a valid
8325 type-id. */
8326 if (!cp_parser_next_token_ends_template_argument_p (parser))
8327 cp_parser_error (parser, "expected template-argument");
8328 /* If that worked, we're done. */
8329 if (cp_parser_parse_definitely (parser))
8330 return argument;
8331 }
8332 /* We're still not sure what the argument will be. */
8333 cp_parser_parse_tentatively (parser);
8334 /* Try a template. */
8335 argument = cp_parser_id_expression (parser,
8336 /*template_keyword_p=*/false,
8337 /*check_dependency_p=*/true,
8338 &template_p,
8339 /*declarator_p=*/false);
8340 /* If the next token isn't a `,' or a `>', then this argument wasn't
8341 really finished. */
8342 if (!cp_parser_next_token_ends_template_argument_p (parser))
8343 cp_parser_error (parser, "expected template-argument");
8344 if (!cp_parser_error_occurred (parser))
8345 {
8346 /* Figure out what is being referred to. If the id-expression
8347 was for a class template specialization, then we will have a
8348 TYPE_DECL at this point. There is no need to do name lookup
8349 at this point in that case. */
8350 if (TREE_CODE (argument) != TYPE_DECL)
8351 argument = cp_parser_lookup_name (parser, argument,
8352 /*is_type=*/false,
8353 /*is_template=*/template_p,
8354 /*is_namespace=*/false,
8355 /*check_dependency=*/true);
8356 if (TREE_CODE (argument) != TEMPLATE_DECL
8357 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8358 cp_parser_error (parser, "expected template-name");
8359 }
8360 if (cp_parser_parse_definitely (parser))
8361 return argument;
8362 /* It must be a non-type argument. There permitted cases are given
8363 in [temp.arg.nontype]:
8364
8365 -- an integral constant-expression of integral or enumeration
8366 type; or
8367
8368 -- the name of a non-type template-parameter; or
8369
8370 -- the name of an object or function with external linkage...
8371
8372 -- the address of an object or function with external linkage...
8373
8374 -- a pointer to member... */
8375 /* Look for a non-type template parameter. */
8376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8377 {
8378 cp_parser_parse_tentatively (parser);
8379 argument = cp_parser_primary_expression (parser,
8380 &idk,
8381 &qualifying_class);
8382 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8383 || !cp_parser_next_token_ends_template_argument_p (parser))
8384 cp_parser_simulate_error (parser);
8385 if (cp_parser_parse_definitely (parser))
8386 return argument;
8387 }
8388 /* If the next token is "&", the argument must be the address of an
8389 object or function with external linkage. */
8390 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8391 if (address_p)
8392 cp_lexer_consume_token (parser->lexer);
8393 /* See if we might have an id-expression. */
8394 token = cp_lexer_peek_token (parser->lexer);
8395 if (token->type == CPP_NAME
8396 || token->keyword == RID_OPERATOR
8397 || token->type == CPP_SCOPE
8398 || token->type == CPP_TEMPLATE_ID
8399 || token->type == CPP_NESTED_NAME_SPECIFIER)
8400 {
8401 cp_parser_parse_tentatively (parser);
8402 argument = cp_parser_primary_expression (parser,
8403 &idk,
8404 &qualifying_class);
8405 if (cp_parser_error_occurred (parser)
8406 || !cp_parser_next_token_ends_template_argument_p (parser))
8407 cp_parser_abort_tentative_parse (parser);
8408 else
8409 {
8410 if (qualifying_class)
8411 argument = finish_qualified_id_expr (qualifying_class,
8412 argument,
8413 /*done=*/true,
8414 address_p);
8415 if (TREE_CODE (argument) == VAR_DECL)
8416 {
8417 /* A variable without external linkage might still be a
8418 valid constant-expression, so no error is issued here
8419 if the external-linkage check fails. */
8420 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8421 cp_parser_simulate_error (parser);
8422 }
8423 else if (is_overloaded_fn (argument))
8424 /* All overloaded functions are allowed; if the external
8425 linkage test does not pass, an error will be issued
8426 later. */
8427 ;
8428 else if (address_p
8429 && (TREE_CODE (argument) == OFFSET_REF
8430 || TREE_CODE (argument) == SCOPE_REF))
8431 /* A pointer-to-member. */
8432 ;
8433 else
8434 cp_parser_simulate_error (parser);
8435
8436 if (cp_parser_parse_definitely (parser))
8437 {
8438 if (address_p)
8439 argument = build_x_unary_op (ADDR_EXPR, argument);
8440 return argument;
8441 }
8442 }
8443 }
8444 /* If the argument started with "&", there are no other valid
8445 alternatives at this point. */
8446 if (address_p)
8447 {
8448 cp_parser_error (parser, "invalid non-type template argument");
8449 return error_mark_node;
8450 }
8451 /* If the argument wasn't successfully parsed as a type-id followed
8452 by '>>', the argument can only be a constant expression now.
8453 Otherwise, we try parsing the constant-expression tentatively,
8454 because the argument could really be a type-id. */
8455 if (maybe_type_id)
8456 cp_parser_parse_tentatively (parser);
8457 argument = cp_parser_constant_expression (parser,
8458 /*allow_non_constant_p=*/false,
8459 /*non_constant_p=*/NULL);
8460 argument = fold_non_dependent_expr (argument);
8461 if (!maybe_type_id)
8462 return argument;
8463 if (!cp_parser_next_token_ends_template_argument_p (parser))
8464 cp_parser_error (parser, "expected template-argument");
8465 if (cp_parser_parse_definitely (parser))
8466 return argument;
8467 /* We did our best to parse the argument as a non type-id, but that
8468 was the only alternative that matched (albeit with a '>' after
8469 it). We can assume it's just a typo from the user, and a
8470 diagnostic will then be issued. */
8471 return cp_parser_type_id (parser);
8472 }
8473
8474 /* Parse an explicit-instantiation.
8475
8476 explicit-instantiation:
8477 template declaration
8478
8479 Although the standard says `declaration', what it really means is:
8480
8481 explicit-instantiation:
8482 template decl-specifier-seq [opt] declarator [opt] ;
8483
8484 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8485 supposed to be allowed. A defect report has been filed about this
8486 issue.
8487
8488 GNU Extension:
8489
8490 explicit-instantiation:
8491 storage-class-specifier template
8492 decl-specifier-seq [opt] declarator [opt] ;
8493 function-specifier template
8494 decl-specifier-seq [opt] declarator [opt] ; */
8495
8496 static void
cp_parser_explicit_instantiation(cp_parser * parser)8497 cp_parser_explicit_instantiation (cp_parser* parser)
8498 {
8499 int declares_class_or_enum;
8500 tree decl_specifiers;
8501 tree attributes;
8502 tree extension_specifier = NULL_TREE;
8503
8504 /* Look for an (optional) storage-class-specifier or
8505 function-specifier. */
8506 if (cp_parser_allow_gnu_extensions_p (parser))
8507 {
8508 extension_specifier
8509 = cp_parser_storage_class_specifier_opt (parser);
8510 if (!extension_specifier)
8511 extension_specifier = cp_parser_function_specifier_opt (parser);
8512 }
8513
8514 /* Look for the `template' keyword. */
8515 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8516 /* Let the front end know that we are processing an explicit
8517 instantiation. */
8518 begin_explicit_instantiation ();
8519 /* [temp.explicit] says that we are supposed to ignore access
8520 control while processing explicit instantiation directives. */
8521 push_deferring_access_checks (dk_no_check);
8522 /* Parse a decl-specifier-seq. */
8523 decl_specifiers
8524 = cp_parser_decl_specifier_seq (parser,
8525 CP_PARSER_FLAGS_OPTIONAL,
8526 &attributes,
8527 &declares_class_or_enum);
8528 /* If there was exactly one decl-specifier, and it declared a class,
8529 and there's no declarator, then we have an explicit type
8530 instantiation. */
8531 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8532 {
8533 tree type;
8534
8535 type = check_tag_decl (decl_specifiers);
8536 /* Turn access control back on for names used during
8537 template instantiation. */
8538 pop_deferring_access_checks ();
8539 if (type)
8540 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8541 }
8542 else
8543 {
8544 tree declarator;
8545 tree decl;
8546
8547 /* Parse the declarator. */
8548 declarator
8549 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8550 /*ctor_dtor_or_conv_p=*/NULL,
8551 /*parenthesized_p=*/NULL);
8552 cp_parser_check_for_definition_in_return_type (declarator,
8553 declares_class_or_enum);
8554 if (declarator != error_mark_node)
8555 {
8556 decl = grokdeclarator (declarator, decl_specifiers,
8557 NORMAL, 0, NULL);
8558 /* Turn access control back on for names used during
8559 template instantiation. */
8560 pop_deferring_access_checks ();
8561 /* Do the explicit instantiation. */
8562 do_decl_instantiation (decl, extension_specifier);
8563 }
8564 else
8565 {
8566 pop_deferring_access_checks ();
8567 /* Skip the body of the explicit instantiation. */
8568 cp_parser_skip_to_end_of_statement (parser);
8569 }
8570 }
8571 /* We're done with the instantiation. */
8572 end_explicit_instantiation ();
8573
8574 cp_parser_consume_semicolon_at_end_of_statement (parser);
8575 }
8576
8577 /* Parse an explicit-specialization.
8578
8579 explicit-specialization:
8580 template < > declaration
8581
8582 Although the standard says `declaration', what it really means is:
8583
8584 explicit-specialization:
8585 template <> decl-specifier [opt] init-declarator [opt] ;
8586 template <> function-definition
8587 template <> explicit-specialization
8588 template <> template-declaration */
8589
8590 static void
cp_parser_explicit_specialization(cp_parser * parser)8591 cp_parser_explicit_specialization (cp_parser* parser)
8592 {
8593 /* Look for the `template' keyword. */
8594 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8595 /* Look for the `<'. */
8596 cp_parser_require (parser, CPP_LESS, "`<'");
8597 /* Look for the `>'. */
8598 cp_parser_require (parser, CPP_GREATER, "`>'");
8599 /* We have processed another parameter list. */
8600 ++parser->num_template_parameter_lists;
8601 /* Let the front end know that we are beginning a specialization. */
8602 begin_specialization ();
8603
8604 /* If the next keyword is `template', we need to figure out whether
8605 or not we're looking a template-declaration. */
8606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8607 {
8608 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8609 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8610 cp_parser_template_declaration_after_export (parser,
8611 /*member_p=*/false);
8612 else
8613 cp_parser_explicit_specialization (parser);
8614 }
8615 else
8616 /* Parse the dependent declaration. */
8617 cp_parser_single_declaration (parser,
8618 /*member_p=*/false,
8619 /*friend_p=*/NULL);
8620
8621 /* We're done with the specialization. */
8622 end_specialization ();
8623 /* We're done with this parameter list. */
8624 --parser->num_template_parameter_lists;
8625 }
8626
8627 /* Parse a type-specifier.
8628
8629 type-specifier:
8630 simple-type-specifier
8631 class-specifier
8632 enum-specifier
8633 elaborated-type-specifier
8634 cv-qualifier
8635
8636 GNU Extension:
8637
8638 type-specifier:
8639 __complex__
8640
8641 Returns a representation of the type-specifier. If the
8642 type-specifier is a keyword (like `int' or `const', or
8643 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8644 For a class-specifier, enum-specifier, or elaborated-type-specifier
8645 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8646
8647 If IS_FRIEND is TRUE then this type-specifier is being declared a
8648 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8649 appearing in a decl-specifier-seq.
8650
8651 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8652 class-specifier, enum-specifier, or elaborated-type-specifier, then
8653 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8654 if a type is declared; 2 if it is defined. Otherwise, it is set to
8655 zero.
8656
8657 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8658 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8659 is set to FALSE. */
8660
8661 static tree
cp_parser_type_specifier(cp_parser * parser,cp_parser_flags flags,bool is_friend,bool is_declaration,int * declares_class_or_enum,bool * is_cv_qualifier)8662 cp_parser_type_specifier (cp_parser* parser,
8663 cp_parser_flags flags,
8664 bool is_friend,
8665 bool is_declaration,
8666 int* declares_class_or_enum,
8667 bool* is_cv_qualifier)
8668 {
8669 tree type_spec = NULL_TREE;
8670 cp_token *token;
8671 enum rid keyword;
8672
8673 /* Assume this type-specifier does not declare a new type. */
8674 if (declares_class_or_enum)
8675 *declares_class_or_enum = 0;
8676 /* And that it does not specify a cv-qualifier. */
8677 if (is_cv_qualifier)
8678 *is_cv_qualifier = false;
8679 /* Peek at the next token. */
8680 token = cp_lexer_peek_token (parser->lexer);
8681
8682 /* If we're looking at a keyword, we can use that to guide the
8683 production we choose. */
8684 keyword = token->keyword;
8685 switch (keyword)
8686 {
8687 /* Any of these indicate either a class-specifier, or an
8688 elaborated-type-specifier. */
8689 case RID_CLASS:
8690 case RID_STRUCT:
8691 case RID_UNION:
8692 case RID_ENUM:
8693 /* Parse tentatively so that we can back up if we don't find a
8694 class-specifier or enum-specifier. */
8695 cp_parser_parse_tentatively (parser);
8696 /* Look for the class-specifier or enum-specifier. */
8697 if (keyword == RID_ENUM)
8698 type_spec = cp_parser_enum_specifier (parser);
8699 else
8700 type_spec = cp_parser_class_specifier (parser);
8701
8702 /* If that worked, we're done. */
8703 if (cp_parser_parse_definitely (parser))
8704 {
8705 if (declares_class_or_enum)
8706 *declares_class_or_enum = 2;
8707 return type_spec;
8708 }
8709
8710 /* Fall through. */
8711
8712 case RID_TYPENAME:
8713 /* Look for an elaborated-type-specifier. */
8714 type_spec = cp_parser_elaborated_type_specifier (parser,
8715 is_friend,
8716 is_declaration);
8717 /* We're declaring a class or enum -- unless we're using
8718 `typename'. */
8719 if (declares_class_or_enum && keyword != RID_TYPENAME)
8720 *declares_class_or_enum = 1;
8721 return type_spec;
8722
8723 case RID_CONST:
8724 case RID_VOLATILE:
8725 case RID_RESTRICT:
8726 type_spec = cp_parser_cv_qualifier_opt (parser);
8727 /* Even though we call a routine that looks for an optional
8728 qualifier, we know that there should be one. */
8729 my_friendly_assert (type_spec != NULL, 20000328);
8730 /* This type-specifier was a cv-qualified. */
8731 if (is_cv_qualifier)
8732 *is_cv_qualifier = true;
8733
8734 return type_spec;
8735
8736 case RID_COMPLEX:
8737 /* The `__complex__' keyword is a GNU extension. */
8738 return cp_lexer_consume_token (parser->lexer)->value;
8739
8740 default:
8741 break;
8742 }
8743
8744 /* If we do not already have a type-specifier, assume we are looking
8745 at a simple-type-specifier. */
8746 type_spec = cp_parser_simple_type_specifier (parser, flags,
8747 /*identifier_p=*/true);
8748
8749 /* If we didn't find a type-specifier, and a type-specifier was not
8750 optional in this context, issue an error message. */
8751 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8752 {
8753 cp_parser_error (parser, "expected type specifier");
8754 return error_mark_node;
8755 }
8756
8757 return type_spec;
8758 }
8759
8760 /* Parse a simple-type-specifier.
8761
8762 simple-type-specifier:
8763 :: [opt] nested-name-specifier [opt] type-name
8764 :: [opt] nested-name-specifier template template-id
8765 char
8766 wchar_t
8767 bool
8768 short
8769 int
8770 long
8771 signed
8772 unsigned
8773 float
8774 double
8775 void
8776
8777 GNU Extension:
8778
8779 simple-type-specifier:
8780 __typeof__ unary-expression
8781 __typeof__ ( type-id )
8782
8783 For the various keywords, the value returned is simply the
8784 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8785 For the first two productions, and if IDENTIFIER_P is false, the
8786 value returned is the indicated TYPE_DECL. */
8787
8788 static tree
cp_parser_simple_type_specifier(cp_parser * parser,cp_parser_flags flags,bool identifier_p)8789 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8790 bool identifier_p)
8791 {
8792 tree type = NULL_TREE;
8793 cp_token *token;
8794
8795 /* Peek at the next token. */
8796 token = cp_lexer_peek_token (parser->lexer);
8797
8798 /* If we're looking at a keyword, things are easy. */
8799 switch (token->keyword)
8800 {
8801 case RID_CHAR:
8802 type = char_type_node;
8803 break;
8804 case RID_WCHAR:
8805 type = wchar_type_node;
8806 break;
8807 case RID_BOOL:
8808 type = boolean_type_node;
8809 break;
8810 case RID_SHORT:
8811 type = short_integer_type_node;
8812 break;
8813 case RID_INT:
8814 type = integer_type_node;
8815 break;
8816 case RID_LONG:
8817 type = long_integer_type_node;
8818 break;
8819 case RID_SIGNED:
8820 type = integer_type_node;
8821 break;
8822 case RID_UNSIGNED:
8823 type = unsigned_type_node;
8824 break;
8825 case RID_FLOAT:
8826 type = float_type_node;
8827 break;
8828 case RID_DOUBLE:
8829 type = double_type_node;
8830 break;
8831 case RID_VOID:
8832 type = void_type_node;
8833 break;
8834
8835 case RID_TYPEOF:
8836 {
8837 tree operand;
8838
8839 /* Consume the `typeof' token. */
8840 cp_lexer_consume_token (parser->lexer);
8841 /* Parse the operand to `typeof'. */
8842 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8843 /* If it is not already a TYPE, take its type. */
8844 if (!TYPE_P (operand))
8845 operand = finish_typeof (operand);
8846
8847 return operand;
8848 }
8849
8850 default:
8851 break;
8852 }
8853
8854 /* If the type-specifier was for a built-in type, we're done. */
8855 if (type)
8856 {
8857 tree id;
8858
8859 /* Consume the token. */
8860 id = cp_lexer_consume_token (parser->lexer)->value;
8861
8862 /* There is no valid C++ program where a non-template type is
8863 followed by a "<". That usually indicates that the user thought
8864 that the type was a template. */
8865 cp_parser_check_for_invalid_template_id (parser, type);
8866
8867 return identifier_p ? id : TYPE_NAME (type);
8868 }
8869
8870 /* The type-specifier must be a user-defined type. */
8871 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8872 {
8873 bool qualified_p;
8874 bool global_p;
8875
8876 /* Don't gobble tokens or issue error messages if this is an
8877 optional type-specifier. */
8878 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8879 cp_parser_parse_tentatively (parser);
8880
8881 /* Look for the optional `::' operator. */
8882 global_p
8883 = (cp_parser_global_scope_opt (parser,
8884 /*current_scope_valid_p=*/false)
8885 != NULL_TREE);
8886 /* Look for the nested-name specifier. */
8887 qualified_p
8888 = (cp_parser_nested_name_specifier_opt (parser,
8889 /*typename_keyword_p=*/false,
8890 /*check_dependency_p=*/true,
8891 /*type_p=*/false,
8892 /*is_declaration=*/false)
8893 != NULL_TREE);
8894 /* If we have seen a nested-name-specifier, and the next token
8895 is `template', then we are using the template-id production. */
8896 if (parser->scope
8897 && cp_parser_optional_template_keyword (parser))
8898 {
8899 /* Look for the template-id. */
8900 type = cp_parser_template_id (parser,
8901 /*template_keyword_p=*/true,
8902 /*check_dependency_p=*/true,
8903 /*is_declaration=*/false);
8904 /* If the template-id did not name a type, we are out of
8905 luck. */
8906 if (TREE_CODE (type) != TYPE_DECL)
8907 {
8908 cp_parser_error (parser, "expected template-id for type");
8909 type = NULL_TREE;
8910 }
8911 }
8912 /* Otherwise, look for a type-name. */
8913 else
8914 type = cp_parser_type_name (parser);
8915 /* Keep track of all name-lookups performed in class scopes. */
8916 if (type
8917 && !global_p
8918 && !qualified_p
8919 && TREE_CODE (type) == TYPE_DECL
8920 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8921 maybe_note_name_used_in_class (DECL_NAME (type), type);
8922 /* If it didn't work out, we don't have a TYPE. */
8923 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8924 && !cp_parser_parse_definitely (parser))
8925 type = NULL_TREE;
8926 }
8927
8928 /* If we didn't get a type-name, issue an error message. */
8929 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8930 {
8931 cp_parser_error (parser, "expected type-name");
8932 return error_mark_node;
8933 }
8934
8935 /* There is no valid C++ program where a non-template type is
8936 followed by a "<". That usually indicates that the user thought
8937 that the type was a template. */
8938 if (type && type != error_mark_node)
8939 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8940
8941 return type;
8942 }
8943
8944 /* Parse a type-name.
8945
8946 type-name:
8947 class-name
8948 enum-name
8949 typedef-name
8950
8951 enum-name:
8952 identifier
8953
8954 typedef-name:
8955 identifier
8956
8957 Returns a TYPE_DECL for the the type. */
8958
8959 static tree
cp_parser_type_name(cp_parser * parser)8960 cp_parser_type_name (cp_parser* parser)
8961 {
8962 tree type_decl;
8963 tree identifier;
8964
8965 /* We can't know yet whether it is a class-name or not. */
8966 cp_parser_parse_tentatively (parser);
8967 /* Try a class-name. */
8968 type_decl = cp_parser_class_name (parser,
8969 /*typename_keyword_p=*/false,
8970 /*template_keyword_p=*/false,
8971 /*type_p=*/false,
8972 /*check_dependency_p=*/true,
8973 /*class_head_p=*/false,
8974 /*is_declaration=*/false);
8975 /* If it's not a class-name, keep looking. */
8976 if (!cp_parser_parse_definitely (parser))
8977 {
8978 /* It must be a typedef-name or an enum-name. */
8979 identifier = cp_parser_identifier (parser);
8980 if (identifier == error_mark_node)
8981 return error_mark_node;
8982
8983 /* Look up the type-name. */
8984 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8985 /* Issue an error if we did not find a type-name. */
8986 if (TREE_CODE (type_decl) != TYPE_DECL)
8987 {
8988 if (!cp_parser_simulate_error (parser))
8989 cp_parser_name_lookup_error (parser, identifier, type_decl,
8990 "is not a type");
8991 type_decl = error_mark_node;
8992 }
8993 /* Remember that the name was used in the definition of the
8994 current class so that we can check later to see if the
8995 meaning would have been different after the class was
8996 entirely defined. */
8997 else if (type_decl != error_mark_node
8998 && !parser->scope)
8999 maybe_note_name_used_in_class (identifier, type_decl);
9000 }
9001
9002 return type_decl;
9003 }
9004
9005
9006 /* Parse an elaborated-type-specifier. Note that the grammar given
9007 here incorporates the resolution to DR68.
9008
9009 elaborated-type-specifier:
9010 class-key :: [opt] nested-name-specifier [opt] identifier
9011 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9012 enum :: [opt] nested-name-specifier [opt] identifier
9013 typename :: [opt] nested-name-specifier identifier
9014 typename :: [opt] nested-name-specifier template [opt]
9015 template-id
9016
9017 GNU extension:
9018
9019 elaborated-type-specifier:
9020 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9021 class-key attributes :: [opt] nested-name-specifier [opt]
9022 template [opt] template-id
9023 enum attributes :: [opt] nested-name-specifier [opt] identifier
9024
9025 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9026 declared `friend'. If IS_DECLARATION is TRUE, then this
9027 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9028 something is being declared.
9029
9030 Returns the TYPE specified. */
9031
9032 static tree
cp_parser_elaborated_type_specifier(cp_parser * parser,bool is_friend,bool is_declaration)9033 cp_parser_elaborated_type_specifier (cp_parser* parser,
9034 bool is_friend,
9035 bool is_declaration)
9036 {
9037 enum tag_types tag_type;
9038 tree identifier;
9039 tree type = NULL_TREE;
9040 tree attributes = NULL_TREE;
9041
9042 /* See if we're looking at the `enum' keyword. */
9043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9044 {
9045 /* Consume the `enum' token. */
9046 cp_lexer_consume_token (parser->lexer);
9047 /* Remember that it's an enumeration type. */
9048 tag_type = enum_type;
9049 /* Parse the attributes. */
9050 attributes = cp_parser_attributes_opt (parser);
9051 }
9052 /* Or, it might be `typename'. */
9053 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9054 RID_TYPENAME))
9055 {
9056 /* Consume the `typename' token. */
9057 cp_lexer_consume_token (parser->lexer);
9058 /* Remember that it's a `typename' type. */
9059 tag_type = typename_type;
9060 /* The `typename' keyword is only allowed in templates. */
9061 if (!processing_template_decl)
9062 pedwarn ("using `typename' outside of template");
9063 }
9064 /* Otherwise it must be a class-key. */
9065 else
9066 {
9067 tag_type = cp_parser_class_key (parser);
9068 if (tag_type == none_type)
9069 return error_mark_node;
9070 /* Parse the attributes. */
9071 attributes = cp_parser_attributes_opt (parser);
9072 }
9073
9074 /* Look for the `::' operator. */
9075 cp_parser_global_scope_opt (parser,
9076 /*current_scope_valid_p=*/false);
9077 /* Look for the nested-name-specifier. */
9078 if (tag_type == typename_type)
9079 {
9080 if (cp_parser_nested_name_specifier (parser,
9081 /*typename_keyword_p=*/true,
9082 /*check_dependency_p=*/true,
9083 /*type_p=*/true,
9084 is_declaration)
9085 == error_mark_node)
9086 return error_mark_node;
9087 }
9088 else
9089 /* Even though `typename' is not present, the proposed resolution
9090 to Core Issue 180 says that in `class A<T>::B', `B' should be
9091 considered a type-name, even if `A<T>' is dependent. */
9092 cp_parser_nested_name_specifier_opt (parser,
9093 /*typename_keyword_p=*/true,
9094 /*check_dependency_p=*/true,
9095 /*type_p=*/true,
9096 is_declaration);
9097 /* For everything but enumeration types, consider a template-id. */
9098 if (tag_type != enum_type)
9099 {
9100 bool template_p = false;
9101 tree decl;
9102
9103 /* Allow the `template' keyword. */
9104 template_p = cp_parser_optional_template_keyword (parser);
9105 /* If we didn't see `template', we don't know if there's a
9106 template-id or not. */
9107 if (!template_p)
9108 cp_parser_parse_tentatively (parser);
9109 /* Parse the template-id. */
9110 decl = cp_parser_template_id (parser, template_p,
9111 /*check_dependency_p=*/true,
9112 is_declaration);
9113 /* If we didn't find a template-id, look for an ordinary
9114 identifier. */
9115 if (!template_p && !cp_parser_parse_definitely (parser))
9116 ;
9117 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9118 in effect, then we must assume that, upon instantiation, the
9119 template will correspond to a class. */
9120 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9121 && tag_type == typename_type)
9122 type = make_typename_type (parser->scope, decl,
9123 /*complain=*/1);
9124 else
9125 type = TREE_TYPE (decl);
9126 }
9127
9128 /* For an enumeration type, consider only a plain identifier. */
9129 if (!type)
9130 {
9131 identifier = cp_parser_identifier (parser);
9132
9133 if (identifier == error_mark_node)
9134 {
9135 parser->scope = NULL_TREE;
9136 return error_mark_node;
9137 }
9138
9139 /* For a `typename', we needn't call xref_tag. */
9140 if (tag_type == typename_type)
9141 return make_typename_type (parser->scope, identifier,
9142 /*complain=*/1);
9143 /* Look up a qualified name in the usual way. */
9144 if (parser->scope)
9145 {
9146 tree decl;
9147
9148 /* In an elaborated-type-specifier, names are assumed to name
9149 types, so we set IS_TYPE to TRUE when calling
9150 cp_parser_lookup_name. */
9151 decl = cp_parser_lookup_name (parser, identifier,
9152 /*is_type=*/true,
9153 /*is_template=*/false,
9154 /*is_namespace=*/false,
9155 /*check_dependency=*/true);
9156
9157 /* If we are parsing friend declaration, DECL may be a
9158 TEMPLATE_DECL tree node here. However, we need to check
9159 whether this TEMPLATE_DECL results in valid code. Consider
9160 the following example:
9161
9162 namespace N {
9163 template <class T> class C {};
9164 }
9165 class X {
9166 template <class T> friend class N::C; // #1, valid code
9167 };
9168 template <class T> class Y {
9169 friend class N::C; // #2, invalid code
9170 };
9171
9172 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9173 name lookup of `N::C'. We see that friend declaration must
9174 be template for the code to be valid. Note that
9175 processing_template_decl does not work here since it is
9176 always 1 for the above two cases. */
9177
9178 decl = (cp_parser_maybe_treat_template_as_class
9179 (decl, /*tag_name_p=*/is_friend
9180 && parser->num_template_parameter_lists));
9181
9182 if (TREE_CODE (decl) != TYPE_DECL)
9183 {
9184 error ("expected type-name");
9185 return error_mark_node;
9186 }
9187
9188 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9189 check_elaborated_type_specifier
9190 (tag_type, decl,
9191 (parser->num_template_parameter_lists
9192 || DECL_SELF_REFERENCE_P (decl)));
9193
9194 type = TREE_TYPE (decl);
9195 }
9196 else
9197 {
9198 /* An elaborated-type-specifier sometimes introduces a new type and
9199 sometimes names an existing type. Normally, the rule is that it
9200 introduces a new type only if there is not an existing type of
9201 the same name already in scope. For example, given:
9202
9203 struct S {};
9204 void f() { struct S s; }
9205
9206 the `struct S' in the body of `f' is the same `struct S' as in
9207 the global scope; the existing definition is used. However, if
9208 there were no global declaration, this would introduce a new
9209 local class named `S'.
9210
9211 An exception to this rule applies to the following code:
9212
9213 namespace N { struct S; }
9214
9215 Here, the elaborated-type-specifier names a new type
9216 unconditionally; even if there is already an `S' in the
9217 containing scope this declaration names a new type.
9218 This exception only applies if the elaborated-type-specifier
9219 forms the complete declaration:
9220
9221 [class.name]
9222
9223 A declaration consisting solely of `class-key identifier ;' is
9224 either a redeclaration of the name in the current scope or a
9225 forward declaration of the identifier as a class name. It
9226 introduces the name into the current scope.
9227
9228 We are in this situation precisely when the next token is a `;'.
9229
9230 An exception to the exception is that a `friend' declaration does
9231 *not* name a new type; i.e., given:
9232
9233 struct S { friend struct T; };
9234
9235 `T' is not a new type in the scope of `S'.
9236
9237 Also, `new struct S' or `sizeof (struct S)' never results in the
9238 definition of a new type; a new type can only be declared in a
9239 declaration context. */
9240
9241 /* Warn about attributes. They are ignored. */
9242 if (attributes)
9243 warning ("type attributes are honored only at type definition");
9244
9245 type = xref_tag (tag_type, identifier,
9246 (is_friend
9247 || !is_declaration
9248 || cp_lexer_next_token_is_not (parser->lexer,
9249 CPP_SEMICOLON)),
9250 parser->num_template_parameter_lists);
9251 }
9252 }
9253 if (tag_type != enum_type)
9254 cp_parser_check_class_key (tag_type, type);
9255
9256 /* A "<" cannot follow an elaborated type specifier. If that
9257 happens, the user was probably trying to form a template-id. */
9258 cp_parser_check_for_invalid_template_id (parser, type);
9259
9260 return type;
9261 }
9262
9263 /* Parse an enum-specifier.
9264
9265 enum-specifier:
9266 enum identifier [opt] { enumerator-list [opt] }
9267
9268 Returns an ENUM_TYPE representing the enumeration. */
9269
9270 static tree
cp_parser_enum_specifier(cp_parser * parser)9271 cp_parser_enum_specifier (cp_parser* parser)
9272 {
9273 cp_token *token;
9274 tree identifier = NULL_TREE;
9275 tree type;
9276
9277 /* Look for the `enum' keyword. */
9278 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9279 return error_mark_node;
9280 /* Peek at the next token. */
9281 token = cp_lexer_peek_token (parser->lexer);
9282
9283 /* See if it is an identifier. */
9284 if (token->type == CPP_NAME)
9285 identifier = cp_parser_identifier (parser);
9286
9287 /* Look for the `{'. */
9288 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9289 return error_mark_node;
9290
9291 /* At this point, we're going ahead with the enum-specifier, even
9292 if some other problem occurs. */
9293 cp_parser_commit_to_tentative_parse (parser);
9294
9295 /* Issue an error message if type-definitions are forbidden here. */
9296 cp_parser_check_type_definition (parser);
9297
9298 /* Create the new type. */
9299 type = start_enum (identifier ? identifier : make_anon_name ());
9300
9301 /* Peek at the next token. */
9302 token = cp_lexer_peek_token (parser->lexer);
9303 /* If it's not a `}', then there are some enumerators. */
9304 if (token->type != CPP_CLOSE_BRACE)
9305 cp_parser_enumerator_list (parser, type);
9306 /* Look for the `}'. */
9307 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9308
9309 /* Finish up the enumeration. */
9310 finish_enum (type);
9311
9312 return type;
9313 }
9314
9315 /* Parse an enumerator-list. The enumerators all have the indicated
9316 TYPE.
9317
9318 enumerator-list:
9319 enumerator-definition
9320 enumerator-list , enumerator-definition */
9321
9322 static void
cp_parser_enumerator_list(cp_parser * parser,tree type)9323 cp_parser_enumerator_list (cp_parser* parser, tree type)
9324 {
9325 while (true)
9326 {
9327 cp_token *token;
9328
9329 /* Parse an enumerator-definition. */
9330 cp_parser_enumerator_definition (parser, type);
9331 /* Peek at the next token. */
9332 token = cp_lexer_peek_token (parser->lexer);
9333 /* If it's not a `,', then we've reached the end of the
9334 list. */
9335 if (token->type != CPP_COMMA)
9336 break;
9337 /* Otherwise, consume the `,' and keep going. */
9338 cp_lexer_consume_token (parser->lexer);
9339 /* If the next token is a `}', there is a trailing comma. */
9340 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9341 {
9342 if (pedantic && !in_system_header)
9343 pedwarn ("comma at end of enumerator list");
9344 break;
9345 }
9346 }
9347 }
9348
9349 /* Parse an enumerator-definition. The enumerator has the indicated
9350 TYPE.
9351
9352 enumerator-definition:
9353 enumerator
9354 enumerator = constant-expression
9355
9356 enumerator:
9357 identifier */
9358
9359 static void
cp_parser_enumerator_definition(cp_parser * parser,tree type)9360 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9361 {
9362 cp_token *token;
9363 tree identifier;
9364 tree value;
9365
9366 /* Look for the identifier. */
9367 identifier = cp_parser_identifier (parser);
9368 if (identifier == error_mark_node)
9369 return;
9370
9371 /* Peek at the next token. */
9372 token = cp_lexer_peek_token (parser->lexer);
9373 /* If it's an `=', then there's an explicit value. */
9374 if (token->type == CPP_EQ)
9375 {
9376 /* Consume the `=' token. */
9377 cp_lexer_consume_token (parser->lexer);
9378 /* Parse the value. */
9379 value = cp_parser_constant_expression (parser,
9380 /*allow_non_constant_p=*/false,
9381 NULL);
9382 }
9383 else
9384 value = NULL_TREE;
9385
9386 /* Create the enumerator. */
9387 build_enumerator (identifier, value, type);
9388 }
9389
9390 /* Parse a namespace-name.
9391
9392 namespace-name:
9393 original-namespace-name
9394 namespace-alias
9395
9396 Returns the NAMESPACE_DECL for the namespace. */
9397
9398 static tree
cp_parser_namespace_name(cp_parser * parser)9399 cp_parser_namespace_name (cp_parser* parser)
9400 {
9401 tree identifier;
9402 tree namespace_decl;
9403
9404 /* Get the name of the namespace. */
9405 identifier = cp_parser_identifier (parser);
9406 if (identifier == error_mark_node)
9407 return error_mark_node;
9408
9409 /* Look up the identifier in the currently active scope. Look only
9410 for namespaces, due to:
9411
9412 [basic.lookup.udir]
9413
9414 When looking up a namespace-name in a using-directive or alias
9415 definition, only namespace names are considered.
9416
9417 And:
9418
9419 [basic.lookup.qual]
9420
9421 During the lookup of a name preceding the :: scope resolution
9422 operator, object, function, and enumerator names are ignored.
9423
9424 (Note that cp_parser_class_or_namespace_name only calls this
9425 function if the token after the name is the scope resolution
9426 operator.) */
9427 namespace_decl = cp_parser_lookup_name (parser, identifier,
9428 /*is_type=*/false,
9429 /*is_template=*/false,
9430 /*is_namespace=*/true,
9431 /*check_dependency=*/true);
9432 /* If it's not a namespace, issue an error. */
9433 if (namespace_decl == error_mark_node
9434 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9435 {
9436 cp_parser_error (parser, "expected namespace-name");
9437 namespace_decl = error_mark_node;
9438 }
9439
9440 return namespace_decl;
9441 }
9442
9443 /* Parse a namespace-definition.
9444
9445 namespace-definition:
9446 named-namespace-definition
9447 unnamed-namespace-definition
9448
9449 named-namespace-definition:
9450 original-namespace-definition
9451 extension-namespace-definition
9452
9453 original-namespace-definition:
9454 namespace identifier { namespace-body }
9455
9456 extension-namespace-definition:
9457 namespace original-namespace-name { namespace-body }
9458
9459 unnamed-namespace-definition:
9460 namespace { namespace-body } */
9461
9462 static void
cp_parser_namespace_definition(cp_parser * parser)9463 cp_parser_namespace_definition (cp_parser* parser)
9464 {
9465 tree identifier;
9466
9467 /* Look for the `namespace' keyword. */
9468 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9469
9470 /* Get the name of the namespace. We do not attempt to distinguish
9471 between an original-namespace-definition and an
9472 extension-namespace-definition at this point. The semantic
9473 analysis routines are responsible for that. */
9474 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9475 identifier = cp_parser_identifier (parser);
9476 else
9477 identifier = NULL_TREE;
9478
9479 /* Look for the `{' to start the namespace. */
9480 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9481 /* Start the namespace. */
9482 push_namespace (identifier);
9483 /* Parse the body of the namespace. */
9484 cp_parser_namespace_body (parser);
9485 /* Finish the namespace. */
9486 pop_namespace ();
9487 /* Look for the final `}'. */
9488 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9489 }
9490
9491 /* Parse a namespace-body.
9492
9493 namespace-body:
9494 declaration-seq [opt] */
9495
9496 static void
cp_parser_namespace_body(cp_parser * parser)9497 cp_parser_namespace_body (cp_parser* parser)
9498 {
9499 cp_parser_declaration_seq_opt (parser);
9500 }
9501
9502 /* Parse a namespace-alias-definition.
9503
9504 namespace-alias-definition:
9505 namespace identifier = qualified-namespace-specifier ; */
9506
9507 static void
cp_parser_namespace_alias_definition(cp_parser * parser)9508 cp_parser_namespace_alias_definition (cp_parser* parser)
9509 {
9510 tree identifier;
9511 tree namespace_specifier;
9512
9513 /* Look for the `namespace' keyword. */
9514 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9515 /* Look for the identifier. */
9516 identifier = cp_parser_identifier (parser);
9517 if (identifier == error_mark_node)
9518 return;
9519 /* Look for the `=' token. */
9520 cp_parser_require (parser, CPP_EQ, "`='");
9521 /* Look for the qualified-namespace-specifier. */
9522 namespace_specifier
9523 = cp_parser_qualified_namespace_specifier (parser);
9524 /* Look for the `;' token. */
9525 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9526
9527 /* Register the alias in the symbol table. */
9528 do_namespace_alias (identifier, namespace_specifier);
9529 }
9530
9531 /* Parse a qualified-namespace-specifier.
9532
9533 qualified-namespace-specifier:
9534 :: [opt] nested-name-specifier [opt] namespace-name
9535
9536 Returns a NAMESPACE_DECL corresponding to the specified
9537 namespace. */
9538
9539 static tree
cp_parser_qualified_namespace_specifier(cp_parser * parser)9540 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9541 {
9542 /* Look for the optional `::'. */
9543 cp_parser_global_scope_opt (parser,
9544 /*current_scope_valid_p=*/false);
9545
9546 /* Look for the optional nested-name-specifier. */
9547 cp_parser_nested_name_specifier_opt (parser,
9548 /*typename_keyword_p=*/false,
9549 /*check_dependency_p=*/true,
9550 /*type_p=*/false,
9551 /*is_declaration=*/true);
9552
9553 return cp_parser_namespace_name (parser);
9554 }
9555
9556 /* Parse a using-declaration.
9557
9558 using-declaration:
9559 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9560 using :: unqualified-id ; */
9561
9562 static void
cp_parser_using_declaration(cp_parser * parser)9563 cp_parser_using_declaration (cp_parser* parser)
9564 {
9565 cp_token *token;
9566 bool typename_p = false;
9567 bool global_scope_p;
9568 tree decl;
9569 tree identifier;
9570 tree scope;
9571 tree qscope;
9572
9573 /* Look for the `using' keyword. */
9574 cp_parser_require_keyword (parser, RID_USING, "`using'");
9575
9576 /* Peek at the next token. */
9577 token = cp_lexer_peek_token (parser->lexer);
9578 /* See if it's `typename'. */
9579 if (token->keyword == RID_TYPENAME)
9580 {
9581 /* Remember that we've seen it. */
9582 typename_p = true;
9583 /* Consume the `typename' token. */
9584 cp_lexer_consume_token (parser->lexer);
9585 }
9586
9587 /* Look for the optional global scope qualification. */
9588 global_scope_p
9589 = (cp_parser_global_scope_opt (parser,
9590 /*current_scope_valid_p=*/false)
9591 != NULL_TREE);
9592
9593 /* If we saw `typename', or didn't see `::', then there must be a
9594 nested-name-specifier present. */
9595 if (typename_p || !global_scope_p)
9596 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9597 /*check_dependency_p=*/true,
9598 /*type_p=*/false,
9599 /*is_declaration=*/true);
9600 /* Otherwise, we could be in either of the two productions. In that
9601 case, treat the nested-name-specifier as optional. */
9602 else
9603 qscope = cp_parser_nested_name_specifier_opt (parser,
9604 /*typename_keyword_p=*/false,
9605 /*check_dependency_p=*/true,
9606 /*type_p=*/false,
9607 /*is_declaration=*/true);
9608 if (!qscope)
9609 qscope = global_namespace;
9610
9611 /* Parse the unqualified-id. */
9612 identifier = cp_parser_unqualified_id (parser,
9613 /*template_keyword_p=*/false,
9614 /*check_dependency_p=*/true,
9615 /*declarator_p=*/true);
9616
9617 /* The function we call to handle a using-declaration is different
9618 depending on what scope we are in. */
9619 if (identifier == error_mark_node)
9620 ;
9621 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9622 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9623 /* [namespace.udecl]
9624
9625 A using declaration shall not name a template-id. */
9626 error ("a template-id may not appear in a using-declaration");
9627 else
9628 {
9629 scope = current_scope ();
9630 if (scope && TYPE_P (scope))
9631 {
9632 /* Create the USING_DECL. */
9633 decl = do_class_using_decl (build_nt (SCOPE_REF,
9634 parser->scope,
9635 identifier));
9636 /* Add it to the list of members in this class. */
9637 finish_member_declaration (decl);
9638 }
9639 else
9640 {
9641 decl = cp_parser_lookup_name_simple (parser, identifier);
9642 if (decl == error_mark_node)
9643 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9644 else if (scope)
9645 do_local_using_decl (decl, qscope, identifier);
9646 else
9647 do_toplevel_using_decl (decl, qscope, identifier);
9648 }
9649 }
9650
9651 /* Look for the final `;'. */
9652 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9653 }
9654
9655 /* Parse a using-directive.
9656
9657 using-directive:
9658 using namespace :: [opt] nested-name-specifier [opt]
9659 namespace-name ; */
9660
9661 static void
cp_parser_using_directive(cp_parser * parser)9662 cp_parser_using_directive (cp_parser* parser)
9663 {
9664 tree namespace_decl;
9665 tree attribs;
9666
9667 /* Look for the `using' keyword. */
9668 cp_parser_require_keyword (parser, RID_USING, "`using'");
9669 /* And the `namespace' keyword. */
9670 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9671 /* Look for the optional `::' operator. */
9672 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9673 /* And the optional nested-name-specifier. */
9674 cp_parser_nested_name_specifier_opt (parser,
9675 /*typename_keyword_p=*/false,
9676 /*check_dependency_p=*/true,
9677 /*type_p=*/false,
9678 /*is_declaration=*/true);
9679 /* Get the namespace being used. */
9680 namespace_decl = cp_parser_namespace_name (parser);
9681 /* And any specified attributes. */
9682 attribs = cp_parser_attributes_opt (parser);
9683 /* Update the symbol table. */
9684 parse_using_directive (namespace_decl, attribs);
9685 /* Look for the final `;'. */
9686 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9687 }
9688
9689 /* Parse an asm-definition.
9690
9691 asm-definition:
9692 asm ( string-literal ) ;
9693
9694 GNU Extension:
9695
9696 asm-definition:
9697 asm volatile [opt] ( string-literal ) ;
9698 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9699 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9700 : asm-operand-list [opt] ) ;
9701 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9702 : asm-operand-list [opt]
9703 : asm-operand-list [opt] ) ; */
9704
9705 static void
cp_parser_asm_definition(cp_parser * parser)9706 cp_parser_asm_definition (cp_parser* parser)
9707 {
9708 cp_token *token;
9709 tree string;
9710 tree outputs = NULL_TREE;
9711 tree inputs = NULL_TREE;
9712 tree clobbers = NULL_TREE;
9713 tree asm_stmt;
9714 bool volatile_p = false;
9715 bool extended_p = false;
9716
9717 /* Look for the `asm' keyword. */
9718 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9719 /* See if the next token is `volatile'. */
9720 if (cp_parser_allow_gnu_extensions_p (parser)
9721 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9722 {
9723 /* Remember that we saw the `volatile' keyword. */
9724 volatile_p = true;
9725 /* Consume the token. */
9726 cp_lexer_consume_token (parser->lexer);
9727 }
9728 /* Look for the opening `('. */
9729 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9730 /* Look for the string. */
9731 token = cp_parser_require (parser, CPP_STRING, "asm body");
9732 if (!token)
9733 return;
9734 string = token->value;
9735 /* If we're allowing GNU extensions, check for the extended assembly
9736 syntax. Unfortunately, the `:' tokens need not be separated by
9737 a space in C, and so, for compatibility, we tolerate that here
9738 too. Doing that means that we have to treat the `::' operator as
9739 two `:' tokens. */
9740 if (cp_parser_allow_gnu_extensions_p (parser)
9741 && at_function_scope_p ()
9742 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9743 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9744 {
9745 bool inputs_p = false;
9746 bool clobbers_p = false;
9747
9748 /* The extended syntax was used. */
9749 extended_p = true;
9750
9751 /* Look for outputs. */
9752 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9753 {
9754 /* Consume the `:'. */
9755 cp_lexer_consume_token (parser->lexer);
9756 /* Parse the output-operands. */
9757 if (cp_lexer_next_token_is_not (parser->lexer,
9758 CPP_COLON)
9759 && cp_lexer_next_token_is_not (parser->lexer,
9760 CPP_SCOPE)
9761 && cp_lexer_next_token_is_not (parser->lexer,
9762 CPP_CLOSE_PAREN))
9763 outputs = cp_parser_asm_operand_list (parser);
9764 }
9765 /* If the next token is `::', there are no outputs, and the
9766 next token is the beginning of the inputs. */
9767 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9768 /* The inputs are coming next. */
9769 inputs_p = true;
9770
9771 /* Look for inputs. */
9772 if (inputs_p
9773 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9774 {
9775 /* Consume the `:' or `::'. */
9776 cp_lexer_consume_token (parser->lexer);
9777 /* Parse the output-operands. */
9778 if (cp_lexer_next_token_is_not (parser->lexer,
9779 CPP_COLON)
9780 && cp_lexer_next_token_is_not (parser->lexer,
9781 CPP_CLOSE_PAREN))
9782 inputs = cp_parser_asm_operand_list (parser);
9783 }
9784 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9785 /* The clobbers are coming next. */
9786 clobbers_p = true;
9787
9788 /* Look for clobbers. */
9789 if (clobbers_p
9790 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9791 {
9792 /* Consume the `:' or `::'. */
9793 cp_lexer_consume_token (parser->lexer);
9794 /* Parse the clobbers. */
9795 if (cp_lexer_next_token_is_not (parser->lexer,
9796 CPP_CLOSE_PAREN))
9797 clobbers = cp_parser_asm_clobber_list (parser);
9798 }
9799 }
9800 /* Look for the closing `)'. */
9801 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9802 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9803 /*consume_paren=*/true);
9804 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9805
9806 /* Create the ASM_STMT. */
9807 if (at_function_scope_p ())
9808 {
9809 asm_stmt =
9810 finish_asm_stmt (volatile_p
9811 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9812 string, outputs, inputs, clobbers);
9813 /* If the extended syntax was not used, mark the ASM_STMT. */
9814 if (!extended_p)
9815 ASM_INPUT_P (asm_stmt) = 1;
9816 }
9817 else
9818 assemble_asm (string);
9819 }
9820
9821 /* Declarators [gram.dcl.decl] */
9822
9823 /* Parse an init-declarator.
9824
9825 init-declarator:
9826 declarator initializer [opt]
9827
9828 GNU Extension:
9829
9830 init-declarator:
9831 declarator asm-specification [opt] attributes [opt] initializer [opt]
9832
9833 function-definition:
9834 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9835 function-body
9836 decl-specifier-seq [opt] declarator function-try-block
9837
9838 GNU Extension:
9839
9840 function-definition:
9841 __extension__ function-definition
9842
9843 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9844 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9845 then this declarator appears in a class scope. The new DECL created
9846 by this declarator is returned.
9847
9848 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9849 for a function-definition here as well. If the declarator is a
9850 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9851 be TRUE upon return. By that point, the function-definition will
9852 have been completely parsed.
9853
9854 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9855 is FALSE. */
9856
9857 static tree
cp_parser_init_declarator(cp_parser * parser,tree decl_specifiers,tree prefix_attributes,bool function_definition_allowed_p,bool member_p,int declares_class_or_enum,bool * function_definition_p)9858 cp_parser_init_declarator (cp_parser* parser,
9859 tree decl_specifiers,
9860 tree prefix_attributes,
9861 bool function_definition_allowed_p,
9862 bool member_p,
9863 int declares_class_or_enum,
9864 bool* function_definition_p)
9865 {
9866 cp_token *token;
9867 tree declarator;
9868 tree attributes;
9869 tree asm_specification;
9870 tree initializer;
9871 tree decl = NULL_TREE;
9872 tree scope;
9873 bool is_initialized;
9874 bool is_parenthesized_init;
9875 bool is_non_constant_init;
9876 int ctor_dtor_or_conv_p;
9877 bool friend_p;
9878 bool pop_p = false;
9879
9880 /* Assume that this is not the declarator for a function
9881 definition. */
9882 if (function_definition_p)
9883 *function_definition_p = false;
9884
9885 /* Defer access checks while parsing the declarator; we cannot know
9886 what names are accessible until we know what is being
9887 declared. */
9888 resume_deferring_access_checks ();
9889
9890 /* Parse the declarator. */
9891 declarator
9892 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9893 &ctor_dtor_or_conv_p,
9894 /*parenthesized_p=*/NULL);
9895 /* Gather up the deferred checks. */
9896 stop_deferring_access_checks ();
9897
9898 /* If the DECLARATOR was erroneous, there's no need to go
9899 further. */
9900 if (declarator == error_mark_node)
9901 return error_mark_node;
9902
9903 cp_parser_check_for_definition_in_return_type (declarator,
9904 declares_class_or_enum);
9905
9906 /* Figure out what scope the entity declared by the DECLARATOR is
9907 located in. `grokdeclarator' sometimes changes the scope, so
9908 we compute it now. */
9909 scope = get_scope_of_declarator (declarator);
9910
9911 /* If we're allowing GNU extensions, look for an asm-specification
9912 and attributes. */
9913 if (cp_parser_allow_gnu_extensions_p (parser))
9914 {
9915 /* Look for an asm-specification. */
9916 asm_specification = cp_parser_asm_specification_opt (parser);
9917 /* And attributes. */
9918 attributes = cp_parser_attributes_opt (parser);
9919 }
9920 else
9921 {
9922 asm_specification = NULL_TREE;
9923 attributes = NULL_TREE;
9924 }
9925
9926 /* Peek at the next token. */
9927 token = cp_lexer_peek_token (parser->lexer);
9928 /* Check to see if the token indicates the start of a
9929 function-definition. */
9930 if (cp_parser_token_starts_function_definition_p (token))
9931 {
9932 if (!function_definition_allowed_p)
9933 {
9934 /* If a function-definition should not appear here, issue an
9935 error message. */
9936 cp_parser_error (parser,
9937 "a function-definition is not allowed here");
9938 return error_mark_node;
9939 }
9940 else
9941 {
9942 /* Neither attributes nor an asm-specification are allowed
9943 on a function-definition. */
9944 if (asm_specification)
9945 error ("an asm-specification is not allowed on a function-definition");
9946 if (attributes)
9947 error ("attributes are not allowed on a function-definition");
9948 /* This is a function-definition. */
9949 *function_definition_p = true;
9950
9951 /* Parse the function definition. */
9952 if (member_p)
9953 decl = cp_parser_save_member_function_body (parser,
9954 decl_specifiers,
9955 declarator,
9956 prefix_attributes);
9957 else
9958 decl
9959 = (cp_parser_function_definition_from_specifiers_and_declarator
9960 (parser, decl_specifiers, prefix_attributes, declarator));
9961
9962 return decl;
9963 }
9964 }
9965
9966 /* [dcl.dcl]
9967
9968 Only in function declarations for constructors, destructors, and
9969 type conversions can the decl-specifier-seq be omitted.
9970
9971 We explicitly postpone this check past the point where we handle
9972 function-definitions because we tolerate function-definitions
9973 that are missing their return types in some modes. */
9974 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9975 {
9976 cp_parser_error (parser,
9977 "expected constructor, destructor, or type conversion");
9978 return error_mark_node;
9979 }
9980
9981 /* An `=' or an `(' indicates an initializer. */
9982 is_initialized = (token->type == CPP_EQ
9983 || token->type == CPP_OPEN_PAREN);
9984 /* If the init-declarator isn't initialized and isn't followed by a
9985 `,' or `;', it's not a valid init-declarator. */
9986 if (!is_initialized
9987 && token->type != CPP_COMMA
9988 && token->type != CPP_SEMICOLON)
9989 {
9990 cp_parser_error (parser, "expected init-declarator");
9991 return error_mark_node;
9992 }
9993
9994 /* Because start_decl has side-effects, we should only call it if we
9995 know we're going ahead. By this point, we know that we cannot
9996 possibly be looking at any other construct. */
9997 cp_parser_commit_to_tentative_parse (parser);
9998
9999 /* If the decl specifiers were bad, issue an error now that we're
10000 sure this was intended to be a declarator. Then continue
10001 declaring the variable(s), as int, to try to cut down on further
10002 errors. */
10003 if (decl_specifiers != NULL
10004 && TREE_VALUE (decl_specifiers) == error_mark_node)
10005 {
10006 cp_parser_error (parser, "invalid type in declaration");
10007 TREE_VALUE (decl_specifiers) = integer_type_node;
10008 }
10009
10010 /* Check to see whether or not this declaration is a friend. */
10011 friend_p = cp_parser_friend_p (decl_specifiers);
10012
10013 /* Check that the number of template-parameter-lists is OK. */
10014 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10015 return error_mark_node;
10016
10017 /* Enter the newly declared entry in the symbol table. If we're
10018 processing a declaration in a class-specifier, we wait until
10019 after processing the initializer. */
10020 if (!member_p)
10021 {
10022 if (parser->in_unbraced_linkage_specification_p)
10023 {
10024 decl_specifiers = tree_cons (error_mark_node,
10025 get_identifier ("extern"),
10026 decl_specifiers);
10027 have_extern_spec = false;
10028 }
10029 decl = start_decl (declarator, decl_specifiers,
10030 is_initialized, attributes, prefix_attributes);
10031 }
10032
10033 /* Enter the SCOPE. That way unqualified names appearing in the
10034 initializer will be looked up in SCOPE. */
10035 if (scope)
10036 pop_p = push_scope (scope);
10037
10038 /* Perform deferred access control checks, now that we know in which
10039 SCOPE the declared entity resides. */
10040 if (!member_p && decl)
10041 {
10042 tree saved_current_function_decl = NULL_TREE;
10043
10044 /* If the entity being declared is a function, pretend that we
10045 are in its scope. If it is a `friend', it may have access to
10046 things that would not otherwise be accessible. */
10047 if (TREE_CODE (decl) == FUNCTION_DECL)
10048 {
10049 saved_current_function_decl = current_function_decl;
10050 current_function_decl = decl;
10051 }
10052
10053 /* Perform the access control checks for the declarator and the
10054 the decl-specifiers. */
10055 perform_deferred_access_checks ();
10056
10057 /* Restore the saved value. */
10058 if (TREE_CODE (decl) == FUNCTION_DECL)
10059 current_function_decl = saved_current_function_decl;
10060 }
10061
10062 /* Parse the initializer. */
10063 if (is_initialized)
10064 initializer = cp_parser_initializer (parser,
10065 &is_parenthesized_init,
10066 &is_non_constant_init);
10067 else
10068 {
10069 initializer = NULL_TREE;
10070 is_parenthesized_init = false;
10071 is_non_constant_init = true;
10072 }
10073
10074 /* The old parser allows attributes to appear after a parenthesized
10075 initializer. Mark Mitchell proposed removing this functionality
10076 on the GCC mailing lists on 2002-08-13. This parser accepts the
10077 attributes -- but ignores them. */
10078 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10079 if (cp_parser_attributes_opt (parser))
10080 warning ("attributes after parenthesized initializer ignored");
10081
10082 /* Leave the SCOPE, now that we have processed the initializer. It
10083 is important to do this before calling cp_finish_decl because it
10084 makes decisions about whether to create DECL_STMTs or not based
10085 on the current scope. */
10086 if (pop_p)
10087 pop_scope (scope);
10088
10089 /* For an in-class declaration, use `grokfield' to create the
10090 declaration. */
10091 if (member_p)
10092 {
10093 decl = grokfield (declarator, decl_specifiers,
10094 initializer, /*asmspec=*/NULL_TREE,
10095 /*attributes=*/NULL_TREE);
10096 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10097 cp_parser_save_default_args (parser, decl);
10098 }
10099
10100 /* Finish processing the declaration. But, skip friend
10101 declarations. */
10102 if (!friend_p && decl)
10103 cp_finish_decl (decl,
10104 initializer,
10105 asm_specification,
10106 /* If the initializer is in parentheses, then this is
10107 a direct-initialization, which means that an
10108 `explicit' constructor is OK. Otherwise, an
10109 `explicit' constructor cannot be used. */
10110 ((is_parenthesized_init || !is_initialized)
10111 ? 0 : LOOKUP_ONLYCONVERTING));
10112
10113 /* Remember whether or not variables were initialized by
10114 constant-expressions. */
10115 if (decl && TREE_CODE (decl) == VAR_DECL
10116 && is_initialized && !is_non_constant_init)
10117 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10118
10119 return decl;
10120 }
10121
10122 /* Parse a declarator.
10123
10124 declarator:
10125 direct-declarator
10126 ptr-operator declarator
10127
10128 abstract-declarator:
10129 ptr-operator abstract-declarator [opt]
10130 direct-abstract-declarator
10131
10132 GNU Extensions:
10133
10134 declarator:
10135 attributes [opt] direct-declarator
10136 attributes [opt] ptr-operator declarator
10137
10138 abstract-declarator:
10139 attributes [opt] ptr-operator abstract-declarator [opt]
10140 attributes [opt] direct-abstract-declarator
10141
10142 Returns a representation of the declarator. If the declarator has
10143 the form `* declarator', then an INDIRECT_REF is returned, whose
10144 only operand is the sub-declarator. Analogously, `& declarator' is
10145 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10146 used. The first operand is the TYPE for `X'. The second operand
10147 is an INDIRECT_REF whose operand is the sub-declarator.
10148
10149 Otherwise, the representation is as for a direct-declarator.
10150
10151 (It would be better to define a structure type to represent
10152 declarators, rather than abusing `tree' nodes to represent
10153 declarators. That would be much clearer and save some memory.
10154 There is no reason for declarators to be garbage-collected, for
10155 example; they are created during parser and no longer needed after
10156 `grokdeclarator' has been called.)
10157
10158 For a ptr-operator that has the optional cv-qualifier-seq,
10159 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10160 node.
10161
10162 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10163 detect constructor, destructor or conversion operators. It is set
10164 to -1 if the declarator is a name, and +1 if it is a
10165 function. Otherwise it is set to zero. Usually you just want to
10166 test for >0, but internally the negative value is used.
10167
10168 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10169 a decl-specifier-seq unless it declares a constructor, destructor,
10170 or conversion. It might seem that we could check this condition in
10171 semantic analysis, rather than parsing, but that makes it difficult
10172 to handle something like `f()'. We want to notice that there are
10173 no decl-specifiers, and therefore realize that this is an
10174 expression, not a declaration.)
10175
10176 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10177 the declarator is a direct-declarator of the form "(...)". */
10178
10179 static tree
cp_parser_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p,bool * parenthesized_p)10180 cp_parser_declarator (cp_parser* parser,
10181 cp_parser_declarator_kind dcl_kind,
10182 int* ctor_dtor_or_conv_p,
10183 bool* parenthesized_p)
10184 {
10185 cp_token *token;
10186 tree declarator;
10187 enum tree_code code;
10188 tree cv_qualifier_seq;
10189 tree class_type;
10190 tree attributes = NULL_TREE;
10191
10192 /* Assume this is not a constructor, destructor, or type-conversion
10193 operator. */
10194 if (ctor_dtor_or_conv_p)
10195 *ctor_dtor_or_conv_p = 0;
10196
10197 if (cp_parser_allow_gnu_extensions_p (parser))
10198 attributes = cp_parser_attributes_opt (parser);
10199
10200 /* Peek at the next token. */
10201 token = cp_lexer_peek_token (parser->lexer);
10202
10203 /* Check for the ptr-operator production. */
10204 cp_parser_parse_tentatively (parser);
10205 /* Parse the ptr-operator. */
10206 code = cp_parser_ptr_operator (parser,
10207 &class_type,
10208 &cv_qualifier_seq);
10209 /* If that worked, then we have a ptr-operator. */
10210 if (cp_parser_parse_definitely (parser))
10211 {
10212 /* If a ptr-operator was found, then this declarator was not
10213 parenthesized. */
10214 if (parenthesized_p)
10215 *parenthesized_p = true;
10216 /* The dependent declarator is optional if we are parsing an
10217 abstract-declarator. */
10218 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10219 cp_parser_parse_tentatively (parser);
10220
10221 /* Parse the dependent declarator. */
10222 declarator = cp_parser_declarator (parser, dcl_kind,
10223 /*ctor_dtor_or_conv_p=*/NULL,
10224 /*parenthesized_p=*/NULL);
10225
10226 /* If we are parsing an abstract-declarator, we must handle the
10227 case where the dependent declarator is absent. */
10228 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10229 && !cp_parser_parse_definitely (parser))
10230 declarator = NULL_TREE;
10231
10232 /* Build the representation of the ptr-operator. */
10233 if (code == INDIRECT_REF)
10234 declarator = make_pointer_declarator (cv_qualifier_seq,
10235 declarator);
10236 else
10237 declarator = make_reference_declarator (cv_qualifier_seq,
10238 declarator);
10239 /* Handle the pointer-to-member case. */
10240 if (class_type)
10241 declarator = build_nt (SCOPE_REF, class_type, declarator);
10242 }
10243 /* Everything else is a direct-declarator. */
10244 else
10245 {
10246 if (parenthesized_p)
10247 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10248 CPP_OPEN_PAREN);
10249 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10250 ctor_dtor_or_conv_p);
10251 }
10252
10253 if (attributes && declarator != error_mark_node)
10254 declarator = tree_cons (attributes, declarator, NULL_TREE);
10255
10256 return declarator;
10257 }
10258
10259 /* Parse a direct-declarator or direct-abstract-declarator.
10260
10261 direct-declarator:
10262 declarator-id
10263 direct-declarator ( parameter-declaration-clause )
10264 cv-qualifier-seq [opt]
10265 exception-specification [opt]
10266 direct-declarator [ constant-expression [opt] ]
10267 ( declarator )
10268
10269 direct-abstract-declarator:
10270 direct-abstract-declarator [opt]
10271 ( parameter-declaration-clause )
10272 cv-qualifier-seq [opt]
10273 exception-specification [opt]
10274 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10275 ( abstract-declarator )
10276
10277 Returns a representation of the declarator. DCL_KIND is
10278 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10279 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10280 we are parsing a direct-declarator. It is
10281 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10282 of ambiguity we prefer an abstract declarator, as per
10283 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10284 cp_parser_declarator.
10285
10286 For the declarator-id production, the representation is as for an
10287 id-expression, except that a qualified name is represented as a
10288 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10289 see the documentation of the FUNCTION_DECLARATOR_* macros for
10290 information about how to find the various declarator components.
10291 An array-declarator is represented as an ARRAY_REF. The
10292 direct-declarator is the first operand; the constant-expression
10293 indicating the size of the array is the second operand. */
10294
10295 static tree
cp_parser_direct_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p)10296 cp_parser_direct_declarator (cp_parser* parser,
10297 cp_parser_declarator_kind dcl_kind,
10298 int* ctor_dtor_or_conv_p)
10299 {
10300 cp_token *token;
10301 tree declarator = NULL_TREE;
10302 tree scope = NULL_TREE;
10303 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10304 bool saved_in_declarator_p = parser->in_declarator_p;
10305 bool first = true;
10306 bool pop_p = false;
10307
10308 while (true)
10309 {
10310 /* Peek at the next token. */
10311 token = cp_lexer_peek_token (parser->lexer);
10312 if (token->type == CPP_OPEN_PAREN)
10313 {
10314 /* This is either a parameter-declaration-clause, or a
10315 parenthesized declarator. When we know we are parsing a
10316 named declarator, it must be a parenthesized declarator
10317 if FIRST is true. For instance, `(int)' is a
10318 parameter-declaration-clause, with an omitted
10319 direct-abstract-declarator. But `((*))', is a
10320 parenthesized abstract declarator. Finally, when T is a
10321 template parameter `(T)' is a
10322 parameter-declaration-clause, and not a parenthesized
10323 named declarator.
10324
10325 We first try and parse a parameter-declaration-clause,
10326 and then try a nested declarator (if FIRST is true).
10327
10328 It is not an error for it not to be a
10329 parameter-declaration-clause, even when FIRST is
10330 false. Consider,
10331
10332 int i (int);
10333 int i (3);
10334
10335 The first is the declaration of a function while the
10336 second is a the definition of a variable, including its
10337 initializer.
10338
10339 Having seen only the parenthesis, we cannot know which of
10340 these two alternatives should be selected. Even more
10341 complex are examples like:
10342
10343 int i (int (a));
10344 int i (int (3));
10345
10346 The former is a function-declaration; the latter is a
10347 variable initialization.
10348
10349 Thus again, we try a parameter-declaration-clause, and if
10350 that fails, we back out and return. */
10351
10352 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10353 {
10354 tree params;
10355 unsigned saved_num_template_parameter_lists;
10356
10357 cp_parser_parse_tentatively (parser);
10358
10359 /* Consume the `('. */
10360 cp_lexer_consume_token (parser->lexer);
10361 if (first)
10362 {
10363 /* If this is going to be an abstract declarator, we're
10364 in a declarator and we can't have default args. */
10365 parser->default_arg_ok_p = false;
10366 parser->in_declarator_p = true;
10367 }
10368
10369 /* Inside the function parameter list, surrounding
10370 template-parameter-lists do not apply. */
10371 saved_num_template_parameter_lists
10372 = parser->num_template_parameter_lists;
10373 parser->num_template_parameter_lists = 0;
10374
10375 /* Parse the parameter-declaration-clause. */
10376 params = cp_parser_parameter_declaration_clause (parser);
10377
10378 parser->num_template_parameter_lists
10379 = saved_num_template_parameter_lists;
10380
10381 /* If all went well, parse the cv-qualifier-seq and the
10382 exception-specification. */
10383 if (cp_parser_parse_definitely (parser))
10384 {
10385 tree cv_qualifiers;
10386 tree exception_specification;
10387
10388 if (ctor_dtor_or_conv_p)
10389 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10390 first = false;
10391 /* Consume the `)'. */
10392 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10393
10394 /* Parse the cv-qualifier-seq. */
10395 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10396 /* And the exception-specification. */
10397 exception_specification
10398 = cp_parser_exception_specification_opt (parser);
10399
10400 /* Create the function-declarator. */
10401 declarator = make_call_declarator (declarator,
10402 params,
10403 cv_qualifiers,
10404 exception_specification);
10405 /* Any subsequent parameter lists are to do with
10406 return type, so are not those of the declared
10407 function. */
10408 parser->default_arg_ok_p = false;
10409
10410 /* Repeat the main loop. */
10411 continue;
10412 }
10413 }
10414
10415 /* If this is the first, we can try a parenthesized
10416 declarator. */
10417 if (first)
10418 {
10419 bool saved_in_type_id_in_expr_p;
10420
10421 parser->default_arg_ok_p = saved_default_arg_ok_p;
10422 parser->in_declarator_p = saved_in_declarator_p;
10423
10424 /* Consume the `('. */
10425 cp_lexer_consume_token (parser->lexer);
10426 /* Parse the nested declarator. */
10427 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10428 parser->in_type_id_in_expr_p = true;
10429 declarator
10430 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10431 /*parenthesized_p=*/NULL);
10432 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10433 first = false;
10434 /* Expect a `)'. */
10435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10436 declarator = error_mark_node;
10437 if (declarator == error_mark_node)
10438 break;
10439
10440 goto handle_declarator;
10441 }
10442 /* Otherwise, we must be done. */
10443 else
10444 break;
10445 }
10446 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10447 && token->type == CPP_OPEN_SQUARE)
10448 {
10449 /* Parse an array-declarator. */
10450 tree bounds;
10451
10452 if (ctor_dtor_or_conv_p)
10453 *ctor_dtor_or_conv_p = 0;
10454
10455 first = false;
10456 parser->default_arg_ok_p = false;
10457 parser->in_declarator_p = true;
10458 /* Consume the `['. */
10459 cp_lexer_consume_token (parser->lexer);
10460 /* Peek at the next token. */
10461 token = cp_lexer_peek_token (parser->lexer);
10462 /* If the next token is `]', then there is no
10463 constant-expression. */
10464 if (token->type != CPP_CLOSE_SQUARE)
10465 {
10466 bool non_constant_p;
10467
10468 bounds
10469 = cp_parser_constant_expression (parser,
10470 /*allow_non_constant=*/true,
10471 &non_constant_p);
10472 if (!non_constant_p)
10473 bounds = fold_non_dependent_expr (bounds);
10474 }
10475 else
10476 bounds = NULL_TREE;
10477 /* Look for the closing `]'. */
10478 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10479 {
10480 declarator = error_mark_node;
10481 break;
10482 }
10483
10484 declarator = build_nt (ARRAY_REF, declarator, bounds);
10485 }
10486 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10487 {
10488 /* Parse a declarator-id */
10489 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10490 cp_parser_parse_tentatively (parser);
10491 declarator = cp_parser_declarator_id (parser);
10492 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10493 {
10494 if (!cp_parser_parse_definitely (parser))
10495 declarator = error_mark_node;
10496 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10497 {
10498 cp_parser_error (parser, "expected unqualified-id");
10499 declarator = error_mark_node;
10500 }
10501 }
10502
10503 if (declarator == error_mark_node)
10504 break;
10505
10506 if (TREE_CODE (declarator) == SCOPE_REF
10507 && !current_scope ())
10508 {
10509 tree scope = TREE_OPERAND (declarator, 0);
10510
10511 /* In the declaration of a member of a template class
10512 outside of the class itself, the SCOPE will sometimes
10513 be a TYPENAME_TYPE. For example, given:
10514
10515 template <typename T>
10516 int S<T>::R::i = 3;
10517
10518 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10519 this context, we must resolve S<T>::R to an ordinary
10520 type, rather than a typename type.
10521
10522 The reason we normally avoid resolving TYPENAME_TYPEs
10523 is that a specialization of `S' might render
10524 `S<T>::R' not a type. However, if `S' is
10525 specialized, then this `i' will not be used, so there
10526 is no harm in resolving the types here. */
10527 if (TREE_CODE (scope) == TYPENAME_TYPE)
10528 {
10529 tree type;
10530
10531 /* Resolve the TYPENAME_TYPE. */
10532 type = resolve_typename_type (scope,
10533 /*only_current_p=*/false);
10534 /* If that failed, the declarator is invalid. */
10535 if (type == error_mark_node)
10536 error ("`%T::%D' is not a type",
10537 TYPE_CONTEXT (scope),
10538 TYPE_IDENTIFIER (scope));
10539 /* Build a new DECLARATOR. */
10540 declarator = build_nt (SCOPE_REF,
10541 type,
10542 TREE_OPERAND (declarator, 1));
10543 }
10544 }
10545
10546 /* Check to see whether the declarator-id names a constructor,
10547 destructor, or conversion. */
10548 if (declarator && ctor_dtor_or_conv_p
10549 && ((TREE_CODE (declarator) == SCOPE_REF
10550 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10551 || (TREE_CODE (declarator) != SCOPE_REF
10552 && at_class_scope_p ())))
10553 {
10554 tree unqualified_name;
10555 tree class_type;
10556
10557 /* Get the unqualified part of the name. */
10558 if (TREE_CODE (declarator) == SCOPE_REF)
10559 {
10560 class_type = TREE_OPERAND (declarator, 0);
10561 unqualified_name = TREE_OPERAND (declarator, 1);
10562 }
10563 else
10564 {
10565 class_type = current_class_type;
10566 unqualified_name = declarator;
10567 }
10568
10569 /* See if it names ctor, dtor or conv. */
10570 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10571 || IDENTIFIER_TYPENAME_P (unqualified_name)
10572 || constructor_name_p (unqualified_name, class_type)
10573 || (TREE_CODE (unqualified_name) == TYPE_DECL
10574 && same_type_p (TREE_TYPE (unqualified_name),
10575 class_type)))
10576 *ctor_dtor_or_conv_p = -1;
10577 }
10578
10579 handle_declarator:;
10580 scope = get_scope_of_declarator (declarator);
10581 if (scope)
10582 /* Any names that appear after the declarator-id for a
10583 member are looked up in the containing scope. */
10584 pop_p = push_scope (scope);
10585 parser->in_declarator_p = true;
10586 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10587 || (declarator
10588 && (TREE_CODE (declarator) == SCOPE_REF
10589 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10590 /* Default args are only allowed on function
10591 declarations. */
10592 parser->default_arg_ok_p = saved_default_arg_ok_p;
10593 else
10594 parser->default_arg_ok_p = false;
10595
10596 first = false;
10597 }
10598 /* We're done. */
10599 else
10600 break;
10601 }
10602
10603 /* For an abstract declarator, we might wind up with nothing at this
10604 point. That's an error; the declarator is not optional. */
10605 if (!declarator)
10606 cp_parser_error (parser, "expected declarator");
10607
10608 /* If we entered a scope, we must exit it now. */
10609 if (pop_p)
10610 pop_scope (scope);
10611
10612 parser->default_arg_ok_p = saved_default_arg_ok_p;
10613 parser->in_declarator_p = saved_in_declarator_p;
10614
10615 return declarator;
10616 }
10617
10618 /* Parse a ptr-operator.
10619
10620 ptr-operator:
10621 * cv-qualifier-seq [opt]
10622 &
10623 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10624
10625 GNU Extension:
10626
10627 ptr-operator:
10628 & cv-qualifier-seq [opt]
10629
10630 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10631 used. Returns ADDR_EXPR if a reference was used. In the
10632 case of a pointer-to-member, *TYPE is filled in with the
10633 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10634 with the cv-qualifier-seq, or NULL_TREE, if there are no
10635 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10636
10637 static enum tree_code
cp_parser_ptr_operator(cp_parser * parser,tree * type,tree * cv_qualifier_seq)10638 cp_parser_ptr_operator (cp_parser* parser,
10639 tree* type,
10640 tree* cv_qualifier_seq)
10641 {
10642 enum tree_code code = ERROR_MARK;
10643 cp_token *token;
10644
10645 /* Assume that it's not a pointer-to-member. */
10646 *type = NULL_TREE;
10647 /* And that there are no cv-qualifiers. */
10648 *cv_qualifier_seq = NULL_TREE;
10649
10650 /* Peek at the next token. */
10651 token = cp_lexer_peek_token (parser->lexer);
10652 /* If it's a `*' or `&' we have a pointer or reference. */
10653 if (token->type == CPP_MULT || token->type == CPP_AND)
10654 {
10655 /* Remember which ptr-operator we were processing. */
10656 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10657
10658 /* Consume the `*' or `&'. */
10659 cp_lexer_consume_token (parser->lexer);
10660
10661 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10662 `&', if we are allowing GNU extensions. (The only qualifier
10663 that can legally appear after `&' is `restrict', but that is
10664 enforced during semantic analysis. */
10665 if (code == INDIRECT_REF
10666 || cp_parser_allow_gnu_extensions_p (parser))
10667 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10668 }
10669 else
10670 {
10671 /* Try the pointer-to-member case. */
10672 cp_parser_parse_tentatively (parser);
10673 /* Look for the optional `::' operator. */
10674 cp_parser_global_scope_opt (parser,
10675 /*current_scope_valid_p=*/false);
10676 /* Look for the nested-name specifier. */
10677 cp_parser_nested_name_specifier (parser,
10678 /*typename_keyword_p=*/false,
10679 /*check_dependency_p=*/true,
10680 /*type_p=*/false,
10681 /*is_declaration=*/false);
10682 /* If we found it, and the next token is a `*', then we are
10683 indeed looking at a pointer-to-member operator. */
10684 if (!cp_parser_error_occurred (parser)
10685 && cp_parser_require (parser, CPP_MULT, "`*'"))
10686 {
10687 /* The type of which the member is a member is given by the
10688 current SCOPE. */
10689 *type = parser->scope;
10690 /* The next name will not be qualified. */
10691 parser->scope = NULL_TREE;
10692 parser->qualifying_scope = NULL_TREE;
10693 parser->object_scope = NULL_TREE;
10694 /* Indicate that the `*' operator was used. */
10695 code = INDIRECT_REF;
10696 /* Look for the optional cv-qualifier-seq. */
10697 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10698 }
10699 /* If that didn't work we don't have a ptr-operator. */
10700 if (!cp_parser_parse_definitely (parser))
10701 cp_parser_error (parser, "expected ptr-operator");
10702 }
10703
10704 return code;
10705 }
10706
10707 /* Parse an (optional) cv-qualifier-seq.
10708
10709 cv-qualifier-seq:
10710 cv-qualifier cv-qualifier-seq [opt]
10711
10712 Returns a TREE_LIST. The TREE_VALUE of each node is the
10713 representation of a cv-qualifier. */
10714
10715 static tree
cp_parser_cv_qualifier_seq_opt(cp_parser * parser)10716 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10717 {
10718 tree cv_qualifiers = NULL_TREE;
10719
10720 while (true)
10721 {
10722 tree cv_qualifier;
10723
10724 /* Look for the next cv-qualifier. */
10725 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10726 /* If we didn't find one, we're done. */
10727 if (!cv_qualifier)
10728 break;
10729
10730 /* Add this cv-qualifier to the list. */
10731 cv_qualifiers
10732 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10733 }
10734
10735 /* We built up the list in reverse order. */
10736 return nreverse (cv_qualifiers);
10737 }
10738
10739 /* Parse an (optional) cv-qualifier.
10740
10741 cv-qualifier:
10742 const
10743 volatile
10744
10745 GNU Extension:
10746
10747 cv-qualifier:
10748 __restrict__ */
10749
10750 static tree
cp_parser_cv_qualifier_opt(cp_parser * parser)10751 cp_parser_cv_qualifier_opt (cp_parser* parser)
10752 {
10753 cp_token *token;
10754 tree cv_qualifier = NULL_TREE;
10755
10756 /* Peek at the next token. */
10757 token = cp_lexer_peek_token (parser->lexer);
10758 /* See if it's a cv-qualifier. */
10759 switch (token->keyword)
10760 {
10761 case RID_CONST:
10762 case RID_VOLATILE:
10763 case RID_RESTRICT:
10764 /* Save the value of the token. */
10765 cv_qualifier = token->value;
10766 /* Consume the token. */
10767 cp_lexer_consume_token (parser->lexer);
10768 break;
10769
10770 default:
10771 break;
10772 }
10773
10774 return cv_qualifier;
10775 }
10776
10777 /* Parse a declarator-id.
10778
10779 declarator-id:
10780 id-expression
10781 :: [opt] nested-name-specifier [opt] type-name
10782
10783 In the `id-expression' case, the value returned is as for
10784 cp_parser_id_expression if the id-expression was an unqualified-id.
10785 If the id-expression was a qualified-id, then a SCOPE_REF is
10786 returned. The first operand is the scope (either a NAMESPACE_DECL
10787 or TREE_TYPE), but the second is still just a representation of an
10788 unqualified-id. */
10789
10790 static tree
cp_parser_declarator_id(cp_parser * parser)10791 cp_parser_declarator_id (cp_parser* parser)
10792 {
10793 tree id_expression;
10794
10795 /* The expression must be an id-expression. Assume that qualified
10796 names are the names of types so that:
10797
10798 template <class T>
10799 int S<T>::R::i = 3;
10800
10801 will work; we must treat `S<T>::R' as the name of a type.
10802 Similarly, assume that qualified names are templates, where
10803 required, so that:
10804
10805 template <class T>
10806 int S<T>::R<T>::i = 3;
10807
10808 will work, too. */
10809 id_expression = cp_parser_id_expression (parser,
10810 /*template_keyword_p=*/false,
10811 /*check_dependency_p=*/false,
10812 /*template_p=*/NULL,
10813 /*declarator_p=*/true);
10814 /* If the name was qualified, create a SCOPE_REF to represent
10815 that. */
10816 if (parser->scope)
10817 {
10818 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10819 parser->scope = NULL_TREE;
10820 }
10821
10822 return id_expression;
10823 }
10824
10825 /* Parse a type-id.
10826
10827 type-id:
10828 type-specifier-seq abstract-declarator [opt]
10829
10830 Returns the TYPE specified. */
10831
10832 static tree
cp_parser_type_id(cp_parser * parser)10833 cp_parser_type_id (cp_parser* parser)
10834 {
10835 tree type_specifier_seq;
10836 tree abstract_declarator;
10837
10838 /* Parse the type-specifier-seq. */
10839 type_specifier_seq
10840 = cp_parser_type_specifier_seq (parser);
10841 if (type_specifier_seq == error_mark_node)
10842 return error_mark_node;
10843
10844 /* There might or might not be an abstract declarator. */
10845 cp_parser_parse_tentatively (parser);
10846 /* Look for the declarator. */
10847 abstract_declarator
10848 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10849 /*parenthesized_p=*/NULL);
10850 /* Check to see if there really was a declarator. */
10851 if (!cp_parser_parse_definitely (parser))
10852 abstract_declarator = NULL_TREE;
10853
10854 return groktypename (build_tree_list (type_specifier_seq,
10855 abstract_declarator));
10856 }
10857
10858 /* Parse a type-specifier-seq.
10859
10860 type-specifier-seq:
10861 type-specifier type-specifier-seq [opt]
10862
10863 GNU extension:
10864
10865 type-specifier-seq:
10866 attributes type-specifier-seq [opt]
10867
10868 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10869 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10870
10871 static tree
cp_parser_type_specifier_seq(cp_parser * parser)10872 cp_parser_type_specifier_seq (cp_parser* parser)
10873 {
10874 bool seen_type_specifier = false;
10875 tree type_specifier_seq = NULL_TREE;
10876
10877 /* Parse the type-specifiers and attributes. */
10878 while (true)
10879 {
10880 tree type_specifier;
10881
10882 /* Check for attributes first. */
10883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10884 {
10885 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10886 NULL_TREE,
10887 type_specifier_seq);
10888 continue;
10889 }
10890
10891 /* After the first type-specifier, others are optional. */
10892 if (seen_type_specifier)
10893 cp_parser_parse_tentatively (parser);
10894 /* Look for the type-specifier. */
10895 type_specifier = cp_parser_type_specifier (parser,
10896 CP_PARSER_FLAGS_NONE,
10897 /*is_friend=*/false,
10898 /*is_declaration=*/false,
10899 NULL,
10900 NULL);
10901 /* If the first type-specifier could not be found, this is not a
10902 type-specifier-seq at all. */
10903 if (!seen_type_specifier && type_specifier == error_mark_node)
10904 return error_mark_node;
10905 /* If subsequent type-specifiers could not be found, the
10906 type-specifier-seq is complete. */
10907 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10908 break;
10909
10910 /* Add the new type-specifier to the list. */
10911 type_specifier_seq
10912 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10913 seen_type_specifier = true;
10914 }
10915
10916 /* We built up the list in reverse order. */
10917 return nreverse (type_specifier_seq);
10918 }
10919
10920 /* Parse a parameter-declaration-clause.
10921
10922 parameter-declaration-clause:
10923 parameter-declaration-list [opt] ... [opt]
10924 parameter-declaration-list , ...
10925
10926 Returns a representation for the parameter declarations. Each node
10927 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10928 representation.) If the parameter-declaration-clause ends with an
10929 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10930 list. A return value of NULL_TREE indicates a
10931 parameter-declaration-clause consisting only of an ellipsis. */
10932
10933 static tree
cp_parser_parameter_declaration_clause(cp_parser * parser)10934 cp_parser_parameter_declaration_clause (cp_parser* parser)
10935 {
10936 tree parameters;
10937 cp_token *token;
10938 bool ellipsis_p;
10939
10940 /* Peek at the next token. */
10941 token = cp_lexer_peek_token (parser->lexer);
10942 /* Check for trivial parameter-declaration-clauses. */
10943 if (token->type == CPP_ELLIPSIS)
10944 {
10945 /* Consume the `...' token. */
10946 cp_lexer_consume_token (parser->lexer);
10947 return NULL_TREE;
10948 }
10949 else if (token->type == CPP_CLOSE_PAREN)
10950 /* There are no parameters. */
10951 {
10952 #ifndef NO_IMPLICIT_EXTERN_C
10953 if (in_system_header && current_class_type == NULL
10954 && current_lang_name == lang_name_c)
10955 return NULL_TREE;
10956 else
10957 #endif
10958 return void_list_node;
10959 }
10960 /* Check for `(void)', too, which is a special case. */
10961 else if (token->keyword == RID_VOID
10962 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10963 == CPP_CLOSE_PAREN))
10964 {
10965 /* Consume the `void' token. */
10966 cp_lexer_consume_token (parser->lexer);
10967 /* There are no parameters. */
10968 return void_list_node;
10969 }
10970
10971 /* Parse the parameter-declaration-list. */
10972 parameters = cp_parser_parameter_declaration_list (parser);
10973 /* If a parse error occurred while parsing the
10974 parameter-declaration-list, then the entire
10975 parameter-declaration-clause is erroneous. */
10976 if (parameters == error_mark_node)
10977 return error_mark_node;
10978
10979 /* Peek at the next token. */
10980 token = cp_lexer_peek_token (parser->lexer);
10981 /* If it's a `,', the clause should terminate with an ellipsis. */
10982 if (token->type == CPP_COMMA)
10983 {
10984 /* Consume the `,'. */
10985 cp_lexer_consume_token (parser->lexer);
10986 /* Expect an ellipsis. */
10987 ellipsis_p
10988 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10989 }
10990 /* It might also be `...' if the optional trailing `,' was
10991 omitted. */
10992 else if (token->type == CPP_ELLIPSIS)
10993 {
10994 /* Consume the `...' token. */
10995 cp_lexer_consume_token (parser->lexer);
10996 /* And remember that we saw it. */
10997 ellipsis_p = true;
10998 }
10999 else
11000 ellipsis_p = false;
11001
11002 /* Finish the parameter list. */
11003 return finish_parmlist (parameters, ellipsis_p);
11004 }
11005
11006 /* Parse a parameter-declaration-list.
11007
11008 parameter-declaration-list:
11009 parameter-declaration
11010 parameter-declaration-list , parameter-declaration
11011
11012 Returns a representation of the parameter-declaration-list, as for
11013 cp_parser_parameter_declaration_clause. However, the
11014 `void_list_node' is never appended to the list. */
11015
11016 static tree
cp_parser_parameter_declaration_list(cp_parser * parser)11017 cp_parser_parameter_declaration_list (cp_parser* parser)
11018 {
11019 tree parameters = NULL_TREE;
11020
11021 /* Look for more parameters. */
11022 while (true)
11023 {
11024 tree parameter;
11025 bool parenthesized_p;
11026 /* Parse the parameter. */
11027 parameter
11028 = cp_parser_parameter_declaration (parser,
11029 /*template_parm_p=*/false,
11030 &parenthesized_p);
11031
11032 /* If a parse error occurred parsing the parameter declaration,
11033 then the entire parameter-declaration-list is erroneous. */
11034 if (parameter == error_mark_node)
11035 {
11036 parameters = error_mark_node;
11037 break;
11038 }
11039 /* Add the new parameter to the list. */
11040 TREE_CHAIN (parameter) = parameters;
11041 parameters = parameter;
11042
11043 /* Peek at the next token. */
11044 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11045 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11046 /* The parameter-declaration-list is complete. */
11047 break;
11048 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11049 {
11050 cp_token *token;
11051
11052 /* Peek at the next token. */
11053 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11054 /* If it's an ellipsis, then the list is complete. */
11055 if (token->type == CPP_ELLIPSIS)
11056 break;
11057 /* Otherwise, there must be more parameters. Consume the
11058 `,'. */
11059 cp_lexer_consume_token (parser->lexer);
11060 /* When parsing something like:
11061
11062 int i(float f, double d)
11063
11064 we can tell after seeing the declaration for "f" that we
11065 are not looking at an initialization of a variable "i",
11066 but rather at the declaration of a function "i".
11067
11068 Due to the fact that the parsing of template arguments
11069 (as specified to a template-id) requires backtracking we
11070 cannot use this technique when inside a template argument
11071 list. */
11072 if (!parser->in_template_argument_list_p
11073 && !parser->in_type_id_in_expr_p
11074 && cp_parser_parsing_tentatively (parser)
11075 && !cp_parser_committed_to_tentative_parse (parser)
11076 /* However, a parameter-declaration of the form
11077 "foat(f)" (which is a valid declaration of a
11078 parameter "f") can also be interpreted as an
11079 expression (the conversion of "f" to "float"). */
11080 && !parenthesized_p)
11081 cp_parser_commit_to_tentative_parse (parser);
11082 }
11083 else
11084 {
11085 cp_parser_error (parser, "expected `,' or `...'");
11086 if (!cp_parser_parsing_tentatively (parser)
11087 || cp_parser_committed_to_tentative_parse (parser))
11088 cp_parser_skip_to_closing_parenthesis (parser,
11089 /*recovering=*/true,
11090 /*or_comma=*/false,
11091 /*consume_paren=*/false);
11092 break;
11093 }
11094 }
11095
11096 /* We built up the list in reverse order; straighten it out now. */
11097 return nreverse (parameters);
11098 }
11099
11100 /* Parse a parameter declaration.
11101
11102 parameter-declaration:
11103 decl-specifier-seq declarator
11104 decl-specifier-seq declarator = assignment-expression
11105 decl-specifier-seq abstract-declarator [opt]
11106 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11107
11108 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11109 declares a template parameter. (In that case, a non-nested `>'
11110 token encountered during the parsing of the assignment-expression
11111 is not interpreted as a greater-than operator.)
11112
11113 Returns a TREE_LIST representing the parameter-declaration. The
11114 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11115 there is no default argument. The TREE_VALUE is a representation
11116 of the decl-specifier-seq and declarator. In particular, the
11117 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11118 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11119 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11120 the declarator is of the form "(p)". */
11121
11122 static tree
cp_parser_parameter_declaration(cp_parser * parser,bool template_parm_p,bool * parenthesized_p)11123 cp_parser_parameter_declaration (cp_parser *parser,
11124 bool template_parm_p,
11125 bool *parenthesized_p)
11126 {
11127 int declares_class_or_enum;
11128 bool greater_than_is_operator_p;
11129 tree decl_specifiers;
11130 tree attributes;
11131 tree declarator;
11132 tree default_argument;
11133 tree parameter;
11134 cp_token *token;
11135 const char *saved_message;
11136
11137 /* In a template parameter, `>' is not an operator.
11138
11139 [temp.param]
11140
11141 When parsing a default template-argument for a non-type
11142 template-parameter, the first non-nested `>' is taken as the end
11143 of the template parameter-list rather than a greater-than
11144 operator. */
11145 greater_than_is_operator_p = !template_parm_p;
11146
11147 /* Type definitions may not appear in parameter types. */
11148 saved_message = parser->type_definition_forbidden_message;
11149 parser->type_definition_forbidden_message
11150 = "types may not be defined in parameter types";
11151
11152 /* Parse the declaration-specifiers. */
11153 decl_specifiers
11154 = cp_parser_decl_specifier_seq (parser,
11155 CP_PARSER_FLAGS_NONE,
11156 &attributes,
11157 &declares_class_or_enum);
11158 /* If an error occurred, there's no reason to attempt to parse the
11159 rest of the declaration. */
11160 if (cp_parser_error_occurred (parser))
11161 {
11162 parser->type_definition_forbidden_message = saved_message;
11163 return error_mark_node;
11164 }
11165
11166 /* Peek at the next token. */
11167 token = cp_lexer_peek_token (parser->lexer);
11168 /* If the next token is a `)', `,', `=', `>', or `...', then there
11169 is no declarator. */
11170 if (token->type == CPP_CLOSE_PAREN
11171 || token->type == CPP_COMMA
11172 || token->type == CPP_EQ
11173 || token->type == CPP_ELLIPSIS
11174 || token->type == CPP_GREATER)
11175 {
11176 declarator = NULL_TREE;
11177 if (parenthesized_p)
11178 *parenthesized_p = false;
11179 }
11180 /* Otherwise, there should be a declarator. */
11181 else
11182 {
11183 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11184 parser->default_arg_ok_p = false;
11185
11186 /* After seeing a decl-specifier-seq, if the next token is not a
11187 "(", there is no possibility that the code is a valid
11188 expression. Therefore, if parsing tentatively, we commit at
11189 this point. */
11190 if (!parser->in_template_argument_list_p
11191 /* In an expression context, having seen:
11192
11193 (int((char ...
11194
11195 we cannot be sure whether we are looking at a
11196 function-type (taking a "char" as a parameter) or a cast
11197 of some object of type "char" to "int". */
11198 && !parser->in_type_id_in_expr_p
11199 && cp_parser_parsing_tentatively (parser)
11200 && !cp_parser_committed_to_tentative_parse (parser)
11201 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11202 cp_parser_commit_to_tentative_parse (parser);
11203 /* Parse the declarator. */
11204 declarator = cp_parser_declarator (parser,
11205 CP_PARSER_DECLARATOR_EITHER,
11206 /*ctor_dtor_or_conv_p=*/NULL,
11207 parenthesized_p);
11208 parser->default_arg_ok_p = saved_default_arg_ok_p;
11209 /* After the declarator, allow more attributes. */
11210 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11211 }
11212
11213 /* The restriction on defining new types applies only to the type
11214 of the parameter, not to the default argument. */
11215 parser->type_definition_forbidden_message = saved_message;
11216
11217 /* If the next token is `=', then process a default argument. */
11218 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11219 {
11220 bool saved_greater_than_is_operator_p;
11221 /* Consume the `='. */
11222 cp_lexer_consume_token (parser->lexer);
11223
11224 /* If we are defining a class, then the tokens that make up the
11225 default argument must be saved and processed later. */
11226 if (!template_parm_p && at_class_scope_p ()
11227 && TYPE_BEING_DEFINED (current_class_type))
11228 {
11229 unsigned depth = 0;
11230
11231 /* Create a DEFAULT_ARG to represented the unparsed default
11232 argument. */
11233 default_argument = make_node (DEFAULT_ARG);
11234 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11235
11236 /* Add tokens until we have processed the entire default
11237 argument. */
11238 while (true)
11239 {
11240 bool done = false;
11241 cp_token *token;
11242
11243 /* Peek at the next token. */
11244 token = cp_lexer_peek_token (parser->lexer);
11245 /* What we do depends on what token we have. */
11246 switch (token->type)
11247 {
11248 /* In valid code, a default argument must be
11249 immediately followed by a `,' `)', or `...'. */
11250 case CPP_COMMA:
11251 case CPP_CLOSE_PAREN:
11252 case CPP_ELLIPSIS:
11253 /* If we run into a non-nested `;', `}', or `]',
11254 then the code is invalid -- but the default
11255 argument is certainly over. */
11256 case CPP_SEMICOLON:
11257 case CPP_CLOSE_BRACE:
11258 case CPP_CLOSE_SQUARE:
11259 if (depth == 0)
11260 done = true;
11261 /* Update DEPTH, if necessary. */
11262 else if (token->type == CPP_CLOSE_PAREN
11263 || token->type == CPP_CLOSE_BRACE
11264 || token->type == CPP_CLOSE_SQUARE)
11265 --depth;
11266 break;
11267
11268 case CPP_OPEN_PAREN:
11269 case CPP_OPEN_SQUARE:
11270 case CPP_OPEN_BRACE:
11271 ++depth;
11272 break;
11273
11274 case CPP_GREATER:
11275 /* If we see a non-nested `>', and `>' is not an
11276 operator, then it marks the end of the default
11277 argument. */
11278 if (!depth && !greater_than_is_operator_p)
11279 done = true;
11280 break;
11281
11282 /* If we run out of tokens, issue an error message. */
11283 case CPP_EOF:
11284 error ("file ends in default argument");
11285 done = true;
11286 break;
11287
11288 case CPP_NAME:
11289 case CPP_SCOPE:
11290 /* In these cases, we should look for template-ids.
11291 For example, if the default argument is
11292 `X<int, double>()', we need to do name lookup to
11293 figure out whether or not `X' is a template; if
11294 so, the `,' does not end the default argument.
11295
11296 That is not yet done. */
11297 break;
11298
11299 default:
11300 break;
11301 }
11302
11303 /* If we've reached the end, stop. */
11304 if (done)
11305 break;
11306
11307 /* Add the token to the token block. */
11308 token = cp_lexer_consume_token (parser->lexer);
11309 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11310 token);
11311 }
11312 }
11313 /* Outside of a class definition, we can just parse the
11314 assignment-expression. */
11315 else
11316 {
11317 bool saved_local_variables_forbidden_p;
11318
11319 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11320 set correctly. */
11321 saved_greater_than_is_operator_p
11322 = parser->greater_than_is_operator_p;
11323 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11324 /* Local variable names (and the `this' keyword) may not
11325 appear in a default argument. */
11326 saved_local_variables_forbidden_p
11327 = parser->local_variables_forbidden_p;
11328 parser->local_variables_forbidden_p = true;
11329 /* Parse the assignment-expression. */
11330 default_argument = cp_parser_assignment_expression (parser);
11331 /* Restore saved state. */
11332 parser->greater_than_is_operator_p
11333 = saved_greater_than_is_operator_p;
11334 parser->local_variables_forbidden_p
11335 = saved_local_variables_forbidden_p;
11336 }
11337 if (!parser->default_arg_ok_p)
11338 {
11339 if (!flag_pedantic_errors)
11340 warning ("deprecated use of default argument for parameter of non-function");
11341 else
11342 {
11343 error ("default arguments are only permitted for function parameters");
11344 default_argument = NULL_TREE;
11345 }
11346 }
11347 }
11348 else
11349 default_argument = NULL_TREE;
11350
11351 /* Create the representation of the parameter. */
11352 if (attributes)
11353 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11354 parameter = build_tree_list (default_argument,
11355 build_tree_list (decl_specifiers,
11356 declarator));
11357
11358 return parameter;
11359 }
11360
11361 /* Parse a function-body.
11362
11363 function-body:
11364 compound_statement */
11365
11366 static void
cp_parser_function_body(cp_parser * parser)11367 cp_parser_function_body (cp_parser *parser)
11368 {
11369 cp_parser_compound_statement (parser, false);
11370 }
11371
11372 /* Parse a ctor-initializer-opt followed by a function-body. Return
11373 true if a ctor-initializer was present. */
11374
11375 static bool
cp_parser_ctor_initializer_opt_and_function_body(cp_parser * parser)11376 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11377 {
11378 tree body;
11379 bool ctor_initializer_p;
11380
11381 /* Begin the function body. */
11382 body = begin_function_body ();
11383 /* Parse the optional ctor-initializer. */
11384 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11385 /* Parse the function-body. */
11386 cp_parser_function_body (parser);
11387 /* Finish the function body. */
11388 finish_function_body (body);
11389
11390 return ctor_initializer_p;
11391 }
11392
11393 /* Parse an initializer.
11394
11395 initializer:
11396 = initializer-clause
11397 ( expression-list )
11398
11399 Returns a expression representing the initializer. If no
11400 initializer is present, NULL_TREE is returned.
11401
11402 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11403 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11404 set to FALSE if there is no initializer present. If there is an
11405 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11406 is set to true; otherwise it is set to false. */
11407
11408 static tree
cp_parser_initializer(cp_parser * parser,bool * is_parenthesized_init,bool * non_constant_p)11409 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11410 bool* non_constant_p)
11411 {
11412 cp_token *token;
11413 tree init;
11414
11415 /* Peek at the next token. */
11416 token = cp_lexer_peek_token (parser->lexer);
11417
11418 /* Let our caller know whether or not this initializer was
11419 parenthesized. */
11420 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11421 /* Assume that the initializer is constant. */
11422 *non_constant_p = false;
11423
11424 if (token->type == CPP_EQ)
11425 {
11426 /* Consume the `='. */
11427 cp_lexer_consume_token (parser->lexer);
11428 /* Parse the initializer-clause. */
11429 init = cp_parser_initializer_clause (parser, non_constant_p);
11430 }
11431 else if (token->type == CPP_OPEN_PAREN)
11432 init = cp_parser_parenthesized_expression_list (parser, false,
11433 non_constant_p);
11434 else
11435 {
11436 /* Anything else is an error. */
11437 cp_parser_error (parser, "expected initializer");
11438 init = error_mark_node;
11439 }
11440
11441 return init;
11442 }
11443
11444 /* Parse an initializer-clause.
11445
11446 initializer-clause:
11447 assignment-expression
11448 { initializer-list , [opt] }
11449 { }
11450
11451 Returns an expression representing the initializer.
11452
11453 If the `assignment-expression' production is used the value
11454 returned is simply a representation for the expression.
11455
11456 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11457 the elements of the initializer-list (or NULL_TREE, if the last
11458 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11459 NULL_TREE. There is no way to detect whether or not the optional
11460 trailing `,' was provided. NON_CONSTANT_P is as for
11461 cp_parser_initializer. */
11462
11463 static tree
cp_parser_initializer_clause(cp_parser * parser,bool * non_constant_p)11464 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11465 {
11466 tree initializer;
11467
11468 /* If it is not a `{', then we are looking at an
11469 assignment-expression. */
11470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11471 {
11472 initializer
11473 = cp_parser_constant_expression (parser,
11474 /*allow_non_constant_p=*/true,
11475 non_constant_p);
11476 if (!*non_constant_p)
11477 initializer = fold_non_dependent_expr (initializer);
11478 }
11479 else
11480 {
11481 /* Consume the `{' token. */
11482 cp_lexer_consume_token (parser->lexer);
11483 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11484 initializer = make_node (CONSTRUCTOR);
11485 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11486 necessary, but check_initializer depends upon it, for
11487 now. */
11488 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11489 /* If it's not a `}', then there is a non-trivial initializer. */
11490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11491 {
11492 /* Parse the initializer list. */
11493 CONSTRUCTOR_ELTS (initializer)
11494 = cp_parser_initializer_list (parser, non_constant_p);
11495 /* A trailing `,' token is allowed. */
11496 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11497 cp_lexer_consume_token (parser->lexer);
11498 }
11499 /* Now, there should be a trailing `}'. */
11500 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11501 }
11502
11503 return initializer;
11504 }
11505
11506 /* Parse an initializer-list.
11507
11508 initializer-list:
11509 initializer-clause
11510 initializer-list , initializer-clause
11511
11512 GNU Extension:
11513
11514 initializer-list:
11515 identifier : initializer-clause
11516 initializer-list, identifier : initializer-clause
11517
11518 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11519 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11520 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11521 as for cp_parser_initializer. */
11522
11523 static tree
cp_parser_initializer_list(cp_parser * parser,bool * non_constant_p)11524 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11525 {
11526 tree initializers = NULL_TREE;
11527
11528 /* Assume all of the expressions are constant. */
11529 *non_constant_p = false;
11530
11531 /* Parse the rest of the list. */
11532 while (true)
11533 {
11534 cp_token *token;
11535 tree identifier;
11536 tree initializer;
11537 bool clause_non_constant_p;
11538
11539 /* If the next token is an identifier and the following one is a
11540 colon, we are looking at the GNU designated-initializer
11541 syntax. */
11542 if (cp_parser_allow_gnu_extensions_p (parser)
11543 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11544 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11545 {
11546 /* Consume the identifier. */
11547 identifier = cp_lexer_consume_token (parser->lexer)->value;
11548 /* Consume the `:'. */
11549 cp_lexer_consume_token (parser->lexer);
11550 }
11551 else
11552 identifier = NULL_TREE;
11553
11554 /* Parse the initializer. */
11555 initializer = cp_parser_initializer_clause (parser,
11556 &clause_non_constant_p);
11557 /* If any clause is non-constant, so is the entire initializer. */
11558 if (clause_non_constant_p)
11559 *non_constant_p = true;
11560 /* Add it to the list. */
11561 initializers = tree_cons (identifier, initializer, initializers);
11562
11563 /* If the next token is not a comma, we have reached the end of
11564 the list. */
11565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11566 break;
11567
11568 /* Peek at the next token. */
11569 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11570 /* If the next token is a `}', then we're still done. An
11571 initializer-clause can have a trailing `,' after the
11572 initializer-list and before the closing `}'. */
11573 if (token->type == CPP_CLOSE_BRACE)
11574 break;
11575
11576 /* Consume the `,' token. */
11577 cp_lexer_consume_token (parser->lexer);
11578 }
11579
11580 /* The initializers were built up in reverse order, so we need to
11581 reverse them now. */
11582 return nreverse (initializers);
11583 }
11584
11585 /* Classes [gram.class] */
11586
11587 /* Parse a class-name.
11588
11589 class-name:
11590 identifier
11591 template-id
11592
11593 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11594 to indicate that names looked up in dependent types should be
11595 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11596 keyword has been used to indicate that the name that appears next
11597 is a template. TYPE_P is true iff the next name should be treated
11598 as class-name, even if it is declared to be some other kind of name
11599 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11600 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11601 being defined in a class-head.
11602
11603 Returns the TYPE_DECL representing the class. */
11604
11605 static tree
cp_parser_class_name(cp_parser * parser,bool typename_keyword_p,bool template_keyword_p,bool type_p,bool check_dependency_p,bool class_head_p,bool is_declaration)11606 cp_parser_class_name (cp_parser *parser,
11607 bool typename_keyword_p,
11608 bool template_keyword_p,
11609 bool type_p,
11610 bool check_dependency_p,
11611 bool class_head_p,
11612 bool is_declaration)
11613 {
11614 tree decl;
11615 tree scope;
11616 bool typename_p;
11617 cp_token *token;
11618
11619 /* All class-names start with an identifier. */
11620 token = cp_lexer_peek_token (parser->lexer);
11621 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11622 {
11623 cp_parser_error (parser, "expected class-name");
11624 return error_mark_node;
11625 }
11626
11627 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11628 to a template-id, so we save it here. */
11629 scope = parser->scope;
11630 if (scope == error_mark_node)
11631 return error_mark_node;
11632
11633 /* Any name names a type if we're following the `typename' keyword
11634 in a qualified name where the enclosing scope is type-dependent. */
11635 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11636 && dependent_type_p (scope));
11637 /* Handle the common case (an identifier, but not a template-id)
11638 efficiently. */
11639 if (token->type == CPP_NAME
11640 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11641 {
11642 tree identifier;
11643
11644 /* Look for the identifier. */
11645 identifier = cp_parser_identifier (parser);
11646 /* If the next token isn't an identifier, we are certainly not
11647 looking at a class-name. */
11648 if (identifier == error_mark_node)
11649 decl = error_mark_node;
11650 /* If we know this is a type-name, there's no need to look it
11651 up. */
11652 else if (typename_p)
11653 decl = identifier;
11654 else
11655 {
11656 /* If the next token is a `::', then the name must be a type
11657 name.
11658
11659 [basic.lookup.qual]
11660
11661 During the lookup for a name preceding the :: scope
11662 resolution operator, object, function, and enumerator
11663 names are ignored. */
11664 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11665 type_p = true;
11666 /* Look up the name. */
11667 decl = cp_parser_lookup_name (parser, identifier,
11668 type_p,
11669 /*is_template=*/false,
11670 /*is_namespace=*/false,
11671 check_dependency_p);
11672 }
11673 }
11674 else
11675 {
11676 /* Try a template-id. */
11677 decl = cp_parser_template_id (parser, template_keyword_p,
11678 check_dependency_p,
11679 is_declaration);
11680 if (decl == error_mark_node)
11681 return error_mark_node;
11682 }
11683
11684 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11685
11686 /* If this is a typename, create a TYPENAME_TYPE. */
11687 if (typename_p && decl != error_mark_node)
11688 {
11689 decl = make_typename_type (scope, decl, /*complain=*/1);
11690 if (decl != error_mark_node)
11691 decl = TYPE_NAME (decl);
11692 }
11693
11694 /* Check to see that it is really the name of a class. */
11695 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11696 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11697 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11698 /* Situations like this:
11699
11700 template <typename T> struct A {
11701 typename T::template X<int>::I i;
11702 };
11703
11704 are problematic. Is `T::template X<int>' a class-name? The
11705 standard does not seem to be definitive, but there is no other
11706 valid interpretation of the following `::'. Therefore, those
11707 names are considered class-names. */
11708 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11709 else if (decl == error_mark_node
11710 || TREE_CODE (decl) != TYPE_DECL
11711 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11712 {
11713 cp_parser_error (parser, "expected class-name");
11714 return error_mark_node;
11715 }
11716
11717 return decl;
11718 }
11719
11720 /* Parse a class-specifier.
11721
11722 class-specifier:
11723 class-head { member-specification [opt] }
11724
11725 Returns the TREE_TYPE representing the class. */
11726
11727 static tree
cp_parser_class_specifier(cp_parser * parser)11728 cp_parser_class_specifier (cp_parser* parser)
11729 {
11730 cp_token *token;
11731 tree type;
11732 tree attributes;
11733 int has_trailing_semicolon;
11734 bool nested_name_specifier_p;
11735 unsigned saved_num_template_parameter_lists;
11736 bool pop_p = false;
11737 tree scope = NULL_TREE;
11738
11739 push_deferring_access_checks (dk_no_deferred);
11740
11741 /* Parse the class-head. */
11742 type = cp_parser_class_head (parser,
11743 &nested_name_specifier_p,
11744 &attributes);
11745 /* If the class-head was a semantic disaster, skip the entire body
11746 of the class. */
11747 if (!type)
11748 {
11749 cp_parser_skip_to_end_of_block_or_statement (parser);
11750 pop_deferring_access_checks ();
11751 return error_mark_node;
11752 }
11753
11754 /* Look for the `{'. */
11755 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11756 {
11757 pop_deferring_access_checks ();
11758 return error_mark_node;
11759 }
11760
11761 /* Issue an error message if type-definitions are forbidden here. */
11762 cp_parser_check_type_definition (parser);
11763 /* Remember that we are defining one more class. */
11764 ++parser->num_classes_being_defined;
11765 /* Inside the class, surrounding template-parameter-lists do not
11766 apply. */
11767 saved_num_template_parameter_lists
11768 = parser->num_template_parameter_lists;
11769 parser->num_template_parameter_lists = 0;
11770
11771 /* Start the class. */
11772 if (nested_name_specifier_p)
11773 {
11774 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11775 pop_p = push_scope (scope);
11776 }
11777 type = begin_class_definition (type);
11778 if (type == error_mark_node)
11779 /* If the type is erroneous, skip the entire body of the class. */
11780 cp_parser_skip_to_closing_brace (parser);
11781 else
11782 /* Parse the member-specification. */
11783 cp_parser_member_specification_opt (parser);
11784 /* Look for the trailing `}'. */
11785 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11786 /* We get better error messages by noticing a common problem: a
11787 missing trailing `;'. */
11788 token = cp_lexer_peek_token (parser->lexer);
11789 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11790 /* Look for trailing attributes to apply to this class. */
11791 if (cp_parser_allow_gnu_extensions_p (parser))
11792 {
11793 tree sub_attr = cp_parser_attributes_opt (parser);
11794 attributes = chainon (attributes, sub_attr);
11795 }
11796 if (type != error_mark_node)
11797 type = finish_struct (type, attributes);
11798 if (pop_p)
11799 pop_scope (scope);
11800 /* If this class is not itself within the scope of another class,
11801 then we need to parse the bodies of all of the queued function
11802 definitions. Note that the queued functions defined in a class
11803 are not always processed immediately following the
11804 class-specifier for that class. Consider:
11805
11806 struct A {
11807 struct B { void f() { sizeof (A); } };
11808 };
11809
11810 If `f' were processed before the processing of `A' were
11811 completed, there would be no way to compute the size of `A'.
11812 Note that the nesting we are interested in here is lexical --
11813 not the semantic nesting given by TYPE_CONTEXT. In particular,
11814 for:
11815
11816 struct A { struct B; };
11817 struct A::B { void f() { } };
11818
11819 there is no need to delay the parsing of `A::B::f'. */
11820 if (--parser->num_classes_being_defined == 0)
11821 {
11822 tree queue_entry;
11823 tree fn;
11824
11825 /* In a first pass, parse default arguments to the functions.
11826 Then, in a second pass, parse the bodies of the functions.
11827 This two-phased approach handles cases like:
11828
11829 struct S {
11830 void f() { g(); }
11831 void g(int i = 3);
11832 };
11833
11834 */
11835 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11836 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11837 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11838 TREE_PURPOSE (parser->unparsed_functions_queues)
11839 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11840 {
11841 fn = TREE_VALUE (queue_entry);
11842 /* Make sure that any template parameters are in scope. */
11843 maybe_begin_member_template_processing (fn);
11844 /* If there are default arguments that have not yet been processed,
11845 take care of them now. */
11846 cp_parser_late_parsing_default_args (parser, fn);
11847 /* Remove any template parameters from the symbol table. */
11848 maybe_end_member_template_processing ();
11849 }
11850 /* Now parse the body of the functions. */
11851 for (TREE_VALUE (parser->unparsed_functions_queues)
11852 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11853 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11854 TREE_VALUE (parser->unparsed_functions_queues)
11855 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11856 {
11857 /* Figure out which function we need to process. */
11858 fn = TREE_VALUE (queue_entry);
11859
11860 /* A hack to prevent garbage collection. */
11861 function_depth++;
11862
11863 /* Parse the function. */
11864 cp_parser_late_parsing_for_member (parser, fn);
11865 function_depth--;
11866 }
11867
11868 }
11869
11870 /* Put back any saved access checks. */
11871 pop_deferring_access_checks ();
11872
11873 /* Restore the count of active template-parameter-lists. */
11874 parser->num_template_parameter_lists
11875 = saved_num_template_parameter_lists;
11876
11877 return type;
11878 }
11879
11880 /* Parse a class-head.
11881
11882 class-head:
11883 class-key identifier [opt] base-clause [opt]
11884 class-key nested-name-specifier identifier base-clause [opt]
11885 class-key nested-name-specifier [opt] template-id
11886 base-clause [opt]
11887
11888 GNU Extensions:
11889 class-key attributes identifier [opt] base-clause [opt]
11890 class-key attributes nested-name-specifier identifier base-clause [opt]
11891 class-key attributes nested-name-specifier [opt] template-id
11892 base-clause [opt]
11893
11894 Returns the TYPE of the indicated class. Sets
11895 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11896 involving a nested-name-specifier was used, and FALSE otherwise.
11897
11898 Returns NULL_TREE if the class-head is syntactically valid, but
11899 semantically invalid in a way that means we should skip the entire
11900 body of the class. */
11901
11902 static tree
cp_parser_class_head(cp_parser * parser,bool * nested_name_specifier_p,tree * attributes_p)11903 cp_parser_class_head (cp_parser* parser,
11904 bool* nested_name_specifier_p,
11905 tree *attributes_p)
11906 {
11907 cp_token *token;
11908 tree nested_name_specifier;
11909 enum tag_types class_key;
11910 tree id = NULL_TREE;
11911 tree type = NULL_TREE;
11912 tree attributes;
11913 bool template_id_p = false;
11914 bool qualified_p = false;
11915 bool invalid_nested_name_p = false;
11916 bool invalid_explicit_specialization_p = false;
11917 bool pop_p = false;
11918 unsigned num_templates;
11919
11920 /* Assume no nested-name-specifier will be present. */
11921 *nested_name_specifier_p = false;
11922 /* Assume no template parameter lists will be used in defining the
11923 type. */
11924 num_templates = 0;
11925
11926 /* Look for the class-key. */
11927 class_key = cp_parser_class_key (parser);
11928 if (class_key == none_type)
11929 return error_mark_node;
11930
11931 /* Parse the attributes. */
11932 attributes = cp_parser_attributes_opt (parser);
11933
11934 /* If the next token is `::', that is invalid -- but sometimes
11935 people do try to write:
11936
11937 struct ::S {};
11938
11939 Handle this gracefully by accepting the extra qualifier, and then
11940 issuing an error about it later if this really is a
11941 class-head. If it turns out just to be an elaborated type
11942 specifier, remain silent. */
11943 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11944 qualified_p = true;
11945
11946 push_deferring_access_checks (dk_no_check);
11947
11948 /* Determine the name of the class. Begin by looking for an
11949 optional nested-name-specifier. */
11950 nested_name_specifier
11951 = cp_parser_nested_name_specifier_opt (parser,
11952 /*typename_keyword_p=*/false,
11953 /*check_dependency_p=*/false,
11954 /*type_p=*/false,
11955 /*is_declaration=*/false);
11956 /* If there was a nested-name-specifier, then there *must* be an
11957 identifier. */
11958 if (nested_name_specifier)
11959 {
11960 /* Although the grammar says `identifier', it really means
11961 `class-name' or `template-name'. You are only allowed to
11962 define a class that has already been declared with this
11963 syntax.
11964
11965 The proposed resolution for Core Issue 180 says that whever
11966 you see `class T::X' you should treat `X' as a type-name.
11967
11968 It is OK to define an inaccessible class; for example:
11969
11970 class A { class B; };
11971 class A::B {};
11972
11973 We do not know if we will see a class-name, or a
11974 template-name. We look for a class-name first, in case the
11975 class-name is a template-id; if we looked for the
11976 template-name first we would stop after the template-name. */
11977 cp_parser_parse_tentatively (parser);
11978 type = cp_parser_class_name (parser,
11979 /*typename_keyword_p=*/false,
11980 /*template_keyword_p=*/false,
11981 /*type_p=*/true,
11982 /*check_dependency_p=*/false,
11983 /*class_head_p=*/true,
11984 /*is_declaration=*/false);
11985 /* If that didn't work, ignore the nested-name-specifier. */
11986 if (!cp_parser_parse_definitely (parser))
11987 {
11988 invalid_nested_name_p = true;
11989 id = cp_parser_identifier (parser);
11990 if (id == error_mark_node)
11991 id = NULL_TREE;
11992 }
11993 /* If we could not find a corresponding TYPE, treat this
11994 declaration like an unqualified declaration. */
11995 if (type == error_mark_node)
11996 nested_name_specifier = NULL_TREE;
11997 /* Otherwise, count the number of templates used in TYPE and its
11998 containing scopes. */
11999 else
12000 {
12001 tree scope;
12002
12003 for (scope = TREE_TYPE (type);
12004 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12005 scope = (TYPE_P (scope)
12006 ? TYPE_CONTEXT (scope)
12007 : DECL_CONTEXT (scope)))
12008 if (TYPE_P (scope)
12009 && CLASS_TYPE_P (scope)
12010 && CLASSTYPE_TEMPLATE_INFO (scope)
12011 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12012 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12013 ++num_templates;
12014 }
12015 }
12016 /* Otherwise, the identifier is optional. */
12017 else
12018 {
12019 /* We don't know whether what comes next is a template-id,
12020 an identifier, or nothing at all. */
12021 cp_parser_parse_tentatively (parser);
12022 /* Check for a template-id. */
12023 id = cp_parser_template_id (parser,
12024 /*template_keyword_p=*/false,
12025 /*check_dependency_p=*/true,
12026 /*is_declaration=*/true);
12027 /* If that didn't work, it could still be an identifier. */
12028 if (!cp_parser_parse_definitely (parser))
12029 {
12030 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12031 id = cp_parser_identifier (parser);
12032 else
12033 id = NULL_TREE;
12034 }
12035 else
12036 {
12037 template_id_p = true;
12038 ++num_templates;
12039 }
12040 }
12041
12042 pop_deferring_access_checks ();
12043
12044 if (id)
12045 cp_parser_check_for_invalid_template_id (parser, id);
12046
12047 /* If it's not a `:' or a `{' then we can't really be looking at a
12048 class-head, since a class-head only appears as part of a
12049 class-specifier. We have to detect this situation before calling
12050 xref_tag, since that has irreversible side-effects. */
12051 if (!cp_parser_next_token_starts_class_definition_p (parser))
12052 {
12053 cp_parser_error (parser, "expected `{' or `:'");
12054 return error_mark_node;
12055 }
12056
12057 /* At this point, we're going ahead with the class-specifier, even
12058 if some other problem occurs. */
12059 cp_parser_commit_to_tentative_parse (parser);
12060 /* Issue the error about the overly-qualified name now. */
12061 if (qualified_p)
12062 cp_parser_error (parser,
12063 "global qualification of class name is invalid");
12064 else if (invalid_nested_name_p)
12065 cp_parser_error (parser,
12066 "qualified name does not name a class");
12067 else if (nested_name_specifier)
12068 {
12069 tree scope;
12070 /* Figure out in what scope the declaration is being placed. */
12071 scope = current_scope ();
12072 if (!scope)
12073 scope = current_namespace;
12074 /* If that scope does not contain the scope in which the
12075 class was originally declared, the program is invalid. */
12076 if (scope && !is_ancestor (scope, nested_name_specifier))
12077 {
12078 error ("declaration of `%D' in `%D' which does not "
12079 "enclose `%D'", type, scope, nested_name_specifier);
12080 type = NULL_TREE;
12081 goto done;
12082 }
12083 /* [dcl.meaning]
12084
12085 A declarator-id shall not be qualified exception of the
12086 definition of a ... nested class outside of its class
12087 ... [or] a the definition or explicit instantiation of a
12088 class member of a namespace outside of its namespace. */
12089 if (scope == nested_name_specifier)
12090 {
12091 pedwarn ("extra qualification ignored");
12092 nested_name_specifier = NULL_TREE;
12093 num_templates = 0;
12094 }
12095 }
12096 /* An explicit-specialization must be preceded by "template <>". If
12097 it is not, try to recover gracefully. */
12098 if (at_namespace_scope_p ()
12099 && parser->num_template_parameter_lists == 0
12100 && template_id_p)
12101 {
12102 error ("an explicit specialization must be preceded by 'template <>'");
12103 invalid_explicit_specialization_p = true;
12104 /* Take the same action that would have been taken by
12105 cp_parser_explicit_specialization. */
12106 ++parser->num_template_parameter_lists;
12107 begin_specialization ();
12108 }
12109 /* There must be no "return" statements between this point and the
12110 end of this function; set "type "to the correct return value and
12111 use "goto done;" to return. */
12112 /* Make sure that the right number of template parameters were
12113 present. */
12114 if (!cp_parser_check_template_parameters (parser, num_templates))
12115 {
12116 /* If something went wrong, there is no point in even trying to
12117 process the class-definition. */
12118 type = NULL_TREE;
12119 goto done;
12120 }
12121
12122 /* Look up the type. */
12123 if (template_id_p)
12124 {
12125 type = TREE_TYPE (id);
12126 maybe_process_partial_specialization (type);
12127 }
12128 else if (!nested_name_specifier)
12129 {
12130 /* If the class was unnamed, create a dummy name. */
12131 if (!id)
12132 id = make_anon_name ();
12133 type = xref_tag (class_key, id, /*globalize=*/false,
12134 parser->num_template_parameter_lists);
12135 }
12136 else
12137 {
12138 tree class_type;
12139 bool pop_p = false;
12140
12141 /* Given:
12142
12143 template <typename T> struct S { struct T };
12144 template <typename T> struct S<T>::T { };
12145
12146 we will get a TYPENAME_TYPE when processing the definition of
12147 `S::T'. We need to resolve it to the actual type before we
12148 try to define it. */
12149 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12150 {
12151 class_type = resolve_typename_type (TREE_TYPE (type),
12152 /*only_current_p=*/false);
12153 if (class_type != error_mark_node)
12154 type = TYPE_NAME (class_type);
12155 else
12156 {
12157 cp_parser_error (parser, "could not resolve typename type");
12158 type = error_mark_node;
12159 }
12160 }
12161
12162 maybe_process_partial_specialization (TREE_TYPE (type));
12163 class_type = current_class_type;
12164 /* Enter the scope indicated by the nested-name-specifier. */
12165 if (nested_name_specifier)
12166 pop_p = push_scope (nested_name_specifier);
12167 /* Get the canonical version of this type. */
12168 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12169 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12171 type = push_template_decl (type);
12172 type = TREE_TYPE (type);
12173 if (nested_name_specifier)
12174 {
12175 *nested_name_specifier_p = true;
12176 if (pop_p)
12177 pop_scope (nested_name_specifier);
12178 }
12179 }
12180 /* Indicate whether this class was declared as a `class' or as a
12181 `struct'. */
12182 if (TREE_CODE (type) == RECORD_TYPE)
12183 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12184 cp_parser_check_class_key (class_key, type);
12185
12186 /* Enter the scope containing the class; the names of base classes
12187 should be looked up in that context. For example, given:
12188
12189 struct A { struct B {}; struct C; };
12190 struct A::C : B {};
12191
12192 is valid. */
12193 if (nested_name_specifier)
12194 pop_p = push_scope (nested_name_specifier);
12195 /* Now, look for the base-clause. */
12196 token = cp_lexer_peek_token (parser->lexer);
12197 if (token->type == CPP_COLON)
12198 {
12199 tree bases;
12200
12201 /* Get the list of base-classes. */
12202 bases = cp_parser_base_clause (parser);
12203 /* Process them. */
12204 xref_basetypes (type, bases);
12205 }
12206 /* Leave the scope given by the nested-name-specifier. We will
12207 enter the class scope itself while processing the members. */
12208 if (pop_p)
12209 pop_scope (nested_name_specifier);
12210
12211 done:
12212 if (invalid_explicit_specialization_p)
12213 {
12214 end_specialization ();
12215 --parser->num_template_parameter_lists;
12216 }
12217 *attributes_p = attributes;
12218 return type;
12219 }
12220
12221 /* Parse a class-key.
12222
12223 class-key:
12224 class
12225 struct
12226 union
12227
12228 Returns the kind of class-key specified, or none_type to indicate
12229 error. */
12230
12231 static enum tag_types
cp_parser_class_key(cp_parser * parser)12232 cp_parser_class_key (cp_parser* parser)
12233 {
12234 cp_token *token;
12235 enum tag_types tag_type;
12236
12237 /* Look for the class-key. */
12238 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12239 if (!token)
12240 return none_type;
12241
12242 /* Check to see if the TOKEN is a class-key. */
12243 tag_type = cp_parser_token_is_class_key (token);
12244 if (!tag_type)
12245 cp_parser_error (parser, "expected class-key");
12246 return tag_type;
12247 }
12248
12249 /* Parse an (optional) member-specification.
12250
12251 member-specification:
12252 member-declaration member-specification [opt]
12253 access-specifier : member-specification [opt] */
12254
12255 static void
cp_parser_member_specification_opt(cp_parser * parser)12256 cp_parser_member_specification_opt (cp_parser* parser)
12257 {
12258 while (true)
12259 {
12260 cp_token *token;
12261 enum rid keyword;
12262
12263 /* Peek at the next token. */
12264 token = cp_lexer_peek_token (parser->lexer);
12265 /* If it's a `}', or EOF then we've seen all the members. */
12266 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12267 break;
12268
12269 /* See if this token is a keyword. */
12270 keyword = token->keyword;
12271 switch (keyword)
12272 {
12273 case RID_PUBLIC:
12274 case RID_PROTECTED:
12275 case RID_PRIVATE:
12276 /* Consume the access-specifier. */
12277 cp_lexer_consume_token (parser->lexer);
12278 /* Remember which access-specifier is active. */
12279 current_access_specifier = token->value;
12280 /* Look for the `:'. */
12281 cp_parser_require (parser, CPP_COLON, "`:'");
12282 break;
12283
12284 default:
12285 /* Otherwise, the next construction must be a
12286 member-declaration. */
12287 cp_parser_member_declaration (parser);
12288 }
12289 }
12290 }
12291
12292 /* Parse a member-declaration.
12293
12294 member-declaration:
12295 decl-specifier-seq [opt] member-declarator-list [opt] ;
12296 function-definition ; [opt]
12297 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12298 using-declaration
12299 template-declaration
12300
12301 member-declarator-list:
12302 member-declarator
12303 member-declarator-list , member-declarator
12304
12305 member-declarator:
12306 declarator pure-specifier [opt]
12307 declarator constant-initializer [opt]
12308 identifier [opt] : constant-expression
12309
12310 GNU Extensions:
12311
12312 member-declaration:
12313 __extension__ member-declaration
12314
12315 member-declarator:
12316 declarator attributes [opt] pure-specifier [opt]
12317 declarator attributes [opt] constant-initializer [opt]
12318 identifier [opt] attributes [opt] : constant-expression */
12319
12320 static void
cp_parser_member_declaration(cp_parser * parser)12321 cp_parser_member_declaration (cp_parser* parser)
12322 {
12323 tree decl_specifiers;
12324 tree prefix_attributes;
12325 tree decl;
12326 int declares_class_or_enum;
12327 bool friend_p;
12328 cp_token *token;
12329 int saved_pedantic;
12330
12331 /* Check for the `__extension__' keyword. */
12332 if (cp_parser_extension_opt (parser, &saved_pedantic))
12333 {
12334 /* Recurse. */
12335 cp_parser_member_declaration (parser);
12336 /* Restore the old value of the PEDANTIC flag. */
12337 pedantic = saved_pedantic;
12338
12339 return;
12340 }
12341
12342 /* Check for a template-declaration. */
12343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12344 {
12345 /* Parse the template-declaration. */
12346 cp_parser_template_declaration (parser, /*member_p=*/true);
12347
12348 return;
12349 }
12350
12351 /* Check for a using-declaration. */
12352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12353 {
12354 /* Parse the using-declaration. */
12355 cp_parser_using_declaration (parser);
12356
12357 return;
12358 }
12359
12360 /* Parse the decl-specifier-seq. */
12361 decl_specifiers
12362 = cp_parser_decl_specifier_seq (parser,
12363 CP_PARSER_FLAGS_OPTIONAL,
12364 &prefix_attributes,
12365 &declares_class_or_enum);
12366 /* Check for an invalid type-name. */
12367 if (cp_parser_diagnose_invalid_type_name (parser))
12368 return;
12369 /* If there is no declarator, then the decl-specifier-seq should
12370 specify a type. */
12371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12372 {
12373 /* If there was no decl-specifier-seq, and the next token is a
12374 `;', then we have something like:
12375
12376 struct S { ; };
12377
12378 [class.mem]
12379
12380 Each member-declaration shall declare at least one member
12381 name of the class. */
12382 if (!decl_specifiers)
12383 {
12384 if (pedantic)
12385 pedwarn ("extra semicolon");
12386 }
12387 else
12388 {
12389 tree type;
12390
12391 /* See if this declaration is a friend. */
12392 friend_p = cp_parser_friend_p (decl_specifiers);
12393 /* If there were decl-specifiers, check to see if there was
12394 a class-declaration. */
12395 type = check_tag_decl (decl_specifiers);
12396 /* Nested classes have already been added to the class, but
12397 a `friend' needs to be explicitly registered. */
12398 if (friend_p)
12399 {
12400 /* If the `friend' keyword was present, the friend must
12401 be introduced with a class-key. */
12402 if (!declares_class_or_enum)
12403 error ("a class-key must be used when declaring a friend");
12404 /* In this case:
12405
12406 template <typename T> struct A {
12407 friend struct A<T>::B;
12408 };
12409
12410 A<T>::B will be represented by a TYPENAME_TYPE, and
12411 therefore not recognized by check_tag_decl. */
12412 if (!type)
12413 {
12414 tree specifier;
12415
12416 for (specifier = decl_specifiers;
12417 specifier;
12418 specifier = TREE_CHAIN (specifier))
12419 {
12420 tree s = TREE_VALUE (specifier);
12421
12422 if (TREE_CODE (s) == IDENTIFIER_NODE)
12423 get_global_value_if_present (s, &type);
12424 if (TREE_CODE (s) == TYPE_DECL)
12425 s = TREE_TYPE (s);
12426 if (TYPE_P (s))
12427 {
12428 type = s;
12429 break;
12430 }
12431 }
12432 }
12433 if (!type || !TYPE_P (type))
12434 error ("friend declaration does not name a class or "
12435 "function");
12436 else
12437 make_friend_class (current_class_type, type,
12438 /*complain=*/true);
12439 }
12440 /* If there is no TYPE, an error message will already have
12441 been issued. */
12442 else if (!type)
12443 ;
12444 /* An anonymous aggregate has to be handled specially; such
12445 a declaration really declares a data member (with a
12446 particular type), as opposed to a nested class. */
12447 else if (ANON_AGGR_TYPE_P (type))
12448 {
12449 /* Remove constructors and such from TYPE, now that we
12450 know it is an anonymous aggregate. */
12451 fixup_anonymous_aggr (type);
12452 /* And make the corresponding data member. */
12453 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12454 /* Add it to the class. */
12455 finish_member_declaration (decl);
12456 }
12457 else
12458 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12459 }
12460 }
12461 else
12462 {
12463 /* See if these declarations will be friends. */
12464 friend_p = cp_parser_friend_p (decl_specifiers);
12465
12466 /* Keep going until we hit the `;' at the end of the
12467 declaration. */
12468 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12469 {
12470 tree attributes = NULL_TREE;
12471 tree first_attribute;
12472
12473 /* Peek at the next token. */
12474 token = cp_lexer_peek_token (parser->lexer);
12475
12476 /* Check for a bitfield declaration. */
12477 if (token->type == CPP_COLON
12478 || (token->type == CPP_NAME
12479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12480 == CPP_COLON))
12481 {
12482 tree identifier;
12483 tree width;
12484
12485 /* Get the name of the bitfield. Note that we cannot just
12486 check TOKEN here because it may have been invalidated by
12487 the call to cp_lexer_peek_nth_token above. */
12488 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12489 identifier = cp_parser_identifier (parser);
12490 else
12491 identifier = NULL_TREE;
12492
12493 /* Consume the `:' token. */
12494 cp_lexer_consume_token (parser->lexer);
12495 /* Get the width of the bitfield. */
12496 width
12497 = cp_parser_constant_expression (parser,
12498 /*allow_non_constant=*/false,
12499 NULL);
12500
12501 /* Look for attributes that apply to the bitfield. */
12502 attributes = cp_parser_attributes_opt (parser);
12503 /* Remember which attributes are prefix attributes and
12504 which are not. */
12505 first_attribute = attributes;
12506 /* Combine the attributes. */
12507 attributes = chainon (prefix_attributes, attributes);
12508
12509 /* Create the bitfield declaration. */
12510 decl = grokbitfield (identifier,
12511 decl_specifiers,
12512 width);
12513 /* Apply the attributes. */
12514 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12515 }
12516 else
12517 {
12518 tree declarator;
12519 tree initializer;
12520 tree asm_specification;
12521 int ctor_dtor_or_conv_p;
12522
12523 /* Parse the declarator. */
12524 declarator
12525 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12526 &ctor_dtor_or_conv_p,
12527 /*parenthesized_p=*/NULL);
12528
12529 /* If something went wrong parsing the declarator, make sure
12530 that we at least consume some tokens. */
12531 if (declarator == error_mark_node)
12532 {
12533 /* Skip to the end of the statement. */
12534 cp_parser_skip_to_end_of_statement (parser);
12535 /* If the next token is not a semicolon, that is
12536 probably because we just skipped over the body of
12537 a function. So, we consume a semicolon if
12538 present, but do not issue an error message if it
12539 is not present. */
12540 if (cp_lexer_next_token_is (parser->lexer,
12541 CPP_SEMICOLON))
12542 cp_lexer_consume_token (parser->lexer);
12543 return;
12544 }
12545
12546 cp_parser_check_for_definition_in_return_type
12547 (declarator, declares_class_or_enum);
12548
12549 /* Look for an asm-specification. */
12550 asm_specification = cp_parser_asm_specification_opt (parser);
12551 /* Look for attributes that apply to the declaration. */
12552 attributes = cp_parser_attributes_opt (parser);
12553 /* Remember which attributes are prefix attributes and
12554 which are not. */
12555 first_attribute = attributes;
12556 /* Combine the attributes. */
12557 attributes = chainon (prefix_attributes, attributes);
12558
12559 /* If it's an `=', then we have a constant-initializer or a
12560 pure-specifier. It is not correct to parse the
12561 initializer before registering the member declaration
12562 since the member declaration should be in scope while
12563 its initializer is processed. However, the rest of the
12564 front end does not yet provide an interface that allows
12565 us to handle this correctly. */
12566 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12567 {
12568 /* In [class.mem]:
12569
12570 A pure-specifier shall be used only in the declaration of
12571 a virtual function.
12572
12573 A member-declarator can contain a constant-initializer
12574 only if it declares a static member of integral or
12575 enumeration type.
12576
12577 Therefore, if the DECLARATOR is for a function, we look
12578 for a pure-specifier; otherwise, we look for a
12579 constant-initializer. When we call `grokfield', it will
12580 perform more stringent semantics checks. */
12581 if (TREE_CODE (declarator) == CALL_EXPR)
12582 initializer = cp_parser_pure_specifier (parser);
12583 else
12584 /* Parse the initializer. */
12585 initializer = cp_parser_constant_initializer (parser);
12586 }
12587 /* Otherwise, there is no initializer. */
12588 else
12589 initializer = NULL_TREE;
12590
12591 /* See if we are probably looking at a function
12592 definition. We are certainly not looking at at a
12593 member-declarator. Calling `grokfield' has
12594 side-effects, so we must not do it unless we are sure
12595 that we are looking at a member-declarator. */
12596 if (cp_parser_token_starts_function_definition_p
12597 (cp_lexer_peek_token (parser->lexer)))
12598 {
12599 /* The grammar does not allow a pure-specifier to be
12600 used when a member function is defined. (It is
12601 possible that this fact is an oversight in the
12602 standard, since a pure function may be defined
12603 outside of the class-specifier. */
12604 if (initializer)
12605 error ("pure-specifier on function-definition");
12606 decl = cp_parser_save_member_function_body (parser,
12607 decl_specifiers,
12608 declarator,
12609 attributes);
12610 /* If the member was not a friend, declare it here. */
12611 if (!friend_p)
12612 finish_member_declaration (decl);
12613 /* Peek at the next token. */
12614 token = cp_lexer_peek_token (parser->lexer);
12615 /* If the next token is a semicolon, consume it. */
12616 if (token->type == CPP_SEMICOLON)
12617 cp_lexer_consume_token (parser->lexer);
12618 return;
12619 }
12620 else
12621 {
12622 /* Create the declaration. */
12623 decl = grokfield (declarator, decl_specifiers,
12624 initializer, asm_specification,
12625 attributes);
12626 /* Any initialization must have been from a
12627 constant-expression. */
12628 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12629 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12630 }
12631 }
12632
12633 /* Reset PREFIX_ATTRIBUTES. */
12634 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12635 attributes = TREE_CHAIN (attributes);
12636 if (attributes)
12637 TREE_CHAIN (attributes) = NULL_TREE;
12638
12639 /* If there is any qualification still in effect, clear it
12640 now; we will be starting fresh with the next declarator. */
12641 parser->scope = NULL_TREE;
12642 parser->qualifying_scope = NULL_TREE;
12643 parser->object_scope = NULL_TREE;
12644 /* If it's a `,', then there are more declarators. */
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12646 cp_lexer_consume_token (parser->lexer);
12647 /* If the next token isn't a `;', then we have a parse error. */
12648 else if (cp_lexer_next_token_is_not (parser->lexer,
12649 CPP_SEMICOLON))
12650 {
12651 cp_parser_error (parser, "expected `;'");
12652 /* Skip tokens until we find a `;'. */
12653 cp_parser_skip_to_end_of_statement (parser);
12654
12655 break;
12656 }
12657
12658 if (decl)
12659 {
12660 /* Add DECL to the list of members. */
12661 if (!friend_p)
12662 finish_member_declaration (decl);
12663
12664 if (TREE_CODE (decl) == FUNCTION_DECL)
12665 cp_parser_save_default_args (parser, decl);
12666 }
12667 }
12668 }
12669
12670 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12671 }
12672
12673 /* Parse a pure-specifier.
12674
12675 pure-specifier:
12676 = 0
12677
12678 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12679 Otherwise, ERROR_MARK_NODE is returned. */
12680
12681 static tree
cp_parser_pure_specifier(cp_parser * parser)12682 cp_parser_pure_specifier (cp_parser* parser)
12683 {
12684 cp_token *token;
12685
12686 /* Look for the `=' token. */
12687 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12688 return error_mark_node;
12689 /* Look for the `0' token. */
12690 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12691 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12692 to get information from the lexer about how the number was
12693 spelled in order to fix this problem. */
12694 if (!token || !integer_zerop (token->value))
12695 return error_mark_node;
12696
12697 return integer_zero_node;
12698 }
12699
12700 /* Parse a constant-initializer.
12701
12702 constant-initializer:
12703 = constant-expression
12704
12705 Returns a representation of the constant-expression. */
12706
12707 static tree
cp_parser_constant_initializer(cp_parser * parser)12708 cp_parser_constant_initializer (cp_parser* parser)
12709 {
12710 /* Look for the `=' token. */
12711 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12712 return error_mark_node;
12713
12714 /* It is invalid to write:
12715
12716 struct S { static const int i = { 7 }; };
12717
12718 */
12719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12720 {
12721 cp_parser_error (parser,
12722 "a brace-enclosed initializer is not allowed here");
12723 /* Consume the opening brace. */
12724 cp_lexer_consume_token (parser->lexer);
12725 /* Skip the initializer. */
12726 cp_parser_skip_to_closing_brace (parser);
12727 /* Look for the trailing `}'. */
12728 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12729
12730 return error_mark_node;
12731 }
12732
12733 return cp_parser_constant_expression (parser,
12734 /*allow_non_constant=*/false,
12735 NULL);
12736 }
12737
12738 /* Derived classes [gram.class.derived] */
12739
12740 /* Parse a base-clause.
12741
12742 base-clause:
12743 : base-specifier-list
12744
12745 base-specifier-list:
12746 base-specifier
12747 base-specifier-list , base-specifier
12748
12749 Returns a TREE_LIST representing the base-classes, in the order in
12750 which they were declared. The representation of each node is as
12751 described by cp_parser_base_specifier.
12752
12753 In the case that no bases are specified, this function will return
12754 NULL_TREE, not ERROR_MARK_NODE. */
12755
12756 static tree
cp_parser_base_clause(cp_parser * parser)12757 cp_parser_base_clause (cp_parser* parser)
12758 {
12759 tree bases = NULL_TREE;
12760
12761 /* Look for the `:' that begins the list. */
12762 cp_parser_require (parser, CPP_COLON, "`:'");
12763
12764 /* Scan the base-specifier-list. */
12765 while (true)
12766 {
12767 cp_token *token;
12768 tree base;
12769
12770 /* Look for the base-specifier. */
12771 base = cp_parser_base_specifier (parser);
12772 /* Add BASE to the front of the list. */
12773 if (base != error_mark_node)
12774 {
12775 TREE_CHAIN (base) = bases;
12776 bases = base;
12777 }
12778 /* Peek at the next token. */
12779 token = cp_lexer_peek_token (parser->lexer);
12780 /* If it's not a comma, then the list is complete. */
12781 if (token->type != CPP_COMMA)
12782 break;
12783 /* Consume the `,'. */
12784 cp_lexer_consume_token (parser->lexer);
12785 }
12786
12787 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12788 base class had a qualified name. However, the next name that
12789 appears is certainly not qualified. */
12790 parser->scope = NULL_TREE;
12791 parser->qualifying_scope = NULL_TREE;
12792 parser->object_scope = NULL_TREE;
12793
12794 return nreverse (bases);
12795 }
12796
12797 /* Parse a base-specifier.
12798
12799 base-specifier:
12800 :: [opt] nested-name-specifier [opt] class-name
12801 virtual access-specifier [opt] :: [opt] nested-name-specifier
12802 [opt] class-name
12803 access-specifier virtual [opt] :: [opt] nested-name-specifier
12804 [opt] class-name
12805
12806 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12807 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12808 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12809 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12810
12811 static tree
cp_parser_base_specifier(cp_parser * parser)12812 cp_parser_base_specifier (cp_parser* parser)
12813 {
12814 cp_token *token;
12815 bool done = false;
12816 bool virtual_p = false;
12817 bool duplicate_virtual_error_issued_p = false;
12818 bool duplicate_access_error_issued_p = false;
12819 bool class_scope_p, template_p;
12820 tree access = access_default_node;
12821 tree type;
12822
12823 /* Process the optional `virtual' and `access-specifier'. */
12824 while (!done)
12825 {
12826 /* Peek at the next token. */
12827 token = cp_lexer_peek_token (parser->lexer);
12828 /* Process `virtual'. */
12829 switch (token->keyword)
12830 {
12831 case RID_VIRTUAL:
12832 /* If `virtual' appears more than once, issue an error. */
12833 if (virtual_p && !duplicate_virtual_error_issued_p)
12834 {
12835 cp_parser_error (parser,
12836 "`virtual' specified more than once in base-specified");
12837 duplicate_virtual_error_issued_p = true;
12838 }
12839
12840 virtual_p = true;
12841
12842 /* Consume the `virtual' token. */
12843 cp_lexer_consume_token (parser->lexer);
12844
12845 break;
12846
12847 case RID_PUBLIC:
12848 case RID_PROTECTED:
12849 case RID_PRIVATE:
12850 /* If more than one access specifier appears, issue an
12851 error. */
12852 if (access != access_default_node
12853 && !duplicate_access_error_issued_p)
12854 {
12855 cp_parser_error (parser,
12856 "more than one access specifier in base-specified");
12857 duplicate_access_error_issued_p = true;
12858 }
12859
12860 access = ridpointers[(int) token->keyword];
12861
12862 /* Consume the access-specifier. */
12863 cp_lexer_consume_token (parser->lexer);
12864
12865 break;
12866
12867 default:
12868 done = true;
12869 break;
12870 }
12871 }
12872 /* It is not uncommon to see programs mechanically, errouneously, use
12873 the 'typename' keyword to denote (dependent) qualified types
12874 as base classes. */
12875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12876 {
12877 if (!processing_template_decl)
12878 error ("keyword `typename' not allowed outside of templates");
12879 else
12880 error ("keyword `typename' not allowed in this context "
12881 "(the base class is implicitly a type)");
12882 cp_lexer_consume_token (parser->lexer);
12883 }
12884
12885 /* Look for the optional `::' operator. */
12886 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12887 /* Look for the nested-name-specifier. The simplest way to
12888 implement:
12889
12890 [temp.res]
12891
12892 The keyword `typename' is not permitted in a base-specifier or
12893 mem-initializer; in these contexts a qualified name that
12894 depends on a template-parameter is implicitly assumed to be a
12895 type name.
12896
12897 is to pretend that we have seen the `typename' keyword at this
12898 point. */
12899 cp_parser_nested_name_specifier_opt (parser,
12900 /*typename_keyword_p=*/true,
12901 /*check_dependency_p=*/true,
12902 /*type_p=*/true,
12903 /*is_declaration=*/true);
12904 /* If the base class is given by a qualified name, assume that names
12905 we see are type names or templates, as appropriate. */
12906 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12907 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12908
12909 /* Finally, look for the class-name. */
12910 type = cp_parser_class_name (parser,
12911 class_scope_p,
12912 template_p,
12913 /*type_p=*/true,
12914 /*check_dependency_p=*/true,
12915 /*class_head_p=*/false,
12916 /*is_declaration=*/true);
12917
12918 if (type == error_mark_node)
12919 return error_mark_node;
12920
12921 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12922 }
12923
12924 /* Exception handling [gram.exception] */
12925
12926 /* Parse an (optional) exception-specification.
12927
12928 exception-specification:
12929 throw ( type-id-list [opt] )
12930
12931 Returns a TREE_LIST representing the exception-specification. The
12932 TREE_VALUE of each node is a type. */
12933
12934 static tree
cp_parser_exception_specification_opt(cp_parser * parser)12935 cp_parser_exception_specification_opt (cp_parser* parser)
12936 {
12937 cp_token *token;
12938 tree type_id_list;
12939
12940 /* Peek at the next token. */
12941 token = cp_lexer_peek_token (parser->lexer);
12942 /* If it's not `throw', then there's no exception-specification. */
12943 if (!cp_parser_is_keyword (token, RID_THROW))
12944 return NULL_TREE;
12945
12946 /* Consume the `throw'. */
12947 cp_lexer_consume_token (parser->lexer);
12948
12949 /* Look for the `('. */
12950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12951
12952 /* Peek at the next token. */
12953 token = cp_lexer_peek_token (parser->lexer);
12954 /* If it's not a `)', then there is a type-id-list. */
12955 if (token->type != CPP_CLOSE_PAREN)
12956 {
12957 const char *saved_message;
12958
12959 /* Types may not be defined in an exception-specification. */
12960 saved_message = parser->type_definition_forbidden_message;
12961 parser->type_definition_forbidden_message
12962 = "types may not be defined in an exception-specification";
12963 /* Parse the type-id-list. */
12964 type_id_list = cp_parser_type_id_list (parser);
12965 /* Restore the saved message. */
12966 parser->type_definition_forbidden_message = saved_message;
12967 }
12968 else
12969 type_id_list = empty_except_spec;
12970
12971 /* Look for the `)'. */
12972 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12973
12974 return type_id_list;
12975 }
12976
12977 /* Parse an (optional) type-id-list.
12978
12979 type-id-list:
12980 type-id
12981 type-id-list , type-id
12982
12983 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12984 in the order that the types were presented. */
12985
12986 static tree
cp_parser_type_id_list(cp_parser * parser)12987 cp_parser_type_id_list (cp_parser* parser)
12988 {
12989 tree types = NULL_TREE;
12990
12991 while (true)
12992 {
12993 cp_token *token;
12994 tree type;
12995
12996 /* Get the next type-id. */
12997 type = cp_parser_type_id (parser);
12998 /* Add it to the list. */
12999 types = add_exception_specifier (types, type, /*complain=*/1);
13000 /* Peek at the next token. */
13001 token = cp_lexer_peek_token (parser->lexer);
13002 /* If it is not a `,', we are done. */
13003 if (token->type != CPP_COMMA)
13004 break;
13005 /* Consume the `,'. */
13006 cp_lexer_consume_token (parser->lexer);
13007 }
13008
13009 return nreverse (types);
13010 }
13011
13012 /* Parse a try-block.
13013
13014 try-block:
13015 try compound-statement handler-seq */
13016
13017 static tree
cp_parser_try_block(cp_parser * parser)13018 cp_parser_try_block (cp_parser* parser)
13019 {
13020 tree try_block;
13021
13022 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13023 try_block = begin_try_block ();
13024 cp_parser_compound_statement (parser, false);
13025 finish_try_block (try_block);
13026 cp_parser_handler_seq (parser);
13027 finish_handler_sequence (try_block);
13028
13029 return try_block;
13030 }
13031
13032 /* Parse a function-try-block.
13033
13034 function-try-block:
13035 try ctor-initializer [opt] function-body handler-seq */
13036
13037 static bool
cp_parser_function_try_block(cp_parser * parser)13038 cp_parser_function_try_block (cp_parser* parser)
13039 {
13040 tree try_block;
13041 bool ctor_initializer_p;
13042
13043 /* Look for the `try' keyword. */
13044 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13045 return false;
13046 /* Let the rest of the front-end know where we are. */
13047 try_block = begin_function_try_block ();
13048 /* Parse the function-body. */
13049 ctor_initializer_p
13050 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13051 /* We're done with the `try' part. */
13052 finish_function_try_block (try_block);
13053 /* Parse the handlers. */
13054 cp_parser_handler_seq (parser);
13055 /* We're done with the handlers. */
13056 finish_function_handler_sequence (try_block);
13057
13058 return ctor_initializer_p;
13059 }
13060
13061 /* Parse a handler-seq.
13062
13063 handler-seq:
13064 handler handler-seq [opt] */
13065
13066 static void
cp_parser_handler_seq(cp_parser * parser)13067 cp_parser_handler_seq (cp_parser* parser)
13068 {
13069 while (true)
13070 {
13071 cp_token *token;
13072
13073 /* Parse the handler. */
13074 cp_parser_handler (parser);
13075 /* Peek at the next token. */
13076 token = cp_lexer_peek_token (parser->lexer);
13077 /* If it's not `catch' then there are no more handlers. */
13078 if (!cp_parser_is_keyword (token, RID_CATCH))
13079 break;
13080 }
13081 }
13082
13083 /* Parse a handler.
13084
13085 handler:
13086 catch ( exception-declaration ) compound-statement */
13087
13088 static void
cp_parser_handler(cp_parser * parser)13089 cp_parser_handler (cp_parser* parser)
13090 {
13091 tree handler;
13092 tree declaration;
13093
13094 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13095 handler = begin_handler ();
13096 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13097 declaration = cp_parser_exception_declaration (parser);
13098 finish_handler_parms (declaration, handler);
13099 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13100 cp_parser_compound_statement (parser, false);
13101 finish_handler (handler);
13102 }
13103
13104 /* Parse an exception-declaration.
13105
13106 exception-declaration:
13107 type-specifier-seq declarator
13108 type-specifier-seq abstract-declarator
13109 type-specifier-seq
13110 ...
13111
13112 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13113 ellipsis variant is used. */
13114
13115 static tree
cp_parser_exception_declaration(cp_parser * parser)13116 cp_parser_exception_declaration (cp_parser* parser)
13117 {
13118 tree type_specifiers;
13119 tree declarator;
13120 const char *saved_message;
13121
13122 /* If it's an ellipsis, it's easy to handle. */
13123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13124 {
13125 /* Consume the `...' token. */
13126 cp_lexer_consume_token (parser->lexer);
13127 return NULL_TREE;
13128 }
13129
13130 /* Types may not be defined in exception-declarations. */
13131 saved_message = parser->type_definition_forbidden_message;
13132 parser->type_definition_forbidden_message
13133 = "types may not be defined in exception-declarations";
13134
13135 /* Parse the type-specifier-seq. */
13136 type_specifiers = cp_parser_type_specifier_seq (parser);
13137 /* If it's a `)', then there is no declarator. */
13138 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13139 declarator = NULL_TREE;
13140 else
13141 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13142 /*ctor_dtor_or_conv_p=*/NULL,
13143 /*parenthesized_p=*/NULL);
13144
13145 /* Restore the saved message. */
13146 parser->type_definition_forbidden_message = saved_message;
13147
13148 return start_handler_parms (type_specifiers, declarator);
13149 }
13150
13151 /* Parse a throw-expression.
13152
13153 throw-expression:
13154 throw assignment-expression [opt]
13155
13156 Returns a THROW_EXPR representing the throw-expression. */
13157
13158 static tree
cp_parser_throw_expression(cp_parser * parser)13159 cp_parser_throw_expression (cp_parser* parser)
13160 {
13161 tree expression;
13162 cp_token* token;
13163
13164 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13165 token = cp_lexer_peek_token (parser->lexer);
13166 /* Figure out whether or not there is an assignment-expression
13167 following the "throw" keyword. */
13168 if (token->type == CPP_COMMA
13169 || token->type == CPP_SEMICOLON
13170 || token->type == CPP_CLOSE_PAREN
13171 || token->type == CPP_CLOSE_SQUARE
13172 || token->type == CPP_CLOSE_BRACE
13173 || token->type == CPP_COLON)
13174 expression = NULL_TREE;
13175 else
13176 expression = cp_parser_assignment_expression (parser);
13177
13178 return build_throw (expression);
13179 }
13180
13181 /* GNU Extensions */
13182
13183 /* Parse an (optional) asm-specification.
13184
13185 asm-specification:
13186 asm ( string-literal )
13187
13188 If the asm-specification is present, returns a STRING_CST
13189 corresponding to the string-literal. Otherwise, returns
13190 NULL_TREE. */
13191
13192 static tree
cp_parser_asm_specification_opt(cp_parser * parser)13193 cp_parser_asm_specification_opt (cp_parser* parser)
13194 {
13195 cp_token *token;
13196 tree asm_specification;
13197
13198 /* Peek at the next token. */
13199 token = cp_lexer_peek_token (parser->lexer);
13200 /* If the next token isn't the `asm' keyword, then there's no
13201 asm-specification. */
13202 if (!cp_parser_is_keyword (token, RID_ASM))
13203 return NULL_TREE;
13204
13205 /* Consume the `asm' token. */
13206 cp_lexer_consume_token (parser->lexer);
13207 /* Look for the `('. */
13208 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13209
13210 /* Look for the string-literal. */
13211 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13212 if (token)
13213 asm_specification = token->value;
13214 else
13215 asm_specification = NULL_TREE;
13216
13217 /* Look for the `)'. */
13218 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13219
13220 return asm_specification;
13221 }
13222
13223 /* Parse an asm-operand-list.
13224
13225 asm-operand-list:
13226 asm-operand
13227 asm-operand-list , asm-operand
13228
13229 asm-operand:
13230 string-literal ( expression )
13231 [ string-literal ] string-literal ( expression )
13232
13233 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13234 each node is the expression. The TREE_PURPOSE is itself a
13235 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13236 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13237 is a STRING_CST for the string literal before the parenthesis. */
13238
13239 static tree
cp_parser_asm_operand_list(cp_parser * parser)13240 cp_parser_asm_operand_list (cp_parser* parser)
13241 {
13242 tree asm_operands = NULL_TREE;
13243
13244 while (true)
13245 {
13246 tree string_literal;
13247 tree expression;
13248 tree name;
13249 cp_token *token;
13250
13251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13252 {
13253 /* Consume the `[' token. */
13254 cp_lexer_consume_token (parser->lexer);
13255 /* Read the operand name. */
13256 name = cp_parser_identifier (parser);
13257 if (name != error_mark_node)
13258 name = build_string (IDENTIFIER_LENGTH (name),
13259 IDENTIFIER_POINTER (name));
13260 /* Look for the closing `]'. */
13261 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13262 }
13263 else
13264 name = NULL_TREE;
13265 /* Look for the string-literal. */
13266 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13267 string_literal = token ? token->value : error_mark_node;
13268 /* Look for the `('. */
13269 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13270 /* Parse the expression. */
13271 expression = cp_parser_expression (parser);
13272 /* Look for the `)'. */
13273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13274 /* Add this operand to the list. */
13275 asm_operands = tree_cons (build_tree_list (name, string_literal),
13276 expression,
13277 asm_operands);
13278 /* If the next token is not a `,', there are no more
13279 operands. */
13280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13281 break;
13282 /* Consume the `,'. */
13283 cp_lexer_consume_token (parser->lexer);
13284 }
13285
13286 return nreverse (asm_operands);
13287 }
13288
13289 /* Parse an asm-clobber-list.
13290
13291 asm-clobber-list:
13292 string-literal
13293 asm-clobber-list , string-literal
13294
13295 Returns a TREE_LIST, indicating the clobbers in the order that they
13296 appeared. The TREE_VALUE of each node is a STRING_CST. */
13297
13298 static tree
cp_parser_asm_clobber_list(cp_parser * parser)13299 cp_parser_asm_clobber_list (cp_parser* parser)
13300 {
13301 tree clobbers = NULL_TREE;
13302
13303 while (true)
13304 {
13305 cp_token *token;
13306 tree string_literal;
13307
13308 /* Look for the string literal. */
13309 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13310 string_literal = token ? token->value : error_mark_node;
13311 /* Add it to the list. */
13312 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13313 /* If the next token is not a `,', then the list is
13314 complete. */
13315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13316 break;
13317 /* Consume the `,' token. */
13318 cp_lexer_consume_token (parser->lexer);
13319 }
13320
13321 return clobbers;
13322 }
13323
13324 /* Parse an (optional) series of attributes.
13325
13326 attributes:
13327 attributes attribute
13328
13329 attribute:
13330 __attribute__ (( attribute-list [opt] ))
13331
13332 The return value is as for cp_parser_attribute_list. */
13333
13334 static tree
cp_parser_attributes_opt(cp_parser * parser)13335 cp_parser_attributes_opt (cp_parser* parser)
13336 {
13337 tree attributes = NULL_TREE;
13338
13339 while (true)
13340 {
13341 cp_token *token;
13342 tree attribute_list;
13343
13344 /* Peek at the next token. */
13345 token = cp_lexer_peek_token (parser->lexer);
13346 /* If it's not `__attribute__', then we're done. */
13347 if (token->keyword != RID_ATTRIBUTE)
13348 break;
13349
13350 /* Consume the `__attribute__' keyword. */
13351 cp_lexer_consume_token (parser->lexer);
13352 /* Look for the two `(' tokens. */
13353 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13354 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13355
13356 /* Peek at the next token. */
13357 token = cp_lexer_peek_token (parser->lexer);
13358 if (token->type != CPP_CLOSE_PAREN)
13359 /* Parse the attribute-list. */
13360 attribute_list = cp_parser_attribute_list (parser);
13361 else
13362 /* If the next token is a `)', then there is no attribute
13363 list. */
13364 attribute_list = NULL;
13365
13366 /* Look for the two `)' tokens. */
13367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13368 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13369
13370 /* Add these new attributes to the list. */
13371 attributes = chainon (attributes, attribute_list);
13372 }
13373
13374 return attributes;
13375 }
13376
13377 /* Parse an attribute-list.
13378
13379 attribute-list:
13380 attribute
13381 attribute-list , attribute
13382
13383 attribute:
13384 identifier
13385 identifier ( identifier )
13386 identifier ( identifier , expression-list )
13387 identifier ( expression-list )
13388
13389 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13390 TREE_PURPOSE of each node is the identifier indicating which
13391 attribute is in use. The TREE_VALUE represents the arguments, if
13392 any. */
13393
13394 static tree
cp_parser_attribute_list(cp_parser * parser)13395 cp_parser_attribute_list (cp_parser* parser)
13396 {
13397 tree attribute_list = NULL_TREE;
13398
13399 while (true)
13400 {
13401 cp_token *token;
13402 tree identifier;
13403 tree attribute;
13404
13405 /* Look for the identifier. We also allow keywords here; for
13406 example `__attribute__ ((const))' is legal. */
13407 token = cp_lexer_peek_token (parser->lexer);
13408 if (token->type != CPP_NAME
13409 && token->type != CPP_KEYWORD)
13410 return error_mark_node;
13411 /* Consume the token. */
13412 token = cp_lexer_consume_token (parser->lexer);
13413
13414 /* Save away the identifier that indicates which attribute this is. */
13415 identifier = token->value;
13416 attribute = build_tree_list (identifier, NULL_TREE);
13417
13418 /* Peek at the next token. */
13419 token = cp_lexer_peek_token (parser->lexer);
13420 /* If it's an `(', then parse the attribute arguments. */
13421 if (token->type == CPP_OPEN_PAREN)
13422 {
13423 tree arguments;
13424
13425 arguments = (cp_parser_parenthesized_expression_list
13426 (parser, true, /*non_constant_p=*/NULL));
13427 /* Save the identifier and arguments away. */
13428 TREE_VALUE (attribute) = arguments;
13429 }
13430
13431 /* Add this attribute to the list. */
13432 TREE_CHAIN (attribute) = attribute_list;
13433 attribute_list = attribute;
13434
13435 /* Now, look for more attributes. */
13436 token = cp_lexer_peek_token (parser->lexer);
13437 /* If the next token isn't a `,', we're done. */
13438 if (token->type != CPP_COMMA)
13439 break;
13440
13441 /* Consume the comma and keep going. */
13442 cp_lexer_consume_token (parser->lexer);
13443 }
13444
13445 /* We built up the list in reverse order. */
13446 return nreverse (attribute_list);
13447 }
13448
13449 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13450 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13451 current value of the PEDANTIC flag, regardless of whether or not
13452 the `__extension__' keyword is present. The caller is responsible
13453 for restoring the value of the PEDANTIC flag. */
13454
13455 static bool
cp_parser_extension_opt(cp_parser * parser,int * saved_pedantic)13456 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13457 {
13458 /* Save the old value of the PEDANTIC flag. */
13459 *saved_pedantic = pedantic;
13460
13461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13462 {
13463 /* Consume the `__extension__' token. */
13464 cp_lexer_consume_token (parser->lexer);
13465 /* We're not being pedantic while the `__extension__' keyword is
13466 in effect. */
13467 pedantic = 0;
13468
13469 return true;
13470 }
13471
13472 return false;
13473 }
13474
13475 /* Parse a label declaration.
13476
13477 label-declaration:
13478 __label__ label-declarator-seq ;
13479
13480 label-declarator-seq:
13481 identifier , label-declarator-seq
13482 identifier */
13483
13484 static void
cp_parser_label_declaration(cp_parser * parser)13485 cp_parser_label_declaration (cp_parser* parser)
13486 {
13487 /* Look for the `__label__' keyword. */
13488 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13489
13490 while (true)
13491 {
13492 tree identifier;
13493
13494 /* Look for an identifier. */
13495 identifier = cp_parser_identifier (parser);
13496 /* Declare it as a lobel. */
13497 finish_label_decl (identifier);
13498 /* If the next token is a `;', stop. */
13499 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13500 break;
13501 /* Look for the `,' separating the label declarations. */
13502 cp_parser_require (parser, CPP_COMMA, "`,'");
13503 }
13504
13505 /* Look for the final `;'. */
13506 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13507 }
13508
13509 /* Support Functions */
13510
13511 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13512 NAME should have one of the representations used for an
13513 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13514 is returned. If PARSER->SCOPE is a dependent type, then a
13515 SCOPE_REF is returned.
13516
13517 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13518 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13519 was formed. Abstractly, such entities should not be passed to this
13520 function, because they do not need to be looked up, but it is
13521 simpler to check for this special case here, rather than at the
13522 call-sites.
13523
13524 In cases not explicitly covered above, this function returns a
13525 DECL, OVERLOAD, or baselink representing the result of the lookup.
13526 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13527 is returned.
13528
13529 If IS_TYPE is TRUE, bindings that do not refer to types are
13530 ignored.
13531
13532 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13533 ignored.
13534
13535 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13536 are ignored.
13537
13538 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13539 types. */
13540
13541 static tree
cp_parser_lookup_name(cp_parser * parser,tree name,bool is_type,bool is_template,bool is_namespace,bool check_dependency)13542 cp_parser_lookup_name (cp_parser *parser, tree name,
13543 bool is_type, bool is_template, bool is_namespace,
13544 bool check_dependency)
13545 {
13546 tree decl;
13547 tree object_type = parser->context->object_type;
13548
13549 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13550 no longer valid. Note that if we are parsing tentatively, and
13551 the parse fails, OBJECT_TYPE will be automatically restored. */
13552 parser->context->object_type = NULL_TREE;
13553
13554 if (name == error_mark_node)
13555 return error_mark_node;
13556
13557 /* A template-id has already been resolved; there is no lookup to
13558 do. */
13559 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13560 return name;
13561 if (BASELINK_P (name))
13562 {
13563 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13564 == TEMPLATE_ID_EXPR),
13565 20020909);
13566 return name;
13567 }
13568
13569 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13570 it should already have been checked to make sure that the name
13571 used matches the type being destroyed. */
13572 if (TREE_CODE (name) == BIT_NOT_EXPR)
13573 {
13574 tree type;
13575
13576 /* Figure out to which type this destructor applies. */
13577 if (parser->scope)
13578 type = parser->scope;
13579 else if (object_type)
13580 type = object_type;
13581 else
13582 type = current_class_type;
13583 /* If that's not a class type, there is no destructor. */
13584 if (!type || !CLASS_TYPE_P (type))
13585 return error_mark_node;
13586 if (!CLASSTYPE_DESTRUCTORS (type))
13587 return error_mark_node;
13588 /* If it was a class type, return the destructor. */
13589 return CLASSTYPE_DESTRUCTORS (type);
13590 }
13591
13592 /* By this point, the NAME should be an ordinary identifier. If
13593 the id-expression was a qualified name, the qualifying scope is
13594 stored in PARSER->SCOPE at this point. */
13595 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13596 20000619);
13597
13598 /* Perform the lookup. */
13599 if (parser->scope)
13600 {
13601 bool dependent_p;
13602
13603 if (parser->scope == error_mark_node)
13604 return error_mark_node;
13605
13606 /* If the SCOPE is dependent, the lookup must be deferred until
13607 the template is instantiated -- unless we are explicitly
13608 looking up names in uninstantiated templates. Even then, we
13609 cannot look up the name if the scope is not a class type; it
13610 might, for example, be a template type parameter. */
13611 dependent_p = (TYPE_P (parser->scope)
13612 && !(parser->in_declarator_p
13613 && currently_open_class (parser->scope))
13614 && dependent_type_p (parser->scope));
13615 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13616 && dependent_p)
13617 {
13618 if (is_type)
13619 /* The resolution to Core Issue 180 says that `struct A::B'
13620 should be considered a type-name, even if `A' is
13621 dependent. */
13622 decl = TYPE_NAME (make_typename_type (parser->scope,
13623 name,
13624 /*complain=*/1));
13625 else if (is_template)
13626 decl = make_unbound_class_template (parser->scope,
13627 name,
13628 /*complain=*/1);
13629 else
13630 decl = build_nt (SCOPE_REF, parser->scope, name);
13631 }
13632 else
13633 {
13634 bool pop_p = false;
13635
13636 /* If PARSER->SCOPE is a dependent type, then it must be a
13637 class type, and we must not be checking dependencies;
13638 otherwise, we would have processed this lookup above. So
13639 that PARSER->SCOPE is not considered a dependent base by
13640 lookup_member, we must enter the scope here. */
13641 if (dependent_p)
13642 pop_p = push_scope (parser->scope);
13643 /* If the PARSER->SCOPE is a a template specialization, it
13644 may be instantiated during name lookup. In that case,
13645 errors may be issued. Even if we rollback the current
13646 tentative parse, those errors are valid. */
13647 decl = lookup_qualified_name (parser->scope, name, is_type,
13648 /*complain=*/true);
13649 if (pop_p)
13650 pop_scope (parser->scope);
13651 }
13652 parser->qualifying_scope = parser->scope;
13653 parser->object_scope = NULL_TREE;
13654 }
13655 else if (object_type)
13656 {
13657 tree object_decl = NULL_TREE;
13658 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13659 OBJECT_TYPE is not a class. */
13660 if (CLASS_TYPE_P (object_type))
13661 /* If the OBJECT_TYPE is a template specialization, it may
13662 be instantiated during name lookup. In that case, errors
13663 may be issued. Even if we rollback the current tentative
13664 parse, those errors are valid. */
13665 object_decl = lookup_member (object_type,
13666 name,
13667 /*protect=*/0, is_type);
13668 /* Look it up in the enclosing context, too. */
13669 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13670 is_namespace,
13671 /*flags=*/0);
13672 parser->object_scope = object_type;
13673 parser->qualifying_scope = NULL_TREE;
13674 if (object_decl)
13675 decl = object_decl;
13676 }
13677 else
13678 {
13679 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13680 is_namespace,
13681 /*flags=*/0);
13682 parser->qualifying_scope = NULL_TREE;
13683 parser->object_scope = NULL_TREE;
13684 }
13685
13686 /* If the lookup failed, let our caller know. */
13687 if (!decl
13688 || decl == error_mark_node
13689 || (TREE_CODE (decl) == FUNCTION_DECL
13690 && DECL_ANTICIPATED (decl)))
13691 return error_mark_node;
13692
13693 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13694 if (TREE_CODE (decl) == TREE_LIST)
13695 {
13696 /* The error message we have to print is too complicated for
13697 cp_parser_error, so we incorporate its actions directly. */
13698 if (!cp_parser_simulate_error (parser))
13699 {
13700 error ("reference to `%D' is ambiguous", name);
13701 print_candidates (decl);
13702 }
13703 return error_mark_node;
13704 }
13705
13706 my_friendly_assert (DECL_P (decl)
13707 || TREE_CODE (decl) == OVERLOAD
13708 || TREE_CODE (decl) == SCOPE_REF
13709 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13710 || BASELINK_P (decl),
13711 20000619);
13712
13713 /* If we have resolved the name of a member declaration, check to
13714 see if the declaration is accessible. When the name resolves to
13715 set of overloaded functions, accessibility is checked when
13716 overload resolution is done.
13717
13718 During an explicit instantiation, access is not checked at all,
13719 as per [temp.explicit]. */
13720 if (DECL_P (decl))
13721 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13722
13723 return decl;
13724 }
13725
13726 /* Like cp_parser_lookup_name, but for use in the typical case where
13727 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13728 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13729
13730 static tree
cp_parser_lookup_name_simple(cp_parser * parser,tree name)13731 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13732 {
13733 return cp_parser_lookup_name (parser, name,
13734 /*is_type=*/false,
13735 /*is_template=*/false,
13736 /*is_namespace=*/false,
13737 /*check_dependency=*/true);
13738 }
13739
13740 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13741 the current context, return the TYPE_DECL. If TAG_NAME_P is
13742 true, the DECL indicates the class being defined in a class-head,
13743 or declared in an elaborated-type-specifier.
13744
13745 Otherwise, return DECL. */
13746
13747 static tree
cp_parser_maybe_treat_template_as_class(tree decl,bool tag_name_p)13748 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13749 {
13750 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13751 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13752
13753 struct A {
13754 template <typename T> struct B;
13755 };
13756
13757 template <typename T> struct A::B {};
13758
13759 Similarly, in a elaborated-type-specifier:
13760
13761 namespace N { struct X{}; }
13762
13763 struct A {
13764 template <typename T> friend struct N::X;
13765 };
13766
13767 However, if the DECL refers to a class type, and we are in
13768 the scope of the class, then the name lookup automatically
13769 finds the TYPE_DECL created by build_self_reference rather
13770 than a TEMPLATE_DECL. For example, in:
13771
13772 template <class T> struct S {
13773 S s;
13774 };
13775
13776 there is no need to handle such case. */
13777
13778 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13779 return DECL_TEMPLATE_RESULT (decl);
13780
13781 return decl;
13782 }
13783
13784 /* If too many, or too few, template-parameter lists apply to the
13785 declarator, issue an error message. Returns TRUE if all went well,
13786 and FALSE otherwise. */
13787
13788 static bool
cp_parser_check_declarator_template_parameters(cp_parser * parser,tree declarator)13789 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13790 tree declarator)
13791 {
13792 unsigned num_templates;
13793
13794 /* We haven't seen any classes that involve template parameters yet. */
13795 num_templates = 0;
13796
13797 switch (TREE_CODE (declarator))
13798 {
13799 case CALL_EXPR:
13800 case ARRAY_REF:
13801 case INDIRECT_REF:
13802 case ADDR_EXPR:
13803 {
13804 tree main_declarator = TREE_OPERAND (declarator, 0);
13805 return
13806 cp_parser_check_declarator_template_parameters (parser,
13807 main_declarator);
13808 }
13809
13810 case SCOPE_REF:
13811 {
13812 tree scope;
13813 tree member;
13814
13815 scope = TREE_OPERAND (declarator, 0);
13816 member = TREE_OPERAND (declarator, 1);
13817
13818 /* If this is a pointer-to-member, then we are not interested
13819 in the SCOPE, because it does not qualify the thing that is
13820 being declared. */
13821 if (TREE_CODE (member) == INDIRECT_REF)
13822 return (cp_parser_check_declarator_template_parameters
13823 (parser, member));
13824
13825 while (scope && CLASS_TYPE_P (scope))
13826 {
13827 /* You're supposed to have one `template <...>'
13828 for every template class, but you don't need one
13829 for a full specialization. For example:
13830
13831 template <class T> struct S{};
13832 template <> struct S<int> { void f(); };
13833 void S<int>::f () {}
13834
13835 is correct; there shouldn't be a `template <>' for
13836 the definition of `S<int>::f'. */
13837 if (CLASSTYPE_TEMPLATE_INFO (scope)
13838 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13839 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13840 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13841 ++num_templates;
13842
13843 scope = TYPE_CONTEXT (scope);
13844 }
13845 }
13846
13847 /* Fall through. */
13848
13849 default:
13850 /* If the DECLARATOR has the form `X<y>' then it uses one
13851 additional level of template parameters. */
13852 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13853 ++num_templates;
13854
13855 return cp_parser_check_template_parameters (parser,
13856 num_templates);
13857 }
13858 }
13859
13860 /* NUM_TEMPLATES were used in the current declaration. If that is
13861 invalid, return FALSE and issue an error messages. Otherwise,
13862 return TRUE. */
13863
13864 static bool
cp_parser_check_template_parameters(cp_parser * parser,unsigned num_templates)13865 cp_parser_check_template_parameters (cp_parser* parser,
13866 unsigned num_templates)
13867 {
13868 /* If there are more template classes than parameter lists, we have
13869 something like:
13870
13871 template <class T> void S<T>::R<T>::f (); */
13872 if (parser->num_template_parameter_lists < num_templates)
13873 {
13874 error ("too few template-parameter-lists");
13875 return false;
13876 }
13877 /* If there are the same number of template classes and parameter
13878 lists, that's OK. */
13879 if (parser->num_template_parameter_lists == num_templates)
13880 return true;
13881 /* If there are more, but only one more, then we are referring to a
13882 member template. That's OK too. */
13883 if (parser->num_template_parameter_lists == num_templates + 1)
13884 return true;
13885 /* Otherwise, there are too many template parameter lists. We have
13886 something like:
13887
13888 template <class T> template <class U> void S::f(); */
13889 error ("too many template-parameter-lists");
13890 return false;
13891 }
13892
13893 /* Parse a binary-expression of the general form:
13894
13895 binary-expression:
13896 <expr>
13897 binary-expression <token> <expr>
13898
13899 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13900 to parser the <expr>s. If the first production is used, then the
13901 value returned by FN is returned directly. Otherwise, a node with
13902 the indicated EXPR_TYPE is returned, with operands corresponding to
13903 the two sub-expressions. */
13904
13905 static tree
cp_parser_binary_expression(cp_parser * parser,const cp_parser_token_tree_map token_tree_map,cp_parser_expression_fn fn)13906 cp_parser_binary_expression (cp_parser* parser,
13907 const cp_parser_token_tree_map token_tree_map,
13908 cp_parser_expression_fn fn)
13909 {
13910 tree lhs;
13911
13912 /* Parse the first expression. */
13913 lhs = (*fn) (parser);
13914 /* Now, look for more expressions. */
13915 while (true)
13916 {
13917 cp_token *token;
13918 const cp_parser_token_tree_map_node *map_node;
13919 tree rhs;
13920
13921 /* Peek at the next token. */
13922 token = cp_lexer_peek_token (parser->lexer);
13923 /* If the token is `>', and that's not an operator at the
13924 moment, then we're done. */
13925 if (token->type == CPP_GREATER
13926 && !parser->greater_than_is_operator_p)
13927 break;
13928 /* If we find one of the tokens we want, build the corresponding
13929 tree representation. */
13930 for (map_node = token_tree_map;
13931 map_node->token_type != CPP_EOF;
13932 ++map_node)
13933 if (map_node->token_type == token->type)
13934 {
13935 /* Assume that an overloaded operator will not be used. */
13936 bool overloaded_p = false;
13937
13938 /* Consume the operator token. */
13939 cp_lexer_consume_token (parser->lexer);
13940 /* Parse the right-hand side of the expression. */
13941 rhs = (*fn) (parser);
13942 /* Build the binary tree node. */
13943 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
13944 &overloaded_p);
13945 /* If the binary operator required the use of an
13946 overloaded operator, then this expression cannot be an
13947 integral constant-expression. An overloaded operator
13948 can be used even if both operands are otherwise
13949 permissible in an integral constant-expression if at
13950 least one of the operands is of enumeration type. */
13951 if (overloaded_p
13952 && (cp_parser_non_integral_constant_expression
13953 (parser, "calls to overloaded operators")))
13954 lhs = error_mark_node;
13955 break;
13956 }
13957
13958 /* If the token wasn't one of the ones we want, we're done. */
13959 if (map_node->token_type == CPP_EOF)
13960 break;
13961 }
13962
13963 return lhs;
13964 }
13965
13966 /* Parse an optional `::' token indicating that the following name is
13967 from the global namespace. If so, PARSER->SCOPE is set to the
13968 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13969 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13970 Returns the new value of PARSER->SCOPE, if the `::' token is
13971 present, and NULL_TREE otherwise. */
13972
13973 static tree
cp_parser_global_scope_opt(cp_parser * parser,bool current_scope_valid_p)13974 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13975 {
13976 cp_token *token;
13977
13978 /* Peek at the next token. */
13979 token = cp_lexer_peek_token (parser->lexer);
13980 /* If we're looking at a `::' token then we're starting from the
13981 global namespace, not our current location. */
13982 if (token->type == CPP_SCOPE)
13983 {
13984 /* Consume the `::' token. */
13985 cp_lexer_consume_token (parser->lexer);
13986 /* Set the SCOPE so that we know where to start the lookup. */
13987 parser->scope = global_namespace;
13988 parser->qualifying_scope = global_namespace;
13989 parser->object_scope = NULL_TREE;
13990
13991 return parser->scope;
13992 }
13993 else if (!current_scope_valid_p)
13994 {
13995 parser->scope = NULL_TREE;
13996 parser->qualifying_scope = NULL_TREE;
13997 parser->object_scope = NULL_TREE;
13998 }
13999
14000 return NULL_TREE;
14001 }
14002
14003 /* Returns TRUE if the upcoming token sequence is the start of a
14004 constructor declarator. If FRIEND_P is true, the declarator is
14005 preceded by the `friend' specifier. */
14006
14007 static bool
cp_parser_constructor_declarator_p(cp_parser * parser,bool friend_p)14008 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14009 {
14010 bool constructor_p;
14011 tree type_decl = NULL_TREE;
14012 bool nested_name_p;
14013 cp_token *next_token;
14014
14015 /* The common case is that this is not a constructor declarator, so
14016 try to avoid doing lots of work if at all possible. It's not
14017 valid declare a constructor at function scope. */
14018 if (at_function_scope_p ())
14019 return false;
14020 /* And only certain tokens can begin a constructor declarator. */
14021 next_token = cp_lexer_peek_token (parser->lexer);
14022 if (next_token->type != CPP_NAME
14023 && next_token->type != CPP_SCOPE
14024 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14025 && next_token->type != CPP_TEMPLATE_ID)
14026 return false;
14027
14028 /* Parse tentatively; we are going to roll back all of the tokens
14029 consumed here. */
14030 cp_parser_parse_tentatively (parser);
14031 /* Assume that we are looking at a constructor declarator. */
14032 constructor_p = true;
14033
14034 /* Look for the optional `::' operator. */
14035 cp_parser_global_scope_opt (parser,
14036 /*current_scope_valid_p=*/false);
14037 /* Look for the nested-name-specifier. */
14038 nested_name_p
14039 = (cp_parser_nested_name_specifier_opt (parser,
14040 /*typename_keyword_p=*/false,
14041 /*check_dependency_p=*/false,
14042 /*type_p=*/false,
14043 /*is_declaration=*/false)
14044 != NULL_TREE);
14045 /* Outside of a class-specifier, there must be a
14046 nested-name-specifier. */
14047 if (!nested_name_p &&
14048 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14049 || friend_p))
14050 constructor_p = false;
14051 /* If we still think that this might be a constructor-declarator,
14052 look for a class-name. */
14053 if (constructor_p)
14054 {
14055 /* If we have:
14056
14057 template <typename T> struct S { S(); };
14058 template <typename T> S<T>::S ();
14059
14060 we must recognize that the nested `S' names a class.
14061 Similarly, for:
14062
14063 template <typename T> S<T>::S<T> ();
14064
14065 we must recognize that the nested `S' names a template. */
14066 type_decl = cp_parser_class_name (parser,
14067 /*typename_keyword_p=*/false,
14068 /*template_keyword_p=*/false,
14069 /*type_p=*/false,
14070 /*check_dependency_p=*/false,
14071 /*class_head_p=*/false,
14072 /*is_declaration=*/false);
14073 /* If there was no class-name, then this is not a constructor. */
14074 constructor_p = !cp_parser_error_occurred (parser);
14075 }
14076
14077 /* If we're still considering a constructor, we have to see a `(',
14078 to begin the parameter-declaration-clause, followed by either a
14079 `)', an `...', or a decl-specifier. We need to check for a
14080 type-specifier to avoid being fooled into thinking that:
14081
14082 S::S (f) (int);
14083
14084 is a constructor. (It is actually a function named `f' that
14085 takes one parameter (of type `int') and returns a value of type
14086 `S::S'. */
14087 if (constructor_p
14088 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14089 {
14090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14091 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14092 /* A parameter declaration begins with a decl-specifier,
14093 which is either the "attribute" keyword, a storage class
14094 specifier, or (usually) a type-specifier. */
14095 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14096 && !cp_parser_storage_class_specifier_opt (parser))
14097 {
14098 tree type;
14099 bool pop_p = false;
14100 unsigned saved_num_template_parameter_lists;
14101
14102 /* Names appearing in the type-specifier should be looked up
14103 in the scope of the class. */
14104 if (current_class_type)
14105 type = NULL_TREE;
14106 else
14107 {
14108 type = TREE_TYPE (type_decl);
14109 if (TREE_CODE (type) == TYPENAME_TYPE)
14110 {
14111 type = resolve_typename_type (type,
14112 /*only_current_p=*/false);
14113 if (type == error_mark_node)
14114 {
14115 cp_parser_abort_tentative_parse (parser);
14116 return false;
14117 }
14118 }
14119 pop_p = push_scope (type);
14120 }
14121
14122 /* Inside the constructor parameter list, surrounding
14123 template-parameter-lists do not apply. */
14124 saved_num_template_parameter_lists
14125 = parser->num_template_parameter_lists;
14126 parser->num_template_parameter_lists = 0;
14127
14128 /* Look for the type-specifier. */
14129 cp_parser_type_specifier (parser,
14130 CP_PARSER_FLAGS_NONE,
14131 /*is_friend=*/false,
14132 /*is_declarator=*/true,
14133 /*declares_class_or_enum=*/NULL,
14134 /*is_cv_qualifier=*/NULL);
14135
14136 parser->num_template_parameter_lists
14137 = saved_num_template_parameter_lists;
14138
14139 /* Leave the scope of the class. */
14140 if (pop_p)
14141 pop_scope (type);
14142
14143 constructor_p = !cp_parser_error_occurred (parser);
14144 }
14145 }
14146 else
14147 constructor_p = false;
14148 /* We did not really want to consume any tokens. */
14149 cp_parser_abort_tentative_parse (parser);
14150
14151 return constructor_p;
14152 }
14153
14154 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14155 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14156 they must be performed once we are in the scope of the function.
14157
14158 Returns the function defined. */
14159
14160 static tree
cp_parser_function_definition_from_specifiers_and_declarator(cp_parser * parser,tree decl_specifiers,tree attributes,tree declarator)14161 cp_parser_function_definition_from_specifiers_and_declarator
14162 (cp_parser* parser,
14163 tree decl_specifiers,
14164 tree attributes,
14165 tree declarator)
14166 {
14167 tree fn;
14168 bool success_p;
14169
14170 /* Begin the function-definition. */
14171 success_p = begin_function_definition (decl_specifiers,
14172 attributes,
14173 declarator);
14174
14175 /* If there were names looked up in the decl-specifier-seq that we
14176 did not check, check them now. We must wait until we are in the
14177 scope of the function to perform the checks, since the function
14178 might be a friend. */
14179 perform_deferred_access_checks ();
14180
14181 if (!success_p)
14182 {
14183 /* If begin_function_definition didn't like the definition, skip
14184 the entire function. */
14185 error ("invalid function declaration");
14186 cp_parser_skip_to_end_of_block_or_statement (parser);
14187 fn = error_mark_node;
14188 }
14189 else
14190 fn = cp_parser_function_definition_after_declarator (parser,
14191 /*inline_p=*/false);
14192
14193 return fn;
14194 }
14195
14196 /* Parse the part of a function-definition that follows the
14197 declarator. INLINE_P is TRUE iff this function is an inline
14198 function defined with a class-specifier.
14199
14200 Returns the function defined. */
14201
14202 static tree
cp_parser_function_definition_after_declarator(cp_parser * parser,bool inline_p)14203 cp_parser_function_definition_after_declarator (cp_parser* parser,
14204 bool inline_p)
14205 {
14206 tree fn;
14207 bool ctor_initializer_p = false;
14208 bool saved_in_unbraced_linkage_specification_p;
14209 unsigned saved_num_template_parameter_lists;
14210
14211 /* If the next token is `return', then the code may be trying to
14212 make use of the "named return value" extension that G++ used to
14213 support. */
14214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14215 {
14216 /* Consume the `return' keyword. */
14217 cp_lexer_consume_token (parser->lexer);
14218 /* Look for the identifier that indicates what value is to be
14219 returned. */
14220 cp_parser_identifier (parser);
14221 /* Issue an error message. */
14222 error ("named return values are no longer supported");
14223 /* Skip tokens until we reach the start of the function body. */
14224 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14225 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14226 cp_lexer_consume_token (parser->lexer);
14227 }
14228 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14229 anything declared inside `f'. */
14230 saved_in_unbraced_linkage_specification_p
14231 = parser->in_unbraced_linkage_specification_p;
14232 parser->in_unbraced_linkage_specification_p = false;
14233 /* Inside the function, surrounding template-parameter-lists do not
14234 apply. */
14235 saved_num_template_parameter_lists
14236 = parser->num_template_parameter_lists;
14237 parser->num_template_parameter_lists = 0;
14238 /* If the next token is `try', then we are looking at a
14239 function-try-block. */
14240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14241 ctor_initializer_p = cp_parser_function_try_block (parser);
14242 /* A function-try-block includes the function-body, so we only do
14243 this next part if we're not processing a function-try-block. */
14244 else
14245 ctor_initializer_p
14246 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14247
14248 /* Finish the function. */
14249 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14250 (inline_p ? 2 : 0));
14251 /* Generate code for it, if necessary. */
14252 expand_or_defer_fn (fn);
14253 /* Restore the saved values. */
14254 parser->in_unbraced_linkage_specification_p
14255 = saved_in_unbraced_linkage_specification_p;
14256 parser->num_template_parameter_lists
14257 = saved_num_template_parameter_lists;
14258
14259 return fn;
14260 }
14261
14262 /* Parse a template-declaration, assuming that the `export' (and
14263 `extern') keywords, if present, has already been scanned. MEMBER_P
14264 is as for cp_parser_template_declaration. */
14265
14266 static void
cp_parser_template_declaration_after_export(cp_parser * parser,bool member_p)14267 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14268 {
14269 tree decl = NULL_TREE;
14270 tree parameter_list;
14271 bool friend_p = false;
14272
14273 /* Look for the `template' keyword. */
14274 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14275 return;
14276
14277 /* And the `<'. */
14278 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14279 return;
14280
14281 /* If the next token is `>', then we have an invalid
14282 specialization. Rather than complain about an invalid template
14283 parameter, issue an error message here. */
14284 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14285 {
14286 cp_parser_error (parser, "invalid explicit specialization");
14287 begin_specialization ();
14288 parameter_list = NULL_TREE;
14289 }
14290 else
14291 {
14292 /* Parse the template parameters. */
14293 begin_template_parm_list ();
14294 parameter_list = cp_parser_template_parameter_list (parser);
14295 parameter_list = end_template_parm_list (parameter_list);
14296 }
14297
14298 /* Look for the `>'. */
14299 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14300 /* We just processed one more parameter list. */
14301 ++parser->num_template_parameter_lists;
14302 /* If the next token is `template', there are more template
14303 parameters. */
14304 if (cp_lexer_next_token_is_keyword (parser->lexer,
14305 RID_TEMPLATE))
14306 cp_parser_template_declaration_after_export (parser, member_p);
14307 else
14308 {
14309 decl = cp_parser_single_declaration (parser,
14310 member_p,
14311 &friend_p);
14312
14313 /* If this is a member template declaration, let the front
14314 end know. */
14315 if (member_p && !friend_p && decl)
14316 {
14317 if (TREE_CODE (decl) == TYPE_DECL)
14318 cp_parser_check_access_in_redeclaration (decl);
14319
14320 decl = finish_member_template_decl (decl);
14321 }
14322 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14323 make_friend_class (current_class_type, TREE_TYPE (decl),
14324 /*complain=*/true);
14325 }
14326 /* We are done with the current parameter list. */
14327 --parser->num_template_parameter_lists;
14328
14329 /* Finish up. */
14330 finish_template_decl (parameter_list);
14331
14332 /* Register member declarations. */
14333 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14334 finish_member_declaration (decl);
14335
14336 /* If DECL is a function template, we must return to parse it later.
14337 (Even though there is no definition, there might be default
14338 arguments that need handling.) */
14339 if (member_p && decl
14340 && (TREE_CODE (decl) == FUNCTION_DECL
14341 || DECL_FUNCTION_TEMPLATE_P (decl)))
14342 TREE_VALUE (parser->unparsed_functions_queues)
14343 = tree_cons (NULL_TREE, decl,
14344 TREE_VALUE (parser->unparsed_functions_queues));
14345 }
14346
14347 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14348 `function-definition' sequence. MEMBER_P is true, this declaration
14349 appears in a class scope.
14350
14351 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14352 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14353
14354 static tree
cp_parser_single_declaration(cp_parser * parser,bool member_p,bool * friend_p)14355 cp_parser_single_declaration (cp_parser* parser,
14356 bool member_p,
14357 bool* friend_p)
14358 {
14359 int declares_class_or_enum;
14360 tree decl = NULL_TREE;
14361 tree decl_specifiers;
14362 tree attributes;
14363 bool function_definition_p = false;
14364
14365 /* Defer access checks until we know what is being declared. */
14366 push_deferring_access_checks (dk_deferred);
14367
14368 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14369 alternative. */
14370 decl_specifiers
14371 = cp_parser_decl_specifier_seq (parser,
14372 CP_PARSER_FLAGS_OPTIONAL,
14373 &attributes,
14374 &declares_class_or_enum);
14375 if (friend_p)
14376 *friend_p = cp_parser_friend_p (decl_specifiers);
14377 /* Gather up the access checks that occurred the
14378 decl-specifier-seq. */
14379 stop_deferring_access_checks ();
14380
14381 /* Check for the declaration of a template class. */
14382 if (declares_class_or_enum)
14383 {
14384 if (cp_parser_declares_only_class_p (parser))
14385 {
14386 decl = shadow_tag (decl_specifiers);
14387 if (decl)
14388 decl = TYPE_NAME (decl);
14389 else
14390 decl = error_mark_node;
14391 }
14392 }
14393 else
14394 decl = NULL_TREE;
14395 /* If it's not a template class, try for a template function. If
14396 the next token is a `;', then this declaration does not declare
14397 anything. But, if there were errors in the decl-specifiers, then
14398 the error might well have come from an attempted class-specifier.
14399 In that case, there's no need to warn about a missing declarator. */
14400 if (!decl
14401 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14402 || !value_member (error_mark_node, decl_specifiers)))
14403 decl = cp_parser_init_declarator (parser,
14404 decl_specifiers,
14405 attributes,
14406 /*function_definition_allowed_p=*/true,
14407 member_p,
14408 declares_class_or_enum,
14409 &function_definition_p);
14410
14411 pop_deferring_access_checks ();
14412
14413 /* Clear any current qualification; whatever comes next is the start
14414 of something new. */
14415 parser->scope = NULL_TREE;
14416 parser->qualifying_scope = NULL_TREE;
14417 parser->object_scope = NULL_TREE;
14418 /* Look for a trailing `;' after the declaration. */
14419 if (!function_definition_p
14420 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14421 cp_parser_skip_to_end_of_block_or_statement (parser);
14422
14423 return decl;
14424 }
14425
14426 /* Parse a cast-expression that is not the operand of a unary "&". */
14427
14428 static tree
cp_parser_simple_cast_expression(cp_parser * parser)14429 cp_parser_simple_cast_expression (cp_parser *parser)
14430 {
14431 return cp_parser_cast_expression (parser, /*address_p=*/false);
14432 }
14433
14434 /* Parse a functional cast to TYPE. Returns an expression
14435 representing the cast. */
14436
14437 static tree
cp_parser_functional_cast(cp_parser * parser,tree type)14438 cp_parser_functional_cast (cp_parser* parser, tree type)
14439 {
14440 tree expression_list;
14441 tree cast;
14442
14443 expression_list
14444 = cp_parser_parenthesized_expression_list (parser, false,
14445 /*non_constant_p=*/NULL);
14446
14447 cast = build_functional_cast (type, expression_list);
14448 /* [expr.const]/1: In an integral constant expression "only type
14449 conversions to integral or enumeration type can be used". */
14450 if (cast != error_mark_node && !type_dependent_expression_p (type)
14451 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14452 {
14453 if (cp_parser_non_integral_constant_expression
14454 (parser, "a call to a constructor"))
14455 return error_mark_node;
14456 }
14457 return cast;
14458 }
14459
14460 /* Save the tokens that make up the body of a member function defined
14461 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14462 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14463 specifiers applied to the declaration. Returns the FUNCTION_DECL
14464 for the member function. */
14465
14466 static tree
cp_parser_save_member_function_body(cp_parser * parser,tree decl_specifiers,tree declarator,tree attributes)14467 cp_parser_save_member_function_body (cp_parser* parser,
14468 tree decl_specifiers,
14469 tree declarator,
14470 tree attributes)
14471 {
14472 cp_token_cache *cache;
14473 tree fn;
14474
14475 /* Create the function-declaration. */
14476 fn = start_method (decl_specifiers, declarator, attributes);
14477 /* If something went badly wrong, bail out now. */
14478 if (fn == error_mark_node)
14479 {
14480 /* If there's a function-body, skip it. */
14481 if (cp_parser_token_starts_function_definition_p
14482 (cp_lexer_peek_token (parser->lexer)))
14483 cp_parser_skip_to_end_of_block_or_statement (parser);
14484 return error_mark_node;
14485 }
14486
14487 /* Remember it, if there default args to post process. */
14488 cp_parser_save_default_args (parser, fn);
14489
14490 /* Create a token cache. */
14491 cache = cp_token_cache_new ();
14492 /* Save away the tokens that make up the body of the
14493 function. */
14494 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14495 /* Handle function try blocks. */
14496 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14497 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14498
14499 /* Save away the inline definition; we will process it when the
14500 class is complete. */
14501 DECL_PENDING_INLINE_INFO (fn) = cache;
14502 DECL_PENDING_INLINE_P (fn) = 1;
14503
14504 /* We need to know that this was defined in the class, so that
14505 friend templates are handled correctly. */
14506 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14507
14508 /* We're done with the inline definition. */
14509 finish_method (fn);
14510
14511 /* Add FN to the queue of functions to be parsed later. */
14512 TREE_VALUE (parser->unparsed_functions_queues)
14513 = tree_cons (NULL_TREE, fn,
14514 TREE_VALUE (parser->unparsed_functions_queues));
14515
14516 return fn;
14517 }
14518
14519 /* Parse a template-argument-list, as well as the trailing ">" (but
14520 not the opening ">"). See cp_parser_template_argument_list for the
14521 return value. */
14522
14523 static tree
cp_parser_enclosed_template_argument_list(cp_parser * parser)14524 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14525 {
14526 tree arguments;
14527 tree saved_scope;
14528 tree saved_qualifying_scope;
14529 tree saved_object_scope;
14530 bool saved_greater_than_is_operator_p;
14531
14532 /* [temp.names]
14533
14534 When parsing a template-id, the first non-nested `>' is taken as
14535 the end of the template-argument-list rather than a greater-than
14536 operator. */
14537 saved_greater_than_is_operator_p
14538 = parser->greater_than_is_operator_p;
14539 parser->greater_than_is_operator_p = false;
14540 /* Parsing the argument list may modify SCOPE, so we save it
14541 here. */
14542 saved_scope = parser->scope;
14543 saved_qualifying_scope = parser->qualifying_scope;
14544 saved_object_scope = parser->object_scope;
14545 /* Parse the template-argument-list itself. */
14546 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14547 arguments = NULL_TREE;
14548 else
14549 arguments = cp_parser_template_argument_list (parser);
14550 /* Look for the `>' that ends the template-argument-list. If we find
14551 a '>>' instead, it's probably just a typo. */
14552 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14553 {
14554 if (!saved_greater_than_is_operator_p)
14555 {
14556 /* If we're in a nested template argument list, the '>>' has to be
14557 a typo for '> >'. We emit the error message, but we continue
14558 parsing and we push a '>' as next token, so that the argument
14559 list will be parsed correctly.. */
14560 cp_token* token;
14561 error ("`>>' should be `> >' within a nested template argument list");
14562 token = cp_lexer_peek_token (parser->lexer);
14563 token->type = CPP_GREATER;
14564 }
14565 else
14566 {
14567 /* If this is not a nested template argument list, the '>>' is
14568 a typo for '>'. Emit an error message and continue. */
14569 error ("spurious `>>', use `>' to terminate a template argument list");
14570 cp_lexer_consume_token (parser->lexer);
14571 }
14572 }
14573 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14574 error ("missing `>' to terminate the template argument list");
14575 /* The `>' token might be a greater-than operator again now. */
14576 parser->greater_than_is_operator_p
14577 = saved_greater_than_is_operator_p;
14578 /* Restore the SAVED_SCOPE. */
14579 parser->scope = saved_scope;
14580 parser->qualifying_scope = saved_qualifying_scope;
14581 parser->object_scope = saved_object_scope;
14582
14583 return arguments;
14584 }
14585
14586 /* MEMBER_FUNCTION is a member function, or a friend. If default
14587 arguments, or the body of the function have not yet been parsed,
14588 parse them now. */
14589
14590 static void
cp_parser_late_parsing_for_member(cp_parser * parser,tree member_function)14591 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14592 {
14593 cp_lexer *saved_lexer;
14594
14595 /* If this member is a template, get the underlying
14596 FUNCTION_DECL. */
14597 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14598 member_function = DECL_TEMPLATE_RESULT (member_function);
14599
14600 /* There should not be any class definitions in progress at this
14601 point; the bodies of members are only parsed outside of all class
14602 definitions. */
14603 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14604 /* While we're parsing the member functions we might encounter more
14605 classes. We want to handle them right away, but we don't want
14606 them getting mixed up with functions that are currently in the
14607 queue. */
14608 parser->unparsed_functions_queues
14609 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14610
14611 /* Make sure that any template parameters are in scope. */
14612 maybe_begin_member_template_processing (member_function);
14613
14614 /* If the body of the function has not yet been parsed, parse it
14615 now. */
14616 if (DECL_PENDING_INLINE_P (member_function))
14617 {
14618 tree function_scope;
14619 cp_token_cache *tokens;
14620
14621 /* The function is no longer pending; we are processing it. */
14622 tokens = DECL_PENDING_INLINE_INFO (member_function);
14623 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14624 DECL_PENDING_INLINE_P (member_function) = 0;
14625 /* If this was an inline function in a local class, enter the scope
14626 of the containing function. */
14627 function_scope = decl_function_context (member_function);
14628 if (function_scope)
14629 push_function_context_to (function_scope);
14630
14631 /* Save away the current lexer. */
14632 saved_lexer = parser->lexer;
14633 /* Make a new lexer to feed us the tokens saved for this function. */
14634 parser->lexer = cp_lexer_new_from_tokens (tokens);
14635 parser->lexer->next = saved_lexer;
14636
14637 /* Set the current source position to be the location of the first
14638 token in the saved inline body. */
14639 cp_lexer_peek_token (parser->lexer);
14640
14641 /* Let the front end know that we going to be defining this
14642 function. */
14643 start_function (NULL_TREE, member_function, NULL_TREE,
14644 SF_PRE_PARSED | SF_INCLASS_INLINE);
14645
14646 /* Now, parse the body of the function. */
14647 cp_parser_function_definition_after_declarator (parser,
14648 /*inline_p=*/true);
14649
14650 /* Leave the scope of the containing function. */
14651 if (function_scope)
14652 pop_function_context_from (function_scope);
14653 /* Restore the lexer. */
14654 parser->lexer = saved_lexer;
14655 }
14656
14657 /* Remove any template parameters from the symbol table. */
14658 maybe_end_member_template_processing ();
14659
14660 /* Restore the queue. */
14661 parser->unparsed_functions_queues
14662 = TREE_CHAIN (parser->unparsed_functions_queues);
14663 }
14664
14665 /* If DECL contains any default args, remember it on the unparsed
14666 functions queue. */
14667
14668 static void
cp_parser_save_default_args(cp_parser * parser,tree decl)14669 cp_parser_save_default_args (cp_parser* parser, tree decl)
14670 {
14671 tree probe;
14672
14673 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14674 probe;
14675 probe = TREE_CHAIN (probe))
14676 if (TREE_PURPOSE (probe))
14677 {
14678 TREE_PURPOSE (parser->unparsed_functions_queues)
14679 = tree_cons (NULL_TREE, decl,
14680 TREE_PURPOSE (parser->unparsed_functions_queues));
14681 break;
14682 }
14683 return;
14684 }
14685
14686 /* FN is a FUNCTION_DECL which may contains a parameter with an
14687 unparsed DEFAULT_ARG. Parse the default args now. */
14688
14689 static void
cp_parser_late_parsing_default_args(cp_parser * parser,tree fn)14690 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14691 {
14692 cp_lexer *saved_lexer;
14693 cp_token_cache *tokens;
14694 bool saved_local_variables_forbidden_p;
14695 tree parameters;
14696
14697 /* While we're parsing the default args, we might (due to the
14698 statement expression extension) encounter more classes. We want
14699 to handle them right away, but we don't want them getting mixed
14700 up with default args that are currently in the queue. */
14701 parser->unparsed_functions_queues
14702 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14703
14704 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14705 parameters;
14706 parameters = TREE_CHAIN (parameters))
14707 {
14708 if (!TREE_PURPOSE (parameters)
14709 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14710 continue;
14711
14712 /* Save away the current lexer. */
14713 saved_lexer = parser->lexer;
14714 /* Create a new one, using the tokens we have saved. */
14715 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14716 parser->lexer = cp_lexer_new_from_tokens (tokens);
14717
14718 /* Set the current source position to be the location of the
14719 first token in the default argument. */
14720 cp_lexer_peek_token (parser->lexer);
14721
14722 /* Local variable names (and the `this' keyword) may not appear
14723 in a default argument. */
14724 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14725 parser->local_variables_forbidden_p = true;
14726 /* Parse the assignment-expression. */
14727 if (DECL_CLASS_SCOPE_P (fn))
14728 push_nested_class (DECL_CONTEXT (fn));
14729 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14730 if (DECL_CLASS_SCOPE_P (fn))
14731 pop_nested_class ();
14732
14733 /* If the token stream has not been completely used up, then
14734 there was extra junk after the end of the default
14735 argument. */
14736 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14737 cp_parser_error (parser, "expected `,'");
14738
14739 /* Restore saved state. */
14740 parser->lexer = saved_lexer;
14741 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14742 }
14743
14744 /* Restore the queue. */
14745 parser->unparsed_functions_queues
14746 = TREE_CHAIN (parser->unparsed_functions_queues);
14747 }
14748
14749 /* Parse the operand of `sizeof' (or a similar operator). Returns
14750 either a TYPE or an expression, depending on the form of the
14751 input. The KEYWORD indicates which kind of expression we have
14752 encountered. */
14753
14754 static tree
cp_parser_sizeof_operand(cp_parser * parser,enum rid keyword)14755 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14756 {
14757 static const char *format;
14758 tree expr = NULL_TREE;
14759 const char *saved_message;
14760 bool saved_integral_constant_expression_p;
14761
14762 /* Initialize FORMAT the first time we get here. */
14763 if (!format)
14764 format = "types may not be defined in `%s' expressions";
14765
14766 /* Types cannot be defined in a `sizeof' expression. Save away the
14767 old message. */
14768 saved_message = parser->type_definition_forbidden_message;
14769 /* And create the new one. */
14770 parser->type_definition_forbidden_message
14771 = xmalloc (strlen (format)
14772 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14773 + 1 /* `\0' */);
14774 sprintf ((char *) parser->type_definition_forbidden_message,
14775 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14776
14777 /* The restrictions on constant-expressions do not apply inside
14778 sizeof expressions. */
14779 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14780 parser->integral_constant_expression_p = false;
14781
14782 /* Do not actually evaluate the expression. */
14783 ++skip_evaluation;
14784 /* If it's a `(', then we might be looking at the type-id
14785 construction. */
14786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14787 {
14788 tree type;
14789 bool saved_in_type_id_in_expr_p;
14790
14791 /* We can't be sure yet whether we're looking at a type-id or an
14792 expression. */
14793 cp_parser_parse_tentatively (parser);
14794 /* Consume the `('. */
14795 cp_lexer_consume_token (parser->lexer);
14796 /* Parse the type-id. */
14797 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14798 parser->in_type_id_in_expr_p = true;
14799 type = cp_parser_type_id (parser);
14800 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14801 /* Now, look for the trailing `)'. */
14802 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14803 /* If all went well, then we're done. */
14804 if (cp_parser_parse_definitely (parser))
14805 {
14806 /* Build a list of decl-specifiers; right now, we have only
14807 a single type-specifier. */
14808 type = build_tree_list (NULL_TREE,
14809 type);
14810
14811 /* Call grokdeclarator to figure out what type this is. */
14812 expr = grokdeclarator (NULL_TREE,
14813 type,
14814 TYPENAME,
14815 /*initialized=*/0,
14816 /*attrlist=*/NULL);
14817 }
14818 }
14819
14820 /* If the type-id production did not work out, then we must be
14821 looking at the unary-expression production. */
14822 if (!expr)
14823 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14824 /* Go back to evaluating expressions. */
14825 --skip_evaluation;
14826
14827 /* Free the message we created. */
14828 free ((char *) parser->type_definition_forbidden_message);
14829 /* And restore the old one. */
14830 parser->type_definition_forbidden_message = saved_message;
14831 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14832
14833 return expr;
14834 }
14835
14836 /* If the current declaration has no declarator, return true. */
14837
14838 static bool
cp_parser_declares_only_class_p(cp_parser * parser)14839 cp_parser_declares_only_class_p (cp_parser *parser)
14840 {
14841 /* If the next token is a `;' or a `,' then there is no
14842 declarator. */
14843 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14844 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14845 }
14846
14847 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14848 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14849
14850 static bool
cp_parser_friend_p(tree decl_specifiers)14851 cp_parser_friend_p (tree decl_specifiers)
14852 {
14853 while (decl_specifiers)
14854 {
14855 /* See if this decl-specifier is `friend'. */
14856 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14857 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14858 return true;
14859
14860 /* Go on to the next decl-specifier. */
14861 decl_specifiers = TREE_CHAIN (decl_specifiers);
14862 }
14863
14864 return false;
14865 }
14866
14867 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14868 issue an error message indicating that TOKEN_DESC was expected.
14869
14870 Returns the token consumed, if the token had the appropriate type.
14871 Otherwise, returns NULL. */
14872
14873 static cp_token *
cp_parser_require(cp_parser * parser,enum cpp_ttype type,const char * token_desc)14874 cp_parser_require (cp_parser* parser,
14875 enum cpp_ttype type,
14876 const char* token_desc)
14877 {
14878 if (cp_lexer_next_token_is (parser->lexer, type))
14879 return cp_lexer_consume_token (parser->lexer);
14880 else
14881 {
14882 /* Output the MESSAGE -- unless we're parsing tentatively. */
14883 if (!cp_parser_simulate_error (parser))
14884 {
14885 char *message = concat ("expected ", token_desc, NULL);
14886 cp_parser_error (parser, message);
14887 free (message);
14888 }
14889 return NULL;
14890 }
14891 }
14892
14893 /* Like cp_parser_require, except that tokens will be skipped until
14894 the desired token is found. An error message is still produced if
14895 the next token is not as expected. */
14896
14897 static void
cp_parser_skip_until_found(cp_parser * parser,enum cpp_ttype type,const char * token_desc)14898 cp_parser_skip_until_found (cp_parser* parser,
14899 enum cpp_ttype type,
14900 const char* token_desc)
14901 {
14902 cp_token *token;
14903 unsigned nesting_depth = 0;
14904
14905 if (cp_parser_require (parser, type, token_desc))
14906 return;
14907
14908 /* Skip tokens until the desired token is found. */
14909 while (true)
14910 {
14911 /* Peek at the next token. */
14912 token = cp_lexer_peek_token (parser->lexer);
14913 /* If we've reached the token we want, consume it and
14914 stop. */
14915 if (token->type == type && !nesting_depth)
14916 {
14917 cp_lexer_consume_token (parser->lexer);
14918 return;
14919 }
14920 /* If we've run out of tokens, stop. */
14921 if (token->type == CPP_EOF)
14922 return;
14923 if (token->type == CPP_OPEN_BRACE
14924 || token->type == CPP_OPEN_PAREN
14925 || token->type == CPP_OPEN_SQUARE)
14926 ++nesting_depth;
14927 else if (token->type == CPP_CLOSE_BRACE
14928 || token->type == CPP_CLOSE_PAREN
14929 || token->type == CPP_CLOSE_SQUARE)
14930 {
14931 if (nesting_depth-- == 0)
14932 return;
14933 }
14934 /* Consume this token. */
14935 cp_lexer_consume_token (parser->lexer);
14936 }
14937 }
14938
14939 /* If the next token is the indicated keyword, consume it. Otherwise,
14940 issue an error message indicating that TOKEN_DESC was expected.
14941
14942 Returns the token consumed, if the token had the appropriate type.
14943 Otherwise, returns NULL. */
14944
14945 static cp_token *
cp_parser_require_keyword(cp_parser * parser,enum rid keyword,const char * token_desc)14946 cp_parser_require_keyword (cp_parser* parser,
14947 enum rid keyword,
14948 const char* token_desc)
14949 {
14950 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14951
14952 if (token && token->keyword != keyword)
14953 {
14954 dyn_string_t error_msg;
14955
14956 /* Format the error message. */
14957 error_msg = dyn_string_new (0);
14958 dyn_string_append_cstr (error_msg, "expected ");
14959 dyn_string_append_cstr (error_msg, token_desc);
14960 cp_parser_error (parser, error_msg->s);
14961 dyn_string_delete (error_msg);
14962 return NULL;
14963 }
14964
14965 return token;
14966 }
14967
14968 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14969 function-definition. */
14970
14971 static bool
cp_parser_token_starts_function_definition_p(cp_token * token)14972 cp_parser_token_starts_function_definition_p (cp_token* token)
14973 {
14974 return (/* An ordinary function-body begins with an `{'. */
14975 token->type == CPP_OPEN_BRACE
14976 /* A ctor-initializer begins with a `:'. */
14977 || token->type == CPP_COLON
14978 /* A function-try-block begins with `try'. */
14979 || token->keyword == RID_TRY
14980 /* The named return value extension begins with `return'. */
14981 || token->keyword == RID_RETURN);
14982 }
14983
14984 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14985 definition. */
14986
14987 static bool
cp_parser_next_token_starts_class_definition_p(cp_parser * parser)14988 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14989 {
14990 cp_token *token;
14991
14992 token = cp_lexer_peek_token (parser->lexer);
14993 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14994 }
14995
14996 /* Returns TRUE iff the next token is the "," or ">" ending a
14997 template-argument. ">>" is also accepted (after the full
14998 argument was parsed) because it's probably a typo for "> >",
14999 and there is a specific diagnostic for this. */
15000
15001 static bool
cp_parser_next_token_ends_template_argument_p(cp_parser * parser)15002 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15003 {
15004 cp_token *token;
15005
15006 token = cp_lexer_peek_token (parser->lexer);
15007 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15008 || token->type == CPP_RSHIFT);
15009 }
15010
15011 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15012 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15013
15014 static bool
cp_parser_nth_token_starts_template_argument_list_p(cp_parser * parser,size_t n)15015 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15016 size_t n)
15017 {
15018 cp_token *token;
15019
15020 token = cp_lexer_peek_nth_token (parser->lexer, n);
15021 if (token->type == CPP_LESS)
15022 return true;
15023 /* Check for the sequence `<::' in the original code. It would be lexed as
15024 `[:', where `[' is a digraph, and there is no whitespace before
15025 `:'. */
15026 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15027 {
15028 cp_token *token2;
15029 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15030 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15031 return true;
15032 }
15033 return false;
15034 }
15035
15036 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15037 or none_type otherwise. */
15038
15039 static enum tag_types
cp_parser_token_is_class_key(cp_token * token)15040 cp_parser_token_is_class_key (cp_token* token)
15041 {
15042 switch (token->keyword)
15043 {
15044 case RID_CLASS:
15045 return class_type;
15046 case RID_STRUCT:
15047 return record_type;
15048 case RID_UNION:
15049 return union_type;
15050
15051 default:
15052 return none_type;
15053 }
15054 }
15055
15056 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15057
15058 static void
cp_parser_check_class_key(enum tag_types class_key,tree type)15059 cp_parser_check_class_key (enum tag_types class_key, tree type)
15060 {
15061 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15062 pedwarn ("`%s' tag used in naming `%#T'",
15063 class_key == union_type ? "union"
15064 : class_key == record_type ? "struct" : "class",
15065 type);
15066 }
15067
15068 /* Issue an error message if DECL is redeclared with different
15069 access than its original declaration [class.access.spec/3].
15070 This applies to nested classes and nested class templates.
15071 [class.mem/1]. */
15072
cp_parser_check_access_in_redeclaration(tree decl)15073 static void cp_parser_check_access_in_redeclaration (tree decl)
15074 {
15075 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15076 return;
15077
15078 if ((TREE_PRIVATE (decl)
15079 != (current_access_specifier == access_private_node))
15080 || (TREE_PROTECTED (decl)
15081 != (current_access_specifier == access_protected_node)))
15082 error ("%D redeclared with different access", decl);
15083 }
15084
15085 /* Look for the `template' keyword, as a syntactic disambiguator.
15086 Return TRUE iff it is present, in which case it will be
15087 consumed. */
15088
15089 static bool
cp_parser_optional_template_keyword(cp_parser * parser)15090 cp_parser_optional_template_keyword (cp_parser *parser)
15091 {
15092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15093 {
15094 /* The `template' keyword can only be used within templates;
15095 outside templates the parser can always figure out what is a
15096 template and what is not. */
15097 if (!processing_template_decl)
15098 {
15099 error ("`template' (as a disambiguator) is only allowed "
15100 "within templates");
15101 /* If this part of the token stream is rescanned, the same
15102 error message would be generated. So, we purge the token
15103 from the stream. */
15104 cp_lexer_purge_token (parser->lexer);
15105 return false;
15106 }
15107 else
15108 {
15109 /* Consume the `template' keyword. */
15110 cp_lexer_consume_token (parser->lexer);
15111 return true;
15112 }
15113 }
15114
15115 return false;
15116 }
15117
15118 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15119 set PARSER->SCOPE, and perform other related actions. */
15120
15121 static void
cp_parser_pre_parsed_nested_name_specifier(cp_parser * parser)15122 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15123 {
15124 tree value;
15125 tree check;
15126
15127 /* Get the stored value. */
15128 value = cp_lexer_consume_token (parser->lexer)->value;
15129 /* Perform any access checks that were deferred. */
15130 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15131 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15132 /* Set the scope from the stored value. */
15133 parser->scope = TREE_VALUE (value);
15134 parser->qualifying_scope = TREE_TYPE (value);
15135 parser->object_scope = NULL_TREE;
15136 }
15137
15138 /* Add tokens to CACHE until an non-nested END token appears. */
15139
15140 static void
cp_parser_cache_group(cp_parser * parser,cp_token_cache * cache,enum cpp_ttype end,unsigned depth)15141 cp_parser_cache_group (cp_parser *parser,
15142 cp_token_cache *cache,
15143 enum cpp_ttype end,
15144 unsigned depth)
15145 {
15146 while (true)
15147 {
15148 cp_token *token;
15149
15150 /* Abort a parenthesized expression if we encounter a brace. */
15151 if ((end == CPP_CLOSE_PAREN || depth == 0)
15152 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15153 return;
15154 /* If we've reached the end of the file, stop. */
15155 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15156 return;
15157 /* Consume the next token. */
15158 token = cp_lexer_consume_token (parser->lexer);
15159 /* Add this token to the tokens we are saving. */
15160 cp_token_cache_push_token (cache, token);
15161 /* See if it starts a new group. */
15162 if (token->type == CPP_OPEN_BRACE)
15163 {
15164 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15165 if (depth == 0)
15166 return;
15167 }
15168 else if (token->type == CPP_OPEN_PAREN)
15169 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15170 else if (token->type == end)
15171 return;
15172 }
15173 }
15174
15175 /* Begin parsing tentatively. We always save tokens while parsing
15176 tentatively so that if the tentative parsing fails we can restore the
15177 tokens. */
15178
15179 static void
cp_parser_parse_tentatively(cp_parser * parser)15180 cp_parser_parse_tentatively (cp_parser* parser)
15181 {
15182 /* Enter a new parsing context. */
15183 parser->context = cp_parser_context_new (parser->context);
15184 /* Begin saving tokens. */
15185 cp_lexer_save_tokens (parser->lexer);
15186 /* In order to avoid repetitive access control error messages,
15187 access checks are queued up until we are no longer parsing
15188 tentatively. */
15189 push_deferring_access_checks (dk_deferred);
15190 }
15191
15192 /* Commit to the currently active tentative parse. */
15193
15194 static void
cp_parser_commit_to_tentative_parse(cp_parser * parser)15195 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15196 {
15197 cp_parser_context *context;
15198 cp_lexer *lexer;
15199
15200 /* Mark all of the levels as committed. */
15201 lexer = parser->lexer;
15202 for (context = parser->context; context->next; context = context->next)
15203 {
15204 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15205 break;
15206 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15207 while (!cp_lexer_saving_tokens (lexer))
15208 lexer = lexer->next;
15209 cp_lexer_commit_tokens (lexer);
15210 }
15211 }
15212
15213 /* Abort the currently active tentative parse. All consumed tokens
15214 will be rolled back, and no diagnostics will be issued. */
15215
15216 static void
cp_parser_abort_tentative_parse(cp_parser * parser)15217 cp_parser_abort_tentative_parse (cp_parser* parser)
15218 {
15219 cp_parser_simulate_error (parser);
15220 /* Now, pretend that we want to see if the construct was
15221 successfully parsed. */
15222 cp_parser_parse_definitely (parser);
15223 }
15224
15225 /* Stop parsing tentatively. If a parse error has occurred, restore the
15226 token stream. Otherwise, commit to the tokens we have consumed.
15227 Returns true if no error occurred; false otherwise. */
15228
15229 static bool
cp_parser_parse_definitely(cp_parser * parser)15230 cp_parser_parse_definitely (cp_parser* parser)
15231 {
15232 bool error_occurred;
15233 cp_parser_context *context;
15234
15235 /* Remember whether or not an error occurred, since we are about to
15236 destroy that information. */
15237 error_occurred = cp_parser_error_occurred (parser);
15238 /* Remove the topmost context from the stack. */
15239 context = parser->context;
15240 parser->context = context->next;
15241 /* If no parse errors occurred, commit to the tentative parse. */
15242 if (!error_occurred)
15243 {
15244 /* Commit to the tokens read tentatively, unless that was
15245 already done. */
15246 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15247 cp_lexer_commit_tokens (parser->lexer);
15248
15249 pop_to_parent_deferring_access_checks ();
15250 }
15251 /* Otherwise, if errors occurred, roll back our state so that things
15252 are just as they were before we began the tentative parse. */
15253 else
15254 {
15255 cp_lexer_rollback_tokens (parser->lexer);
15256 pop_deferring_access_checks ();
15257 }
15258 /* Add the context to the front of the free list. */
15259 context->next = cp_parser_context_free_list;
15260 cp_parser_context_free_list = context;
15261
15262 return !error_occurred;
15263 }
15264
15265 /* Returns true if we are parsing tentatively -- but have decided that
15266 we will stick with this tentative parse, even if errors occur. */
15267
15268 static bool
cp_parser_committed_to_tentative_parse(cp_parser * parser)15269 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15270 {
15271 return (cp_parser_parsing_tentatively (parser)
15272 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15273 }
15274
15275 /* Returns nonzero iff an error has occurred during the most recent
15276 tentative parse. */
15277
15278 static bool
cp_parser_error_occurred(cp_parser * parser)15279 cp_parser_error_occurred (cp_parser* parser)
15280 {
15281 return (cp_parser_parsing_tentatively (parser)
15282 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15283 }
15284
15285 /* Returns nonzero if GNU extensions are allowed. */
15286
15287 static bool
cp_parser_allow_gnu_extensions_p(cp_parser * parser)15288 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15289 {
15290 return parser->allow_gnu_extensions_p;
15291 }
15292
15293
15294
15295 /* The parser. */
15296
15297 static GTY (()) cp_parser *the_parser;
15298
15299 /* External interface. */
15300
15301 /* Parse one entire translation unit. */
15302
15303 void
c_parse_file(void)15304 c_parse_file (void)
15305 {
15306 bool error_occurred;
15307
15308 the_parser = cp_parser_new ();
15309 push_deferring_access_checks (flag_access_control
15310 ? dk_no_deferred : dk_no_check);
15311 error_occurred = cp_parser_translation_unit (the_parser);
15312 the_parser = NULL;
15313 }
15314
15315 /* This variable must be provided by every front end. */
15316
15317 int yydebug;
15318
15319 #include "gt-cp-parser.h"
15320