]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
re PR c++/28249 ("long long long" accepted by catch)
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer. */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
47
48 /* A C++ token. */
49
50 typedef struct cp_token GTY (())
51 {
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
57 /* Token flags. */
58 unsigned char flags;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The value associated with this token, if any. */
70 tree value;
71 /* The location at which this token was found. */
72 location_t location;
73 } cp_token;
74
75 /* We use a stack of token pointer for saving token sets. */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
79
80 static const cp_token eof_token =
81 {
82 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
84 0
85 #else
86 {0, 0}
87 #endif
88 };
89
90 /* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
92 it to the parser. Tokens are never added to the cp_lexer after
93 it is created. */
94
95 typedef struct cp_lexer GTY (())
96 {
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
99 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100 /* If the lexer owns the buffer, this is the number of tokens in the
101 buffer. */
102 size_t buffer_length;
103
104 /* A pointer just past the last available token. The tokens
105 in this lexer are [buffer, last_token). */
106 cp_token_position GTY ((skip)) last_token;
107
108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
109 no more available tokens. */
110 cp_token_position GTY ((skip)) next_token;
111
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
114 began saving tokens. If the stack is non-empty, we are saving
115 tokens. */
116 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
117
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
120
121 /* True if we should output debugging information. */
122 bool debugging_p;
123
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
126 bool in_pragma;
127 } cp_lexer;
128
129 /* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
133 a lexer. */
134
135 typedef struct cp_token_cache GTY(())
136 {
137 /* The beginning of the token range. */
138 cp_token * GTY((skip)) first;
139
140 /* Points immediately after the last token in the range. */
141 cp_token * GTY ((skip)) last;
142 } cp_token_cache;
143
144 /* Prototypes. */
145
146 static cp_lexer *cp_lexer_new_main
147 (void);
148 static cp_lexer *cp_lexer_new_from_tokens
149 (cp_token_cache *tokens);
150 static void cp_lexer_destroy
151 (cp_lexer *);
152 static int cp_lexer_saving_tokens
153 (const cp_lexer *);
154 static cp_token_position cp_lexer_token_position
155 (cp_lexer *, bool);
156 static cp_token *cp_lexer_token_at
157 (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159 (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
161 (cp_lexer *);
162 static cp_token *cp_lexer_peek_nth_token
163 (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165 (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169 (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
171 (cp_lexer *);
172 static void cp_lexer_purge_token
173 (cp_lexer *);
174 static void cp_lexer_purge_tokens_after
175 (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
177 (cp_lexer *);
178 static void cp_lexer_commit_tokens
179 (cp_lexer *);
180 static void cp_lexer_rollback_tokens
181 (cp_lexer *);
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184 (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
186 (cp_lexer *);
187 static void cp_lexer_start_debugging
188 (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
191 #else
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
200
201 static cp_token_cache *cp_token_cache_new
202 (cp_token *, cp_token *);
203
204 static void cp_parser_initial_pragma
205 (cp_token *);
206
207 /* Manifest constants. */
208 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
209 #define CP_SAVED_TOKEN_STACK 5
210
211 /* A token type for keywords, as opposed to ordinary identifiers. */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
213
214 /* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
219
220 /* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
226
227 /* A token type for tokens that are not tokens at all; these are used
228 to represent slots in the array where there used to be a token
229 that has now been deleted. */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
231
232 /* The number of token types, including C++-specific ones. */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
234
235 /* Variables. */
236
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written. */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
241
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
243 preprocessor. */
244
245 static cp_lexer *
246 cp_lexer_new_main (void)
247 {
248 cp_token first_token;
249 cp_lexer *lexer;
250 cp_token *pos;
251 size_t alloc;
252 size_t space;
253 cp_token *buffer;
254
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token);
259
260 /* Tell c_lex_with_flags not to merge string constants. */
261 c_lex_return_raw_strings = true;
262
263 c_common_no_more_pch ();
264
265 /* Allocate the memory. */
266 lexer = GGC_CNEW (cp_lexer);
267
268 #ifdef ENABLE_CHECKING
269 /* Initially we are not debugging. */
270 lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273 CP_SAVED_TOKEN_STACK);
274
275 /* Create the buffer. */
276 alloc = CP_LEXER_BUFFER_SIZE;
277 buffer = GGC_NEWVEC (cp_token, alloc);
278
279 /* Put the first token in the buffer. */
280 space = alloc;
281 pos = buffer;
282 *pos = first_token;
283
284 /* Get the remaining tokens from the preprocessor. */
285 while (pos->type != CPP_EOF)
286 {
287 pos++;
288 if (!--space)
289 {
290 space = alloc;
291 alloc *= 2;
292 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293 pos = buffer + space;
294 }
295 cp_lexer_get_preprocessor_token (lexer, pos);
296 }
297 lexer->buffer = buffer;
298 lexer->buffer_length = alloc - space;
299 lexer->last_token = pos;
300 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
301
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in)->client_diagnostic = true;
305 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
306
307 gcc_assert (lexer->next_token->type != CPP_PURGED);
308 return lexer;
309 }
310
311 /* Create a new lexer whose token stream is primed with the tokens in
312 CACHE. When these tokens are exhausted, no new tokens will be read. */
313
314 static cp_lexer *
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
316 {
317 cp_token *first = cache->first;
318 cp_token *last = cache->last;
319 cp_lexer *lexer = GGC_CNEW (cp_lexer);
320
321 /* We do not own the buffer. */
322 lexer->buffer = NULL;
323 lexer->buffer_length = 0;
324 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325 lexer->last_token = last;
326
327 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328 CP_SAVED_TOKEN_STACK);
329
330 #ifdef ENABLE_CHECKING
331 /* Initially we are not debugging. */
332 lexer->debugging_p = false;
333 #endif
334
335 gcc_assert (lexer->next_token->type != CPP_PURGED);
336 return lexer;
337 }
338
339 /* Frees all resources associated with LEXER. */
340
341 static void
342 cp_lexer_destroy (cp_lexer *lexer)
343 {
344 if (lexer->buffer)
345 ggc_free (lexer->buffer);
346 VEC_free (cp_token_position, heap, lexer->saved_tokens);
347 ggc_free (lexer);
348 }
349
350 /* Returns nonzero if debugging information should be output. */
351
352 #ifdef ENABLE_CHECKING
353
354 static inline bool
355 cp_lexer_debugging_p (cp_lexer *lexer)
356 {
357 return lexer->debugging_p;
358 }
359
360 #endif /* ENABLE_CHECKING */
361
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
364 {
365 gcc_assert (!previous_p || lexer->next_token != &eof_token);
366
367 return lexer->next_token - previous_p;
368 }
369
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
372 {
373 return pos;
374 }
375
376 /* nonzero if we are presently saving tokens. */
377
378 static inline int
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
380 {
381 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
382 }
383
384 /* Store the next token from the preprocessor in *TOKEN. Return true
385 if we reach EOF. */
386
387 static void
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
389 cp_token *token)
390 {
391 static int is_extern_c = 0;
392
393 /* Get a new token from the preprocessor. */
394 token->type
395 = c_lex_with_flags (&token->value, &token->location, &token->flags);
396 token->keyword = RID_MAX;
397 token->pragma_kind = PRAGMA_NONE;
398 token->in_system_header = in_system_header;
399
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
406
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
409 {
410 if (C_IS_RESERVED_WORD (token->value))
411 {
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
421 }
422 else
423 {
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
426 }
427 }
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
430 {
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
433 {
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
443 }
444 }
445 else if (token->type == CPP_PRAGMA)
446 {
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token->pragma_kind = TREE_INT_CST_LOW (token->value);
449 token->value = NULL;
450 }
451 }
452
453 /* Update the globals input_location and in_system_header from TOKEN. */
454 static inline void
455 cp_lexer_set_source_position_from_token (cp_token *token)
456 {
457 if (token->type != CPP_EOF)
458 {
459 input_location = token->location;
460 in_system_header = token->in_system_header;
461 }
462 }
463
464 /* Return a pointer to the next token in the token stream, but do not
465 consume it. */
466
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
469 {
470 if (cp_lexer_debugging_p (lexer))
471 {
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474 putc ('\n', cp_lexer_debug_stream);
475 }
476 return lexer->next_token;
477 }
478
479 /* Return true if the next token has the indicated TYPE. */
480
481 static inline bool
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
483 {
484 return cp_lexer_peek_token (lexer)->type == type;
485 }
486
487 /* Return true if the next token does not have the indicated TYPE. */
488
489 static inline bool
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
491 {
492 return !cp_lexer_next_token_is (lexer, type);
493 }
494
495 /* Return true if the next token is the indicated KEYWORD. */
496
497 static inline bool
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
499 {
500 return cp_lexer_peek_token (lexer)->keyword == keyword;
501 }
502
503 /* Return a pointer to the Nth token in the token stream. If N is 1,
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
508
509 static cp_token *
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
511 {
512 cp_token *token;
513
514 /* N is 1-based, not zero-based. */
515 gcc_assert (n > 0);
516
517 if (cp_lexer_debugging_p (lexer))
518 fprintf (cp_lexer_debug_stream,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n);
520
521 --n;
522 token = lexer->next_token;
523 gcc_assert (!n || token != &eof_token);
524 while (n != 0)
525 {
526 ++token;
527 if (token == lexer->last_token)
528 {
529 token = (cp_token *)&eof_token;
530 break;
531 }
532
533 if (token->type != CPP_PURGED)
534 --n;
535 }
536
537 if (cp_lexer_debugging_p (lexer))
538 {
539 cp_lexer_print_token (cp_lexer_debug_stream, token);
540 putc ('\n', cp_lexer_debug_stream);
541 }
542
543 return token;
544 }
545
546 /* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
548
549 static cp_token *
550 cp_lexer_consume_token (cp_lexer* lexer)
551 {
552 cp_token *token = lexer->next_token;
553
554 gcc_assert (token != &eof_token);
555 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
556
557 do
558 {
559 lexer->next_token++;
560 if (lexer->next_token == lexer->last_token)
561 {
562 lexer->next_token = (cp_token *)&eof_token;
563 break;
564 }
565
566 }
567 while (lexer->next_token->type == CPP_PURGED);
568
569 cp_lexer_set_source_position_from_token (token);
570
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
573 {
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, token);
576 putc ('\n', cp_lexer_debug_stream);
577 }
578
579 return token;
580 }
581
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
584 token. */
585
586 static void
587 cp_lexer_purge_token (cp_lexer *lexer)
588 {
589 cp_token *tok = lexer->next_token;
590
591 gcc_assert (tok != &eof_token);
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
596
597 do
598 {
599 tok++;
600 if (tok == lexer->last_token)
601 {
602 tok = (cp_token *)&eof_token;
603 break;
604 }
605 }
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
608 }
609
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
613
614 static void
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
616 {
617 cp_token *peek = lexer->next_token;
618
619 if (peek == &eof_token)
620 peek = lexer->last_token;
621
622 gcc_assert (tok < peek);
623
624 for ( tok += 1; tok != peek; tok += 1)
625 {
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
630 }
631 }
632
633 /* Begin saving tokens. All tokens consumed after this point will be
634 preserved. */
635
636 static void
637 cp_lexer_save_tokens (cp_lexer* lexer)
638 {
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer))
641 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
642
643 VEC_safe_push (cp_token_position, heap,
644 lexer->saved_tokens, lexer->next_token);
645 }
646
647 /* Commit to the portion of the token stream most recently saved. */
648
649 static void
650 cp_lexer_commit_tokens (cp_lexer* lexer)
651 {
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
655
656 VEC_pop (cp_token_position, lexer->saved_tokens);
657 }
658
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
661
662 static void
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
664 {
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer))
667 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
668
669 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
670 }
671
672 /* Print a representation of the TOKEN on the STREAM. */
673
674 #ifdef ENABLE_CHECKING
675
676 static void
677 cp_lexer_print_token (FILE * stream, cp_token *token)
678 {
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names[] = {
682 /* cpplib-defined token types */
683 #define OP(e, s) #e,
684 #define TK(e, s) #e,
685 TTYPE_TABLE
686 #undef OP
687 #undef TK
688 /* C++ parser token types - see "Manifest constants", above. */
689 "KEYWORD",
690 "TEMPLATE_ID",
691 "NESTED_NAME_SPECIFIER",
692 "PURGED"
693 };
694
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token->type < ARRAY_SIZE(token_names));
698 fputs (token_names[token->type], stream);
699
700 /* For some tokens, print the associated data. */
701 switch (token->type)
702 {
703 case CPP_KEYWORD:
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
707 break;
708 /* else fall through */
709 case CPP_NAME:
710 fputs (IDENTIFIER_POINTER (token->value), stream);
711 break;
712
713 case CPP_STRING:
714 case CPP_WSTRING:
715 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
716 break;
717
718 default:
719 break;
720 }
721 }
722
723 /* Start emitting debugging information. */
724
725 static void
726 cp_lexer_start_debugging (cp_lexer* lexer)
727 {
728 lexer->debugging_p = true;
729 }
730
731 /* Stop emitting debugging information. */
732
733 static void
734 cp_lexer_stop_debugging (cp_lexer* lexer)
735 {
736 lexer->debugging_p = false;
737 }
738
739 #endif /* ENABLE_CHECKING */
740
741 /* Create a new cp_token_cache, representing a range of tokens. */
742
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
745 {
746 cp_token_cache *cache = GGC_NEW (cp_token_cache);
747 cache->first = first;
748 cache->last = last;
749 return cache;
750 }
751
752 \f
753 /* Decl-specifiers. */
754
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
756
757 static void
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
759 {
760 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
761 }
762
763 /* Declarators. */
764
765 /* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
769
770 static cp_declarator *make_call_declarator
771 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773 (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775 (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777 (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779 (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781 (cp_cv_quals, tree, cp_declarator *);
782
783 /* An erroneous declarator. */
784 static cp_declarator *cp_error_declarator;
785
786 /* The obstack on which declarators and related data structures are
787 allocated. */
788 static struct obstack declarator_obstack;
789
790 /* Alloc BYTES from the declarator memory pool. */
791
792 static inline void *
793 alloc_declarator (size_t bytes)
794 {
795 return obstack_alloc (&declarator_obstack, bytes);
796 }
797
798 /* Allocate a declarator of the indicated KIND. Clear fields that are
799 common to all declarators. */
800
801 static cp_declarator *
802 make_declarator (cp_declarator_kind kind)
803 {
804 cp_declarator *declarator;
805
806 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
807 declarator->kind = kind;
808 declarator->attributes = NULL_TREE;
809 declarator->declarator = NULL;
810
811 return declarator;
812 }
813
814 /* Make a declarator for a generalized identifier. If
815 QUALIFYING_SCOPE is non-NULL, the identifier is
816 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
817 UNQUALIFIED_NAME. SFK indicates the kind of special function this
818 is, if any. */
819
820 static cp_declarator *
821 make_id_declarator (tree qualifying_scope, tree unqualified_name,
822 special_function_kind sfk)
823 {
824 cp_declarator *declarator;
825
826 /* It is valid to write:
827
828 class C { void f(); };
829 typedef C D;
830 void D::f();
831
832 The standard is not clear about whether `typedef const C D' is
833 legal; as of 2002-09-15 the committee is considering that
834 question. EDG 3.0 allows that syntax. Therefore, we do as
835 well. */
836 if (qualifying_scope && TYPE_P (qualifying_scope))
837 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
838
839 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
840 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
841 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
842
843 declarator = make_declarator (cdk_id);
844 declarator->u.id.qualifying_scope = qualifying_scope;
845 declarator->u.id.unqualified_name = unqualified_name;
846 declarator->u.id.sfk = sfk;
847
848 return declarator;
849 }
850
851 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
852 of modifiers such as const or volatile to apply to the pointer
853 type, represented as identifiers. */
854
855 cp_declarator *
856 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
857 {
858 cp_declarator *declarator;
859
860 declarator = make_declarator (cdk_pointer);
861 declarator->declarator = target;
862 declarator->u.pointer.qualifiers = cv_qualifiers;
863 declarator->u.pointer.class_type = NULL_TREE;
864
865 return declarator;
866 }
867
868 /* Like make_pointer_declarator -- but for references. */
869
870 cp_declarator *
871 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
872 {
873 cp_declarator *declarator;
874
875 declarator = make_declarator (cdk_reference);
876 declarator->declarator = target;
877 declarator->u.pointer.qualifiers = cv_qualifiers;
878 declarator->u.pointer.class_type = NULL_TREE;
879
880 return declarator;
881 }
882
883 /* Like make_pointer_declarator -- but for a pointer to a non-static
884 member of CLASS_TYPE. */
885
886 cp_declarator *
887 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
888 cp_declarator *pointee)
889 {
890 cp_declarator *declarator;
891
892 declarator = make_declarator (cdk_ptrmem);
893 declarator->declarator = pointee;
894 declarator->u.pointer.qualifiers = cv_qualifiers;
895 declarator->u.pointer.class_type = class_type;
896
897 return declarator;
898 }
899
900 /* Make a declarator for the function given by TARGET, with the
901 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
902 "const"-qualified member function. The EXCEPTION_SPECIFICATION
903 indicates what exceptions can be thrown. */
904
905 cp_declarator *
906 make_call_declarator (cp_declarator *target,
907 cp_parameter_declarator *parms,
908 cp_cv_quals cv_qualifiers,
909 tree exception_specification)
910 {
911 cp_declarator *declarator;
912
913 declarator = make_declarator (cdk_function);
914 declarator->declarator = target;
915 declarator->u.function.parameters = parms;
916 declarator->u.function.qualifiers = cv_qualifiers;
917 declarator->u.function.exception_specification = exception_specification;
918
919 return declarator;
920 }
921
922 /* Make a declarator for an array of BOUNDS elements, each of which is
923 defined by ELEMENT. */
924
925 cp_declarator *
926 make_array_declarator (cp_declarator *element, tree bounds)
927 {
928 cp_declarator *declarator;
929
930 declarator = make_declarator (cdk_array);
931 declarator->declarator = element;
932 declarator->u.array.bounds = bounds;
933
934 return declarator;
935 }
936
937 cp_parameter_declarator *no_parameters;
938
939 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
940 DECLARATOR and DEFAULT_ARGUMENT. */
941
942 cp_parameter_declarator *
943 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
944 cp_declarator *declarator,
945 tree default_argument)
946 {
947 cp_parameter_declarator *parameter;
948
949 parameter = ((cp_parameter_declarator *)
950 alloc_declarator (sizeof (cp_parameter_declarator)));
951 parameter->next = NULL;
952 if (decl_specifiers)
953 parameter->decl_specifiers = *decl_specifiers;
954 else
955 clear_decl_specs (&parameter->decl_specifiers);
956 parameter->declarator = declarator;
957 parameter->default_argument = default_argument;
958 parameter->ellipsis_p = false;
959
960 return parameter;
961 }
962
963 /* The parser. */
964
965 /* Overview
966 --------
967
968 A cp_parser parses the token stream as specified by the C++
969 grammar. Its job is purely parsing, not semantic analysis. For
970 example, the parser breaks the token stream into declarators,
971 expressions, statements, and other similar syntactic constructs.
972 It does not check that the types of the expressions on either side
973 of an assignment-statement are compatible, or that a function is
974 not declared with a parameter of type `void'.
975
976 The parser invokes routines elsewhere in the compiler to perform
977 semantic analysis and to build up the abstract syntax tree for the
978 code processed.
979
980 The parser (and the template instantiation code, which is, in a
981 way, a close relative of parsing) are the only parts of the
982 compiler that should be calling push_scope and pop_scope, or
983 related functions. The parser (and template instantiation code)
984 keeps track of what scope is presently active; everything else
985 should simply honor that. (The code that generates static
986 initializers may also need to set the scope, in order to check
987 access control correctly when emitting the initializers.)
988
989 Methodology
990 -----------
991
992 The parser is of the standard recursive-descent variety. Upcoming
993 tokens in the token stream are examined in order to determine which
994 production to use when parsing a non-terminal. Some C++ constructs
995 require arbitrary look ahead to disambiguate. For example, it is
996 impossible, in the general case, to tell whether a statement is an
997 expression or declaration without scanning the entire statement.
998 Therefore, the parser is capable of "parsing tentatively." When the
999 parser is not sure what construct comes next, it enters this mode.
1000 Then, while we attempt to parse the construct, the parser queues up
1001 error messages, rather than issuing them immediately, and saves the
1002 tokens it consumes. If the construct is parsed successfully, the
1003 parser "commits", i.e., it issues any queued error messages and
1004 the tokens that were being preserved are permanently discarded.
1005 If, however, the construct is not parsed successfully, the parser
1006 rolls back its state completely so that it can resume parsing using
1007 a different alternative.
1008
1009 Future Improvements
1010 -------------------
1011
1012 The performance of the parser could probably be improved substantially.
1013 We could often eliminate the need to parse tentatively by looking ahead
1014 a little bit. In some places, this approach might not entirely eliminate
1015 the need to parse tentatively, but it might still speed up the average
1016 case. */
1017
1018 /* Flags that are passed to some parsing functions. These values can
1019 be bitwise-ored together. */
1020
1021 typedef enum cp_parser_flags
1022 {
1023 /* No flags. */
1024 CP_PARSER_FLAGS_NONE = 0x0,
1025 /* The construct is optional. If it is not present, then no error
1026 should be issued. */
1027 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1028 /* When parsing a type-specifier, do not allow user-defined types. */
1029 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1030 } cp_parser_flags;
1031
1032 /* The different kinds of declarators we want to parse. */
1033
1034 typedef enum cp_parser_declarator_kind
1035 {
1036 /* We want an abstract declarator. */
1037 CP_PARSER_DECLARATOR_ABSTRACT,
1038 /* We want a named declarator. */
1039 CP_PARSER_DECLARATOR_NAMED,
1040 /* We don't mind, but the name must be an unqualified-id. */
1041 CP_PARSER_DECLARATOR_EITHER
1042 } cp_parser_declarator_kind;
1043
1044 /* The precedence values used to parse binary expressions. The minimum value
1045 of PREC must be 1, because zero is reserved to quickly discriminate
1046 binary operators from other tokens. */
1047
1048 enum cp_parser_prec
1049 {
1050 PREC_NOT_OPERATOR,
1051 PREC_LOGICAL_OR_EXPRESSION,
1052 PREC_LOGICAL_AND_EXPRESSION,
1053 PREC_INCLUSIVE_OR_EXPRESSION,
1054 PREC_EXCLUSIVE_OR_EXPRESSION,
1055 PREC_AND_EXPRESSION,
1056 PREC_EQUALITY_EXPRESSION,
1057 PREC_RELATIONAL_EXPRESSION,
1058 PREC_SHIFT_EXPRESSION,
1059 PREC_ADDITIVE_EXPRESSION,
1060 PREC_MULTIPLICATIVE_EXPRESSION,
1061 PREC_PM_EXPRESSION,
1062 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1063 };
1064
1065 /* A mapping from a token type to a corresponding tree node type, with a
1066 precedence value. */
1067
1068 typedef struct cp_parser_binary_operations_map_node
1069 {
1070 /* The token type. */
1071 enum cpp_ttype token_type;
1072 /* The corresponding tree code. */
1073 enum tree_code tree_type;
1074 /* The precedence of this operator. */
1075 enum cp_parser_prec prec;
1076 } cp_parser_binary_operations_map_node;
1077
1078 /* The status of a tentative parse. */
1079
1080 typedef enum cp_parser_status_kind
1081 {
1082 /* No errors have occurred. */
1083 CP_PARSER_STATUS_KIND_NO_ERROR,
1084 /* An error has occurred. */
1085 CP_PARSER_STATUS_KIND_ERROR,
1086 /* We are committed to this tentative parse, whether or not an error
1087 has occurred. */
1088 CP_PARSER_STATUS_KIND_COMMITTED
1089 } cp_parser_status_kind;
1090
1091 typedef struct cp_parser_expression_stack_entry
1092 {
1093 tree lhs;
1094 enum tree_code tree_type;
1095 int prec;
1096 } cp_parser_expression_stack_entry;
1097
1098 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1099 entries because precedence levels on the stack are monotonically
1100 increasing. */
1101 typedef struct cp_parser_expression_stack_entry
1102 cp_parser_expression_stack[NUM_PREC_VALUES];
1103
1104 /* Context that is saved and restored when parsing tentatively. */
1105 typedef struct cp_parser_context GTY (())
1106 {
1107 /* If this is a tentative parsing context, the status of the
1108 tentative parse. */
1109 enum cp_parser_status_kind status;
1110 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1111 that are looked up in this context must be looked up both in the
1112 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1113 the context of the containing expression. */
1114 tree object_type;
1115
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 /* The operator-precedence table used by cp_parser_binary_expression.
1132 Transformed into an associative array (binops_by_token) by
1133 cp_parser_new. */
1134
1135 static const cp_parser_binary_operations_map_node binops[] = {
1136 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1137 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1138
1139 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1142
1143 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1145
1146 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1148
1149 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1154 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1155
1156 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1157 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1158
1159 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1160
1161 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1162
1163 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1164
1165 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1166
1167 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1168 };
1169
1170 /* The same as binops, but initialized by cp_parser_new so that
1171 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1172 for speed. */
1173 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1174
1175 /* Constructors and destructors. */
1176
1177 /* Construct a new context. The context below this one on the stack
1178 is given by NEXT. */
1179
1180 static cp_parser_context *
1181 cp_parser_context_new (cp_parser_context* next)
1182 {
1183 cp_parser_context *context;
1184
1185 /* Allocate the storage. */
1186 if (cp_parser_context_free_list != NULL)
1187 {
1188 /* Pull the first entry from the free list. */
1189 context = cp_parser_context_free_list;
1190 cp_parser_context_free_list = context->next;
1191 memset (context, 0, sizeof (*context));
1192 }
1193 else
1194 context = GGC_CNEW (cp_parser_context);
1195
1196 /* No errors have occurred yet in this context. */
1197 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1198 /* If this is not the bottomost context, copy information that we
1199 need from the previous context. */
1200 if (next)
1201 {
1202 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203 expression, then we are parsing one in this context, too. */
1204 context->object_type = next->object_type;
1205 /* Thread the stack. */
1206 context->next = next;
1207 }
1208
1209 return context;
1210 }
1211
1212 /* The cp_parser structure represents the C++ parser. */
1213
1214 typedef struct cp_parser GTY(())
1215 {
1216 /* The lexer from which we are obtaining tokens. */
1217 cp_lexer *lexer;
1218
1219 /* The scope in which names should be looked up. If NULL_TREE, then
1220 we look up names in the scope that is currently open in the
1221 source program. If non-NULL, this is either a TYPE or
1222 NAMESPACE_DECL for the scope in which we should look. It can
1223 also be ERROR_MARK, when we've parsed a bogus scope.
1224
1225 This value is not cleared automatically after a name is looked
1226 up, so we must be careful to clear it before starting a new look
1227 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1228 will look up `Z' in the scope of `X', rather than the current
1229 scope.) Unfortunately, it is difficult to tell when name lookup
1230 is complete, because we sometimes peek at a token, look it up,
1231 and then decide not to consume it. */
1232 tree scope;
1233
1234 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1235 last lookup took place. OBJECT_SCOPE is used if an expression
1236 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1237 respectively. QUALIFYING_SCOPE is used for an expression of the
1238 form "X::Y"; it refers to X. */
1239 tree object_scope;
1240 tree qualifying_scope;
1241
1242 /* A stack of parsing contexts. All but the bottom entry on the
1243 stack will be tentative contexts.
1244
1245 We parse tentatively in order to determine which construct is in
1246 use in some situations. For example, in order to determine
1247 whether a statement is an expression-statement or a
1248 declaration-statement we parse it tentatively as a
1249 declaration-statement. If that fails, we then reparse the same
1250 token stream as an expression-statement. */
1251 cp_parser_context *context;
1252
1253 /* True if we are parsing GNU C++. If this flag is not set, then
1254 GNU extensions are not recognized. */
1255 bool allow_gnu_extensions_p;
1256
1257 /* TRUE if the `>' token should be interpreted as the greater-than
1258 operator. FALSE if it is the end of a template-id or
1259 template-parameter-list. */
1260 bool greater_than_is_operator_p;
1261
1262 /* TRUE if default arguments are allowed within a parameter list
1263 that starts at this point. FALSE if only a gnu extension makes
1264 them permissible. */
1265 bool default_arg_ok_p;
1266
1267 /* TRUE if we are parsing an integral constant-expression. See
1268 [expr.const] for a precise definition. */
1269 bool integral_constant_expression_p;
1270
1271 /* TRUE if we are parsing an integral constant-expression -- but a
1272 non-constant expression should be permitted as well. This flag
1273 is used when parsing an array bound so that GNU variable-length
1274 arrays are tolerated. */
1275 bool allow_non_integral_constant_expression_p;
1276
1277 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1278 been seen that makes the expression non-constant. */
1279 bool non_integral_constant_expression_p;
1280
1281 /* TRUE if local variable names and `this' are forbidden in the
1282 current context. */
1283 bool local_variables_forbidden_p;
1284
1285 /* TRUE if the declaration we are parsing is part of a
1286 linkage-specification of the form `extern string-literal
1287 declaration'. */
1288 bool in_unbraced_linkage_specification_p;
1289
1290 /* TRUE if we are presently parsing a declarator, after the
1291 direct-declarator. */
1292 bool in_declarator_p;
1293
1294 /* TRUE if we are presently parsing a template-argument-list. */
1295 bool in_template_argument_list_p;
1296
1297 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1298 to IN_OMP_BLOCK if parsing OpenMP structured block and
1299 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1300 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1301 iteration-statement, OpenMP block or loop within that switch. */
1302 #define IN_SWITCH_STMT 1
1303 #define IN_ITERATION_STMT 2
1304 #define IN_OMP_BLOCK 4
1305 #define IN_OMP_FOR 8
1306 unsigned char in_statement;
1307
1308 /* TRUE if we are presently parsing the body of a switch statement.
1309 Note that this doesn't quite overlap with in_statement above.
1310 The difference relates to giving the right sets of error messages:
1311 "case not in switch" vs "break statement used with OpenMP...". */
1312 bool in_switch_statement_p;
1313
1314 /* TRUE if we are parsing a type-id in an expression context. In
1315 such a situation, both "type (expr)" and "type (type)" are valid
1316 alternatives. */
1317 bool in_type_id_in_expr_p;
1318
1319 /* TRUE if we are currently in a header file where declarations are
1320 implicitly extern "C". */
1321 bool implicit_extern_c;
1322
1323 /* TRUE if strings in expressions should be translated to the execution
1324 character set. */
1325 bool translate_strings_p;
1326
1327 /* If non-NULL, then we are parsing a construct where new type
1328 definitions are not permitted. The string stored here will be
1329 issued as an error message if a type is defined. */
1330 const char *type_definition_forbidden_message;
1331
1332 /* A list of lists. The outer list is a stack, used for member
1333 functions of local classes. At each level there are two sub-list,
1334 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1335 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1336 TREE_VALUE's. The functions are chained in reverse declaration
1337 order.
1338
1339 The TREE_PURPOSE sublist contains those functions with default
1340 arguments that need post processing, and the TREE_VALUE sublist
1341 contains those functions with definitions that need post
1342 processing.
1343
1344 These lists can only be processed once the outermost class being
1345 defined is complete. */
1346 tree unparsed_functions_queues;
1347
1348 /* The number of classes whose definitions are currently in
1349 progress. */
1350 unsigned num_classes_being_defined;
1351
1352 /* The number of template parameter lists that apply directly to the
1353 current declaration. */
1354 unsigned num_template_parameter_lists;
1355 } cp_parser;
1356
1357 /* Prototypes. */
1358
1359 /* Constructors and destructors. */
1360
1361 static cp_parser *cp_parser_new
1362 (void);
1363
1364 /* Routines to parse various constructs.
1365
1366 Those that return `tree' will return the error_mark_node (rather
1367 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368 Sometimes, they will return an ordinary node if error-recovery was
1369 attempted, even though a parse error occurred. So, to check
1370 whether or not a parse error occurred, you should always use
1371 cp_parser_error_occurred. If the construct is optional (indicated
1372 either by an `_opt' in the name of the function that does the
1373 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374 the construct is not present. */
1375
1376 /* Lexical conventions [gram.lex] */
1377
1378 static tree cp_parser_identifier
1379 (cp_parser *);
1380 static tree cp_parser_string_literal
1381 (cp_parser *, bool, bool);
1382
1383 /* Basic concepts [gram.basic] */
1384
1385 static bool cp_parser_translation_unit
1386 (cp_parser *);
1387
1388 /* Expressions [gram.expr] */
1389
1390 static tree cp_parser_primary_expression
1391 (cp_parser *, bool, bool, bool, cp_id_kind *);
1392 static tree cp_parser_id_expression
1393 (cp_parser *, bool, bool, bool *, bool, bool);
1394 static tree cp_parser_unqualified_id
1395 (cp_parser *, bool, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier_opt
1397 (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_nested_name_specifier
1399 (cp_parser *, bool, bool, bool, bool);
1400 static tree cp_parser_class_or_namespace_name
1401 (cp_parser *, bool, bool, bool, bool, bool);
1402 static tree cp_parser_postfix_expression
1403 (cp_parser *, bool, bool);
1404 static tree cp_parser_postfix_open_square_expression
1405 (cp_parser *, tree, bool);
1406 static tree cp_parser_postfix_dot_deref_expression
1407 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1408 static tree cp_parser_parenthesized_expression_list
1409 (cp_parser *, bool, bool, bool *);
1410 static void cp_parser_pseudo_destructor_name
1411 (cp_parser *, tree *, tree *);
1412 static tree cp_parser_unary_expression
1413 (cp_parser *, bool, bool);
1414 static enum tree_code cp_parser_unary_operator
1415 (cp_token *);
1416 static tree cp_parser_new_expression
1417 (cp_parser *);
1418 static tree cp_parser_new_placement
1419 (cp_parser *);
1420 static tree cp_parser_new_type_id
1421 (cp_parser *, tree *);
1422 static cp_declarator *cp_parser_new_declarator_opt
1423 (cp_parser *);
1424 static cp_declarator *cp_parser_direct_new_declarator
1425 (cp_parser *);
1426 static tree cp_parser_new_initializer
1427 (cp_parser *);
1428 static tree cp_parser_delete_expression
1429 (cp_parser *);
1430 static tree cp_parser_cast_expression
1431 (cp_parser *, bool, bool);
1432 static tree cp_parser_binary_expression
1433 (cp_parser *, bool);
1434 static tree cp_parser_question_colon_clause
1435 (cp_parser *, tree);
1436 static tree cp_parser_assignment_expression
1437 (cp_parser *, bool);
1438 static enum tree_code cp_parser_assignment_operator_opt
1439 (cp_parser *);
1440 static tree cp_parser_expression
1441 (cp_parser *, bool);
1442 static tree cp_parser_constant_expression
1443 (cp_parser *, bool, bool *);
1444 static tree cp_parser_builtin_offsetof
1445 (cp_parser *);
1446
1447 /* Statements [gram.stmt.stmt] */
1448
1449 static void cp_parser_statement
1450 (cp_parser *, tree, bool);
1451 static tree cp_parser_labeled_statement
1452 (cp_parser *, tree, bool);
1453 static tree cp_parser_expression_statement
1454 (cp_parser *, tree);
1455 static tree cp_parser_compound_statement
1456 (cp_parser *, tree, bool);
1457 static void cp_parser_statement_seq_opt
1458 (cp_parser *, tree);
1459 static tree cp_parser_selection_statement
1460 (cp_parser *);
1461 static tree cp_parser_condition
1462 (cp_parser *);
1463 static tree cp_parser_iteration_statement
1464 (cp_parser *);
1465 static void cp_parser_for_init_statement
1466 (cp_parser *);
1467 static tree cp_parser_jump_statement
1468 (cp_parser *);
1469 static void cp_parser_declaration_statement
1470 (cp_parser *);
1471
1472 static tree cp_parser_implicitly_scoped_statement
1473 (cp_parser *);
1474 static void cp_parser_already_scoped_statement
1475 (cp_parser *);
1476
1477 /* Declarations [gram.dcl.dcl] */
1478
1479 static void cp_parser_declaration_seq_opt
1480 (cp_parser *);
1481 static void cp_parser_declaration
1482 (cp_parser *);
1483 static void cp_parser_block_declaration
1484 (cp_parser *, bool);
1485 static void cp_parser_simple_declaration
1486 (cp_parser *, bool);
1487 static void cp_parser_decl_specifier_seq
1488 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1489 static tree cp_parser_storage_class_specifier_opt
1490 (cp_parser *);
1491 static tree cp_parser_function_specifier_opt
1492 (cp_parser *, cp_decl_specifier_seq *);
1493 static tree cp_parser_type_specifier
1494 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1495 int *, bool *);
1496 static tree cp_parser_simple_type_specifier
1497 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1498 static tree cp_parser_type_name
1499 (cp_parser *);
1500 static tree cp_parser_elaborated_type_specifier
1501 (cp_parser *, bool, bool);
1502 static tree cp_parser_enum_specifier
1503 (cp_parser *);
1504 static void cp_parser_enumerator_list
1505 (cp_parser *, tree);
1506 static void cp_parser_enumerator_definition
1507 (cp_parser *, tree);
1508 static tree cp_parser_namespace_name
1509 (cp_parser *);
1510 static void cp_parser_namespace_definition
1511 (cp_parser *);
1512 static void cp_parser_namespace_body
1513 (cp_parser *);
1514 static tree cp_parser_qualified_namespace_specifier
1515 (cp_parser *);
1516 static void cp_parser_namespace_alias_definition
1517 (cp_parser *);
1518 static void cp_parser_using_declaration
1519 (cp_parser *);
1520 static void cp_parser_using_directive
1521 (cp_parser *);
1522 static void cp_parser_asm_definition
1523 (cp_parser *);
1524 static void cp_parser_linkage_specification
1525 (cp_parser *);
1526
1527 /* Declarators [gram.dcl.decl] */
1528
1529 static tree cp_parser_init_declarator
1530 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1531 static cp_declarator *cp_parser_declarator
1532 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1533 static cp_declarator *cp_parser_direct_declarator
1534 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1535 static enum tree_code cp_parser_ptr_operator
1536 (cp_parser *, tree *, cp_cv_quals *);
1537 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1538 (cp_parser *);
1539 static tree cp_parser_declarator_id
1540 (cp_parser *, bool);
1541 static tree cp_parser_type_id
1542 (cp_parser *);
1543 static void cp_parser_type_specifier_seq
1544 (cp_parser *, bool, cp_decl_specifier_seq *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1546 (cp_parser *);
1547 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1548 (cp_parser *, bool *);
1549 static cp_parameter_declarator *cp_parser_parameter_declaration
1550 (cp_parser *, bool, bool *);
1551 static void cp_parser_function_body
1552 (cp_parser *);
1553 static tree cp_parser_initializer
1554 (cp_parser *, bool *, bool *);
1555 static tree cp_parser_initializer_clause
1556 (cp_parser *, bool *);
1557 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1558 (cp_parser *, bool *);
1559
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1561 (cp_parser *);
1562
1563 /* Classes [gram.class] */
1564
1565 static tree cp_parser_class_name
1566 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1568 (cp_parser *);
1569 static tree cp_parser_class_head
1570 (cp_parser *, bool *, tree *);
1571 static enum tag_types cp_parser_class_key
1572 (cp_parser *);
1573 static void cp_parser_member_specification_opt
1574 (cp_parser *);
1575 static void cp_parser_member_declaration
1576 (cp_parser *);
1577 static tree cp_parser_pure_specifier
1578 (cp_parser *);
1579 static tree cp_parser_constant_initializer
1580 (cp_parser *);
1581
1582 /* Derived classes [gram.class.derived] */
1583
1584 static tree cp_parser_base_clause
1585 (cp_parser *);
1586 static tree cp_parser_base_specifier
1587 (cp_parser *);
1588
1589 /* Special member functions [gram.special] */
1590
1591 static tree cp_parser_conversion_function_id
1592 (cp_parser *);
1593 static tree cp_parser_conversion_type_id
1594 (cp_parser *);
1595 static cp_declarator *cp_parser_conversion_declarator_opt
1596 (cp_parser *);
1597 static bool cp_parser_ctor_initializer_opt
1598 (cp_parser *);
1599 static void cp_parser_mem_initializer_list
1600 (cp_parser *);
1601 static tree cp_parser_mem_initializer
1602 (cp_parser *);
1603 static tree cp_parser_mem_initializer_id
1604 (cp_parser *);
1605
1606 /* Overloading [gram.over] */
1607
1608 static tree cp_parser_operator_function_id
1609 (cp_parser *);
1610 static tree cp_parser_operator
1611 (cp_parser *);
1612
1613 /* Templates [gram.temp] */
1614
1615 static void cp_parser_template_declaration
1616 (cp_parser *, bool);
1617 static tree cp_parser_template_parameter_list
1618 (cp_parser *);
1619 static tree cp_parser_template_parameter
1620 (cp_parser *, bool *);
1621 static tree cp_parser_type_parameter
1622 (cp_parser *);
1623 static tree cp_parser_template_id
1624 (cp_parser *, bool, bool, bool);
1625 static tree cp_parser_template_name
1626 (cp_parser *, bool, bool, bool, bool *);
1627 static tree cp_parser_template_argument_list
1628 (cp_parser *);
1629 static tree cp_parser_template_argument
1630 (cp_parser *);
1631 static void cp_parser_explicit_instantiation
1632 (cp_parser *);
1633 static void cp_parser_explicit_specialization
1634 (cp_parser *);
1635
1636 /* Exception handling [gram.exception] */
1637
1638 static tree cp_parser_try_block
1639 (cp_parser *);
1640 static bool cp_parser_function_try_block
1641 (cp_parser *);
1642 static void cp_parser_handler_seq
1643 (cp_parser *);
1644 static void cp_parser_handler
1645 (cp_parser *);
1646 static tree cp_parser_exception_declaration
1647 (cp_parser *);
1648 static tree cp_parser_throw_expression
1649 (cp_parser *);
1650 static tree cp_parser_exception_specification_opt
1651 (cp_parser *);
1652 static tree cp_parser_type_id_list
1653 (cp_parser *);
1654
1655 /* GNU Extensions */
1656
1657 static tree cp_parser_asm_specification_opt
1658 (cp_parser *);
1659 static tree cp_parser_asm_operand_list
1660 (cp_parser *);
1661 static tree cp_parser_asm_clobber_list
1662 (cp_parser *);
1663 static tree cp_parser_attributes_opt
1664 (cp_parser *);
1665 static tree cp_parser_attribute_list
1666 (cp_parser *);
1667 static bool cp_parser_extension_opt
1668 (cp_parser *, int *);
1669 static void cp_parser_label_declaration
1670 (cp_parser *);
1671
1672 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1673 static bool cp_parser_pragma
1674 (cp_parser *, enum pragma_context);
1675
1676 /* Objective-C++ Productions */
1677
1678 static tree cp_parser_objc_message_receiver
1679 (cp_parser *);
1680 static tree cp_parser_objc_message_args
1681 (cp_parser *);
1682 static tree cp_parser_objc_message_expression
1683 (cp_parser *);
1684 static tree cp_parser_objc_encode_expression
1685 (cp_parser *);
1686 static tree cp_parser_objc_defs_expression
1687 (cp_parser *);
1688 static tree cp_parser_objc_protocol_expression
1689 (cp_parser *);
1690 static tree cp_parser_objc_selector_expression
1691 (cp_parser *);
1692 static tree cp_parser_objc_expression
1693 (cp_parser *);
1694 static bool cp_parser_objc_selector_p
1695 (enum cpp_ttype);
1696 static tree cp_parser_objc_selector
1697 (cp_parser *);
1698 static tree cp_parser_objc_protocol_refs_opt
1699 (cp_parser *);
1700 static void cp_parser_objc_declaration
1701 (cp_parser *);
1702 static tree cp_parser_objc_statement
1703 (cp_parser *);
1704
1705 /* Utility Routines */
1706
1707 static tree cp_parser_lookup_name
1708 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1709 static tree cp_parser_lookup_name_simple
1710 (cp_parser *, tree);
1711 static tree cp_parser_maybe_treat_template_as_class
1712 (tree, bool);
1713 static bool cp_parser_check_declarator_template_parameters
1714 (cp_parser *, cp_declarator *);
1715 static bool cp_parser_check_template_parameters
1716 (cp_parser *, unsigned);
1717 static tree cp_parser_simple_cast_expression
1718 (cp_parser *);
1719 static tree cp_parser_global_scope_opt
1720 (cp_parser *, bool);
1721 static bool cp_parser_constructor_declarator_p
1722 (cp_parser *, bool);
1723 static tree cp_parser_function_definition_from_specifiers_and_declarator
1724 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1725 static tree cp_parser_function_definition_after_declarator
1726 (cp_parser *, bool);
1727 static void cp_parser_template_declaration_after_export
1728 (cp_parser *, bool);
1729 static void cp_parser_perform_template_parameter_access_checks
1730 (tree);
1731 static tree cp_parser_single_declaration
1732 (cp_parser *, tree, bool, bool *);
1733 static tree cp_parser_functional_cast
1734 (cp_parser *, tree);
1735 static tree cp_parser_save_member_function_body
1736 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1737 static tree cp_parser_enclosed_template_argument_list
1738 (cp_parser *);
1739 static void cp_parser_save_default_args
1740 (cp_parser *, tree);
1741 static void cp_parser_late_parsing_for_member
1742 (cp_parser *, tree);
1743 static void cp_parser_late_parsing_default_args
1744 (cp_parser *, tree);
1745 static tree cp_parser_sizeof_operand
1746 (cp_parser *, enum rid);
1747 static bool cp_parser_declares_only_class_p
1748 (cp_parser *);
1749 static void cp_parser_set_storage_class
1750 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1751 static void cp_parser_set_decl_spec_type
1752 (cp_decl_specifier_seq *, tree, bool);
1753 static bool cp_parser_friend_p
1754 (const cp_decl_specifier_seq *);
1755 static cp_token *cp_parser_require
1756 (cp_parser *, enum cpp_ttype, const char *);
1757 static cp_token *cp_parser_require_keyword
1758 (cp_parser *, enum rid, const char *);
1759 static bool cp_parser_token_starts_function_definition_p
1760 (cp_token *);
1761 static bool cp_parser_next_token_starts_class_definition_p
1762 (cp_parser *);
1763 static bool cp_parser_next_token_ends_template_argument_p
1764 (cp_parser *);
1765 static bool cp_parser_nth_token_starts_template_argument_list_p
1766 (cp_parser *, size_t);
1767 static enum tag_types cp_parser_token_is_class_key
1768 (cp_token *);
1769 static void cp_parser_check_class_key
1770 (enum tag_types, tree type);
1771 static void cp_parser_check_access_in_redeclaration
1772 (tree type);
1773 static bool cp_parser_optional_template_keyword
1774 (cp_parser *);
1775 static void cp_parser_pre_parsed_nested_name_specifier
1776 (cp_parser *);
1777 static void cp_parser_cache_group
1778 (cp_parser *, enum cpp_ttype, unsigned);
1779 static void cp_parser_parse_tentatively
1780 (cp_parser *);
1781 static void cp_parser_commit_to_tentative_parse
1782 (cp_parser *);
1783 static void cp_parser_abort_tentative_parse
1784 (cp_parser *);
1785 static bool cp_parser_parse_definitely
1786 (cp_parser *);
1787 static inline bool cp_parser_parsing_tentatively
1788 (cp_parser *);
1789 static bool cp_parser_uncommitted_to_tentative_parse_p
1790 (cp_parser *);
1791 static void cp_parser_error
1792 (cp_parser *, const char *);
1793 static void cp_parser_name_lookup_error
1794 (cp_parser *, tree, tree, const char *);
1795 static bool cp_parser_simulate_error
1796 (cp_parser *);
1797 static void cp_parser_check_type_definition
1798 (cp_parser *);
1799 static void cp_parser_check_for_definition_in_return_type
1800 (cp_declarator *, tree);
1801 static void cp_parser_check_for_invalid_template_id
1802 (cp_parser *, tree);
1803 static bool cp_parser_non_integral_constant_expression
1804 (cp_parser *, const char *);
1805 static void cp_parser_diagnose_invalid_type_name
1806 (cp_parser *, tree, tree);
1807 static bool cp_parser_parse_and_diagnose_invalid_type_name
1808 (cp_parser *);
1809 static int cp_parser_skip_to_closing_parenthesis
1810 (cp_parser *, bool, bool, bool);
1811 static void cp_parser_skip_to_end_of_statement
1812 (cp_parser *);
1813 static void cp_parser_consume_semicolon_at_end_of_statement
1814 (cp_parser *);
1815 static void cp_parser_skip_to_end_of_block_or_statement
1816 (cp_parser *);
1817 static void cp_parser_skip_to_closing_brace
1818 (cp_parser *);
1819 static void cp_parser_skip_until_found
1820 (cp_parser *, enum cpp_ttype, const char *);
1821 static void cp_parser_skip_to_pragma_eol
1822 (cp_parser*, cp_token *);
1823 static bool cp_parser_error_occurred
1824 (cp_parser *);
1825 static bool cp_parser_allow_gnu_extensions_p
1826 (cp_parser *);
1827 static bool cp_parser_is_string_literal
1828 (cp_token *);
1829 static bool cp_parser_is_keyword
1830 (cp_token *, enum rid);
1831 static tree cp_parser_make_typename_type
1832 (cp_parser *, tree, tree);
1833
1834 /* Returns nonzero if we are parsing tentatively. */
1835
1836 static inline bool
1837 cp_parser_parsing_tentatively (cp_parser* parser)
1838 {
1839 return parser->context->next != NULL;
1840 }
1841
1842 /* Returns nonzero if TOKEN is a string literal. */
1843
1844 static bool
1845 cp_parser_is_string_literal (cp_token* token)
1846 {
1847 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 }
1849
1850 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1851
1852 static bool
1853 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1854 {
1855 return token->keyword == keyword;
1856 }
1857
1858 /* A minimum or maximum operator has been seen. As these are
1859 deprecated, issue a warning. */
1860
1861 static inline void
1862 cp_parser_warn_min_max (void)
1863 {
1864 if (warn_deprecated && !in_system_header)
1865 warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1866 }
1867
1868 /* If not parsing tentatively, issue a diagnostic of the form
1869 FILE:LINE: MESSAGE before TOKEN
1870 where TOKEN is the next token in the input stream. MESSAGE
1871 (specified by the caller) is usually of the form "expected
1872 OTHER-TOKEN". */
1873
1874 static void
1875 cp_parser_error (cp_parser* parser, const char* message)
1876 {
1877 if (!cp_parser_simulate_error (parser))
1878 {
1879 cp_token *token = cp_lexer_peek_token (parser->lexer);
1880 /* This diagnostic makes more sense if it is tagged to the line
1881 of the token we just peeked at. */
1882 cp_lexer_set_source_position_from_token (token);
1883
1884 if (token->type == CPP_PRAGMA)
1885 {
1886 error ("%<#pragma%> is not allowed here");
1887 cp_parser_skip_to_pragma_eol (parser, token);
1888 return;
1889 }
1890
1891 c_parse_error (message,
1892 /* Because c_parser_error does not understand
1893 CPP_KEYWORD, keywords are treated like
1894 identifiers. */
1895 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1896 token->value);
1897 }
1898 }
1899
1900 /* Issue an error about name-lookup failing. NAME is the
1901 IDENTIFIER_NODE DECL is the result of
1902 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1903 the thing that we hoped to find. */
1904
1905 static void
1906 cp_parser_name_lookup_error (cp_parser* parser,
1907 tree name,
1908 tree decl,
1909 const char* desired)
1910 {
1911 /* If name lookup completely failed, tell the user that NAME was not
1912 declared. */
1913 if (decl == error_mark_node)
1914 {
1915 if (parser->scope && parser->scope != global_namespace)
1916 error ("%<%D::%D%> has not been declared",
1917 parser->scope, name);
1918 else if (parser->scope == global_namespace)
1919 error ("%<::%D%> has not been declared", name);
1920 else if (parser->object_scope
1921 && !CLASS_TYPE_P (parser->object_scope))
1922 error ("request for member %qD in non-class type %qT",
1923 name, parser->object_scope);
1924 else if (parser->object_scope)
1925 error ("%<%T::%D%> has not been declared",
1926 parser->object_scope, name);
1927 else
1928 error ("%qD has not been declared", name);
1929 }
1930 else if (parser->scope && parser->scope != global_namespace)
1931 error ("%<%D::%D%> %s", parser->scope, name, desired);
1932 else if (parser->scope == global_namespace)
1933 error ("%<::%D%> %s", name, desired);
1934 else
1935 error ("%qD %s", name, desired);
1936 }
1937
1938 /* If we are parsing tentatively, remember that an error has occurred
1939 during this tentative parse. Returns true if the error was
1940 simulated; false if a message should be issued by the caller. */
1941
1942 static bool
1943 cp_parser_simulate_error (cp_parser* parser)
1944 {
1945 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1946 {
1947 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1948 return true;
1949 }
1950 return false;
1951 }
1952
1953 /* Check for repeated decl-specifiers. */
1954
1955 static void
1956 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1957 {
1958 cp_decl_spec ds;
1959
1960 for (ds = ds_first; ds != ds_last; ++ds)
1961 {
1962 unsigned count = decl_specs->specs[(int)ds];
1963 if (count < 2)
1964 continue;
1965 /* The "long" specifier is a special case because of "long long". */
1966 if (ds == ds_long)
1967 {
1968 if (count > 2)
1969 error ("%<long long long%> is too long for GCC");
1970 else if (pedantic && !in_system_header && warn_long_long)
1971 pedwarn ("ISO C++ does not support %<long long%>");
1972 }
1973 else if (count > 1)
1974 {
1975 static const char *const decl_spec_names[] = {
1976 "signed",
1977 "unsigned",
1978 "short",
1979 "long",
1980 "const",
1981 "volatile",
1982 "restrict",
1983 "inline",
1984 "virtual",
1985 "explicit",
1986 "friend",
1987 "typedef",
1988 "__complex",
1989 "__thread"
1990 };
1991 error ("duplicate %qs", decl_spec_names[(int)ds]);
1992 }
1993 }
1994 }
1995
1996 /* This function is called when a type is defined. If type
1997 definitions are forbidden at this point, an error message is
1998 issued. */
1999
2000 static void
2001 cp_parser_check_type_definition (cp_parser* parser)
2002 {
2003 /* If types are forbidden here, issue a message. */
2004 if (parser->type_definition_forbidden_message)
2005 /* Use `%s' to print the string in case there are any escape
2006 characters in the message. */
2007 error ("%s", parser->type_definition_forbidden_message);
2008 }
2009
2010 /* This function is called when the DECLARATOR is processed. The TYPE
2011 was a type defined in the decl-specifiers. If it is invalid to
2012 define a type in the decl-specifiers for DECLARATOR, an error is
2013 issued. */
2014
2015 static void
2016 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2017 tree type)
2018 {
2019 /* [dcl.fct] forbids type definitions in return types.
2020 Unfortunately, it's not easy to know whether or not we are
2021 processing a return type until after the fact. */
2022 while (declarator
2023 && (declarator->kind == cdk_pointer
2024 || declarator->kind == cdk_reference
2025 || declarator->kind == cdk_ptrmem))
2026 declarator = declarator->declarator;
2027 if (declarator
2028 && declarator->kind == cdk_function)
2029 {
2030 error ("new types may not be defined in a return type");
2031 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2032 type);
2033 }
2034 }
2035
2036 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2037 "<" in any valid C++ program. If the next token is indeed "<",
2038 issue a message warning the user about what appears to be an
2039 invalid attempt to form a template-id. */
2040
2041 static void
2042 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2043 tree type)
2044 {
2045 cp_token_position start = 0;
2046
2047 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2048 {
2049 if (TYPE_P (type))
2050 error ("%qT is not a template", type);
2051 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2052 error ("%qE is not a template", type);
2053 else
2054 error ("invalid template-id");
2055 /* Remember the location of the invalid "<". */
2056 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2057 start = cp_lexer_token_position (parser->lexer, true);
2058 /* Consume the "<". */
2059 cp_lexer_consume_token (parser->lexer);
2060 /* Parse the template arguments. */
2061 cp_parser_enclosed_template_argument_list (parser);
2062 /* Permanently remove the invalid template arguments so that
2063 this error message is not issued again. */
2064 if (start)
2065 cp_lexer_purge_tokens_after (parser->lexer, start);
2066 }
2067 }
2068
2069 /* If parsing an integral constant-expression, issue an error message
2070 about the fact that THING appeared and return true. Otherwise,
2071 return false. In either case, set
2072 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2073
2074 static bool
2075 cp_parser_non_integral_constant_expression (cp_parser *parser,
2076 const char *thing)
2077 {
2078 parser->non_integral_constant_expression_p = true;
2079 if (parser->integral_constant_expression_p)
2080 {
2081 if (!parser->allow_non_integral_constant_expression_p)
2082 {
2083 error ("%s cannot appear in a constant-expression", thing);
2084 return true;
2085 }
2086 }
2087 return false;
2088 }
2089
2090 /* Emit a diagnostic for an invalid type name. SCOPE is the
2091 qualifying scope (or NULL, if none) for ID. This function commits
2092 to the current active tentative parse, if any. (Otherwise, the
2093 problematic construct might be encountered again later, resulting
2094 in duplicate error messages.) */
2095
2096 static void
2097 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2098 {
2099 tree decl, old_scope;
2100 /* Try to lookup the identifier. */
2101 old_scope = parser->scope;
2102 parser->scope = scope;
2103 decl = cp_parser_lookup_name_simple (parser, id);
2104 parser->scope = old_scope;
2105 /* If the lookup found a template-name, it means that the user forgot
2106 to specify an argument list. Emit a useful error message. */
2107 if (TREE_CODE (decl) == TEMPLATE_DECL)
2108 error ("invalid use of template-name %qE without an argument list",
2109 decl);
2110 else if (!parser->scope)
2111 {
2112 /* Issue an error message. */
2113 error ("%qE does not name a type", id);
2114 /* If we're in a template class, it's possible that the user was
2115 referring to a type from a base class. For example:
2116
2117 template <typename T> struct A { typedef T X; };
2118 template <typename T> struct B : public A<T> { X x; };
2119
2120 The user should have said "typename A<T>::X". */
2121 if (processing_template_decl && current_class_type
2122 && TYPE_BINFO (current_class_type))
2123 {
2124 tree b;
2125
2126 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2127 b;
2128 b = TREE_CHAIN (b))
2129 {
2130 tree base_type = BINFO_TYPE (b);
2131 if (CLASS_TYPE_P (base_type)
2132 && dependent_type_p (base_type))
2133 {
2134 tree field;
2135 /* Go from a particular instantiation of the
2136 template (which will have an empty TYPE_FIELDs),
2137 to the main version. */
2138 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2139 for (field = TYPE_FIELDS (base_type);
2140 field;
2141 field = TREE_CHAIN (field))
2142 if (TREE_CODE (field) == TYPE_DECL
2143 && DECL_NAME (field) == id)
2144 {
2145 inform ("(perhaps %<typename %T::%E%> was intended)",
2146 BINFO_TYPE (b), id);
2147 break;
2148 }
2149 if (field)
2150 break;
2151 }
2152 }
2153 }
2154 }
2155 /* Here we diagnose qualified-ids where the scope is actually correct,
2156 but the identifier does not resolve to a valid type name. */
2157 else if (parser->scope != error_mark_node)
2158 {
2159 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2160 error ("%qE in namespace %qE does not name a type",
2161 id, parser->scope);
2162 else if (TYPE_P (parser->scope))
2163 error ("%qE in class %qT does not name a type", id, parser->scope);
2164 else
2165 gcc_unreachable ();
2166 }
2167 cp_parser_commit_to_tentative_parse (parser);
2168 }
2169
2170 /* Check for a common situation where a type-name should be present,
2171 but is not, and issue a sensible error message. Returns true if an
2172 invalid type-name was detected.
2173
2174 The situation handled by this function are variable declarations of the
2175 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2176 Usually, `ID' should name a type, but if we got here it means that it
2177 does not. We try to emit the best possible error message depending on
2178 how exactly the id-expression looks like. */
2179
2180 static bool
2181 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2182 {
2183 tree id;
2184
2185 cp_parser_parse_tentatively (parser);
2186 id = cp_parser_id_expression (parser,
2187 /*template_keyword_p=*/false,
2188 /*check_dependency_p=*/true,
2189 /*template_p=*/NULL,
2190 /*declarator_p=*/true,
2191 /*optional_p=*/false);
2192 /* After the id-expression, there should be a plain identifier,
2193 otherwise this is not a simple variable declaration. Also, if
2194 the scope is dependent, we cannot do much. */
2195 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2196 || (parser->scope && TYPE_P (parser->scope)
2197 && dependent_type_p (parser->scope)))
2198 {
2199 cp_parser_abort_tentative_parse (parser);
2200 return false;
2201 }
2202 if (!cp_parser_parse_definitely (parser)
2203 || TREE_CODE (id) != IDENTIFIER_NODE)
2204 return false;
2205
2206 /* Emit a diagnostic for the invalid type. */
2207 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2208 /* Skip to the end of the declaration; there's no point in
2209 trying to process it. */
2210 cp_parser_skip_to_end_of_block_or_statement (parser);
2211 return true;
2212 }
2213
2214 /* Consume tokens up to, and including, the next non-nested closing `)'.
2215 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2216 are doing error recovery. Returns -1 if OR_COMMA is true and we
2217 found an unnested comma. */
2218
2219 static int
2220 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2221 bool recovering,
2222 bool or_comma,
2223 bool consume_paren)
2224 {
2225 unsigned paren_depth = 0;
2226 unsigned brace_depth = 0;
2227
2228 if (recovering && !or_comma
2229 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2230 return 0;
2231
2232 while (true)
2233 {
2234 cp_token * token = cp_lexer_peek_token (parser->lexer);
2235
2236 switch (token->type)
2237 {
2238 case CPP_EOF:
2239 case CPP_PRAGMA_EOL:
2240 /* If we've run out of tokens, then there is no closing `)'. */
2241 return 0;
2242
2243 case CPP_SEMICOLON:
2244 /* This matches the processing in skip_to_end_of_statement. */
2245 if (!brace_depth)
2246 return 0;
2247 break;
2248
2249 case CPP_OPEN_BRACE:
2250 ++brace_depth;
2251 break;
2252 case CPP_CLOSE_BRACE:
2253 if (!brace_depth--)
2254 return 0;
2255 break;
2256
2257 case CPP_COMMA:
2258 if (recovering && or_comma && !brace_depth && !paren_depth)
2259 return -1;
2260 break;
2261
2262 case CPP_OPEN_PAREN:
2263 if (!brace_depth)
2264 ++paren_depth;
2265 break;
2266
2267 case CPP_CLOSE_PAREN:
2268 if (!brace_depth && !paren_depth--)
2269 {
2270 if (consume_paren)
2271 cp_lexer_consume_token (parser->lexer);
2272 return 1;
2273 }
2274 break;
2275
2276 default:
2277 break;
2278 }
2279
2280 /* Consume the token. */
2281 cp_lexer_consume_token (parser->lexer);
2282 }
2283 }
2284
2285 /* Consume tokens until we reach the end of the current statement.
2286 Normally, that will be just before consuming a `;'. However, if a
2287 non-nested `}' comes first, then we stop before consuming that. */
2288
2289 static void
2290 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2291 {
2292 unsigned nesting_depth = 0;
2293
2294 while (true)
2295 {
2296 cp_token *token = cp_lexer_peek_token (parser->lexer);
2297
2298 switch (token->type)
2299 {
2300 case CPP_EOF:
2301 case CPP_PRAGMA_EOL:
2302 /* If we've run out of tokens, stop. */
2303 return;
2304
2305 case CPP_SEMICOLON:
2306 /* If the next token is a `;', we have reached the end of the
2307 statement. */
2308 if (!nesting_depth)
2309 return;
2310 break;
2311
2312 case CPP_CLOSE_BRACE:
2313 /* If this is a non-nested '}', stop before consuming it.
2314 That way, when confronted with something like:
2315
2316 { 3 + }
2317
2318 we stop before consuming the closing '}', even though we
2319 have not yet reached a `;'. */
2320 if (nesting_depth == 0)
2321 return;
2322
2323 /* If it is the closing '}' for a block that we have
2324 scanned, stop -- but only after consuming the token.
2325 That way given:
2326
2327 void f g () { ... }
2328 typedef int I;
2329
2330 we will stop after the body of the erroneously declared
2331 function, but before consuming the following `typedef'
2332 declaration. */
2333 if (--nesting_depth == 0)
2334 {
2335 cp_lexer_consume_token (parser->lexer);
2336 return;
2337 }
2338
2339 case CPP_OPEN_BRACE:
2340 ++nesting_depth;
2341 break;
2342
2343 default:
2344 break;
2345 }
2346
2347 /* Consume the token. */
2348 cp_lexer_consume_token (parser->lexer);
2349 }
2350 }
2351
2352 /* This function is called at the end of a statement or declaration.
2353 If the next token is a semicolon, it is consumed; otherwise, error
2354 recovery is attempted. */
2355
2356 static void
2357 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2358 {
2359 /* Look for the trailing `;'. */
2360 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2361 {
2362 /* If there is additional (erroneous) input, skip to the end of
2363 the statement. */
2364 cp_parser_skip_to_end_of_statement (parser);
2365 /* If the next token is now a `;', consume it. */
2366 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2367 cp_lexer_consume_token (parser->lexer);
2368 }
2369 }
2370
2371 /* Skip tokens until we have consumed an entire block, or until we
2372 have consumed a non-nested `;'. */
2373
2374 static void
2375 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2376 {
2377 int nesting_depth = 0;
2378
2379 while (nesting_depth >= 0)
2380 {
2381 cp_token *token = cp_lexer_peek_token (parser->lexer);
2382
2383 switch (token->type)
2384 {
2385 case CPP_EOF:
2386 case CPP_PRAGMA_EOL:
2387 /* If we've run out of tokens, stop. */
2388 return;
2389
2390 case CPP_SEMICOLON:
2391 /* Stop if this is an unnested ';'. */
2392 if (!nesting_depth)
2393 nesting_depth = -1;
2394 break;
2395
2396 case CPP_CLOSE_BRACE:
2397 /* Stop if this is an unnested '}', or closes the outermost
2398 nesting level. */
2399 nesting_depth--;
2400 if (!nesting_depth)
2401 nesting_depth = -1;
2402 break;
2403
2404 case CPP_OPEN_BRACE:
2405 /* Nest. */
2406 nesting_depth++;
2407 break;
2408
2409 default:
2410 break;
2411 }
2412
2413 /* Consume the token. */
2414 cp_lexer_consume_token (parser->lexer);
2415 }
2416 }
2417
2418 /* Skip tokens until a non-nested closing curly brace is the next
2419 token. */
2420
2421 static void
2422 cp_parser_skip_to_closing_brace (cp_parser *parser)
2423 {
2424 unsigned nesting_depth = 0;
2425
2426 while (true)
2427 {
2428 cp_token *token = cp_lexer_peek_token (parser->lexer);
2429
2430 switch (token->type)
2431 {
2432 case CPP_EOF:
2433 case CPP_PRAGMA_EOL:
2434 /* If we've run out of tokens, stop. */
2435 return;
2436
2437 case CPP_CLOSE_BRACE:
2438 /* If the next token is a non-nested `}', then we have reached
2439 the end of the current block. */
2440 if (nesting_depth-- == 0)
2441 return;
2442 break;
2443
2444 case CPP_OPEN_BRACE:
2445 /* If it the next token is a `{', then we are entering a new
2446 block. Consume the entire block. */
2447 ++nesting_depth;
2448 break;
2449
2450 default:
2451 break;
2452 }
2453
2454 /* Consume the token. */
2455 cp_lexer_consume_token (parser->lexer);
2456 }
2457 }
2458
2459 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2460 parameter is the PRAGMA token, allowing us to purge the entire pragma
2461 sequence. */
2462
2463 static void
2464 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2465 {
2466 cp_token *token;
2467
2468 parser->lexer->in_pragma = false;
2469
2470 do
2471 token = cp_lexer_consume_token (parser->lexer);
2472 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2473
2474 /* Ensure that the pragma is not parsed again. */
2475 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2476 }
2477
2478 /* Require pragma end of line, resyncing with it as necessary. The
2479 arguments are as for cp_parser_skip_to_pragma_eol. */
2480
2481 static void
2482 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2483 {
2484 parser->lexer->in_pragma = false;
2485 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2487 }
2488
2489 /* This is a simple wrapper around make_typename_type. When the id is
2490 an unresolved identifier node, we can provide a superior diagnostic
2491 using cp_parser_diagnose_invalid_type_name. */
2492
2493 static tree
2494 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2495 {
2496 tree result;
2497 if (TREE_CODE (id) == IDENTIFIER_NODE)
2498 {
2499 result = make_typename_type (scope, id, typename_type,
2500 /*complain=*/tf_none);
2501 if (result == error_mark_node)
2502 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2503 return result;
2504 }
2505 return make_typename_type (scope, id, typename_type, tf_error);
2506 }
2507
2508
2509 /* Create a new C++ parser. */
2510
2511 static cp_parser *
2512 cp_parser_new (void)
2513 {
2514 cp_parser *parser;
2515 cp_lexer *lexer;
2516 unsigned i;
2517
2518 /* cp_lexer_new_main is called before calling ggc_alloc because
2519 cp_lexer_new_main might load a PCH file. */
2520 lexer = cp_lexer_new_main ();
2521
2522 /* Initialize the binops_by_token so that we can get the tree
2523 directly from the token. */
2524 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2525 binops_by_token[binops[i].token_type] = binops[i];
2526
2527 parser = GGC_CNEW (cp_parser);
2528 parser->lexer = lexer;
2529 parser->context = cp_parser_context_new (NULL);
2530
2531 /* For now, we always accept GNU extensions. */
2532 parser->allow_gnu_extensions_p = 1;
2533
2534 /* The `>' token is a greater-than operator, not the end of a
2535 template-id. */
2536 parser->greater_than_is_operator_p = true;
2537
2538 parser->default_arg_ok_p = true;
2539
2540 /* We are not parsing a constant-expression. */
2541 parser->integral_constant_expression_p = false;
2542 parser->allow_non_integral_constant_expression_p = false;
2543 parser->non_integral_constant_expression_p = false;
2544
2545 /* Local variable names are not forbidden. */
2546 parser->local_variables_forbidden_p = false;
2547
2548 /* We are not processing an `extern "C"' declaration. */
2549 parser->in_unbraced_linkage_specification_p = false;
2550
2551 /* We are not processing a declarator. */
2552 parser->in_declarator_p = false;
2553
2554 /* We are not processing a template-argument-list. */
2555 parser->in_template_argument_list_p = false;
2556
2557 /* We are not in an iteration statement. */
2558 parser->in_statement = 0;
2559
2560 /* We are not in a switch statement. */
2561 parser->in_switch_statement_p = false;
2562
2563 /* We are not parsing a type-id inside an expression. */
2564 parser->in_type_id_in_expr_p = false;
2565
2566 /* Declarations aren't implicitly extern "C". */
2567 parser->implicit_extern_c = false;
2568
2569 /* String literals should be translated to the execution character set. */
2570 parser->translate_strings_p = true;
2571
2572 /* The unparsed function queue is empty. */
2573 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2574
2575 /* There are no classes being defined. */
2576 parser->num_classes_being_defined = 0;
2577
2578 /* No template parameters apply. */
2579 parser->num_template_parameter_lists = 0;
2580
2581 return parser;
2582 }
2583
2584 /* Create a cp_lexer structure which will emit the tokens in CACHE
2585 and push it onto the parser's lexer stack. This is used for delayed
2586 parsing of in-class method bodies and default arguments, and should
2587 not be confused with tentative parsing. */
2588 static void
2589 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2590 {
2591 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2592 lexer->next = parser->lexer;
2593 parser->lexer = lexer;
2594
2595 /* Move the current source position to that of the first token in the
2596 new lexer. */
2597 cp_lexer_set_source_position_from_token (lexer->next_token);
2598 }
2599
2600 /* Pop the top lexer off the parser stack. This is never used for the
2601 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2602 static void
2603 cp_parser_pop_lexer (cp_parser *parser)
2604 {
2605 cp_lexer *lexer = parser->lexer;
2606 parser->lexer = lexer->next;
2607 cp_lexer_destroy (lexer);
2608
2609 /* Put the current source position back where it was before this
2610 lexer was pushed. */
2611 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2612 }
2613
2614 /* Lexical conventions [gram.lex] */
2615
2616 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2617 identifier. */
2618
2619 static tree
2620 cp_parser_identifier (cp_parser* parser)
2621 {
2622 cp_token *token;
2623
2624 /* Look for the identifier. */
2625 token = cp_parser_require (parser, CPP_NAME, "identifier");
2626 /* Return the value. */
2627 return token ? token->value : error_mark_node;
2628 }
2629
2630 /* Parse a sequence of adjacent string constants. Returns a
2631 TREE_STRING representing the combined, nul-terminated string
2632 constant. If TRANSLATE is true, translate the string to the
2633 execution character set. If WIDE_OK is true, a wide string is
2634 invalid here.
2635
2636 C++98 [lex.string] says that if a narrow string literal token is
2637 adjacent to a wide string literal token, the behavior is undefined.
2638 However, C99 6.4.5p4 says that this results in a wide string literal.
2639 We follow C99 here, for consistency with the C front end.
2640
2641 This code is largely lifted from lex_string() in c-lex.c.
2642
2643 FUTURE: ObjC++ will need to handle @-strings here. */
2644 static tree
2645 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2646 {
2647 tree value;
2648 bool wide = false;
2649 size_t count;
2650 struct obstack str_ob;
2651 cpp_string str, istr, *strs;
2652 cp_token *tok;
2653
2654 tok = cp_lexer_peek_token (parser->lexer);
2655 if (!cp_parser_is_string_literal (tok))
2656 {
2657 cp_parser_error (parser, "expected string-literal");
2658 return error_mark_node;
2659 }
2660
2661 /* Try to avoid the overhead of creating and destroying an obstack
2662 for the common case of just one string. */
2663 if (!cp_parser_is_string_literal
2664 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2665 {
2666 cp_lexer_consume_token (parser->lexer);
2667
2668 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2669 str.len = TREE_STRING_LENGTH (tok->value);
2670 count = 1;
2671 if (tok->type == CPP_WSTRING)
2672 wide = true;
2673
2674 strs = &str;
2675 }
2676 else
2677 {
2678 gcc_obstack_init (&str_ob);
2679 count = 0;
2680
2681 do
2682 {
2683 cp_lexer_consume_token (parser->lexer);
2684 count++;
2685 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2686 str.len = TREE_STRING_LENGTH (tok->value);
2687 if (tok->type == CPP_WSTRING)
2688 wide = true;
2689
2690 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2691
2692 tok = cp_lexer_peek_token (parser->lexer);
2693 }
2694 while (cp_parser_is_string_literal (tok));
2695
2696 strs = (cpp_string *) obstack_finish (&str_ob);
2697 }
2698
2699 if (wide && !wide_ok)
2700 {
2701 cp_parser_error (parser, "a wide string is invalid in this context");
2702 wide = false;
2703 }
2704
2705 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2706 (parse_in, strs, count, &istr, wide))
2707 {
2708 value = build_string (istr.len, (char *)istr.text);
2709 free ((void *)istr.text);
2710
2711 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2712 value = fix_string_type (value);
2713 }
2714 else
2715 /* cpp_interpret_string has issued an error. */
2716 value = error_mark_node;
2717
2718 if (count > 1)
2719 obstack_free (&str_ob, 0);
2720
2721 return value;
2722 }
2723
2724
2725 /* Basic concepts [gram.basic] */
2726
2727 /* Parse a translation-unit.
2728
2729 translation-unit:
2730 declaration-seq [opt]
2731
2732 Returns TRUE if all went well. */
2733
2734 static bool
2735 cp_parser_translation_unit (cp_parser* parser)
2736 {
2737 /* The address of the first non-permanent object on the declarator
2738 obstack. */
2739 static void *declarator_obstack_base;
2740
2741 bool success;
2742
2743 /* Create the declarator obstack, if necessary. */
2744 if (!cp_error_declarator)
2745 {
2746 gcc_obstack_init (&declarator_obstack);
2747 /* Create the error declarator. */
2748 cp_error_declarator = make_declarator (cdk_error);
2749 /* Create the empty parameter list. */
2750 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2751 /* Remember where the base of the declarator obstack lies. */
2752 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2753 }
2754
2755 cp_parser_declaration_seq_opt (parser);
2756
2757 /* If there are no tokens left then all went well. */
2758 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2759 {
2760 /* Get rid of the token array; we don't need it any more. */
2761 cp_lexer_destroy (parser->lexer);
2762 parser->lexer = NULL;
2763
2764 /* This file might have been a context that's implicitly extern
2765 "C". If so, pop the lang context. (Only relevant for PCH.) */
2766 if (parser->implicit_extern_c)
2767 {
2768 pop_lang_context ();
2769 parser->implicit_extern_c = false;
2770 }
2771
2772 /* Finish up. */
2773 finish_translation_unit ();
2774
2775 success = true;
2776 }
2777 else
2778 {
2779 cp_parser_error (parser, "expected declaration");
2780 success = false;
2781 }
2782
2783 /* Make sure the declarator obstack was fully cleaned up. */
2784 gcc_assert (obstack_next_free (&declarator_obstack)
2785 == declarator_obstack_base);
2786
2787 /* All went well. */
2788 return success;
2789 }
2790
2791 /* Expressions [gram.expr] */
2792
2793 /* Parse a primary-expression.
2794
2795 primary-expression:
2796 literal
2797 this
2798 ( expression )
2799 id-expression
2800
2801 GNU Extensions:
2802
2803 primary-expression:
2804 ( compound-statement )
2805 __builtin_va_arg ( assignment-expression , type-id )
2806 __builtin_offsetof ( type-id , offsetof-expression )
2807
2808 Objective-C++ Extension:
2809
2810 primary-expression:
2811 objc-expression
2812
2813 literal:
2814 __null
2815
2816 ADDRESS_P is true iff this expression was immediately preceded by
2817 "&" and therefore might denote a pointer-to-member. CAST_P is true
2818 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2819 true iff this expression is a template argument.
2820
2821 Returns a representation of the expression. Upon return, *IDK
2822 indicates what kind of id-expression (if any) was present. */
2823
2824 static tree
2825 cp_parser_primary_expression (cp_parser *parser,
2826 bool address_p,
2827 bool cast_p,
2828 bool template_arg_p,
2829 cp_id_kind *idk)
2830 {
2831 cp_token *token;
2832
2833 /* Assume the primary expression is not an id-expression. */
2834 *idk = CP_ID_KIND_NONE;
2835
2836 /* Peek at the next token. */
2837 token = cp_lexer_peek_token (parser->lexer);
2838 switch (token->type)
2839 {
2840 /* literal:
2841 integer-literal
2842 character-literal
2843 floating-literal
2844 string-literal
2845 boolean-literal */
2846 case CPP_CHAR:
2847 case CPP_WCHAR:
2848 case CPP_NUMBER:
2849 token = cp_lexer_consume_token (parser->lexer);
2850 /* Floating-point literals are only allowed in an integral
2851 constant expression if they are cast to an integral or
2852 enumeration type. */
2853 if (TREE_CODE (token->value) == REAL_CST
2854 && parser->integral_constant_expression_p
2855 && pedantic)
2856 {
2857 /* CAST_P will be set even in invalid code like "int(2.7 +
2858 ...)". Therefore, we have to check that the next token
2859 is sure to end the cast. */
2860 if (cast_p)
2861 {
2862 cp_token *next_token;
2863
2864 next_token = cp_lexer_peek_token (parser->lexer);
2865 if (/* The comma at the end of an
2866 enumerator-definition. */
2867 next_token->type != CPP_COMMA
2868 /* The curly brace at the end of an enum-specifier. */
2869 && next_token->type != CPP_CLOSE_BRACE
2870 /* The end of a statement. */
2871 && next_token->type != CPP_SEMICOLON
2872 /* The end of the cast-expression. */
2873 && next_token->type != CPP_CLOSE_PAREN
2874 /* The end of an array bound. */
2875 && next_token->type != CPP_CLOSE_SQUARE
2876 /* The closing ">" in a template-argument-list. */
2877 && (next_token->type != CPP_GREATER
2878 || parser->greater_than_is_operator_p))
2879 cast_p = false;
2880 }
2881
2882 /* If we are within a cast, then the constraint that the
2883 cast is to an integral or enumeration type will be
2884 checked at that point. If we are not within a cast, then
2885 this code is invalid. */
2886 if (!cast_p)
2887 cp_parser_non_integral_constant_expression
2888 (parser, "floating-point literal");
2889 }
2890 return token->value;
2891
2892 case CPP_STRING:
2893 case CPP_WSTRING:
2894 /* ??? Should wide strings be allowed when parser->translate_strings_p
2895 is false (i.e. in attributes)? If not, we can kill the third
2896 argument to cp_parser_string_literal. */
2897 return cp_parser_string_literal (parser,
2898 parser->translate_strings_p,
2899 true);
2900
2901 case CPP_OPEN_PAREN:
2902 {
2903 tree expr;
2904 bool saved_greater_than_is_operator_p;
2905
2906 /* Consume the `('. */
2907 cp_lexer_consume_token (parser->lexer);
2908 /* Within a parenthesized expression, a `>' token is always
2909 the greater-than operator. */
2910 saved_greater_than_is_operator_p
2911 = parser->greater_than_is_operator_p;
2912 parser->greater_than_is_operator_p = true;
2913 /* If we see `( { ' then we are looking at the beginning of
2914 a GNU statement-expression. */
2915 if (cp_parser_allow_gnu_extensions_p (parser)
2916 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2917 {
2918 /* Statement-expressions are not allowed by the standard. */
2919 if (pedantic)
2920 pedwarn ("ISO C++ forbids braced-groups within expressions");
2921
2922 /* And they're not allowed outside of a function-body; you
2923 cannot, for example, write:
2924
2925 int i = ({ int j = 3; j + 1; });
2926
2927 at class or namespace scope. */
2928 if (!at_function_scope_p ())
2929 error ("statement-expressions are allowed only inside functions");
2930 /* Start the statement-expression. */
2931 expr = begin_stmt_expr ();
2932 /* Parse the compound-statement. */
2933 cp_parser_compound_statement (parser, expr, false);
2934 /* Finish up. */
2935 expr = finish_stmt_expr (expr, false);
2936 }
2937 else
2938 {
2939 /* Parse the parenthesized expression. */
2940 expr = cp_parser_expression (parser, cast_p);
2941 /* Let the front end know that this expression was
2942 enclosed in parentheses. This matters in case, for
2943 example, the expression is of the form `A::B', since
2944 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2945 not. */
2946 finish_parenthesized_expr (expr);
2947 }
2948 /* The `>' token might be the end of a template-id or
2949 template-parameter-list now. */
2950 parser->greater_than_is_operator_p
2951 = saved_greater_than_is_operator_p;
2952 /* Consume the `)'. */
2953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2954 cp_parser_skip_to_end_of_statement (parser);
2955
2956 return expr;
2957 }
2958
2959 case CPP_KEYWORD:
2960 switch (token->keyword)
2961 {
2962 /* These two are the boolean literals. */
2963 case RID_TRUE:
2964 cp_lexer_consume_token (parser->lexer);
2965 return boolean_true_node;
2966 case RID_FALSE:
2967 cp_lexer_consume_token (parser->lexer);
2968 return boolean_false_node;
2969
2970 /* The `__null' literal. */
2971 case RID_NULL:
2972 cp_lexer_consume_token (parser->lexer);
2973 return null_node;
2974
2975 /* Recognize the `this' keyword. */
2976 case RID_THIS:
2977 cp_lexer_consume_token (parser->lexer);
2978 if (parser->local_variables_forbidden_p)
2979 {
2980 error ("%<this%> may not be used in this context");
2981 return error_mark_node;
2982 }
2983 /* Pointers cannot appear in constant-expressions. */
2984 if (cp_parser_non_integral_constant_expression (parser,
2985 "`this'"))
2986 return error_mark_node;
2987 return finish_this_expr ();
2988
2989 /* The `operator' keyword can be the beginning of an
2990 id-expression. */
2991 case RID_OPERATOR:
2992 goto id_expression;
2993
2994 case RID_FUNCTION_NAME:
2995 case RID_PRETTY_FUNCTION_NAME:
2996 case RID_C99_FUNCTION_NAME:
2997 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2998 __func__ are the names of variables -- but they are
2999 treated specially. Therefore, they are handled here,
3000 rather than relying on the generic id-expression logic
3001 below. Grammatically, these names are id-expressions.
3002
3003 Consume the token. */
3004 token = cp_lexer_consume_token (parser->lexer);
3005 /* Look up the name. */
3006 return finish_fname (token->value);
3007
3008 case RID_VA_ARG:
3009 {
3010 tree expression;
3011 tree type;
3012
3013 /* The `__builtin_va_arg' construct is used to handle
3014 `va_arg'. Consume the `__builtin_va_arg' token. */
3015 cp_lexer_consume_token (parser->lexer);
3016 /* Look for the opening `('. */
3017 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3018 /* Now, parse the assignment-expression. */
3019 expression = cp_parser_assignment_expression (parser,
3020 /*cast_p=*/false);
3021 /* Look for the `,'. */
3022 cp_parser_require (parser, CPP_COMMA, "`,'");
3023 /* Parse the type-id. */
3024 type = cp_parser_type_id (parser);
3025 /* Look for the closing `)'. */
3026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3027 /* Using `va_arg' in a constant-expression is not
3028 allowed. */
3029 if (cp_parser_non_integral_constant_expression (parser,
3030 "`va_arg'"))
3031 return error_mark_node;
3032 return build_x_va_arg (expression, type);
3033 }
3034
3035 case RID_OFFSETOF:
3036 return cp_parser_builtin_offsetof (parser);
3037
3038 /* Objective-C++ expressions. */
3039 case RID_AT_ENCODE:
3040 case RID_AT_PROTOCOL:
3041 case RID_AT_SELECTOR:
3042 return cp_parser_objc_expression (parser);
3043
3044 default:
3045 cp_parser_error (parser, "expected primary-expression");
3046 return error_mark_node;
3047 }
3048
3049 /* An id-expression can start with either an identifier, a
3050 `::' as the beginning of a qualified-id, or the "operator"
3051 keyword. */
3052 case CPP_NAME:
3053 case CPP_SCOPE:
3054 case CPP_TEMPLATE_ID:
3055 case CPP_NESTED_NAME_SPECIFIER:
3056 {
3057 tree id_expression;
3058 tree decl;
3059 const char *error_msg;
3060 bool template_p;
3061 bool done;
3062
3063 id_expression:
3064 /* Parse the id-expression. */
3065 id_expression
3066 = cp_parser_id_expression (parser,
3067 /*template_keyword_p=*/false,
3068 /*check_dependency_p=*/true,
3069 &template_p,
3070 /*declarator_p=*/false,
3071 /*optional_p=*/false);
3072 if (id_expression == error_mark_node)
3073 return error_mark_node;
3074 token = cp_lexer_peek_token (parser->lexer);
3075 done = (token->type != CPP_OPEN_SQUARE
3076 && token->type != CPP_OPEN_PAREN
3077 && token->type != CPP_DOT
3078 && token->type != CPP_DEREF
3079 && token->type != CPP_PLUS_PLUS
3080 && token->type != CPP_MINUS_MINUS);
3081 /* If we have a template-id, then no further lookup is
3082 required. If the template-id was for a template-class, we
3083 will sometimes have a TYPE_DECL at this point. */
3084 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3085 || TREE_CODE (id_expression) == TYPE_DECL)
3086 decl = id_expression;
3087 /* Look up the name. */
3088 else
3089 {
3090 tree ambiguous_decls;
3091
3092 decl = cp_parser_lookup_name (parser, id_expression,
3093 none_type,
3094 template_p,
3095 /*is_namespace=*/false,
3096 /*check_dependency=*/true,
3097 &ambiguous_decls);
3098 /* If the lookup was ambiguous, an error will already have
3099 been issued. */
3100 if (ambiguous_decls)
3101 return error_mark_node;
3102
3103 /* In Objective-C++, an instance variable (ivar) may be preferred
3104 to whatever cp_parser_lookup_name() found. */
3105 decl = objc_lookup_ivar (decl, id_expression);
3106
3107 /* If name lookup gives us a SCOPE_REF, then the
3108 qualifying scope was dependent. */
3109 if (TREE_CODE (decl) == SCOPE_REF)
3110 return decl;
3111 /* Check to see if DECL is a local variable in a context
3112 where that is forbidden. */
3113 if (parser->local_variables_forbidden_p
3114 && local_variable_p (decl))
3115 {
3116 /* It might be that we only found DECL because we are
3117 trying to be generous with pre-ISO scoping rules.
3118 For example, consider:
3119
3120 int i;
3121 void g() {
3122 for (int i = 0; i < 10; ++i) {}
3123 extern void f(int j = i);
3124 }
3125
3126 Here, name look up will originally find the out
3127 of scope `i'. We need to issue a warning message,
3128 but then use the global `i'. */
3129 decl = check_for_out_of_scope_variable (decl);
3130 if (local_variable_p (decl))
3131 {
3132 error ("local variable %qD may not appear in this context",
3133 decl);
3134 return error_mark_node;
3135 }
3136 }
3137 }
3138
3139 decl = (finish_id_expression
3140 (id_expression, decl, parser->scope,
3141 idk,
3142 parser->integral_constant_expression_p,
3143 parser->allow_non_integral_constant_expression_p,
3144 &parser->non_integral_constant_expression_p,
3145 template_p, done, address_p,
3146 template_arg_p,
3147 &error_msg));
3148 if (error_msg)
3149 cp_parser_error (parser, error_msg);
3150 return decl;
3151 }
3152
3153 /* Anything else is an error. */
3154 default:
3155 /* ...unless we have an Objective-C++ message or string literal, that is. */
3156 if (c_dialect_objc ()
3157 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3158 return cp_parser_objc_expression (parser);
3159
3160 cp_parser_error (parser, "expected primary-expression");
3161 return error_mark_node;
3162 }
3163 }
3164
3165 /* Parse an id-expression.
3166
3167 id-expression:
3168 unqualified-id
3169 qualified-id
3170
3171 qualified-id:
3172 :: [opt] nested-name-specifier template [opt] unqualified-id
3173 :: identifier
3174 :: operator-function-id
3175 :: template-id
3176
3177 Return a representation of the unqualified portion of the
3178 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3179 a `::' or nested-name-specifier.
3180
3181 Often, if the id-expression was a qualified-id, the caller will
3182 want to make a SCOPE_REF to represent the qualified-id. This
3183 function does not do this in order to avoid wastefully creating
3184 SCOPE_REFs when they are not required.
3185
3186 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3187 `template' keyword.
3188
3189 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3190 uninstantiated templates.
3191
3192 If *TEMPLATE_P is non-NULL, it is set to true iff the
3193 `template' keyword is used to explicitly indicate that the entity
3194 named is a template.
3195
3196 If DECLARATOR_P is true, the id-expression is appearing as part of
3197 a declarator, rather than as part of an expression. */
3198
3199 static tree
3200 cp_parser_id_expression (cp_parser *parser,
3201 bool template_keyword_p,
3202 bool check_dependency_p,
3203 bool *template_p,
3204 bool declarator_p,
3205 bool optional_p)
3206 {
3207 bool global_scope_p;
3208 bool nested_name_specifier_p;
3209
3210 /* Assume the `template' keyword was not used. */
3211 if (template_p)
3212 *template_p = template_keyword_p;
3213
3214 /* Look for the optional `::' operator. */
3215 global_scope_p
3216 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3217 != NULL_TREE);
3218 /* Look for the optional nested-name-specifier. */
3219 nested_name_specifier_p
3220 = (cp_parser_nested_name_specifier_opt (parser,
3221 /*typename_keyword_p=*/false,
3222 check_dependency_p,
3223 /*type_p=*/false,
3224 declarator_p)
3225 != NULL_TREE);
3226 /* If there is a nested-name-specifier, then we are looking at
3227 the first qualified-id production. */
3228 if (nested_name_specifier_p)
3229 {
3230 tree saved_scope;
3231 tree saved_object_scope;
3232 tree saved_qualifying_scope;
3233 tree unqualified_id;
3234 bool is_template;
3235
3236 /* See if the next token is the `template' keyword. */
3237 if (!template_p)
3238 template_p = &is_template;
3239 *template_p = cp_parser_optional_template_keyword (parser);
3240 /* Name lookup we do during the processing of the
3241 unqualified-id might obliterate SCOPE. */
3242 saved_scope = parser->scope;
3243 saved_object_scope = parser->object_scope;
3244 saved_qualifying_scope = parser->qualifying_scope;
3245 /* Process the final unqualified-id. */
3246 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3247 check_dependency_p,
3248 declarator_p,
3249 /*optional_p=*/false);
3250 /* Restore the SAVED_SCOPE for our caller. */
3251 parser->scope = saved_scope;
3252 parser->object_scope = saved_object_scope;
3253 parser->qualifying_scope = saved_qualifying_scope;
3254
3255 return unqualified_id;
3256 }
3257 /* Otherwise, if we are in global scope, then we are looking at one
3258 of the other qualified-id productions. */
3259 else if (global_scope_p)
3260 {
3261 cp_token *token;
3262 tree id;
3263
3264 /* Peek at the next token. */
3265 token = cp_lexer_peek_token (parser->lexer);
3266
3267 /* If it's an identifier, and the next token is not a "<", then
3268 we can avoid the template-id case. This is an optimization
3269 for this common case. */
3270 if (token->type == CPP_NAME
3271 && !cp_parser_nth_token_starts_template_argument_list_p
3272 (parser, 2))
3273 return cp_parser_identifier (parser);
3274
3275 cp_parser_parse_tentatively (parser);
3276 /* Try a template-id. */
3277 id = cp_parser_template_id (parser,
3278 /*template_keyword_p=*/false,
3279 /*check_dependency_p=*/true,
3280 declarator_p);
3281 /* If that worked, we're done. */
3282 if (cp_parser_parse_definitely (parser))
3283 return id;
3284
3285 /* Peek at the next token. (Changes in the token buffer may
3286 have invalidated the pointer obtained above.) */
3287 token = cp_lexer_peek_token (parser->lexer);
3288
3289 switch (token->type)
3290 {
3291 case CPP_NAME:
3292 return cp_parser_identifier (parser);
3293
3294 case CPP_KEYWORD:
3295 if (token->keyword == RID_OPERATOR)
3296 return cp_parser_operator_function_id (parser);
3297 /* Fall through. */
3298
3299 default:
3300 cp_parser_error (parser, "expected id-expression");
3301 return error_mark_node;
3302 }
3303 }
3304 else
3305 return cp_parser_unqualified_id (parser, template_keyword_p,
3306 /*check_dependency_p=*/true,
3307 declarator_p,
3308 optional_p);
3309 }
3310
3311 /* Parse an unqualified-id.
3312
3313 unqualified-id:
3314 identifier
3315 operator-function-id
3316 conversion-function-id
3317 ~ class-name
3318 template-id
3319
3320 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3321 keyword, in a construct like `A::template ...'.
3322
3323 Returns a representation of unqualified-id. For the `identifier'
3324 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3325 production a BIT_NOT_EXPR is returned; the operand of the
3326 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3327 other productions, see the documentation accompanying the
3328 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3329 names are looked up in uninstantiated templates. If DECLARATOR_P
3330 is true, the unqualified-id is appearing as part of a declarator,
3331 rather than as part of an expression. */
3332
3333 static tree
3334 cp_parser_unqualified_id (cp_parser* parser,
3335 bool template_keyword_p,
3336 bool check_dependency_p,
3337 bool declarator_p,
3338 bool optional_p)
3339 {
3340 cp_token *token;
3341
3342 /* Peek at the next token. */
3343 token = cp_lexer_peek_token (parser->lexer);
3344
3345 switch (token->type)
3346 {
3347 case CPP_NAME:
3348 {
3349 tree id;
3350
3351 /* We don't know yet whether or not this will be a
3352 template-id. */
3353 cp_parser_parse_tentatively (parser);
3354 /* Try a template-id. */
3355 id = cp_parser_template_id (parser, template_keyword_p,
3356 check_dependency_p,
3357 declarator_p);
3358 /* If it worked, we're done. */
3359 if (cp_parser_parse_definitely (parser))
3360 return id;
3361 /* Otherwise, it's an ordinary identifier. */
3362 return cp_parser_identifier (parser);
3363 }
3364
3365 case CPP_TEMPLATE_ID:
3366 return cp_parser_template_id (parser, template_keyword_p,
3367 check_dependency_p,
3368 declarator_p);
3369
3370 case CPP_COMPL:
3371 {
3372 tree type_decl;
3373 tree qualifying_scope;
3374 tree object_scope;
3375 tree scope;
3376 bool done;
3377
3378 /* Consume the `~' token. */
3379 cp_lexer_consume_token (parser->lexer);
3380 /* Parse the class-name. The standard, as written, seems to
3381 say that:
3382
3383 template <typename T> struct S { ~S (); };
3384 template <typename T> S<T>::~S() {}
3385
3386 is invalid, since `~' must be followed by a class-name, but
3387 `S<T>' is dependent, and so not known to be a class.
3388 That's not right; we need to look in uninstantiated
3389 templates. A further complication arises from:
3390
3391 template <typename T> void f(T t) {
3392 t.T::~T();
3393 }
3394
3395 Here, it is not possible to look up `T' in the scope of `T'
3396 itself. We must look in both the current scope, and the
3397 scope of the containing complete expression.
3398
3399 Yet another issue is:
3400
3401 struct S {
3402 int S;
3403 ~S();
3404 };
3405
3406 S::~S() {}
3407
3408 The standard does not seem to say that the `S' in `~S'
3409 should refer to the type `S' and not the data member
3410 `S::S'. */
3411
3412 /* DR 244 says that we look up the name after the "~" in the
3413 same scope as we looked up the qualifying name. That idea
3414 isn't fully worked out; it's more complicated than that. */
3415 scope = parser->scope;
3416 object_scope = parser->object_scope;
3417 qualifying_scope = parser->qualifying_scope;
3418
3419 /* If the name is of the form "X::~X" it's OK. */
3420 token = cp_lexer_peek_token (parser->lexer);
3421 if (scope && TYPE_P (scope)
3422 && token->type == CPP_NAME
3423 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3424 == CPP_OPEN_PAREN)
3425 && constructor_name_p (token->value, scope))
3426 {
3427 cp_lexer_consume_token (parser->lexer);
3428 return build_nt (BIT_NOT_EXPR, scope);
3429 }
3430
3431 /* If there was an explicit qualification (S::~T), first look
3432 in the scope given by the qualification (i.e., S). */
3433 done = false;
3434 type_decl = NULL_TREE;
3435 if (scope)
3436 {
3437 cp_parser_parse_tentatively (parser);
3438 type_decl = cp_parser_class_name (parser,
3439 /*typename_keyword_p=*/false,
3440 /*template_keyword_p=*/false,
3441 none_type,
3442 /*check_dependency=*/false,
3443 /*class_head_p=*/false,
3444 declarator_p);
3445 if (cp_parser_parse_definitely (parser))
3446 done = true;
3447 }
3448 /* In "N::S::~S", look in "N" as well. */
3449 if (!done && scope && qualifying_scope)
3450 {
3451 cp_parser_parse_tentatively (parser);
3452 parser->scope = qualifying_scope;
3453 parser->object_scope = NULL_TREE;
3454 parser->qualifying_scope = NULL_TREE;
3455 type_decl
3456 = cp_parser_class_name (parser,
3457 /*typename_keyword_p=*/false,
3458 /*template_keyword_p=*/false,
3459 none_type,
3460 /*check_dependency=*/false,
3461 /*class_head_p=*/false,
3462 declarator_p);
3463 if (cp_parser_parse_definitely (parser))
3464 done = true;
3465 }
3466 /* In "p->S::~T", look in the scope given by "*p" as well. */
3467 else if (!done && object_scope)
3468 {
3469 cp_parser_parse_tentatively (parser);
3470 parser->scope = object_scope;
3471 parser->object_scope = NULL_TREE;
3472 parser->qualifying_scope = NULL_TREE;
3473 type_decl
3474 = cp_parser_class_name (parser,
3475 /*typename_keyword_p=*/false,
3476 /*template_keyword_p=*/false,
3477 none_type,
3478 /*check_dependency=*/false,
3479 /*class_head_p=*/false,
3480 declarator_p);
3481 if (cp_parser_parse_definitely (parser))
3482 done = true;
3483 }
3484 /* Look in the surrounding context. */
3485 if (!done)
3486 {
3487 parser->scope = NULL_TREE;
3488 parser->object_scope = NULL_TREE;
3489 parser->qualifying_scope = NULL_TREE;
3490 type_decl
3491 = cp_parser_class_name (parser,
3492 /*typename_keyword_p=*/false,
3493 /*template_keyword_p=*/false,
3494 none_type,
3495 /*check_dependency=*/false,
3496 /*class_head_p=*/false,
3497 declarator_p);
3498 }
3499 /* If an error occurred, assume that the name of the
3500 destructor is the same as the name of the qualifying
3501 class. That allows us to keep parsing after running
3502 into ill-formed destructor names. */
3503 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3504 return build_nt (BIT_NOT_EXPR, scope);
3505 else if (type_decl == error_mark_node)
3506 return error_mark_node;
3507
3508 /* Check that destructor name and scope match. */
3509 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3510 {
3511 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3512 error ("declaration of %<~%T%> as member of %qT",
3513 type_decl, scope);
3514 return error_mark_node;
3515 }
3516
3517 /* [class.dtor]
3518
3519 A typedef-name that names a class shall not be used as the
3520 identifier in the declarator for a destructor declaration. */
3521 if (declarator_p
3522 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3523 && !DECL_SELF_REFERENCE_P (type_decl)
3524 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3525 error ("typedef-name %qD used as destructor declarator",
3526 type_decl);
3527
3528 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3529 }
3530
3531 case CPP_KEYWORD:
3532 if (token->keyword == RID_OPERATOR)
3533 {
3534 tree id;
3535
3536 /* This could be a template-id, so we try that first. */
3537 cp_parser_parse_tentatively (parser);
3538 /* Try a template-id. */
3539 id = cp_parser_template_id (parser, template_keyword_p,
3540 /*check_dependency_p=*/true,
3541 declarator_p);
3542 /* If that worked, we're done. */
3543 if (cp_parser_parse_definitely (parser))
3544 return id;
3545 /* We still don't know whether we're looking at an
3546 operator-function-id or a conversion-function-id. */
3547 cp_parser_parse_tentatively (parser);
3548 /* Try an operator-function-id. */
3549 id = cp_parser_operator_function_id (parser);
3550 /* If that didn't work, try a conversion-function-id. */
3551 if (!cp_parser_parse_definitely (parser))
3552 id = cp_parser_conversion_function_id (parser);
3553
3554 return id;
3555 }
3556 /* Fall through. */
3557
3558 default:
3559 if (optional_p)
3560 return NULL_TREE;
3561 cp_parser_error (parser, "expected unqualified-id");
3562 return error_mark_node;
3563 }
3564 }
3565
3566 /* Parse an (optional) nested-name-specifier.
3567
3568 nested-name-specifier:
3569 class-or-namespace-name :: nested-name-specifier [opt]
3570 class-or-namespace-name :: template nested-name-specifier [opt]
3571
3572 PARSER->SCOPE should be set appropriately before this function is
3573 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3574 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3575 in name lookups.
3576
3577 Sets PARSER->SCOPE to the class (TYPE) or namespace
3578 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3579 it unchanged if there is no nested-name-specifier. Returns the new
3580 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3581
3582 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3583 part of a declaration and/or decl-specifier. */
3584
3585 static tree
3586 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3587 bool typename_keyword_p,
3588 bool check_dependency_p,
3589 bool type_p,
3590 bool is_declaration)
3591 {
3592 bool success = false;
3593 cp_token_position start = 0;
3594 cp_token *token;
3595
3596 /* Remember where the nested-name-specifier starts. */
3597 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3598 {
3599 start = cp_lexer_token_position (parser->lexer, false);
3600 push_deferring_access_checks (dk_deferred);
3601 }
3602
3603 while (true)
3604 {
3605 tree new_scope;
3606 tree old_scope;
3607 tree saved_qualifying_scope;
3608 bool template_keyword_p;
3609
3610 /* Spot cases that cannot be the beginning of a
3611 nested-name-specifier. */
3612 token = cp_lexer_peek_token (parser->lexer);
3613
3614 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3615 the already parsed nested-name-specifier. */
3616 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3617 {
3618 /* Grab the nested-name-specifier and continue the loop. */
3619 cp_parser_pre_parsed_nested_name_specifier (parser);
3620 success = true;
3621 continue;
3622 }
3623
3624 /* Spot cases that cannot be the beginning of a
3625 nested-name-specifier. On the second and subsequent times
3626 through the loop, we look for the `template' keyword. */
3627 if (success && token->keyword == RID_TEMPLATE)
3628 ;
3629 /* A template-id can start a nested-name-specifier. */
3630 else if (token->type == CPP_TEMPLATE_ID)
3631 ;
3632 else
3633 {
3634 /* If the next token is not an identifier, then it is
3635 definitely not a class-or-namespace-name. */
3636 if (token->type != CPP_NAME)
3637 break;
3638 /* If the following token is neither a `<' (to begin a
3639 template-id), nor a `::', then we are not looking at a
3640 nested-name-specifier. */
3641 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3642 if (token->type != CPP_SCOPE
3643 && !cp_parser_nth_token_starts_template_argument_list_p
3644 (parser, 2))
3645 break;
3646 }
3647
3648 /* The nested-name-specifier is optional, so we parse
3649 tentatively. */
3650 cp_parser_parse_tentatively (parser);
3651
3652 /* Look for the optional `template' keyword, if this isn't the
3653 first time through the loop. */
3654 if (success)
3655 template_keyword_p = cp_parser_optional_template_keyword (parser);
3656 else
3657 template_keyword_p = false;
3658
3659 /* Save the old scope since the name lookup we are about to do
3660 might destroy it. */
3661 old_scope = parser->scope;
3662 saved_qualifying_scope = parser->qualifying_scope;
3663 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3664 look up names in "X<T>::I" in order to determine that "Y" is
3665 a template. So, if we have a typename at this point, we make
3666 an effort to look through it. */
3667 if (is_declaration
3668 && !typename_keyword_p
3669 && parser->scope
3670 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3671 parser->scope = resolve_typename_type (parser->scope,
3672 /*only_current_p=*/false);
3673 /* Parse the qualifying entity. */
3674 new_scope
3675 = cp_parser_class_or_namespace_name (parser,
3676 typename_keyword_p,
3677 template_keyword_p,
3678 check_dependency_p,
3679 type_p,
3680 is_declaration);
3681 /* Look for the `::' token. */
3682 cp_parser_require (parser, CPP_SCOPE, "`::'");
3683
3684 /* If we found what we wanted, we keep going; otherwise, we're
3685 done. */
3686 if (!cp_parser_parse_definitely (parser))
3687 {
3688 bool error_p = false;
3689
3690 /* Restore the OLD_SCOPE since it was valid before the
3691 failed attempt at finding the last
3692 class-or-namespace-name. */
3693 parser->scope = old_scope;
3694 parser->qualifying_scope = saved_qualifying_scope;
3695 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3696 break;
3697 /* If the next token is an identifier, and the one after
3698 that is a `::', then any valid interpretation would have
3699 found a class-or-namespace-name. */
3700 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3701 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3702 == CPP_SCOPE)
3703 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3704 != CPP_COMPL))
3705 {
3706 token = cp_lexer_consume_token (parser->lexer);
3707 if (!error_p)
3708 {
3709 if (!token->ambiguous_p)
3710 {
3711 tree decl;
3712 tree ambiguous_decls;
3713
3714 decl = cp_parser_lookup_name (parser, token->value,
3715 none_type,
3716 /*is_template=*/false,
3717 /*is_namespace=*/false,
3718 /*check_dependency=*/true,
3719 &ambiguous_decls);
3720 if (TREE_CODE (decl) == TEMPLATE_DECL)
3721 error ("%qD used without template parameters", decl);
3722 else if (ambiguous_decls)
3723 {
3724 error ("reference to %qD is ambiguous",
3725 token->value);
3726 print_candidates (ambiguous_decls);
3727 decl = error_mark_node;
3728 }
3729 else
3730 cp_parser_name_lookup_error
3731 (parser, token->value, decl,
3732 "is not a class or namespace");
3733 }
3734 parser->scope = error_mark_node;
3735 error_p = true;
3736 /* Treat this as a successful nested-name-specifier
3737 due to:
3738
3739 [basic.lookup.qual]
3740
3741 If the name found is not a class-name (clause
3742 _class_) or namespace-name (_namespace.def_), the
3743 program is ill-formed. */
3744 success = true;
3745 }
3746 cp_lexer_consume_token (parser->lexer);
3747 }
3748 break;
3749 }
3750 /* We've found one valid nested-name-specifier. */
3751 success = true;
3752 /* Name lookup always gives us a DECL. */
3753 if (TREE_CODE (new_scope) == TYPE_DECL)
3754 new_scope = TREE_TYPE (new_scope);
3755 /* Uses of "template" must be followed by actual templates. */
3756 if (template_keyword_p
3757 && !(CLASS_TYPE_P (new_scope)
3758 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3759 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3760 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3761 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3762 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3763 == TEMPLATE_ID_EXPR)))
3764 pedwarn (TYPE_P (new_scope)
3765 ? "%qT is not a template"
3766 : "%qD is not a template",
3767 new_scope);
3768 /* If it is a class scope, try to complete it; we are about to
3769 be looking up names inside the class. */
3770 if (TYPE_P (new_scope)
3771 /* Since checking types for dependency can be expensive,
3772 avoid doing it if the type is already complete. */
3773 && !COMPLETE_TYPE_P (new_scope)
3774 /* Do not try to complete dependent types. */
3775 && !dependent_type_p (new_scope))
3776 new_scope = complete_type (new_scope);
3777 /* Make sure we look in the right scope the next time through
3778 the loop. */
3779 parser->scope = new_scope;
3780 }
3781
3782 /* If parsing tentatively, replace the sequence of tokens that makes
3783 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3784 token. That way, should we re-parse the token stream, we will
3785 not have to repeat the effort required to do the parse, nor will
3786 we issue duplicate error messages. */
3787 if (success && start)
3788 {
3789 cp_token *token;
3790 tree access_checks;
3791
3792 token = cp_lexer_token_at (parser->lexer, start);
3793 /* Reset the contents of the START token. */
3794 token->type = CPP_NESTED_NAME_SPECIFIER;
3795 /* Retrieve any deferred checks. Do not pop this access checks yet
3796 so the memory will not be reclaimed during token replacing below. */
3797 access_checks = get_deferred_access_checks ();
3798 token->value = build_tree_list (copy_list (access_checks),
3799 parser->scope);
3800 TREE_TYPE (token->value) = parser->qualifying_scope;
3801 token->keyword = RID_MAX;
3802
3803 /* Purge all subsequent tokens. */
3804 cp_lexer_purge_tokens_after (parser->lexer, start);
3805 }
3806
3807 if (start)
3808 pop_to_parent_deferring_access_checks ();
3809
3810 return success ? parser->scope : NULL_TREE;
3811 }
3812
3813 /* Parse a nested-name-specifier. See
3814 cp_parser_nested_name_specifier_opt for details. This function
3815 behaves identically, except that it will an issue an error if no
3816 nested-name-specifier is present. */
3817
3818 static tree
3819 cp_parser_nested_name_specifier (cp_parser *parser,
3820 bool typename_keyword_p,
3821 bool check_dependency_p,
3822 bool type_p,
3823 bool is_declaration)
3824 {
3825 tree scope;
3826
3827 /* Look for the nested-name-specifier. */
3828 scope = cp_parser_nested_name_specifier_opt (parser,
3829 typename_keyword_p,
3830 check_dependency_p,
3831 type_p,
3832 is_declaration);
3833 /* If it was not present, issue an error message. */
3834 if (!scope)
3835 {
3836 cp_parser_error (parser, "expected nested-name-specifier");
3837 parser->scope = NULL_TREE;
3838 }
3839
3840 return scope;
3841 }
3842
3843 /* Parse a class-or-namespace-name.
3844
3845 class-or-namespace-name:
3846 class-name
3847 namespace-name
3848
3849 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3850 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3851 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3852 TYPE_P is TRUE iff the next name should be taken as a class-name,
3853 even the same name is declared to be another entity in the same
3854 scope.
3855
3856 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3857 specified by the class-or-namespace-name. If neither is found the
3858 ERROR_MARK_NODE is returned. */
3859
3860 static tree
3861 cp_parser_class_or_namespace_name (cp_parser *parser,
3862 bool typename_keyword_p,
3863 bool template_keyword_p,
3864 bool check_dependency_p,
3865 bool type_p,
3866 bool is_declaration)
3867 {
3868 tree saved_scope;
3869 tree saved_qualifying_scope;
3870 tree saved_object_scope;
3871 tree scope;
3872 bool only_class_p;
3873
3874 /* Before we try to parse the class-name, we must save away the
3875 current PARSER->SCOPE since cp_parser_class_name will destroy
3876 it. */
3877 saved_scope = parser->scope;
3878 saved_qualifying_scope = parser->qualifying_scope;
3879 saved_object_scope = parser->object_scope;
3880 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3881 there is no need to look for a namespace-name. */
3882 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3883 if (!only_class_p)
3884 cp_parser_parse_tentatively (parser);
3885 scope = cp_parser_class_name (parser,
3886 typename_keyword_p,
3887 template_keyword_p,
3888 type_p ? class_type : none_type,
3889 check_dependency_p,
3890 /*class_head_p=*/false,
3891 is_declaration);
3892 /* If that didn't work, try for a namespace-name. */
3893 if (!only_class_p && !cp_parser_parse_definitely (parser))
3894 {
3895 /* Restore the saved scope. */
3896 parser->scope = saved_scope;
3897 parser->qualifying_scope = saved_qualifying_scope;
3898 parser->object_scope = saved_object_scope;
3899 /* If we are not looking at an identifier followed by the scope
3900 resolution operator, then this is not part of a
3901 nested-name-specifier. (Note that this function is only used
3902 to parse the components of a nested-name-specifier.) */
3903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3904 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3905 return error_mark_node;
3906 scope = cp_parser_namespace_name (parser);
3907 }
3908
3909 return scope;
3910 }
3911
3912 /* Parse a postfix-expression.
3913
3914 postfix-expression:
3915 primary-expression
3916 postfix-expression [ expression ]
3917 postfix-expression ( expression-list [opt] )
3918 simple-type-specifier ( expression-list [opt] )
3919 typename :: [opt] nested-name-specifier identifier
3920 ( expression-list [opt] )
3921 typename :: [opt] nested-name-specifier template [opt] template-id
3922 ( expression-list [opt] )
3923 postfix-expression . template [opt] id-expression
3924 postfix-expression -> template [opt] id-expression
3925 postfix-expression . pseudo-destructor-name
3926 postfix-expression -> pseudo-destructor-name
3927 postfix-expression ++
3928 postfix-expression --
3929 dynamic_cast < type-id > ( expression )
3930 static_cast < type-id > ( expression )
3931 reinterpret_cast < type-id > ( expression )
3932 const_cast < type-id > ( expression )
3933 typeid ( expression )
3934 typeid ( type-id )
3935
3936 GNU Extension:
3937
3938 postfix-expression:
3939 ( type-id ) { initializer-list , [opt] }
3940
3941 This extension is a GNU version of the C99 compound-literal
3942 construct. (The C99 grammar uses `type-name' instead of `type-id',
3943 but they are essentially the same concept.)
3944
3945 If ADDRESS_P is true, the postfix expression is the operand of the
3946 `&' operator. CAST_P is true if this expression is the target of a
3947 cast.
3948
3949 Returns a representation of the expression. */
3950
3951 static tree
3952 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3953 {
3954 cp_token *token;
3955 enum rid keyword;
3956 cp_id_kind idk = CP_ID_KIND_NONE;
3957 tree postfix_expression = NULL_TREE;
3958
3959 /* Peek at the next token. */
3960 token = cp_lexer_peek_token (parser->lexer);
3961 /* Some of the productions are determined by keywords. */
3962 keyword = token->keyword;
3963 switch (keyword)
3964 {
3965 case RID_DYNCAST:
3966 case RID_STATCAST:
3967 case RID_REINTCAST:
3968 case RID_CONSTCAST:
3969 {
3970 tree type;
3971 tree expression;
3972 const char *saved_message;
3973
3974 /* All of these can be handled in the same way from the point
3975 of view of parsing. Begin by consuming the token
3976 identifying the cast. */
3977 cp_lexer_consume_token (parser->lexer);
3978
3979 /* New types cannot be defined in the cast. */
3980 saved_message = parser->type_definition_forbidden_message;
3981 parser->type_definition_forbidden_message
3982 = "types may not be defined in casts";
3983
3984 /* Look for the opening `<'. */
3985 cp_parser_require (parser, CPP_LESS, "`<'");
3986 /* Parse the type to which we are casting. */
3987 type = cp_parser_type_id (parser);
3988 /* Look for the closing `>'. */
3989 cp_parser_require (parser, CPP_GREATER, "`>'");
3990 /* Restore the old message. */
3991 parser->type_definition_forbidden_message = saved_message;
3992
3993 /* And the expression which is being cast. */
3994 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3995 expression = cp_parser_expression (parser, /*cast_p=*/true);
3996 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3997
3998 /* Only type conversions to integral or enumeration types
3999 can be used in constant-expressions. */
4000 if (parser->integral_constant_expression_p
4001 && !dependent_type_p (type)
4002 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4003 && (cp_parser_non_integral_constant_expression
4004 (parser,
4005 "a cast to a type other than an integral or "
4006 "enumeration type")))
4007 return error_mark_node;
4008
4009 switch (keyword)
4010 {
4011 case RID_DYNCAST:
4012 postfix_expression
4013 = build_dynamic_cast (type, expression);
4014 break;
4015 case RID_STATCAST:
4016 postfix_expression
4017 = build_static_cast (type, expression);
4018 break;
4019 case RID_REINTCAST:
4020 postfix_expression
4021 = build_reinterpret_cast (type, expression);
4022 break;
4023 case RID_CONSTCAST:
4024 postfix_expression
4025 = build_const_cast (type, expression);
4026 break;
4027 default:
4028 gcc_unreachable ();
4029 }
4030 }
4031 break;
4032
4033 case RID_TYPEID:
4034 {
4035 tree type;
4036 const char *saved_message;
4037 bool saved_in_type_id_in_expr_p;
4038
4039 /* Consume the `typeid' token. */
4040 cp_lexer_consume_token (parser->lexer);
4041 /* Look for the `(' token. */
4042 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4043 /* Types cannot be defined in a `typeid' expression. */
4044 saved_message = parser->type_definition_forbidden_message;
4045 parser->type_definition_forbidden_message
4046 = "types may not be defined in a `typeid\' expression";
4047 /* We can't be sure yet whether we're looking at a type-id or an
4048 expression. */
4049 cp_parser_parse_tentatively (parser);
4050 /* Try a type-id first. */
4051 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4052 parser->in_type_id_in_expr_p = true;
4053 type = cp_parser_type_id (parser);
4054 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4055 /* Look for the `)' token. Otherwise, we can't be sure that
4056 we're not looking at an expression: consider `typeid (int
4057 (3))', for example. */
4058 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4059 /* If all went well, simply lookup the type-id. */
4060 if (cp_parser_parse_definitely (parser))
4061 postfix_expression = get_typeid (type);
4062 /* Otherwise, fall back to the expression variant. */
4063 else
4064 {
4065 tree expression;
4066
4067 /* Look for an expression. */
4068 expression = cp_parser_expression (parser, /*cast_p=*/false);
4069 /* Compute its typeid. */
4070 postfix_expression = build_typeid (expression);
4071 /* Look for the `)' token. */
4072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4073 }
4074 /* `typeid' may not appear in an integral constant expression. */
4075 if (cp_parser_non_integral_constant_expression(parser,
4076 "`typeid' operator"))
4077 return error_mark_node;
4078 /* Restore the saved message. */
4079 parser->type_definition_forbidden_message = saved_message;
4080 }
4081 break;
4082
4083 case RID_TYPENAME:
4084 {
4085 tree type;
4086 /* The syntax permitted here is the same permitted for an
4087 elaborated-type-specifier. */
4088 type = cp_parser_elaborated_type_specifier (parser,
4089 /*is_friend=*/false,
4090 /*is_declaration=*/false);
4091 postfix_expression = cp_parser_functional_cast (parser, type);
4092 }
4093 break;
4094
4095 default:
4096 {
4097 tree type;
4098
4099 /* If the next thing is a simple-type-specifier, we may be
4100 looking at a functional cast. We could also be looking at
4101 an id-expression. So, we try the functional cast, and if
4102 that doesn't work we fall back to the primary-expression. */
4103 cp_parser_parse_tentatively (parser);
4104 /* Look for the simple-type-specifier. */
4105 type = cp_parser_simple_type_specifier (parser,
4106 /*decl_specs=*/NULL,
4107 CP_PARSER_FLAGS_NONE);
4108 /* Parse the cast itself. */
4109 if (!cp_parser_error_occurred (parser))
4110 postfix_expression
4111 = cp_parser_functional_cast (parser, type);
4112 /* If that worked, we're done. */
4113 if (cp_parser_parse_definitely (parser))
4114 break;
4115
4116 /* If the functional-cast didn't work out, try a
4117 compound-literal. */
4118 if (cp_parser_allow_gnu_extensions_p (parser)
4119 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4120 {
4121 VEC(constructor_elt,gc) *initializer_list = NULL;
4122 bool saved_in_type_id_in_expr_p;
4123
4124 cp_parser_parse_tentatively (parser);
4125 /* Consume the `('. */
4126 cp_lexer_consume_token (parser->lexer);
4127 /* Parse the type. */
4128 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4129 parser->in_type_id_in_expr_p = true;
4130 type = cp_parser_type_id (parser);
4131 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4132 /* Look for the `)'. */
4133 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4134 /* Look for the `{'. */
4135 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4136 /* If things aren't going well, there's no need to
4137 keep going. */
4138 if (!cp_parser_error_occurred (parser))
4139 {
4140 bool non_constant_p;
4141 /* Parse the initializer-list. */
4142 initializer_list
4143 = cp_parser_initializer_list (parser, &non_constant_p);
4144 /* Allow a trailing `,'. */
4145 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4146 cp_lexer_consume_token (parser->lexer);
4147 /* Look for the final `}'. */
4148 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4149 }
4150 /* If that worked, we're definitely looking at a
4151 compound-literal expression. */
4152 if (cp_parser_parse_definitely (parser))
4153 {
4154 /* Warn the user that a compound literal is not
4155 allowed in standard C++. */
4156 if (pedantic)
4157 pedwarn ("ISO C++ forbids compound-literals");
4158 /* Form the representation of the compound-literal. */
4159 postfix_expression
4160 = finish_compound_literal (type, initializer_list);
4161 break;
4162 }
4163 }
4164
4165 /* It must be a primary-expression. */
4166 postfix_expression
4167 = cp_parser_primary_expression (parser, address_p, cast_p,
4168 /*template_arg_p=*/false,
4169 &idk);
4170 }
4171 break;
4172 }
4173
4174 /* Keep looping until the postfix-expression is complete. */
4175 while (true)
4176 {
4177 if (idk == CP_ID_KIND_UNQUALIFIED
4178 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4179 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4180 /* It is not a Koenig lookup function call. */
4181 postfix_expression
4182 = unqualified_name_lookup_error (postfix_expression);
4183
4184 /* Peek at the next token. */
4185 token = cp_lexer_peek_token (parser->lexer);
4186
4187 switch (token->type)
4188 {
4189 case CPP_OPEN_SQUARE:
4190 postfix_expression
4191 = cp_parser_postfix_open_square_expression (parser,
4192 postfix_expression,
4193 false);
4194 idk = CP_ID_KIND_NONE;
4195 break;
4196
4197 case CPP_OPEN_PAREN:
4198 /* postfix-expression ( expression-list [opt] ) */
4199 {
4200 bool koenig_p;
4201 bool is_builtin_constant_p;
4202 bool saved_integral_constant_expression_p = false;
4203 bool saved_non_integral_constant_expression_p = false;
4204 tree args;
4205
4206 is_builtin_constant_p
4207 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4208 if (is_builtin_constant_p)
4209 {
4210 /* The whole point of __builtin_constant_p is to allow
4211 non-constant expressions to appear as arguments. */
4212 saved_integral_constant_expression_p
4213 = parser->integral_constant_expression_p;
4214 saved_non_integral_constant_expression_p
4215 = parser->non_integral_constant_expression_p;
4216 parser->integral_constant_expression_p = false;
4217 }
4218 args = (cp_parser_parenthesized_expression_list
4219 (parser, /*is_attribute_list=*/false,
4220 /*cast_p=*/false,
4221 /*non_constant_p=*/NULL));
4222 if (is_builtin_constant_p)
4223 {
4224 parser->integral_constant_expression_p
4225 = saved_integral_constant_expression_p;
4226 parser->non_integral_constant_expression_p
4227 = saved_non_integral_constant_expression_p;
4228 }
4229
4230 if (args == error_mark_node)
4231 {
4232 postfix_expression = error_mark_node;
4233 break;
4234 }
4235
4236 /* Function calls are not permitted in
4237 constant-expressions. */
4238 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4239 && cp_parser_non_integral_constant_expression (parser,
4240 "a function call"))
4241 {
4242 postfix_expression = error_mark_node;
4243 break;
4244 }
4245
4246 koenig_p = false;
4247 if (idk == CP_ID_KIND_UNQUALIFIED)
4248 {
4249 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4250 {
4251 if (args)
4252 {
4253 koenig_p = true;
4254 postfix_expression
4255 = perform_koenig_lookup (postfix_expression, args);
4256 }
4257 else
4258 postfix_expression
4259 = unqualified_fn_lookup_error (postfix_expression);
4260 }
4261 /* We do not perform argument-dependent lookup if
4262 normal lookup finds a non-function, in accordance
4263 with the expected resolution of DR 218. */
4264 else if (args && is_overloaded_fn (postfix_expression))
4265 {
4266 tree fn = get_first_fn (postfix_expression);
4267
4268 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4269 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4270
4271 /* Only do argument dependent lookup if regular
4272 lookup does not find a set of member functions.
4273 [basic.lookup.koenig]/2a */
4274 if (!DECL_FUNCTION_MEMBER_P (fn))
4275 {
4276 koenig_p = true;
4277 postfix_expression
4278 = perform_koenig_lookup (postfix_expression, args);
4279 }
4280 }
4281 }
4282
4283 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4284 {
4285 tree instance = TREE_OPERAND (postfix_expression, 0);
4286 tree fn = TREE_OPERAND (postfix_expression, 1);
4287
4288 if (processing_template_decl
4289 && (type_dependent_expression_p (instance)
4290 || (!BASELINK_P (fn)
4291 && TREE_CODE (fn) != FIELD_DECL)
4292 || type_dependent_expression_p (fn)
4293 || any_type_dependent_arguments_p (args)))
4294 {
4295 postfix_expression
4296 = build_min_nt (CALL_EXPR, postfix_expression,
4297 args, NULL_TREE);
4298 break;
4299 }
4300
4301 if (BASELINK_P (fn))
4302 postfix_expression
4303 = (build_new_method_call
4304 (instance, fn, args, NULL_TREE,
4305 (idk == CP_ID_KIND_QUALIFIED
4306 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4307 /*fn_p=*/NULL));
4308 else
4309 postfix_expression
4310 = finish_call_expr (postfix_expression, args,
4311 /*disallow_virtual=*/false,
4312 /*koenig_p=*/false);
4313 }
4314 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4315 || TREE_CODE (postfix_expression) == MEMBER_REF
4316 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4317 postfix_expression = (build_offset_ref_call_from_tree
4318 (postfix_expression, args));
4319 else if (idk == CP_ID_KIND_QUALIFIED)
4320 /* A call to a static class member, or a namespace-scope
4321 function. */
4322 postfix_expression
4323 = finish_call_expr (postfix_expression, args,
4324 /*disallow_virtual=*/true,
4325 koenig_p);
4326 else
4327 /* All other function calls. */
4328 postfix_expression
4329 = finish_call_expr (postfix_expression, args,
4330 /*disallow_virtual=*/false,
4331 koenig_p);
4332
4333 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4334 idk = CP_ID_KIND_NONE;
4335 }
4336 break;
4337
4338 case CPP_DOT:
4339 case CPP_DEREF:
4340 /* postfix-expression . template [opt] id-expression
4341 postfix-expression . pseudo-destructor-name
4342 postfix-expression -> template [opt] id-expression
4343 postfix-expression -> pseudo-destructor-name */
4344
4345 /* Consume the `.' or `->' operator. */
4346 cp_lexer_consume_token (parser->lexer);
4347
4348 postfix_expression
4349 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4350 postfix_expression,
4351 false, &idk);
4352 break;
4353
4354 case CPP_PLUS_PLUS:
4355 /* postfix-expression ++ */
4356 /* Consume the `++' token. */
4357 cp_lexer_consume_token (parser->lexer);
4358 /* Generate a representation for the complete expression. */
4359 postfix_expression
4360 = finish_increment_expr (postfix_expression,
4361 POSTINCREMENT_EXPR);
4362 /* Increments may not appear in constant-expressions. */
4363 if (cp_parser_non_integral_constant_expression (parser,
4364 "an increment"))
4365 postfix_expression = error_mark_node;
4366 idk = CP_ID_KIND_NONE;
4367 break;
4368
4369 case CPP_MINUS_MINUS:
4370 /* postfix-expression -- */
4371 /* Consume the `--' token. */
4372 cp_lexer_consume_token (parser->lexer);
4373 /* Generate a representation for the complete expression. */
4374 postfix_expression
4375 = finish_increment_expr (postfix_expression,
4376 POSTDECREMENT_EXPR);
4377 /* Decrements may not appear in constant-expressions. */
4378 if (cp_parser_non_integral_constant_expression (parser,
4379 "a decrement"))
4380 postfix_expression = error_mark_node;
4381 idk = CP_ID_KIND_NONE;
4382 break;
4383
4384 default:
4385 return postfix_expression;
4386 }
4387 }
4388
4389 /* We should never get here. */
4390 gcc_unreachable ();
4391 return error_mark_node;
4392 }
4393
4394 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4395 by cp_parser_builtin_offsetof. We're looking for
4396
4397 postfix-expression [ expression ]
4398
4399 FOR_OFFSETOF is set if we're being called in that context, which
4400 changes how we deal with integer constant expressions. */
4401
4402 static tree
4403 cp_parser_postfix_open_square_expression (cp_parser *parser,
4404 tree postfix_expression,
4405 bool for_offsetof)
4406 {
4407 tree index;
4408
4409 /* Consume the `[' token. */
4410 cp_lexer_consume_token (parser->lexer);
4411
4412 /* Parse the index expression. */
4413 /* ??? For offsetof, there is a question of what to allow here. If
4414 offsetof is not being used in an integral constant expression context,
4415 then we *could* get the right answer by computing the value at runtime.
4416 If we are in an integral constant expression context, then we might
4417 could accept any constant expression; hard to say without analysis.
4418 Rather than open the barn door too wide right away, allow only integer
4419 constant expressions here. */
4420 if (for_offsetof)
4421 index = cp_parser_constant_expression (parser, false, NULL);
4422 else
4423 index = cp_parser_expression (parser, /*cast_p=*/false);
4424
4425 /* Look for the closing `]'. */
4426 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4427
4428 /* Build the ARRAY_REF. */
4429 postfix_expression = grok_array_decl (postfix_expression, index);
4430
4431 /* When not doing offsetof, array references are not permitted in
4432 constant-expressions. */
4433 if (!for_offsetof
4434 && (cp_parser_non_integral_constant_expression
4435 (parser, "an array reference")))
4436 postfix_expression = error_mark_node;
4437
4438 return postfix_expression;
4439 }
4440
4441 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4442 by cp_parser_builtin_offsetof. We're looking for
4443
4444 postfix-expression . template [opt] id-expression
4445 postfix-expression . pseudo-destructor-name
4446 postfix-expression -> template [opt] id-expression
4447 postfix-expression -> pseudo-destructor-name
4448
4449 FOR_OFFSETOF is set if we're being called in that context. That sorta
4450 limits what of the above we'll actually accept, but nevermind.
4451 TOKEN_TYPE is the "." or "->" token, which will already have been
4452 removed from the stream. */
4453
4454 static tree
4455 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4456 enum cpp_ttype token_type,
4457 tree postfix_expression,
4458 bool for_offsetof, cp_id_kind *idk)
4459 {
4460 tree name;
4461 bool dependent_p;
4462 bool pseudo_destructor_p;
4463 tree scope = NULL_TREE;
4464
4465 /* If this is a `->' operator, dereference the pointer. */
4466 if (token_type == CPP_DEREF)
4467 postfix_expression = build_x_arrow (postfix_expression);
4468 /* Check to see whether or not the expression is type-dependent. */
4469 dependent_p = type_dependent_expression_p (postfix_expression);
4470 /* The identifier following the `->' or `.' is not qualified. */
4471 parser->scope = NULL_TREE;
4472 parser->qualifying_scope = NULL_TREE;
4473 parser->object_scope = NULL_TREE;
4474 *idk = CP_ID_KIND_NONE;
4475 /* Enter the scope corresponding to the type of the object
4476 given by the POSTFIX_EXPRESSION. */
4477 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4478 {
4479 scope = TREE_TYPE (postfix_expression);
4480 /* According to the standard, no expression should ever have
4481 reference type. Unfortunately, we do not currently match
4482 the standard in this respect in that our internal representation
4483 of an expression may have reference type even when the standard
4484 says it does not. Therefore, we have to manually obtain the
4485 underlying type here. */
4486 scope = non_reference (scope);
4487 /* The type of the POSTFIX_EXPRESSION must be complete. */
4488 if (scope == unknown_type_node)
4489 {
4490 error ("%qE does not have class type", postfix_expression);
4491 scope = NULL_TREE;
4492 }
4493 else
4494 scope = complete_type_or_else (scope, NULL_TREE);
4495 /* Let the name lookup machinery know that we are processing a
4496 class member access expression. */
4497 parser->context->object_type = scope;
4498 /* If something went wrong, we want to be able to discern that case,
4499 as opposed to the case where there was no SCOPE due to the type
4500 of expression being dependent. */
4501 if (!scope)
4502 scope = error_mark_node;
4503 /* If the SCOPE was erroneous, make the various semantic analysis
4504 functions exit quickly -- and without issuing additional error
4505 messages. */
4506 if (scope == error_mark_node)
4507 postfix_expression = error_mark_node;
4508 }
4509
4510 /* Assume this expression is not a pseudo-destructor access. */
4511 pseudo_destructor_p = false;
4512
4513 /* If the SCOPE is a scalar type, then, if this is a valid program,
4514 we must be looking at a pseudo-destructor-name. */
4515 if (scope && SCALAR_TYPE_P (scope))
4516 {
4517 tree s;
4518 tree type;
4519
4520 cp_parser_parse_tentatively (parser);
4521 /* Parse the pseudo-destructor-name. */
4522 s = NULL_TREE;
4523 cp_parser_pseudo_destructor_name (parser, &s, &type);
4524 if (cp_parser_parse_definitely (parser))
4525 {
4526 pseudo_destructor_p = true;
4527 postfix_expression
4528 = finish_pseudo_destructor_expr (postfix_expression,
4529 s, TREE_TYPE (type));
4530 }
4531 }
4532
4533 if (!pseudo_destructor_p)
4534 {
4535 /* If the SCOPE is not a scalar type, we are looking at an
4536 ordinary class member access expression, rather than a
4537 pseudo-destructor-name. */
4538 bool template_p;
4539 /* Parse the id-expression. */
4540 name = (cp_parser_id_expression
4541 (parser,
4542 cp_parser_optional_template_keyword (parser),
4543 /*check_dependency_p=*/true,
4544 &template_p,
4545 /*declarator_p=*/false,
4546 /*optional_p=*/false));
4547 /* In general, build a SCOPE_REF if the member name is qualified.
4548 However, if the name was not dependent and has already been
4549 resolved; there is no need to build the SCOPE_REF. For example;
4550
4551 struct X { void f(); };
4552 template <typename T> void f(T* t) { t->X::f(); }
4553
4554 Even though "t" is dependent, "X::f" is not and has been resolved
4555 to a BASELINK; there is no need to include scope information. */
4556
4557 /* But we do need to remember that there was an explicit scope for
4558 virtual function calls. */
4559 if (parser->scope)
4560 *idk = CP_ID_KIND_QUALIFIED;
4561
4562 /* If the name is a template-id that names a type, we will get a
4563 TYPE_DECL here. That is invalid code. */
4564 if (TREE_CODE (name) == TYPE_DECL)
4565 {
4566 error ("invalid use of %qD", name);
4567 postfix_expression = error_mark_node;
4568 }
4569 else
4570 {
4571 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4572 {
4573 name = build_qualified_name (/*type=*/NULL_TREE,
4574 parser->scope,
4575 name,
4576 template_p);
4577 parser->scope = NULL_TREE;
4578 parser->qualifying_scope = NULL_TREE;
4579 parser->object_scope = NULL_TREE;
4580 }
4581 if (scope && name && BASELINK_P (name))
4582 adjust_result_of_qualified_name_lookup
4583 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4584 postfix_expression
4585 = finish_class_member_access_expr (postfix_expression, name,
4586 template_p);
4587 }
4588 }
4589
4590 /* We no longer need to look up names in the scope of the object on
4591 the left-hand side of the `.' or `->' operator. */
4592 parser->context->object_type = NULL_TREE;
4593
4594 /* Outside of offsetof, these operators may not appear in
4595 constant-expressions. */
4596 if (!for_offsetof
4597 && (cp_parser_non_integral_constant_expression
4598 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4599 postfix_expression = error_mark_node;
4600
4601 return postfix_expression;
4602 }
4603
4604 /* Parse a parenthesized expression-list.
4605
4606 expression-list:
4607 assignment-expression
4608 expression-list, assignment-expression
4609
4610 attribute-list:
4611 expression-list
4612 identifier
4613 identifier, expression-list
4614
4615 CAST_P is true if this expression is the target of a cast.
4616
4617 Returns a TREE_LIST. The TREE_VALUE of each node is a
4618 representation of an assignment-expression. Note that a TREE_LIST
4619 is returned even if there is only a single expression in the list.
4620 error_mark_node is returned if the ( and or ) are
4621 missing. NULL_TREE is returned on no expressions. The parentheses
4622 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4623 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4624 indicates whether or not all of the expressions in the list were
4625 constant. */
4626
4627 static tree
4628 cp_parser_parenthesized_expression_list (cp_parser* parser,
4629 bool is_attribute_list,
4630 bool cast_p,
4631 bool *non_constant_p)
4632 {
4633 tree expression_list = NULL_TREE;
4634 bool fold_expr_p = is_attribute_list;
4635 tree identifier = NULL_TREE;
4636
4637 /* Assume all the expressions will be constant. */
4638 if (non_constant_p)
4639 *non_constant_p = false;
4640
4641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4642 return error_mark_node;
4643
4644 /* Consume expressions until there are no more. */
4645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4646 while (true)
4647 {
4648 tree expr;
4649
4650 /* At the beginning of attribute lists, check to see if the
4651 next token is an identifier. */
4652 if (is_attribute_list
4653 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4654 {
4655 cp_token *token;
4656
4657 /* Consume the identifier. */
4658 token = cp_lexer_consume_token (parser->lexer);
4659 /* Save the identifier. */
4660 identifier = token->value;
4661 }
4662 else
4663 {
4664 /* Parse the next assignment-expression. */
4665 if (non_constant_p)
4666 {
4667 bool expr_non_constant_p;
4668 expr = (cp_parser_constant_expression
4669 (parser, /*allow_non_constant_p=*/true,
4670 &expr_non_constant_p));
4671 if (expr_non_constant_p)
4672 *non_constant_p = true;
4673 }
4674 else
4675 expr = cp_parser_assignment_expression (parser, cast_p);
4676
4677 if (fold_expr_p)
4678 expr = fold_non_dependent_expr (expr);
4679
4680 /* Add it to the list. We add error_mark_node
4681 expressions to the list, so that we can still tell if
4682 the correct form for a parenthesized expression-list
4683 is found. That gives better errors. */
4684 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4685
4686 if (expr == error_mark_node)
4687 goto skip_comma;
4688 }
4689
4690 /* After the first item, attribute lists look the same as
4691 expression lists. */
4692 is_attribute_list = false;
4693
4694 get_comma:;
4695 /* If the next token isn't a `,', then we are done. */
4696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4697 break;
4698
4699 /* Otherwise, consume the `,' and keep going. */
4700 cp_lexer_consume_token (parser->lexer);
4701 }
4702
4703 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4704 {
4705 int ending;
4706
4707 skip_comma:;
4708 /* We try and resync to an unnested comma, as that will give the
4709 user better diagnostics. */
4710 ending = cp_parser_skip_to_closing_parenthesis (parser,
4711 /*recovering=*/true,
4712 /*or_comma=*/true,
4713 /*consume_paren=*/true);
4714 if (ending < 0)
4715 goto get_comma;
4716 if (!ending)
4717 return error_mark_node;
4718 }
4719
4720 /* We built up the list in reverse order so we must reverse it now. */
4721 expression_list = nreverse (expression_list);
4722 if (identifier)
4723 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4724
4725 return expression_list;
4726 }
4727
4728 /* Parse a pseudo-destructor-name.
4729
4730 pseudo-destructor-name:
4731 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4732 :: [opt] nested-name-specifier template template-id :: ~ type-name
4733 :: [opt] nested-name-specifier [opt] ~ type-name
4734
4735 If either of the first two productions is used, sets *SCOPE to the
4736 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4737 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4738 or ERROR_MARK_NODE if the parse fails. */
4739
4740 static void
4741 cp_parser_pseudo_destructor_name (cp_parser* parser,
4742 tree* scope,
4743 tree* type)
4744 {
4745 bool nested_name_specifier_p;
4746
4747 /* Assume that things will not work out. */
4748 *type = error_mark_node;
4749
4750 /* Look for the optional `::' operator. */
4751 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4752 /* Look for the optional nested-name-specifier. */
4753 nested_name_specifier_p
4754 = (cp_parser_nested_name_specifier_opt (parser,
4755 /*typename_keyword_p=*/false,
4756 /*check_dependency_p=*/true,
4757 /*type_p=*/false,
4758 /*is_declaration=*/true)
4759 != NULL_TREE);
4760 /* Now, if we saw a nested-name-specifier, we might be doing the
4761 second production. */
4762 if (nested_name_specifier_p
4763 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4764 {
4765 /* Consume the `template' keyword. */
4766 cp_lexer_consume_token (parser->lexer);
4767 /* Parse the template-id. */
4768 cp_parser_template_id (parser,
4769 /*template_keyword_p=*/true,
4770 /*check_dependency_p=*/false,
4771 /*is_declaration=*/true);
4772 /* Look for the `::' token. */
4773 cp_parser_require (parser, CPP_SCOPE, "`::'");
4774 }
4775 /* If the next token is not a `~', then there might be some
4776 additional qualification. */
4777 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4778 {
4779 /* Look for the type-name. */
4780 *scope = TREE_TYPE (cp_parser_type_name (parser));
4781
4782 if (*scope == error_mark_node)
4783 return;
4784
4785 /* If we don't have ::~, then something has gone wrong. Since
4786 the only caller of this function is looking for something
4787 after `.' or `->' after a scalar type, most likely the
4788 program is trying to get a member of a non-aggregate
4789 type. */
4790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4791 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4792 {
4793 cp_parser_error (parser, "request for member of non-aggregate type");
4794 return;
4795 }
4796
4797 /* Look for the `::' token. */
4798 cp_parser_require (parser, CPP_SCOPE, "`::'");
4799 }
4800 else
4801 *scope = NULL_TREE;
4802
4803 /* Look for the `~'. */
4804 cp_parser_require (parser, CPP_COMPL, "`~'");
4805 /* Look for the type-name again. We are not responsible for
4806 checking that it matches the first type-name. */
4807 *type = cp_parser_type_name (parser);
4808 }
4809
4810 /* Parse a unary-expression.
4811
4812 unary-expression:
4813 postfix-expression
4814 ++ cast-expression
4815 -- cast-expression
4816 unary-operator cast-expression
4817 sizeof unary-expression
4818 sizeof ( type-id )
4819 new-expression
4820 delete-expression
4821
4822 GNU Extensions:
4823
4824 unary-expression:
4825 __extension__ cast-expression
4826 __alignof__ unary-expression
4827 __alignof__ ( type-id )
4828 __real__ cast-expression
4829 __imag__ cast-expression
4830 && identifier
4831
4832 ADDRESS_P is true iff the unary-expression is appearing as the
4833 operand of the `&' operator. CAST_P is true if this expression is
4834 the target of a cast.
4835
4836 Returns a representation of the expression. */
4837
4838 static tree
4839 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4840 {
4841 cp_token *token;
4842 enum tree_code unary_operator;
4843
4844 /* Peek at the next token. */
4845 token = cp_lexer_peek_token (parser->lexer);
4846 /* Some keywords give away the kind of expression. */
4847 if (token->type == CPP_KEYWORD)
4848 {
4849 enum rid keyword = token->keyword;
4850
4851 switch (keyword)
4852 {
4853 case RID_ALIGNOF:
4854 case RID_SIZEOF:
4855 {
4856 tree operand;
4857 enum tree_code op;
4858
4859 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4860 /* Consume the token. */
4861 cp_lexer_consume_token (parser->lexer);
4862 /* Parse the operand. */
4863 operand = cp_parser_sizeof_operand (parser, keyword);
4864
4865 if (TYPE_P (operand))
4866 return cxx_sizeof_or_alignof_type (operand, op, true);
4867 else
4868 return cxx_sizeof_or_alignof_expr (operand, op);
4869 }
4870
4871 case RID_NEW:
4872 return cp_parser_new_expression (parser);
4873
4874 case RID_DELETE:
4875 return cp_parser_delete_expression (parser);
4876
4877 case RID_EXTENSION:
4878 {
4879 /* The saved value of the PEDANTIC flag. */
4880 int saved_pedantic;
4881 tree expr;
4882
4883 /* Save away the PEDANTIC flag. */
4884 cp_parser_extension_opt (parser, &saved_pedantic);
4885 /* Parse the cast-expression. */
4886 expr = cp_parser_simple_cast_expression (parser);
4887 /* Restore the PEDANTIC flag. */
4888 pedantic = saved_pedantic;
4889
4890 return expr;
4891 }
4892
4893 case RID_REALPART:
4894 case RID_IMAGPART:
4895 {
4896 tree expression;
4897
4898 /* Consume the `__real__' or `__imag__' token. */
4899 cp_lexer_consume_token (parser->lexer);
4900 /* Parse the cast-expression. */
4901 expression = cp_parser_simple_cast_expression (parser);
4902 /* Create the complete representation. */
4903 return build_x_unary_op ((keyword == RID_REALPART
4904 ? REALPART_EXPR : IMAGPART_EXPR),
4905 expression);
4906 }
4907 break;
4908
4909 default:
4910 break;
4911 }
4912 }
4913
4914 /* Look for the `:: new' and `:: delete', which also signal the
4915 beginning of a new-expression, or delete-expression,
4916 respectively. If the next token is `::', then it might be one of
4917 these. */
4918 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4919 {
4920 enum rid keyword;
4921
4922 /* See if the token after the `::' is one of the keywords in
4923 which we're interested. */
4924 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4925 /* If it's `new', we have a new-expression. */
4926 if (keyword == RID_NEW)
4927 return cp_parser_new_expression (parser);
4928 /* Similarly, for `delete'. */
4929 else if (keyword == RID_DELETE)
4930 return cp_parser_delete_expression (parser);
4931 }
4932
4933 /* Look for a unary operator. */
4934 unary_operator = cp_parser_unary_operator (token);
4935 /* The `++' and `--' operators can be handled similarly, even though
4936 they are not technically unary-operators in the grammar. */
4937 if (unary_operator == ERROR_MARK)
4938 {
4939 if (token->type == CPP_PLUS_PLUS)
4940 unary_operator = PREINCREMENT_EXPR;
4941 else if (token->type == CPP_MINUS_MINUS)
4942 unary_operator = PREDECREMENT_EXPR;
4943 /* Handle the GNU address-of-label extension. */
4944 else if (cp_parser_allow_gnu_extensions_p (parser)
4945 && token->type == CPP_AND_AND)
4946 {
4947 tree identifier;
4948
4949 /* Consume the '&&' token. */
4950 cp_lexer_consume_token (parser->lexer);
4951 /* Look for the identifier. */
4952 identifier = cp_parser_identifier (parser);
4953 /* Create an expression representing the address. */
4954 return finish_label_address_expr (identifier);
4955 }
4956 }
4957 if (unary_operator != ERROR_MARK)
4958 {
4959 tree cast_expression;
4960 tree expression = error_mark_node;
4961 const char *non_constant_p = NULL;
4962
4963 /* Consume the operator token. */
4964 token = cp_lexer_consume_token (parser->lexer);
4965 /* Parse the cast-expression. */
4966 cast_expression
4967 = cp_parser_cast_expression (parser,
4968 unary_operator == ADDR_EXPR,
4969 /*cast_p=*/false);
4970 /* Now, build an appropriate representation. */
4971 switch (unary_operator)
4972 {
4973 case INDIRECT_REF:
4974 non_constant_p = "`*'";
4975 expression = build_x_indirect_ref (cast_expression, "unary *");
4976 break;
4977
4978 case ADDR_EXPR:
4979 non_constant_p = "`&'";
4980 /* Fall through. */
4981 case BIT_NOT_EXPR:
4982 expression = build_x_unary_op (unary_operator, cast_expression);
4983 break;
4984
4985 case PREINCREMENT_EXPR:
4986 case PREDECREMENT_EXPR:
4987 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4988 ? "`++'" : "`--'");
4989 /* Fall through. */
4990 case UNARY_PLUS_EXPR:
4991 case NEGATE_EXPR:
4992 case TRUTH_NOT_EXPR:
4993 expression = finish_unary_op_expr (unary_operator, cast_expression);
4994 break;
4995
4996 default:
4997 gcc_unreachable ();
4998 }
4999
5000 if (non_constant_p
5001 && cp_parser_non_integral_constant_expression (parser,
5002 non_constant_p))
5003 expression = error_mark_node;
5004
5005 return expression;
5006 }
5007
5008 return cp_parser_postfix_expression (parser, address_p, cast_p);
5009 }
5010
5011 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5012 unary-operator, the corresponding tree code is returned. */
5013
5014 static enum tree_code
5015 cp_parser_unary_operator (cp_token* token)
5016 {
5017 switch (token->type)
5018 {
5019 case CPP_MULT:
5020 return INDIRECT_REF;
5021
5022 case CPP_AND:
5023 return ADDR_EXPR;
5024
5025 case CPP_PLUS:
5026 return UNARY_PLUS_EXPR;
5027
5028 case CPP_MINUS:
5029 return NEGATE_EXPR;
5030
5031 case CPP_NOT:
5032 return TRUTH_NOT_EXPR;
5033
5034 case CPP_COMPL:
5035 return BIT_NOT_EXPR;
5036
5037 default:
5038 return ERROR_MARK;
5039 }
5040 }
5041
5042 /* Parse a new-expression.
5043
5044 new-expression:
5045 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5046 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5047
5048 Returns a representation of the expression. */
5049
5050 static tree
5051 cp_parser_new_expression (cp_parser* parser)
5052 {
5053 bool global_scope_p;
5054 tree placement;
5055 tree type;
5056 tree initializer;
5057 tree nelts;
5058
5059 /* Look for the optional `::' operator. */
5060 global_scope_p
5061 = (cp_parser_global_scope_opt (parser,
5062 /*current_scope_valid_p=*/false)
5063 != NULL_TREE);
5064 /* Look for the `new' operator. */
5065 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5066 /* There's no easy way to tell a new-placement from the
5067 `( type-id )' construct. */
5068 cp_parser_parse_tentatively (parser);
5069 /* Look for a new-placement. */
5070 placement = cp_parser_new_placement (parser);
5071 /* If that didn't work out, there's no new-placement. */
5072 if (!cp_parser_parse_definitely (parser))
5073 placement = NULL_TREE;
5074
5075 /* If the next token is a `(', then we have a parenthesized
5076 type-id. */
5077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5078 {
5079 /* Consume the `('. */
5080 cp_lexer_consume_token (parser->lexer);
5081 /* Parse the type-id. */
5082 type = cp_parser_type_id (parser);
5083 /* Look for the closing `)'. */
5084 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5085 /* There should not be a direct-new-declarator in this production,
5086 but GCC used to allowed this, so we check and emit a sensible error
5087 message for this case. */
5088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5089 {
5090 error ("array bound forbidden after parenthesized type-id");
5091 inform ("try removing the parentheses around the type-id");
5092 cp_parser_direct_new_declarator (parser);
5093 }
5094 nelts = NULL_TREE;
5095 }
5096 /* Otherwise, there must be a new-type-id. */
5097 else
5098 type = cp_parser_new_type_id (parser, &nelts);
5099
5100 /* If the next token is a `(', then we have a new-initializer. */
5101 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5102 initializer = cp_parser_new_initializer (parser);
5103 else
5104 initializer = NULL_TREE;
5105
5106 /* A new-expression may not appear in an integral constant
5107 expression. */
5108 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5109 return error_mark_node;
5110
5111 /* Create a representation of the new-expression. */
5112 return build_new (placement, type, nelts, initializer, global_scope_p);
5113 }
5114
5115 /* Parse a new-placement.
5116
5117 new-placement:
5118 ( expression-list )
5119
5120 Returns the same representation as for an expression-list. */
5121
5122 static tree
5123 cp_parser_new_placement (cp_parser* parser)
5124 {
5125 tree expression_list;
5126
5127 /* Parse the expression-list. */
5128 expression_list = (cp_parser_parenthesized_expression_list
5129 (parser, false, /*cast_p=*/false,
5130 /*non_constant_p=*/NULL));
5131
5132 return expression_list;
5133 }
5134
5135 /* Parse a new-type-id.
5136
5137 new-type-id:
5138 type-specifier-seq new-declarator [opt]
5139
5140 Returns the TYPE allocated. If the new-type-id indicates an array
5141 type, *NELTS is set to the number of elements in the last array
5142 bound; the TYPE will not include the last array bound. */
5143
5144 static tree
5145 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5146 {
5147 cp_decl_specifier_seq type_specifier_seq;
5148 cp_declarator *new_declarator;
5149 cp_declarator *declarator;
5150 cp_declarator *outer_declarator;
5151 const char *saved_message;
5152 tree type;
5153
5154 /* The type-specifier sequence must not contain type definitions.
5155 (It cannot contain declarations of new types either, but if they
5156 are not definitions we will catch that because they are not
5157 complete.) */
5158 saved_message = parser->type_definition_forbidden_message;
5159 parser->type_definition_forbidden_message
5160 = "types may not be defined in a new-type-id";
5161 /* Parse the type-specifier-seq. */
5162 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5163 &type_specifier_seq);
5164 /* Restore the old message. */
5165 parser->type_definition_forbidden_message = saved_message;
5166 /* Parse the new-declarator. */
5167 new_declarator = cp_parser_new_declarator_opt (parser);
5168
5169 /* Determine the number of elements in the last array dimension, if
5170 any. */
5171 *nelts = NULL_TREE;
5172 /* Skip down to the last array dimension. */
5173 declarator = new_declarator;
5174 outer_declarator = NULL;
5175 while (declarator && (declarator->kind == cdk_pointer
5176 || declarator->kind == cdk_ptrmem))
5177 {
5178 outer_declarator = declarator;
5179 declarator = declarator->declarator;
5180 }
5181 while (declarator
5182 && declarator->kind == cdk_array
5183 && declarator->declarator
5184 && declarator->declarator->kind == cdk_array)
5185 {
5186 outer_declarator = declarator;
5187 declarator = declarator->declarator;
5188 }
5189
5190 if (declarator && declarator->kind == cdk_array)
5191 {
5192 *nelts = declarator->u.array.bounds;
5193 if (*nelts == error_mark_node)
5194 *nelts = integer_one_node;
5195
5196 if (outer_declarator)
5197 outer_declarator->declarator = declarator->declarator;
5198 else
5199 new_declarator = NULL;
5200 }
5201
5202 type = groktypename (&type_specifier_seq, new_declarator);
5203 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5204 {
5205 *nelts = array_type_nelts_top (type);
5206 type = TREE_TYPE (type);
5207 }
5208 return type;
5209 }
5210
5211 /* Parse an (optional) new-declarator.
5212
5213 new-declarator:
5214 ptr-operator new-declarator [opt]
5215 direct-new-declarator
5216
5217 Returns the declarator. */
5218
5219 static cp_declarator *
5220 cp_parser_new_declarator_opt (cp_parser* parser)
5221 {
5222 enum tree_code code;
5223 tree type;
5224 cp_cv_quals cv_quals;
5225
5226 /* We don't know if there's a ptr-operator next, or not. */
5227 cp_parser_parse_tentatively (parser);
5228 /* Look for a ptr-operator. */
5229 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5230 /* If that worked, look for more new-declarators. */
5231 if (cp_parser_parse_definitely (parser))
5232 {
5233 cp_declarator *declarator;
5234
5235 /* Parse another optional declarator. */
5236 declarator = cp_parser_new_declarator_opt (parser);
5237
5238 /* Create the representation of the declarator. */
5239 if (type)
5240 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5241 else if (code == INDIRECT_REF)
5242 declarator = make_pointer_declarator (cv_quals, declarator);
5243 else
5244 declarator = make_reference_declarator (cv_quals, declarator);
5245
5246 return declarator;
5247 }
5248
5249 /* If the next token is a `[', there is a direct-new-declarator. */
5250 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5251 return cp_parser_direct_new_declarator (parser);
5252
5253 return NULL;
5254 }
5255
5256 /* Parse a direct-new-declarator.
5257
5258 direct-new-declarator:
5259 [ expression ]
5260 direct-new-declarator [constant-expression]
5261
5262 */
5263
5264 static cp_declarator *
5265 cp_parser_direct_new_declarator (cp_parser* parser)
5266 {
5267 cp_declarator *declarator = NULL;
5268
5269 while (true)
5270 {
5271 tree expression;
5272
5273 /* Look for the opening `['. */
5274 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5275 /* The first expression is not required to be constant. */
5276 if (!declarator)
5277 {
5278 expression = cp_parser_expression (parser, /*cast_p=*/false);
5279 /* The standard requires that the expression have integral
5280 type. DR 74 adds enumeration types. We believe that the
5281 real intent is that these expressions be handled like the
5282 expression in a `switch' condition, which also allows
5283 classes with a single conversion to integral or
5284 enumeration type. */
5285 if (!processing_template_decl)
5286 {
5287 expression
5288 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5289 expression,
5290 /*complain=*/true);
5291 if (!expression)
5292 {
5293 error ("expression in new-declarator must have integral "
5294 "or enumeration type");
5295 expression = error_mark_node;
5296 }
5297 }
5298 }
5299 /* But all the other expressions must be. */
5300 else
5301 expression
5302 = cp_parser_constant_expression (parser,
5303 /*allow_non_constant=*/false,
5304 NULL);
5305 /* Look for the closing `]'. */
5306 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5307
5308 /* Add this bound to the declarator. */
5309 declarator = make_array_declarator (declarator, expression);
5310
5311 /* If the next token is not a `[', then there are no more
5312 bounds. */
5313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5314 break;
5315 }
5316
5317 return declarator;
5318 }
5319
5320 /* Parse a new-initializer.
5321
5322 new-initializer:
5323 ( expression-list [opt] )
5324
5325 Returns a representation of the expression-list. If there is no
5326 expression-list, VOID_ZERO_NODE is returned. */
5327
5328 static tree
5329 cp_parser_new_initializer (cp_parser* parser)
5330 {
5331 tree expression_list;
5332
5333 expression_list = (cp_parser_parenthesized_expression_list
5334 (parser, false, /*cast_p=*/false,
5335 /*non_constant_p=*/NULL));
5336 if (!expression_list)
5337 expression_list = void_zero_node;
5338
5339 return expression_list;
5340 }
5341
5342 /* Parse a delete-expression.
5343
5344 delete-expression:
5345 :: [opt] delete cast-expression
5346 :: [opt] delete [ ] cast-expression
5347
5348 Returns a representation of the expression. */
5349
5350 static tree
5351 cp_parser_delete_expression (cp_parser* parser)
5352 {
5353 bool global_scope_p;
5354 bool array_p;
5355 tree expression;
5356
5357 /* Look for the optional `::' operator. */
5358 global_scope_p
5359 = (cp_parser_global_scope_opt (parser,
5360 /*current_scope_valid_p=*/false)
5361 != NULL_TREE);
5362 /* Look for the `delete' keyword. */
5363 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5364 /* See if the array syntax is in use. */
5365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5366 {
5367 /* Consume the `[' token. */
5368 cp_lexer_consume_token (parser->lexer);
5369 /* Look for the `]' token. */
5370 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5371 /* Remember that this is the `[]' construct. */
5372 array_p = true;
5373 }
5374 else
5375 array_p = false;
5376
5377 /* Parse the cast-expression. */
5378 expression = cp_parser_simple_cast_expression (parser);
5379
5380 /* A delete-expression may not appear in an integral constant
5381 expression. */
5382 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5383 return error_mark_node;
5384
5385 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5386 }
5387
5388 /* Parse a cast-expression.
5389
5390 cast-expression:
5391 unary-expression
5392 ( type-id ) cast-expression
5393
5394 ADDRESS_P is true iff the unary-expression is appearing as the
5395 operand of the `&' operator. CAST_P is true if this expression is
5396 the target of a cast.
5397
5398 Returns a representation of the expression. */
5399
5400 static tree
5401 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5402 {
5403 /* If it's a `(', then we might be looking at a cast. */
5404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5405 {
5406 tree type = NULL_TREE;
5407 tree expr = NULL_TREE;
5408 bool compound_literal_p;
5409 const char *saved_message;
5410
5411 /* There's no way to know yet whether or not this is a cast.
5412 For example, `(int (3))' is a unary-expression, while `(int)
5413 3' is a cast. So, we resort to parsing tentatively. */
5414 cp_parser_parse_tentatively (parser);
5415 /* Types may not be defined in a cast. */
5416 saved_message = parser->type_definition_forbidden_message;
5417 parser->type_definition_forbidden_message
5418 = "types may not be defined in casts";
5419 /* Consume the `('. */
5420 cp_lexer_consume_token (parser->lexer);
5421 /* A very tricky bit is that `(struct S) { 3 }' is a
5422 compound-literal (which we permit in C++ as an extension).
5423 But, that construct is not a cast-expression -- it is a
5424 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5425 is legal; if the compound-literal were a cast-expression,
5426 you'd need an extra set of parentheses.) But, if we parse
5427 the type-id, and it happens to be a class-specifier, then we
5428 will commit to the parse at that point, because we cannot
5429 undo the action that is done when creating a new class. So,
5430 then we cannot back up and do a postfix-expression.
5431
5432 Therefore, we scan ahead to the closing `)', and check to see
5433 if the token after the `)' is a `{'. If so, we are not
5434 looking at a cast-expression.
5435
5436 Save tokens so that we can put them back. */
5437 cp_lexer_save_tokens (parser->lexer);
5438 /* Skip tokens until the next token is a closing parenthesis.
5439 If we find the closing `)', and the next token is a `{', then
5440 we are looking at a compound-literal. */
5441 compound_literal_p
5442 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5443 /*consume_paren=*/true)
5444 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5445 /* Roll back the tokens we skipped. */
5446 cp_lexer_rollback_tokens (parser->lexer);
5447 /* If we were looking at a compound-literal, simulate an error
5448 so that the call to cp_parser_parse_definitely below will
5449 fail. */
5450 if (compound_literal_p)
5451 cp_parser_simulate_error (parser);
5452 else
5453 {
5454 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5455 parser->in_type_id_in_expr_p = true;
5456 /* Look for the type-id. */
5457 type = cp_parser_type_id (parser);
5458 /* Look for the closing `)'. */
5459 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5460 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5461 }
5462
5463 /* Restore the saved message. */
5464 parser->type_definition_forbidden_message = saved_message;
5465
5466 /* If ok so far, parse the dependent expression. We cannot be
5467 sure it is a cast. Consider `(T ())'. It is a parenthesized
5468 ctor of T, but looks like a cast to function returning T
5469 without a dependent expression. */
5470 if (!cp_parser_error_occurred (parser))
5471 expr = cp_parser_cast_expression (parser,
5472 /*address_p=*/false,
5473 /*cast_p=*/true);
5474
5475 if (cp_parser_parse_definitely (parser))
5476 {
5477 /* Warn about old-style casts, if so requested. */
5478 if (warn_old_style_cast
5479 && !in_system_header
5480 && !VOID_TYPE_P (type)
5481 && current_lang_name != lang_name_c)
5482 warning (OPT_Wold_style_cast, "use of old-style cast");
5483
5484 /* Only type conversions to integral or enumeration types
5485 can be used in constant-expressions. */
5486 if (parser->integral_constant_expression_p
5487 && !dependent_type_p (type)
5488 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5489 && (cp_parser_non_integral_constant_expression
5490 (parser,
5491 "a cast to a type other than an integral or "
5492 "enumeration type")))
5493 return error_mark_node;
5494
5495 /* Perform the cast. */
5496 expr = build_c_cast (type, expr);
5497 return expr;
5498 }
5499 }
5500
5501 /* If we get here, then it's not a cast, so it must be a
5502 unary-expression. */
5503 return cp_parser_unary_expression (parser, address_p, cast_p);
5504 }
5505
5506 /* Parse a binary expression of the general form:
5507
5508 pm-expression:
5509 cast-expression
5510 pm-expression .* cast-expression
5511 pm-expression ->* cast-expression
5512
5513 multiplicative-expression:
5514 pm-expression
5515 multiplicative-expression * pm-expression
5516 multiplicative-expression / pm-expression
5517 multiplicative-expression % pm-expression
5518
5519 additive-expression:
5520 multiplicative-expression
5521 additive-expression + multiplicative-expression
5522 additive-expression - multiplicative-expression
5523
5524 shift-expression:
5525 additive-expression
5526 shift-expression << additive-expression
5527 shift-expression >> additive-expression
5528
5529 relational-expression:
5530 shift-expression
5531 relational-expression < shift-expression
5532 relational-expression > shift-expression
5533 relational-expression <= shift-expression
5534 relational-expression >= shift-expression
5535
5536 GNU Extension:
5537
5538 relational-expression:
5539 relational-expression <? shift-expression
5540 relational-expression >? shift-expression
5541
5542 equality-expression:
5543 relational-expression
5544 equality-expression == relational-expression
5545 equality-expression != relational-expression
5546
5547 and-expression:
5548 equality-expression
5549 and-expression & equality-expression
5550
5551 exclusive-or-expression:
5552 and-expression
5553 exclusive-or-expression ^ and-expression
5554
5555 inclusive-or-expression:
5556 exclusive-or-expression
5557 inclusive-or-expression | exclusive-or-expression
5558
5559 logical-and-expression:
5560 inclusive-or-expression
5561 logical-and-expression && inclusive-or-expression
5562
5563 logical-or-expression:
5564 logical-and-expression
5565 logical-or-expression || logical-and-expression
5566
5567 All these are implemented with a single function like:
5568
5569 binary-expression:
5570 simple-cast-expression
5571 binary-expression <token> binary-expression
5572
5573 CAST_P is true if this expression is the target of a cast.
5574
5575 The binops_by_token map is used to get the tree codes for each <token> type.
5576 binary-expressions are associated according to a precedence table. */
5577
5578 #define TOKEN_PRECEDENCE(token) \
5579 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5580 ? PREC_NOT_OPERATOR \
5581 : binops_by_token[token->type].prec)
5582
5583 static tree
5584 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5585 {
5586 cp_parser_expression_stack stack;
5587 cp_parser_expression_stack_entry *sp = &stack[0];
5588 tree lhs, rhs;
5589 cp_token *token;
5590 enum tree_code tree_type;
5591 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5592 bool overloaded_p;
5593
5594 /* Parse the first expression. */
5595 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5596
5597 for (;;)
5598 {
5599 /* Get an operator token. */
5600 token = cp_lexer_peek_token (parser->lexer);
5601 if (token->type == CPP_MIN || token->type == CPP_MAX)
5602 cp_parser_warn_min_max ();
5603
5604 new_prec = TOKEN_PRECEDENCE (token);
5605
5606 /* Popping an entry off the stack means we completed a subexpression:
5607 - either we found a token which is not an operator (`>' where it is not
5608 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5609 will happen repeatedly;
5610 - or, we found an operator which has lower priority. This is the case
5611 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5612 parsing `3 * 4'. */
5613 if (new_prec <= prec)
5614 {
5615 if (sp == stack)
5616 break;
5617 else
5618 goto pop;
5619 }
5620
5621 get_rhs:
5622 tree_type = binops_by_token[token->type].tree_type;
5623
5624 /* We used the operator token. */
5625 cp_lexer_consume_token (parser->lexer);
5626
5627 /* Extract another operand. It may be the RHS of this expression
5628 or the LHS of a new, higher priority expression. */
5629 rhs = cp_parser_simple_cast_expression (parser);
5630
5631 /* Get another operator token. Look up its precedence to avoid
5632 building a useless (immediately popped) stack entry for common
5633 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5634 token = cp_lexer_peek_token (parser->lexer);
5635 lookahead_prec = TOKEN_PRECEDENCE (token);
5636 if (lookahead_prec > new_prec)
5637 {
5638 /* ... and prepare to parse the RHS of the new, higher priority
5639 expression. Since precedence levels on the stack are
5640 monotonically increasing, we do not have to care about
5641 stack overflows. */
5642 sp->prec = prec;
5643 sp->tree_type = tree_type;
5644 sp->lhs = lhs;
5645 sp++;
5646 lhs = rhs;
5647 prec = new_prec;
5648 new_prec = lookahead_prec;
5649 goto get_rhs;
5650
5651 pop:
5652 /* If the stack is not empty, we have parsed into LHS the right side
5653 (`4' in the example above) of an expression we had suspended.
5654 We can use the information on the stack to recover the LHS (`3')
5655 from the stack together with the tree code (`MULT_EXPR'), and
5656 the precedence of the higher level subexpression
5657 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5658 which will be used to actually build the additive expression. */
5659 --sp;
5660 prec = sp->prec;
5661 tree_type = sp->tree_type;
5662 rhs = lhs;
5663 lhs = sp->lhs;
5664 }
5665
5666 overloaded_p = false;
5667 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5668
5669 /* If the binary operator required the use of an overloaded operator,
5670 then this expression cannot be an integral constant-expression.
5671 An overloaded operator can be used even if both operands are
5672 otherwise permissible in an integral constant-expression if at
5673 least one of the operands is of enumeration type. */
5674
5675 if (overloaded_p
5676 && (cp_parser_non_integral_constant_expression
5677 (parser, "calls to overloaded operators")))
5678 return error_mark_node;
5679 }
5680
5681 return lhs;
5682 }
5683
5684
5685 /* Parse the `? expression : assignment-expression' part of a
5686 conditional-expression. The LOGICAL_OR_EXPR is the
5687 logical-or-expression that started the conditional-expression.
5688 Returns a representation of the entire conditional-expression.
5689
5690 This routine is used by cp_parser_assignment_expression.
5691
5692 ? expression : assignment-expression
5693
5694 GNU Extensions:
5695
5696 ? : assignment-expression */
5697
5698 static tree
5699 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5700 {
5701 tree expr;
5702 tree assignment_expr;
5703
5704 /* Consume the `?' token. */
5705 cp_lexer_consume_token (parser->lexer);
5706 if (cp_parser_allow_gnu_extensions_p (parser)
5707 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5708 /* Implicit true clause. */
5709 expr = NULL_TREE;
5710 else
5711 /* Parse the expression. */
5712 expr = cp_parser_expression (parser, /*cast_p=*/false);
5713
5714 /* The next token should be a `:'. */
5715 cp_parser_require (parser, CPP_COLON, "`:'");
5716 /* Parse the assignment-expression. */
5717 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5718
5719 /* Build the conditional-expression. */
5720 return build_x_conditional_expr (logical_or_expr,
5721 expr,
5722 assignment_expr);
5723 }
5724
5725 /* Parse an assignment-expression.
5726
5727 assignment-expression:
5728 conditional-expression
5729 logical-or-expression assignment-operator assignment_expression
5730 throw-expression
5731
5732 CAST_P is true if this expression is the target of a cast.
5733
5734 Returns a representation for the expression. */
5735
5736 static tree
5737 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5738 {
5739 tree expr;
5740
5741 /* If the next token is the `throw' keyword, then we're looking at
5742 a throw-expression. */
5743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5744 expr = cp_parser_throw_expression (parser);
5745 /* Otherwise, it must be that we are looking at a
5746 logical-or-expression. */
5747 else
5748 {
5749 /* Parse the binary expressions (logical-or-expression). */
5750 expr = cp_parser_binary_expression (parser, cast_p);
5751 /* If the next token is a `?' then we're actually looking at a
5752 conditional-expression. */
5753 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5754 return cp_parser_question_colon_clause (parser, expr);
5755 else
5756 {
5757 enum tree_code assignment_operator;
5758
5759 /* If it's an assignment-operator, we're using the second
5760 production. */
5761 assignment_operator
5762 = cp_parser_assignment_operator_opt (parser);
5763 if (assignment_operator != ERROR_MARK)
5764 {
5765 tree rhs;
5766
5767 /* Parse the right-hand side of the assignment. */
5768 rhs = cp_parser_assignment_expression (parser, cast_p);
5769 /* An assignment may not appear in a
5770 constant-expression. */
5771 if (cp_parser_non_integral_constant_expression (parser,
5772 "an assignment"))
5773 return error_mark_node;
5774 /* Build the assignment expression. */
5775 expr = build_x_modify_expr (expr,
5776 assignment_operator,
5777 rhs);
5778 }
5779 }
5780 }
5781
5782 return expr;
5783 }
5784
5785 /* Parse an (optional) assignment-operator.
5786
5787 assignment-operator: one of
5788 = *= /= %= += -= >>= <<= &= ^= |=
5789
5790 GNU Extension:
5791
5792 assignment-operator: one of
5793 <?= >?=
5794
5795 If the next token is an assignment operator, the corresponding tree
5796 code is returned, and the token is consumed. For example, for
5797 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5798 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5799 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5800 operator, ERROR_MARK is returned. */
5801
5802 static enum tree_code
5803 cp_parser_assignment_operator_opt (cp_parser* parser)
5804 {
5805 enum tree_code op;
5806 cp_token *token;
5807
5808 /* Peek at the next toen. */
5809 token = cp_lexer_peek_token (parser->lexer);
5810
5811 switch (token->type)
5812 {
5813 case CPP_EQ:
5814 op = NOP_EXPR;
5815 break;
5816
5817 case CPP_MULT_EQ:
5818 op = MULT_EXPR;
5819 break;
5820
5821 case CPP_DIV_EQ:
5822 op = TRUNC_DIV_EXPR;
5823 break;
5824
5825 case CPP_MOD_EQ:
5826 op = TRUNC_MOD_EXPR;
5827 break;
5828
5829 case CPP_PLUS_EQ:
5830 op = PLUS_EXPR;
5831 break;
5832
5833 case CPP_MINUS_EQ:
5834 op = MINUS_EXPR;
5835 break;
5836
5837 case CPP_RSHIFT_EQ:
5838 op = RSHIFT_EXPR;
5839 break;
5840
5841 case CPP_LSHIFT_EQ:
5842 op = LSHIFT_EXPR;
5843 break;
5844
5845 case CPP_AND_EQ:
5846 op = BIT_AND_EXPR;
5847 break;
5848
5849 case CPP_XOR_EQ:
5850 op = BIT_XOR_EXPR;
5851 break;
5852
5853 case CPP_OR_EQ:
5854 op = BIT_IOR_EXPR;
5855 break;
5856
5857 case CPP_MIN_EQ:
5858 op = MIN_EXPR;
5859 cp_parser_warn_min_max ();
5860 break;
5861
5862 case CPP_MAX_EQ:
5863 op = MAX_EXPR;
5864 cp_parser_warn_min_max ();
5865 break;
5866
5867 default:
5868 /* Nothing else is an assignment operator. */
5869 op = ERROR_MARK;
5870 }
5871
5872 /* If it was an assignment operator, consume it. */
5873 if (op != ERROR_MARK)
5874 cp_lexer_consume_token (parser->lexer);
5875
5876 return op;
5877 }
5878
5879 /* Parse an expression.
5880
5881 expression:
5882 assignment-expression
5883 expression , assignment-expression
5884
5885 CAST_P is true if this expression is the target of a cast.
5886
5887 Returns a representation of the expression. */
5888
5889 static tree
5890 cp_parser_expression (cp_parser* parser, bool cast_p)
5891 {
5892 tree expression = NULL_TREE;
5893
5894 while (true)
5895 {
5896 tree assignment_expression;
5897
5898 /* Parse the next assignment-expression. */
5899 assignment_expression
5900 = cp_parser_assignment_expression (parser, cast_p);
5901 /* If this is the first assignment-expression, we can just
5902 save it away. */
5903 if (!expression)
5904 expression = assignment_expression;
5905 else
5906 expression = build_x_compound_expr (expression,
5907 assignment_expression);
5908 /* If the next token is not a comma, then we are done with the
5909 expression. */
5910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5911 break;
5912 /* Consume the `,'. */
5913 cp_lexer_consume_token (parser->lexer);
5914 /* A comma operator cannot appear in a constant-expression. */
5915 if (cp_parser_non_integral_constant_expression (parser,
5916 "a comma operator"))
5917 expression = error_mark_node;
5918 }
5919
5920 return expression;
5921 }
5922
5923 /* Parse a constant-expression.
5924
5925 constant-expression:
5926 conditional-expression
5927
5928 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5929 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5930 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5931 is false, NON_CONSTANT_P should be NULL. */
5932
5933 static tree
5934 cp_parser_constant_expression (cp_parser* parser,
5935 bool allow_non_constant_p,
5936 bool *non_constant_p)
5937 {
5938 bool saved_integral_constant_expression_p;
5939 bool saved_allow_non_integral_constant_expression_p;
5940 bool saved_non_integral_constant_expression_p;
5941 tree expression;
5942
5943 /* It might seem that we could simply parse the
5944 conditional-expression, and then check to see if it were
5945 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5946 one that the compiler can figure out is constant, possibly after
5947 doing some simplifications or optimizations. The standard has a
5948 precise definition of constant-expression, and we must honor
5949 that, even though it is somewhat more restrictive.
5950
5951 For example:
5952
5953 int i[(2, 3)];
5954
5955 is not a legal declaration, because `(2, 3)' is not a
5956 constant-expression. The `,' operator is forbidden in a
5957 constant-expression. However, GCC's constant-folding machinery
5958 will fold this operation to an INTEGER_CST for `3'. */
5959
5960 /* Save the old settings. */
5961 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5962 saved_allow_non_integral_constant_expression_p
5963 = parser->allow_non_integral_constant_expression_p;
5964 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5965 /* We are now parsing a constant-expression. */
5966 parser->integral_constant_expression_p = true;
5967 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5968 parser->non_integral_constant_expression_p = false;
5969 /* Although the grammar says "conditional-expression", we parse an
5970 "assignment-expression", which also permits "throw-expression"
5971 and the use of assignment operators. In the case that
5972 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5973 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5974 actually essential that we look for an assignment-expression.
5975 For example, cp_parser_initializer_clauses uses this function to
5976 determine whether a particular assignment-expression is in fact
5977 constant. */
5978 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5979 /* Restore the old settings. */
5980 parser->integral_constant_expression_p
5981 = saved_integral_constant_expression_p;
5982 parser->allow_non_integral_constant_expression_p
5983 = saved_allow_non_integral_constant_expression_p;
5984 if (allow_non_constant_p)
5985 *non_constant_p = parser->non_integral_constant_expression_p;
5986 else if (parser->non_integral_constant_expression_p)
5987 expression = error_mark_node;
5988 parser->non_integral_constant_expression_p
5989 = saved_non_integral_constant_expression_p;
5990
5991 return expression;
5992 }
5993
5994 /* Parse __builtin_offsetof.
5995
5996 offsetof-expression:
5997 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5998
5999 offsetof-member-designator:
6000 id-expression
6001 | offsetof-member-designator "." id-expression
6002 | offsetof-member-designator "[" expression "]" */
6003
6004 static tree
6005 cp_parser_builtin_offsetof (cp_parser *parser)
6006 {
6007 int save_ice_p, save_non_ice_p;
6008 tree type, expr;
6009 cp_id_kind dummy;
6010
6011 /* We're about to accept non-integral-constant things, but will
6012 definitely yield an integral constant expression. Save and
6013 restore these values around our local parsing. */
6014 save_ice_p = parser->integral_constant_expression_p;
6015 save_non_ice_p = parser->non_integral_constant_expression_p;
6016
6017 /* Consume the "__builtin_offsetof" token. */
6018 cp_lexer_consume_token (parser->lexer);
6019 /* Consume the opening `('. */
6020 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6021 /* Parse the type-id. */
6022 type = cp_parser_type_id (parser);
6023 /* Look for the `,'. */
6024 cp_parser_require (parser, CPP_COMMA, "`,'");
6025
6026 /* Build the (type *)null that begins the traditional offsetof macro. */
6027 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6028
6029 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6030 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6031 true, &dummy);
6032 while (true)
6033 {
6034 cp_token *token = cp_lexer_peek_token (parser->lexer);
6035 switch (token->type)
6036 {
6037 case CPP_OPEN_SQUARE:
6038 /* offsetof-member-designator "[" expression "]" */
6039 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6040 break;
6041
6042 case CPP_DOT:
6043 /* offsetof-member-designator "." identifier */
6044 cp_lexer_consume_token (parser->lexer);
6045 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6046 true, &dummy);
6047 break;
6048
6049 case CPP_CLOSE_PAREN:
6050 /* Consume the ")" token. */
6051 cp_lexer_consume_token (parser->lexer);
6052 goto success;
6053
6054 default:
6055 /* Error. We know the following require will fail, but
6056 that gives the proper error message. */
6057 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6058 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6059 expr = error_mark_node;
6060 goto failure;
6061 }
6062 }
6063
6064 success:
6065 /* If we're processing a template, we can't finish the semantics yet.
6066 Otherwise we can fold the entire expression now. */
6067 if (processing_template_decl)
6068 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6069 else
6070 expr = finish_offsetof (expr);
6071
6072 failure:
6073 parser->integral_constant_expression_p = save_ice_p;
6074 parser->non_integral_constant_expression_p = save_non_ice_p;
6075
6076 return expr;
6077 }
6078
6079 /* Statements [gram.stmt.stmt] */
6080
6081 /* Parse a statement.
6082
6083 statement:
6084 labeled-statement
6085 expression-statement
6086 compound-statement
6087 selection-statement
6088 iteration-statement
6089 jump-statement
6090 declaration-statement
6091 try-block
6092
6093 IN_COMPOUND is true when the statement is nested inside a
6094 cp_parser_compound_statement; this matters for certain pragmas. */
6095
6096 static void
6097 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6098 bool in_compound)
6099 {
6100 tree statement;
6101 cp_token *token;
6102 location_t statement_location;
6103
6104 restart:
6105 /* There is no statement yet. */
6106 statement = NULL_TREE;
6107 /* Peek at the next token. */
6108 token = cp_lexer_peek_token (parser->lexer);
6109 /* Remember the location of the first token in the statement. */
6110 statement_location = token->location;
6111 /* If this is a keyword, then that will often determine what kind of
6112 statement we have. */
6113 if (token->type == CPP_KEYWORD)
6114 {
6115 enum rid keyword = token->keyword;
6116
6117 switch (keyword)
6118 {
6119 case RID_CASE:
6120 case RID_DEFAULT:
6121 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6122 in_compound);
6123 break;
6124
6125 case RID_IF:
6126 case RID_SWITCH:
6127 statement = cp_parser_selection_statement (parser);
6128 break;
6129
6130 case RID_WHILE:
6131 case RID_DO:
6132 case RID_FOR:
6133 statement = cp_parser_iteration_statement (parser);
6134 break;
6135
6136 case RID_BREAK:
6137 case RID_CONTINUE:
6138 case RID_RETURN:
6139 case RID_GOTO:
6140 statement = cp_parser_jump_statement (parser);
6141 break;
6142
6143 /* Objective-C++ exception-handling constructs. */
6144 case RID_AT_TRY:
6145 case RID_AT_CATCH:
6146 case RID_AT_FINALLY:
6147 case RID_AT_SYNCHRONIZED:
6148 case RID_AT_THROW:
6149 statement = cp_parser_objc_statement (parser);
6150 break;
6151
6152 case RID_TRY:
6153 statement = cp_parser_try_block (parser);
6154 break;
6155
6156 default:
6157 /* It might be a keyword like `int' that can start a
6158 declaration-statement. */
6159 break;
6160 }
6161 }
6162 else if (token->type == CPP_NAME)
6163 {
6164 /* If the next token is a `:', then we are looking at a
6165 labeled-statement. */
6166 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6167 if (token->type == CPP_COLON)
6168 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6169 in_compound);
6170 }
6171 /* Anything that starts with a `{' must be a compound-statement. */
6172 else if (token->type == CPP_OPEN_BRACE)
6173 statement = cp_parser_compound_statement (parser, NULL, false);
6174 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6175 a statement all its own. */
6176 else if (token->type == CPP_PRAGMA)
6177 {
6178 /* Only certain OpenMP pragmas are attached to statements, and thus
6179 are considered statements themselves. All others are not. In
6180 the context of a compound, accept the pragma as a "statement" and
6181 return so that we can check for a close brace. Otherwise we
6182 require a real statement and must go back and read one. */
6183 if (in_compound)
6184 cp_parser_pragma (parser, pragma_compound);
6185 else if (!cp_parser_pragma (parser, pragma_stmt))
6186 goto restart;
6187 return;
6188 }
6189 else if (token->type == CPP_EOF)
6190 {
6191 cp_parser_error (parser, "expected statement");
6192 return;
6193 }
6194
6195 /* Everything else must be a declaration-statement or an
6196 expression-statement. Try for the declaration-statement
6197 first, unless we are looking at a `;', in which case we know that
6198 we have an expression-statement. */
6199 if (!statement)
6200 {
6201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6202 {
6203 cp_parser_parse_tentatively (parser);
6204 /* Try to parse the declaration-statement. */
6205 cp_parser_declaration_statement (parser);
6206 /* If that worked, we're done. */
6207 if (cp_parser_parse_definitely (parser))
6208 return;
6209 }
6210 /* Look for an expression-statement instead. */
6211 statement = cp_parser_expression_statement (parser, in_statement_expr);
6212 }
6213
6214 /* Set the line number for the statement. */
6215 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6216 SET_EXPR_LOCATION (statement, statement_location);
6217 }
6218
6219 /* Parse a labeled-statement.
6220
6221 labeled-statement:
6222 identifier : statement
6223 case constant-expression : statement
6224 default : statement
6225
6226 GNU Extension:
6227
6228 labeled-statement:
6229 case constant-expression ... constant-expression : statement
6230
6231 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6232 For an ordinary label, returns a LABEL_EXPR.
6233
6234 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6235 inside a compound. */
6236
6237 static tree
6238 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6239 bool in_compound)
6240 {
6241 cp_token *token;
6242 tree statement = error_mark_node;
6243
6244 /* The next token should be an identifier. */
6245 token = cp_lexer_peek_token (parser->lexer);
6246 if (token->type != CPP_NAME
6247 && token->type != CPP_KEYWORD)
6248 {
6249 cp_parser_error (parser, "expected labeled-statement");
6250 return error_mark_node;
6251 }
6252
6253 switch (token->keyword)
6254 {
6255 case RID_CASE:
6256 {
6257 tree expr, expr_hi;
6258 cp_token *ellipsis;
6259
6260 /* Consume the `case' token. */
6261 cp_lexer_consume_token (parser->lexer);
6262 /* Parse the constant-expression. */
6263 expr = cp_parser_constant_expression (parser,
6264 /*allow_non_constant_p=*/false,
6265 NULL);
6266
6267 ellipsis = cp_lexer_peek_token (parser->lexer);
6268 if (ellipsis->type == CPP_ELLIPSIS)
6269 {
6270 /* Consume the `...' token. */
6271 cp_lexer_consume_token (parser->lexer);
6272 expr_hi =
6273 cp_parser_constant_expression (parser,
6274 /*allow_non_constant_p=*/false,
6275 NULL);
6276 /* We don't need to emit warnings here, as the common code
6277 will do this for us. */
6278 }
6279 else
6280 expr_hi = NULL_TREE;
6281
6282 if (parser->in_switch_statement_p)
6283 statement = finish_case_label (expr, expr_hi);
6284 else
6285 error ("case label %qE not within a switch statement", expr);
6286 }
6287 break;
6288
6289 case RID_DEFAULT:
6290 /* Consume the `default' token. */
6291 cp_lexer_consume_token (parser->lexer);
6292
6293 if (parser->in_switch_statement_p)
6294 statement = finish_case_label (NULL_TREE, NULL_TREE);
6295 else
6296 error ("case label not within a switch statement");
6297 break;
6298
6299 default:
6300 /* Anything else must be an ordinary label. */
6301 statement = finish_label_stmt (cp_parser_identifier (parser));
6302 break;
6303 }
6304
6305 /* Require the `:' token. */
6306 cp_parser_require (parser, CPP_COLON, "`:'");
6307 /* Parse the labeled statement. */
6308 cp_parser_statement (parser, in_statement_expr, in_compound);
6309
6310 /* Return the label, in the case of a `case' or `default' label. */
6311 return statement;
6312 }
6313
6314 /* Parse an expression-statement.
6315
6316 expression-statement:
6317 expression [opt] ;
6318
6319 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6320 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6321 indicates whether this expression-statement is part of an
6322 expression statement. */
6323
6324 static tree
6325 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6326 {
6327 tree statement = NULL_TREE;
6328
6329 /* If the next token is a ';', then there is no expression
6330 statement. */
6331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6332 statement = cp_parser_expression (parser, /*cast_p=*/false);
6333
6334 /* Consume the final `;'. */
6335 cp_parser_consume_semicolon_at_end_of_statement (parser);
6336
6337 if (in_statement_expr
6338 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6339 /* This is the final expression statement of a statement
6340 expression. */
6341 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6342 else if (statement)
6343 statement = finish_expr_stmt (statement);
6344 else
6345 finish_stmt ();
6346
6347 return statement;
6348 }
6349
6350 /* Parse a compound-statement.
6351
6352 compound-statement:
6353 { statement-seq [opt] }
6354
6355 Returns a tree representing the statement. */
6356
6357 static tree
6358 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6359 bool in_try)
6360 {
6361 tree compound_stmt;
6362
6363 /* Consume the `{'. */
6364 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6365 return error_mark_node;
6366 /* Begin the compound-statement. */
6367 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6368 /* Parse an (optional) statement-seq. */
6369 cp_parser_statement_seq_opt (parser, in_statement_expr);
6370 /* Finish the compound-statement. */
6371 finish_compound_stmt (compound_stmt);
6372 /* Consume the `}'. */
6373 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6374
6375 return compound_stmt;
6376 }
6377
6378 /* Parse an (optional) statement-seq.
6379
6380 statement-seq:
6381 statement
6382 statement-seq [opt] statement */
6383
6384 static void
6385 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6386 {
6387 /* Scan statements until there aren't any more. */
6388 while (true)
6389 {
6390 cp_token *token = cp_lexer_peek_token (parser->lexer);
6391
6392 /* If we're looking at a `}', then we've run out of statements. */
6393 if (token->type == CPP_CLOSE_BRACE
6394 || token->type == CPP_EOF
6395 || token->type == CPP_PRAGMA_EOL)
6396 break;
6397
6398 /* Parse the statement. */
6399 cp_parser_statement (parser, in_statement_expr, true);
6400 }
6401 }
6402
6403 /* Parse a selection-statement.
6404
6405 selection-statement:
6406 if ( condition ) statement
6407 if ( condition ) statement else statement
6408 switch ( condition ) statement
6409
6410 Returns the new IF_STMT or SWITCH_STMT. */
6411
6412 static tree
6413 cp_parser_selection_statement (cp_parser* parser)
6414 {
6415 cp_token *token;
6416 enum rid keyword;
6417
6418 /* Peek at the next token. */
6419 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6420
6421 /* See what kind of keyword it is. */
6422 keyword = token->keyword;
6423 switch (keyword)
6424 {
6425 case RID_IF:
6426 case RID_SWITCH:
6427 {
6428 tree statement;
6429 tree condition;
6430
6431 /* Look for the `('. */
6432 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6433 {
6434 cp_parser_skip_to_end_of_statement (parser);
6435 return error_mark_node;
6436 }
6437
6438 /* Begin the selection-statement. */
6439 if (keyword == RID_IF)
6440 statement = begin_if_stmt ();
6441 else
6442 statement = begin_switch_stmt ();
6443
6444 /* Parse the condition. */
6445 condition = cp_parser_condition (parser);
6446 /* Look for the `)'. */
6447 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6448 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6449 /*consume_paren=*/true);
6450
6451 if (keyword == RID_IF)
6452 {
6453 /* Add the condition. */
6454 finish_if_stmt_cond (condition, statement);
6455
6456 /* Parse the then-clause. */
6457 cp_parser_implicitly_scoped_statement (parser);
6458 finish_then_clause (statement);
6459
6460 /* If the next token is `else', parse the else-clause. */
6461 if (cp_lexer_next_token_is_keyword (parser->lexer,
6462 RID_ELSE))
6463 {
6464 /* Consume the `else' keyword. */
6465 cp_lexer_consume_token (parser->lexer);
6466 begin_else_clause (statement);
6467 /* Parse the else-clause. */
6468 cp_parser_implicitly_scoped_statement (parser);
6469 finish_else_clause (statement);
6470 }
6471
6472 /* Now we're all done with the if-statement. */
6473 finish_if_stmt (statement);
6474 }
6475 else
6476 {
6477 bool in_switch_statement_p;
6478 unsigned char in_statement;
6479
6480 /* Add the condition. */
6481 finish_switch_cond (condition, statement);
6482
6483 /* Parse the body of the switch-statement. */
6484 in_switch_statement_p = parser->in_switch_statement_p;
6485 in_statement = parser->in_statement;
6486 parser->in_switch_statement_p = true;
6487 parser->in_statement |= IN_SWITCH_STMT;
6488 cp_parser_implicitly_scoped_statement (parser);
6489 parser->in_switch_statement_p = in_switch_statement_p;
6490 parser->in_statement = in_statement;
6491
6492 /* Now we're all done with the switch-statement. */
6493 finish_switch_stmt (statement);
6494 }
6495
6496 return statement;
6497 }
6498 break;
6499
6500 default:
6501 cp_parser_error (parser, "expected selection-statement");
6502 return error_mark_node;
6503 }
6504 }
6505
6506 /* Parse a condition.
6507
6508 condition:
6509 expression
6510 type-specifier-seq declarator = assignment-expression
6511
6512 GNU Extension:
6513
6514 condition:
6515 type-specifier-seq declarator asm-specification [opt]
6516 attributes [opt] = assignment-expression
6517
6518 Returns the expression that should be tested. */
6519
6520 static tree
6521 cp_parser_condition (cp_parser* parser)
6522 {
6523 cp_decl_specifier_seq type_specifiers;
6524 const char *saved_message;
6525
6526 /* Try the declaration first. */
6527 cp_parser_parse_tentatively (parser);
6528 /* New types are not allowed in the type-specifier-seq for a
6529 condition. */
6530 saved_message = parser->type_definition_forbidden_message;
6531 parser->type_definition_forbidden_message
6532 = "types may not be defined in conditions";
6533 /* Parse the type-specifier-seq. */
6534 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6535 &type_specifiers);
6536 /* Restore the saved message. */
6537 parser->type_definition_forbidden_message = saved_message;
6538 /* If all is well, we might be looking at a declaration. */
6539 if (!cp_parser_error_occurred (parser))
6540 {
6541 tree decl;
6542 tree asm_specification;
6543 tree attributes;
6544 cp_declarator *declarator;
6545 tree initializer = NULL_TREE;
6546
6547 /* Parse the declarator. */
6548 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6549 /*ctor_dtor_or_conv_p=*/NULL,
6550 /*parenthesized_p=*/NULL,
6551 /*member_p=*/false);
6552 /* Parse the attributes. */
6553 attributes = cp_parser_attributes_opt (parser);
6554 /* Parse the asm-specification. */
6555 asm_specification = cp_parser_asm_specification_opt (parser);
6556 /* If the next token is not an `=', then we might still be
6557 looking at an expression. For example:
6558
6559 if (A(a).x)
6560
6561 looks like a decl-specifier-seq and a declarator -- but then
6562 there is no `=', so this is an expression. */
6563 cp_parser_require (parser, CPP_EQ, "`='");
6564 /* If we did see an `=', then we are looking at a declaration
6565 for sure. */
6566 if (cp_parser_parse_definitely (parser))
6567 {
6568 tree pushed_scope;
6569 bool non_constant_p;
6570
6571 /* Create the declaration. */
6572 decl = start_decl (declarator, &type_specifiers,
6573 /*initialized_p=*/true,
6574 attributes, /*prefix_attributes=*/NULL_TREE,
6575 &pushed_scope);
6576 /* Parse the assignment-expression. */
6577 initializer
6578 = cp_parser_constant_expression (parser,
6579 /*allow_non_constant_p=*/true,
6580 &non_constant_p);
6581 if (!non_constant_p)
6582 initializer = fold_non_dependent_expr (initializer);
6583
6584 /* Process the initializer. */
6585 cp_finish_decl (decl,
6586 initializer, !non_constant_p,
6587 asm_specification,
6588 LOOKUP_ONLYCONVERTING);
6589
6590 if (pushed_scope)
6591 pop_scope (pushed_scope);
6592
6593 return convert_from_reference (decl);
6594 }
6595 }
6596 /* If we didn't even get past the declarator successfully, we are
6597 definitely not looking at a declaration. */
6598 else
6599 cp_parser_abort_tentative_parse (parser);
6600
6601 /* Otherwise, we are looking at an expression. */
6602 return cp_parser_expression (parser, /*cast_p=*/false);
6603 }
6604
6605 /* Parse an iteration-statement.
6606
6607 iteration-statement:
6608 while ( condition ) statement
6609 do statement while ( expression ) ;
6610 for ( for-init-statement condition [opt] ; expression [opt] )
6611 statement
6612
6613 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6614
6615 static tree
6616 cp_parser_iteration_statement (cp_parser* parser)
6617 {
6618 cp_token *token;
6619 enum rid keyword;
6620 tree statement;
6621 unsigned char in_statement;
6622
6623 /* Peek at the next token. */
6624 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6625 if (!token)
6626 return error_mark_node;
6627
6628 /* Remember whether or not we are already within an iteration
6629 statement. */
6630 in_statement = parser->in_statement;
6631
6632 /* See what kind of keyword it is. */
6633 keyword = token->keyword;
6634 switch (keyword)
6635 {
6636 case RID_WHILE:
6637 {
6638 tree condition;
6639
6640 /* Begin the while-statement. */
6641 statement = begin_while_stmt ();
6642 /* Look for the `('. */
6643 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6644 /* Parse the condition. */
6645 condition = cp_parser_condition (parser);
6646 finish_while_stmt_cond (condition, statement);
6647 /* Look for the `)'. */
6648 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6649 /* Parse the dependent statement. */
6650 parser->in_statement = IN_ITERATION_STMT;
6651 cp_parser_already_scoped_statement (parser);
6652 parser->in_statement = in_statement;
6653 /* We're done with the while-statement. */
6654 finish_while_stmt (statement);
6655 }
6656 break;
6657
6658 case RID_DO:
6659 {
6660 tree expression;
6661
6662 /* Begin the do-statement. */
6663 statement = begin_do_stmt ();
6664 /* Parse the body of the do-statement. */
6665 parser->in_statement = IN_ITERATION_STMT;
6666 cp_parser_implicitly_scoped_statement (parser);
6667 parser->in_statement = in_statement;
6668 finish_do_body (statement);
6669 /* Look for the `while' keyword. */
6670 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6671 /* Look for the `('. */
6672 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6673 /* Parse the expression. */
6674 expression = cp_parser_expression (parser, /*cast_p=*/false);
6675 /* We're done with the do-statement. */
6676 finish_do_stmt (expression, statement);
6677 /* Look for the `)'. */
6678 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6679 /* Look for the `;'. */
6680 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6681 }
6682 break;
6683
6684 case RID_FOR:
6685 {
6686 tree condition = NULL_TREE;
6687 tree expression = NULL_TREE;
6688
6689 /* Begin the for-statement. */
6690 statement = begin_for_stmt ();
6691 /* Look for the `('. */
6692 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6693 /* Parse the initialization. */
6694 cp_parser_for_init_statement (parser);
6695 finish_for_init_stmt (statement);
6696
6697 /* If there's a condition, process it. */
6698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6699 condition = cp_parser_condition (parser);
6700 finish_for_cond (condition, statement);
6701 /* Look for the `;'. */
6702 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6703
6704 /* If there's an expression, process it. */
6705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6706 expression = cp_parser_expression (parser, /*cast_p=*/false);
6707 finish_for_expr (expression, statement);
6708 /* Look for the `)'. */
6709 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6710
6711 /* Parse the body of the for-statement. */
6712 parser->in_statement = IN_ITERATION_STMT;
6713 cp_parser_already_scoped_statement (parser);
6714 parser->in_statement = in_statement;
6715
6716 /* We're done with the for-statement. */
6717 finish_for_stmt (statement);
6718 }
6719 break;
6720
6721 default:
6722 cp_parser_error (parser, "expected iteration-statement");
6723 statement = error_mark_node;
6724 break;
6725 }
6726
6727 return statement;
6728 }
6729
6730 /* Parse a for-init-statement.
6731
6732 for-init-statement:
6733 expression-statement
6734 simple-declaration */
6735
6736 static void
6737 cp_parser_for_init_statement (cp_parser* parser)
6738 {
6739 /* If the next token is a `;', then we have an empty
6740 expression-statement. Grammatically, this is also a
6741 simple-declaration, but an invalid one, because it does not
6742 declare anything. Therefore, if we did not handle this case
6743 specially, we would issue an error message about an invalid
6744 declaration. */
6745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6746 {
6747 /* We're going to speculatively look for a declaration, falling back
6748 to an expression, if necessary. */
6749 cp_parser_parse_tentatively (parser);
6750 /* Parse the declaration. */
6751 cp_parser_simple_declaration (parser,
6752 /*function_definition_allowed_p=*/false);
6753 /* If the tentative parse failed, then we shall need to look for an
6754 expression-statement. */
6755 if (cp_parser_parse_definitely (parser))
6756 return;
6757 }
6758
6759 cp_parser_expression_statement (parser, false);
6760 }
6761
6762 /* Parse a jump-statement.
6763
6764 jump-statement:
6765 break ;
6766 continue ;
6767 return expression [opt] ;
6768 goto identifier ;
6769
6770 GNU extension:
6771
6772 jump-statement:
6773 goto * expression ;
6774
6775 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6776
6777 static tree
6778 cp_parser_jump_statement (cp_parser* parser)
6779 {
6780 tree statement = error_mark_node;
6781 cp_token *token;
6782 enum rid keyword;
6783
6784 /* Peek at the next token. */
6785 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6786 if (!token)
6787 return error_mark_node;
6788
6789 /* See what kind of keyword it is. */
6790 keyword = token->keyword;
6791 switch (keyword)
6792 {
6793 case RID_BREAK:
6794 switch (parser->in_statement)
6795 {
6796 case 0:
6797 error ("break statement not within loop or switch");
6798 break;
6799 default:
6800 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6801 || parser->in_statement == IN_ITERATION_STMT);
6802 statement = finish_break_stmt ();
6803 break;
6804 case IN_OMP_BLOCK:
6805 error ("invalid exit from OpenMP structured block");
6806 break;
6807 case IN_OMP_FOR:
6808 error ("break statement used with OpenMP for loop");
6809 break;
6810 }
6811 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6812 break;
6813
6814 case RID_CONTINUE:
6815 switch (parser->in_statement & ~IN_SWITCH_STMT)
6816 {
6817 case 0:
6818 error ("continue statement not within a loop");
6819 break;
6820 case IN_ITERATION_STMT:
6821 case IN_OMP_FOR:
6822 statement = finish_continue_stmt ();
6823 break;
6824 case IN_OMP_BLOCK:
6825 error ("invalid exit from OpenMP structured block");
6826 break;
6827 default:
6828 gcc_unreachable ();
6829 }
6830 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6831 break;
6832
6833 case RID_RETURN:
6834 {
6835 tree expr;
6836
6837 /* If the next token is a `;', then there is no
6838 expression. */
6839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6840 expr = cp_parser_expression (parser, /*cast_p=*/false);
6841 else
6842 expr = NULL_TREE;
6843 /* Build the return-statement. */
6844 statement = finish_return_stmt (expr);
6845 /* Look for the final `;'. */
6846 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6847 }
6848 break;
6849
6850 case RID_GOTO:
6851 /* Create the goto-statement. */
6852 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6853 {
6854 /* Issue a warning about this use of a GNU extension. */
6855 if (pedantic)
6856 pedwarn ("ISO C++ forbids computed gotos");
6857 /* Consume the '*' token. */
6858 cp_lexer_consume_token (parser->lexer);
6859 /* Parse the dependent expression. */
6860 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6861 }
6862 else
6863 finish_goto_stmt (cp_parser_identifier (parser));
6864 /* Look for the final `;'. */
6865 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6866 break;
6867
6868 default:
6869 cp_parser_error (parser, "expected jump-statement");
6870 break;
6871 }
6872
6873 return statement;
6874 }
6875
6876 /* Parse a declaration-statement.
6877
6878 declaration-statement:
6879 block-declaration */
6880
6881 static void
6882 cp_parser_declaration_statement (cp_parser* parser)
6883 {
6884 void *p;
6885
6886 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6887 p = obstack_alloc (&declarator_obstack, 0);
6888
6889 /* Parse the block-declaration. */
6890 cp_parser_block_declaration (parser, /*statement_p=*/true);
6891
6892 /* Free any declarators allocated. */
6893 obstack_free (&declarator_obstack, p);
6894
6895 /* Finish off the statement. */
6896 finish_stmt ();
6897 }
6898
6899 /* Some dependent statements (like `if (cond) statement'), are
6900 implicitly in their own scope. In other words, if the statement is
6901 a single statement (as opposed to a compound-statement), it is
6902 none-the-less treated as if it were enclosed in braces. Any
6903 declarations appearing in the dependent statement are out of scope
6904 after control passes that point. This function parses a statement,
6905 but ensures that is in its own scope, even if it is not a
6906 compound-statement.
6907
6908 Returns the new statement. */
6909
6910 static tree
6911 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6912 {
6913 tree statement;
6914
6915 /* Mark if () ; with a special NOP_EXPR. */
6916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6917 {
6918 cp_lexer_consume_token (parser->lexer);
6919 statement = add_stmt (build_empty_stmt ());
6920 }
6921 /* if a compound is opened, we simply parse the statement directly. */
6922 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6923 statement = cp_parser_compound_statement (parser, NULL, false);
6924 /* If the token is not a `{', then we must take special action. */
6925 else
6926 {
6927 /* Create a compound-statement. */
6928 statement = begin_compound_stmt (0);
6929 /* Parse the dependent-statement. */
6930 cp_parser_statement (parser, NULL_TREE, false);
6931 /* Finish the dummy compound-statement. */
6932 finish_compound_stmt (statement);
6933 }
6934
6935 /* Return the statement. */
6936 return statement;
6937 }
6938
6939 /* For some dependent statements (like `while (cond) statement'), we
6940 have already created a scope. Therefore, even if the dependent
6941 statement is a compound-statement, we do not want to create another
6942 scope. */
6943
6944 static void
6945 cp_parser_already_scoped_statement (cp_parser* parser)
6946 {
6947 /* If the token is a `{', then we must take special action. */
6948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6949 cp_parser_statement (parser, NULL_TREE, false);
6950 else
6951 {
6952 /* Avoid calling cp_parser_compound_statement, so that we
6953 don't create a new scope. Do everything else by hand. */
6954 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6955 cp_parser_statement_seq_opt (parser, NULL_TREE);
6956 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6957 }
6958 }
6959
6960 /* Declarations [gram.dcl.dcl] */
6961
6962 /* Parse an optional declaration-sequence.
6963
6964 declaration-seq:
6965 declaration
6966 declaration-seq declaration */
6967
6968 static void
6969 cp_parser_declaration_seq_opt (cp_parser* parser)
6970 {
6971 while (true)
6972 {
6973 cp_token *token;
6974
6975 token = cp_lexer_peek_token (parser->lexer);
6976
6977 if (token->type == CPP_CLOSE_BRACE
6978 || token->type == CPP_EOF
6979 || token->type == CPP_PRAGMA_EOL)
6980 break;
6981
6982 if (token->type == CPP_SEMICOLON)
6983 {
6984 /* A declaration consisting of a single semicolon is
6985 invalid. Allow it unless we're being pedantic. */
6986 cp_lexer_consume_token (parser->lexer);
6987 if (pedantic && !in_system_header)
6988 pedwarn ("extra %<;%>");
6989 continue;
6990 }
6991
6992 /* If we're entering or exiting a region that's implicitly
6993 extern "C", modify the lang context appropriately. */
6994 if (!parser->implicit_extern_c && token->implicit_extern_c)
6995 {
6996 push_lang_context (lang_name_c);
6997 parser->implicit_extern_c = true;
6998 }
6999 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7000 {
7001 pop_lang_context ();
7002 parser->implicit_extern_c = false;
7003 }
7004
7005 if (token->type == CPP_PRAGMA)
7006 {
7007 /* A top-level declaration can consist solely of a #pragma.
7008 A nested declaration cannot, so this is done here and not
7009 in cp_parser_declaration. (A #pragma at block scope is
7010 handled in cp_parser_statement.) */
7011 cp_parser_pragma (parser, pragma_external);
7012 continue;
7013 }
7014
7015 /* Parse the declaration itself. */
7016 cp_parser_declaration (parser);
7017 }
7018 }
7019
7020 /* Parse a declaration.
7021
7022 declaration:
7023 block-declaration
7024 function-definition
7025 template-declaration
7026 explicit-instantiation
7027 explicit-specialization
7028 linkage-specification
7029 namespace-definition
7030
7031 GNU extension:
7032
7033 declaration:
7034 __extension__ declaration */
7035
7036 static void
7037 cp_parser_declaration (cp_parser* parser)
7038 {
7039 cp_token token1;
7040 cp_token token2;
7041 int saved_pedantic;
7042 void *p;
7043
7044 /* Check for the `__extension__' keyword. */
7045 if (cp_parser_extension_opt (parser, &saved_pedantic))
7046 {
7047 /* Parse the qualified declaration. */
7048 cp_parser_declaration (parser);
7049 /* Restore the PEDANTIC flag. */
7050 pedantic = saved_pedantic;
7051
7052 return;
7053 }
7054
7055 /* Try to figure out what kind of declaration is present. */
7056 token1 = *cp_lexer_peek_token (parser->lexer);
7057
7058 if (token1.type != CPP_EOF)
7059 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7060 else
7061 {
7062 token2.type = CPP_EOF;
7063 token2.keyword = RID_MAX;
7064 }
7065
7066 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7067 p = obstack_alloc (&declarator_obstack, 0);
7068
7069 /* If the next token is `extern' and the following token is a string
7070 literal, then we have a linkage specification. */
7071 if (token1.keyword == RID_EXTERN
7072 && cp_parser_is_string_literal (&token2))
7073 cp_parser_linkage_specification (parser);
7074 /* If the next token is `template', then we have either a template
7075 declaration, an explicit instantiation, or an explicit
7076 specialization. */
7077 else if (token1.keyword == RID_TEMPLATE)
7078 {
7079 /* `template <>' indicates a template specialization. */
7080 if (token2.type == CPP_LESS
7081 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7082 cp_parser_explicit_specialization (parser);
7083 /* `template <' indicates a template declaration. */
7084 else if (token2.type == CPP_LESS)
7085 cp_parser_template_declaration (parser, /*member_p=*/false);
7086 /* Anything else must be an explicit instantiation. */
7087 else
7088 cp_parser_explicit_instantiation (parser);
7089 }
7090 /* If the next token is `export', then we have a template
7091 declaration. */
7092 else if (token1.keyword == RID_EXPORT)
7093 cp_parser_template_declaration (parser, /*member_p=*/false);
7094 /* If the next token is `extern', 'static' or 'inline' and the one
7095 after that is `template', we have a GNU extended explicit
7096 instantiation directive. */
7097 else if (cp_parser_allow_gnu_extensions_p (parser)
7098 && (token1.keyword == RID_EXTERN
7099 || token1.keyword == RID_STATIC
7100 || token1.keyword == RID_INLINE)
7101 && token2.keyword == RID_TEMPLATE)
7102 cp_parser_explicit_instantiation (parser);
7103 /* If the next token is `namespace', check for a named or unnamed
7104 namespace definition. */
7105 else if (token1.keyword == RID_NAMESPACE
7106 && (/* A named namespace definition. */
7107 (token2.type == CPP_NAME
7108 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7109 != CPP_EQ))
7110 /* An unnamed namespace definition. */
7111 || token2.type == CPP_OPEN_BRACE
7112 || token2.keyword == RID_ATTRIBUTE))
7113 cp_parser_namespace_definition (parser);
7114 /* Objective-C++ declaration/definition. */
7115 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7116 cp_parser_objc_declaration (parser);
7117 /* We must have either a block declaration or a function
7118 definition. */
7119 else
7120 /* Try to parse a block-declaration, or a function-definition. */
7121 cp_parser_block_declaration (parser, /*statement_p=*/false);
7122
7123 /* Free any declarators allocated. */
7124 obstack_free (&declarator_obstack, p);
7125 }
7126
7127 /* Parse a block-declaration.
7128
7129 block-declaration:
7130 simple-declaration
7131 asm-definition
7132 namespace-alias-definition
7133 using-declaration
7134 using-directive
7135
7136 GNU Extension:
7137
7138 block-declaration:
7139 __extension__ block-declaration
7140 label-declaration
7141
7142 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7143 part of a declaration-statement. */
7144
7145 static void
7146 cp_parser_block_declaration (cp_parser *parser,
7147 bool statement_p)
7148 {
7149 cp_token *token1;
7150 int saved_pedantic;
7151
7152 /* Check for the `__extension__' keyword. */
7153 if (cp_parser_extension_opt (parser, &saved_pedantic))
7154 {
7155 /* Parse the qualified declaration. */
7156 cp_parser_block_declaration (parser, statement_p);
7157 /* Restore the PEDANTIC flag. */
7158 pedantic = saved_pedantic;
7159
7160 return;
7161 }
7162
7163 /* Peek at the next token to figure out which kind of declaration is
7164 present. */
7165 token1 = cp_lexer_peek_token (parser->lexer);
7166
7167 /* If the next keyword is `asm', we have an asm-definition. */
7168 if (token1->keyword == RID_ASM)
7169 {
7170 if (statement_p)
7171 cp_parser_commit_to_tentative_parse (parser);
7172 cp_parser_asm_definition (parser);
7173 }
7174 /* If the next keyword is `namespace', we have a
7175 namespace-alias-definition. */
7176 else if (token1->keyword == RID_NAMESPACE)
7177 cp_parser_namespace_alias_definition (parser);
7178 /* If the next keyword is `using', we have either a
7179 using-declaration or a using-directive. */
7180 else if (token1->keyword == RID_USING)
7181 {
7182 cp_token *token2;
7183
7184 if (statement_p)
7185 cp_parser_commit_to_tentative_parse (parser);
7186 /* If the token after `using' is `namespace', then we have a
7187 using-directive. */
7188 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7189 if (token2->keyword == RID_NAMESPACE)
7190 cp_parser_using_directive (parser);
7191 /* Otherwise, it's a using-declaration. */
7192 else
7193 cp_parser_using_declaration (parser);
7194 }
7195 /* If the next keyword is `__label__' we have a label declaration. */
7196 else if (token1->keyword == RID_LABEL)
7197 {
7198 if (statement_p)
7199 cp_parser_commit_to_tentative_parse (parser);
7200 cp_parser_label_declaration (parser);
7201 }
7202 /* Anything else must be a simple-declaration. */
7203 else
7204 cp_parser_simple_declaration (parser, !statement_p);
7205 }
7206
7207 /* Parse a simple-declaration.
7208
7209 simple-declaration:
7210 decl-specifier-seq [opt] init-declarator-list [opt] ;
7211
7212 init-declarator-list:
7213 init-declarator
7214 init-declarator-list , init-declarator
7215
7216 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7217 function-definition as a simple-declaration. */
7218
7219 static void
7220 cp_parser_simple_declaration (cp_parser* parser,
7221 bool function_definition_allowed_p)
7222 {
7223 cp_decl_specifier_seq decl_specifiers;
7224 int declares_class_or_enum;
7225 bool saw_declarator;
7226
7227 /* Defer access checks until we know what is being declared; the
7228 checks for names appearing in the decl-specifier-seq should be
7229 done as if we were in the scope of the thing being declared. */
7230 push_deferring_access_checks (dk_deferred);
7231
7232 /* Parse the decl-specifier-seq. We have to keep track of whether
7233 or not the decl-specifier-seq declares a named class or
7234 enumeration type, since that is the only case in which the
7235 init-declarator-list is allowed to be empty.
7236
7237 [dcl.dcl]
7238
7239 In a simple-declaration, the optional init-declarator-list can be
7240 omitted only when declaring a class or enumeration, that is when
7241 the decl-specifier-seq contains either a class-specifier, an
7242 elaborated-type-specifier, or an enum-specifier. */
7243 cp_parser_decl_specifier_seq (parser,
7244 CP_PARSER_FLAGS_OPTIONAL,
7245 &decl_specifiers,
7246 &declares_class_or_enum);
7247 /* We no longer need to defer access checks. */
7248 stop_deferring_access_checks ();
7249
7250 /* In a block scope, a valid declaration must always have a
7251 decl-specifier-seq. By not trying to parse declarators, we can
7252 resolve the declaration/expression ambiguity more quickly. */
7253 if (!function_definition_allowed_p
7254 && !decl_specifiers.any_specifiers_p)
7255 {
7256 cp_parser_error (parser, "expected declaration");
7257 goto done;
7258 }
7259
7260 /* If the next two tokens are both identifiers, the code is
7261 erroneous. The usual cause of this situation is code like:
7262
7263 T t;
7264
7265 where "T" should name a type -- but does not. */
7266 if (!decl_specifiers.type
7267 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7268 {
7269 /* If parsing tentatively, we should commit; we really are
7270 looking at a declaration. */
7271 cp_parser_commit_to_tentative_parse (parser);
7272 /* Give up. */
7273 goto done;
7274 }
7275
7276 /* If we have seen at least one decl-specifier, and the next token
7277 is not a parenthesis, then we must be looking at a declaration.
7278 (After "int (" we might be looking at a functional cast.) */
7279 if (decl_specifiers.any_specifiers_p
7280 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7281 cp_parser_commit_to_tentative_parse (parser);
7282
7283 /* Keep going until we hit the `;' at the end of the simple
7284 declaration. */
7285 saw_declarator = false;
7286 while (cp_lexer_next_token_is_not (parser->lexer,
7287 CPP_SEMICOLON))
7288 {
7289 cp_token *token;
7290 bool function_definition_p;
7291 tree decl;
7292
7293 if (saw_declarator)
7294 {
7295 /* If we are processing next declarator, coma is expected */
7296 token = cp_lexer_peek_token (parser->lexer);
7297 gcc_assert (token->type == CPP_COMMA);
7298 cp_lexer_consume_token (parser->lexer);
7299 }
7300 else
7301 saw_declarator = true;
7302
7303 /* Parse the init-declarator. */
7304 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7305 /*checks=*/NULL_TREE,
7306 function_definition_allowed_p,
7307 /*member_p=*/false,
7308 declares_class_or_enum,
7309 &function_definition_p);
7310 /* If an error occurred while parsing tentatively, exit quickly.
7311 (That usually happens when in the body of a function; each
7312 statement is treated as a declaration-statement until proven
7313 otherwise.) */
7314 if (cp_parser_error_occurred (parser))
7315 goto done;
7316 /* Handle function definitions specially. */
7317 if (function_definition_p)
7318 {
7319 /* If the next token is a `,', then we are probably
7320 processing something like:
7321
7322 void f() {}, *p;
7323
7324 which is erroneous. */
7325 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7326 error ("mixing declarations and function-definitions is forbidden");
7327 /* Otherwise, we're done with the list of declarators. */
7328 else
7329 {
7330 pop_deferring_access_checks ();
7331 return;
7332 }
7333 }
7334 /* The next token should be either a `,' or a `;'. */
7335 token = cp_lexer_peek_token (parser->lexer);
7336 /* If it's a `,', there are more declarators to come. */
7337 if (token->type == CPP_COMMA)
7338 /* will be consumed next time around */;
7339 /* If it's a `;', we are done. */
7340 else if (token->type == CPP_SEMICOLON)
7341 break;
7342 /* Anything else is an error. */
7343 else
7344 {
7345 /* If we have already issued an error message we don't need
7346 to issue another one. */
7347 if (decl != error_mark_node
7348 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7349 cp_parser_error (parser, "expected %<,%> or %<;%>");
7350 /* Skip tokens until we reach the end of the statement. */
7351 cp_parser_skip_to_end_of_statement (parser);
7352 /* If the next token is now a `;', consume it. */
7353 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7354 cp_lexer_consume_token (parser->lexer);
7355 goto done;
7356 }
7357 /* After the first time around, a function-definition is not
7358 allowed -- even if it was OK at first. For example:
7359
7360 int i, f() {}
7361
7362 is not valid. */
7363 function_definition_allowed_p = false;
7364 }
7365
7366 /* Issue an error message if no declarators are present, and the
7367 decl-specifier-seq does not itself declare a class or
7368 enumeration. */
7369 if (!saw_declarator)
7370 {
7371 if (cp_parser_declares_only_class_p (parser))
7372 shadow_tag (&decl_specifiers);
7373 /* Perform any deferred access checks. */
7374 perform_deferred_access_checks ();
7375 }
7376
7377 /* Consume the `;'. */
7378 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7379
7380 done:
7381 pop_deferring_access_checks ();
7382 }
7383
7384 /* Parse a decl-specifier-seq.
7385
7386 decl-specifier-seq:
7387 decl-specifier-seq [opt] decl-specifier
7388
7389 decl-specifier:
7390 storage-class-specifier
7391 type-specifier
7392 function-specifier
7393 friend
7394 typedef
7395
7396 GNU Extension:
7397
7398 decl-specifier:
7399 attributes
7400
7401 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7402
7403 The parser flags FLAGS is used to control type-specifier parsing.
7404
7405 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7406 flags:
7407
7408 1: one of the decl-specifiers is an elaborated-type-specifier
7409 (i.e., a type declaration)
7410 2: one of the decl-specifiers is an enum-specifier or a
7411 class-specifier (i.e., a type definition)
7412
7413 */
7414
7415 static void
7416 cp_parser_decl_specifier_seq (cp_parser* parser,
7417 cp_parser_flags flags,
7418 cp_decl_specifier_seq *decl_specs,
7419 int* declares_class_or_enum)
7420 {
7421 bool constructor_possible_p = !parser->in_declarator_p;
7422
7423 /* Clear DECL_SPECS. */
7424 clear_decl_specs (decl_specs);
7425
7426 /* Assume no class or enumeration type is declared. */
7427 *declares_class_or_enum = 0;
7428
7429 /* Keep reading specifiers until there are no more to read. */
7430 while (true)
7431 {
7432 bool constructor_p;
7433 bool found_decl_spec;
7434 cp_token *token;
7435
7436 /* Peek at the next token. */
7437 token = cp_lexer_peek_token (parser->lexer);
7438 /* Handle attributes. */
7439 if (token->keyword == RID_ATTRIBUTE)
7440 {
7441 /* Parse the attributes. */
7442 decl_specs->attributes
7443 = chainon (decl_specs->attributes,
7444 cp_parser_attributes_opt (parser));
7445 continue;
7446 }
7447 /* Assume we will find a decl-specifier keyword. */
7448 found_decl_spec = true;
7449 /* If the next token is an appropriate keyword, we can simply
7450 add it to the list. */
7451 switch (token->keyword)
7452 {
7453 /* decl-specifier:
7454 friend */
7455 case RID_FRIEND:
7456 if (!at_class_scope_p ())
7457 {
7458 error ("%<friend%> used outside of class");
7459 cp_lexer_purge_token (parser->lexer);
7460 }
7461 else
7462 {
7463 ++decl_specs->specs[(int) ds_friend];
7464 /* Consume the token. */
7465 cp_lexer_consume_token (parser->lexer);
7466 }
7467 break;
7468
7469 /* function-specifier:
7470 inline
7471 virtual
7472 explicit */
7473 case RID_INLINE:
7474 case RID_VIRTUAL:
7475 case RID_EXPLICIT:
7476 cp_parser_function_specifier_opt (parser, decl_specs);
7477 break;
7478
7479 /* decl-specifier:
7480 typedef */
7481 case RID_TYPEDEF:
7482 ++decl_specs->specs[(int) ds_typedef];
7483 /* Consume the token. */
7484 cp_lexer_consume_token (parser->lexer);
7485 /* A constructor declarator cannot appear in a typedef. */
7486 constructor_possible_p = false;
7487 /* The "typedef" keyword can only occur in a declaration; we
7488 may as well commit at this point. */
7489 cp_parser_commit_to_tentative_parse (parser);
7490 break;
7491
7492 /* storage-class-specifier:
7493 auto
7494 register
7495 static
7496 extern
7497 mutable
7498
7499 GNU Extension:
7500 thread */
7501 case RID_AUTO:
7502 case RID_REGISTER:
7503 case RID_STATIC:
7504 case RID_EXTERN:
7505 case RID_MUTABLE:
7506 /* Consume the token. */
7507 cp_lexer_consume_token (parser->lexer);
7508 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7509 break;
7510 case RID_THREAD:
7511 /* Consume the token. */
7512 cp_lexer_consume_token (parser->lexer);
7513 ++decl_specs->specs[(int) ds_thread];
7514 break;
7515
7516 default:
7517 /* We did not yet find a decl-specifier yet. */
7518 found_decl_spec = false;
7519 break;
7520 }
7521
7522 /* Constructors are a special case. The `S' in `S()' is not a
7523 decl-specifier; it is the beginning of the declarator. */
7524 constructor_p
7525 = (!found_decl_spec
7526 && constructor_possible_p
7527 && (cp_parser_constructor_declarator_p
7528 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7529
7530 /* If we don't have a DECL_SPEC yet, then we must be looking at
7531 a type-specifier. */
7532 if (!found_decl_spec && !constructor_p)
7533 {
7534 int decl_spec_declares_class_or_enum;
7535 bool is_cv_qualifier;
7536 tree type_spec;
7537
7538 type_spec
7539 = cp_parser_type_specifier (parser, flags,
7540 decl_specs,
7541 /*is_declaration=*/true,
7542 &decl_spec_declares_class_or_enum,
7543 &is_cv_qualifier);
7544
7545 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7546
7547 /* If this type-specifier referenced a user-defined type
7548 (a typedef, class-name, etc.), then we can't allow any
7549 more such type-specifiers henceforth.
7550
7551 [dcl.spec]
7552
7553 The longest sequence of decl-specifiers that could
7554 possibly be a type name is taken as the
7555 decl-specifier-seq of a declaration. The sequence shall
7556 be self-consistent as described below.
7557
7558 [dcl.type]
7559
7560 As a general rule, at most one type-specifier is allowed
7561 in the complete decl-specifier-seq of a declaration. The
7562 only exceptions are the following:
7563
7564 -- const or volatile can be combined with any other
7565 type-specifier.
7566
7567 -- signed or unsigned can be combined with char, long,
7568 short, or int.
7569
7570 -- ..
7571
7572 Example:
7573
7574 typedef char* Pc;
7575 void g (const int Pc);
7576
7577 Here, Pc is *not* part of the decl-specifier seq; it's
7578 the declarator. Therefore, once we see a type-specifier
7579 (other than a cv-qualifier), we forbid any additional
7580 user-defined types. We *do* still allow things like `int
7581 int' to be considered a decl-specifier-seq, and issue the
7582 error message later. */
7583 if (type_spec && !is_cv_qualifier)
7584 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7585 /* A constructor declarator cannot follow a type-specifier. */
7586 if (type_spec)
7587 {
7588 constructor_possible_p = false;
7589 found_decl_spec = true;
7590 }
7591 }
7592
7593 /* If we still do not have a DECL_SPEC, then there are no more
7594 decl-specifiers. */
7595 if (!found_decl_spec)
7596 break;
7597
7598 decl_specs->any_specifiers_p = true;
7599 /* After we see one decl-specifier, further decl-specifiers are
7600 always optional. */
7601 flags |= CP_PARSER_FLAGS_OPTIONAL;
7602 }
7603
7604 cp_parser_check_decl_spec (decl_specs);
7605
7606 /* Don't allow a friend specifier with a class definition. */
7607 if (decl_specs->specs[(int) ds_friend] != 0
7608 && (*declares_class_or_enum & 2))
7609 error ("class definition may not be declared a friend");
7610 }
7611
7612 /* Parse an (optional) storage-class-specifier.
7613
7614 storage-class-specifier:
7615 auto
7616 register
7617 static
7618 extern
7619 mutable
7620
7621 GNU Extension:
7622
7623 storage-class-specifier:
7624 thread
7625
7626 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7627
7628 static tree
7629 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7630 {
7631 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7632 {
7633 case RID_AUTO:
7634 case RID_REGISTER:
7635 case RID_STATIC:
7636 case RID_EXTERN:
7637 case RID_MUTABLE:
7638 case RID_THREAD:
7639 /* Consume the token. */
7640 return cp_lexer_consume_token (parser->lexer)->value;
7641
7642 default:
7643 return NULL_TREE;
7644 }
7645 }
7646
7647 /* Parse an (optional) function-specifier.
7648
7649 function-specifier:
7650 inline
7651 virtual
7652 explicit
7653
7654 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7655 Updates DECL_SPECS, if it is non-NULL. */
7656
7657 static tree
7658 cp_parser_function_specifier_opt (cp_parser* parser,
7659 cp_decl_specifier_seq *decl_specs)
7660 {
7661 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7662 {
7663 case RID_INLINE:
7664 if (decl_specs)
7665 ++decl_specs->specs[(int) ds_inline];
7666 break;
7667
7668 case RID_VIRTUAL:
7669 /* 14.5.2.3 [temp.mem]
7670
7671 A member function template shall not be virtual. */
7672 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7673 error ("templates may not be %<virtual%>");
7674 else if (decl_specs)
7675 ++decl_specs->specs[(int) ds_virtual];
7676 break;
7677
7678 case RID_EXPLICIT:
7679 if (decl_specs)
7680 ++decl_specs->specs[(int) ds_explicit];
7681 break;
7682
7683 default:
7684 return NULL_TREE;
7685 }
7686
7687 /* Consume the token. */
7688 return cp_lexer_consume_token (parser->lexer)->value;
7689 }
7690
7691 /* Parse a linkage-specification.
7692
7693 linkage-specification:
7694 extern string-literal { declaration-seq [opt] }
7695 extern string-literal declaration */
7696
7697 static void
7698 cp_parser_linkage_specification (cp_parser* parser)
7699 {
7700 tree linkage;
7701
7702 /* Look for the `extern' keyword. */
7703 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7704
7705 /* Look for the string-literal. */
7706 linkage = cp_parser_string_literal (parser, false, false);
7707
7708 /* Transform the literal into an identifier. If the literal is a
7709 wide-character string, or contains embedded NULs, then we can't
7710 handle it as the user wants. */
7711 if (strlen (TREE_STRING_POINTER (linkage))
7712 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7713 {
7714 cp_parser_error (parser, "invalid linkage-specification");
7715 /* Assume C++ linkage. */
7716 linkage = lang_name_cplusplus;
7717 }
7718 else
7719 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7720
7721 /* We're now using the new linkage. */
7722 push_lang_context (linkage);
7723
7724 /* If the next token is a `{', then we're using the first
7725 production. */
7726 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7727 {
7728 /* Consume the `{' token. */
7729 cp_lexer_consume_token (parser->lexer);
7730 /* Parse the declarations. */
7731 cp_parser_declaration_seq_opt (parser);
7732 /* Look for the closing `}'. */
7733 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7734 }
7735 /* Otherwise, there's just one declaration. */
7736 else
7737 {
7738 bool saved_in_unbraced_linkage_specification_p;
7739
7740 saved_in_unbraced_linkage_specification_p
7741 = parser->in_unbraced_linkage_specification_p;
7742 parser->in_unbraced_linkage_specification_p = true;
7743 cp_parser_declaration (parser);
7744 parser->in_unbraced_linkage_specification_p
7745 = saved_in_unbraced_linkage_specification_p;
7746 }
7747
7748 /* We're done with the linkage-specification. */
7749 pop_lang_context ();
7750 }
7751
7752 /* Special member functions [gram.special] */
7753
7754 /* Parse a conversion-function-id.
7755
7756 conversion-function-id:
7757 operator conversion-type-id
7758
7759 Returns an IDENTIFIER_NODE representing the operator. */
7760
7761 static tree
7762 cp_parser_conversion_function_id (cp_parser* parser)
7763 {
7764 tree type;
7765 tree saved_scope;
7766 tree saved_qualifying_scope;
7767 tree saved_object_scope;
7768 tree pushed_scope = NULL_TREE;
7769
7770 /* Look for the `operator' token. */
7771 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7772 return error_mark_node;
7773 /* When we parse the conversion-type-id, the current scope will be
7774 reset. However, we need that information in able to look up the
7775 conversion function later, so we save it here. */
7776 saved_scope = parser->scope;
7777 saved_qualifying_scope = parser->qualifying_scope;
7778 saved_object_scope = parser->object_scope;
7779 /* We must enter the scope of the class so that the names of
7780 entities declared within the class are available in the
7781 conversion-type-id. For example, consider:
7782
7783 struct S {
7784 typedef int I;
7785 operator I();
7786 };
7787
7788 S::operator I() { ... }
7789
7790 In order to see that `I' is a type-name in the definition, we
7791 must be in the scope of `S'. */
7792 if (saved_scope)
7793 pushed_scope = push_scope (saved_scope);
7794 /* Parse the conversion-type-id. */
7795 type = cp_parser_conversion_type_id (parser);
7796 /* Leave the scope of the class, if any. */
7797 if (pushed_scope)
7798 pop_scope (pushed_scope);
7799 /* Restore the saved scope. */
7800 parser->scope = saved_scope;
7801 parser->qualifying_scope = saved_qualifying_scope;
7802 parser->object_scope = saved_object_scope;
7803 /* If the TYPE is invalid, indicate failure. */
7804 if (type == error_mark_node)
7805 return error_mark_node;
7806 return mangle_conv_op_name_for_type (type);
7807 }
7808
7809 /* Parse a conversion-type-id:
7810
7811 conversion-type-id:
7812 type-specifier-seq conversion-declarator [opt]
7813
7814 Returns the TYPE specified. */
7815
7816 static tree
7817 cp_parser_conversion_type_id (cp_parser* parser)
7818 {
7819 tree attributes;
7820 cp_decl_specifier_seq type_specifiers;
7821 cp_declarator *declarator;
7822 tree type_specified;
7823
7824 /* Parse the attributes. */
7825 attributes = cp_parser_attributes_opt (parser);
7826 /* Parse the type-specifiers. */
7827 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7828 &type_specifiers);
7829 /* If that didn't work, stop. */
7830 if (type_specifiers.type == error_mark_node)
7831 return error_mark_node;
7832 /* Parse the conversion-declarator. */
7833 declarator = cp_parser_conversion_declarator_opt (parser);
7834
7835 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7836 /*initialized=*/0, &attributes);
7837 if (attributes)
7838 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7839 return type_specified;
7840 }
7841
7842 /* Parse an (optional) conversion-declarator.
7843
7844 conversion-declarator:
7845 ptr-operator conversion-declarator [opt]
7846
7847 */
7848
7849 static cp_declarator *
7850 cp_parser_conversion_declarator_opt (cp_parser* parser)
7851 {
7852 enum tree_code code;
7853 tree class_type;
7854 cp_cv_quals cv_quals;
7855
7856 /* We don't know if there's a ptr-operator next, or not. */
7857 cp_parser_parse_tentatively (parser);
7858 /* Try the ptr-operator. */
7859 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7860 /* If it worked, look for more conversion-declarators. */
7861 if (cp_parser_parse_definitely (parser))
7862 {
7863 cp_declarator *declarator;
7864
7865 /* Parse another optional declarator. */
7866 declarator = cp_parser_conversion_declarator_opt (parser);
7867
7868 /* Create the representation of the declarator. */
7869 if (class_type)
7870 declarator = make_ptrmem_declarator (cv_quals, class_type,
7871 declarator);
7872 else if (code == INDIRECT_REF)
7873 declarator = make_pointer_declarator (cv_quals, declarator);
7874 else
7875 declarator = make_reference_declarator (cv_quals, declarator);
7876
7877 return declarator;
7878 }
7879
7880 return NULL;
7881 }
7882
7883 /* Parse an (optional) ctor-initializer.
7884
7885 ctor-initializer:
7886 : mem-initializer-list
7887
7888 Returns TRUE iff the ctor-initializer was actually present. */
7889
7890 static bool
7891 cp_parser_ctor_initializer_opt (cp_parser* parser)
7892 {
7893 /* If the next token is not a `:', then there is no
7894 ctor-initializer. */
7895 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7896 {
7897 /* Do default initialization of any bases and members. */
7898 if (DECL_CONSTRUCTOR_P (current_function_decl))
7899 finish_mem_initializers (NULL_TREE);
7900
7901 return false;
7902 }
7903
7904 /* Consume the `:' token. */
7905 cp_lexer_consume_token (parser->lexer);
7906 /* And the mem-initializer-list. */
7907 cp_parser_mem_initializer_list (parser);
7908
7909 return true;
7910 }
7911
7912 /* Parse a mem-initializer-list.
7913
7914 mem-initializer-list:
7915 mem-initializer
7916 mem-initializer , mem-initializer-list */
7917
7918 static void
7919 cp_parser_mem_initializer_list (cp_parser* parser)
7920 {
7921 tree mem_initializer_list = NULL_TREE;
7922
7923 /* Let the semantic analysis code know that we are starting the
7924 mem-initializer-list. */
7925 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7926 error ("only constructors take base initializers");
7927
7928 /* Loop through the list. */
7929 while (true)
7930 {
7931 tree mem_initializer;
7932
7933 /* Parse the mem-initializer. */
7934 mem_initializer = cp_parser_mem_initializer (parser);
7935 /* Add it to the list, unless it was erroneous. */
7936 if (mem_initializer != error_mark_node)
7937 {
7938 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7939 mem_initializer_list = mem_initializer;
7940 }
7941 /* If the next token is not a `,', we're done. */
7942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7943 break;
7944 /* Consume the `,' token. */
7945 cp_lexer_consume_token (parser->lexer);
7946 }
7947
7948 /* Perform semantic analysis. */
7949 if (DECL_CONSTRUCTOR_P (current_function_decl))
7950 finish_mem_initializers (mem_initializer_list);
7951 }
7952
7953 /* Parse a mem-initializer.
7954
7955 mem-initializer:
7956 mem-initializer-id ( expression-list [opt] )
7957
7958 GNU extension:
7959
7960 mem-initializer:
7961 ( expression-list [opt] )
7962
7963 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7964 class) or FIELD_DECL (for a non-static data member) to initialize;
7965 the TREE_VALUE is the expression-list. An empty initialization
7966 list is represented by void_list_node. */
7967
7968 static tree
7969 cp_parser_mem_initializer (cp_parser* parser)
7970 {
7971 tree mem_initializer_id;
7972 tree expression_list;
7973 tree member;
7974
7975 /* Find out what is being initialized. */
7976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7977 {
7978 pedwarn ("anachronistic old-style base class initializer");
7979 mem_initializer_id = NULL_TREE;
7980 }
7981 else
7982 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7983 member = expand_member_init (mem_initializer_id);
7984 if (member && !DECL_P (member))
7985 in_base_initializer = 1;
7986
7987 expression_list
7988 = cp_parser_parenthesized_expression_list (parser, false,
7989 /*cast_p=*/false,
7990 /*non_constant_p=*/NULL);
7991 if (expression_list == error_mark_node)
7992 return error_mark_node;
7993 if (!expression_list)
7994 expression_list = void_type_node;
7995
7996 in_base_initializer = 0;
7997
7998 return member ? build_tree_list (member, expression_list) : error_mark_node;
7999 }
8000
8001 /* Parse a mem-initializer-id.
8002
8003 mem-initializer-id:
8004 :: [opt] nested-name-specifier [opt] class-name
8005 identifier
8006
8007 Returns a TYPE indicating the class to be initializer for the first
8008 production. Returns an IDENTIFIER_NODE indicating the data member
8009 to be initialized for the second production. */
8010
8011 static tree
8012 cp_parser_mem_initializer_id (cp_parser* parser)
8013 {
8014 bool global_scope_p;
8015 bool nested_name_specifier_p;
8016 bool template_p = false;
8017 tree id;
8018
8019 /* `typename' is not allowed in this context ([temp.res]). */
8020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8021 {
8022 error ("keyword %<typename%> not allowed in this context (a qualified "
8023 "member initializer is implicitly a type)");
8024 cp_lexer_consume_token (parser->lexer);
8025 }
8026 /* Look for the optional `::' operator. */
8027 global_scope_p
8028 = (cp_parser_global_scope_opt (parser,
8029 /*current_scope_valid_p=*/false)
8030 != NULL_TREE);
8031 /* Look for the optional nested-name-specifier. The simplest way to
8032 implement:
8033
8034 [temp.res]
8035
8036 The keyword `typename' is not permitted in a base-specifier or
8037 mem-initializer; in these contexts a qualified name that
8038 depends on a template-parameter is implicitly assumed to be a
8039 type name.
8040
8041 is to assume that we have seen the `typename' keyword at this
8042 point. */
8043 nested_name_specifier_p
8044 = (cp_parser_nested_name_specifier_opt (parser,
8045 /*typename_keyword_p=*/true,
8046 /*check_dependency_p=*/true,
8047 /*type_p=*/true,
8048 /*is_declaration=*/true)
8049 != NULL_TREE);
8050 if (nested_name_specifier_p)
8051 template_p = cp_parser_optional_template_keyword (parser);
8052 /* If there is a `::' operator or a nested-name-specifier, then we
8053 are definitely looking for a class-name. */
8054 if (global_scope_p || nested_name_specifier_p)
8055 return cp_parser_class_name (parser,
8056 /*typename_keyword_p=*/true,
8057 /*template_keyword_p=*/template_p,
8058 none_type,
8059 /*check_dependency_p=*/true,
8060 /*class_head_p=*/false,
8061 /*is_declaration=*/true);
8062 /* Otherwise, we could also be looking for an ordinary identifier. */
8063 cp_parser_parse_tentatively (parser);
8064 /* Try a class-name. */
8065 id = cp_parser_class_name (parser,
8066 /*typename_keyword_p=*/true,
8067 /*template_keyword_p=*/false,
8068 none_type,
8069 /*check_dependency_p=*/true,
8070 /*class_head_p=*/false,
8071 /*is_declaration=*/true);
8072 /* If we found one, we're done. */
8073 if (cp_parser_parse_definitely (parser))
8074 return id;
8075 /* Otherwise, look for an ordinary identifier. */
8076 return cp_parser_identifier (parser);
8077 }
8078
8079 /* Overloading [gram.over] */
8080
8081 /* Parse an operator-function-id.
8082
8083 operator-function-id:
8084 operator operator
8085
8086 Returns an IDENTIFIER_NODE for the operator which is a
8087 human-readable spelling of the identifier, e.g., `operator +'. */
8088
8089 static tree
8090 cp_parser_operator_function_id (cp_parser* parser)
8091 {
8092 /* Look for the `operator' keyword. */
8093 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8094 return error_mark_node;
8095 /* And then the name of the operator itself. */
8096 return cp_parser_operator (parser);
8097 }
8098
8099 /* Parse an operator.
8100
8101 operator:
8102 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8103 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8104 || ++ -- , ->* -> () []
8105
8106 GNU Extensions:
8107
8108 operator:
8109 <? >? <?= >?=
8110
8111 Returns an IDENTIFIER_NODE for the operator which is a
8112 human-readable spelling of the identifier, e.g., `operator +'. */
8113
8114 static tree
8115 cp_parser_operator (cp_parser* parser)
8116 {
8117 tree id = NULL_TREE;
8118 cp_token *token;
8119
8120 /* Peek at the next token. */
8121 token = cp_lexer_peek_token (parser->lexer);
8122 /* Figure out which operator we have. */
8123 switch (token->type)
8124 {
8125 case CPP_KEYWORD:
8126 {
8127 enum tree_code op;
8128
8129 /* The keyword should be either `new' or `delete'. */
8130 if (token->keyword == RID_NEW)
8131 op = NEW_EXPR;
8132 else if (token->keyword == RID_DELETE)
8133 op = DELETE_EXPR;
8134 else
8135 break;
8136
8137 /* Consume the `new' or `delete' token. */
8138 cp_lexer_consume_token (parser->lexer);
8139
8140 /* Peek at the next token. */
8141 token = cp_lexer_peek_token (parser->lexer);
8142 /* If it's a `[' token then this is the array variant of the
8143 operator. */
8144 if (token->type == CPP_OPEN_SQUARE)
8145 {
8146 /* Consume the `[' token. */
8147 cp_lexer_consume_token (parser->lexer);
8148 /* Look for the `]' token. */
8149 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8150 id = ansi_opname (op == NEW_EXPR
8151 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8152 }
8153 /* Otherwise, we have the non-array variant. */
8154 else
8155 id = ansi_opname (op);
8156
8157 return id;
8158 }
8159
8160 case CPP_PLUS:
8161 id = ansi_opname (PLUS_EXPR);
8162 break;
8163
8164 case CPP_MINUS:
8165 id = ansi_opname (MINUS_EXPR);
8166 break;
8167
8168 case CPP_MULT:
8169 id = ansi_opname (MULT_EXPR);
8170 break;
8171
8172 case CPP_DIV:
8173 id = ansi_opname (TRUNC_DIV_EXPR);
8174 break;
8175
8176 case CPP_MOD:
8177 id = ansi_opname (TRUNC_MOD_EXPR);
8178 break;
8179
8180 case CPP_XOR:
8181 id = ansi_opname (BIT_XOR_EXPR);
8182 break;
8183
8184 case CPP_AND:
8185 id = ansi_opname (BIT_AND_EXPR);
8186 break;
8187
8188 case CPP_OR:
8189 id = ansi_opname (BIT_IOR_EXPR);
8190 break;
8191
8192 case CPP_COMPL:
8193 id = ansi_opname (BIT_NOT_EXPR);
8194 break;
8195
8196 case CPP_NOT:
8197 id = ansi_opname (TRUTH_NOT_EXPR);
8198 break;
8199
8200 case CPP_EQ:
8201 id = ansi_assopname (NOP_EXPR);
8202 break;
8203
8204 case CPP_LESS:
8205 id = ansi_opname (LT_EXPR);
8206 break;
8207
8208 case CPP_GREATER:
8209 id = ansi_opname (GT_EXPR);
8210 break;
8211
8212 case CPP_PLUS_EQ:
8213 id = ansi_assopname (PLUS_EXPR);
8214 break;
8215
8216 case CPP_MINUS_EQ:
8217 id = ansi_assopname (MINUS_EXPR);
8218 break;
8219
8220 case CPP_MULT_EQ:
8221 id = ansi_assopname (MULT_EXPR);
8222 break;
8223
8224 case CPP_DIV_EQ:
8225 id = ansi_assopname (TRUNC_DIV_EXPR);
8226 break;
8227
8228 case CPP_MOD_EQ:
8229 id = ansi_assopname (TRUNC_MOD_EXPR);
8230 break;
8231
8232 case CPP_XOR_EQ:
8233 id = ansi_assopname (BIT_XOR_EXPR);
8234 break;
8235
8236 case CPP_AND_EQ:
8237 id = ansi_assopname (BIT_AND_EXPR);
8238 break;
8239
8240 case CPP_OR_EQ:
8241 id = ansi_assopname (BIT_IOR_EXPR);
8242 break;
8243
8244 case CPP_LSHIFT:
8245 id = ansi_opname (LSHIFT_EXPR);
8246 break;
8247
8248 case CPP_RSHIFT:
8249 id = ansi_opname (RSHIFT_EXPR);
8250 break;
8251
8252 case CPP_LSHIFT_EQ:
8253 id = ansi_assopname (LSHIFT_EXPR);
8254 break;
8255
8256 case CPP_RSHIFT_EQ:
8257 id = ansi_assopname (RSHIFT_EXPR);
8258 break;
8259
8260 case CPP_EQ_EQ:
8261 id = ansi_opname (EQ_EXPR);
8262 break;
8263
8264 case CPP_NOT_EQ:
8265 id = ansi_opname (NE_EXPR);
8266 break;
8267
8268 case CPP_LESS_EQ:
8269 id = ansi_opname (LE_EXPR);
8270 break;
8271
8272 case CPP_GREATER_EQ:
8273 id = ansi_opname (GE_EXPR);
8274 break;
8275
8276 case CPP_AND_AND:
8277 id = ansi_opname (TRUTH_ANDIF_EXPR);
8278 break;
8279
8280 case CPP_OR_OR:
8281 id = ansi_opname (TRUTH_ORIF_EXPR);
8282 break;
8283
8284 case CPP_PLUS_PLUS:
8285 id = ansi_opname (POSTINCREMENT_EXPR);
8286 break;
8287
8288 case CPP_MINUS_MINUS:
8289 id = ansi_opname (PREDECREMENT_EXPR);
8290 break;
8291
8292 case CPP_COMMA:
8293 id = ansi_opname (COMPOUND_EXPR);
8294 break;
8295
8296 case CPP_DEREF_STAR:
8297 id = ansi_opname (MEMBER_REF);
8298 break;
8299
8300 case CPP_DEREF:
8301 id = ansi_opname (COMPONENT_REF);
8302 break;
8303
8304 case CPP_OPEN_PAREN:
8305 /* Consume the `('. */
8306 cp_lexer_consume_token (parser->lexer);
8307 /* Look for the matching `)'. */
8308 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8309 return ansi_opname (CALL_EXPR);
8310
8311 case CPP_OPEN_SQUARE:
8312 /* Consume the `['. */
8313 cp_lexer_consume_token (parser->lexer);
8314 /* Look for the matching `]'. */
8315 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8316 return ansi_opname (ARRAY_REF);
8317
8318 /* Extensions. */
8319 case CPP_MIN:
8320 id = ansi_opname (MIN_EXPR);
8321 cp_parser_warn_min_max ();
8322 break;
8323
8324 case CPP_MAX:
8325 id = ansi_opname (MAX_EXPR);
8326 cp_parser_warn_min_max ();
8327 break;
8328
8329 case CPP_MIN_EQ:
8330 id = ansi_assopname (MIN_EXPR);
8331 cp_parser_warn_min_max ();
8332 break;
8333
8334 case CPP_MAX_EQ:
8335 id = ansi_assopname (MAX_EXPR);
8336 cp_parser_warn_min_max ();
8337 break;
8338
8339 default:
8340 /* Anything else is an error. */
8341 break;
8342 }
8343
8344 /* If we have selected an identifier, we need to consume the
8345 operator token. */
8346 if (id)
8347 cp_lexer_consume_token (parser->lexer);
8348 /* Otherwise, no valid operator name was present. */
8349 else
8350 {
8351 cp_parser_error (parser, "expected operator");
8352 id = error_mark_node;
8353 }
8354
8355 return id;
8356 }
8357
8358 /* Parse a template-declaration.
8359
8360 template-declaration:
8361 export [opt] template < template-parameter-list > declaration
8362
8363 If MEMBER_P is TRUE, this template-declaration occurs within a
8364 class-specifier.
8365
8366 The grammar rule given by the standard isn't correct. What
8367 is really meant is:
8368
8369 template-declaration:
8370 export [opt] template-parameter-list-seq
8371 decl-specifier-seq [opt] init-declarator [opt] ;
8372 export [opt] template-parameter-list-seq
8373 function-definition
8374
8375 template-parameter-list-seq:
8376 template-parameter-list-seq [opt]
8377 template < template-parameter-list > */
8378
8379 static void
8380 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8381 {
8382 /* Check for `export'. */
8383 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8384 {
8385 /* Consume the `export' token. */
8386 cp_lexer_consume_token (parser->lexer);
8387 /* Warn that we do not support `export'. */
8388 warning (0, "keyword %<export%> not implemented, and will be ignored");
8389 }
8390
8391 cp_parser_template_declaration_after_export (parser, member_p);
8392 }
8393
8394 /* Parse a template-parameter-list.
8395
8396 template-parameter-list:
8397 template-parameter
8398 template-parameter-list , template-parameter
8399
8400 Returns a TREE_LIST. Each node represents a template parameter.
8401 The nodes are connected via their TREE_CHAINs. */
8402
8403 static tree
8404 cp_parser_template_parameter_list (cp_parser* parser)
8405 {
8406 tree parameter_list = NULL_TREE;
8407
8408 begin_template_parm_list ();
8409 while (true)
8410 {
8411 tree parameter;
8412 cp_token *token;
8413 bool is_non_type;
8414
8415 /* Parse the template-parameter. */
8416 parameter = cp_parser_template_parameter (parser, &is_non_type);
8417 /* Add it to the list. */
8418 if (parameter != error_mark_node)
8419 parameter_list = process_template_parm (parameter_list,
8420 parameter,
8421 is_non_type);
8422 /* Peek at the next token. */
8423 token = cp_lexer_peek_token (parser->lexer);
8424 /* If it's not a `,', we're done. */
8425 if (token->type != CPP_COMMA)
8426 break;
8427 /* Otherwise, consume the `,' token. */
8428 cp_lexer_consume_token (parser->lexer);
8429 }
8430
8431 return end_template_parm_list (parameter_list);
8432 }
8433
8434 /* Parse a template-parameter.
8435
8436 template-parameter:
8437 type-parameter
8438 parameter-declaration
8439
8440 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8441 the parameter. The TREE_PURPOSE is the default value, if any.
8442 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8443 iff this parameter is a non-type parameter. */
8444
8445 static tree
8446 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8447 {
8448 cp_token *token;
8449 cp_parameter_declarator *parameter_declarator;
8450 tree parm;
8451
8452 /* Assume it is a type parameter or a template parameter. */
8453 *is_non_type = false;
8454 /* Peek at the next token. */
8455 token = cp_lexer_peek_token (parser->lexer);
8456 /* If it is `class' or `template', we have a type-parameter. */
8457 if (token->keyword == RID_TEMPLATE)
8458 return cp_parser_type_parameter (parser);
8459 /* If it is `class' or `typename' we do not know yet whether it is a
8460 type parameter or a non-type parameter. Consider:
8461
8462 template <typename T, typename T::X X> ...
8463
8464 or:
8465
8466 template <class C, class D*> ...
8467
8468 Here, the first parameter is a type parameter, and the second is
8469 a non-type parameter. We can tell by looking at the token after
8470 the identifier -- if it is a `,', `=', or `>' then we have a type
8471 parameter. */
8472 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8473 {
8474 /* Peek at the token after `class' or `typename'. */
8475 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8476 /* If it's an identifier, skip it. */
8477 if (token->type == CPP_NAME)
8478 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8479 /* Now, see if the token looks like the end of a template
8480 parameter. */
8481 if (token->type == CPP_COMMA
8482 || token->type == CPP_EQ
8483 || token->type == CPP_GREATER)
8484 return cp_parser_type_parameter (parser);
8485 }
8486
8487 /* Otherwise, it is a non-type parameter.
8488
8489 [temp.param]
8490
8491 When parsing a default template-argument for a non-type
8492 template-parameter, the first non-nested `>' is taken as the end
8493 of the template parameter-list rather than a greater-than
8494 operator. */
8495 *is_non_type = true;
8496 parameter_declarator
8497 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8498 /*parenthesized_p=*/NULL);
8499 parm = grokdeclarator (parameter_declarator->declarator,
8500 &parameter_declarator->decl_specifiers,
8501 PARM, /*initialized=*/0,
8502 /*attrlist=*/NULL);
8503 if (parm == error_mark_node)
8504 return error_mark_node;
8505 return build_tree_list (parameter_declarator->default_argument, parm);
8506 }
8507
8508 /* Parse a type-parameter.
8509
8510 type-parameter:
8511 class identifier [opt]
8512 class identifier [opt] = type-id
8513 typename identifier [opt]
8514 typename identifier [opt] = type-id
8515 template < template-parameter-list > class identifier [opt]
8516 template < template-parameter-list > class identifier [opt]
8517 = id-expression
8518
8519 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8520 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8521 the declaration of the parameter. */
8522
8523 static tree
8524 cp_parser_type_parameter (cp_parser* parser)
8525 {
8526 cp_token *token;
8527 tree parameter;
8528
8529 /* Look for a keyword to tell us what kind of parameter this is. */
8530 token = cp_parser_require (parser, CPP_KEYWORD,
8531 "`class', `typename', or `template'");
8532 if (!token)
8533 return error_mark_node;
8534
8535 switch (token->keyword)
8536 {
8537 case RID_CLASS:
8538 case RID_TYPENAME:
8539 {
8540 tree identifier;
8541 tree default_argument;
8542
8543 /* If the next token is an identifier, then it names the
8544 parameter. */
8545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8546 identifier = cp_parser_identifier (parser);
8547 else
8548 identifier = NULL_TREE;
8549
8550 /* Create the parameter. */
8551 parameter = finish_template_type_parm (class_type_node, identifier);
8552
8553 /* If the next token is an `=', we have a default argument. */
8554 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8555 {
8556 /* Consume the `=' token. */
8557 cp_lexer_consume_token (parser->lexer);
8558 /* Parse the default-argument. */
8559 push_deferring_access_checks (dk_no_deferred);
8560 default_argument = cp_parser_type_id (parser);
8561 pop_deferring_access_checks ();
8562 }
8563 else
8564 default_argument = NULL_TREE;
8565
8566 /* Create the combined representation of the parameter and the
8567 default argument. */
8568 parameter = build_tree_list (default_argument, parameter);
8569 }
8570 break;
8571
8572 case RID_TEMPLATE:
8573 {
8574 tree parameter_list;
8575 tree identifier;
8576 tree default_argument;
8577
8578 /* Look for the `<'. */
8579 cp_parser_require (parser, CPP_LESS, "`<'");
8580 /* Parse the template-parameter-list. */
8581 parameter_list = cp_parser_template_parameter_list (parser);
8582 /* Look for the `>'. */
8583 cp_parser_require (parser, CPP_GREATER, "`>'");
8584 /* Look for the `class' keyword. */
8585 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8586 /* If the next token is an `=', then there is a
8587 default-argument. If the next token is a `>', we are at
8588 the end of the parameter-list. If the next token is a `,',
8589 then we are at the end of this parameter. */
8590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8591 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8592 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8593 {
8594 identifier = cp_parser_identifier (parser);
8595 /* Treat invalid names as if the parameter were nameless. */
8596 if (identifier == error_mark_node)
8597 identifier = NULL_TREE;
8598 }
8599 else
8600 identifier = NULL_TREE;
8601
8602 /* Create the template parameter. */
8603 parameter = finish_template_template_parm (class_type_node,
8604 identifier);
8605
8606 /* If the next token is an `=', then there is a
8607 default-argument. */
8608 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8609 {
8610 bool is_template;
8611
8612 /* Consume the `='. */
8613 cp_lexer_consume_token (parser->lexer);
8614 /* Parse the id-expression. */
8615 push_deferring_access_checks (dk_no_deferred);
8616 default_argument
8617 = cp_parser_id_expression (parser,
8618 /*template_keyword_p=*/false,
8619 /*check_dependency_p=*/true,
8620 /*template_p=*/&is_template,
8621 /*declarator_p=*/false,
8622 /*optional_p=*/false);
8623 if (TREE_CODE (default_argument) == TYPE_DECL)
8624 /* If the id-expression was a template-id that refers to
8625 a template-class, we already have the declaration here,
8626 so no further lookup is needed. */
8627 ;
8628 else
8629 /* Look up the name. */
8630 default_argument
8631 = cp_parser_lookup_name (parser, default_argument,
8632 none_type,
8633 /*is_template=*/is_template,
8634 /*is_namespace=*/false,
8635 /*check_dependency=*/true,
8636 /*ambiguous_decls=*/NULL);
8637 /* See if the default argument is valid. */
8638 default_argument
8639 = check_template_template_default_arg (default_argument);
8640 pop_deferring_access_checks ();
8641 }
8642 else
8643 default_argument = NULL_TREE;
8644
8645 /* Create the combined representation of the parameter and the
8646 default argument. */
8647 parameter = build_tree_list (default_argument, parameter);
8648 }
8649 break;
8650
8651 default:
8652 gcc_unreachable ();
8653 break;
8654 }
8655
8656 return parameter;
8657 }
8658
8659 /* Parse a template-id.
8660
8661 template-id:
8662 template-name < template-argument-list [opt] >
8663
8664 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8665 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8666 returned. Otherwise, if the template-name names a function, or set
8667 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8668 names a class, returns a TYPE_DECL for the specialization.
8669
8670 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8671 uninstantiated templates. */
8672
8673 static tree
8674 cp_parser_template_id (cp_parser *parser,
8675 bool template_keyword_p,
8676 bool check_dependency_p,
8677 bool is_declaration)
8678 {
8679 tree template;
8680 tree arguments;
8681 tree template_id;
8682 cp_token_position start_of_id = 0;
8683 tree access_check = NULL_TREE;
8684 cp_token *next_token, *next_token_2;
8685 bool is_identifier;
8686
8687 /* If the next token corresponds to a template-id, there is no need
8688 to reparse it. */
8689 next_token = cp_lexer_peek_token (parser->lexer);
8690 if (next_token->type == CPP_TEMPLATE_ID)
8691 {
8692 tree value;
8693 tree check;
8694
8695 /* Get the stored value. */
8696 value = cp_lexer_consume_token (parser->lexer)->value;
8697 /* Perform any access checks that were deferred. */
8698 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8699 perform_or_defer_access_check (TREE_PURPOSE (check),
8700 TREE_VALUE (check));
8701 /* Return the stored value. */
8702 return TREE_VALUE (value);
8703 }
8704
8705 /* Avoid performing name lookup if there is no possibility of
8706 finding a template-id. */
8707 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8708 || (next_token->type == CPP_NAME
8709 && !cp_parser_nth_token_starts_template_argument_list_p
8710 (parser, 2)))
8711 {
8712 cp_parser_error (parser, "expected template-id");
8713 return error_mark_node;
8714 }
8715
8716 /* Remember where the template-id starts. */
8717 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8718 start_of_id = cp_lexer_token_position (parser->lexer, false);
8719
8720 push_deferring_access_checks (dk_deferred);
8721
8722 /* Parse the template-name. */
8723 is_identifier = false;
8724 template = cp_parser_template_name (parser, template_keyword_p,
8725 check_dependency_p,
8726 is_declaration,
8727 &is_identifier);
8728 if (template == error_mark_node || is_identifier)
8729 {
8730 pop_deferring_access_checks ();
8731 return template;
8732 }
8733
8734 /* If we find the sequence `[:' after a template-name, it's probably
8735 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8736 parse correctly the argument list. */
8737 next_token = cp_lexer_peek_token (parser->lexer);
8738 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8739 if (next_token->type == CPP_OPEN_SQUARE
8740 && next_token->flags & DIGRAPH
8741 && next_token_2->type == CPP_COLON
8742 && !(next_token_2->flags & PREV_WHITE))
8743 {
8744 cp_parser_parse_tentatively (parser);
8745 /* Change `:' into `::'. */
8746 next_token_2->type = CPP_SCOPE;
8747 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8748 CPP_LESS. */
8749 cp_lexer_consume_token (parser->lexer);
8750 /* Parse the arguments. */
8751 arguments = cp_parser_enclosed_template_argument_list (parser);
8752 if (!cp_parser_parse_definitely (parser))
8753 {
8754 /* If we couldn't parse an argument list, then we revert our changes
8755 and return simply an error. Maybe this is not a template-id
8756 after all. */
8757 next_token_2->type = CPP_COLON;
8758 cp_parser_error (parser, "expected %<<%>");
8759 pop_deferring_access_checks ();
8760 return error_mark_node;
8761 }
8762 /* Otherwise, emit an error about the invalid digraph, but continue
8763 parsing because we got our argument list. */
8764 pedwarn ("%<<::%> cannot begin a template-argument list");
8765 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8766 "between %<<%> and %<::%>");
8767 if (!flag_permissive)
8768 {
8769 static bool hint;
8770 if (!hint)
8771 {
8772 inform ("(if you use -fpermissive G++ will accept your code)");
8773 hint = true;
8774 }
8775 }
8776 }
8777 else
8778 {
8779 /* Look for the `<' that starts the template-argument-list. */
8780 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8781 {
8782 pop_deferring_access_checks ();
8783 return error_mark_node;
8784 }
8785 /* Parse the arguments. */
8786 arguments = cp_parser_enclosed_template_argument_list (parser);
8787 }
8788
8789 /* Build a representation of the specialization. */
8790 if (TREE_CODE (template) == IDENTIFIER_NODE)
8791 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8792 else if (DECL_CLASS_TEMPLATE_P (template)
8793 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8794 {
8795 bool entering_scope;
8796 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8797 template (rather than some instantiation thereof) only if
8798 is not nested within some other construct. For example, in
8799 "template <typename T> void f(T) { A<T>::", A<T> is just an
8800 instantiation of A. */
8801 entering_scope = (template_parm_scope_p ()
8802 && cp_lexer_next_token_is (parser->lexer,
8803 CPP_SCOPE));
8804 template_id
8805 = finish_template_type (template, arguments, entering_scope);
8806 }
8807 else
8808 {
8809 /* If it's not a class-template or a template-template, it should be
8810 a function-template. */
8811 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8812 || TREE_CODE (template) == OVERLOAD
8813 || BASELINK_P (template)));
8814
8815 template_id = lookup_template_function (template, arguments);
8816 }
8817
8818 /* Retrieve any deferred checks. Do not pop this access checks yet
8819 so the memory will not be reclaimed during token replacing below. */
8820 access_check = get_deferred_access_checks ();
8821
8822 /* If parsing tentatively, replace the sequence of tokens that makes
8823 up the template-id with a CPP_TEMPLATE_ID token. That way,
8824 should we re-parse the token stream, we will not have to repeat
8825 the effort required to do the parse, nor will we issue duplicate
8826 error messages about problems during instantiation of the
8827 template. */
8828 if (start_of_id)
8829 {
8830 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8831
8832 /* Reset the contents of the START_OF_ID token. */
8833 token->type = CPP_TEMPLATE_ID;
8834 token->value = build_tree_list (access_check, template_id);
8835 token->keyword = RID_MAX;
8836
8837 /* Purge all subsequent tokens. */
8838 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8839
8840 /* ??? Can we actually assume that, if template_id ==
8841 error_mark_node, we will have issued a diagnostic to the
8842 user, as opposed to simply marking the tentative parse as
8843 failed? */
8844 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8845 error ("parse error in template argument list");
8846 }
8847
8848 pop_deferring_access_checks ();
8849 return template_id;
8850 }
8851
8852 /* Parse a template-name.
8853
8854 template-name:
8855 identifier
8856
8857 The standard should actually say:
8858
8859 template-name:
8860 identifier
8861 operator-function-id
8862
8863 A defect report has been filed about this issue.
8864
8865 A conversion-function-id cannot be a template name because they cannot
8866 be part of a template-id. In fact, looking at this code:
8867
8868 a.operator K<int>()
8869
8870 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8871 It is impossible to call a templated conversion-function-id with an
8872 explicit argument list, since the only allowed template parameter is
8873 the type to which it is converting.
8874
8875 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8876 `template' keyword, in a construction like:
8877
8878 T::template f<3>()
8879
8880 In that case `f' is taken to be a template-name, even though there
8881 is no way of knowing for sure.
8882
8883 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8884 name refers to a set of overloaded functions, at least one of which
8885 is a template, or an IDENTIFIER_NODE with the name of the template,
8886 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8887 names are looked up inside uninstantiated templates. */
8888
8889 static tree
8890 cp_parser_template_name (cp_parser* parser,
8891 bool template_keyword_p,
8892 bool check_dependency_p,
8893 bool is_declaration,
8894 bool *is_identifier)
8895 {
8896 tree identifier;
8897 tree decl;
8898 tree fns;
8899
8900 /* If the next token is `operator', then we have either an
8901 operator-function-id or a conversion-function-id. */
8902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8903 {
8904 /* We don't know whether we're looking at an
8905 operator-function-id or a conversion-function-id. */
8906 cp_parser_parse_tentatively (parser);
8907 /* Try an operator-function-id. */
8908 identifier = cp_parser_operator_function_id (parser);
8909 /* If that didn't work, try a conversion-function-id. */
8910 if (!cp_parser_parse_definitely (parser))
8911 {
8912 cp_parser_error (parser, "expected template-name");
8913 return error_mark_node;
8914 }
8915 }
8916 /* Look for the identifier. */
8917 else
8918 identifier = cp_parser_identifier (parser);
8919
8920 /* If we didn't find an identifier, we don't have a template-id. */
8921 if (identifier == error_mark_node)
8922 return error_mark_node;
8923
8924 /* If the name immediately followed the `template' keyword, then it
8925 is a template-name. However, if the next token is not `<', then
8926 we do not treat it as a template-name, since it is not being used
8927 as part of a template-id. This enables us to handle constructs
8928 like:
8929
8930 template <typename T> struct S { S(); };
8931 template <typename T> S<T>::S();
8932
8933 correctly. We would treat `S' as a template -- if it were `S<T>'
8934 -- but we do not if there is no `<'. */
8935
8936 if (processing_template_decl
8937 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8938 {
8939 /* In a declaration, in a dependent context, we pretend that the
8940 "template" keyword was present in order to improve error
8941 recovery. For example, given:
8942
8943 template <typename T> void f(T::X<int>);
8944
8945 we want to treat "X<int>" as a template-id. */
8946 if (is_declaration
8947 && !template_keyword_p
8948 && parser->scope && TYPE_P (parser->scope)
8949 && check_dependency_p
8950 && dependent_type_p (parser->scope)
8951 /* Do not do this for dtors (or ctors), since they never
8952 need the template keyword before their name. */
8953 && !constructor_name_p (identifier, parser->scope))
8954 {
8955 cp_token_position start = 0;
8956
8957 /* Explain what went wrong. */
8958 error ("non-template %qD used as template", identifier);
8959 inform ("use %<%T::template %D%> to indicate that it is a template",
8960 parser->scope, identifier);
8961 /* If parsing tentatively, find the location of the "<" token. */
8962 if (cp_parser_simulate_error (parser))
8963 start = cp_lexer_token_position (parser->lexer, true);
8964 /* Parse the template arguments so that we can issue error
8965 messages about them. */
8966 cp_lexer_consume_token (parser->lexer);
8967 cp_parser_enclosed_template_argument_list (parser);
8968 /* Skip tokens until we find a good place from which to
8969 continue parsing. */
8970 cp_parser_skip_to_closing_parenthesis (parser,
8971 /*recovering=*/true,
8972 /*or_comma=*/true,
8973 /*consume_paren=*/false);
8974 /* If parsing tentatively, permanently remove the
8975 template argument list. That will prevent duplicate
8976 error messages from being issued about the missing
8977 "template" keyword. */
8978 if (start)
8979 cp_lexer_purge_tokens_after (parser->lexer, start);
8980 if (is_identifier)
8981 *is_identifier = true;
8982 return identifier;
8983 }
8984
8985 /* If the "template" keyword is present, then there is generally
8986 no point in doing name-lookup, so we just return IDENTIFIER.
8987 But, if the qualifying scope is non-dependent then we can
8988 (and must) do name-lookup normally. */
8989 if (template_keyword_p
8990 && (!parser->scope
8991 || (TYPE_P (parser->scope)
8992 && dependent_type_p (parser->scope))))
8993 return identifier;
8994 }
8995
8996 /* Look up the name. */
8997 decl = cp_parser_lookup_name (parser, identifier,
8998 none_type,
8999 /*is_template=*/false,
9000 /*is_namespace=*/false,
9001 check_dependency_p,
9002 /*ambiguous_decls=*/NULL);
9003 decl = maybe_get_template_decl_from_type_decl (decl);
9004
9005 /* If DECL is a template, then the name was a template-name. */
9006 if (TREE_CODE (decl) == TEMPLATE_DECL)
9007 ;
9008 else
9009 {
9010 tree fn = NULL_TREE;
9011
9012 /* The standard does not explicitly indicate whether a name that
9013 names a set of overloaded declarations, some of which are
9014 templates, is a template-name. However, such a name should
9015 be a template-name; otherwise, there is no way to form a
9016 template-id for the overloaded templates. */
9017 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9018 if (TREE_CODE (fns) == OVERLOAD)
9019 for (fn = fns; fn; fn = OVL_NEXT (fn))
9020 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9021 break;
9022
9023 if (!fn)
9024 {
9025 /* The name does not name a template. */
9026 cp_parser_error (parser, "expected template-name");
9027 return error_mark_node;
9028 }
9029 }
9030
9031 /* If DECL is dependent, and refers to a function, then just return
9032 its name; we will look it up again during template instantiation. */
9033 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9034 {
9035 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9036 if (TYPE_P (scope) && dependent_type_p (scope))
9037 return identifier;
9038 }
9039
9040 return decl;
9041 }
9042
9043 /* Parse a template-argument-list.
9044
9045 template-argument-list:
9046 template-argument
9047 template-argument-list , template-argument
9048
9049 Returns a TREE_VEC containing the arguments. */
9050
9051 static tree
9052 cp_parser_template_argument_list (cp_parser* parser)
9053 {
9054 tree fixed_args[10];
9055 unsigned n_args = 0;
9056 unsigned alloced = 10;
9057 tree *arg_ary = fixed_args;
9058 tree vec;
9059 bool saved_in_template_argument_list_p;
9060 bool saved_ice_p;
9061 bool saved_non_ice_p;
9062
9063 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9064 parser->in_template_argument_list_p = true;
9065 /* Even if the template-id appears in an integral
9066 constant-expression, the contents of the argument list do
9067 not. */
9068 saved_ice_p = parser->integral_constant_expression_p;
9069 parser->integral_constant_expression_p = false;
9070 saved_non_ice_p = parser->non_integral_constant_expression_p;
9071 parser->non_integral_constant_expression_p = false;
9072 /* Parse the arguments. */
9073 do
9074 {
9075 tree argument;
9076
9077 if (n_args)
9078 /* Consume the comma. */
9079 cp_lexer_consume_token (parser->lexer);
9080
9081 /* Parse the template-argument. */
9082 argument = cp_parser_template_argument (parser);
9083 if (n_args == alloced)
9084 {
9085 alloced *= 2;
9086
9087 if (arg_ary == fixed_args)
9088 {
9089 arg_ary = XNEWVEC (tree, alloced);
9090 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9091 }
9092 else
9093 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9094 }
9095 arg_ary[n_args++] = argument;
9096 }
9097 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9098
9099 vec = make_tree_vec (n_args);
9100
9101 while (n_args--)
9102 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9103
9104 if (arg_ary != fixed_args)
9105 free (arg_ary);
9106 parser->non_integral_constant_expression_p = saved_non_ice_p;
9107 parser->integral_constant_expression_p = saved_ice_p;
9108 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9109 return vec;
9110 }
9111
9112 /* Parse a template-argument.
9113
9114 template-argument:
9115 assignment-expression
9116 type-id
9117 id-expression
9118
9119 The representation is that of an assignment-expression, type-id, or
9120 id-expression -- except that the qualified id-expression is
9121 evaluated, so that the value returned is either a DECL or an
9122 OVERLOAD.
9123
9124 Although the standard says "assignment-expression", it forbids
9125 throw-expressions or assignments in the template argument.
9126 Therefore, we use "conditional-expression" instead. */
9127
9128 static tree
9129 cp_parser_template_argument (cp_parser* parser)
9130 {
9131 tree argument;
9132 bool template_p;
9133 bool address_p;
9134 bool maybe_type_id = false;
9135 cp_token *token;
9136 cp_id_kind idk;
9137
9138 /* There's really no way to know what we're looking at, so we just
9139 try each alternative in order.
9140
9141 [temp.arg]
9142
9143 In a template-argument, an ambiguity between a type-id and an
9144 expression is resolved to a type-id, regardless of the form of
9145 the corresponding template-parameter.
9146
9147 Therefore, we try a type-id first. */
9148 cp_parser_parse_tentatively (parser);
9149 argument = cp_parser_type_id (parser);
9150 /* If there was no error parsing the type-id but the next token is a '>>',
9151 we probably found a typo for '> >'. But there are type-id which are
9152 also valid expressions. For instance:
9153
9154 struct X { int operator >> (int); };
9155 template <int V> struct Foo {};
9156 Foo<X () >> 5> r;
9157
9158 Here 'X()' is a valid type-id of a function type, but the user just
9159 wanted to write the expression "X() >> 5". Thus, we remember that we
9160 found a valid type-id, but we still try to parse the argument as an
9161 expression to see what happens. */
9162 if (!cp_parser_error_occurred (parser)
9163 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9164 {
9165 maybe_type_id = true;
9166 cp_parser_abort_tentative_parse (parser);
9167 }
9168 else
9169 {
9170 /* If the next token isn't a `,' or a `>', then this argument wasn't
9171 really finished. This means that the argument is not a valid
9172 type-id. */
9173 if (!cp_parser_next_token_ends_template_argument_p (parser))
9174 cp_parser_error (parser, "expected template-argument");
9175 /* If that worked, we're done. */
9176 if (cp_parser_parse_definitely (parser))
9177 return argument;
9178 }
9179 /* We're still not sure what the argument will be. */
9180 cp_parser_parse_tentatively (parser);
9181 /* Try a template. */
9182 argument = cp_parser_id_expression (parser,
9183 /*template_keyword_p=*/false,
9184 /*check_dependency_p=*/true,
9185 &template_p,
9186 /*declarator_p=*/false,
9187 /*optional_p=*/false);
9188 /* If the next token isn't a `,' or a `>', then this argument wasn't
9189 really finished. */
9190 if (!cp_parser_next_token_ends_template_argument_p (parser))
9191 cp_parser_error (parser, "expected template-argument");
9192 if (!cp_parser_error_occurred (parser))
9193 {
9194 /* Figure out what is being referred to. If the id-expression
9195 was for a class template specialization, then we will have a
9196 TYPE_DECL at this point. There is no need to do name lookup
9197 at this point in that case. */
9198 if (TREE_CODE (argument) != TYPE_DECL)
9199 argument = cp_parser_lookup_name (parser, argument,
9200 none_type,
9201 /*is_template=*/template_p,
9202 /*is_namespace=*/false,
9203 /*check_dependency=*/true,
9204 /*ambiguous_decls=*/NULL);
9205 if (TREE_CODE (argument) != TEMPLATE_DECL
9206 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9207 cp_parser_error (parser, "expected template-name");
9208 }
9209 if (cp_parser_parse_definitely (parser))
9210 return argument;
9211 /* It must be a non-type argument. There permitted cases are given
9212 in [temp.arg.nontype]:
9213
9214 -- an integral constant-expression of integral or enumeration
9215 type; or
9216
9217 -- the name of a non-type template-parameter; or
9218
9219 -- the name of an object or function with external linkage...
9220
9221 -- the address of an object or function with external linkage...
9222
9223 -- a pointer to member... */
9224 /* Look for a non-type template parameter. */
9225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9226 {
9227 cp_parser_parse_tentatively (parser);
9228 argument = cp_parser_primary_expression (parser,
9229 /*adress_p=*/false,
9230 /*cast_p=*/false,
9231 /*template_arg_p=*/true,
9232 &idk);
9233 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9234 || !cp_parser_next_token_ends_template_argument_p (parser))
9235 cp_parser_simulate_error (parser);
9236 if (cp_parser_parse_definitely (parser))
9237 return argument;
9238 }
9239
9240 /* If the next token is "&", the argument must be the address of an
9241 object or function with external linkage. */
9242 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9243 if (address_p)
9244 cp_lexer_consume_token (parser->lexer);
9245 /* See if we might have an id-expression. */
9246 token = cp_lexer_peek_token (parser->lexer);
9247 if (token->type == CPP_NAME
9248 || token->keyword == RID_OPERATOR
9249 || token->type == CPP_SCOPE
9250 || token->type == CPP_TEMPLATE_ID
9251 || token->type == CPP_NESTED_NAME_SPECIFIER)
9252 {
9253 cp_parser_parse_tentatively (parser);
9254 argument = cp_parser_primary_expression (parser,
9255 address_p,
9256 /*cast_p=*/false,
9257 /*template_arg_p=*/true,
9258 &idk);
9259 if (cp_parser_error_occurred (parser)
9260 || !cp_parser_next_token_ends_template_argument_p (parser))
9261 cp_parser_abort_tentative_parse (parser);
9262 else
9263 {
9264 if (TREE_CODE (argument) == INDIRECT_REF)
9265 {
9266 gcc_assert (REFERENCE_REF_P (argument));
9267 argument = TREE_OPERAND (argument, 0);
9268 }
9269
9270 if (TREE_CODE (argument) == BASELINK)
9271 /* We don't need the information about what class was used
9272 to name the overloaded functions. */
9273 argument = BASELINK_FUNCTIONS (argument);
9274
9275 if (TREE_CODE (argument) == VAR_DECL)
9276 {
9277 /* A variable without external linkage might still be a
9278 valid constant-expression, so no error is issued here
9279 if the external-linkage check fails. */
9280 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9281 cp_parser_simulate_error (parser);
9282 }
9283 else if (is_overloaded_fn (argument))
9284 /* All overloaded functions are allowed; if the external
9285 linkage test does not pass, an error will be issued
9286 later. */
9287 ;
9288 else if (address_p
9289 && (TREE_CODE (argument) == OFFSET_REF
9290 || TREE_CODE (argument) == SCOPE_REF))
9291 /* A pointer-to-member. */
9292 ;
9293 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9294 ;
9295 else
9296 cp_parser_simulate_error (parser);
9297
9298 if (cp_parser_parse_definitely (parser))
9299 {
9300 if (address_p)
9301 argument = build_x_unary_op (ADDR_EXPR, argument);
9302 return argument;
9303 }
9304 }
9305 }
9306 /* If the argument started with "&", there are no other valid
9307 alternatives at this point. */
9308 if (address_p)
9309 {
9310 cp_parser_error (parser, "invalid non-type template argument");
9311 return error_mark_node;
9312 }
9313
9314 /* If the argument wasn't successfully parsed as a type-id followed
9315 by '>>', the argument can only be a constant expression now.
9316 Otherwise, we try parsing the constant-expression tentatively,
9317 because the argument could really be a type-id. */
9318 if (maybe_type_id)
9319 cp_parser_parse_tentatively (parser);
9320 argument = cp_parser_constant_expression (parser,
9321 /*allow_non_constant_p=*/false,
9322 /*non_constant_p=*/NULL);
9323 argument = fold_non_dependent_expr (argument);
9324 if (!maybe_type_id)
9325 return argument;
9326 if (!cp_parser_next_token_ends_template_argument_p (parser))
9327 cp_parser_error (parser, "expected template-argument");
9328 if (cp_parser_parse_definitely (parser))
9329 return argument;
9330 /* We did our best to parse the argument as a non type-id, but that
9331 was the only alternative that matched (albeit with a '>' after
9332 it). We can assume it's just a typo from the user, and a
9333 diagnostic will then be issued. */
9334 return cp_parser_type_id (parser);
9335 }
9336
9337 /* Parse an explicit-instantiation.
9338
9339 explicit-instantiation:
9340 template declaration
9341
9342 Although the standard says `declaration', what it really means is:
9343
9344 explicit-instantiation:
9345 template decl-specifier-seq [opt] declarator [opt] ;
9346
9347 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9348 supposed to be allowed. A defect report has been filed about this
9349 issue.
9350
9351 GNU Extension:
9352
9353 explicit-instantiation:
9354 storage-class-specifier template
9355 decl-specifier-seq [opt] declarator [opt] ;
9356 function-specifier template
9357 decl-specifier-seq [opt] declarator [opt] ; */
9358
9359 static void
9360 cp_parser_explicit_instantiation (cp_parser* parser)
9361 {
9362 int declares_class_or_enum;
9363 cp_decl_specifier_seq decl_specifiers;
9364 tree extension_specifier = NULL_TREE;
9365
9366 /* Look for an (optional) storage-class-specifier or
9367 function-specifier. */
9368 if (cp_parser_allow_gnu_extensions_p (parser))
9369 {
9370 extension_specifier
9371 = cp_parser_storage_class_specifier_opt (parser);
9372 if (!extension_specifier)
9373 extension_specifier
9374 = cp_parser_function_specifier_opt (parser,
9375 /*decl_specs=*/NULL);
9376 }
9377
9378 /* Look for the `template' keyword. */
9379 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9380 /* Let the front end know that we are processing an explicit
9381 instantiation. */
9382 begin_explicit_instantiation ();
9383 /* [temp.explicit] says that we are supposed to ignore access
9384 control while processing explicit instantiation directives. */
9385 push_deferring_access_checks (dk_no_check);
9386 /* Parse a decl-specifier-seq. */
9387 cp_parser_decl_specifier_seq (parser,
9388 CP_PARSER_FLAGS_OPTIONAL,
9389 &decl_specifiers,
9390 &declares_class_or_enum);
9391 /* If there was exactly one decl-specifier, and it declared a class,
9392 and there's no declarator, then we have an explicit type
9393 instantiation. */
9394 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9395 {
9396 tree type;
9397
9398 type = check_tag_decl (&decl_specifiers);
9399 /* Turn access control back on for names used during
9400 template instantiation. */
9401 pop_deferring_access_checks ();
9402 if (type)
9403 do_type_instantiation (type, extension_specifier,
9404 /*complain=*/tf_error);
9405 }
9406 else
9407 {
9408 cp_declarator *declarator;
9409 tree decl;
9410
9411 /* Parse the declarator. */
9412 declarator
9413 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9414 /*ctor_dtor_or_conv_p=*/NULL,
9415 /*parenthesized_p=*/NULL,
9416 /*member_p=*/false);
9417 if (declares_class_or_enum & 2)
9418 cp_parser_check_for_definition_in_return_type (declarator,
9419 decl_specifiers.type);
9420 if (declarator != cp_error_declarator)
9421 {
9422 decl = grokdeclarator (declarator, &decl_specifiers,
9423 NORMAL, 0, &decl_specifiers.attributes);
9424 /* Turn access control back on for names used during
9425 template instantiation. */
9426 pop_deferring_access_checks ();
9427 /* Do the explicit instantiation. */
9428 do_decl_instantiation (decl, extension_specifier);
9429 }
9430 else
9431 {
9432 pop_deferring_access_checks ();
9433 /* Skip the body of the explicit instantiation. */
9434 cp_parser_skip_to_end_of_statement (parser);
9435 }
9436 }
9437 /* We're done with the instantiation. */
9438 end_explicit_instantiation ();
9439
9440 cp_parser_consume_semicolon_at_end_of_statement (parser);
9441 }
9442
9443 /* Parse an explicit-specialization.
9444
9445 explicit-specialization:
9446 template < > declaration
9447
9448 Although the standard says `declaration', what it really means is:
9449
9450 explicit-specialization:
9451 template <> decl-specifier [opt] init-declarator [opt] ;
9452 template <> function-definition
9453 template <> explicit-specialization
9454 template <> template-declaration */
9455
9456 static void
9457 cp_parser_explicit_specialization (cp_parser* parser)
9458 {
9459 bool need_lang_pop;
9460 /* Look for the `template' keyword. */
9461 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9462 /* Look for the `<'. */
9463 cp_parser_require (parser, CPP_LESS, "`<'");
9464 /* Look for the `>'. */
9465 cp_parser_require (parser, CPP_GREATER, "`>'");
9466 /* We have processed another parameter list. */
9467 ++parser->num_template_parameter_lists;
9468 /* [temp]
9469
9470 A template ... explicit specialization ... shall not have C
9471 linkage. */
9472 if (current_lang_name == lang_name_c)
9473 {
9474 error ("template specialization with C linkage");
9475 /* Give it C++ linkage to avoid confusing other parts of the
9476 front end. */
9477 push_lang_context (lang_name_cplusplus);
9478 need_lang_pop = true;
9479 }
9480 else
9481 need_lang_pop = false;
9482 /* Let the front end know that we are beginning a specialization. */
9483 begin_specialization ();
9484 /* If the next keyword is `template', we need to figure out whether
9485 or not we're looking a template-declaration. */
9486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9487 {
9488 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9489 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9490 cp_parser_template_declaration_after_export (parser,
9491 /*member_p=*/false);
9492 else
9493 cp_parser_explicit_specialization (parser);
9494 }
9495 else
9496 /* Parse the dependent declaration. */
9497 cp_parser_single_declaration (parser,
9498 /*checks=*/NULL_TREE,
9499 /*member_p=*/false,
9500 /*friend_p=*/NULL);
9501 /* We're done with the specialization. */
9502 end_specialization ();
9503 /* For the erroneous case of a template with C linkage, we pushed an
9504 implicit C++ linkage scope; exit that scope now. */
9505 if (need_lang_pop)
9506 pop_lang_context ();
9507 /* We're done with this parameter list. */
9508 --parser->num_template_parameter_lists;
9509 }
9510
9511 /* Parse a type-specifier.
9512
9513 type-specifier:
9514 simple-type-specifier
9515 class-specifier
9516 enum-specifier
9517 elaborated-type-specifier
9518 cv-qualifier
9519
9520 GNU Extension:
9521
9522 type-specifier:
9523 __complex__
9524
9525 Returns a representation of the type-specifier. For a
9526 class-specifier, enum-specifier, or elaborated-type-specifier, a
9527 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9528
9529 The parser flags FLAGS is used to control type-specifier parsing.
9530
9531 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9532 in a decl-specifier-seq.
9533
9534 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9535 class-specifier, enum-specifier, or elaborated-type-specifier, then
9536 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9537 if a type is declared; 2 if it is defined. Otherwise, it is set to
9538 zero.
9539
9540 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9541 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9542 is set to FALSE. */
9543
9544 static tree
9545 cp_parser_type_specifier (cp_parser* parser,
9546 cp_parser_flags flags,
9547 cp_decl_specifier_seq *decl_specs,
9548 bool is_declaration,
9549 int* declares_class_or_enum,
9550 bool* is_cv_qualifier)
9551 {
9552 tree type_spec = NULL_TREE;
9553 cp_token *token;
9554 enum rid keyword;
9555 cp_decl_spec ds = ds_last;
9556
9557 /* Assume this type-specifier does not declare a new type. */
9558 if (declares_class_or_enum)
9559 *declares_class_or_enum = 0;
9560 /* And that it does not specify a cv-qualifier. */
9561 if (is_cv_qualifier)
9562 *is_cv_qualifier = false;
9563 /* Peek at the next token. */
9564 token = cp_lexer_peek_token (parser->lexer);
9565
9566 /* If we're looking at a keyword, we can use that to guide the
9567 production we choose. */
9568 keyword = token->keyword;
9569 switch (keyword)
9570 {
9571 case RID_ENUM:
9572 /* Look for the enum-specifier. */
9573 type_spec = cp_parser_enum_specifier (parser);
9574 /* If that worked, we're done. */
9575 if (type_spec)
9576 {
9577 if (declares_class_or_enum)
9578 *declares_class_or_enum = 2;
9579 if (decl_specs)
9580 cp_parser_set_decl_spec_type (decl_specs,
9581 type_spec,
9582 /*user_defined_p=*/true);
9583 return type_spec;
9584 }
9585 else
9586 goto elaborated_type_specifier;
9587
9588 /* Any of these indicate either a class-specifier, or an
9589 elaborated-type-specifier. */
9590 case RID_CLASS:
9591 case RID_STRUCT:
9592 case RID_UNION:
9593 /* Parse tentatively so that we can back up if we don't find a
9594 class-specifier. */
9595 cp_parser_parse_tentatively (parser);
9596 /* Look for the class-specifier. */
9597 type_spec = cp_parser_class_specifier (parser);
9598 /* If that worked, we're done. */
9599 if (cp_parser_parse_definitely (parser))
9600 {
9601 if (declares_class_or_enum)
9602 *declares_class_or_enum = 2;
9603 if (decl_specs)
9604 cp_parser_set_decl_spec_type (decl_specs,
9605 type_spec,
9606 /*user_defined_p=*/true);
9607 return type_spec;
9608 }
9609
9610 /* Fall through. */
9611 elaborated_type_specifier:
9612 /* We're declaring (not defining) a class or enum. */
9613 if (declares_class_or_enum)
9614 *declares_class_or_enum = 1;
9615
9616 /* Fall through. */
9617 case RID_TYPENAME:
9618 /* Look for an elaborated-type-specifier. */
9619 type_spec
9620 = (cp_parser_elaborated_type_specifier
9621 (parser,
9622 decl_specs && decl_specs->specs[(int) ds_friend],
9623 is_declaration));
9624 if (decl_specs)
9625 cp_parser_set_decl_spec_type (decl_specs,
9626 type_spec,
9627 /*user_defined_p=*/true);
9628 return type_spec;
9629
9630 case RID_CONST:
9631 ds = ds_const;
9632 if (is_cv_qualifier)
9633 *is_cv_qualifier = true;
9634 break;
9635
9636 case RID_VOLATILE:
9637 ds = ds_volatile;
9638 if (is_cv_qualifier)
9639 *is_cv_qualifier = true;
9640 break;
9641
9642 case RID_RESTRICT:
9643 ds = ds_restrict;
9644 if (is_cv_qualifier)
9645 *is_cv_qualifier = true;
9646 break;
9647
9648 case RID_COMPLEX:
9649 /* The `__complex__' keyword is a GNU extension. */
9650 ds = ds_complex;
9651 break;
9652
9653 default:
9654 break;
9655 }
9656
9657 /* Handle simple keywords. */
9658 if (ds != ds_last)
9659 {
9660 if (decl_specs)
9661 {
9662 ++decl_specs->specs[(int)ds];
9663 decl_specs->any_specifiers_p = true;
9664 }
9665 return cp_lexer_consume_token (parser->lexer)->value;
9666 }
9667
9668 /* If we do not already have a type-specifier, assume we are looking
9669 at a simple-type-specifier. */
9670 type_spec = cp_parser_simple_type_specifier (parser,
9671 decl_specs,
9672 flags);
9673
9674 /* If we didn't find a type-specifier, and a type-specifier was not
9675 optional in this context, issue an error message. */
9676 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9677 {
9678 cp_parser_error (parser, "expected type specifier");
9679 return error_mark_node;
9680 }
9681
9682 return type_spec;
9683 }
9684
9685 /* Parse a simple-type-specifier.
9686
9687 simple-type-specifier:
9688 :: [opt] nested-name-specifier [opt] type-name
9689 :: [opt] nested-name-specifier template template-id
9690 char
9691 wchar_t
9692 bool
9693 short
9694 int
9695 long
9696 signed
9697 unsigned
9698 float
9699 double
9700 void
9701
9702 GNU Extension:
9703
9704 simple-type-specifier:
9705 __typeof__ unary-expression
9706 __typeof__ ( type-id )
9707
9708 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9709 appropriately updated. */
9710
9711 static tree
9712 cp_parser_simple_type_specifier (cp_parser* parser,
9713 cp_decl_specifier_seq *decl_specs,
9714 cp_parser_flags flags)
9715 {
9716 tree type = NULL_TREE;
9717 cp_token *token;
9718
9719 /* Peek at the next token. */
9720 token = cp_lexer_peek_token (parser->lexer);
9721
9722 /* If we're looking at a keyword, things are easy. */
9723 switch (token->keyword)
9724 {
9725 case RID_CHAR:
9726 if (decl_specs)
9727 decl_specs->explicit_char_p = true;
9728 type = char_type_node;
9729 break;
9730 case RID_WCHAR:
9731 type = wchar_type_node;
9732 break;
9733 case RID_BOOL:
9734 type = boolean_type_node;
9735 break;
9736 case RID_SHORT:
9737 if (decl_specs)
9738 ++decl_specs->specs[(int) ds_short];
9739 type = short_integer_type_node;
9740 break;
9741 case RID_INT:
9742 if (decl_specs)
9743 decl_specs->explicit_int_p = true;
9744 type = integer_type_node;
9745 break;
9746 case RID_LONG:
9747 if (decl_specs)
9748 ++decl_specs->specs[(int) ds_long];
9749 type = long_integer_type_node;
9750 break;
9751 case RID_SIGNED:
9752 if (decl_specs)
9753 ++decl_specs->specs[(int) ds_signed];
9754 type = integer_type_node;
9755 break;
9756 case RID_UNSIGNED:
9757 if (decl_specs)
9758 ++decl_specs->specs[(int) ds_unsigned];
9759 type = unsigned_type_node;
9760 break;
9761 case RID_FLOAT:
9762 type = float_type_node;
9763 break;
9764 case RID_DOUBLE:
9765 type = double_type_node;
9766 break;
9767 case RID_VOID:
9768 type = void_type_node;
9769 break;
9770
9771 case RID_TYPEOF:
9772 /* Consume the `typeof' token. */
9773 cp_lexer_consume_token (parser->lexer);
9774 /* Parse the operand to `typeof'. */
9775 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9776 /* If it is not already a TYPE, take its type. */
9777 if (!TYPE_P (type))
9778 type = finish_typeof (type);
9779
9780 if (decl_specs)
9781 cp_parser_set_decl_spec_type (decl_specs, type,
9782 /*user_defined_p=*/true);
9783
9784 return type;
9785
9786 default:
9787 break;
9788 }
9789
9790 /* If the type-specifier was for a built-in type, we're done. */
9791 if (type)
9792 {
9793 tree id;
9794
9795 /* Record the type. */
9796 if (decl_specs
9797 && (token->keyword != RID_SIGNED
9798 && token->keyword != RID_UNSIGNED
9799 && token->keyword != RID_SHORT
9800 && token->keyword != RID_LONG))
9801 cp_parser_set_decl_spec_type (decl_specs,
9802 type,
9803 /*user_defined=*/false);
9804 if (decl_specs)
9805 decl_specs->any_specifiers_p = true;
9806
9807 /* Consume the token. */
9808 id = cp_lexer_consume_token (parser->lexer)->value;
9809
9810 /* There is no valid C++ program where a non-template type is
9811 followed by a "<". That usually indicates that the user thought
9812 that the type was a template. */
9813 cp_parser_check_for_invalid_template_id (parser, type);
9814
9815 return TYPE_NAME (type);
9816 }
9817
9818 /* The type-specifier must be a user-defined type. */
9819 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9820 {
9821 bool qualified_p;
9822 bool global_p;
9823
9824 /* Don't gobble tokens or issue error messages if this is an
9825 optional type-specifier. */
9826 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9827 cp_parser_parse_tentatively (parser);
9828
9829 /* Look for the optional `::' operator. */
9830 global_p
9831 = (cp_parser_global_scope_opt (parser,
9832 /*current_scope_valid_p=*/false)
9833 != NULL_TREE);
9834 /* Look for the nested-name specifier. */
9835 qualified_p
9836 = (cp_parser_nested_name_specifier_opt (parser,
9837 /*typename_keyword_p=*/false,
9838 /*check_dependency_p=*/true,
9839 /*type_p=*/false,
9840 /*is_declaration=*/false)
9841 != NULL_TREE);
9842 /* If we have seen a nested-name-specifier, and the next token
9843 is `template', then we are using the template-id production. */
9844 if (parser->scope
9845 && cp_parser_optional_template_keyword (parser))
9846 {
9847 /* Look for the template-id. */
9848 type = cp_parser_template_id (parser,
9849 /*template_keyword_p=*/true,
9850 /*check_dependency_p=*/true,
9851 /*is_declaration=*/false);
9852 /* If the template-id did not name a type, we are out of
9853 luck. */
9854 if (TREE_CODE (type) != TYPE_DECL)
9855 {
9856 cp_parser_error (parser, "expected template-id for type");
9857 type = NULL_TREE;
9858 }
9859 }
9860 /* Otherwise, look for a type-name. */
9861 else
9862 type = cp_parser_type_name (parser);
9863 /* Keep track of all name-lookups performed in class scopes. */
9864 if (type
9865 && !global_p
9866 && !qualified_p
9867 && TREE_CODE (type) == TYPE_DECL
9868 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9869 maybe_note_name_used_in_class (DECL_NAME (type), type);
9870 /* If it didn't work out, we don't have a TYPE. */
9871 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9872 && !cp_parser_parse_definitely (parser))
9873 type = NULL_TREE;
9874 if (type && decl_specs)
9875 cp_parser_set_decl_spec_type (decl_specs, type,
9876 /*user_defined=*/true);
9877 }
9878
9879 /* If we didn't get a type-name, issue an error message. */
9880 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9881 {
9882 cp_parser_error (parser, "expected type-name");
9883 return error_mark_node;
9884 }
9885
9886 /* There is no valid C++ program where a non-template type is
9887 followed by a "<". That usually indicates that the user thought
9888 that the type was a template. */
9889 if (type && type != error_mark_node)
9890 {
9891 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9892 If it is, then the '<'...'>' enclose protocol names rather than
9893 template arguments, and so everything is fine. */
9894 if (c_dialect_objc ()
9895 && (objc_is_id (type) || objc_is_class_name (type)))
9896 {
9897 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9898 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9899
9900 /* Clobber the "unqualified" type previously entered into
9901 DECL_SPECS with the new, improved protocol-qualified version. */
9902 if (decl_specs)
9903 decl_specs->type = qual_type;
9904
9905 return qual_type;
9906 }
9907
9908 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9909 }
9910
9911 return type;
9912 }
9913
9914 /* Parse a type-name.
9915
9916 type-name:
9917 class-name
9918 enum-name
9919 typedef-name
9920
9921 enum-name:
9922 identifier
9923
9924 typedef-name:
9925 identifier
9926
9927 Returns a TYPE_DECL for the type. */
9928
9929 static tree
9930 cp_parser_type_name (cp_parser* parser)
9931 {
9932 tree type_decl;
9933 tree identifier;
9934
9935 /* We can't know yet whether it is a class-name or not. */
9936 cp_parser_parse_tentatively (parser);
9937 /* Try a class-name. */
9938 type_decl = cp_parser_class_name (parser,
9939 /*typename_keyword_p=*/false,
9940 /*template_keyword_p=*/false,
9941 none_type,
9942 /*check_dependency_p=*/true,
9943 /*class_head_p=*/false,
9944 /*is_declaration=*/false);
9945 /* If it's not a class-name, keep looking. */
9946 if (!cp_parser_parse_definitely (parser))
9947 {
9948 /* It must be a typedef-name or an enum-name. */
9949 identifier = cp_parser_identifier (parser);
9950 if (identifier == error_mark_node)
9951 return error_mark_node;
9952
9953 /* Look up the type-name. */
9954 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9955
9956 if (TREE_CODE (type_decl) != TYPE_DECL
9957 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9958 {
9959 /* See if this is an Objective-C type. */
9960 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9961 tree type = objc_get_protocol_qualified_type (identifier, protos);
9962 if (type)
9963 type_decl = TYPE_NAME (type);
9964 }
9965
9966 /* Issue an error if we did not find a type-name. */
9967 if (TREE_CODE (type_decl) != TYPE_DECL)
9968 {
9969 if (!cp_parser_simulate_error (parser))
9970 cp_parser_name_lookup_error (parser, identifier, type_decl,
9971 "is not a type");
9972 type_decl = error_mark_node;
9973 }
9974 /* Remember that the name was used in the definition of the
9975 current class so that we can check later to see if the
9976 meaning would have been different after the class was
9977 entirely defined. */
9978 else if (type_decl != error_mark_node
9979 && !parser->scope)
9980 maybe_note_name_used_in_class (identifier, type_decl);
9981 }
9982
9983 return type_decl;
9984 }
9985
9986
9987 /* Parse an elaborated-type-specifier. Note that the grammar given
9988 here incorporates the resolution to DR68.
9989
9990 elaborated-type-specifier:
9991 class-key :: [opt] nested-name-specifier [opt] identifier
9992 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9993 enum :: [opt] nested-name-specifier [opt] identifier
9994 typename :: [opt] nested-name-specifier identifier
9995 typename :: [opt] nested-name-specifier template [opt]
9996 template-id
9997
9998 GNU extension:
9999
10000 elaborated-type-specifier:
10001 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10002 class-key attributes :: [opt] nested-name-specifier [opt]
10003 template [opt] template-id
10004 enum attributes :: [opt] nested-name-specifier [opt] identifier
10005
10006 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10007 declared `friend'. If IS_DECLARATION is TRUE, then this
10008 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10009 something is being declared.
10010
10011 Returns the TYPE specified. */
10012
10013 static tree
10014 cp_parser_elaborated_type_specifier (cp_parser* parser,
10015 bool is_friend,
10016 bool is_declaration)
10017 {
10018 enum tag_types tag_type;
10019 tree identifier;
10020 tree type = NULL_TREE;
10021 tree attributes = NULL_TREE;
10022
10023 /* See if we're looking at the `enum' keyword. */
10024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10025 {
10026 /* Consume the `enum' token. */
10027 cp_lexer_consume_token (parser->lexer);
10028 /* Remember that it's an enumeration type. */
10029 tag_type = enum_type;
10030 /* Parse the attributes. */
10031 attributes = cp_parser_attributes_opt (parser);
10032 }
10033 /* Or, it might be `typename'. */
10034 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10035 RID_TYPENAME))
10036 {
10037 /* Consume the `typename' token. */
10038 cp_lexer_consume_token (parser->lexer);
10039 /* Remember that it's a `typename' type. */
10040 tag_type = typename_type;
10041 /* The `typename' keyword is only allowed in templates. */
10042 if (!processing_template_decl)
10043 pedwarn ("using %<typename%> outside of template");
10044 }
10045 /* Otherwise it must be a class-key. */
10046 else
10047 {
10048 tag_type = cp_parser_class_key (parser);
10049 if (tag_type == none_type)
10050 return error_mark_node;
10051 /* Parse the attributes. */
10052 attributes = cp_parser_attributes_opt (parser);
10053 }
10054
10055 /* Look for the `::' operator. */
10056 cp_parser_global_scope_opt (parser,
10057 /*current_scope_valid_p=*/false);
10058 /* Look for the nested-name-specifier. */
10059 if (tag_type == typename_type)
10060 {
10061 if (!cp_parser_nested_name_specifier (parser,
10062 /*typename_keyword_p=*/true,
10063 /*check_dependency_p=*/true,
10064 /*type_p=*/true,
10065 is_declaration))
10066 return error_mark_node;
10067 }
10068 else
10069 /* Even though `typename' is not present, the proposed resolution
10070 to Core Issue 180 says that in `class A<T>::B', `B' should be
10071 considered a type-name, even if `A<T>' is dependent. */
10072 cp_parser_nested_name_specifier_opt (parser,
10073 /*typename_keyword_p=*/true,
10074 /*check_dependency_p=*/true,
10075 /*type_p=*/true,
10076 is_declaration);
10077 /* For everything but enumeration types, consider a template-id. */
10078 /* For an enumeration type, consider only a plain identifier. */
10079 if (tag_type != enum_type)
10080 {
10081 bool template_p = false;
10082 tree decl;
10083
10084 /* Allow the `template' keyword. */
10085 template_p = cp_parser_optional_template_keyword (parser);
10086 /* If we didn't see `template', we don't know if there's a
10087 template-id or not. */
10088 if (!template_p)
10089 cp_parser_parse_tentatively (parser);
10090 /* Parse the template-id. */
10091 decl = cp_parser_template_id (parser, template_p,
10092 /*check_dependency_p=*/true,
10093 is_declaration);
10094 /* If we didn't find a template-id, look for an ordinary
10095 identifier. */
10096 if (!template_p && !cp_parser_parse_definitely (parser))
10097 ;
10098 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10099 in effect, then we must assume that, upon instantiation, the
10100 template will correspond to a class. */
10101 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10102 && tag_type == typename_type)
10103 type = make_typename_type (parser->scope, decl,
10104 typename_type,
10105 /*complain=*/tf_error);
10106 else
10107 type = TREE_TYPE (decl);
10108 }
10109
10110 if (!type)
10111 {
10112 identifier = cp_parser_identifier (parser);
10113
10114 if (identifier == error_mark_node)
10115 {
10116 parser->scope = NULL_TREE;
10117 return error_mark_node;
10118 }
10119
10120 /* For a `typename', we needn't call xref_tag. */
10121 if (tag_type == typename_type
10122 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10123 return cp_parser_make_typename_type (parser, parser->scope,
10124 identifier);
10125 /* Look up a qualified name in the usual way. */
10126 if (parser->scope)
10127 {
10128 tree decl;
10129
10130 decl = cp_parser_lookup_name (parser, identifier,
10131 tag_type,
10132 /*is_template=*/false,
10133 /*is_namespace=*/false,
10134 /*check_dependency=*/true,
10135 /*ambiguous_decls=*/NULL);
10136
10137 /* If we are parsing friend declaration, DECL may be a
10138 TEMPLATE_DECL tree node here. However, we need to check
10139 whether this TEMPLATE_DECL results in valid code. Consider
10140 the following example:
10141
10142 namespace N {
10143 template <class T> class C {};
10144 }
10145 class X {
10146 template <class T> friend class N::C; // #1, valid code
10147 };
10148 template <class T> class Y {
10149 friend class N::C; // #2, invalid code
10150 };
10151
10152 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10153 name lookup of `N::C'. We see that friend declaration must
10154 be template for the code to be valid. Note that
10155 processing_template_decl does not work here since it is
10156 always 1 for the above two cases. */
10157
10158 decl = (cp_parser_maybe_treat_template_as_class
10159 (decl, /*tag_name_p=*/is_friend
10160 && parser->num_template_parameter_lists));
10161
10162 if (TREE_CODE (decl) != TYPE_DECL)
10163 {
10164 cp_parser_diagnose_invalid_type_name (parser,
10165 parser->scope,
10166 identifier);
10167 return error_mark_node;
10168 }
10169
10170 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10171 check_elaborated_type_specifier
10172 (tag_type, decl,
10173 (parser->num_template_parameter_lists
10174 || DECL_SELF_REFERENCE_P (decl)));
10175
10176 type = TREE_TYPE (decl);
10177 }
10178 else
10179 {
10180 /* An elaborated-type-specifier sometimes introduces a new type and
10181 sometimes names an existing type. Normally, the rule is that it
10182 introduces a new type only if there is not an existing type of
10183 the same name already in scope. For example, given:
10184
10185 struct S {};
10186 void f() { struct S s; }
10187
10188 the `struct S' in the body of `f' is the same `struct S' as in
10189 the global scope; the existing definition is used. However, if
10190 there were no global declaration, this would introduce a new
10191 local class named `S'.
10192
10193 An exception to this rule applies to the following code:
10194
10195 namespace N { struct S; }
10196
10197 Here, the elaborated-type-specifier names a new type
10198 unconditionally; even if there is already an `S' in the
10199 containing scope this declaration names a new type.
10200 This exception only applies if the elaborated-type-specifier
10201 forms the complete declaration:
10202
10203 [class.name]
10204
10205 A declaration consisting solely of `class-key identifier ;' is
10206 either a redeclaration of the name in the current scope or a
10207 forward declaration of the identifier as a class name. It
10208 introduces the name into the current scope.
10209
10210 We are in this situation precisely when the next token is a `;'.
10211
10212 An exception to the exception is that a `friend' declaration does
10213 *not* name a new type; i.e., given:
10214
10215 struct S { friend struct T; };
10216
10217 `T' is not a new type in the scope of `S'.
10218
10219 Also, `new struct S' or `sizeof (struct S)' never results in the
10220 definition of a new type; a new type can only be declared in a
10221 declaration context. */
10222
10223 tag_scope ts;
10224 bool template_p;
10225
10226 if (is_friend)
10227 /* Friends have special name lookup rules. */
10228 ts = ts_within_enclosing_non_class;
10229 else if (is_declaration
10230 && cp_lexer_next_token_is (parser->lexer,
10231 CPP_SEMICOLON))
10232 /* This is a `class-key identifier ;' */
10233 ts = ts_current;
10234 else
10235 ts = ts_global;
10236
10237 template_p =
10238 (parser->num_template_parameter_lists
10239 && (cp_parser_next_token_starts_class_definition_p (parser)
10240 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10241 /* An unqualified name was used to reference this type, so
10242 there were no qualifying templates. */
10243 if (!cp_parser_check_template_parameters (parser,
10244 /*num_templates=*/0))
10245 return error_mark_node;
10246 type = xref_tag (tag_type, identifier, ts, template_p);
10247 }
10248 }
10249
10250 /* Allow attributes on forward declarations of classes. */
10251 if (attributes)
10252 {
10253 if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10254 && ! processing_explicit_instantiation)
10255 warning (OPT_Wattributes,
10256 "attributes ignored on template instantiation");
10257 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10258 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10259 else
10260 warning (OPT_Wattributes,
10261 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10262 }
10263
10264 if (tag_type != enum_type)
10265 cp_parser_check_class_key (tag_type, type);
10266
10267 /* A "<" cannot follow an elaborated type specifier. If that
10268 happens, the user was probably trying to form a template-id. */
10269 cp_parser_check_for_invalid_template_id (parser, type);
10270
10271 return type;
10272 }
10273
10274 /* Parse an enum-specifier.
10275
10276 enum-specifier:
10277 enum identifier [opt] { enumerator-list [opt] }
10278
10279 GNU Extensions:
10280 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10281 attributes[opt]
10282
10283 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10284 if the token stream isn't an enum-specifier after all. */
10285
10286 static tree
10287 cp_parser_enum_specifier (cp_parser* parser)
10288 {
10289 tree identifier;
10290 tree type;
10291 tree attributes;
10292
10293 /* Parse tentatively so that we can back up if we don't find a
10294 enum-specifier. */
10295 cp_parser_parse_tentatively (parser);
10296
10297 /* Caller guarantees that the current token is 'enum', an identifier
10298 possibly follows, and the token after that is an opening brace.
10299 If we don't have an identifier, fabricate an anonymous name for
10300 the enumeration being defined. */
10301 cp_lexer_consume_token (parser->lexer);
10302
10303 attributes = cp_parser_attributes_opt (parser);
10304
10305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10306 identifier = cp_parser_identifier (parser);
10307 else
10308 identifier = make_anon_name ();
10309
10310 /* Look for the `{' but don't consume it yet. */
10311 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10312 cp_parser_simulate_error (parser);
10313
10314 if (!cp_parser_parse_definitely (parser))
10315 return NULL_TREE;
10316
10317 /* Issue an error message if type-definitions are forbidden here. */
10318 cp_parser_check_type_definition (parser);
10319
10320 /* Create the new type. We do this before consuming the opening brace
10321 so the enum will be recorded as being on the line of its tag (or the
10322 'enum' keyword, if there is no tag). */
10323 type = start_enum (identifier);
10324
10325 /* Consume the opening brace. */
10326 cp_lexer_consume_token (parser->lexer);
10327
10328 if (type == error_mark_node)
10329 {
10330 cp_parser_skip_to_end_of_block_or_statement (parser);
10331 return error_mark_node;
10332 }
10333
10334 /* If the next token is not '}', then there are some enumerators. */
10335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10336 cp_parser_enumerator_list (parser, type);
10337
10338 /* Consume the final '}'. */
10339 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10340
10341 /* Look for trailing attributes to apply to this enumeration, and
10342 apply them if appropriate. */
10343 if (cp_parser_allow_gnu_extensions_p (parser))
10344 {
10345 tree trailing_attr = cp_parser_attributes_opt (parser);
10346 cplus_decl_attributes (&type,
10347 trailing_attr,
10348 (int) ATTR_FLAG_TYPE_IN_PLACE);
10349 }
10350
10351 /* Finish up the enumeration. */
10352 finish_enum (type);
10353
10354 return type;
10355 }
10356
10357 /* Parse an enumerator-list. The enumerators all have the indicated
10358 TYPE.
10359
10360 enumerator-list:
10361 enumerator-definition
10362 enumerator-list , enumerator-definition */
10363
10364 static void
10365 cp_parser_enumerator_list (cp_parser* parser, tree type)
10366 {
10367 while (true)
10368 {
10369 /* Parse an enumerator-definition. */
10370 cp_parser_enumerator_definition (parser, type);
10371
10372 /* If the next token is not a ',', we've reached the end of
10373 the list. */
10374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10375 break;
10376 /* Otherwise, consume the `,' and keep going. */
10377 cp_lexer_consume_token (parser->lexer);
10378 /* If the next token is a `}', there is a trailing comma. */
10379 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10380 {
10381 if (pedantic && !in_system_header)
10382 pedwarn ("comma at end of enumerator list");
10383 break;
10384 }
10385 }
10386 }
10387
10388 /* Parse an enumerator-definition. The enumerator has the indicated
10389 TYPE.
10390
10391 enumerator-definition:
10392 enumerator
10393 enumerator = constant-expression
10394
10395 enumerator:
10396 identifier */
10397
10398 static void
10399 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10400 {
10401 tree identifier;
10402 tree value;
10403
10404 /* Look for the identifier. */
10405 identifier = cp_parser_identifier (parser);
10406 if (identifier == error_mark_node)
10407 return;
10408
10409 /* If the next token is an '=', then there is an explicit value. */
10410 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10411 {
10412 /* Consume the `=' token. */
10413 cp_lexer_consume_token (parser->lexer);
10414 /* Parse the value. */
10415 value = cp_parser_constant_expression (parser,
10416 /*allow_non_constant_p=*/false,
10417 NULL);
10418 }
10419 else
10420 value = NULL_TREE;
10421
10422 /* Create the enumerator. */
10423 build_enumerator (identifier, value, type);
10424 }
10425
10426 /* Parse a namespace-name.
10427
10428 namespace-name:
10429 original-namespace-name
10430 namespace-alias
10431
10432 Returns the NAMESPACE_DECL for the namespace. */
10433
10434 static tree
10435 cp_parser_namespace_name (cp_parser* parser)
10436 {
10437 tree identifier;
10438 tree namespace_decl;
10439
10440 /* Get the name of the namespace. */
10441 identifier = cp_parser_identifier (parser);
10442 if (identifier == error_mark_node)
10443 return error_mark_node;
10444
10445 /* Look up the identifier in the currently active scope. Look only
10446 for namespaces, due to:
10447
10448 [basic.lookup.udir]
10449
10450 When looking up a namespace-name in a using-directive or alias
10451 definition, only namespace names are considered.
10452
10453 And:
10454
10455 [basic.lookup.qual]
10456
10457 During the lookup of a name preceding the :: scope resolution
10458 operator, object, function, and enumerator names are ignored.
10459
10460 (Note that cp_parser_class_or_namespace_name only calls this
10461 function if the token after the name is the scope resolution
10462 operator.) */
10463 namespace_decl = cp_parser_lookup_name (parser, identifier,
10464 none_type,
10465 /*is_template=*/false,
10466 /*is_namespace=*/true,
10467 /*check_dependency=*/true,
10468 /*ambiguous_decls=*/NULL);
10469 /* If it's not a namespace, issue an error. */
10470 if (namespace_decl == error_mark_node
10471 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10472 {
10473 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10474 error ("%qD is not a namespace-name", identifier);
10475 cp_parser_error (parser, "expected namespace-name");
10476 namespace_decl = error_mark_node;
10477 }
10478
10479 return namespace_decl;
10480 }
10481
10482 /* Parse a namespace-definition.
10483
10484 namespace-definition:
10485 named-namespace-definition
10486 unnamed-namespace-definition
10487
10488 named-namespace-definition:
10489 original-namespace-definition
10490 extension-namespace-definition
10491
10492 original-namespace-definition:
10493 namespace identifier { namespace-body }
10494
10495 extension-namespace-definition:
10496 namespace original-namespace-name { namespace-body }
10497
10498 unnamed-namespace-definition:
10499 namespace { namespace-body } */
10500
10501 static void
10502 cp_parser_namespace_definition (cp_parser* parser)
10503 {
10504 tree identifier, attribs;
10505
10506 /* Look for the `namespace' keyword. */
10507 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10508
10509 /* Get the name of the namespace. We do not attempt to distinguish
10510 between an original-namespace-definition and an
10511 extension-namespace-definition at this point. The semantic
10512 analysis routines are responsible for that. */
10513 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10514 identifier = cp_parser_identifier (parser);
10515 else
10516 identifier = NULL_TREE;
10517
10518 /* Parse any specified attributes. */
10519 attribs = cp_parser_attributes_opt (parser);
10520
10521 /* Look for the `{' to start the namespace. */
10522 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10523 /* Start the namespace. */
10524 push_namespace_with_attribs (identifier, attribs);
10525 /* Parse the body of the namespace. */
10526 cp_parser_namespace_body (parser);
10527 /* Finish the namespace. */
10528 pop_namespace ();
10529 /* Look for the final `}'. */
10530 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10531 }
10532
10533 /* Parse a namespace-body.
10534
10535 namespace-body:
10536 declaration-seq [opt] */
10537
10538 static void
10539 cp_parser_namespace_body (cp_parser* parser)
10540 {
10541 cp_parser_declaration_seq_opt (parser);
10542 }
10543
10544 /* Parse a namespace-alias-definition.
10545
10546 namespace-alias-definition:
10547 namespace identifier = qualified-namespace-specifier ; */
10548
10549 static void
10550 cp_parser_namespace_alias_definition (cp_parser* parser)
10551 {
10552 tree identifier;
10553 tree namespace_specifier;
10554
10555 /* Look for the `namespace' keyword. */
10556 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10557 /* Look for the identifier. */
10558 identifier = cp_parser_identifier (parser);
10559 if (identifier == error_mark_node)
10560 return;
10561 /* Look for the `=' token. */
10562 cp_parser_require (parser, CPP_EQ, "`='");
10563 /* Look for the qualified-namespace-specifier. */
10564 namespace_specifier
10565 = cp_parser_qualified_namespace_specifier (parser);
10566 /* Look for the `;' token. */
10567 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10568
10569 /* Register the alias in the symbol table. */
10570 do_namespace_alias (identifier, namespace_specifier);
10571 }
10572
10573 /* Parse a qualified-namespace-specifier.
10574
10575 qualified-namespace-specifier:
10576 :: [opt] nested-name-specifier [opt] namespace-name
10577
10578 Returns a NAMESPACE_DECL corresponding to the specified
10579 namespace. */
10580
10581 static tree
10582 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10583 {
10584 /* Look for the optional `::'. */
10585 cp_parser_global_scope_opt (parser,
10586 /*current_scope_valid_p=*/false);
10587
10588 /* Look for the optional nested-name-specifier. */
10589 cp_parser_nested_name_specifier_opt (parser,
10590 /*typename_keyword_p=*/false,
10591 /*check_dependency_p=*/true,
10592 /*type_p=*/false,
10593 /*is_declaration=*/true);
10594
10595 return cp_parser_namespace_name (parser);
10596 }
10597
10598 /* Parse a using-declaration.
10599
10600 using-declaration:
10601 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10602 using :: unqualified-id ; */
10603
10604 static void
10605 cp_parser_using_declaration (cp_parser* parser)
10606 {
10607 cp_token *token;
10608 bool typename_p = false;
10609 bool global_scope_p;
10610 tree decl;
10611 tree identifier;
10612 tree qscope;
10613
10614 /* Look for the `using' keyword. */
10615 cp_parser_require_keyword (parser, RID_USING, "`using'");
10616
10617 /* Peek at the next token. */
10618 token = cp_lexer_peek_token (parser->lexer);
10619 /* See if it's `typename'. */
10620 if (token->keyword == RID_TYPENAME)
10621 {
10622 /* Remember that we've seen it. */
10623 typename_p = true;
10624 /* Consume the `typename' token. */
10625 cp_lexer_consume_token (parser->lexer);
10626 }
10627
10628 /* Look for the optional global scope qualification. */
10629 global_scope_p
10630 = (cp_parser_global_scope_opt (parser,
10631 /*current_scope_valid_p=*/false)
10632 != NULL_TREE);
10633
10634 /* If we saw `typename', or didn't see `::', then there must be a
10635 nested-name-specifier present. */
10636 if (typename_p || !global_scope_p)
10637 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10638 /*check_dependency_p=*/true,
10639 /*type_p=*/false,
10640 /*is_declaration=*/true);
10641 /* Otherwise, we could be in either of the two productions. In that
10642 case, treat the nested-name-specifier as optional. */
10643 else
10644 qscope = cp_parser_nested_name_specifier_opt (parser,
10645 /*typename_keyword_p=*/false,
10646 /*check_dependency_p=*/true,
10647 /*type_p=*/false,
10648 /*is_declaration=*/true);
10649 if (!qscope)
10650 qscope = global_namespace;
10651
10652 /* Parse the unqualified-id. */
10653 identifier = cp_parser_unqualified_id (parser,
10654 /*template_keyword_p=*/false,
10655 /*check_dependency_p=*/true,
10656 /*declarator_p=*/true,
10657 /*optional_p=*/false);
10658
10659 /* The function we call to handle a using-declaration is different
10660 depending on what scope we are in. */
10661 if (qscope == error_mark_node || identifier == error_mark_node)
10662 ;
10663 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10664 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10665 /* [namespace.udecl]
10666
10667 A using declaration shall not name a template-id. */
10668 error ("a template-id may not appear in a using-declaration");
10669 else
10670 {
10671 if (at_class_scope_p ())
10672 {
10673 /* Create the USING_DECL. */
10674 decl = do_class_using_decl (parser->scope, identifier);
10675 /* Add it to the list of members in this class. */
10676 finish_member_declaration (decl);
10677 }
10678 else
10679 {
10680 decl = cp_parser_lookup_name_simple (parser, identifier);
10681 if (decl == error_mark_node)
10682 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10683 else if (!at_namespace_scope_p ())
10684 do_local_using_decl (decl, qscope, identifier);
10685 else
10686 do_toplevel_using_decl (decl, qscope, identifier);
10687 }
10688 }
10689
10690 /* Look for the final `;'. */
10691 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10692 }
10693
10694 /* Parse a using-directive.
10695
10696 using-directive:
10697 using namespace :: [opt] nested-name-specifier [opt]
10698 namespace-name ; */
10699
10700 static void
10701 cp_parser_using_directive (cp_parser* parser)
10702 {
10703 tree namespace_decl;
10704 tree attribs;
10705
10706 /* Look for the `using' keyword. */
10707 cp_parser_require_keyword (parser, RID_USING, "`using'");
10708 /* And the `namespace' keyword. */
10709 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10710 /* Look for the optional `::' operator. */
10711 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10712 /* And the optional nested-name-specifier. */
10713 cp_parser_nested_name_specifier_opt (parser,
10714 /*typename_keyword_p=*/false,
10715 /*check_dependency_p=*/true,
10716 /*type_p=*/false,
10717 /*is_declaration=*/true);
10718 /* Get the namespace being used. */
10719 namespace_decl = cp_parser_namespace_name (parser);
10720 /* And any specified attributes. */
10721 attribs = cp_parser_attributes_opt (parser);
10722 /* Update the symbol table. */
10723 parse_using_directive (namespace_decl, attribs);
10724 /* Look for the final `;'. */
10725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10726 }
10727
10728 /* Parse an asm-definition.
10729
10730 asm-definition:
10731 asm ( string-literal ) ;
10732
10733 GNU Extension:
10734
10735 asm-definition:
10736 asm volatile [opt] ( string-literal ) ;
10737 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10738 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10739 : asm-operand-list [opt] ) ;
10740 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10741 : asm-operand-list [opt]
10742 : asm-operand-list [opt] ) ; */
10743
10744 static void
10745 cp_parser_asm_definition (cp_parser* parser)
10746 {
10747 tree string;
10748 tree outputs = NULL_TREE;
10749 tree inputs = NULL_TREE;
10750 tree clobbers = NULL_TREE;
10751 tree asm_stmt;
10752 bool volatile_p = false;
10753 bool extended_p = false;
10754
10755 /* Look for the `asm' keyword. */
10756 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10757 /* See if the next token is `volatile'. */
10758 if (cp_parser_allow_gnu_extensions_p (parser)
10759 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10760 {
10761 /* Remember that we saw the `volatile' keyword. */
10762 volatile_p = true;
10763 /* Consume the token. */
10764 cp_lexer_consume_token (parser->lexer);
10765 }
10766 /* Look for the opening `('. */
10767 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10768 return;
10769 /* Look for the string. */
10770 string = cp_parser_string_literal (parser, false, false);
10771 if (string == error_mark_node)
10772 {
10773 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10774 /*consume_paren=*/true);
10775 return;
10776 }
10777
10778 /* If we're allowing GNU extensions, check for the extended assembly
10779 syntax. Unfortunately, the `:' tokens need not be separated by
10780 a space in C, and so, for compatibility, we tolerate that here
10781 too. Doing that means that we have to treat the `::' operator as
10782 two `:' tokens. */
10783 if (cp_parser_allow_gnu_extensions_p (parser)
10784 && at_function_scope_p ()
10785 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10786 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10787 {
10788 bool inputs_p = false;
10789 bool clobbers_p = false;
10790
10791 /* The extended syntax was used. */
10792 extended_p = true;
10793
10794 /* Look for outputs. */
10795 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10796 {
10797 /* Consume the `:'. */
10798 cp_lexer_consume_token (parser->lexer);
10799 /* Parse the output-operands. */
10800 if (cp_lexer_next_token_is_not (parser->lexer,
10801 CPP_COLON)
10802 && cp_lexer_next_token_is_not (parser->lexer,
10803 CPP_SCOPE)
10804 && cp_lexer_next_token_is_not (parser->lexer,
10805 CPP_CLOSE_PAREN))
10806 outputs = cp_parser_asm_operand_list (parser);
10807 }
10808 /* If the next token is `::', there are no outputs, and the
10809 next token is the beginning of the inputs. */
10810 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10811 /* The inputs are coming next. */
10812 inputs_p = true;
10813
10814 /* Look for inputs. */
10815 if (inputs_p
10816 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10817 {
10818 /* Consume the `:' or `::'. */
10819 cp_lexer_consume_token (parser->lexer);
10820 /* Parse the output-operands. */
10821 if (cp_lexer_next_token_is_not (parser->lexer,
10822 CPP_COLON)
10823 && cp_lexer_next_token_is_not (parser->lexer,
10824 CPP_CLOSE_PAREN))
10825 inputs = cp_parser_asm_operand_list (parser);
10826 }
10827 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10828 /* The clobbers are coming next. */
10829 clobbers_p = true;
10830
10831 /* Look for clobbers. */
10832 if (clobbers_p
10833 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10834 {
10835 /* Consume the `:' or `::'. */
10836 cp_lexer_consume_token (parser->lexer);
10837 /* Parse the clobbers. */
10838 if (cp_lexer_next_token_is_not (parser->lexer,
10839 CPP_CLOSE_PAREN))
10840 clobbers = cp_parser_asm_clobber_list (parser);
10841 }
10842 }
10843 /* Look for the closing `)'. */
10844 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10845 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10846 /*consume_paren=*/true);
10847 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10848
10849 /* Create the ASM_EXPR. */
10850 if (at_function_scope_p ())
10851 {
10852 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10853 inputs, clobbers);
10854 /* If the extended syntax was not used, mark the ASM_EXPR. */
10855 if (!extended_p)
10856 {
10857 tree temp = asm_stmt;
10858 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10859 temp = TREE_OPERAND (temp, 0);
10860
10861 ASM_INPUT_P (temp) = 1;
10862 }
10863 }
10864 else
10865 cgraph_add_asm_node (string);
10866 }
10867
10868 /* Declarators [gram.dcl.decl] */
10869
10870 /* Parse an init-declarator.
10871
10872 init-declarator:
10873 declarator initializer [opt]
10874
10875 GNU Extension:
10876
10877 init-declarator:
10878 declarator asm-specification [opt] attributes [opt] initializer [opt]
10879
10880 function-definition:
10881 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10882 function-body
10883 decl-specifier-seq [opt] declarator function-try-block
10884
10885 GNU Extension:
10886
10887 function-definition:
10888 __extension__ function-definition
10889
10890 The DECL_SPECIFIERS apply to this declarator. Returns a
10891 representation of the entity declared. If MEMBER_P is TRUE, then
10892 this declarator appears in a class scope. The new DECL created by
10893 this declarator is returned.
10894
10895 The CHECKS are access checks that should be performed once we know
10896 what entity is being declared (and, therefore, what classes have
10897 befriended it).
10898
10899 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10900 for a function-definition here as well. If the declarator is a
10901 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10902 be TRUE upon return. By that point, the function-definition will
10903 have been completely parsed.
10904
10905 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10906 is FALSE. */
10907
10908 static tree
10909 cp_parser_init_declarator (cp_parser* parser,
10910 cp_decl_specifier_seq *decl_specifiers,
10911 tree checks,
10912 bool function_definition_allowed_p,
10913 bool member_p,
10914 int declares_class_or_enum,
10915 bool* function_definition_p)
10916 {
10917 cp_token *token;
10918 cp_declarator *declarator;
10919 tree prefix_attributes;
10920 tree attributes;
10921 tree asm_specification;
10922 tree initializer;
10923 tree decl = NULL_TREE;
10924 tree scope;
10925 bool is_initialized;
10926 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10927 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10928 "(...)". */
10929 enum cpp_ttype initialization_kind;
10930 bool is_parenthesized_init = false;
10931 bool is_non_constant_init;
10932 int ctor_dtor_or_conv_p;
10933 bool friend_p;
10934 tree pushed_scope = NULL;
10935
10936 /* Gather the attributes that were provided with the
10937 decl-specifiers. */
10938 prefix_attributes = decl_specifiers->attributes;
10939
10940 /* Assume that this is not the declarator for a function
10941 definition. */
10942 if (function_definition_p)
10943 *function_definition_p = false;
10944
10945 /* Defer access checks while parsing the declarator; we cannot know
10946 what names are accessible until we know what is being
10947 declared. */
10948 resume_deferring_access_checks ();
10949
10950 /* Parse the declarator. */
10951 declarator
10952 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10953 &ctor_dtor_or_conv_p,
10954 /*parenthesized_p=*/NULL,
10955 /*member_p=*/false);
10956 /* Gather up the deferred checks. */
10957 stop_deferring_access_checks ();
10958
10959 /* If the DECLARATOR was erroneous, there's no need to go
10960 further. */
10961 if (declarator == cp_error_declarator)
10962 return error_mark_node;
10963
10964 if (declares_class_or_enum & 2)
10965 cp_parser_check_for_definition_in_return_type (declarator,
10966 decl_specifiers->type);
10967
10968 /* Figure out what scope the entity declared by the DECLARATOR is
10969 located in. `grokdeclarator' sometimes changes the scope, so
10970 we compute it now. */
10971 scope = get_scope_of_declarator (declarator);
10972
10973 /* If we're allowing GNU extensions, look for an asm-specification
10974 and attributes. */
10975 if (cp_parser_allow_gnu_extensions_p (parser))
10976 {
10977 /* Look for an asm-specification. */
10978 asm_specification = cp_parser_asm_specification_opt (parser);
10979 /* And attributes. */
10980 attributes = cp_parser_attributes_opt (parser);
10981 }
10982 else
10983 {
10984 asm_specification = NULL_TREE;
10985 attributes = NULL_TREE;
10986 }
10987
10988 /* Peek at the next token. */
10989 token = cp_lexer_peek_token (parser->lexer);
10990 /* Check to see if the token indicates the start of a
10991 function-definition. */
10992 if (cp_parser_token_starts_function_definition_p (token))
10993 {
10994 if (!function_definition_allowed_p)
10995 {
10996 /* If a function-definition should not appear here, issue an
10997 error message. */
10998 cp_parser_error (parser,
10999 "a function-definition is not allowed here");
11000 return error_mark_node;
11001 }
11002 else
11003 {
11004 /* Neither attributes nor an asm-specification are allowed
11005 on a function-definition. */
11006 if (asm_specification)
11007 error ("an asm-specification is not allowed on a function-definition");
11008 if (attributes)
11009 error ("attributes are not allowed on a function-definition");
11010 /* This is a function-definition. */
11011 *function_definition_p = true;
11012
11013 /* Parse the function definition. */
11014 if (member_p)
11015 decl = cp_parser_save_member_function_body (parser,
11016 decl_specifiers,
11017 declarator,
11018 prefix_attributes);
11019 else
11020 decl
11021 = (cp_parser_function_definition_from_specifiers_and_declarator
11022 (parser, decl_specifiers, prefix_attributes, declarator));
11023
11024 return decl;
11025 }
11026 }
11027
11028 /* [dcl.dcl]
11029
11030 Only in function declarations for constructors, destructors, and
11031 type conversions can the decl-specifier-seq be omitted.
11032
11033 We explicitly postpone this check past the point where we handle
11034 function-definitions because we tolerate function-definitions
11035 that are missing their return types in some modes. */
11036 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11037 {
11038 cp_parser_error (parser,
11039 "expected constructor, destructor, or type conversion");
11040 return error_mark_node;
11041 }
11042
11043 /* An `=' or an `(' indicates an initializer. */
11044 if (token->type == CPP_EQ
11045 || token->type == CPP_OPEN_PAREN)
11046 {
11047 is_initialized = true;
11048 initialization_kind = token->type;
11049 }
11050 else
11051 {
11052 /* If the init-declarator isn't initialized and isn't followed by a
11053 `,' or `;', it's not a valid init-declarator. */
11054 if (token->type != CPP_COMMA
11055 && token->type != CPP_SEMICOLON)
11056 {
11057 cp_parser_error (parser, "expected initializer");
11058 return error_mark_node;
11059 }
11060 is_initialized = false;
11061 initialization_kind = CPP_EOF;
11062 }
11063
11064 /* Because start_decl has side-effects, we should only call it if we
11065 know we're going ahead. By this point, we know that we cannot
11066 possibly be looking at any other construct. */
11067 cp_parser_commit_to_tentative_parse (parser);
11068
11069 /* If the decl specifiers were bad, issue an error now that we're
11070 sure this was intended to be a declarator. Then continue
11071 declaring the variable(s), as int, to try to cut down on further
11072 errors. */
11073 if (decl_specifiers->any_specifiers_p
11074 && decl_specifiers->type == error_mark_node)
11075 {
11076 cp_parser_error (parser, "invalid type in declaration");
11077 decl_specifiers->type = integer_type_node;
11078 }
11079
11080 /* Check to see whether or not this declaration is a friend. */
11081 friend_p = cp_parser_friend_p (decl_specifiers);
11082
11083 /* Check that the number of template-parameter-lists is OK. */
11084 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11085 return error_mark_node;
11086
11087 /* Enter the newly declared entry in the symbol table. If we're
11088 processing a declaration in a class-specifier, we wait until
11089 after processing the initializer. */
11090 if (!member_p)
11091 {
11092 if (parser->in_unbraced_linkage_specification_p)
11093 decl_specifiers->storage_class = sc_extern;
11094 decl = start_decl (declarator, decl_specifiers,
11095 is_initialized, attributes, prefix_attributes,
11096 &pushed_scope);
11097 }
11098 else if (scope)
11099 /* Enter the SCOPE. That way unqualified names appearing in the
11100 initializer will be looked up in SCOPE. */
11101 pushed_scope = push_scope (scope);
11102
11103 /* Perform deferred access control checks, now that we know in which
11104 SCOPE the declared entity resides. */
11105 if (!member_p && decl)
11106 {
11107 tree saved_current_function_decl = NULL_TREE;
11108
11109 /* If the entity being declared is a function, pretend that we
11110 are in its scope. If it is a `friend', it may have access to
11111 things that would not otherwise be accessible. */
11112 if (TREE_CODE (decl) == FUNCTION_DECL)
11113 {
11114 saved_current_function_decl = current_function_decl;
11115 current_function_decl = decl;
11116 }
11117
11118 /* Perform access checks for template parameters. */
11119 cp_parser_perform_template_parameter_access_checks (checks);
11120
11121 /* Perform the access control checks for the declarator and the
11122 the decl-specifiers. */
11123 perform_deferred_access_checks ();
11124
11125 /* Restore the saved value. */
11126 if (TREE_CODE (decl) == FUNCTION_DECL)
11127 current_function_decl = saved_current_function_decl;
11128 }
11129
11130 /* Parse the initializer. */
11131 initializer = NULL_TREE;
11132 is_parenthesized_init = false;
11133 is_non_constant_init = true;
11134 if (is_initialized)
11135 {
11136 if (declarator->kind == cdk_function
11137 && declarator->declarator->kind == cdk_id
11138 && initialization_kind == CPP_EQ)
11139 initializer = cp_parser_pure_specifier (parser);
11140 else
11141 initializer = cp_parser_initializer (parser,
11142 &is_parenthesized_init,
11143 &is_non_constant_init);
11144 }
11145
11146 /* The old parser allows attributes to appear after a parenthesized
11147 initializer. Mark Mitchell proposed removing this functionality
11148 on the GCC mailing lists on 2002-08-13. This parser accepts the
11149 attributes -- but ignores them. */
11150 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11151 if (cp_parser_attributes_opt (parser))
11152 warning (OPT_Wattributes,
11153 "attributes after parenthesized initializer ignored");
11154
11155 /* For an in-class declaration, use `grokfield' to create the
11156 declaration. */
11157 if (member_p)
11158 {
11159 if (pushed_scope)
11160 {
11161 pop_scope (pushed_scope);
11162 pushed_scope = false;
11163 }
11164 decl = grokfield (declarator, decl_specifiers,
11165 initializer, !is_non_constant_init,
11166 /*asmspec=*/NULL_TREE,
11167 prefix_attributes);
11168 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11169 cp_parser_save_default_args (parser, decl);
11170 }
11171
11172 /* Finish processing the declaration. But, skip friend
11173 declarations. */
11174 if (!friend_p && decl && decl != error_mark_node)
11175 {
11176 cp_finish_decl (decl,
11177 initializer, !is_non_constant_init,
11178 asm_specification,
11179 /* If the initializer is in parentheses, then this is
11180 a direct-initialization, which means that an
11181 `explicit' constructor is OK. Otherwise, an
11182 `explicit' constructor cannot be used. */
11183 ((is_parenthesized_init || !is_initialized)
11184 ? 0 : LOOKUP_ONLYCONVERTING));
11185 }
11186 if (!friend_p && pushed_scope)
11187 pop_scope (pushed_scope);
11188
11189 return decl;
11190 }
11191
11192 /* Parse a declarator.
11193
11194 declarator:
11195 direct-declarator
11196 ptr-operator declarator
11197
11198 abstract-declarator:
11199 ptr-operator abstract-declarator [opt]
11200 direct-abstract-declarator
11201
11202 GNU Extensions:
11203
11204 declarator:
11205 attributes [opt] direct-declarator
11206 attributes [opt] ptr-operator declarator
11207
11208 abstract-declarator:
11209 attributes [opt] ptr-operator abstract-declarator [opt]
11210 attributes [opt] direct-abstract-declarator
11211
11212 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11213 detect constructor, destructor or conversion operators. It is set
11214 to -1 if the declarator is a name, and +1 if it is a
11215 function. Otherwise it is set to zero. Usually you just want to
11216 test for >0, but internally the negative value is used.
11217
11218 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11219 a decl-specifier-seq unless it declares a constructor, destructor,
11220 or conversion. It might seem that we could check this condition in
11221 semantic analysis, rather than parsing, but that makes it difficult
11222 to handle something like `f()'. We want to notice that there are
11223 no decl-specifiers, and therefore realize that this is an
11224 expression, not a declaration.)
11225
11226 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11227 the declarator is a direct-declarator of the form "(...)".
11228
11229 MEMBER_P is true iff this declarator is a member-declarator. */
11230
11231 static cp_declarator *
11232 cp_parser_declarator (cp_parser* parser,
11233 cp_parser_declarator_kind dcl_kind,
11234 int* ctor_dtor_or_conv_p,
11235 bool* parenthesized_p,
11236 bool member_p)
11237 {
11238 cp_token *token;
11239 cp_declarator *declarator;
11240 enum tree_code code;
11241 cp_cv_quals cv_quals;
11242 tree class_type;
11243 tree attributes = NULL_TREE;
11244
11245 /* Assume this is not a constructor, destructor, or type-conversion
11246 operator. */
11247 if (ctor_dtor_or_conv_p)
11248 *ctor_dtor_or_conv_p = 0;
11249
11250 if (cp_parser_allow_gnu_extensions_p (parser))
11251 attributes = cp_parser_attributes_opt (parser);
11252
11253 /* Peek at the next token. */
11254 token = cp_lexer_peek_token (parser->lexer);
11255
11256 /* Check for the ptr-operator production. */
11257 cp_parser_parse_tentatively (parser);
11258 /* Parse the ptr-operator. */
11259 code = cp_parser_ptr_operator (parser,
11260 &class_type,
11261 &cv_quals);
11262 /* If that worked, then we have a ptr-operator. */
11263 if (cp_parser_parse_definitely (parser))
11264 {
11265 /* If a ptr-operator was found, then this declarator was not
11266 parenthesized. */
11267 if (parenthesized_p)
11268 *parenthesized_p = true;
11269 /* The dependent declarator is optional if we are parsing an
11270 abstract-declarator. */
11271 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11272 cp_parser_parse_tentatively (parser);
11273
11274 /* Parse the dependent declarator. */
11275 declarator = cp_parser_declarator (parser, dcl_kind,
11276 /*ctor_dtor_or_conv_p=*/NULL,
11277 /*parenthesized_p=*/NULL,
11278 /*member_p=*/false);
11279
11280 /* If we are parsing an abstract-declarator, we must handle the
11281 case where the dependent declarator is absent. */
11282 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11283 && !cp_parser_parse_definitely (parser))
11284 declarator = NULL;
11285
11286 /* Build the representation of the ptr-operator. */
11287 if (class_type)
11288 declarator = make_ptrmem_declarator (cv_quals,
11289 class_type,
11290 declarator);
11291 else if (code == INDIRECT_REF)
11292 declarator = make_pointer_declarator (cv_quals, declarator);
11293 else
11294 declarator = make_reference_declarator (cv_quals, declarator);
11295 }
11296 /* Everything else is a direct-declarator. */
11297 else
11298 {
11299 if (parenthesized_p)
11300 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11301 CPP_OPEN_PAREN);
11302 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11303 ctor_dtor_or_conv_p,
11304 member_p);
11305 }
11306
11307 if (attributes && declarator && declarator != cp_error_declarator)
11308 declarator->attributes = attributes;
11309
11310 return declarator;
11311 }
11312
11313 /* Parse a direct-declarator or direct-abstract-declarator.
11314
11315 direct-declarator:
11316 declarator-id
11317 direct-declarator ( parameter-declaration-clause )
11318 cv-qualifier-seq [opt]
11319 exception-specification [opt]
11320 direct-declarator [ constant-expression [opt] ]
11321 ( declarator )
11322
11323 direct-abstract-declarator:
11324 direct-abstract-declarator [opt]
11325 ( parameter-declaration-clause )
11326 cv-qualifier-seq [opt]
11327 exception-specification [opt]
11328 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11329 ( abstract-declarator )
11330
11331 Returns a representation of the declarator. DCL_KIND is
11332 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11333 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11334 we are parsing a direct-declarator. It is
11335 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11336 of ambiguity we prefer an abstract declarator, as per
11337 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11338 cp_parser_declarator. */
11339
11340 static cp_declarator *
11341 cp_parser_direct_declarator (cp_parser* parser,
11342 cp_parser_declarator_kind dcl_kind,
11343 int* ctor_dtor_or_conv_p,
11344 bool member_p)
11345 {
11346 cp_token *token;
11347 cp_declarator *declarator = NULL;
11348 tree scope = NULL_TREE;
11349 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11350 bool saved_in_declarator_p = parser->in_declarator_p;
11351 bool first = true;
11352 tree pushed_scope = NULL_TREE;
11353
11354 while (true)
11355 {
11356 /* Peek at the next token. */
11357 token = cp_lexer_peek_token (parser->lexer);
11358 if (token->type == CPP_OPEN_PAREN)
11359 {
11360 /* This is either a parameter-declaration-clause, or a
11361 parenthesized declarator. When we know we are parsing a
11362 named declarator, it must be a parenthesized declarator
11363 if FIRST is true. For instance, `(int)' is a
11364 parameter-declaration-clause, with an omitted
11365 direct-abstract-declarator. But `((*))', is a
11366 parenthesized abstract declarator. Finally, when T is a
11367 template parameter `(T)' is a
11368 parameter-declaration-clause, and not a parenthesized
11369 named declarator.
11370
11371 We first try and parse a parameter-declaration-clause,
11372 and then try a nested declarator (if FIRST is true).
11373
11374 It is not an error for it not to be a
11375 parameter-declaration-clause, even when FIRST is
11376 false. Consider,
11377
11378 int i (int);
11379 int i (3);
11380
11381 The first is the declaration of a function while the
11382 second is a the definition of a variable, including its
11383 initializer.
11384
11385 Having seen only the parenthesis, we cannot know which of
11386 these two alternatives should be selected. Even more
11387 complex are examples like:
11388
11389 int i (int (a));
11390 int i (int (3));
11391
11392 The former is a function-declaration; the latter is a
11393 variable initialization.
11394
11395 Thus again, we try a parameter-declaration-clause, and if
11396 that fails, we back out and return. */
11397
11398 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11399 {
11400 cp_parameter_declarator *params;
11401 unsigned saved_num_template_parameter_lists;
11402
11403 /* In a member-declarator, the only valid interpretation
11404 of a parenthesis is the start of a
11405 parameter-declaration-clause. (It is invalid to
11406 initialize a static data member with a parenthesized
11407 initializer; only the "=" form of initialization is
11408 permitted.) */
11409 if (!member_p)
11410 cp_parser_parse_tentatively (parser);
11411
11412 /* Consume the `('. */
11413 cp_lexer_consume_token (parser->lexer);
11414 if (first)
11415 {
11416 /* If this is going to be an abstract declarator, we're
11417 in a declarator and we can't have default args. */
11418 parser->default_arg_ok_p = false;
11419 parser->in_declarator_p = true;
11420 }
11421
11422 /* Inside the function parameter list, surrounding
11423 template-parameter-lists do not apply. */
11424 saved_num_template_parameter_lists
11425 = parser->num_template_parameter_lists;
11426 parser->num_template_parameter_lists = 0;
11427
11428 /* Parse the parameter-declaration-clause. */
11429 params = cp_parser_parameter_declaration_clause (parser);
11430
11431 parser->num_template_parameter_lists
11432 = saved_num_template_parameter_lists;
11433
11434 /* If all went well, parse the cv-qualifier-seq and the
11435 exception-specification. */
11436 if (member_p || cp_parser_parse_definitely (parser))
11437 {
11438 cp_cv_quals cv_quals;
11439 tree exception_specification;
11440
11441 if (ctor_dtor_or_conv_p)
11442 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11443 first = false;
11444 /* Consume the `)'. */
11445 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11446
11447 /* Parse the cv-qualifier-seq. */
11448 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11449 /* And the exception-specification. */
11450 exception_specification
11451 = cp_parser_exception_specification_opt (parser);
11452
11453 /* Create the function-declarator. */
11454 declarator = make_call_declarator (declarator,
11455 params,
11456 cv_quals,
11457 exception_specification);
11458 /* Any subsequent parameter lists are to do with
11459 return type, so are not those of the declared
11460 function. */
11461 parser->default_arg_ok_p = false;
11462
11463 /* Repeat the main loop. */
11464 continue;
11465 }
11466 }
11467
11468 /* If this is the first, we can try a parenthesized
11469 declarator. */
11470 if (first)
11471 {
11472 bool saved_in_type_id_in_expr_p;
11473
11474 parser->default_arg_ok_p = saved_default_arg_ok_p;
11475 parser->in_declarator_p = saved_in_declarator_p;
11476
11477 /* Consume the `('. */
11478 cp_lexer_consume_token (parser->lexer);
11479 /* Parse the nested declarator. */
11480 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11481 parser->in_type_id_in_expr_p = true;
11482 declarator
11483 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11484 /*parenthesized_p=*/NULL,
11485 member_p);
11486 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11487 first = false;
11488 /* Expect a `)'. */
11489 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11490 declarator = cp_error_declarator;
11491 if (declarator == cp_error_declarator)
11492 break;
11493
11494 goto handle_declarator;
11495 }
11496 /* Otherwise, we must be done. */
11497 else
11498 break;
11499 }
11500 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11501 && token->type == CPP_OPEN_SQUARE)
11502 {
11503 /* Parse an array-declarator. */
11504 tree bounds;
11505
11506 if (ctor_dtor_or_conv_p)
11507 *ctor_dtor_or_conv_p = 0;
11508
11509 first = false;
11510 parser->default_arg_ok_p = false;
11511 parser->in_declarator_p = true;
11512 /* Consume the `['. */
11513 cp_lexer_consume_token (parser->lexer);
11514 /* Peek at the next token. */
11515 token = cp_lexer_peek_token (parser->lexer);
11516 /* If the next token is `]', then there is no
11517 constant-expression. */
11518 if (token->type != CPP_CLOSE_SQUARE)
11519 {
11520 bool non_constant_p;
11521
11522 bounds
11523 = cp_parser_constant_expression (parser,
11524 /*allow_non_constant=*/true,
11525 &non_constant_p);
11526 if (!non_constant_p)
11527 bounds = fold_non_dependent_expr (bounds);
11528 /* Normally, the array bound must be an integral constant
11529 expression. However, as an extension, we allow VLAs
11530 in function scopes. */
11531 else if (!at_function_scope_p ())
11532 {
11533 error ("array bound is not an integer constant");
11534 bounds = error_mark_node;
11535 }
11536 }
11537 else
11538 bounds = NULL_TREE;
11539 /* Look for the closing `]'. */
11540 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11541 {
11542 declarator = cp_error_declarator;
11543 break;
11544 }
11545
11546 declarator = make_array_declarator (declarator, bounds);
11547 }
11548 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11549 {
11550 tree qualifying_scope;
11551 tree unqualified_name;
11552 special_function_kind sfk;
11553 bool abstract_ok;
11554
11555 /* Parse a declarator-id */
11556 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11557 if (abstract_ok)
11558 cp_parser_parse_tentatively (parser);
11559 unqualified_name
11560 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11561 qualifying_scope = parser->scope;
11562 if (abstract_ok)
11563 {
11564 if (!cp_parser_parse_definitely (parser))
11565 unqualified_name = error_mark_node;
11566 else if (unqualified_name
11567 && (qualifying_scope
11568 || (TREE_CODE (unqualified_name)
11569 != IDENTIFIER_NODE)))
11570 {
11571 cp_parser_error (parser, "expected unqualified-id");
11572 unqualified_name = error_mark_node;
11573 }
11574 }
11575
11576 if (!unqualified_name)
11577 return NULL;
11578 if (unqualified_name == error_mark_node)
11579 {
11580 declarator = cp_error_declarator;
11581 break;
11582 }
11583
11584 if (qualifying_scope && at_namespace_scope_p ()
11585 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11586 {
11587 /* In the declaration of a member of a template class
11588 outside of the class itself, the SCOPE will sometimes
11589 be a TYPENAME_TYPE. For example, given:
11590
11591 template <typename T>
11592 int S<T>::R::i = 3;
11593
11594 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11595 this context, we must resolve S<T>::R to an ordinary
11596 type, rather than a typename type.
11597
11598 The reason we normally avoid resolving TYPENAME_TYPEs
11599 is that a specialization of `S' might render
11600 `S<T>::R' not a type. However, if `S' is
11601 specialized, then this `i' will not be used, so there
11602 is no harm in resolving the types here. */
11603 tree type;
11604
11605 /* Resolve the TYPENAME_TYPE. */
11606 type = resolve_typename_type (qualifying_scope,
11607 /*only_current_p=*/false);
11608 /* If that failed, the declarator is invalid. */
11609 if (type == error_mark_node)
11610 error ("%<%T::%D%> is not a type",
11611 TYPE_CONTEXT (qualifying_scope),
11612 TYPE_IDENTIFIER (qualifying_scope));
11613 qualifying_scope = type;
11614 }
11615
11616 sfk = sfk_none;
11617 if (unqualified_name)
11618 {
11619 tree class_type;
11620
11621 if (qualifying_scope
11622 && CLASS_TYPE_P (qualifying_scope))
11623 class_type = qualifying_scope;
11624 else
11625 class_type = current_class_type;
11626
11627 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11628 {
11629 tree name_type = TREE_TYPE (unqualified_name);
11630 if (class_type && same_type_p (name_type, class_type))
11631 {
11632 if (qualifying_scope
11633 && CLASSTYPE_USE_TEMPLATE (name_type))
11634 {
11635 error ("invalid use of constructor as a template");
11636 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11637 "name the constructor in a qualified name",
11638 class_type,
11639 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11640 class_type, name_type);
11641 declarator = cp_error_declarator;
11642 break;
11643 }
11644 else
11645 unqualified_name = constructor_name (class_type);
11646 }
11647 else
11648 {
11649 /* We do not attempt to print the declarator
11650 here because we do not have enough
11651 information about its original syntactic
11652 form. */
11653 cp_parser_error (parser, "invalid declarator");
11654 declarator = cp_error_declarator;
11655 break;
11656 }
11657 }
11658
11659 if (class_type)
11660 {
11661 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11662 sfk = sfk_destructor;
11663 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11664 sfk = sfk_conversion;
11665 else if (/* There's no way to declare a constructor
11666 for an anonymous type, even if the type
11667 got a name for linkage purposes. */
11668 !TYPE_WAS_ANONYMOUS (class_type)
11669 && constructor_name_p (unqualified_name,
11670 class_type))
11671 {
11672 unqualified_name = constructor_name (class_type);
11673 sfk = sfk_constructor;
11674 }
11675
11676 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11677 *ctor_dtor_or_conv_p = -1;
11678 }
11679 }
11680 declarator = make_id_declarator (qualifying_scope,
11681 unqualified_name,
11682 sfk);
11683 declarator->id_loc = token->location;
11684
11685 handle_declarator:;
11686 scope = get_scope_of_declarator (declarator);
11687 if (scope)
11688 /* Any names that appear after the declarator-id for a
11689 member are looked up in the containing scope. */
11690 pushed_scope = push_scope (scope);
11691 parser->in_declarator_p = true;
11692 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11693 || (declarator && declarator->kind == cdk_id))
11694 /* Default args are only allowed on function
11695 declarations. */
11696 parser->default_arg_ok_p = saved_default_arg_ok_p;
11697 else
11698 parser->default_arg_ok_p = false;
11699
11700 first = false;
11701 }
11702 /* We're done. */
11703 else
11704 break;
11705 }
11706
11707 /* For an abstract declarator, we might wind up with nothing at this
11708 point. That's an error; the declarator is not optional. */
11709 if (!declarator)
11710 cp_parser_error (parser, "expected declarator");
11711
11712 /* If we entered a scope, we must exit it now. */
11713 if (pushed_scope)
11714 pop_scope (pushed_scope);
11715
11716 parser->default_arg_ok_p = saved_default_arg_ok_p;
11717 parser->in_declarator_p = saved_in_declarator_p;
11718
11719 return declarator;
11720 }
11721
11722 /* Parse a ptr-operator.
11723
11724 ptr-operator:
11725 * cv-qualifier-seq [opt]
11726 &
11727 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11728
11729 GNU Extension:
11730
11731 ptr-operator:
11732 & cv-qualifier-seq [opt]
11733
11734 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11735 Returns ADDR_EXPR if a reference was used. In the case of a
11736 pointer-to-member, *TYPE is filled in with the TYPE containing the
11737 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11738 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11739 ERROR_MARK if an error occurred. */
11740
11741 static enum tree_code
11742 cp_parser_ptr_operator (cp_parser* parser,
11743 tree* type,
11744 cp_cv_quals *cv_quals)
11745 {
11746 enum tree_code code = ERROR_MARK;
11747 cp_token *token;
11748
11749 /* Assume that it's not a pointer-to-member. */
11750 *type = NULL_TREE;
11751 /* And that there are no cv-qualifiers. */
11752 *cv_quals = TYPE_UNQUALIFIED;
11753
11754 /* Peek at the next token. */
11755 token = cp_lexer_peek_token (parser->lexer);
11756 /* If it's a `*' or `&' we have a pointer or reference. */
11757 if (token->type == CPP_MULT || token->type == CPP_AND)
11758 {
11759 /* Remember which ptr-operator we were processing. */
11760 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11761
11762 /* Consume the `*' or `&'. */
11763 cp_lexer_consume_token (parser->lexer);
11764
11765 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11766 `&', if we are allowing GNU extensions. (The only qualifier
11767 that can legally appear after `&' is `restrict', but that is
11768 enforced during semantic analysis. */
11769 if (code == INDIRECT_REF
11770 || cp_parser_allow_gnu_extensions_p (parser))
11771 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11772 }
11773 else
11774 {
11775 /* Try the pointer-to-member case. */
11776 cp_parser_parse_tentatively (parser);
11777 /* Look for the optional `::' operator. */
11778 cp_parser_global_scope_opt (parser,
11779 /*current_scope_valid_p=*/false);
11780 /* Look for the nested-name specifier. */
11781 cp_parser_nested_name_specifier (parser,
11782 /*typename_keyword_p=*/false,
11783 /*check_dependency_p=*/true,
11784 /*type_p=*/false,
11785 /*is_declaration=*/false);
11786 /* If we found it, and the next token is a `*', then we are
11787 indeed looking at a pointer-to-member operator. */
11788 if (!cp_parser_error_occurred (parser)
11789 && cp_parser_require (parser, CPP_MULT, "`*'"))
11790 {
11791 /* Indicate that the `*' operator was used. */
11792 code = INDIRECT_REF;
11793
11794 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11795 error ("%qD is a namespace", parser->scope);
11796 else
11797 {
11798 /* The type of which the member is a member is given by the
11799 current SCOPE. */
11800 *type = parser->scope;
11801 /* The next name will not be qualified. */
11802 parser->scope = NULL_TREE;
11803 parser->qualifying_scope = NULL_TREE;
11804 parser->object_scope = NULL_TREE;
11805 /* Look for the optional cv-qualifier-seq. */
11806 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11807 }
11808 }
11809 /* If that didn't work we don't have a ptr-operator. */
11810 if (!cp_parser_parse_definitely (parser))
11811 cp_parser_error (parser, "expected ptr-operator");
11812 }
11813
11814 return code;
11815 }
11816
11817 /* Parse an (optional) cv-qualifier-seq.
11818
11819 cv-qualifier-seq:
11820 cv-qualifier cv-qualifier-seq [opt]
11821
11822 cv-qualifier:
11823 const
11824 volatile
11825
11826 GNU Extension:
11827
11828 cv-qualifier:
11829 __restrict__
11830
11831 Returns a bitmask representing the cv-qualifiers. */
11832
11833 static cp_cv_quals
11834 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11835 {
11836 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11837
11838 while (true)
11839 {
11840 cp_token *token;
11841 cp_cv_quals cv_qualifier;
11842
11843 /* Peek at the next token. */
11844 token = cp_lexer_peek_token (parser->lexer);
11845 /* See if it's a cv-qualifier. */
11846 switch (token->keyword)
11847 {
11848 case RID_CONST:
11849 cv_qualifier = TYPE_QUAL_CONST;
11850 break;
11851
11852 case RID_VOLATILE:
11853 cv_qualifier = TYPE_QUAL_VOLATILE;
11854 break;
11855
11856 case RID_RESTRICT:
11857 cv_qualifier = TYPE_QUAL_RESTRICT;
11858 break;
11859
11860 default:
11861 cv_qualifier = TYPE_UNQUALIFIED;
11862 break;
11863 }
11864
11865 if (!cv_qualifier)
11866 break;
11867
11868 if (cv_quals & cv_qualifier)
11869 {
11870 error ("duplicate cv-qualifier");
11871 cp_lexer_purge_token (parser->lexer);
11872 }
11873 else
11874 {
11875 cp_lexer_consume_token (parser->lexer);
11876 cv_quals |= cv_qualifier;
11877 }
11878 }
11879
11880 return cv_quals;
11881 }
11882
11883 /* Parse a declarator-id.
11884
11885 declarator-id:
11886 id-expression
11887 :: [opt] nested-name-specifier [opt] type-name
11888
11889 In the `id-expression' case, the value returned is as for
11890 cp_parser_id_expression if the id-expression was an unqualified-id.
11891 If the id-expression was a qualified-id, then a SCOPE_REF is
11892 returned. The first operand is the scope (either a NAMESPACE_DECL
11893 or TREE_TYPE), but the second is still just a representation of an
11894 unqualified-id. */
11895
11896 static tree
11897 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11898 {
11899 tree id;
11900 /* The expression must be an id-expression. Assume that qualified
11901 names are the names of types so that:
11902
11903 template <class T>
11904 int S<T>::R::i = 3;
11905
11906 will work; we must treat `S<T>::R' as the name of a type.
11907 Similarly, assume that qualified names are templates, where
11908 required, so that:
11909
11910 template <class T>
11911 int S<T>::R<T>::i = 3;
11912
11913 will work, too. */
11914 id = cp_parser_id_expression (parser,
11915 /*template_keyword_p=*/false,
11916 /*check_dependency_p=*/false,
11917 /*template_p=*/NULL,
11918 /*declarator_p=*/true,
11919 optional_p);
11920 if (id && BASELINK_P (id))
11921 id = BASELINK_FUNCTIONS (id);
11922 return id;
11923 }
11924
11925 /* Parse a type-id.
11926
11927 type-id:
11928 type-specifier-seq abstract-declarator [opt]
11929
11930 Returns the TYPE specified. */
11931
11932 static tree
11933 cp_parser_type_id (cp_parser* parser)
11934 {
11935 cp_decl_specifier_seq type_specifier_seq;
11936 cp_declarator *abstract_declarator;
11937
11938 /* Parse the type-specifier-seq. */
11939 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11940 &type_specifier_seq);
11941 if (type_specifier_seq.type == error_mark_node)
11942 return error_mark_node;
11943
11944 /* There might or might not be an abstract declarator. */
11945 cp_parser_parse_tentatively (parser);
11946 /* Look for the declarator. */
11947 abstract_declarator
11948 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11949 /*parenthesized_p=*/NULL,
11950 /*member_p=*/false);
11951 /* Check to see if there really was a declarator. */
11952 if (!cp_parser_parse_definitely (parser))
11953 abstract_declarator = NULL;
11954
11955 return groktypename (&type_specifier_seq, abstract_declarator);
11956 }
11957
11958 /* Parse a type-specifier-seq.
11959
11960 type-specifier-seq:
11961 type-specifier type-specifier-seq [opt]
11962
11963 GNU extension:
11964
11965 type-specifier-seq:
11966 attributes type-specifier-seq [opt]
11967
11968 If IS_CONDITION is true, we are at the start of a "condition",
11969 e.g., we've just seen "if (".
11970
11971 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11972
11973 static void
11974 cp_parser_type_specifier_seq (cp_parser* parser,
11975 bool is_condition,
11976 cp_decl_specifier_seq *type_specifier_seq)
11977 {
11978 bool seen_type_specifier = false;
11979 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11980
11981 /* Clear the TYPE_SPECIFIER_SEQ. */
11982 clear_decl_specs (type_specifier_seq);
11983
11984 /* Parse the type-specifiers and attributes. */
11985 while (true)
11986 {
11987 tree type_specifier;
11988 bool is_cv_qualifier;
11989
11990 /* Check for attributes first. */
11991 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11992 {
11993 type_specifier_seq->attributes =
11994 chainon (type_specifier_seq->attributes,
11995 cp_parser_attributes_opt (parser));
11996 continue;
11997 }
11998
11999 /* Look for the type-specifier. */
12000 type_specifier = cp_parser_type_specifier (parser,
12001 flags,
12002 type_specifier_seq,
12003 /*is_declaration=*/false,
12004 NULL,
12005 &is_cv_qualifier);
12006 if (!type_specifier)
12007 {
12008 /* If the first type-specifier could not be found, this is not a
12009 type-specifier-seq at all. */
12010 if (!seen_type_specifier)
12011 {
12012 cp_parser_error (parser, "expected type-specifier");
12013 type_specifier_seq->type = error_mark_node;
12014 return;
12015 }
12016 /* If subsequent type-specifiers could not be found, the
12017 type-specifier-seq is complete. */
12018 break;
12019 }
12020
12021 seen_type_specifier = true;
12022 /* The standard says that a condition can be:
12023
12024 type-specifier-seq declarator = assignment-expression
12025
12026 However, given:
12027
12028 struct S {};
12029 if (int S = ...)
12030
12031 we should treat the "S" as a declarator, not as a
12032 type-specifier. The standard doesn't say that explicitly for
12033 type-specifier-seq, but it does say that for
12034 decl-specifier-seq in an ordinary declaration. Perhaps it
12035 would be clearer just to allow a decl-specifier-seq here, and
12036 then add a semantic restriction that if any decl-specifiers
12037 that are not type-specifiers appear, the program is invalid. */
12038 if (is_condition && !is_cv_qualifier)
12039 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12040 }
12041
12042 cp_parser_check_decl_spec (type_specifier_seq);
12043 }
12044
12045 /* Parse a parameter-declaration-clause.
12046
12047 parameter-declaration-clause:
12048 parameter-declaration-list [opt] ... [opt]
12049 parameter-declaration-list , ...
12050
12051 Returns a representation for the parameter declarations. A return
12052 value of NULL indicates a parameter-declaration-clause consisting
12053 only of an ellipsis. */
12054
12055 static cp_parameter_declarator *
12056 cp_parser_parameter_declaration_clause (cp_parser* parser)
12057 {
12058 cp_parameter_declarator *parameters;
12059 cp_token *token;
12060 bool ellipsis_p;
12061 bool is_error;
12062
12063 /* Peek at the next token. */
12064 token = cp_lexer_peek_token (parser->lexer);
12065 /* Check for trivial parameter-declaration-clauses. */
12066 if (token->type == CPP_ELLIPSIS)
12067 {
12068 /* Consume the `...' token. */
12069 cp_lexer_consume_token (parser->lexer);
12070 return NULL;
12071 }
12072 else if (token->type == CPP_CLOSE_PAREN)
12073 /* There are no parameters. */
12074 {
12075 #ifndef NO_IMPLICIT_EXTERN_C
12076 if (in_system_header && current_class_type == NULL
12077 && current_lang_name == lang_name_c)
12078 return NULL;
12079 else
12080 #endif
12081 return no_parameters;
12082 }
12083 /* Check for `(void)', too, which is a special case. */
12084 else if (token->keyword == RID_VOID
12085 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12086 == CPP_CLOSE_PAREN))
12087 {
12088 /* Consume the `void' token. */
12089 cp_lexer_consume_token (parser->lexer);
12090 /* There are no parameters. */
12091 return no_parameters;
12092 }
12093
12094 /* Parse the parameter-declaration-list. */
12095 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12096 /* If a parse error occurred while parsing the
12097 parameter-declaration-list, then the entire
12098 parameter-declaration-clause is erroneous. */
12099 if (is_error)
12100 return NULL;
12101
12102 /* Peek at the next token. */
12103 token = cp_lexer_peek_token (parser->lexer);
12104 /* If it's a `,', the clause should terminate with an ellipsis. */
12105 if (token->type == CPP_COMMA)
12106 {
12107 /* Consume the `,'. */
12108 cp_lexer_consume_token (parser->lexer);
12109 /* Expect an ellipsis. */
12110 ellipsis_p
12111 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12112 }
12113 /* It might also be `...' if the optional trailing `,' was
12114 omitted. */
12115 else if (token->type == CPP_ELLIPSIS)
12116 {
12117 /* Consume the `...' token. */
12118 cp_lexer_consume_token (parser->lexer);
12119 /* And remember that we saw it. */
12120 ellipsis_p = true;
12121 }
12122 else
12123 ellipsis_p = false;
12124
12125 /* Finish the parameter list. */
12126 if (parameters && ellipsis_p)
12127 parameters->ellipsis_p = true;
12128
12129 return parameters;
12130 }
12131
12132 /* Parse a parameter-declaration-list.
12133
12134 parameter-declaration-list:
12135 parameter-declaration
12136 parameter-declaration-list , parameter-declaration
12137
12138 Returns a representation of the parameter-declaration-list, as for
12139 cp_parser_parameter_declaration_clause. However, the
12140 `void_list_node' is never appended to the list. Upon return,
12141 *IS_ERROR will be true iff an error occurred. */
12142
12143 static cp_parameter_declarator *
12144 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12145 {
12146 cp_parameter_declarator *parameters = NULL;
12147 cp_parameter_declarator **tail = &parameters;
12148 bool saved_in_unbraced_linkage_specification_p;
12149
12150 /* Assume all will go well. */
12151 *is_error = false;
12152 /* The special considerations that apply to a function within an
12153 unbraced linkage specifications do not apply to the parameters
12154 to the function. */
12155 saved_in_unbraced_linkage_specification_p
12156 = parser->in_unbraced_linkage_specification_p;
12157 parser->in_unbraced_linkage_specification_p = false;
12158
12159 /* Look for more parameters. */
12160 while (true)
12161 {
12162 cp_parameter_declarator *parameter;
12163 bool parenthesized_p;
12164 /* Parse the parameter. */
12165 parameter
12166 = cp_parser_parameter_declaration (parser,
12167 /*template_parm_p=*/false,
12168 &parenthesized_p);
12169
12170 /* If a parse error occurred parsing the parameter declaration,
12171 then the entire parameter-declaration-list is erroneous. */
12172 if (!parameter)
12173 {
12174 *is_error = true;
12175 parameters = NULL;
12176 break;
12177 }
12178 /* Add the new parameter to the list. */
12179 *tail = parameter;
12180 tail = &parameter->next;
12181
12182 /* Peek at the next token. */
12183 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12184 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12185 /* These are for Objective-C++ */
12186 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12187 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12188 /* The parameter-declaration-list is complete. */
12189 break;
12190 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12191 {
12192 cp_token *token;
12193
12194 /* Peek at the next token. */
12195 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12196 /* If it's an ellipsis, then the list is complete. */
12197 if (token->type == CPP_ELLIPSIS)
12198 break;
12199 /* Otherwise, there must be more parameters. Consume the
12200 `,'. */
12201 cp_lexer_consume_token (parser->lexer);
12202 /* When parsing something like:
12203
12204 int i(float f, double d)
12205
12206 we can tell after seeing the declaration for "f" that we
12207 are not looking at an initialization of a variable "i",
12208 but rather at the declaration of a function "i".
12209
12210 Due to the fact that the parsing of template arguments
12211 (as specified to a template-id) requires backtracking we
12212 cannot use this technique when inside a template argument
12213 list. */
12214 if (!parser->in_template_argument_list_p
12215 && !parser->in_type_id_in_expr_p
12216 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12217 /* However, a parameter-declaration of the form
12218 "foat(f)" (which is a valid declaration of a
12219 parameter "f") can also be interpreted as an
12220 expression (the conversion of "f" to "float"). */
12221 && !parenthesized_p)
12222 cp_parser_commit_to_tentative_parse (parser);
12223 }
12224 else
12225 {
12226 cp_parser_error (parser, "expected %<,%> or %<...%>");
12227 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12228 cp_parser_skip_to_closing_parenthesis (parser,
12229 /*recovering=*/true,
12230 /*or_comma=*/false,
12231 /*consume_paren=*/false);
12232 break;
12233 }
12234 }
12235
12236 parser->in_unbraced_linkage_specification_p
12237 = saved_in_unbraced_linkage_specification_p;
12238
12239 return parameters;
12240 }
12241
12242 /* Parse a parameter declaration.
12243
12244 parameter-declaration:
12245 decl-specifier-seq declarator
12246 decl-specifier-seq declarator = assignment-expression
12247 decl-specifier-seq abstract-declarator [opt]
12248 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12249
12250 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12251 declares a template parameter. (In that case, a non-nested `>'
12252 token encountered during the parsing of the assignment-expression
12253 is not interpreted as a greater-than operator.)
12254
12255 Returns a representation of the parameter, or NULL if an error
12256 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12257 true iff the declarator is of the form "(p)". */
12258
12259 static cp_parameter_declarator *
12260 cp_parser_parameter_declaration (cp_parser *parser,
12261 bool template_parm_p,
12262 bool *parenthesized_p)
12263 {
12264 int declares_class_or_enum;
12265 bool greater_than_is_operator_p;
12266 cp_decl_specifier_seq decl_specifiers;
12267 cp_declarator *declarator;
12268 tree default_argument;
12269 cp_token *token;
12270 const char *saved_message;
12271
12272 /* In a template parameter, `>' is not an operator.
12273
12274 [temp.param]
12275
12276 When parsing a default template-argument for a non-type
12277 template-parameter, the first non-nested `>' is taken as the end
12278 of the template parameter-list rather than a greater-than
12279 operator. */
12280 greater_than_is_operator_p = !template_parm_p;
12281
12282 /* Type definitions may not appear in parameter types. */
12283 saved_message = parser->type_definition_forbidden_message;
12284 parser->type_definition_forbidden_message
12285 = "types may not be defined in parameter types";
12286
12287 /* Parse the declaration-specifiers. */
12288 cp_parser_decl_specifier_seq (parser,
12289 CP_PARSER_FLAGS_NONE,
12290 &decl_specifiers,
12291 &declares_class_or_enum);
12292 /* If an error occurred, there's no reason to attempt to parse the
12293 rest of the declaration. */
12294 if (cp_parser_error_occurred (parser))
12295 {
12296 parser->type_definition_forbidden_message = saved_message;
12297 return NULL;
12298 }
12299
12300 /* Peek at the next token. */
12301 token = cp_lexer_peek_token (parser->lexer);
12302 /* If the next token is a `)', `,', `=', `>', or `...', then there
12303 is no declarator. */
12304 if (token->type == CPP_CLOSE_PAREN
12305 || token->type == CPP_COMMA
12306 || token->type == CPP_EQ
12307 || token->type == CPP_ELLIPSIS
12308 || token->type == CPP_GREATER)
12309 {
12310 declarator = NULL;
12311 if (parenthesized_p)
12312 *parenthesized_p = false;
12313 }
12314 /* Otherwise, there should be a declarator. */
12315 else
12316 {
12317 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12318 parser->default_arg_ok_p = false;
12319
12320 /* After seeing a decl-specifier-seq, if the next token is not a
12321 "(", there is no possibility that the code is a valid
12322 expression. Therefore, if parsing tentatively, we commit at
12323 this point. */
12324 if (!parser->in_template_argument_list_p
12325 /* In an expression context, having seen:
12326
12327 (int((char ...
12328
12329 we cannot be sure whether we are looking at a
12330 function-type (taking a "char" as a parameter) or a cast
12331 of some object of type "char" to "int". */
12332 && !parser->in_type_id_in_expr_p
12333 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12334 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12335 cp_parser_commit_to_tentative_parse (parser);
12336 /* Parse the declarator. */
12337 declarator = cp_parser_declarator (parser,
12338 CP_PARSER_DECLARATOR_EITHER,
12339 /*ctor_dtor_or_conv_p=*/NULL,
12340 parenthesized_p,
12341 /*member_p=*/false);
12342 parser->default_arg_ok_p = saved_default_arg_ok_p;
12343 /* After the declarator, allow more attributes. */
12344 decl_specifiers.attributes
12345 = chainon (decl_specifiers.attributes,
12346 cp_parser_attributes_opt (parser));
12347 }
12348
12349 /* The restriction on defining new types applies only to the type
12350 of the parameter, not to the default argument. */
12351 parser->type_definition_forbidden_message = saved_message;
12352
12353 /* If the next token is `=', then process a default argument. */
12354 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12355 {
12356 bool saved_greater_than_is_operator_p;
12357 /* Consume the `='. */
12358 cp_lexer_consume_token (parser->lexer);
12359
12360 /* If we are defining a class, then the tokens that make up the
12361 default argument must be saved and processed later. */
12362 if (!template_parm_p && at_class_scope_p ()
12363 && TYPE_BEING_DEFINED (current_class_type))
12364 {
12365 unsigned depth = 0;
12366 cp_token *first_token;
12367 cp_token *token;
12368
12369 /* Add tokens until we have processed the entire default
12370 argument. We add the range [first_token, token). */
12371 first_token = cp_lexer_peek_token (parser->lexer);
12372 while (true)
12373 {
12374 bool done = false;
12375
12376 /* Peek at the next token. */
12377 token = cp_lexer_peek_token (parser->lexer);
12378 /* What we do depends on what token we have. */
12379 switch (token->type)
12380 {
12381 /* In valid code, a default argument must be
12382 immediately followed by a `,' `)', or `...'. */
12383 case CPP_COMMA:
12384 case CPP_CLOSE_PAREN:
12385 case CPP_ELLIPSIS:
12386 /* If we run into a non-nested `;', `}', or `]',
12387 then the code is invalid -- but the default
12388 argument is certainly over. */
12389 case CPP_SEMICOLON:
12390 case CPP_CLOSE_BRACE:
12391 case CPP_CLOSE_SQUARE:
12392 if (depth == 0)
12393 done = true;
12394 /* Update DEPTH, if necessary. */
12395 else if (token->type == CPP_CLOSE_PAREN
12396 || token->type == CPP_CLOSE_BRACE
12397 || token->type == CPP_CLOSE_SQUARE)
12398 --depth;
12399 break;
12400
12401 case CPP_OPEN_PAREN:
12402 case CPP_OPEN_SQUARE:
12403 case CPP_OPEN_BRACE:
12404 ++depth;
12405 break;
12406
12407 case CPP_GREATER:
12408 /* If we see a non-nested `>', and `>' is not an
12409 operator, then it marks the end of the default
12410 argument. */
12411 if (!depth && !greater_than_is_operator_p)
12412 done = true;
12413 break;
12414
12415 /* If we run out of tokens, issue an error message. */
12416 case CPP_EOF:
12417 case CPP_PRAGMA_EOL:
12418 error ("file ends in default argument");
12419 done = true;
12420 break;
12421
12422 case CPP_NAME:
12423 case CPP_SCOPE:
12424 /* In these cases, we should look for template-ids.
12425 For example, if the default argument is
12426 `X<int, double>()', we need to do name lookup to
12427 figure out whether or not `X' is a template; if
12428 so, the `,' does not end the default argument.
12429
12430 That is not yet done. */
12431 break;
12432
12433 default:
12434 break;
12435 }
12436
12437 /* If we've reached the end, stop. */
12438 if (done)
12439 break;
12440
12441 /* Add the token to the token block. */
12442 token = cp_lexer_consume_token (parser->lexer);
12443 }
12444
12445 /* Create a DEFAULT_ARG to represented the unparsed default
12446 argument. */
12447 default_argument = make_node (DEFAULT_ARG);
12448 DEFARG_TOKENS (default_argument)
12449 = cp_token_cache_new (first_token, token);
12450 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12451 }
12452 /* Outside of a class definition, we can just parse the
12453 assignment-expression. */
12454 else
12455 {
12456 bool saved_local_variables_forbidden_p;
12457
12458 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12459 set correctly. */
12460 saved_greater_than_is_operator_p
12461 = parser->greater_than_is_operator_p;
12462 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12463 /* Local variable names (and the `this' keyword) may not
12464 appear in a default argument. */
12465 saved_local_variables_forbidden_p
12466 = parser->local_variables_forbidden_p;
12467 parser->local_variables_forbidden_p = true;
12468 /* The default argument expression may cause implicitly
12469 defined member functions to be synthesized, which will
12470 result in garbage collection. We must treat this
12471 situation as if we were within the body of function so as
12472 to avoid collecting live data on the stack. */
12473 ++function_depth;
12474 /* Parse the assignment-expression. */
12475 if (template_parm_p)
12476 push_deferring_access_checks (dk_no_deferred);
12477 default_argument
12478 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12479 if (template_parm_p)
12480 pop_deferring_access_checks ();
12481 /* Restore saved state. */
12482 --function_depth;
12483 parser->greater_than_is_operator_p
12484 = saved_greater_than_is_operator_p;
12485 parser->local_variables_forbidden_p
12486 = saved_local_variables_forbidden_p;
12487 }
12488 if (!parser->default_arg_ok_p)
12489 {
12490 if (!flag_pedantic_errors)
12491 warning (0, "deprecated use of default argument for parameter of non-function");
12492 else
12493 {
12494 error ("default arguments are only permitted for function parameters");
12495 default_argument = NULL_TREE;
12496 }
12497 }
12498 }
12499 else
12500 default_argument = NULL_TREE;
12501
12502 return make_parameter_declarator (&decl_specifiers,
12503 declarator,
12504 default_argument);
12505 }
12506
12507 /* Parse a function-body.
12508
12509 function-body:
12510 compound_statement */
12511
12512 static void
12513 cp_parser_function_body (cp_parser *parser)
12514 {
12515 cp_parser_compound_statement (parser, NULL, false);
12516 }
12517
12518 /* Parse a ctor-initializer-opt followed by a function-body. Return
12519 true if a ctor-initializer was present. */
12520
12521 static bool
12522 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12523 {
12524 tree body;
12525 bool ctor_initializer_p;
12526
12527 /* Begin the function body. */
12528 body = begin_function_body ();
12529 /* Parse the optional ctor-initializer. */
12530 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12531 /* Parse the function-body. */
12532 cp_parser_function_body (parser);
12533 /* Finish the function body. */
12534 finish_function_body (body);
12535
12536 return ctor_initializer_p;
12537 }
12538
12539 /* Parse an initializer.
12540
12541 initializer:
12542 = initializer-clause
12543 ( expression-list )
12544
12545 Returns an expression representing the initializer. If no
12546 initializer is present, NULL_TREE is returned.
12547
12548 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12549 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12550 set to FALSE if there is no initializer present. If there is an
12551 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12552 is set to true; otherwise it is set to false. */
12553
12554 static tree
12555 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12556 bool* non_constant_p)
12557 {
12558 cp_token *token;
12559 tree init;
12560
12561 /* Peek at the next token. */
12562 token = cp_lexer_peek_token (parser->lexer);
12563
12564 /* Let our caller know whether or not this initializer was
12565 parenthesized. */
12566 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12567 /* Assume that the initializer is constant. */
12568 *non_constant_p = false;
12569
12570 if (token->type == CPP_EQ)
12571 {
12572 /* Consume the `='. */
12573 cp_lexer_consume_token (parser->lexer);
12574 /* Parse the initializer-clause. */
12575 init = cp_parser_initializer_clause (parser, non_constant_p);
12576 }
12577 else if (token->type == CPP_OPEN_PAREN)
12578 init = cp_parser_parenthesized_expression_list (parser, false,
12579 /*cast_p=*/false,
12580 non_constant_p);
12581 else
12582 {
12583 /* Anything else is an error. */
12584 cp_parser_error (parser, "expected initializer");
12585 init = error_mark_node;
12586 }
12587
12588 return init;
12589 }
12590
12591 /* Parse an initializer-clause.
12592
12593 initializer-clause:
12594 assignment-expression
12595 { initializer-list , [opt] }
12596 { }
12597
12598 Returns an expression representing the initializer.
12599
12600 If the `assignment-expression' production is used the value
12601 returned is simply a representation for the expression.
12602
12603 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12604 the elements of the initializer-list (or NULL, if the last
12605 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12606 NULL_TREE. There is no way to detect whether or not the optional
12607 trailing `,' was provided. NON_CONSTANT_P is as for
12608 cp_parser_initializer. */
12609
12610 static tree
12611 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12612 {
12613 tree initializer;
12614
12615 /* Assume the expression is constant. */
12616 *non_constant_p = false;
12617
12618 /* If it is not a `{', then we are looking at an
12619 assignment-expression. */
12620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12621 {
12622 initializer
12623 = cp_parser_constant_expression (parser,
12624 /*allow_non_constant_p=*/true,
12625 non_constant_p);
12626 if (!*non_constant_p)
12627 initializer = fold_non_dependent_expr (initializer);
12628 }
12629 else
12630 {
12631 /* Consume the `{' token. */
12632 cp_lexer_consume_token (parser->lexer);
12633 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12634 initializer = make_node (CONSTRUCTOR);
12635 /* If it's not a `}', then there is a non-trivial initializer. */
12636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12637 {
12638 /* Parse the initializer list. */
12639 CONSTRUCTOR_ELTS (initializer)
12640 = cp_parser_initializer_list (parser, non_constant_p);
12641 /* A trailing `,' token is allowed. */
12642 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12643 cp_lexer_consume_token (parser->lexer);
12644 }
12645 /* Now, there should be a trailing `}'. */
12646 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12647 }
12648
12649 return initializer;
12650 }
12651
12652 /* Parse an initializer-list.
12653
12654 initializer-list:
12655 initializer-clause
12656 initializer-list , initializer-clause
12657
12658 GNU Extension:
12659
12660 initializer-list:
12661 identifier : initializer-clause
12662 initializer-list, identifier : initializer-clause
12663
12664 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12665 for the initializer. If the INDEX of the elt is non-NULL, it is the
12666 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12667 as for cp_parser_initializer. */
12668
12669 static VEC(constructor_elt,gc) *
12670 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12671 {
12672 VEC(constructor_elt,gc) *v = NULL;
12673
12674 /* Assume all of the expressions are constant. */
12675 *non_constant_p = false;
12676
12677 /* Parse the rest of the list. */
12678 while (true)
12679 {
12680 cp_token *token;
12681 tree identifier;
12682 tree initializer;
12683 bool clause_non_constant_p;
12684
12685 /* If the next token is an identifier and the following one is a
12686 colon, we are looking at the GNU designated-initializer
12687 syntax. */
12688 if (cp_parser_allow_gnu_extensions_p (parser)
12689 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12690 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12691 {
12692 /* Consume the identifier. */
12693 identifier = cp_lexer_consume_token (parser->lexer)->value;
12694 /* Consume the `:'. */
12695 cp_lexer_consume_token (parser->lexer);
12696 }
12697 else
12698 identifier = NULL_TREE;
12699
12700 /* Parse the initializer. */
12701 initializer = cp_parser_initializer_clause (parser,
12702 &clause_non_constant_p);
12703 /* If any clause is non-constant, so is the entire initializer. */
12704 if (clause_non_constant_p)
12705 *non_constant_p = true;
12706
12707 /* Add it to the vector. */
12708 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12709
12710 /* If the next token is not a comma, we have reached the end of
12711 the list. */
12712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12713 break;
12714
12715 /* Peek at the next token. */
12716 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12717 /* If the next token is a `}', then we're still done. An
12718 initializer-clause can have a trailing `,' after the
12719 initializer-list and before the closing `}'. */
12720 if (token->type == CPP_CLOSE_BRACE)
12721 break;
12722
12723 /* Consume the `,' token. */
12724 cp_lexer_consume_token (parser->lexer);
12725 }
12726
12727 return v;
12728 }
12729
12730 /* Classes [gram.class] */
12731
12732 /* Parse a class-name.
12733
12734 class-name:
12735 identifier
12736 template-id
12737
12738 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12739 to indicate that names looked up in dependent types should be
12740 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12741 keyword has been used to indicate that the name that appears next
12742 is a template. TAG_TYPE indicates the explicit tag given before
12743 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12744 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12745 is the class being defined in a class-head.
12746
12747 Returns the TYPE_DECL representing the class. */
12748
12749 static tree
12750 cp_parser_class_name (cp_parser *parser,
12751 bool typename_keyword_p,
12752 bool template_keyword_p,
12753 enum tag_types tag_type,
12754 bool check_dependency_p,
12755 bool class_head_p,
12756 bool is_declaration)
12757 {
12758 tree decl;
12759 tree scope;
12760 bool typename_p;
12761 cp_token *token;
12762
12763 /* All class-names start with an identifier. */
12764 token = cp_lexer_peek_token (parser->lexer);
12765 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12766 {
12767 cp_parser_error (parser, "expected class-name");
12768 return error_mark_node;
12769 }
12770
12771 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12772 to a template-id, so we save it here. */
12773 scope = parser->scope;
12774 if (scope == error_mark_node)
12775 return error_mark_node;
12776
12777 /* Any name names a type if we're following the `typename' keyword
12778 in a qualified name where the enclosing scope is type-dependent. */
12779 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12780 && dependent_type_p (scope));
12781 /* Handle the common case (an identifier, but not a template-id)
12782 efficiently. */
12783 if (token->type == CPP_NAME
12784 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12785 {
12786 cp_token *identifier_token;
12787 tree identifier;
12788 bool ambiguous_p;
12789
12790 /* Look for the identifier. */
12791 identifier_token = cp_lexer_peek_token (parser->lexer);
12792 ambiguous_p = identifier_token->ambiguous_p;
12793 identifier = cp_parser_identifier (parser);
12794 /* If the next token isn't an identifier, we are certainly not
12795 looking at a class-name. */
12796 if (identifier == error_mark_node)
12797 decl = error_mark_node;
12798 /* If we know this is a type-name, there's no need to look it
12799 up. */
12800 else if (typename_p)
12801 decl = identifier;
12802 else
12803 {
12804 tree ambiguous_decls;
12805 /* If we already know that this lookup is ambiguous, then
12806 we've already issued an error message; there's no reason
12807 to check again. */
12808 if (ambiguous_p)
12809 {
12810 cp_parser_simulate_error (parser);
12811 return error_mark_node;
12812 }
12813 /* If the next token is a `::', then the name must be a type
12814 name.
12815
12816 [basic.lookup.qual]
12817
12818 During the lookup for a name preceding the :: scope
12819 resolution operator, object, function, and enumerator
12820 names are ignored. */
12821 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12822 tag_type = typename_type;
12823 /* Look up the name. */
12824 decl = cp_parser_lookup_name (parser, identifier,
12825 tag_type,
12826 /*is_template=*/false,
12827 /*is_namespace=*/false,
12828 check_dependency_p,
12829 &ambiguous_decls);
12830 if (ambiguous_decls)
12831 {
12832 error ("reference to %qD is ambiguous", identifier);
12833 print_candidates (ambiguous_decls);
12834 if (cp_parser_parsing_tentatively (parser))
12835 {
12836 identifier_token->ambiguous_p = true;
12837 cp_parser_simulate_error (parser);
12838 }
12839 return error_mark_node;
12840 }
12841 }
12842 }
12843 else
12844 {
12845 /* Try a template-id. */
12846 decl = cp_parser_template_id (parser, template_keyword_p,
12847 check_dependency_p,
12848 is_declaration);
12849 if (decl == error_mark_node)
12850 return error_mark_node;
12851 }
12852
12853 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12854
12855 /* If this is a typename, create a TYPENAME_TYPE. */
12856 if (typename_p && decl != error_mark_node)
12857 {
12858 decl = make_typename_type (scope, decl, typename_type,
12859 /*complain=*/tf_error);
12860 if (decl != error_mark_node)
12861 decl = TYPE_NAME (decl);
12862 }
12863
12864 /* Check to see that it is really the name of a class. */
12865 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12866 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12867 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12868 /* Situations like this:
12869
12870 template <typename T> struct A {
12871 typename T::template X<int>::I i;
12872 };
12873
12874 are problematic. Is `T::template X<int>' a class-name? The
12875 standard does not seem to be definitive, but there is no other
12876 valid interpretation of the following `::'. Therefore, those
12877 names are considered class-names. */
12878 {
12879 decl = make_typename_type (scope, decl, tag_type, tf_error);
12880 if (decl != error_mark_node)
12881 decl = TYPE_NAME (decl);
12882 }
12883 else if (TREE_CODE (decl) != TYPE_DECL
12884 || TREE_TYPE (decl) == error_mark_node
12885 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12886 decl = error_mark_node;
12887
12888 if (decl == error_mark_node)
12889 cp_parser_error (parser, "expected class-name");
12890
12891 return decl;
12892 }
12893
12894 /* Parse a class-specifier.
12895
12896 class-specifier:
12897 class-head { member-specification [opt] }
12898
12899 Returns the TREE_TYPE representing the class. */
12900
12901 static tree
12902 cp_parser_class_specifier (cp_parser* parser)
12903 {
12904 cp_token *token;
12905 tree type;
12906 tree attributes = NULL_TREE;
12907 int has_trailing_semicolon;
12908 bool nested_name_specifier_p;
12909 unsigned saved_num_template_parameter_lists;
12910 tree old_scope = NULL_TREE;
12911 tree scope = NULL_TREE;
12912
12913 push_deferring_access_checks (dk_no_deferred);
12914
12915 /* Parse the class-head. */
12916 type = cp_parser_class_head (parser,
12917 &nested_name_specifier_p,
12918 &attributes);
12919 /* If the class-head was a semantic disaster, skip the entire body
12920 of the class. */
12921 if (!type)
12922 {
12923 cp_parser_skip_to_end_of_block_or_statement (parser);
12924 pop_deferring_access_checks ();
12925 return error_mark_node;
12926 }
12927
12928 /* Look for the `{'. */
12929 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12930 {
12931 pop_deferring_access_checks ();
12932 return error_mark_node;
12933 }
12934
12935 /* Issue an error message if type-definitions are forbidden here. */
12936 cp_parser_check_type_definition (parser);
12937 /* Remember that we are defining one more class. */
12938 ++parser->num_classes_being_defined;
12939 /* Inside the class, surrounding template-parameter-lists do not
12940 apply. */
12941 saved_num_template_parameter_lists
12942 = parser->num_template_parameter_lists;
12943 parser->num_template_parameter_lists = 0;
12944
12945 /* Start the class. */
12946 if (nested_name_specifier_p)
12947 {
12948 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12949 old_scope = push_inner_scope (scope);
12950 }
12951 type = begin_class_definition (type, attributes);
12952
12953 if (type == error_mark_node)
12954 /* If the type is erroneous, skip the entire body of the class. */
12955 cp_parser_skip_to_closing_brace (parser);
12956 else
12957 /* Parse the member-specification. */
12958 cp_parser_member_specification_opt (parser);
12959
12960 /* Look for the trailing `}'. */
12961 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12962 /* We get better error messages by noticing a common problem: a
12963 missing trailing `;'. */
12964 token = cp_lexer_peek_token (parser->lexer);
12965 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12966 /* Look for trailing attributes to apply to this class. */
12967 if (cp_parser_allow_gnu_extensions_p (parser))
12968 attributes = cp_parser_attributes_opt (parser);
12969 if (type != error_mark_node)
12970 type = finish_struct (type, attributes);
12971 if (nested_name_specifier_p)
12972 pop_inner_scope (old_scope, scope);
12973 /* If this class is not itself within the scope of another class,
12974 then we need to parse the bodies of all of the queued function
12975 definitions. Note that the queued functions defined in a class
12976 are not always processed immediately following the
12977 class-specifier for that class. Consider:
12978
12979 struct A {
12980 struct B { void f() { sizeof (A); } };
12981 };
12982
12983 If `f' were processed before the processing of `A' were
12984 completed, there would be no way to compute the size of `A'.
12985 Note that the nesting we are interested in here is lexical --
12986 not the semantic nesting given by TYPE_CONTEXT. In particular,
12987 for:
12988
12989 struct A { struct B; };
12990 struct A::B { void f() { } };
12991
12992 there is no need to delay the parsing of `A::B::f'. */
12993 if (--parser->num_classes_being_defined == 0)
12994 {
12995 tree queue_entry;
12996 tree fn;
12997 tree class_type = NULL_TREE;
12998 tree pushed_scope = NULL_TREE;
12999
13000 /* In a first pass, parse default arguments to the functions.
13001 Then, in a second pass, parse the bodies of the functions.
13002 This two-phased approach handles cases like:
13003
13004 struct S {
13005 void f() { g(); }
13006 void g(int i = 3);
13007 };
13008
13009 */
13010 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13011 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13012 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13013 TREE_PURPOSE (parser->unparsed_functions_queues)
13014 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13015 {
13016 fn = TREE_VALUE (queue_entry);
13017 /* If there are default arguments that have not yet been processed,
13018 take care of them now. */
13019 if (class_type != TREE_PURPOSE (queue_entry))
13020 {
13021 if (pushed_scope)
13022 pop_scope (pushed_scope);
13023 class_type = TREE_PURPOSE (queue_entry);
13024 pushed_scope = push_scope (class_type);
13025 }
13026 /* Make sure that any template parameters are in scope. */
13027 maybe_begin_member_template_processing (fn);
13028 /* Parse the default argument expressions. */
13029 cp_parser_late_parsing_default_args (parser, fn);
13030 /* Remove any template parameters from the symbol table. */
13031 maybe_end_member_template_processing ();
13032 }
13033 if (pushed_scope)
13034 pop_scope (pushed_scope);
13035 /* Now parse the body of the functions. */
13036 for (TREE_VALUE (parser->unparsed_functions_queues)
13037 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13038 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13039 TREE_VALUE (parser->unparsed_functions_queues)
13040 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13041 {
13042 /* Figure out which function we need to process. */
13043 fn = TREE_VALUE (queue_entry);
13044 /* Parse the function. */
13045 cp_parser_late_parsing_for_member (parser, fn);
13046 }
13047 }
13048
13049 /* Put back any saved access checks. */
13050 pop_deferring_access_checks ();
13051
13052 /* Restore the count of active template-parameter-lists. */
13053 parser->num_template_parameter_lists
13054 = saved_num_template_parameter_lists;
13055
13056 return type;
13057 }
13058
13059 /* Parse a class-head.
13060
13061 class-head:
13062 class-key identifier [opt] base-clause [opt]
13063 class-key nested-name-specifier identifier base-clause [opt]
13064 class-key nested-name-specifier [opt] template-id
13065 base-clause [opt]
13066
13067 GNU Extensions:
13068 class-key attributes identifier [opt] base-clause [opt]
13069 class-key attributes nested-name-specifier identifier base-clause [opt]
13070 class-key attributes nested-name-specifier [opt] template-id
13071 base-clause [opt]
13072
13073 Returns the TYPE of the indicated class. Sets
13074 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13075 involving a nested-name-specifier was used, and FALSE otherwise.
13076
13077 Returns error_mark_node if this is not a class-head.
13078
13079 Returns NULL_TREE if the class-head is syntactically valid, but
13080 semantically invalid in a way that means we should skip the entire
13081 body of the class. */
13082
13083 static tree
13084 cp_parser_class_head (cp_parser* parser,
13085 bool* nested_name_specifier_p,
13086 tree *attributes_p)
13087 {
13088 tree nested_name_specifier;
13089 enum tag_types class_key;
13090 tree id = NULL_TREE;
13091 tree type = NULL_TREE;
13092 tree attributes;
13093 bool template_id_p = false;
13094 bool qualified_p = false;
13095 bool invalid_nested_name_p = false;
13096 bool invalid_explicit_specialization_p = false;
13097 tree pushed_scope = NULL_TREE;
13098 unsigned num_templates;
13099 tree bases;
13100
13101 /* Assume no nested-name-specifier will be present. */
13102 *nested_name_specifier_p = false;
13103 /* Assume no template parameter lists will be used in defining the
13104 type. */
13105 num_templates = 0;
13106
13107 /* Look for the class-key. */
13108 class_key = cp_parser_class_key (parser);
13109 if (class_key == none_type)
13110 return error_mark_node;
13111
13112 /* Parse the attributes. */
13113 attributes = cp_parser_attributes_opt (parser);
13114
13115 /* If the next token is `::', that is invalid -- but sometimes
13116 people do try to write:
13117
13118 struct ::S {};
13119
13120 Handle this gracefully by accepting the extra qualifier, and then
13121 issuing an error about it later if this really is a
13122 class-head. If it turns out just to be an elaborated type
13123 specifier, remain silent. */
13124 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13125 qualified_p = true;
13126
13127 push_deferring_access_checks (dk_no_check);
13128
13129 /* Determine the name of the class. Begin by looking for an
13130 optional nested-name-specifier. */
13131 nested_name_specifier
13132 = cp_parser_nested_name_specifier_opt (parser,
13133 /*typename_keyword_p=*/false,
13134 /*check_dependency_p=*/false,
13135 /*type_p=*/false,
13136 /*is_declaration=*/false);
13137 /* If there was a nested-name-specifier, then there *must* be an
13138 identifier. */
13139 if (nested_name_specifier)
13140 {
13141 /* Although the grammar says `identifier', it really means
13142 `class-name' or `template-name'. You are only allowed to
13143 define a class that has already been declared with this
13144 syntax.
13145
13146 The proposed resolution for Core Issue 180 says that wherever
13147 you see `class T::X' you should treat `X' as a type-name.
13148
13149 It is OK to define an inaccessible class; for example:
13150
13151 class A { class B; };
13152 class A::B {};
13153
13154 We do not know if we will see a class-name, or a
13155 template-name. We look for a class-name first, in case the
13156 class-name is a template-id; if we looked for the
13157 template-name first we would stop after the template-name. */
13158 cp_parser_parse_tentatively (parser);
13159 type = cp_parser_class_name (parser,
13160 /*typename_keyword_p=*/false,
13161 /*template_keyword_p=*/false,
13162 class_type,
13163 /*check_dependency_p=*/false,
13164 /*class_head_p=*/true,
13165 /*is_declaration=*/false);
13166 /* If that didn't work, ignore the nested-name-specifier. */
13167 if (!cp_parser_parse_definitely (parser))
13168 {
13169 invalid_nested_name_p = true;
13170 id = cp_parser_identifier (parser);
13171 if (id == error_mark_node)
13172 id = NULL_TREE;
13173 }
13174 /* If we could not find a corresponding TYPE, treat this
13175 declaration like an unqualified declaration. */
13176 if (type == error_mark_node)
13177 nested_name_specifier = NULL_TREE;
13178 /* Otherwise, count the number of templates used in TYPE and its
13179 containing scopes. */
13180 else
13181 {
13182 tree scope;
13183
13184 for (scope = TREE_TYPE (type);
13185 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13186 scope = (TYPE_P (scope)
13187 ? TYPE_CONTEXT (scope)
13188 : DECL_CONTEXT (scope)))
13189 if (TYPE_P (scope)
13190 && CLASS_TYPE_P (scope)
13191 && CLASSTYPE_TEMPLATE_INFO (scope)
13192 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13193 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13194 ++num_templates;
13195 }
13196 }
13197 /* Otherwise, the identifier is optional. */
13198 else
13199 {
13200 /* We don't know whether what comes next is a template-id,
13201 an identifier, or nothing at all. */
13202 cp_parser_parse_tentatively (parser);
13203 /* Check for a template-id. */
13204 id = cp_parser_template_id (parser,
13205 /*template_keyword_p=*/false,
13206 /*check_dependency_p=*/true,
13207 /*is_declaration=*/true);
13208 /* If that didn't work, it could still be an identifier. */
13209 if (!cp_parser_parse_definitely (parser))
13210 {
13211 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13212 id = cp_parser_identifier (parser);
13213 else
13214 id = NULL_TREE;
13215 }
13216 else
13217 {
13218 template_id_p = true;
13219 ++num_templates;
13220 }
13221 }
13222
13223 pop_deferring_access_checks ();
13224
13225 if (id)
13226 cp_parser_check_for_invalid_template_id (parser, id);
13227
13228 /* If it's not a `:' or a `{' then we can't really be looking at a
13229 class-head, since a class-head only appears as part of a
13230 class-specifier. We have to detect this situation before calling
13231 xref_tag, since that has irreversible side-effects. */
13232 if (!cp_parser_next_token_starts_class_definition_p (parser))
13233 {
13234 cp_parser_error (parser, "expected %<{%> or %<:%>");
13235 return error_mark_node;
13236 }
13237
13238 /* At this point, we're going ahead with the class-specifier, even
13239 if some other problem occurs. */
13240 cp_parser_commit_to_tentative_parse (parser);
13241 /* Issue the error about the overly-qualified name now. */
13242 if (qualified_p)
13243 cp_parser_error (parser,
13244 "global qualification of class name is invalid");
13245 else if (invalid_nested_name_p)
13246 cp_parser_error (parser,
13247 "qualified name does not name a class");
13248 else if (nested_name_specifier)
13249 {
13250 tree scope;
13251
13252 /* Reject typedef-names in class heads. */
13253 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13254 {
13255 error ("invalid class name in declaration of %qD", type);
13256 type = NULL_TREE;
13257 goto done;
13258 }
13259
13260 /* Figure out in what scope the declaration is being placed. */
13261 scope = current_scope ();
13262 /* If that scope does not contain the scope in which the
13263 class was originally declared, the program is invalid. */
13264 if (scope && !is_ancestor (scope, nested_name_specifier))
13265 {
13266 error ("declaration of %qD in %qD which does not enclose %qD",
13267 type, scope, nested_name_specifier);
13268 type = NULL_TREE;
13269 goto done;
13270 }
13271 /* [dcl.meaning]
13272
13273 A declarator-id shall not be qualified exception of the
13274 definition of a ... nested class outside of its class
13275 ... [or] a the definition or explicit instantiation of a
13276 class member of a namespace outside of its namespace. */
13277 if (scope == nested_name_specifier)
13278 {
13279 pedwarn ("extra qualification ignored");
13280 nested_name_specifier = NULL_TREE;
13281 num_templates = 0;
13282 }
13283 }
13284 /* An explicit-specialization must be preceded by "template <>". If
13285 it is not, try to recover gracefully. */
13286 if (at_namespace_scope_p ()
13287 && parser->num_template_parameter_lists == 0
13288 && template_id_p)
13289 {
13290 error ("an explicit specialization must be preceded by %<template <>%>");
13291 invalid_explicit_specialization_p = true;
13292 /* Take the same action that would have been taken by
13293 cp_parser_explicit_specialization. */
13294 ++parser->num_template_parameter_lists;
13295 begin_specialization ();
13296 }
13297 /* There must be no "return" statements between this point and the
13298 end of this function; set "type "to the correct return value and
13299 use "goto done;" to return. */
13300 /* Make sure that the right number of template parameters were
13301 present. */
13302 if (!cp_parser_check_template_parameters (parser, num_templates))
13303 {
13304 /* If something went wrong, there is no point in even trying to
13305 process the class-definition. */
13306 type = NULL_TREE;
13307 goto done;
13308 }
13309
13310 /* Look up the type. */
13311 if (template_id_p)
13312 {
13313 type = TREE_TYPE (id);
13314 maybe_process_partial_specialization (type);
13315 if (nested_name_specifier)
13316 pushed_scope = push_scope (nested_name_specifier);
13317 }
13318 else if (nested_name_specifier)
13319 {
13320 tree class_type;
13321
13322 /* Given:
13323
13324 template <typename T> struct S { struct T };
13325 template <typename T> struct S<T>::T { };
13326
13327 we will get a TYPENAME_TYPE when processing the definition of
13328 `S::T'. We need to resolve it to the actual type before we
13329 try to define it. */
13330 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13331 {
13332 class_type = resolve_typename_type (TREE_TYPE (type),
13333 /*only_current_p=*/false);
13334 if (class_type != error_mark_node)
13335 type = TYPE_NAME (class_type);
13336 else
13337 {
13338 cp_parser_error (parser, "could not resolve typename type");
13339 type = error_mark_node;
13340 }
13341 }
13342
13343 maybe_process_partial_specialization (TREE_TYPE (type));
13344 class_type = current_class_type;
13345 /* Enter the scope indicated by the nested-name-specifier. */
13346 pushed_scope = push_scope (nested_name_specifier);
13347 /* Get the canonical version of this type. */
13348 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13349 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13350 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13351 {
13352 type = push_template_decl (type);
13353 if (type == error_mark_node)
13354 {
13355 type = NULL_TREE;
13356 goto done;
13357 }
13358 }
13359
13360 type = TREE_TYPE (type);
13361 *nested_name_specifier_p = true;
13362 }
13363 else /* The name is not a nested name. */
13364 {
13365 /* If the class was unnamed, create a dummy name. */
13366 if (!id)
13367 id = make_anon_name ();
13368 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13369 parser->num_template_parameter_lists);
13370 }
13371
13372 /* Indicate whether this class was declared as a `class' or as a
13373 `struct'. */
13374 if (TREE_CODE (type) == RECORD_TYPE)
13375 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13376 cp_parser_check_class_key (class_key, type);
13377
13378 /* If this type was already complete, and we see another definition,
13379 that's an error. */
13380 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13381 {
13382 error ("redefinition of %q#T", type);
13383 error ("previous definition of %q+#T", type);
13384 type = NULL_TREE;
13385 goto done;
13386 }
13387
13388 /* We will have entered the scope containing the class; the names of
13389 base classes should be looked up in that context. For example:
13390
13391 struct A { struct B {}; struct C; };
13392 struct A::C : B {};
13393
13394 is valid. */
13395 bases = NULL_TREE;
13396
13397 /* Get the list of base-classes, if there is one. */
13398 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13399 bases = cp_parser_base_clause (parser);
13400
13401 /* Process the base classes. */
13402 xref_basetypes (type, bases);
13403
13404 done:
13405 /* Leave the scope given by the nested-name-specifier. We will
13406 enter the class scope itself while processing the members. */
13407 if (pushed_scope)
13408 pop_scope (pushed_scope);
13409
13410 if (invalid_explicit_specialization_p)
13411 {
13412 end_specialization ();
13413 --parser->num_template_parameter_lists;
13414 }
13415 *attributes_p = attributes;
13416 return type;
13417 }
13418
13419 /* Parse a class-key.
13420
13421 class-key:
13422 class
13423 struct
13424 union
13425
13426 Returns the kind of class-key specified, or none_type to indicate
13427 error. */
13428
13429 static enum tag_types
13430 cp_parser_class_key (cp_parser* parser)
13431 {
13432 cp_token *token;
13433 enum tag_types tag_type;
13434
13435 /* Look for the class-key. */
13436 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13437 if (!token)
13438 return none_type;
13439
13440 /* Check to see if the TOKEN is a class-key. */
13441 tag_type = cp_parser_token_is_class_key (token);
13442 if (!tag_type)
13443 cp_parser_error (parser, "expected class-key");
13444 return tag_type;
13445 }
13446
13447 /* Parse an (optional) member-specification.
13448
13449 member-specification:
13450 member-declaration member-specification [opt]
13451 access-specifier : member-specification [opt] */
13452
13453 static void
13454 cp_parser_member_specification_opt (cp_parser* parser)
13455 {
13456 while (true)
13457 {
13458 cp_token *token;
13459 enum rid keyword;
13460
13461 /* Peek at the next token. */
13462 token = cp_lexer_peek_token (parser->lexer);
13463 /* If it's a `}', or EOF then we've seen all the members. */
13464 if (token->type == CPP_CLOSE_BRACE
13465 || token->type == CPP_EOF
13466 || token->type == CPP_PRAGMA_EOL)
13467 break;
13468
13469 /* See if this token is a keyword. */
13470 keyword = token->keyword;
13471 switch (keyword)
13472 {
13473 case RID_PUBLIC:
13474 case RID_PROTECTED:
13475 case RID_PRIVATE:
13476 /* Consume the access-specifier. */
13477 cp_lexer_consume_token (parser->lexer);
13478 /* Remember which access-specifier is active. */
13479 current_access_specifier = token->value;
13480 /* Look for the `:'. */
13481 cp_parser_require (parser, CPP_COLON, "`:'");
13482 break;
13483
13484 default:
13485 /* Accept #pragmas at class scope. */
13486 if (token->type == CPP_PRAGMA)
13487 {
13488 cp_parser_pragma (parser, pragma_external);
13489 break;
13490 }
13491
13492 /* Otherwise, the next construction must be a
13493 member-declaration. */
13494 cp_parser_member_declaration (parser);
13495 }
13496 }
13497 }
13498
13499 /* Parse a member-declaration.
13500
13501 member-declaration:
13502 decl-specifier-seq [opt] member-declarator-list [opt] ;
13503 function-definition ; [opt]
13504 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13505 using-declaration
13506 template-declaration
13507
13508 member-declarator-list:
13509 member-declarator
13510 member-declarator-list , member-declarator
13511
13512 member-declarator:
13513 declarator pure-specifier [opt]
13514 declarator constant-initializer [opt]
13515 identifier [opt] : constant-expression
13516
13517 GNU Extensions:
13518
13519 member-declaration:
13520 __extension__ member-declaration
13521
13522 member-declarator:
13523 declarator attributes [opt] pure-specifier [opt]
13524 declarator attributes [opt] constant-initializer [opt]
13525 identifier [opt] attributes [opt] : constant-expression */
13526
13527 static void
13528 cp_parser_member_declaration (cp_parser* parser)
13529 {
13530 cp_decl_specifier_seq decl_specifiers;
13531 tree prefix_attributes;
13532 tree decl;
13533 int declares_class_or_enum;
13534 bool friend_p;
13535 cp_token *token;
13536 int saved_pedantic;
13537
13538 /* Check for the `__extension__' keyword. */
13539 if (cp_parser_extension_opt (parser, &saved_pedantic))
13540 {
13541 /* Recurse. */
13542 cp_parser_member_declaration (parser);
13543 /* Restore the old value of the PEDANTIC flag. */
13544 pedantic = saved_pedantic;
13545
13546 return;
13547 }
13548
13549 /* Check for a template-declaration. */
13550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13551 {
13552 /* An explicit specialization here is an error condition, and we
13553 expect the specialization handler to detect and report this. */
13554 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13555 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13556 cp_parser_explicit_specialization (parser);
13557 else
13558 cp_parser_template_declaration (parser, /*member_p=*/true);
13559
13560 return;
13561 }
13562
13563 /* Check for a using-declaration. */
13564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13565 {
13566 /* Parse the using-declaration. */
13567 cp_parser_using_declaration (parser);
13568
13569 return;
13570 }
13571
13572 /* Check for @defs. */
13573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13574 {
13575 tree ivar, member;
13576 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13577 ivar = ivar_chains;
13578 while (ivar)
13579 {
13580 member = ivar;
13581 ivar = TREE_CHAIN (member);
13582 TREE_CHAIN (member) = NULL_TREE;
13583 finish_member_declaration (member);
13584 }
13585 return;
13586 }
13587
13588 /* Parse the decl-specifier-seq. */
13589 cp_parser_decl_specifier_seq (parser,
13590 CP_PARSER_FLAGS_OPTIONAL,
13591 &decl_specifiers,
13592 &declares_class_or_enum);
13593 prefix_attributes = decl_specifiers.attributes;
13594 decl_specifiers.attributes = NULL_TREE;
13595 /* Check for an invalid type-name. */
13596 if (!decl_specifiers.type
13597 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13598 return;
13599 /* If there is no declarator, then the decl-specifier-seq should
13600 specify a type. */
13601 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13602 {
13603 /* If there was no decl-specifier-seq, and the next token is a
13604 `;', then we have something like:
13605
13606 struct S { ; };
13607
13608 [class.mem]
13609
13610 Each member-declaration shall declare at least one member
13611 name of the class. */
13612 if (!decl_specifiers.any_specifiers_p)
13613 {
13614 cp_token *token = cp_lexer_peek_token (parser->lexer);
13615 if (pedantic && !token->in_system_header)
13616 pedwarn ("%Hextra %<;%>", &token->location);
13617 }
13618 else
13619 {
13620 tree type;
13621
13622 /* See if this declaration is a friend. */
13623 friend_p = cp_parser_friend_p (&decl_specifiers);
13624 /* If there were decl-specifiers, check to see if there was
13625 a class-declaration. */
13626 type = check_tag_decl (&decl_specifiers);
13627 /* Nested classes have already been added to the class, but
13628 a `friend' needs to be explicitly registered. */
13629 if (friend_p)
13630 {
13631 /* If the `friend' keyword was present, the friend must
13632 be introduced with a class-key. */
13633 if (!declares_class_or_enum)
13634 error ("a class-key must be used when declaring a friend");
13635 /* In this case:
13636
13637 template <typename T> struct A {
13638 friend struct A<T>::B;
13639 };
13640
13641 A<T>::B will be represented by a TYPENAME_TYPE, and
13642 therefore not recognized by check_tag_decl. */
13643 if (!type
13644 && decl_specifiers.type
13645 && TYPE_P (decl_specifiers.type))
13646 type = decl_specifiers.type;
13647 if (!type || !TYPE_P (type))
13648 error ("friend declaration does not name a class or "
13649 "function");
13650 else
13651 make_friend_class (current_class_type, type,
13652 /*complain=*/true);
13653 }
13654 /* If there is no TYPE, an error message will already have
13655 been issued. */
13656 else if (!type || type == error_mark_node)
13657 ;
13658 /* An anonymous aggregate has to be handled specially; such
13659 a declaration really declares a data member (with a
13660 particular type), as opposed to a nested class. */
13661 else if (ANON_AGGR_TYPE_P (type))
13662 {
13663 /* Remove constructors and such from TYPE, now that we
13664 know it is an anonymous aggregate. */
13665 fixup_anonymous_aggr (type);
13666 /* And make the corresponding data member. */
13667 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13668 /* Add it to the class. */
13669 finish_member_declaration (decl);
13670 }
13671 else
13672 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13673 }
13674 }
13675 else
13676 {
13677 /* See if these declarations will be friends. */
13678 friend_p = cp_parser_friend_p (&decl_specifiers);
13679
13680 /* Keep going until we hit the `;' at the end of the
13681 declaration. */
13682 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13683 {
13684 tree attributes = NULL_TREE;
13685 tree first_attribute;
13686
13687 /* Peek at the next token. */
13688 token = cp_lexer_peek_token (parser->lexer);
13689
13690 /* Check for a bitfield declaration. */
13691 if (token->type == CPP_COLON
13692 || (token->type == CPP_NAME
13693 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13694 == CPP_COLON))
13695 {
13696 tree identifier;
13697 tree width;
13698
13699 /* Get the name of the bitfield. Note that we cannot just
13700 check TOKEN here because it may have been invalidated by
13701 the call to cp_lexer_peek_nth_token above. */
13702 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13703 identifier = cp_parser_identifier (parser);
13704 else
13705 identifier = NULL_TREE;
13706
13707 /* Consume the `:' token. */
13708 cp_lexer_consume_token (parser->lexer);
13709 /* Get the width of the bitfield. */
13710 width
13711 = cp_parser_constant_expression (parser,
13712 /*allow_non_constant=*/false,
13713 NULL);
13714
13715 /* Look for attributes that apply to the bitfield. */
13716 attributes = cp_parser_attributes_opt (parser);
13717 /* Remember which attributes are prefix attributes and
13718 which are not. */
13719 first_attribute = attributes;
13720 /* Combine the attributes. */
13721 attributes = chainon (prefix_attributes, attributes);
13722
13723 /* Create the bitfield declaration. */
13724 decl = grokbitfield (identifier
13725 ? make_id_declarator (NULL_TREE,
13726 identifier,
13727 sfk_none)
13728 : NULL,
13729 &decl_specifiers,
13730 width);
13731 /* Apply the attributes. */
13732 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13733 }
13734 else
13735 {
13736 cp_declarator *declarator;
13737 tree initializer;
13738 tree asm_specification;
13739 int ctor_dtor_or_conv_p;
13740
13741 /* Parse the declarator. */
13742 declarator
13743 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13744 &ctor_dtor_or_conv_p,
13745 /*parenthesized_p=*/NULL,
13746 /*member_p=*/true);
13747
13748 /* If something went wrong parsing the declarator, make sure
13749 that we at least consume some tokens. */
13750 if (declarator == cp_error_declarator)
13751 {
13752 /* Skip to the end of the statement. */
13753 cp_parser_skip_to_end_of_statement (parser);
13754 /* If the next token is not a semicolon, that is
13755 probably because we just skipped over the body of
13756 a function. So, we consume a semicolon if
13757 present, but do not issue an error message if it
13758 is not present. */
13759 if (cp_lexer_next_token_is (parser->lexer,
13760 CPP_SEMICOLON))
13761 cp_lexer_consume_token (parser->lexer);
13762 return;
13763 }
13764
13765 if (declares_class_or_enum & 2)
13766 cp_parser_check_for_definition_in_return_type
13767 (declarator, decl_specifiers.type);
13768
13769 /* Look for an asm-specification. */
13770 asm_specification = cp_parser_asm_specification_opt (parser);
13771 /* Look for attributes that apply to the declaration. */
13772 attributes = cp_parser_attributes_opt (parser);
13773 /* Remember which attributes are prefix attributes and
13774 which are not. */
13775 first_attribute = attributes;
13776 /* Combine the attributes. */
13777 attributes = chainon (prefix_attributes, attributes);
13778
13779 /* If it's an `=', then we have a constant-initializer or a
13780 pure-specifier. It is not correct to parse the
13781 initializer before registering the member declaration
13782 since the member declaration should be in scope while
13783 its initializer is processed. However, the rest of the
13784 front end does not yet provide an interface that allows
13785 us to handle this correctly. */
13786 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13787 {
13788 /* In [class.mem]:
13789
13790 A pure-specifier shall be used only in the declaration of
13791 a virtual function.
13792
13793 A member-declarator can contain a constant-initializer
13794 only if it declares a static member of integral or
13795 enumeration type.
13796
13797 Therefore, if the DECLARATOR is for a function, we look
13798 for a pure-specifier; otherwise, we look for a
13799 constant-initializer. When we call `grokfield', it will
13800 perform more stringent semantics checks. */
13801 if (declarator->kind == cdk_function
13802 && declarator->declarator->kind == cdk_id)
13803 initializer = cp_parser_pure_specifier (parser);
13804 else
13805 /* Parse the initializer. */
13806 initializer = cp_parser_constant_initializer (parser);
13807 }
13808 /* Otherwise, there is no initializer. */
13809 else
13810 initializer = NULL_TREE;
13811
13812 /* See if we are probably looking at a function
13813 definition. We are certainly not looking at a
13814 member-declarator. Calling `grokfield' has
13815 side-effects, so we must not do it unless we are sure
13816 that we are looking at a member-declarator. */
13817 if (cp_parser_token_starts_function_definition_p
13818 (cp_lexer_peek_token (parser->lexer)))
13819 {
13820 /* The grammar does not allow a pure-specifier to be
13821 used when a member function is defined. (It is
13822 possible that this fact is an oversight in the
13823 standard, since a pure function may be defined
13824 outside of the class-specifier. */
13825 if (initializer)
13826 error ("pure-specifier on function-definition");
13827 decl = cp_parser_save_member_function_body (parser,
13828 &decl_specifiers,
13829 declarator,
13830 attributes);
13831 /* If the member was not a friend, declare it here. */
13832 if (!friend_p)
13833 finish_member_declaration (decl);
13834 /* Peek at the next token. */
13835 token = cp_lexer_peek_token (parser->lexer);
13836 /* If the next token is a semicolon, consume it. */
13837 if (token->type == CPP_SEMICOLON)
13838 cp_lexer_consume_token (parser->lexer);
13839 return;
13840 }
13841 else
13842 /* Create the declaration. */
13843 decl = grokfield (declarator, &decl_specifiers,
13844 initializer, /*init_const_expr_p=*/true,
13845 asm_specification,
13846 attributes);
13847 }
13848
13849 /* Reset PREFIX_ATTRIBUTES. */
13850 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13851 attributes = TREE_CHAIN (attributes);
13852 if (attributes)
13853 TREE_CHAIN (attributes) = NULL_TREE;
13854
13855 /* If there is any qualification still in effect, clear it
13856 now; we will be starting fresh with the next declarator. */
13857 parser->scope = NULL_TREE;
13858 parser->qualifying_scope = NULL_TREE;
13859 parser->object_scope = NULL_TREE;
13860 /* If it's a `,', then there are more declarators. */
13861 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13862 cp_lexer_consume_token (parser->lexer);
13863 /* If the next token isn't a `;', then we have a parse error. */
13864 else if (cp_lexer_next_token_is_not (parser->lexer,
13865 CPP_SEMICOLON))
13866 {
13867 cp_parser_error (parser, "expected %<;%>");
13868 /* Skip tokens until we find a `;'. */
13869 cp_parser_skip_to_end_of_statement (parser);
13870
13871 break;
13872 }
13873
13874 if (decl)
13875 {
13876 /* Add DECL to the list of members. */
13877 if (!friend_p)
13878 finish_member_declaration (decl);
13879
13880 if (TREE_CODE (decl) == FUNCTION_DECL)
13881 cp_parser_save_default_args (parser, decl);
13882 }
13883 }
13884 }
13885
13886 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13887 }
13888
13889 /* Parse a pure-specifier.
13890
13891 pure-specifier:
13892 = 0
13893
13894 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13895 Otherwise, ERROR_MARK_NODE is returned. */
13896
13897 static tree
13898 cp_parser_pure_specifier (cp_parser* parser)
13899 {
13900 cp_token *token;
13901
13902 /* Look for the `=' token. */
13903 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13904 return error_mark_node;
13905 /* Look for the `0' token. */
13906 token = cp_lexer_consume_token (parser->lexer);
13907 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13908 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13909 {
13910 cp_parser_error (parser,
13911 "invalid pure specifier (only `= 0' is allowed)");
13912 cp_parser_skip_to_end_of_statement (parser);
13913 return error_mark_node;
13914 }
13915 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13916 {
13917 error ("templates may not be %<virtual%>");
13918 return error_mark_node;
13919 }
13920
13921 return integer_zero_node;
13922 }
13923
13924 /* Parse a constant-initializer.
13925
13926 constant-initializer:
13927 = constant-expression
13928
13929 Returns a representation of the constant-expression. */
13930
13931 static tree
13932 cp_parser_constant_initializer (cp_parser* parser)
13933 {
13934 /* Look for the `=' token. */
13935 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13936 return error_mark_node;
13937
13938 /* It is invalid to write:
13939
13940 struct S { static const int i = { 7 }; };
13941
13942 */
13943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13944 {
13945 cp_parser_error (parser,
13946 "a brace-enclosed initializer is not allowed here");
13947 /* Consume the opening brace. */
13948 cp_lexer_consume_token (parser->lexer);
13949 /* Skip the initializer. */
13950 cp_parser_skip_to_closing_brace (parser);
13951 /* Look for the trailing `}'. */
13952 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13953
13954 return error_mark_node;
13955 }
13956
13957 return cp_parser_constant_expression (parser,
13958 /*allow_non_constant=*/false,
13959 NULL);
13960 }
13961
13962 /* Derived classes [gram.class.derived] */
13963
13964 /* Parse a base-clause.
13965
13966 base-clause:
13967 : base-specifier-list
13968
13969 base-specifier-list:
13970 base-specifier
13971 base-specifier-list , base-specifier
13972
13973 Returns a TREE_LIST representing the base-classes, in the order in
13974 which they were declared. The representation of each node is as
13975 described by cp_parser_base_specifier.
13976
13977 In the case that no bases are specified, this function will return
13978 NULL_TREE, not ERROR_MARK_NODE. */
13979
13980 static tree
13981 cp_parser_base_clause (cp_parser* parser)
13982 {
13983 tree bases = NULL_TREE;
13984
13985 /* Look for the `:' that begins the list. */
13986 cp_parser_require (parser, CPP_COLON, "`:'");
13987
13988 /* Scan the base-specifier-list. */
13989 while (true)
13990 {
13991 cp_token *token;
13992 tree base;
13993
13994 /* Look for the base-specifier. */
13995 base = cp_parser_base_specifier (parser);
13996 /* Add BASE to the front of the list. */
13997 if (base != error_mark_node)
13998 {
13999 TREE_CHAIN (base) = bases;
14000 bases = base;
14001 }
14002 /* Peek at the next token. */
14003 token = cp_lexer_peek_token (parser->lexer);
14004 /* If it's not a comma, then the list is complete. */
14005 if (token->type != CPP_COMMA)
14006 break;
14007 /* Consume the `,'. */
14008 cp_lexer_consume_token (parser->lexer);
14009 }
14010
14011 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14012 base class had a qualified name. However, the next name that
14013 appears is certainly not qualified. */
14014 parser->scope = NULL_TREE;
14015 parser->qualifying_scope = NULL_TREE;
14016 parser->object_scope = NULL_TREE;
14017
14018 return nreverse (bases);
14019 }
14020
14021 /* Parse a base-specifier.
14022
14023 base-specifier:
14024 :: [opt] nested-name-specifier [opt] class-name
14025 virtual access-specifier [opt] :: [opt] nested-name-specifier
14026 [opt] class-name
14027 access-specifier virtual [opt] :: [opt] nested-name-specifier
14028 [opt] class-name
14029
14030 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14031 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14032 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14033 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14034
14035 static tree
14036 cp_parser_base_specifier (cp_parser* parser)
14037 {
14038 cp_token *token;
14039 bool done = false;
14040 bool virtual_p = false;
14041 bool duplicate_virtual_error_issued_p = false;
14042 bool duplicate_access_error_issued_p = false;
14043 bool class_scope_p, template_p;
14044 tree access = access_default_node;
14045 tree type;
14046
14047 /* Process the optional `virtual' and `access-specifier'. */
14048 while (!done)
14049 {
14050 /* Peek at the next token. */
14051 token = cp_lexer_peek_token (parser->lexer);
14052 /* Process `virtual'. */
14053 switch (token->keyword)
14054 {
14055 case RID_VIRTUAL:
14056 /* If `virtual' appears more than once, issue an error. */
14057 if (virtual_p && !duplicate_virtual_error_issued_p)
14058 {
14059 cp_parser_error (parser,
14060 "%<virtual%> specified more than once in base-specified");
14061 duplicate_virtual_error_issued_p = true;
14062 }
14063
14064 virtual_p = true;
14065
14066 /* Consume the `virtual' token. */
14067 cp_lexer_consume_token (parser->lexer);
14068
14069 break;
14070
14071 case RID_PUBLIC:
14072 case RID_PROTECTED:
14073 case RID_PRIVATE:
14074 /* If more than one access specifier appears, issue an
14075 error. */
14076 if (access != access_default_node
14077 && !duplicate_access_error_issued_p)
14078 {
14079 cp_parser_error (parser,
14080 "more than one access specifier in base-specified");
14081 duplicate_access_error_issued_p = true;
14082 }
14083
14084 access = ridpointers[(int) token->keyword];
14085
14086 /* Consume the access-specifier. */
14087 cp_lexer_consume_token (parser->lexer);
14088
14089 break;
14090
14091 default:
14092 done = true;
14093 break;
14094 }
14095 }
14096 /* It is not uncommon to see programs mechanically, erroneously, use
14097 the 'typename' keyword to denote (dependent) qualified types
14098 as base classes. */
14099 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14100 {
14101 if (!processing_template_decl)
14102 error ("keyword %<typename%> not allowed outside of templates");
14103 else
14104 error ("keyword %<typename%> not allowed in this context "
14105 "(the base class is implicitly a type)");
14106 cp_lexer_consume_token (parser->lexer);
14107 }
14108
14109 /* Look for the optional `::' operator. */
14110 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14111 /* Look for the nested-name-specifier. The simplest way to
14112 implement:
14113
14114 [temp.res]
14115
14116 The keyword `typename' is not permitted in a base-specifier or
14117 mem-initializer; in these contexts a qualified name that
14118 depends on a template-parameter is implicitly assumed to be a
14119 type name.
14120
14121 is to pretend that we have seen the `typename' keyword at this
14122 point. */
14123 cp_parser_nested_name_specifier_opt (parser,
14124 /*typename_keyword_p=*/true,
14125 /*check_dependency_p=*/true,
14126 typename_type,
14127 /*is_declaration=*/true);
14128 /* If the base class is given by a qualified name, assume that names
14129 we see are type names or templates, as appropriate. */
14130 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14131 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14132
14133 /* Finally, look for the class-name. */
14134 type = cp_parser_class_name (parser,
14135 class_scope_p,
14136 template_p,
14137 typename_type,
14138 /*check_dependency_p=*/true,
14139 /*class_head_p=*/false,
14140 /*is_declaration=*/true);
14141
14142 if (type == error_mark_node)
14143 return error_mark_node;
14144
14145 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14146 }
14147
14148 /* Exception handling [gram.exception] */
14149
14150 /* Parse an (optional) exception-specification.
14151
14152 exception-specification:
14153 throw ( type-id-list [opt] )
14154
14155 Returns a TREE_LIST representing the exception-specification. The
14156 TREE_VALUE of each node is a type. */
14157
14158 static tree
14159 cp_parser_exception_specification_opt (cp_parser* parser)
14160 {
14161 cp_token *token;
14162 tree type_id_list;
14163
14164 /* Peek at the next token. */
14165 token = cp_lexer_peek_token (parser->lexer);
14166 /* If it's not `throw', then there's no exception-specification. */
14167 if (!cp_parser_is_keyword (token, RID_THROW))
14168 return NULL_TREE;
14169
14170 /* Consume the `throw'. */
14171 cp_lexer_consume_token (parser->lexer);
14172
14173 /* Look for the `('. */
14174 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14175
14176 /* Peek at the next token. */
14177 token = cp_lexer_peek_token (parser->lexer);
14178 /* If it's not a `)', then there is a type-id-list. */
14179 if (token->type != CPP_CLOSE_PAREN)
14180 {
14181 const char *saved_message;
14182
14183 /* Types may not be defined in an exception-specification. */
14184 saved_message = parser->type_definition_forbidden_message;
14185 parser->type_definition_forbidden_message
14186 = "types may not be defined in an exception-specification";
14187 /* Parse the type-id-list. */
14188 type_id_list = cp_parser_type_id_list (parser);
14189 /* Restore the saved message. */
14190 parser->type_definition_forbidden_message = saved_message;
14191 }
14192 else
14193 type_id_list = empty_except_spec;
14194
14195 /* Look for the `)'. */
14196 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14197
14198 return type_id_list;
14199 }
14200
14201 /* Parse an (optional) type-id-list.
14202
14203 type-id-list:
14204 type-id
14205 type-id-list , type-id
14206
14207 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14208 in the order that the types were presented. */
14209
14210 static tree
14211 cp_parser_type_id_list (cp_parser* parser)
14212 {
14213 tree types = NULL_TREE;
14214
14215 while (true)
14216 {
14217 cp_token *token;
14218 tree type;
14219
14220 /* Get the next type-id. */
14221 type = cp_parser_type_id (parser);
14222 /* Add it to the list. */
14223 types = add_exception_specifier (types, type, /*complain=*/1);
14224 /* Peek at the next token. */
14225 token = cp_lexer_peek_token (parser->lexer);
14226 /* If it is not a `,', we are done. */
14227 if (token->type != CPP_COMMA)
14228 break;
14229 /* Consume the `,'. */
14230 cp_lexer_consume_token (parser->lexer);
14231 }
14232
14233 return nreverse (types);
14234 }
14235
14236 /* Parse a try-block.
14237
14238 try-block:
14239 try compound-statement handler-seq */
14240
14241 static tree
14242 cp_parser_try_block (cp_parser* parser)
14243 {
14244 tree try_block;
14245
14246 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14247 try_block = begin_try_block ();
14248 cp_parser_compound_statement (parser, NULL, true);
14249 finish_try_block (try_block);
14250 cp_parser_handler_seq (parser);
14251 finish_handler_sequence (try_block);
14252
14253 return try_block;
14254 }
14255
14256 /* Parse a function-try-block.
14257
14258 function-try-block:
14259 try ctor-initializer [opt] function-body handler-seq */
14260
14261 static bool
14262 cp_parser_function_try_block (cp_parser* parser)
14263 {
14264 tree compound_stmt;
14265 tree try_block;
14266 bool ctor_initializer_p;
14267
14268 /* Look for the `try' keyword. */
14269 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14270 return false;
14271 /* Let the rest of the front-end know where we are. */
14272 try_block = begin_function_try_block (&compound_stmt);
14273 /* Parse the function-body. */
14274 ctor_initializer_p
14275 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14276 /* We're done with the `try' part. */
14277 finish_function_try_block (try_block);
14278 /* Parse the handlers. */
14279 cp_parser_handler_seq (parser);
14280 /* We're done with the handlers. */
14281 finish_function_handler_sequence (try_block, compound_stmt);
14282
14283 return ctor_initializer_p;
14284 }
14285
14286 /* Parse a handler-seq.
14287
14288 handler-seq:
14289 handler handler-seq [opt] */
14290
14291 static void
14292 cp_parser_handler_seq (cp_parser* parser)
14293 {
14294 while (true)
14295 {
14296 cp_token *token;
14297
14298 /* Parse the handler. */
14299 cp_parser_handler (parser);
14300 /* Peek at the next token. */
14301 token = cp_lexer_peek_token (parser->lexer);
14302 /* If it's not `catch' then there are no more handlers. */
14303 if (!cp_parser_is_keyword (token, RID_CATCH))
14304 break;
14305 }
14306 }
14307
14308 /* Parse a handler.
14309
14310 handler:
14311 catch ( exception-declaration ) compound-statement */
14312
14313 static void
14314 cp_parser_handler (cp_parser* parser)
14315 {
14316 tree handler;
14317 tree declaration;
14318
14319 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14320 handler = begin_handler ();
14321 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14322 declaration = cp_parser_exception_declaration (parser);
14323 finish_handler_parms (declaration, handler);
14324 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14325 cp_parser_compound_statement (parser, NULL, false);
14326 finish_handler (handler);
14327 }
14328
14329 /* Parse an exception-declaration.
14330
14331 exception-declaration:
14332 type-specifier-seq declarator
14333 type-specifier-seq abstract-declarator
14334 type-specifier-seq
14335 ...
14336
14337 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14338 ellipsis variant is used. */
14339
14340 static tree
14341 cp_parser_exception_declaration (cp_parser* parser)
14342 {
14343 tree decl;
14344 cp_decl_specifier_seq type_specifiers;
14345 cp_declarator *declarator;
14346 const char *saved_message;
14347
14348 /* If it's an ellipsis, it's easy to handle. */
14349 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14350 {
14351 /* Consume the `...' token. */
14352 cp_lexer_consume_token (parser->lexer);
14353 return NULL_TREE;
14354 }
14355
14356 /* Types may not be defined in exception-declarations. */
14357 saved_message = parser->type_definition_forbidden_message;
14358 parser->type_definition_forbidden_message
14359 = "types may not be defined in exception-declarations";
14360
14361 /* Parse the type-specifier-seq. */
14362 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14363 &type_specifiers);
14364 /* If it's a `)', then there is no declarator. */
14365 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14366 declarator = NULL;
14367 else
14368 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14369 /*ctor_dtor_or_conv_p=*/NULL,
14370 /*parenthesized_p=*/NULL,
14371 /*member_p=*/false);
14372
14373 /* Restore the saved message. */
14374 parser->type_definition_forbidden_message = saved_message;
14375
14376 if (type_specifiers.any_specifiers_p)
14377 {
14378 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14379 if (decl == NULL_TREE)
14380 error ("invalid catch parameter");
14381 }
14382 else
14383 decl = NULL_TREE;
14384
14385 return decl;
14386 }
14387
14388 /* Parse a throw-expression.
14389
14390 throw-expression:
14391 throw assignment-expression [opt]
14392
14393 Returns a THROW_EXPR representing the throw-expression. */
14394
14395 static tree
14396 cp_parser_throw_expression (cp_parser* parser)
14397 {
14398 tree expression;
14399 cp_token* token;
14400
14401 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14402 token = cp_lexer_peek_token (parser->lexer);
14403 /* Figure out whether or not there is an assignment-expression
14404 following the "throw" keyword. */
14405 if (token->type == CPP_COMMA
14406 || token->type == CPP_SEMICOLON
14407 || token->type == CPP_CLOSE_PAREN
14408 || token->type == CPP_CLOSE_SQUARE
14409 || token->type == CPP_CLOSE_BRACE
14410 || token->type == CPP_COLON)
14411 expression = NULL_TREE;
14412 else
14413 expression = cp_parser_assignment_expression (parser,
14414 /*cast_p=*/false);
14415
14416 return build_throw (expression);
14417 }
14418
14419 /* GNU Extensions */
14420
14421 /* Parse an (optional) asm-specification.
14422
14423 asm-specification:
14424 asm ( string-literal )
14425
14426 If the asm-specification is present, returns a STRING_CST
14427 corresponding to the string-literal. Otherwise, returns
14428 NULL_TREE. */
14429
14430 static tree
14431 cp_parser_asm_specification_opt (cp_parser* parser)
14432 {
14433 cp_token *token;
14434 tree asm_specification;
14435
14436 /* Peek at the next token. */
14437 token = cp_lexer_peek_token (parser->lexer);
14438 /* If the next token isn't the `asm' keyword, then there's no
14439 asm-specification. */
14440 if (!cp_parser_is_keyword (token, RID_ASM))
14441 return NULL_TREE;
14442
14443 /* Consume the `asm' token. */
14444 cp_lexer_consume_token (parser->lexer);
14445 /* Look for the `('. */
14446 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14447
14448 /* Look for the string-literal. */
14449 asm_specification = cp_parser_string_literal (parser, false, false);
14450
14451 /* Look for the `)'. */
14452 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14453
14454 return asm_specification;
14455 }
14456
14457 /* Parse an asm-operand-list.
14458
14459 asm-operand-list:
14460 asm-operand
14461 asm-operand-list , asm-operand
14462
14463 asm-operand:
14464 string-literal ( expression )
14465 [ string-literal ] string-literal ( expression )
14466
14467 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14468 each node is the expression. The TREE_PURPOSE is itself a
14469 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14470 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14471 is a STRING_CST for the string literal before the parenthesis. */
14472
14473 static tree
14474 cp_parser_asm_operand_list (cp_parser* parser)
14475 {
14476 tree asm_operands = NULL_TREE;
14477
14478 while (true)
14479 {
14480 tree string_literal;
14481 tree expression;
14482 tree name;
14483
14484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14485 {
14486 /* Consume the `[' token. */
14487 cp_lexer_consume_token (parser->lexer);
14488 /* Read the operand name. */
14489 name = cp_parser_identifier (parser);
14490 if (name != error_mark_node)
14491 name = build_string (IDENTIFIER_LENGTH (name),
14492 IDENTIFIER_POINTER (name));
14493 /* Look for the closing `]'. */
14494 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14495 }
14496 else
14497 name = NULL_TREE;
14498 /* Look for the string-literal. */
14499 string_literal = cp_parser_string_literal (parser, false, false);
14500
14501 /* Look for the `('. */
14502 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14503 /* Parse the expression. */
14504 expression = cp_parser_expression (parser, /*cast_p=*/false);
14505 /* Look for the `)'. */
14506 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14507
14508 /* Add this operand to the list. */
14509 asm_operands = tree_cons (build_tree_list (name, string_literal),
14510 expression,
14511 asm_operands);
14512 /* If the next token is not a `,', there are no more
14513 operands. */
14514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14515 break;
14516 /* Consume the `,'. */
14517 cp_lexer_consume_token (parser->lexer);
14518 }
14519
14520 return nreverse (asm_operands);
14521 }
14522
14523 /* Parse an asm-clobber-list.
14524
14525 asm-clobber-list:
14526 string-literal
14527 asm-clobber-list , string-literal
14528
14529 Returns a TREE_LIST, indicating the clobbers in the order that they
14530 appeared. The TREE_VALUE of each node is a STRING_CST. */
14531
14532 static tree
14533 cp_parser_asm_clobber_list (cp_parser* parser)
14534 {
14535 tree clobbers = NULL_TREE;
14536
14537 while (true)
14538 {
14539 tree string_literal;
14540
14541 /* Look for the string literal. */
14542 string_literal = cp_parser_string_literal (parser, false, false);
14543 /* Add it to the list. */
14544 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14545 /* If the next token is not a `,', then the list is
14546 complete. */
14547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14548 break;
14549 /* Consume the `,' token. */
14550 cp_lexer_consume_token (parser->lexer);
14551 }
14552
14553 return clobbers;
14554 }
14555
14556 /* Parse an (optional) series of attributes.
14557
14558 attributes:
14559 attributes attribute
14560
14561 attribute:
14562 __attribute__ (( attribute-list [opt] ))
14563
14564 The return value is as for cp_parser_attribute_list. */
14565
14566 static tree
14567 cp_parser_attributes_opt (cp_parser* parser)
14568 {
14569 tree attributes = NULL_TREE;
14570
14571 while (true)
14572 {
14573 cp_token *token;
14574 tree attribute_list;
14575
14576 /* Peek at the next token. */
14577 token = cp_lexer_peek_token (parser->lexer);
14578 /* If it's not `__attribute__', then we're done. */
14579 if (token->keyword != RID_ATTRIBUTE)
14580 break;
14581
14582 /* Consume the `__attribute__' keyword. */
14583 cp_lexer_consume_token (parser->lexer);
14584 /* Look for the two `(' tokens. */
14585 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14586 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14587
14588 /* Peek at the next token. */
14589 token = cp_lexer_peek_token (parser->lexer);
14590 if (token->type != CPP_CLOSE_PAREN)
14591 /* Parse the attribute-list. */
14592 attribute_list = cp_parser_attribute_list (parser);
14593 else
14594 /* If the next token is a `)', then there is no attribute
14595 list. */
14596 attribute_list = NULL;
14597
14598 /* Look for the two `)' tokens. */
14599 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14600 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14601
14602 /* Add these new attributes to the list. */
14603 attributes = chainon (attributes, attribute_list);
14604 }
14605
14606 return attributes;
14607 }
14608
14609 /* Parse an attribute-list.
14610
14611 attribute-list:
14612 attribute
14613 attribute-list , attribute
14614
14615 attribute:
14616 identifier
14617 identifier ( identifier )
14618 identifier ( identifier , expression-list )
14619 identifier ( expression-list )
14620
14621 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14622 to an attribute. The TREE_PURPOSE of each node is the identifier
14623 indicating which attribute is in use. The TREE_VALUE represents
14624 the arguments, if any. */
14625
14626 static tree
14627 cp_parser_attribute_list (cp_parser* parser)
14628 {
14629 tree attribute_list = NULL_TREE;
14630 bool save_translate_strings_p = parser->translate_strings_p;
14631
14632 parser->translate_strings_p = false;
14633 while (true)
14634 {
14635 cp_token *token;
14636 tree identifier;
14637 tree attribute;
14638
14639 /* Look for the identifier. We also allow keywords here; for
14640 example `__attribute__ ((const))' is legal. */
14641 token = cp_lexer_peek_token (parser->lexer);
14642 if (token->type == CPP_NAME
14643 || token->type == CPP_KEYWORD)
14644 {
14645 tree arguments = NULL_TREE;
14646
14647 /* Consume the token. */
14648 token = cp_lexer_consume_token (parser->lexer);
14649
14650 /* Save away the identifier that indicates which attribute
14651 this is. */
14652 identifier = token->value;
14653 attribute = build_tree_list (identifier, NULL_TREE);
14654
14655 /* Peek at the next token. */
14656 token = cp_lexer_peek_token (parser->lexer);
14657 /* If it's an `(', then parse the attribute arguments. */
14658 if (token->type == CPP_OPEN_PAREN)
14659 {
14660 arguments = cp_parser_parenthesized_expression_list
14661 (parser, true, /*cast_p=*/false,
14662 /*non_constant_p=*/NULL);
14663 /* Save the arguments away. */
14664 TREE_VALUE (attribute) = arguments;
14665 }
14666
14667 if (arguments != error_mark_node)
14668 {
14669 /* Add this attribute to the list. */
14670 TREE_CHAIN (attribute) = attribute_list;
14671 attribute_list = attribute;
14672 }
14673
14674 token = cp_lexer_peek_token (parser->lexer);
14675 }
14676 /* Now, look for more attributes. If the next token isn't a
14677 `,', we're done. */
14678 if (token->type != CPP_COMMA)
14679 break;
14680
14681 /* Consume the comma and keep going. */
14682 cp_lexer_consume_token (parser->lexer);
14683 }
14684 parser->translate_strings_p = save_translate_strings_p;
14685
14686 /* We built up the list in reverse order. */
14687 return nreverse (attribute_list);
14688 }
14689
14690 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14691 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14692 current value of the PEDANTIC flag, regardless of whether or not
14693 the `__extension__' keyword is present. The caller is responsible
14694 for restoring the value of the PEDANTIC flag. */
14695
14696 static bool
14697 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14698 {
14699 /* Save the old value of the PEDANTIC flag. */
14700 *saved_pedantic = pedantic;
14701
14702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14703 {
14704 /* Consume the `__extension__' token. */
14705 cp_lexer_consume_token (parser->lexer);
14706 /* We're not being pedantic while the `__extension__' keyword is
14707 in effect. */
14708 pedantic = 0;
14709
14710 return true;
14711 }
14712
14713 return false;
14714 }
14715
14716 /* Parse a label declaration.
14717
14718 label-declaration:
14719 __label__ label-declarator-seq ;
14720
14721 label-declarator-seq:
14722 identifier , label-declarator-seq
14723 identifier */
14724
14725 static void
14726 cp_parser_label_declaration (cp_parser* parser)
14727 {
14728 /* Look for the `__label__' keyword. */
14729 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14730
14731 while (true)
14732 {
14733 tree identifier;
14734
14735 /* Look for an identifier. */
14736 identifier = cp_parser_identifier (parser);
14737 /* If we failed, stop. */
14738 if (identifier == error_mark_node)
14739 break;
14740 /* Declare it as a label. */
14741 finish_label_decl (identifier);
14742 /* If the next token is a `;', stop. */
14743 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14744 break;
14745 /* Look for the `,' separating the label declarations. */
14746 cp_parser_require (parser, CPP_COMMA, "`,'");
14747 }
14748
14749 /* Look for the final `;'. */
14750 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14751 }
14752
14753 /* Support Functions */
14754
14755 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14756 NAME should have one of the representations used for an
14757 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14758 is returned. If PARSER->SCOPE is a dependent type, then a
14759 SCOPE_REF is returned.
14760
14761 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14762 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14763 was formed. Abstractly, such entities should not be passed to this
14764 function, because they do not need to be looked up, but it is
14765 simpler to check for this special case here, rather than at the
14766 call-sites.
14767
14768 In cases not explicitly covered above, this function returns a
14769 DECL, OVERLOAD, or baselink representing the result of the lookup.
14770 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14771 is returned.
14772
14773 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14774 (e.g., "struct") that was used. In that case bindings that do not
14775 refer to types are ignored.
14776
14777 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14778 ignored.
14779
14780 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14781 are ignored.
14782
14783 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14784 types.
14785
14786 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14787 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14788 NULL_TREE otherwise. */
14789
14790 static tree
14791 cp_parser_lookup_name (cp_parser *parser, tree name,
14792 enum tag_types tag_type,
14793 bool is_template,
14794 bool is_namespace,
14795 bool check_dependency,
14796 tree *ambiguous_decls)
14797 {
14798 int flags = 0;
14799 tree decl;
14800 tree object_type = parser->context->object_type;
14801
14802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14803 flags |= LOOKUP_COMPLAIN;
14804
14805 /* Assume that the lookup will be unambiguous. */
14806 if (ambiguous_decls)
14807 *ambiguous_decls = NULL_TREE;
14808
14809 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14810 no longer valid. Note that if we are parsing tentatively, and
14811 the parse fails, OBJECT_TYPE will be automatically restored. */
14812 parser->context->object_type = NULL_TREE;
14813
14814 if (name == error_mark_node)
14815 return error_mark_node;
14816
14817 /* A template-id has already been resolved; there is no lookup to
14818 do. */
14819 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14820 return name;
14821 if (BASELINK_P (name))
14822 {
14823 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14824 == TEMPLATE_ID_EXPR);
14825 return name;
14826 }
14827
14828 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14829 it should already have been checked to make sure that the name
14830 used matches the type being destroyed. */
14831 if (TREE_CODE (name) == BIT_NOT_EXPR)
14832 {
14833 tree type;
14834
14835 /* Figure out to which type this destructor applies. */
14836 if (parser->scope)
14837 type = parser->scope;
14838 else if (object_type)
14839 type = object_type;
14840 else
14841 type = current_class_type;
14842 /* If that's not a class type, there is no destructor. */
14843 if (!type || !CLASS_TYPE_P (type))
14844 return error_mark_node;
14845 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14846 lazily_declare_fn (sfk_destructor, type);
14847 if (!CLASSTYPE_DESTRUCTORS (type))
14848 return error_mark_node;
14849 /* If it was a class type, return the destructor. */
14850 return CLASSTYPE_DESTRUCTORS (type);
14851 }
14852
14853 /* By this point, the NAME should be an ordinary identifier. If
14854 the id-expression was a qualified name, the qualifying scope is
14855 stored in PARSER->SCOPE at this point. */
14856 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14857
14858 /* Perform the lookup. */
14859 if (parser->scope)
14860 {
14861 bool dependent_p;
14862
14863 if (parser->scope == error_mark_node)
14864 return error_mark_node;
14865
14866 /* If the SCOPE is dependent, the lookup must be deferred until
14867 the template is instantiated -- unless we are explicitly
14868 looking up names in uninstantiated templates. Even then, we
14869 cannot look up the name if the scope is not a class type; it
14870 might, for example, be a template type parameter. */
14871 dependent_p = (TYPE_P (parser->scope)
14872 && !(parser->in_declarator_p
14873 && currently_open_class (parser->scope))
14874 && dependent_type_p (parser->scope));
14875 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14876 && dependent_p)
14877 {
14878 if (tag_type)
14879 {
14880 tree type;
14881
14882 /* The resolution to Core Issue 180 says that `struct
14883 A::B' should be considered a type-name, even if `A'
14884 is dependent. */
14885 type = make_typename_type (parser->scope, name, tag_type,
14886 /*complain=*/tf_error);
14887 decl = TYPE_NAME (type);
14888 }
14889 else if (is_template
14890 && (cp_parser_next_token_ends_template_argument_p (parser)
14891 || cp_lexer_next_token_is (parser->lexer,
14892 CPP_CLOSE_PAREN)))
14893 decl = make_unbound_class_template (parser->scope,
14894 name, NULL_TREE,
14895 /*complain=*/tf_error);
14896 else
14897 decl = build_qualified_name (/*type=*/NULL_TREE,
14898 parser->scope, name,
14899 is_template);
14900 }
14901 else
14902 {
14903 tree pushed_scope = NULL_TREE;
14904
14905 /* If PARSER->SCOPE is a dependent type, then it must be a
14906 class type, and we must not be checking dependencies;
14907 otherwise, we would have processed this lookup above. So
14908 that PARSER->SCOPE is not considered a dependent base by
14909 lookup_member, we must enter the scope here. */
14910 if (dependent_p)
14911 pushed_scope = push_scope (parser->scope);
14912 /* If the PARSER->SCOPE is a template specialization, it
14913 may be instantiated during name lookup. In that case,
14914 errors may be issued. Even if we rollback the current
14915 tentative parse, those errors are valid. */
14916 decl = lookup_qualified_name (parser->scope, name,
14917 tag_type != none_type,
14918 /*complain=*/true);
14919 if (pushed_scope)
14920 pop_scope (pushed_scope);
14921 }
14922 parser->qualifying_scope = parser->scope;
14923 parser->object_scope = NULL_TREE;
14924 }
14925 else if (object_type)
14926 {
14927 tree object_decl = NULL_TREE;
14928 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14929 OBJECT_TYPE is not a class. */
14930 if (CLASS_TYPE_P (object_type))
14931 /* If the OBJECT_TYPE is a template specialization, it may
14932 be instantiated during name lookup. In that case, errors
14933 may be issued. Even if we rollback the current tentative
14934 parse, those errors are valid. */
14935 object_decl = lookup_member (object_type,
14936 name,
14937 /*protect=*/0,
14938 tag_type != none_type);
14939 /* Look it up in the enclosing context, too. */
14940 decl = lookup_name_real (name, tag_type != none_type,
14941 /*nonclass=*/0,
14942 /*block_p=*/true, is_namespace, flags);
14943 parser->object_scope = object_type;
14944 parser->qualifying_scope = NULL_TREE;
14945 if (object_decl)
14946 decl = object_decl;
14947 }
14948 else
14949 {
14950 decl = lookup_name_real (name, tag_type != none_type,
14951 /*nonclass=*/0,
14952 /*block_p=*/true, is_namespace, flags);
14953 parser->qualifying_scope = NULL_TREE;
14954 parser->object_scope = NULL_TREE;
14955 }
14956
14957 /* If the lookup failed, let our caller know. */
14958 if (!decl || decl == error_mark_node)
14959 return error_mark_node;
14960
14961 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14962 if (TREE_CODE (decl) == TREE_LIST)
14963 {
14964 if (ambiguous_decls)
14965 *ambiguous_decls = decl;
14966 /* The error message we have to print is too complicated for
14967 cp_parser_error, so we incorporate its actions directly. */
14968 if (!cp_parser_simulate_error (parser))
14969 {
14970 error ("reference to %qD is ambiguous", name);
14971 print_candidates (decl);
14972 }
14973 return error_mark_node;
14974 }
14975
14976 gcc_assert (DECL_P (decl)
14977 || TREE_CODE (decl) == OVERLOAD
14978 || TREE_CODE (decl) == SCOPE_REF
14979 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14980 || BASELINK_P (decl));
14981
14982 /* If we have resolved the name of a member declaration, check to
14983 see if the declaration is accessible. When the name resolves to
14984 set of overloaded functions, accessibility is checked when
14985 overload resolution is done.
14986
14987 During an explicit instantiation, access is not checked at all,
14988 as per [temp.explicit]. */
14989 if (DECL_P (decl))
14990 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14991
14992 return decl;
14993 }
14994
14995 /* Like cp_parser_lookup_name, but for use in the typical case where
14996 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14997 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14998
14999 static tree
15000 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15001 {
15002 return cp_parser_lookup_name (parser, name,
15003 none_type,
15004 /*is_template=*/false,
15005 /*is_namespace=*/false,
15006 /*check_dependency=*/true,
15007 /*ambiguous_decls=*/NULL);
15008 }
15009
15010 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15011 the current context, return the TYPE_DECL. If TAG_NAME_P is
15012 true, the DECL indicates the class being defined in a class-head,
15013 or declared in an elaborated-type-specifier.
15014
15015 Otherwise, return DECL. */
15016
15017 static tree
15018 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15019 {
15020 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15021 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15022
15023 struct A {
15024 template <typename T> struct B;
15025 };
15026
15027 template <typename T> struct A::B {};
15028
15029 Similarly, in an elaborated-type-specifier:
15030
15031 namespace N { struct X{}; }
15032
15033 struct A {
15034 template <typename T> friend struct N::X;
15035 };
15036
15037 However, if the DECL refers to a class type, and we are in
15038 the scope of the class, then the name lookup automatically
15039 finds the TYPE_DECL created by build_self_reference rather
15040 than a TEMPLATE_DECL. For example, in:
15041
15042 template <class T> struct S {
15043 S s;
15044 };
15045
15046 there is no need to handle such case. */
15047
15048 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15049 return DECL_TEMPLATE_RESULT (decl);
15050
15051 return decl;
15052 }
15053
15054 /* If too many, or too few, template-parameter lists apply to the
15055 declarator, issue an error message. Returns TRUE if all went well,
15056 and FALSE otherwise. */
15057
15058 static bool
15059 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15060 cp_declarator *declarator)
15061 {
15062 unsigned num_templates;
15063
15064 /* We haven't seen any classes that involve template parameters yet. */
15065 num_templates = 0;
15066
15067 switch (declarator->kind)
15068 {
15069 case cdk_id:
15070 if (declarator->u.id.qualifying_scope)
15071 {
15072 tree scope;
15073 tree member;
15074
15075 scope = declarator->u.id.qualifying_scope;
15076 member = declarator->u.id.unqualified_name;
15077
15078 while (scope && CLASS_TYPE_P (scope))
15079 {
15080 /* You're supposed to have one `template <...>'
15081 for every template class, but you don't need one
15082 for a full specialization. For example:
15083
15084 template <class T> struct S{};
15085 template <> struct S<int> { void f(); };
15086 void S<int>::f () {}
15087
15088 is correct; there shouldn't be a `template <>' for
15089 the definition of `S<int>::f'. */
15090 if (CLASSTYPE_TEMPLATE_INFO (scope)
15091 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15092 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15093 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15094 ++num_templates;
15095
15096 scope = TYPE_CONTEXT (scope);
15097 }
15098 }
15099 else if (TREE_CODE (declarator->u.id.unqualified_name)
15100 == TEMPLATE_ID_EXPR)
15101 /* If the DECLARATOR has the form `X<y>' then it uses one
15102 additional level of template parameters. */
15103 ++num_templates;
15104
15105 return cp_parser_check_template_parameters (parser,
15106 num_templates);
15107
15108 case cdk_function:
15109 case cdk_array:
15110 case cdk_pointer:
15111 case cdk_reference:
15112 case cdk_ptrmem:
15113 return (cp_parser_check_declarator_template_parameters
15114 (parser, declarator->declarator));
15115
15116 case cdk_error:
15117 return true;
15118
15119 default:
15120 gcc_unreachable ();
15121 }
15122 return false;
15123 }
15124
15125 /* NUM_TEMPLATES were used in the current declaration. If that is
15126 invalid, return FALSE and issue an error messages. Otherwise,
15127 return TRUE. */
15128
15129 static bool
15130 cp_parser_check_template_parameters (cp_parser* parser,
15131 unsigned num_templates)
15132 {
15133 /* If there are more template classes than parameter lists, we have
15134 something like:
15135
15136 template <class T> void S<T>::R<T>::f (); */
15137 if (parser->num_template_parameter_lists < num_templates)
15138 {
15139 error ("too few template-parameter-lists");
15140 return false;
15141 }
15142 /* If there are the same number of template classes and parameter
15143 lists, that's OK. */
15144 if (parser->num_template_parameter_lists == num_templates)
15145 return true;
15146 /* If there are more, but only one more, then we are referring to a
15147 member template. That's OK too. */
15148 if (parser->num_template_parameter_lists == num_templates + 1)
15149 return true;
15150 /* Otherwise, there are too many template parameter lists. We have
15151 something like:
15152
15153 template <class T> template <class U> void S::f(); */
15154 error ("too many template-parameter-lists");
15155 return false;
15156 }
15157
15158 /* Parse an optional `::' token indicating that the following name is
15159 from the global namespace. If so, PARSER->SCOPE is set to the
15160 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15161 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15162 Returns the new value of PARSER->SCOPE, if the `::' token is
15163 present, and NULL_TREE otherwise. */
15164
15165 static tree
15166 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15167 {
15168 cp_token *token;
15169
15170 /* Peek at the next token. */
15171 token = cp_lexer_peek_token (parser->lexer);
15172 /* If we're looking at a `::' token then we're starting from the
15173 global namespace, not our current location. */
15174 if (token->type == CPP_SCOPE)
15175 {
15176 /* Consume the `::' token. */
15177 cp_lexer_consume_token (parser->lexer);
15178 /* Set the SCOPE so that we know where to start the lookup. */
15179 parser->scope = global_namespace;
15180 parser->qualifying_scope = global_namespace;
15181 parser->object_scope = NULL_TREE;
15182
15183 return parser->scope;
15184 }
15185 else if (!current_scope_valid_p)
15186 {
15187 parser->scope = NULL_TREE;
15188 parser->qualifying_scope = NULL_TREE;
15189 parser->object_scope = NULL_TREE;
15190 }
15191
15192 return NULL_TREE;
15193 }
15194
15195 /* Returns TRUE if the upcoming token sequence is the start of a
15196 constructor declarator. If FRIEND_P is true, the declarator is
15197 preceded by the `friend' specifier. */
15198
15199 static bool
15200 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15201 {
15202 bool constructor_p;
15203 tree type_decl = NULL_TREE;
15204 bool nested_name_p;
15205 cp_token *next_token;
15206
15207 /* The common case is that this is not a constructor declarator, so
15208 try to avoid doing lots of work if at all possible. It's not
15209 valid declare a constructor at function scope. */
15210 if (at_function_scope_p ())
15211 return false;
15212 /* And only certain tokens can begin a constructor declarator. */
15213 next_token = cp_lexer_peek_token (parser->lexer);
15214 if (next_token->type != CPP_NAME
15215 && next_token->type != CPP_SCOPE
15216 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15217 && next_token->type != CPP_TEMPLATE_ID)
15218 return false;
15219
15220 /* Parse tentatively; we are going to roll back all of the tokens
15221 consumed here. */
15222 cp_parser_parse_tentatively (parser);
15223 /* Assume that we are looking at a constructor declarator. */
15224 constructor_p = true;
15225
15226 /* Look for the optional `::' operator. */
15227 cp_parser_global_scope_opt (parser,
15228 /*current_scope_valid_p=*/false);
15229 /* Look for the nested-name-specifier. */
15230 nested_name_p
15231 = (cp_parser_nested_name_specifier_opt (parser,
15232 /*typename_keyword_p=*/false,
15233 /*check_dependency_p=*/false,
15234 /*type_p=*/false,
15235 /*is_declaration=*/false)
15236 != NULL_TREE);
15237 /* Outside of a class-specifier, there must be a
15238 nested-name-specifier. */
15239 if (!nested_name_p &&
15240 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15241 || friend_p))
15242 constructor_p = false;
15243 /* If we still think that this might be a constructor-declarator,
15244 look for a class-name. */
15245 if (constructor_p)
15246 {
15247 /* If we have:
15248
15249 template <typename T> struct S { S(); };
15250 template <typename T> S<T>::S ();
15251
15252 we must recognize that the nested `S' names a class.
15253 Similarly, for:
15254
15255 template <typename T> S<T>::S<T> ();
15256
15257 we must recognize that the nested `S' names a template. */
15258 type_decl = cp_parser_class_name (parser,
15259 /*typename_keyword_p=*/false,
15260 /*template_keyword_p=*/false,
15261 none_type,
15262 /*check_dependency_p=*/false,
15263 /*class_head_p=*/false,
15264 /*is_declaration=*/false);
15265 /* If there was no class-name, then this is not a constructor. */
15266 constructor_p = !cp_parser_error_occurred (parser);
15267 }
15268
15269 /* If we're still considering a constructor, we have to see a `(',
15270 to begin the parameter-declaration-clause, followed by either a
15271 `)', an `...', or a decl-specifier. We need to check for a
15272 type-specifier to avoid being fooled into thinking that:
15273
15274 S::S (f) (int);
15275
15276 is a constructor. (It is actually a function named `f' that
15277 takes one parameter (of type `int') and returns a value of type
15278 `S::S'. */
15279 if (constructor_p
15280 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15281 {
15282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15283 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15284 /* A parameter declaration begins with a decl-specifier,
15285 which is either the "attribute" keyword, a storage class
15286 specifier, or (usually) a type-specifier. */
15287 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15288 && !cp_parser_storage_class_specifier_opt (parser))
15289 {
15290 tree type;
15291 tree pushed_scope = NULL_TREE;
15292 unsigned saved_num_template_parameter_lists;
15293
15294 /* Names appearing in the type-specifier should be looked up
15295 in the scope of the class. */
15296 if (current_class_type)
15297 type = NULL_TREE;
15298 else
15299 {
15300 type = TREE_TYPE (type_decl);
15301 if (TREE_CODE (type) == TYPENAME_TYPE)
15302 {
15303 type = resolve_typename_type (type,
15304 /*only_current_p=*/false);
15305 if (type == error_mark_node)
15306 {
15307 cp_parser_abort_tentative_parse (parser);
15308 return false;
15309 }
15310 }
15311 pushed_scope = push_scope (type);
15312 }
15313
15314 /* Inside the constructor parameter list, surrounding
15315 template-parameter-lists do not apply. */
15316 saved_num_template_parameter_lists
15317 = parser->num_template_parameter_lists;
15318 parser->num_template_parameter_lists = 0;
15319
15320 /* Look for the type-specifier. */
15321 cp_parser_type_specifier (parser,
15322 CP_PARSER_FLAGS_NONE,
15323 /*decl_specs=*/NULL,
15324 /*is_declarator=*/true,
15325 /*declares_class_or_enum=*/NULL,
15326 /*is_cv_qualifier=*/NULL);
15327
15328 parser->num_template_parameter_lists
15329 = saved_num_template_parameter_lists;
15330
15331 /* Leave the scope of the class. */
15332 if (pushed_scope)
15333 pop_scope (pushed_scope);
15334
15335 constructor_p = !cp_parser_error_occurred (parser);
15336 }
15337 }
15338 else
15339 constructor_p = false;
15340 /* We did not really want to consume any tokens. */
15341 cp_parser_abort_tentative_parse (parser);
15342
15343 return constructor_p;
15344 }
15345
15346 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15347 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15348 they must be performed once we are in the scope of the function.
15349
15350 Returns the function defined. */
15351
15352 static tree
15353 cp_parser_function_definition_from_specifiers_and_declarator
15354 (cp_parser* parser,
15355 cp_decl_specifier_seq *decl_specifiers,
15356 tree attributes,
15357 const cp_declarator *declarator)
15358 {
15359 tree fn;
15360 bool success_p;
15361
15362 /* Begin the function-definition. */
15363 success_p = start_function (decl_specifiers, declarator, attributes);
15364
15365 /* The things we're about to see are not directly qualified by any
15366 template headers we've seen thus far. */
15367 reset_specialization ();
15368
15369 /* If there were names looked up in the decl-specifier-seq that we
15370 did not check, check them now. We must wait until we are in the
15371 scope of the function to perform the checks, since the function
15372 might be a friend. */
15373 perform_deferred_access_checks ();
15374
15375 if (!success_p)
15376 {
15377 /* Skip the entire function. */
15378 cp_parser_skip_to_end_of_block_or_statement (parser);
15379 fn = error_mark_node;
15380 }
15381 else
15382 fn = cp_parser_function_definition_after_declarator (parser,
15383 /*inline_p=*/false);
15384
15385 return fn;
15386 }
15387
15388 /* Parse the part of a function-definition that follows the
15389 declarator. INLINE_P is TRUE iff this function is an inline
15390 function defined with a class-specifier.
15391
15392 Returns the function defined. */
15393
15394 static tree
15395 cp_parser_function_definition_after_declarator (cp_parser* parser,
15396 bool inline_p)
15397 {
15398 tree fn;
15399 bool ctor_initializer_p = false;
15400 bool saved_in_unbraced_linkage_specification_p;
15401 unsigned saved_num_template_parameter_lists;
15402
15403 /* If the next token is `return', then the code may be trying to
15404 make use of the "named return value" extension that G++ used to
15405 support. */
15406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15407 {
15408 /* Consume the `return' keyword. */
15409 cp_lexer_consume_token (parser->lexer);
15410 /* Look for the identifier that indicates what value is to be
15411 returned. */
15412 cp_parser_identifier (parser);
15413 /* Issue an error message. */
15414 error ("named return values are no longer supported");
15415 /* Skip tokens until we reach the start of the function body. */
15416 while (true)
15417 {
15418 cp_token *token = cp_lexer_peek_token (parser->lexer);
15419 if (token->type == CPP_OPEN_BRACE
15420 || token->type == CPP_EOF
15421 || token->type == CPP_PRAGMA_EOL)
15422 break;
15423 cp_lexer_consume_token (parser->lexer);
15424 }
15425 }
15426 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15427 anything declared inside `f'. */
15428 saved_in_unbraced_linkage_specification_p
15429 = parser->in_unbraced_linkage_specification_p;
15430 parser->in_unbraced_linkage_specification_p = false;
15431 /* Inside the function, surrounding template-parameter-lists do not
15432 apply. */
15433 saved_num_template_parameter_lists
15434 = parser->num_template_parameter_lists;
15435 parser->num_template_parameter_lists = 0;
15436 /* If the next token is `try', then we are looking at a
15437 function-try-block. */
15438 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15439 ctor_initializer_p = cp_parser_function_try_block (parser);
15440 /* A function-try-block includes the function-body, so we only do
15441 this next part if we're not processing a function-try-block. */
15442 else
15443 ctor_initializer_p
15444 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15445
15446 /* Finish the function. */
15447 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15448 (inline_p ? 2 : 0));
15449 /* Generate code for it, if necessary. */
15450 expand_or_defer_fn (fn);
15451 /* Restore the saved values. */
15452 parser->in_unbraced_linkage_specification_p
15453 = saved_in_unbraced_linkage_specification_p;
15454 parser->num_template_parameter_lists
15455 = saved_num_template_parameter_lists;
15456
15457 return fn;
15458 }
15459
15460 /* Parse a template-declaration, assuming that the `export' (and
15461 `extern') keywords, if present, has already been scanned. MEMBER_P
15462 is as for cp_parser_template_declaration. */
15463
15464 static void
15465 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15466 {
15467 tree decl = NULL_TREE;
15468 tree checks;
15469 tree parameter_list;
15470 bool friend_p = false;
15471 bool need_lang_pop;
15472
15473 /* Look for the `template' keyword. */
15474 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15475 return;
15476
15477 /* And the `<'. */
15478 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15479 return;
15480 /* [temp]
15481
15482 A template ... shall not have C linkage. */
15483 if (current_lang_name == lang_name_c)
15484 {
15485 error ("template with C linkage");
15486 /* Give it C++ linkage to avoid confusing other parts of the
15487 front end. */
15488 push_lang_context (lang_name_cplusplus);
15489 need_lang_pop = true;
15490 }
15491 else
15492 need_lang_pop = false;
15493
15494 /* We cannot perform access checks on the template parameter
15495 declarations until we know what is being declared, just as we
15496 cannot check the decl-specifier list. */
15497 push_deferring_access_checks (dk_deferred);
15498
15499 /* If the next token is `>', then we have an invalid
15500 specialization. Rather than complain about an invalid template
15501 parameter, issue an error message here. */
15502 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15503 {
15504 cp_parser_error (parser, "invalid explicit specialization");
15505 begin_specialization ();
15506 parameter_list = NULL_TREE;
15507 }
15508 else
15509 /* Parse the template parameters. */
15510 parameter_list = cp_parser_template_parameter_list (parser);
15511
15512 /* Get the deferred access checks from the parameter list. These
15513 will be checked once we know what is being declared, as for a
15514 member template the checks must be performed in the scope of the
15515 class containing the member. */
15516 checks = get_deferred_access_checks ();
15517
15518 /* Look for the `>'. */
15519 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15520 /* We just processed one more parameter list. */
15521 ++parser->num_template_parameter_lists;
15522 /* If the next token is `template', there are more template
15523 parameters. */
15524 if (cp_lexer_next_token_is_keyword (parser->lexer,
15525 RID_TEMPLATE))
15526 cp_parser_template_declaration_after_export (parser, member_p);
15527 else
15528 {
15529 /* There are no access checks when parsing a template, as we do not
15530 know if a specialization will be a friend. */
15531 push_deferring_access_checks (dk_no_check);
15532 decl = cp_parser_single_declaration (parser,
15533 checks,
15534 member_p,
15535 &friend_p);
15536 pop_deferring_access_checks ();
15537
15538 /* If this is a member template declaration, let the front
15539 end know. */
15540 if (member_p && !friend_p && decl)
15541 {
15542 if (TREE_CODE (decl) == TYPE_DECL)
15543 cp_parser_check_access_in_redeclaration (decl);
15544
15545 decl = finish_member_template_decl (decl);
15546 }
15547 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15548 make_friend_class (current_class_type, TREE_TYPE (decl),
15549 /*complain=*/true);
15550 }
15551 /* We are done with the current parameter list. */
15552 --parser->num_template_parameter_lists;
15553
15554 pop_deferring_access_checks ();
15555
15556 /* Finish up. */
15557 finish_template_decl (parameter_list);
15558
15559 /* Register member declarations. */
15560 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15561 finish_member_declaration (decl);
15562 /* For the erroneous case of a template with C linkage, we pushed an
15563 implicit C++ linkage scope; exit that scope now. */
15564 if (need_lang_pop)
15565 pop_lang_context ();
15566 /* If DECL is a function template, we must return to parse it later.
15567 (Even though there is no definition, there might be default
15568 arguments that need handling.) */
15569 if (member_p && decl
15570 && (TREE_CODE (decl) == FUNCTION_DECL
15571 || DECL_FUNCTION_TEMPLATE_P (decl)))
15572 TREE_VALUE (parser->unparsed_functions_queues)
15573 = tree_cons (NULL_TREE, decl,
15574 TREE_VALUE (parser->unparsed_functions_queues));
15575 }
15576
15577 /* Perform the deferred access checks from a template-parameter-list.
15578 CHECKS is a TREE_LIST of access checks, as returned by
15579 get_deferred_access_checks. */
15580
15581 static void
15582 cp_parser_perform_template_parameter_access_checks (tree checks)
15583 {
15584 ++processing_template_parmlist;
15585 perform_access_checks (checks);
15586 --processing_template_parmlist;
15587 }
15588
15589 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15590 `function-definition' sequence. MEMBER_P is true, this declaration
15591 appears in a class scope.
15592
15593 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15594 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15595
15596 static tree
15597 cp_parser_single_declaration (cp_parser* parser,
15598 tree checks,
15599 bool member_p,
15600 bool* friend_p)
15601 {
15602 int declares_class_or_enum;
15603 tree decl = NULL_TREE;
15604 cp_decl_specifier_seq decl_specifiers;
15605 bool function_definition_p = false;
15606
15607 /* This function is only used when processing a template
15608 declaration. */
15609 gcc_assert (innermost_scope_kind () == sk_template_parms
15610 || innermost_scope_kind () == sk_template_spec);
15611
15612 /* Defer access checks until we know what is being declared. */
15613 push_deferring_access_checks (dk_deferred);
15614
15615 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15616 alternative. */
15617 cp_parser_decl_specifier_seq (parser,
15618 CP_PARSER_FLAGS_OPTIONAL,
15619 &decl_specifiers,
15620 &declares_class_or_enum);
15621 if (friend_p)
15622 *friend_p = cp_parser_friend_p (&decl_specifiers);
15623
15624 /* There are no template typedefs. */
15625 if (decl_specifiers.specs[(int) ds_typedef])
15626 {
15627 error ("template declaration of %qs", "typedef");
15628 decl = error_mark_node;
15629 }
15630
15631 /* Gather up the access checks that occurred the
15632 decl-specifier-seq. */
15633 stop_deferring_access_checks ();
15634
15635 /* Check for the declaration of a template class. */
15636 if (declares_class_or_enum)
15637 {
15638 if (cp_parser_declares_only_class_p (parser))
15639 {
15640 decl = shadow_tag (&decl_specifiers);
15641
15642 /* In this case:
15643
15644 struct C {
15645 friend template <typename T> struct A<T>::B;
15646 };
15647
15648 A<T>::B will be represented by a TYPENAME_TYPE, and
15649 therefore not recognized by shadow_tag. */
15650 if (friend_p && *friend_p
15651 && !decl
15652 && decl_specifiers.type
15653 && TYPE_P (decl_specifiers.type))
15654 decl = decl_specifiers.type;
15655
15656 if (decl && decl != error_mark_node)
15657 decl = TYPE_NAME (decl);
15658 else
15659 decl = error_mark_node;
15660
15661 /* Perform access checks for template parameters. */
15662 cp_parser_perform_template_parameter_access_checks (checks);
15663 }
15664 }
15665 /* If it's not a template class, try for a template function. If
15666 the next token is a `;', then this declaration does not declare
15667 anything. But, if there were errors in the decl-specifiers, then
15668 the error might well have come from an attempted class-specifier.
15669 In that case, there's no need to warn about a missing declarator. */
15670 if (!decl
15671 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15672 || decl_specifiers.type != error_mark_node))
15673 decl = cp_parser_init_declarator (parser,
15674 &decl_specifiers,
15675 checks,
15676 /*function_definition_allowed_p=*/true,
15677 member_p,
15678 declares_class_or_enum,
15679 &function_definition_p);
15680
15681 pop_deferring_access_checks ();
15682
15683 /* Clear any current qualification; whatever comes next is the start
15684 of something new. */
15685 parser->scope = NULL_TREE;
15686 parser->qualifying_scope = NULL_TREE;
15687 parser->object_scope = NULL_TREE;
15688 /* Look for a trailing `;' after the declaration. */
15689 if (!function_definition_p
15690 && (decl == error_mark_node
15691 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15692 cp_parser_skip_to_end_of_block_or_statement (parser);
15693
15694 return decl;
15695 }
15696
15697 /* Parse a cast-expression that is not the operand of a unary "&". */
15698
15699 static tree
15700 cp_parser_simple_cast_expression (cp_parser *parser)
15701 {
15702 return cp_parser_cast_expression (parser, /*address_p=*/false,
15703 /*cast_p=*/false);
15704 }
15705
15706 /* Parse a functional cast to TYPE. Returns an expression
15707 representing the cast. */
15708
15709 static tree
15710 cp_parser_functional_cast (cp_parser* parser, tree type)
15711 {
15712 tree expression_list;
15713 tree cast;
15714
15715 expression_list
15716 = cp_parser_parenthesized_expression_list (parser, false,
15717 /*cast_p=*/true,
15718 /*non_constant_p=*/NULL);
15719
15720 cast = build_functional_cast (type, expression_list);
15721 /* [expr.const]/1: In an integral constant expression "only type
15722 conversions to integral or enumeration type can be used". */
15723 if (TREE_CODE (type) == TYPE_DECL)
15724 type = TREE_TYPE (type);
15725 if (cast != error_mark_node && !dependent_type_p (type)
15726 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15727 {
15728 if (cp_parser_non_integral_constant_expression
15729 (parser, "a call to a constructor"))
15730 return error_mark_node;
15731 }
15732 return cast;
15733 }
15734
15735 /* Save the tokens that make up the body of a member function defined
15736 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15737 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15738 specifiers applied to the declaration. Returns the FUNCTION_DECL
15739 for the member function. */
15740
15741 static tree
15742 cp_parser_save_member_function_body (cp_parser* parser,
15743 cp_decl_specifier_seq *decl_specifiers,
15744 cp_declarator *declarator,
15745 tree attributes)
15746 {
15747 cp_token *first;
15748 cp_token *last;
15749 tree fn;
15750
15751 /* Create the function-declaration. */
15752 fn = start_method (decl_specifiers, declarator, attributes);
15753 /* If something went badly wrong, bail out now. */
15754 if (fn == error_mark_node)
15755 {
15756 /* If there's a function-body, skip it. */
15757 if (cp_parser_token_starts_function_definition_p
15758 (cp_lexer_peek_token (parser->lexer)))
15759 cp_parser_skip_to_end_of_block_or_statement (parser);
15760 return error_mark_node;
15761 }
15762
15763 /* Remember it, if there default args to post process. */
15764 cp_parser_save_default_args (parser, fn);
15765
15766 /* Save away the tokens that make up the body of the
15767 function. */
15768 first = parser->lexer->next_token;
15769 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15770 /* Handle function try blocks. */
15771 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15772 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15773 last = parser->lexer->next_token;
15774
15775 /* Save away the inline definition; we will process it when the
15776 class is complete. */
15777 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15778 DECL_PENDING_INLINE_P (fn) = 1;
15779
15780 /* We need to know that this was defined in the class, so that
15781 friend templates are handled correctly. */
15782 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15783
15784 /* We're done with the inline definition. */
15785 finish_method (fn);
15786
15787 /* Add FN to the queue of functions to be parsed later. */
15788 TREE_VALUE (parser->unparsed_functions_queues)
15789 = tree_cons (NULL_TREE, fn,
15790 TREE_VALUE (parser->unparsed_functions_queues));
15791
15792 return fn;
15793 }
15794
15795 /* Parse a template-argument-list, as well as the trailing ">" (but
15796 not the opening ">"). See cp_parser_template_argument_list for the
15797 return value. */
15798
15799 static tree
15800 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15801 {
15802 tree arguments;
15803 tree saved_scope;
15804 tree saved_qualifying_scope;
15805 tree saved_object_scope;
15806 bool saved_greater_than_is_operator_p;
15807 bool saved_skip_evaluation;
15808
15809 /* [temp.names]
15810
15811 When parsing a template-id, the first non-nested `>' is taken as
15812 the end of the template-argument-list rather than a greater-than
15813 operator. */
15814 saved_greater_than_is_operator_p
15815 = parser->greater_than_is_operator_p;
15816 parser->greater_than_is_operator_p = false;
15817 /* Parsing the argument list may modify SCOPE, so we save it
15818 here. */
15819 saved_scope = parser->scope;
15820 saved_qualifying_scope = parser->qualifying_scope;
15821 saved_object_scope = parser->object_scope;
15822 /* We need to evaluate the template arguments, even though this
15823 template-id may be nested within a "sizeof". */
15824 saved_skip_evaluation = skip_evaluation;
15825 skip_evaluation = false;
15826 /* Parse the template-argument-list itself. */
15827 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15828 arguments = NULL_TREE;
15829 else
15830 arguments = cp_parser_template_argument_list (parser);
15831 /* Look for the `>' that ends the template-argument-list. If we find
15832 a '>>' instead, it's probably just a typo. */
15833 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15834 {
15835 if (!saved_greater_than_is_operator_p)
15836 {
15837 /* If we're in a nested template argument list, the '>>' has
15838 to be a typo for '> >'. We emit the error message, but we
15839 continue parsing and we push a '>' as next token, so that
15840 the argument list will be parsed correctly. Note that the
15841 global source location is still on the token before the
15842 '>>', so we need to say explicitly where we want it. */
15843 cp_token *token = cp_lexer_peek_token (parser->lexer);
15844 error ("%H%<>>%> should be %<> >%> "
15845 "within a nested template argument list",
15846 &token->location);
15847
15848 /* ??? Proper recovery should terminate two levels of
15849 template argument list here. */
15850 token->type = CPP_GREATER;
15851 }
15852 else
15853 {
15854 /* If this is not a nested template argument list, the '>>'
15855 is a typo for '>'. Emit an error message and continue.
15856 Same deal about the token location, but here we can get it
15857 right by consuming the '>>' before issuing the diagnostic. */
15858 cp_lexer_consume_token (parser->lexer);
15859 error ("spurious %<>>%>, use %<>%> to terminate "
15860 "a template argument list");
15861 }
15862 }
15863 else
15864 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15865 /* The `>' token might be a greater-than operator again now. */
15866 parser->greater_than_is_operator_p
15867 = saved_greater_than_is_operator_p;
15868 /* Restore the SAVED_SCOPE. */
15869 parser->scope = saved_scope;
15870 parser->qualifying_scope = saved_qualifying_scope;
15871 parser->object_scope = saved_object_scope;
15872 skip_evaluation = saved_skip_evaluation;
15873
15874 return arguments;
15875 }
15876
15877 /* MEMBER_FUNCTION is a member function, or a friend. If default
15878 arguments, or the body of the function have not yet been parsed,
15879 parse them now. */
15880
15881 static void
15882 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15883 {
15884 /* If this member is a template, get the underlying
15885 FUNCTION_DECL. */
15886 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15887 member_function = DECL_TEMPLATE_RESULT (member_function);
15888
15889 /* There should not be any class definitions in progress at this
15890 point; the bodies of members are only parsed outside of all class
15891 definitions. */
15892 gcc_assert (parser->num_classes_being_defined == 0);
15893 /* While we're parsing the member functions we might encounter more
15894 classes. We want to handle them right away, but we don't want
15895 them getting mixed up with functions that are currently in the
15896 queue. */
15897 parser->unparsed_functions_queues
15898 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15899
15900 /* Make sure that any template parameters are in scope. */
15901 maybe_begin_member_template_processing (member_function);
15902
15903 /* If the body of the function has not yet been parsed, parse it
15904 now. */
15905 if (DECL_PENDING_INLINE_P (member_function))
15906 {
15907 tree function_scope;
15908 cp_token_cache *tokens;
15909
15910 /* The function is no longer pending; we are processing it. */
15911 tokens = DECL_PENDING_INLINE_INFO (member_function);
15912 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15913 DECL_PENDING_INLINE_P (member_function) = 0;
15914
15915 /* If this is a local class, enter the scope of the containing
15916 function. */
15917 function_scope = current_function_decl;
15918 if (function_scope)
15919 push_function_context_to (function_scope);
15920
15921
15922 /* Push the body of the function onto the lexer stack. */
15923 cp_parser_push_lexer_for_tokens (parser, tokens);
15924
15925 /* Let the front end know that we going to be defining this
15926 function. */
15927 start_preparsed_function (member_function, NULL_TREE,
15928 SF_PRE_PARSED | SF_INCLASS_INLINE);
15929
15930 /* Don't do access checking if it is a templated function. */
15931 if (processing_template_decl)
15932 push_deferring_access_checks (dk_no_check);
15933
15934 /* Now, parse the body of the function. */
15935 cp_parser_function_definition_after_declarator (parser,
15936 /*inline_p=*/true);
15937
15938 if (processing_template_decl)
15939 pop_deferring_access_checks ();
15940
15941 /* Leave the scope of the containing function. */
15942 if (function_scope)
15943 pop_function_context_from (function_scope);
15944 cp_parser_pop_lexer (parser);
15945 }
15946
15947 /* Remove any template parameters from the symbol table. */
15948 maybe_end_member_template_processing ();
15949
15950 /* Restore the queue. */
15951 parser->unparsed_functions_queues
15952 = TREE_CHAIN (parser->unparsed_functions_queues);
15953 }
15954
15955 /* If DECL contains any default args, remember it on the unparsed
15956 functions queue. */
15957
15958 static void
15959 cp_parser_save_default_args (cp_parser* parser, tree decl)
15960 {
15961 tree probe;
15962
15963 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15964 probe;
15965 probe = TREE_CHAIN (probe))
15966 if (TREE_PURPOSE (probe))
15967 {
15968 TREE_PURPOSE (parser->unparsed_functions_queues)
15969 = tree_cons (current_class_type, decl,
15970 TREE_PURPOSE (parser->unparsed_functions_queues));
15971 break;
15972 }
15973 }
15974
15975 /* FN is a FUNCTION_DECL which may contains a parameter with an
15976 unparsed DEFAULT_ARG. Parse the default args now. This function
15977 assumes that the current scope is the scope in which the default
15978 argument should be processed. */
15979
15980 static void
15981 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15982 {
15983 bool saved_local_variables_forbidden_p;
15984 tree parm;
15985
15986 /* While we're parsing the default args, we might (due to the
15987 statement expression extension) encounter more classes. We want
15988 to handle them right away, but we don't want them getting mixed
15989 up with default args that are currently in the queue. */
15990 parser->unparsed_functions_queues
15991 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15992
15993 /* Local variable names (and the `this' keyword) may not appear
15994 in a default argument. */
15995 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15996 parser->local_variables_forbidden_p = true;
15997
15998 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15999 parm;
16000 parm = TREE_CHAIN (parm))
16001 {
16002 cp_token_cache *tokens;
16003 tree default_arg = TREE_PURPOSE (parm);
16004 tree parsed_arg;
16005 VEC(tree,gc) *insts;
16006 tree copy;
16007 unsigned ix;
16008
16009 if (!default_arg)
16010 continue;
16011
16012 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16013 /* This can happen for a friend declaration for a function
16014 already declared with default arguments. */
16015 continue;
16016
16017 /* Push the saved tokens for the default argument onto the parser's
16018 lexer stack. */
16019 tokens = DEFARG_TOKENS (default_arg);
16020 cp_parser_push_lexer_for_tokens (parser, tokens);
16021
16022 /* Parse the assignment-expression. */
16023 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16024
16025 if (!processing_template_decl)
16026 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16027
16028 TREE_PURPOSE (parm) = parsed_arg;
16029
16030 /* Update any instantiations we've already created. */
16031 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16032 VEC_iterate (tree, insts, ix, copy); ix++)
16033 TREE_PURPOSE (copy) = parsed_arg;
16034
16035 /* If the token stream has not been completely used up, then
16036 there was extra junk after the end of the default
16037 argument. */
16038 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16039 cp_parser_error (parser, "expected %<,%>");
16040
16041 /* Revert to the main lexer. */
16042 cp_parser_pop_lexer (parser);
16043 }
16044
16045 /* Make sure no default arg is missing. */
16046 check_default_args (fn);
16047
16048 /* Restore the state of local_variables_forbidden_p. */
16049 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16050
16051 /* Restore the queue. */
16052 parser->unparsed_functions_queues
16053 = TREE_CHAIN (parser->unparsed_functions_queues);
16054 }
16055
16056 /* Parse the operand of `sizeof' (or a similar operator). Returns
16057 either a TYPE or an expression, depending on the form of the
16058 input. The KEYWORD indicates which kind of expression we have
16059 encountered. */
16060
16061 static tree
16062 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16063 {
16064 static const char *format;
16065 tree expr = NULL_TREE;
16066 const char *saved_message;
16067 bool saved_integral_constant_expression_p;
16068 bool saved_non_integral_constant_expression_p;
16069
16070 /* Initialize FORMAT the first time we get here. */
16071 if (!format)
16072 format = "types may not be defined in '%s' expressions";
16073
16074 /* Types cannot be defined in a `sizeof' expression. Save away the
16075 old message. */
16076 saved_message = parser->type_definition_forbidden_message;
16077 /* And create the new one. */
16078 parser->type_definition_forbidden_message
16079 = XNEWVEC (const char, strlen (format)
16080 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16081 + 1 /* `\0' */);
16082 sprintf ((char *) parser->type_definition_forbidden_message,
16083 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16084
16085 /* The restrictions on constant-expressions do not apply inside
16086 sizeof expressions. */
16087 saved_integral_constant_expression_p
16088 = parser->integral_constant_expression_p;
16089 saved_non_integral_constant_expression_p
16090 = parser->non_integral_constant_expression_p;
16091 parser->integral_constant_expression_p = false;
16092
16093 /* Do not actually evaluate the expression. */
16094 ++skip_evaluation;
16095 /* If it's a `(', then we might be looking at the type-id
16096 construction. */
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16098 {
16099 tree type;
16100 bool saved_in_type_id_in_expr_p;
16101
16102 /* We can't be sure yet whether we're looking at a type-id or an
16103 expression. */
16104 cp_parser_parse_tentatively (parser);
16105 /* Consume the `('. */
16106 cp_lexer_consume_token (parser->lexer);
16107 /* Parse the type-id. */
16108 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16109 parser->in_type_id_in_expr_p = true;
16110 type = cp_parser_type_id (parser);
16111 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16112 /* Now, look for the trailing `)'. */
16113 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16114 /* If all went well, then we're done. */
16115 if (cp_parser_parse_definitely (parser))
16116 {
16117 cp_decl_specifier_seq decl_specs;
16118
16119 /* Build a trivial decl-specifier-seq. */
16120 clear_decl_specs (&decl_specs);
16121 decl_specs.type = type;
16122
16123 /* Call grokdeclarator to figure out what type this is. */
16124 expr = grokdeclarator (NULL,
16125 &decl_specs,
16126 TYPENAME,
16127 /*initialized=*/0,
16128 /*attrlist=*/NULL);
16129 }
16130 }
16131
16132 /* If the type-id production did not work out, then we must be
16133 looking at the unary-expression production. */
16134 if (!expr)
16135 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16136 /*cast_p=*/false);
16137 /* Go back to evaluating expressions. */
16138 --skip_evaluation;
16139
16140 /* Free the message we created. */
16141 free ((char *) parser->type_definition_forbidden_message);
16142 /* And restore the old one. */
16143 parser->type_definition_forbidden_message = saved_message;
16144 parser->integral_constant_expression_p
16145 = saved_integral_constant_expression_p;
16146 parser->non_integral_constant_expression_p
16147 = saved_non_integral_constant_expression_p;
16148
16149 return expr;
16150 }
16151
16152 /* If the current declaration has no declarator, return true. */
16153
16154 static bool
16155 cp_parser_declares_only_class_p (cp_parser *parser)
16156 {
16157 /* If the next token is a `;' or a `,' then there is no
16158 declarator. */
16159 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16160 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16161 }
16162
16163 /* Update the DECL_SPECS to reflect the storage class indicated by
16164 KEYWORD. */
16165
16166 static void
16167 cp_parser_set_storage_class (cp_parser *parser,
16168 cp_decl_specifier_seq *decl_specs,
16169 enum rid keyword)
16170 {
16171 cp_storage_class storage_class;
16172
16173 if (parser->in_unbraced_linkage_specification_p)
16174 {
16175 error ("invalid use of %qD in linkage specification",
16176 ridpointers[keyword]);
16177 return;
16178 }
16179 else if (decl_specs->storage_class != sc_none)
16180 {
16181 decl_specs->multiple_storage_classes_p = true;
16182 return;
16183 }
16184
16185 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16186 && decl_specs->specs[(int) ds_thread])
16187 {
16188 error ("%<__thread%> before %qD", ridpointers[keyword]);
16189 decl_specs->specs[(int) ds_thread] = 0;
16190 }
16191
16192 switch (keyword)
16193 {
16194 case RID_AUTO:
16195 storage_class = sc_auto;
16196 break;
16197 case RID_REGISTER:
16198 storage_class = sc_register;
16199 break;
16200 case RID_STATIC:
16201 storage_class = sc_static;
16202 break;
16203 case RID_EXTERN:
16204 storage_class = sc_extern;
16205 break;
16206 case RID_MUTABLE:
16207 storage_class = sc_mutable;
16208 break;
16209 default:
16210 gcc_unreachable ();
16211 }
16212 decl_specs->storage_class = storage_class;
16213 }
16214
16215 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16216 is true, the type is a user-defined type; otherwise it is a
16217 built-in type specified by a keyword. */
16218
16219 static void
16220 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16221 tree type_spec,
16222 bool user_defined_p)
16223 {
16224 decl_specs->any_specifiers_p = true;
16225
16226 /* If the user tries to redeclare bool or wchar_t (with, for
16227 example, in "typedef int wchar_t;") we remember that this is what
16228 happened. In system headers, we ignore these declarations so
16229 that G++ can work with system headers that are not C++-safe. */
16230 if (decl_specs->specs[(int) ds_typedef]
16231 && !user_defined_p
16232 && (type_spec == boolean_type_node
16233 || type_spec == wchar_type_node)
16234 && (decl_specs->type
16235 || decl_specs->specs[(int) ds_long]
16236 || decl_specs->specs[(int) ds_short]
16237 || decl_specs->specs[(int) ds_unsigned]
16238 || decl_specs->specs[(int) ds_signed]))
16239 {
16240 decl_specs->redefined_builtin_type = type_spec;
16241 if (!decl_specs->type)
16242 {
16243 decl_specs->type = type_spec;
16244 decl_specs->user_defined_type_p = false;
16245 }
16246 }
16247 else if (decl_specs->type)
16248 decl_specs->multiple_types_p = true;
16249 else
16250 {
16251 decl_specs->type = type_spec;
16252 decl_specs->user_defined_type_p = user_defined_p;
16253 decl_specs->redefined_builtin_type = NULL_TREE;
16254 }
16255 }
16256
16257 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16258 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16259
16260 static bool
16261 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16262 {
16263 return decl_specifiers->specs[(int) ds_friend] != 0;
16264 }
16265
16266 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16267 issue an error message indicating that TOKEN_DESC was expected.
16268
16269 Returns the token consumed, if the token had the appropriate type.
16270 Otherwise, returns NULL. */
16271
16272 static cp_token *
16273 cp_parser_require (cp_parser* parser,
16274 enum cpp_ttype type,
16275 const char* token_desc)
16276 {
16277 if (cp_lexer_next_token_is (parser->lexer, type))
16278 return cp_lexer_consume_token (parser->lexer);
16279 else
16280 {
16281 /* Output the MESSAGE -- unless we're parsing tentatively. */
16282 if (!cp_parser_simulate_error (parser))
16283 {
16284 char *message = concat ("expected ", token_desc, NULL);
16285 cp_parser_error (parser, message);
16286 free (message);
16287 }
16288 return NULL;
16289 }
16290 }
16291
16292 /* Like cp_parser_require, except that tokens will be skipped until
16293 the desired token is found. An error message is still produced if
16294 the next token is not as expected. */
16295
16296 static void
16297 cp_parser_skip_until_found (cp_parser* parser,
16298 enum cpp_ttype type,
16299 const char* token_desc)
16300 {
16301 cp_token *token;
16302 unsigned nesting_depth = 0;
16303
16304 if (cp_parser_require (parser, type, token_desc))
16305 return;
16306
16307 /* Skip tokens until the desired token is found. */
16308 while (true)
16309 {
16310 /* Peek at the next token. */
16311 token = cp_lexer_peek_token (parser->lexer);
16312
16313 /* If we've reached the token we want, consume it and stop. */
16314 if (token->type == type && !nesting_depth)
16315 {
16316 cp_lexer_consume_token (parser->lexer);
16317 return;
16318 }
16319
16320 switch (token->type)
16321 {
16322 case CPP_EOF:
16323 case CPP_PRAGMA_EOL:
16324 /* If we've run out of tokens, stop. */
16325 return;
16326
16327 case CPP_OPEN_BRACE:
16328 case CPP_OPEN_PAREN:
16329 case CPP_OPEN_SQUARE:
16330 ++nesting_depth;
16331 break;
16332
16333 case CPP_CLOSE_BRACE:
16334 case CPP_CLOSE_PAREN:
16335 case CPP_CLOSE_SQUARE:
16336 if (nesting_depth-- == 0)
16337 return;
16338 break;
16339
16340 default:
16341 break;
16342 }
16343
16344 /* Consume this token. */
16345 cp_lexer_consume_token (parser->lexer);
16346 }
16347 }
16348
16349 /* If the next token is the indicated keyword, consume it. Otherwise,
16350 issue an error message indicating that TOKEN_DESC was expected.
16351
16352 Returns the token consumed, if the token had the appropriate type.
16353 Otherwise, returns NULL. */
16354
16355 static cp_token *
16356 cp_parser_require_keyword (cp_parser* parser,
16357 enum rid keyword,
16358 const char* token_desc)
16359 {
16360 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16361
16362 if (token && token->keyword != keyword)
16363 {
16364 dyn_string_t error_msg;
16365
16366 /* Format the error message. */
16367 error_msg = dyn_string_new (0);
16368 dyn_string_append_cstr (error_msg, "expected ");
16369 dyn_string_append_cstr (error_msg, token_desc);
16370 cp_parser_error (parser, error_msg->s);
16371 dyn_string_delete (error_msg);
16372 return NULL;
16373 }
16374
16375 return token;
16376 }
16377
16378 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16379 function-definition. */
16380
16381 static bool
16382 cp_parser_token_starts_function_definition_p (cp_token* token)
16383 {
16384 return (/* An ordinary function-body begins with an `{'. */
16385 token->type == CPP_OPEN_BRACE
16386 /* A ctor-initializer begins with a `:'. */
16387 || token->type == CPP_COLON
16388 /* A function-try-block begins with `try'. */
16389 || token->keyword == RID_TRY
16390 /* The named return value extension begins with `return'. */
16391 || token->keyword == RID_RETURN);
16392 }
16393
16394 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16395 definition. */
16396
16397 static bool
16398 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16399 {
16400 cp_token *token;
16401
16402 token = cp_lexer_peek_token (parser->lexer);
16403 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16404 }
16405
16406 /* Returns TRUE iff the next token is the "," or ">" ending a
16407 template-argument. */
16408
16409 static bool
16410 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16411 {
16412 cp_token *token;
16413
16414 token = cp_lexer_peek_token (parser->lexer);
16415 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16416 }
16417
16418 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16419 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16420
16421 static bool
16422 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16423 size_t n)
16424 {
16425 cp_token *token;
16426
16427 token = cp_lexer_peek_nth_token (parser->lexer, n);
16428 if (token->type == CPP_LESS)
16429 return true;
16430 /* Check for the sequence `<::' in the original code. It would be lexed as
16431 `[:', where `[' is a digraph, and there is no whitespace before
16432 `:'. */
16433 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16434 {
16435 cp_token *token2;
16436 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16437 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16438 return true;
16439 }
16440 return false;
16441 }
16442
16443 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16444 or none_type otherwise. */
16445
16446 static enum tag_types
16447 cp_parser_token_is_class_key (cp_token* token)
16448 {
16449 switch (token->keyword)
16450 {
16451 case RID_CLASS:
16452 return class_type;
16453 case RID_STRUCT:
16454 return record_type;
16455 case RID_UNION:
16456 return union_type;
16457
16458 default:
16459 return none_type;
16460 }
16461 }
16462
16463 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16464
16465 static void
16466 cp_parser_check_class_key (enum tag_types class_key, tree type)
16467 {
16468 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16469 pedwarn ("%qs tag used in naming %q#T",
16470 class_key == union_type ? "union"
16471 : class_key == record_type ? "struct" : "class",
16472 type);
16473 }
16474
16475 /* Issue an error message if DECL is redeclared with different
16476 access than its original declaration [class.access.spec/3].
16477 This applies to nested classes and nested class templates.
16478 [class.mem/1]. */
16479
16480 static void
16481 cp_parser_check_access_in_redeclaration (tree decl)
16482 {
16483 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16484 return;
16485
16486 if ((TREE_PRIVATE (decl)
16487 != (current_access_specifier == access_private_node))
16488 || (TREE_PROTECTED (decl)
16489 != (current_access_specifier == access_protected_node)))
16490 error ("%qD redeclared with different access", decl);
16491 }
16492
16493 /* Look for the `template' keyword, as a syntactic disambiguator.
16494 Return TRUE iff it is present, in which case it will be
16495 consumed. */
16496
16497 static bool
16498 cp_parser_optional_template_keyword (cp_parser *parser)
16499 {
16500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16501 {
16502 /* The `template' keyword can only be used within templates;
16503 outside templates the parser can always figure out what is a
16504 template and what is not. */
16505 if (!processing_template_decl)
16506 {
16507 error ("%<template%> (as a disambiguator) is only allowed "
16508 "within templates");
16509 /* If this part of the token stream is rescanned, the same
16510 error message would be generated. So, we purge the token
16511 from the stream. */
16512 cp_lexer_purge_token (parser->lexer);
16513 return false;
16514 }
16515 else
16516 {
16517 /* Consume the `template' keyword. */
16518 cp_lexer_consume_token (parser->lexer);
16519 return true;
16520 }
16521 }
16522
16523 return false;
16524 }
16525
16526 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16527 set PARSER->SCOPE, and perform other related actions. */
16528
16529 static void
16530 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16531 {
16532 tree value;
16533 tree check;
16534
16535 /* Get the stored value. */
16536 value = cp_lexer_consume_token (parser->lexer)->value;
16537 /* Perform any access checks that were deferred. */
16538 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16539 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16540 /* Set the scope from the stored value. */
16541 parser->scope = TREE_VALUE (value);
16542 parser->qualifying_scope = TREE_TYPE (value);
16543 parser->object_scope = NULL_TREE;
16544 }
16545
16546 /* Consume tokens up through a non-nested END token. */
16547
16548 static void
16549 cp_parser_cache_group (cp_parser *parser,
16550 enum cpp_ttype end,
16551 unsigned depth)
16552 {
16553 while (true)
16554 {
16555 cp_token *token;
16556
16557 /* Abort a parenthesized expression if we encounter a brace. */
16558 if ((end == CPP_CLOSE_PAREN || depth == 0)
16559 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16560 return;
16561 /* If we've reached the end of the file, stop. */
16562 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16563 || (end != CPP_PRAGMA_EOL
16564 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16565 return;
16566 /* Consume the next token. */
16567 token = cp_lexer_consume_token (parser->lexer);
16568 /* See if it starts a new group. */
16569 if (token->type == CPP_OPEN_BRACE)
16570 {
16571 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16572 if (depth == 0)
16573 return;
16574 }
16575 else if (token->type == CPP_OPEN_PAREN)
16576 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16577 else if (token->type == CPP_PRAGMA)
16578 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16579 else if (token->type == end)
16580 return;
16581 }
16582 }
16583
16584 /* Begin parsing tentatively. We always save tokens while parsing
16585 tentatively so that if the tentative parsing fails we can restore the
16586 tokens. */
16587
16588 static void
16589 cp_parser_parse_tentatively (cp_parser* parser)
16590 {
16591 /* Enter a new parsing context. */
16592 parser->context = cp_parser_context_new (parser->context);
16593 /* Begin saving tokens. */
16594 cp_lexer_save_tokens (parser->lexer);
16595 /* In order to avoid repetitive access control error messages,
16596 access checks are queued up until we are no longer parsing
16597 tentatively. */
16598 push_deferring_access_checks (dk_deferred);
16599 }
16600
16601 /* Commit to the currently active tentative parse. */
16602
16603 static void
16604 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16605 {
16606 cp_parser_context *context;
16607 cp_lexer *lexer;
16608
16609 /* Mark all of the levels as committed. */
16610 lexer = parser->lexer;
16611 for (context = parser->context; context->next; context = context->next)
16612 {
16613 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16614 break;
16615 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16616 while (!cp_lexer_saving_tokens (lexer))
16617 lexer = lexer->next;
16618 cp_lexer_commit_tokens (lexer);
16619 }
16620 }
16621
16622 /* Abort the currently active tentative parse. All consumed tokens
16623 will be rolled back, and no diagnostics will be issued. */
16624
16625 static void
16626 cp_parser_abort_tentative_parse (cp_parser* parser)
16627 {
16628 cp_parser_simulate_error (parser);
16629 /* Now, pretend that we want to see if the construct was
16630 successfully parsed. */
16631 cp_parser_parse_definitely (parser);
16632 }
16633
16634 /* Stop parsing tentatively. If a parse error has occurred, restore the
16635 token stream. Otherwise, commit to the tokens we have consumed.
16636 Returns true if no error occurred; false otherwise. */
16637
16638 static bool
16639 cp_parser_parse_definitely (cp_parser* parser)
16640 {
16641 bool error_occurred;
16642 cp_parser_context *context;
16643
16644 /* Remember whether or not an error occurred, since we are about to
16645 destroy that information. */
16646 error_occurred = cp_parser_error_occurred (parser);
16647 /* Remove the topmost context from the stack. */
16648 context = parser->context;
16649 parser->context = context->next;
16650 /* If no parse errors occurred, commit to the tentative parse. */
16651 if (!error_occurred)
16652 {
16653 /* Commit to the tokens read tentatively, unless that was
16654 already done. */
16655 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16656 cp_lexer_commit_tokens (parser->lexer);
16657
16658 pop_to_parent_deferring_access_checks ();
16659 }
16660 /* Otherwise, if errors occurred, roll back our state so that things
16661 are just as they were before we began the tentative parse. */
16662 else
16663 {
16664 cp_lexer_rollback_tokens (parser->lexer);
16665 pop_deferring_access_checks ();
16666 }
16667 /* Add the context to the front of the free list. */
16668 context->next = cp_parser_context_free_list;
16669 cp_parser_context_free_list = context;
16670
16671 return !error_occurred;
16672 }
16673
16674 /* Returns true if we are parsing tentatively and are not committed to
16675 this tentative parse. */
16676
16677 static bool
16678 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16679 {
16680 return (cp_parser_parsing_tentatively (parser)
16681 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16682 }
16683
16684 /* Returns nonzero iff an error has occurred during the most recent
16685 tentative parse. */
16686
16687 static bool
16688 cp_parser_error_occurred (cp_parser* parser)
16689 {
16690 return (cp_parser_parsing_tentatively (parser)
16691 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16692 }
16693
16694 /* Returns nonzero if GNU extensions are allowed. */
16695
16696 static bool
16697 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16698 {
16699 return parser->allow_gnu_extensions_p;
16700 }
16701 \f
16702 /* Objective-C++ Productions */
16703
16704
16705 /* Parse an Objective-C expression, which feeds into a primary-expression
16706 above.
16707
16708 objc-expression:
16709 objc-message-expression
16710 objc-string-literal
16711 objc-encode-expression
16712 objc-protocol-expression
16713 objc-selector-expression
16714
16715 Returns a tree representation of the expression. */
16716
16717 static tree
16718 cp_parser_objc_expression (cp_parser* parser)
16719 {
16720 /* Try to figure out what kind of declaration is present. */
16721 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16722
16723 switch (kwd->type)
16724 {
16725 case CPP_OPEN_SQUARE:
16726 return cp_parser_objc_message_expression (parser);
16727
16728 case CPP_OBJC_STRING:
16729 kwd = cp_lexer_consume_token (parser->lexer);
16730 return objc_build_string_object (kwd->value);
16731
16732 case CPP_KEYWORD:
16733 switch (kwd->keyword)
16734 {
16735 case RID_AT_ENCODE:
16736 return cp_parser_objc_encode_expression (parser);
16737
16738 case RID_AT_PROTOCOL:
16739 return cp_parser_objc_protocol_expression (parser);
16740
16741 case RID_AT_SELECTOR:
16742 return cp_parser_objc_selector_expression (parser);
16743
16744 default:
16745 break;
16746 }
16747 default:
16748 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16749 cp_parser_skip_to_end_of_block_or_statement (parser);
16750 }
16751
16752 return error_mark_node;
16753 }
16754
16755 /* Parse an Objective-C message expression.
16756
16757 objc-message-expression:
16758 [ objc-message-receiver objc-message-args ]
16759
16760 Returns a representation of an Objective-C message. */
16761
16762 static tree
16763 cp_parser_objc_message_expression (cp_parser* parser)
16764 {
16765 tree receiver, messageargs;
16766
16767 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16768 receiver = cp_parser_objc_message_receiver (parser);
16769 messageargs = cp_parser_objc_message_args (parser);
16770 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16771
16772 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16773 }
16774
16775 /* Parse an objc-message-receiver.
16776
16777 objc-message-receiver:
16778 expression
16779 simple-type-specifier
16780
16781 Returns a representation of the type or expression. */
16782
16783 static tree
16784 cp_parser_objc_message_receiver (cp_parser* parser)
16785 {
16786 tree rcv;
16787
16788 /* An Objective-C message receiver may be either (1) a type
16789 or (2) an expression. */
16790 cp_parser_parse_tentatively (parser);
16791 rcv = cp_parser_expression (parser, false);
16792
16793 if (cp_parser_parse_definitely (parser))
16794 return rcv;
16795
16796 rcv = cp_parser_simple_type_specifier (parser,
16797 /*decl_specs=*/NULL,
16798 CP_PARSER_FLAGS_NONE);
16799
16800 return objc_get_class_reference (rcv);
16801 }
16802
16803 /* Parse the arguments and selectors comprising an Objective-C message.
16804
16805 objc-message-args:
16806 objc-selector
16807 objc-selector-args
16808 objc-selector-args , objc-comma-args
16809
16810 objc-selector-args:
16811 objc-selector [opt] : assignment-expression
16812 objc-selector-args objc-selector [opt] : assignment-expression
16813
16814 objc-comma-args:
16815 assignment-expression
16816 objc-comma-args , assignment-expression
16817
16818 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16819 selector arguments and TREE_VALUE containing a list of comma
16820 arguments. */
16821
16822 static tree
16823 cp_parser_objc_message_args (cp_parser* parser)
16824 {
16825 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16826 bool maybe_unary_selector_p = true;
16827 cp_token *token = cp_lexer_peek_token (parser->lexer);
16828
16829 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16830 {
16831 tree selector = NULL_TREE, arg;
16832
16833 if (token->type != CPP_COLON)
16834 selector = cp_parser_objc_selector (parser);
16835
16836 /* Detect if we have a unary selector. */
16837 if (maybe_unary_selector_p
16838 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16839 return build_tree_list (selector, NULL_TREE);
16840
16841 maybe_unary_selector_p = false;
16842 cp_parser_require (parser, CPP_COLON, "`:'");
16843 arg = cp_parser_assignment_expression (parser, false);
16844
16845 sel_args
16846 = chainon (sel_args,
16847 build_tree_list (selector, arg));
16848
16849 token = cp_lexer_peek_token (parser->lexer);
16850 }
16851
16852 /* Handle non-selector arguments, if any. */
16853 while (token->type == CPP_COMMA)
16854 {
16855 tree arg;
16856
16857 cp_lexer_consume_token (parser->lexer);
16858 arg = cp_parser_assignment_expression (parser, false);
16859
16860 addl_args
16861 = chainon (addl_args,
16862 build_tree_list (NULL_TREE, arg));
16863
16864 token = cp_lexer_peek_token (parser->lexer);
16865 }
16866
16867 return build_tree_list (sel_args, addl_args);
16868 }
16869
16870 /* Parse an Objective-C encode expression.
16871
16872 objc-encode-expression:
16873 @encode objc-typename
16874
16875 Returns an encoded representation of the type argument. */
16876
16877 static tree
16878 cp_parser_objc_encode_expression (cp_parser* parser)
16879 {
16880 tree type;
16881
16882 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16883 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16884 type = complete_type (cp_parser_type_id (parser));
16885 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16886
16887 if (!type)
16888 {
16889 error ("%<@encode%> must specify a type as an argument");
16890 return error_mark_node;
16891 }
16892
16893 return objc_build_encode_expr (type);
16894 }
16895
16896 /* Parse an Objective-C @defs expression. */
16897
16898 static tree
16899 cp_parser_objc_defs_expression (cp_parser *parser)
16900 {
16901 tree name;
16902
16903 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16904 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16905 name = cp_parser_identifier (parser);
16906 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16907
16908 return objc_get_class_ivars (name);
16909 }
16910
16911 /* Parse an Objective-C protocol expression.
16912
16913 objc-protocol-expression:
16914 @protocol ( identifier )
16915
16916 Returns a representation of the protocol expression. */
16917
16918 static tree
16919 cp_parser_objc_protocol_expression (cp_parser* parser)
16920 {
16921 tree proto;
16922
16923 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16924 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16925 proto = cp_parser_identifier (parser);
16926 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16927
16928 return objc_build_protocol_expr (proto);
16929 }
16930
16931 /* Parse an Objective-C selector expression.
16932
16933 objc-selector-expression:
16934 @selector ( objc-method-signature )
16935
16936 objc-method-signature:
16937 objc-selector
16938 objc-selector-seq
16939
16940 objc-selector-seq:
16941 objc-selector :
16942 objc-selector-seq objc-selector :
16943
16944 Returns a representation of the method selector. */
16945
16946 static tree
16947 cp_parser_objc_selector_expression (cp_parser* parser)
16948 {
16949 tree sel_seq = NULL_TREE;
16950 bool maybe_unary_selector_p = true;
16951 cp_token *token;
16952
16953 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16954 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16955 token = cp_lexer_peek_token (parser->lexer);
16956
16957 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16958 || token->type == CPP_SCOPE)
16959 {
16960 tree selector = NULL_TREE;
16961
16962 if (token->type != CPP_COLON
16963 || token->type == CPP_SCOPE)
16964 selector = cp_parser_objc_selector (parser);
16965
16966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16967 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16968 {
16969 /* Detect if we have a unary selector. */
16970 if (maybe_unary_selector_p)
16971 {
16972 sel_seq = selector;
16973 goto finish_selector;
16974 }
16975 else
16976 {
16977 cp_parser_error (parser, "expected %<:%>");
16978 }
16979 }
16980 maybe_unary_selector_p = false;
16981 token = cp_lexer_consume_token (parser->lexer);
16982
16983 if (token->type == CPP_SCOPE)
16984 {
16985 sel_seq
16986 = chainon (sel_seq,
16987 build_tree_list (selector, NULL_TREE));
16988 sel_seq
16989 = chainon (sel_seq,
16990 build_tree_list (NULL_TREE, NULL_TREE));
16991 }
16992 else
16993 sel_seq
16994 = chainon (sel_seq,
16995 build_tree_list (selector, NULL_TREE));
16996
16997 token = cp_lexer_peek_token (parser->lexer);
16998 }
16999
17000 finish_selector:
17001 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17002
17003 return objc_build_selector_expr (sel_seq);
17004 }
17005
17006 /* Parse a list of identifiers.
17007
17008 objc-identifier-list:
17009 identifier
17010 objc-identifier-list , identifier
17011
17012 Returns a TREE_LIST of identifier nodes. */
17013
17014 static tree
17015 cp_parser_objc_identifier_list (cp_parser* parser)
17016 {
17017 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17018 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17019
17020 while (sep->type == CPP_COMMA)
17021 {
17022 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17023 list = chainon (list,
17024 build_tree_list (NULL_TREE,
17025 cp_parser_identifier (parser)));
17026 sep = cp_lexer_peek_token (parser->lexer);
17027 }
17028
17029 return list;
17030 }
17031
17032 /* Parse an Objective-C alias declaration.
17033
17034 objc-alias-declaration:
17035 @compatibility_alias identifier identifier ;
17036
17037 This function registers the alias mapping with the Objective-C front-end.
17038 It returns nothing. */
17039
17040 static void
17041 cp_parser_objc_alias_declaration (cp_parser* parser)
17042 {
17043 tree alias, orig;
17044
17045 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17046 alias = cp_parser_identifier (parser);
17047 orig = cp_parser_identifier (parser);
17048 objc_declare_alias (alias, orig);
17049 cp_parser_consume_semicolon_at_end_of_statement (parser);
17050 }
17051
17052 /* Parse an Objective-C class forward-declaration.
17053
17054 objc-class-declaration:
17055 @class objc-identifier-list ;
17056
17057 The function registers the forward declarations with the Objective-C
17058 front-end. It returns nothing. */
17059
17060 static void
17061 cp_parser_objc_class_declaration (cp_parser* parser)
17062 {
17063 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17064 objc_declare_class (cp_parser_objc_identifier_list (parser));
17065 cp_parser_consume_semicolon_at_end_of_statement (parser);
17066 }
17067
17068 /* Parse a list of Objective-C protocol references.
17069
17070 objc-protocol-refs-opt:
17071 objc-protocol-refs [opt]
17072
17073 objc-protocol-refs:
17074 < objc-identifier-list >
17075
17076 Returns a TREE_LIST of identifiers, if any. */
17077
17078 static tree
17079 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17080 {
17081 tree protorefs = NULL_TREE;
17082
17083 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17084 {
17085 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17086 protorefs = cp_parser_objc_identifier_list (parser);
17087 cp_parser_require (parser, CPP_GREATER, "`>'");
17088 }
17089
17090 return protorefs;
17091 }
17092
17093 /* Parse a Objective-C visibility specification. */
17094
17095 static void
17096 cp_parser_objc_visibility_spec (cp_parser* parser)
17097 {
17098 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17099
17100 switch (vis->keyword)
17101 {
17102 case RID_AT_PRIVATE:
17103 objc_set_visibility (2);
17104 break;
17105 case RID_AT_PROTECTED:
17106 objc_set_visibility (0);
17107 break;
17108 case RID_AT_PUBLIC:
17109 objc_set_visibility (1);
17110 break;
17111 default:
17112 return;
17113 }
17114
17115 /* Eat '@private'/'@protected'/'@public'. */
17116 cp_lexer_consume_token (parser->lexer);
17117 }
17118
17119 /* Parse an Objective-C method type. */
17120
17121 static void
17122 cp_parser_objc_method_type (cp_parser* parser)
17123 {
17124 objc_set_method_type
17125 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17126 ? PLUS_EXPR
17127 : MINUS_EXPR);
17128 }
17129
17130 /* Parse an Objective-C protocol qualifier. */
17131
17132 static tree
17133 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17134 {
17135 tree quals = NULL_TREE, node;
17136 cp_token *token = cp_lexer_peek_token (parser->lexer);
17137
17138 node = token->value;
17139
17140 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17141 && (node == ridpointers [(int) RID_IN]
17142 || node == ridpointers [(int) RID_OUT]
17143 || node == ridpointers [(int) RID_INOUT]
17144 || node == ridpointers [(int) RID_BYCOPY]
17145 || node == ridpointers [(int) RID_BYREF]
17146 || node == ridpointers [(int) RID_ONEWAY]))
17147 {
17148 quals = tree_cons (NULL_TREE, node, quals);
17149 cp_lexer_consume_token (parser->lexer);
17150 token = cp_lexer_peek_token (parser->lexer);
17151 node = token->value;
17152 }
17153
17154 return quals;
17155 }
17156
17157 /* Parse an Objective-C typename. */
17158
17159 static tree
17160 cp_parser_objc_typename (cp_parser* parser)
17161 {
17162 tree typename = NULL_TREE;
17163
17164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17165 {
17166 tree proto_quals, cp_type = NULL_TREE;
17167
17168 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17169 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17170
17171 /* An ObjC type name may consist of just protocol qualifiers, in which
17172 case the type shall default to 'id'. */
17173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17174 cp_type = cp_parser_type_id (parser);
17175
17176 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17177 typename = build_tree_list (proto_quals, cp_type);
17178 }
17179
17180 return typename;
17181 }
17182
17183 /* Check to see if TYPE refers to an Objective-C selector name. */
17184
17185 static bool
17186 cp_parser_objc_selector_p (enum cpp_ttype type)
17187 {
17188 return (type == CPP_NAME || type == CPP_KEYWORD
17189 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17190 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17191 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17192 || type == CPP_XOR || type == CPP_XOR_EQ);
17193 }
17194
17195 /* Parse an Objective-C selector. */
17196
17197 static tree
17198 cp_parser_objc_selector (cp_parser* parser)
17199 {
17200 cp_token *token = cp_lexer_consume_token (parser->lexer);
17201
17202 if (!cp_parser_objc_selector_p (token->type))
17203 {
17204 error ("invalid Objective-C++ selector name");
17205 return error_mark_node;
17206 }
17207
17208 /* C++ operator names are allowed to appear in ObjC selectors. */
17209 switch (token->type)
17210 {
17211 case CPP_AND_AND: return get_identifier ("and");
17212 case CPP_AND_EQ: return get_identifier ("and_eq");
17213 case CPP_AND: return get_identifier ("bitand");
17214 case CPP_OR: return get_identifier ("bitor");
17215 case CPP_COMPL: return get_identifier ("compl");
17216 case CPP_NOT: return get_identifier ("not");
17217 case CPP_NOT_EQ: return get_identifier ("not_eq");
17218 case CPP_OR_OR: return get_identifier ("or");
17219 case CPP_OR_EQ: return get_identifier ("or_eq");
17220 case CPP_XOR: return get_identifier ("xor");
17221 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17222 default: return token->value;
17223 }
17224 }
17225
17226 /* Parse an Objective-C params list. */
17227
17228 static tree
17229 cp_parser_objc_method_keyword_params (cp_parser* parser)
17230 {
17231 tree params = NULL_TREE;
17232 bool maybe_unary_selector_p = true;
17233 cp_token *token = cp_lexer_peek_token (parser->lexer);
17234
17235 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17236 {
17237 tree selector = NULL_TREE, typename, identifier;
17238
17239 if (token->type != CPP_COLON)
17240 selector = cp_parser_objc_selector (parser);
17241
17242 /* Detect if we have a unary selector. */
17243 if (maybe_unary_selector_p
17244 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17245 return selector;
17246
17247 maybe_unary_selector_p = false;
17248 cp_parser_require (parser, CPP_COLON, "`:'");
17249 typename = cp_parser_objc_typename (parser);
17250 identifier = cp_parser_identifier (parser);
17251
17252 params
17253 = chainon (params,
17254 objc_build_keyword_decl (selector,
17255 typename,
17256 identifier));
17257
17258 token = cp_lexer_peek_token (parser->lexer);
17259 }
17260
17261 return params;
17262 }
17263
17264 /* Parse the non-keyword Objective-C params. */
17265
17266 static tree
17267 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17268 {
17269 tree params = make_node (TREE_LIST);
17270 cp_token *token = cp_lexer_peek_token (parser->lexer);
17271 *ellipsisp = false; /* Initially, assume no ellipsis. */
17272
17273 while (token->type == CPP_COMMA)
17274 {
17275 cp_parameter_declarator *parmdecl;
17276 tree parm;
17277
17278 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17279 token = cp_lexer_peek_token (parser->lexer);
17280
17281 if (token->type == CPP_ELLIPSIS)
17282 {
17283 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17284 *ellipsisp = true;
17285 break;
17286 }
17287
17288 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17289 parm = grokdeclarator (parmdecl->declarator,
17290 &parmdecl->decl_specifiers,
17291 PARM, /*initialized=*/0,
17292 /*attrlist=*/NULL);
17293
17294 chainon (params, build_tree_list (NULL_TREE, parm));
17295 token = cp_lexer_peek_token (parser->lexer);
17296 }
17297
17298 return params;
17299 }
17300
17301 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17302
17303 static void
17304 cp_parser_objc_interstitial_code (cp_parser* parser)
17305 {
17306 cp_token *token = cp_lexer_peek_token (parser->lexer);
17307
17308 /* If the next token is `extern' and the following token is a string
17309 literal, then we have a linkage specification. */
17310 if (token->keyword == RID_EXTERN
17311 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17312 cp_parser_linkage_specification (parser);
17313 /* Handle #pragma, if any. */
17314 else if (token->type == CPP_PRAGMA)
17315 cp_parser_pragma (parser, pragma_external);
17316 /* Allow stray semicolons. */
17317 else if (token->type == CPP_SEMICOLON)
17318 cp_lexer_consume_token (parser->lexer);
17319 /* Finally, try to parse a block-declaration, or a function-definition. */
17320 else
17321 cp_parser_block_declaration (parser, /*statement_p=*/false);
17322 }
17323
17324 /* Parse a method signature. */
17325
17326 static tree
17327 cp_parser_objc_method_signature (cp_parser* parser)
17328 {
17329 tree rettype, kwdparms, optparms;
17330 bool ellipsis = false;
17331
17332 cp_parser_objc_method_type (parser);
17333 rettype = cp_parser_objc_typename (parser);
17334 kwdparms = cp_parser_objc_method_keyword_params (parser);
17335 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17336
17337 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17338 }
17339
17340 /* Pars an Objective-C method prototype list. */
17341
17342 static void
17343 cp_parser_objc_method_prototype_list (cp_parser* parser)
17344 {
17345 cp_token *token = cp_lexer_peek_token (parser->lexer);
17346
17347 while (token->keyword != RID_AT_END)
17348 {
17349 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17350 {
17351 objc_add_method_declaration
17352 (cp_parser_objc_method_signature (parser));
17353 cp_parser_consume_semicolon_at_end_of_statement (parser);
17354 }
17355 else
17356 /* Allow for interspersed non-ObjC++ code. */
17357 cp_parser_objc_interstitial_code (parser);
17358
17359 token = cp_lexer_peek_token (parser->lexer);
17360 }
17361
17362 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17363 objc_finish_interface ();
17364 }
17365
17366 /* Parse an Objective-C method definition list. */
17367
17368 static void
17369 cp_parser_objc_method_definition_list (cp_parser* parser)
17370 {
17371 cp_token *token = cp_lexer_peek_token (parser->lexer);
17372
17373 while (token->keyword != RID_AT_END)
17374 {
17375 tree meth;
17376
17377 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17378 {
17379 push_deferring_access_checks (dk_deferred);
17380 objc_start_method_definition
17381 (cp_parser_objc_method_signature (parser));
17382
17383 /* For historical reasons, we accept an optional semicolon. */
17384 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17385 cp_lexer_consume_token (parser->lexer);
17386
17387 perform_deferred_access_checks ();
17388 stop_deferring_access_checks ();
17389 meth = cp_parser_function_definition_after_declarator (parser,
17390 false);
17391 pop_deferring_access_checks ();
17392 objc_finish_method_definition (meth);
17393 }
17394 else
17395 /* Allow for interspersed non-ObjC++ code. */
17396 cp_parser_objc_interstitial_code (parser);
17397
17398 token = cp_lexer_peek_token (parser->lexer);
17399 }
17400
17401 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17402 objc_finish_implementation ();
17403 }
17404
17405 /* Parse Objective-C ivars. */
17406
17407 static void
17408 cp_parser_objc_class_ivars (cp_parser* parser)
17409 {
17410 cp_token *token = cp_lexer_peek_token (parser->lexer);
17411
17412 if (token->type != CPP_OPEN_BRACE)
17413 return; /* No ivars specified. */
17414
17415 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17416 token = cp_lexer_peek_token (parser->lexer);
17417
17418 while (token->type != CPP_CLOSE_BRACE)
17419 {
17420 cp_decl_specifier_seq declspecs;
17421 int decl_class_or_enum_p;
17422 tree prefix_attributes;
17423
17424 cp_parser_objc_visibility_spec (parser);
17425
17426 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17427 break;
17428
17429 cp_parser_decl_specifier_seq (parser,
17430 CP_PARSER_FLAGS_OPTIONAL,
17431 &declspecs,
17432 &decl_class_or_enum_p);
17433 prefix_attributes = declspecs.attributes;
17434 declspecs.attributes = NULL_TREE;
17435
17436 /* Keep going until we hit the `;' at the end of the
17437 declaration. */
17438 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17439 {
17440 tree width = NULL_TREE, attributes, first_attribute, decl;
17441 cp_declarator *declarator = NULL;
17442 int ctor_dtor_or_conv_p;
17443
17444 /* Check for a (possibly unnamed) bitfield declaration. */
17445 token = cp_lexer_peek_token (parser->lexer);
17446 if (token->type == CPP_COLON)
17447 goto eat_colon;
17448
17449 if (token->type == CPP_NAME
17450 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17451 == CPP_COLON))
17452 {
17453 /* Get the name of the bitfield. */
17454 declarator = make_id_declarator (NULL_TREE,
17455 cp_parser_identifier (parser),
17456 sfk_none);
17457
17458 eat_colon:
17459 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17460 /* Get the width of the bitfield. */
17461 width
17462 = cp_parser_constant_expression (parser,
17463 /*allow_non_constant=*/false,
17464 NULL);
17465 }
17466 else
17467 {
17468 /* Parse the declarator. */
17469 declarator
17470 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17471 &ctor_dtor_or_conv_p,
17472 /*parenthesized_p=*/NULL,
17473 /*member_p=*/false);
17474 }
17475
17476 /* Look for attributes that apply to the ivar. */
17477 attributes = cp_parser_attributes_opt (parser);
17478 /* Remember which attributes are prefix attributes and
17479 which are not. */
17480 first_attribute = attributes;
17481 /* Combine the attributes. */
17482 attributes = chainon (prefix_attributes, attributes);
17483
17484 if (width)
17485 {
17486 /* Create the bitfield declaration. */
17487 decl = grokbitfield (declarator, &declspecs, width);
17488 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17489 }
17490 else
17491 decl = grokfield (declarator, &declspecs,
17492 NULL_TREE, /*init_const_expr_p=*/false,
17493 NULL_TREE, attributes);
17494
17495 /* Add the instance variable. */
17496 objc_add_instance_variable (decl);
17497
17498 /* Reset PREFIX_ATTRIBUTES. */
17499 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17500 attributes = TREE_CHAIN (attributes);
17501 if (attributes)
17502 TREE_CHAIN (attributes) = NULL_TREE;
17503
17504 token = cp_lexer_peek_token (parser->lexer);
17505
17506 if (token->type == CPP_COMMA)
17507 {
17508 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17509 continue;
17510 }
17511 break;
17512 }
17513
17514 cp_parser_consume_semicolon_at_end_of_statement (parser);
17515 token = cp_lexer_peek_token (parser->lexer);
17516 }
17517
17518 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17519 /* For historical reasons, we accept an optional semicolon. */
17520 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17521 cp_lexer_consume_token (parser->lexer);
17522 }
17523
17524 /* Parse an Objective-C protocol declaration. */
17525
17526 static void
17527 cp_parser_objc_protocol_declaration (cp_parser* parser)
17528 {
17529 tree proto, protorefs;
17530 cp_token *tok;
17531
17532 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17534 {
17535 error ("identifier expected after %<@protocol%>");
17536 goto finish;
17537 }
17538
17539 /* See if we have a forward declaration or a definition. */
17540 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17541
17542 /* Try a forward declaration first. */
17543 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17544 {
17545 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17546 finish:
17547 cp_parser_consume_semicolon_at_end_of_statement (parser);
17548 }
17549
17550 /* Ok, we got a full-fledged definition (or at least should). */
17551 else
17552 {
17553 proto = cp_parser_identifier (parser);
17554 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17555 objc_start_protocol (proto, protorefs);
17556 cp_parser_objc_method_prototype_list (parser);
17557 }
17558 }
17559
17560 /* Parse an Objective-C superclass or category. */
17561
17562 static void
17563 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17564 tree *categ)
17565 {
17566 cp_token *next = cp_lexer_peek_token (parser->lexer);
17567
17568 *super = *categ = NULL_TREE;
17569 if (next->type == CPP_COLON)
17570 {
17571 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17572 *super = cp_parser_identifier (parser);
17573 }
17574 else if (next->type == CPP_OPEN_PAREN)
17575 {
17576 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17577 *categ = cp_parser_identifier (parser);
17578 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17579 }
17580 }
17581
17582 /* Parse an Objective-C class interface. */
17583
17584 static void
17585 cp_parser_objc_class_interface (cp_parser* parser)
17586 {
17587 tree name, super, categ, protos;
17588
17589 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17590 name = cp_parser_identifier (parser);
17591 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17592 protos = cp_parser_objc_protocol_refs_opt (parser);
17593
17594 /* We have either a class or a category on our hands. */
17595 if (categ)
17596 objc_start_category_interface (name, categ, protos);
17597 else
17598 {
17599 objc_start_class_interface (name, super, protos);
17600 /* Handle instance variable declarations, if any. */
17601 cp_parser_objc_class_ivars (parser);
17602 objc_continue_interface ();
17603 }
17604
17605 cp_parser_objc_method_prototype_list (parser);
17606 }
17607
17608 /* Parse an Objective-C class implementation. */
17609
17610 static void
17611 cp_parser_objc_class_implementation (cp_parser* parser)
17612 {
17613 tree name, super, categ;
17614
17615 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17616 name = cp_parser_identifier (parser);
17617 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17618
17619 /* We have either a class or a category on our hands. */
17620 if (categ)
17621 objc_start_category_implementation (name, categ);
17622 else
17623 {
17624 objc_start_class_implementation (name, super);
17625 /* Handle instance variable declarations, if any. */
17626 cp_parser_objc_class_ivars (parser);
17627 objc_continue_implementation ();
17628 }
17629
17630 cp_parser_objc_method_definition_list (parser);
17631 }
17632
17633 /* Consume the @end token and finish off the implementation. */
17634
17635 static void
17636 cp_parser_objc_end_implementation (cp_parser* parser)
17637 {
17638 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17639 objc_finish_implementation ();
17640 }
17641
17642 /* Parse an Objective-C declaration. */
17643
17644 static void
17645 cp_parser_objc_declaration (cp_parser* parser)
17646 {
17647 /* Try to figure out what kind of declaration is present. */
17648 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17649
17650 switch (kwd->keyword)
17651 {
17652 case RID_AT_ALIAS:
17653 cp_parser_objc_alias_declaration (parser);
17654 break;
17655 case RID_AT_CLASS:
17656 cp_parser_objc_class_declaration (parser);
17657 break;
17658 case RID_AT_PROTOCOL:
17659 cp_parser_objc_protocol_declaration (parser);
17660 break;
17661 case RID_AT_INTERFACE:
17662 cp_parser_objc_class_interface (parser);
17663 break;
17664 case RID_AT_IMPLEMENTATION:
17665 cp_parser_objc_class_implementation (parser);
17666 break;
17667 case RID_AT_END:
17668 cp_parser_objc_end_implementation (parser);
17669 break;
17670 default:
17671 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17672 cp_parser_skip_to_end_of_block_or_statement (parser);
17673 }
17674 }
17675
17676 /* Parse an Objective-C try-catch-finally statement.
17677
17678 objc-try-catch-finally-stmt:
17679 @try compound-statement objc-catch-clause-seq [opt]
17680 objc-finally-clause [opt]
17681
17682 objc-catch-clause-seq:
17683 objc-catch-clause objc-catch-clause-seq [opt]
17684
17685 objc-catch-clause:
17686 @catch ( exception-declaration ) compound-statement
17687
17688 objc-finally-clause
17689 @finally compound-statement
17690
17691 Returns NULL_TREE. */
17692
17693 static tree
17694 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17695 location_t location;
17696 tree stmt;
17697
17698 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17699 location = cp_lexer_peek_token (parser->lexer)->location;
17700 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17701 node, lest it get absorbed into the surrounding block. */
17702 stmt = push_stmt_list ();
17703 cp_parser_compound_statement (parser, NULL, false);
17704 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17705
17706 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17707 {
17708 cp_parameter_declarator *parmdecl;
17709 tree parm;
17710
17711 cp_lexer_consume_token (parser->lexer);
17712 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17713 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17714 parm = grokdeclarator (parmdecl->declarator,
17715 &parmdecl->decl_specifiers,
17716 PARM, /*initialized=*/0,
17717 /*attrlist=*/NULL);
17718 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17719 objc_begin_catch_clause (parm);
17720 cp_parser_compound_statement (parser, NULL, false);
17721 objc_finish_catch_clause ();
17722 }
17723
17724 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17725 {
17726 cp_lexer_consume_token (parser->lexer);
17727 location = cp_lexer_peek_token (parser->lexer)->location;
17728 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17729 node, lest it get absorbed into the surrounding block. */
17730 stmt = push_stmt_list ();
17731 cp_parser_compound_statement (parser, NULL, false);
17732 objc_build_finally_clause (location, pop_stmt_list (stmt));
17733 }
17734
17735 return objc_finish_try_stmt ();
17736 }
17737
17738 /* Parse an Objective-C synchronized statement.
17739
17740 objc-synchronized-stmt:
17741 @synchronized ( expression ) compound-statement
17742
17743 Returns NULL_TREE. */
17744
17745 static tree
17746 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17747 location_t location;
17748 tree lock, stmt;
17749
17750 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17751
17752 location = cp_lexer_peek_token (parser->lexer)->location;
17753 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17754 lock = cp_parser_expression (parser, false);
17755 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17756
17757 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17758 node, lest it get absorbed into the surrounding block. */
17759 stmt = push_stmt_list ();
17760 cp_parser_compound_statement (parser, NULL, false);
17761
17762 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17763 }
17764
17765 /* Parse an Objective-C throw statement.
17766
17767 objc-throw-stmt:
17768 @throw assignment-expression [opt] ;
17769
17770 Returns a constructed '@throw' statement. */
17771
17772 static tree
17773 cp_parser_objc_throw_statement (cp_parser *parser) {
17774 tree expr = NULL_TREE;
17775
17776 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17777
17778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17779 expr = cp_parser_assignment_expression (parser, false);
17780
17781 cp_parser_consume_semicolon_at_end_of_statement (parser);
17782
17783 return objc_build_throw_stmt (expr);
17784 }
17785
17786 /* Parse an Objective-C statement. */
17787
17788 static tree
17789 cp_parser_objc_statement (cp_parser * parser) {
17790 /* Try to figure out what kind of declaration is present. */
17791 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17792
17793 switch (kwd->keyword)
17794 {
17795 case RID_AT_TRY:
17796 return cp_parser_objc_try_catch_finally_statement (parser);
17797 case RID_AT_SYNCHRONIZED:
17798 return cp_parser_objc_synchronized_statement (parser);
17799 case RID_AT_THROW:
17800 return cp_parser_objc_throw_statement (parser);
17801 default:
17802 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17803 cp_parser_skip_to_end_of_block_or_statement (parser);
17804 }
17805
17806 return error_mark_node;
17807 }
17808 \f
17809 /* OpenMP 2.5 parsing routines. */
17810
17811 /* All OpenMP clauses. OpenMP 2.5. */
17812 typedef enum pragma_omp_clause {
17813 PRAGMA_OMP_CLAUSE_NONE = 0,
17814
17815 PRAGMA_OMP_CLAUSE_COPYIN,
17816 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17817 PRAGMA_OMP_CLAUSE_DEFAULT,
17818 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17819 PRAGMA_OMP_CLAUSE_IF,
17820 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17821 PRAGMA_OMP_CLAUSE_NOWAIT,
17822 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17823 PRAGMA_OMP_CLAUSE_ORDERED,
17824 PRAGMA_OMP_CLAUSE_PRIVATE,
17825 PRAGMA_OMP_CLAUSE_REDUCTION,
17826 PRAGMA_OMP_CLAUSE_SCHEDULE,
17827 PRAGMA_OMP_CLAUSE_SHARED
17828 } pragma_omp_clause;
17829
17830 /* Returns name of the next clause.
17831 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17832 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17833 returned and the token is consumed. */
17834
17835 static pragma_omp_clause
17836 cp_parser_omp_clause_name (cp_parser *parser)
17837 {
17838 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17839
17840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17841 result = PRAGMA_OMP_CLAUSE_IF;
17842 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17843 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17844 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17845 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17846 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17847 {
17848 tree id = cp_lexer_peek_token (parser->lexer)->value;
17849 const char *p = IDENTIFIER_POINTER (id);
17850
17851 switch (p[0])
17852 {
17853 case 'c':
17854 if (!strcmp ("copyin", p))
17855 result = PRAGMA_OMP_CLAUSE_COPYIN;
17856 else if (!strcmp ("copyprivate", p))
17857 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17858 break;
17859 case 'f':
17860 if (!strcmp ("firstprivate", p))
17861 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17862 break;
17863 case 'l':
17864 if (!strcmp ("lastprivate", p))
17865 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17866 break;
17867 case 'n':
17868 if (!strcmp ("nowait", p))
17869 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17870 else if (!strcmp ("num_threads", p))
17871 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17872 break;
17873 case 'o':
17874 if (!strcmp ("ordered", p))
17875 result = PRAGMA_OMP_CLAUSE_ORDERED;
17876 break;
17877 case 'r':
17878 if (!strcmp ("reduction", p))
17879 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17880 break;
17881 case 's':
17882 if (!strcmp ("schedule", p))
17883 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17884 else if (!strcmp ("shared", p))
17885 result = PRAGMA_OMP_CLAUSE_SHARED;
17886 break;
17887 }
17888 }
17889
17890 if (result != PRAGMA_OMP_CLAUSE_NONE)
17891 cp_lexer_consume_token (parser->lexer);
17892
17893 return result;
17894 }
17895
17896 /* Validate that a clause of the given type does not already exist. */
17897
17898 static void
17899 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17900 {
17901 tree c;
17902
17903 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17904 if (OMP_CLAUSE_CODE (c) == code)
17905 {
17906 error ("too many %qs clauses", name);
17907 break;
17908 }
17909 }
17910
17911 /* OpenMP 2.5:
17912 variable-list:
17913 identifier
17914 variable-list , identifier
17915
17916 In addition, we match a closing parenthesis. An opening parenthesis
17917 will have been consumed by the caller.
17918
17919 If KIND is nonzero, create the appropriate node and install the decl
17920 in OMP_CLAUSE_DECL and add the node to the head of the list.
17921
17922 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17923 return the list created. */
17924
17925 static tree
17926 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17927 tree list)
17928 {
17929 while (1)
17930 {
17931 tree name, decl;
17932
17933 name = cp_parser_id_expression (parser, /*template_p=*/false,
17934 /*check_dependency_p=*/true,
17935 /*template_p=*/NULL,
17936 /*declarator_p=*/false,
17937 /*optional_p=*/false);
17938 if (name == error_mark_node)
17939 goto skip_comma;
17940
17941 decl = cp_parser_lookup_name_simple (parser, name);
17942 if (decl == error_mark_node)
17943 cp_parser_name_lookup_error (parser, name, decl, NULL);
17944 else if (kind != 0)
17945 {
17946 tree u = build_omp_clause (kind);
17947 OMP_CLAUSE_DECL (u) = decl;
17948 OMP_CLAUSE_CHAIN (u) = list;
17949 list = u;
17950 }
17951 else
17952 list = tree_cons (decl, NULL_TREE, list);
17953
17954 get_comma:
17955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17956 break;
17957 cp_lexer_consume_token (parser->lexer);
17958 }
17959
17960 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17961 {
17962 int ending;
17963
17964 /* Try to resync to an unnested comma. Copied from
17965 cp_parser_parenthesized_expression_list. */
17966 skip_comma:
17967 ending = cp_parser_skip_to_closing_parenthesis (parser,
17968 /*recovering=*/true,
17969 /*or_comma=*/true,
17970 /*consume_paren=*/true);
17971 if (ending < 0)
17972 goto get_comma;
17973 }
17974
17975 return list;
17976 }
17977
17978 /* Similarly, but expect leading and trailing parenthesis. This is a very
17979 common case for omp clauses. */
17980
17981 static tree
17982 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17983 {
17984 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17985 return cp_parser_omp_var_list_no_open (parser, kind, list);
17986 return list;
17987 }
17988
17989 /* OpenMP 2.5:
17990 default ( shared | none ) */
17991
17992 static tree
17993 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17994 {
17995 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17996 tree c;
17997
17998 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17999 return list;
18000 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18001 {
18002 tree id = cp_lexer_peek_token (parser->lexer)->value;
18003 const char *p = IDENTIFIER_POINTER (id);
18004
18005 switch (p[0])
18006 {
18007 case 'n':
18008 if (strcmp ("none", p) != 0)
18009 goto invalid_kind;
18010 kind = OMP_CLAUSE_DEFAULT_NONE;
18011 break;
18012
18013 case 's':
18014 if (strcmp ("shared", p) != 0)
18015 goto invalid_kind;
18016 kind = OMP_CLAUSE_DEFAULT_SHARED;
18017 break;
18018
18019 default:
18020 goto invalid_kind;
18021 }
18022
18023 cp_lexer_consume_token (parser->lexer);
18024 }
18025 else
18026 {
18027 invalid_kind:
18028 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18029 }
18030
18031 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18033 /*or_comma=*/false,
18034 /*consume_paren=*/true);
18035
18036 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18037 return list;
18038
18039 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18040 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18041 OMP_CLAUSE_CHAIN (c) = list;
18042 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18043
18044 return c;
18045 }
18046
18047 /* OpenMP 2.5:
18048 if ( expression ) */
18049
18050 static tree
18051 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18052 {
18053 tree t, c;
18054
18055 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18056 return list;
18057
18058 t = cp_parser_condition (parser);
18059
18060 if (t == error_mark_node
18061 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18062 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18063 /*or_comma=*/false,
18064 /*consume_paren=*/true);
18065
18066 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18067
18068 c = build_omp_clause (OMP_CLAUSE_IF);
18069 OMP_CLAUSE_IF_EXPR (c) = t;
18070 OMP_CLAUSE_CHAIN (c) = list;
18071
18072 return c;
18073 }
18074
18075 /* OpenMP 2.5:
18076 nowait */
18077
18078 static tree
18079 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18080 {
18081 tree c;
18082
18083 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18084
18085 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18086 OMP_CLAUSE_CHAIN (c) = list;
18087 return c;
18088 }
18089
18090 /* OpenMP 2.5:
18091 num_threads ( expression ) */
18092
18093 static tree
18094 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18095 {
18096 tree t, c;
18097
18098 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18099 return list;
18100
18101 t = cp_parser_expression (parser, false);
18102
18103 if (t == error_mark_node
18104 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18105 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18106 /*or_comma=*/false,
18107 /*consume_paren=*/true);
18108
18109 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18110
18111 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18112 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18113 OMP_CLAUSE_CHAIN (c) = list;
18114
18115 return c;
18116 }
18117
18118 /* OpenMP 2.5:
18119 ordered */
18120
18121 static tree
18122 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18123 {
18124 tree c;
18125
18126 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18127
18128 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18129 OMP_CLAUSE_CHAIN (c) = list;
18130 return c;
18131 }
18132
18133 /* OpenMP 2.5:
18134 reduction ( reduction-operator : variable-list )
18135
18136 reduction-operator:
18137 One of: + * - & ^ | && || */
18138
18139 static tree
18140 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18141 {
18142 enum tree_code code;
18143 tree nlist, c;
18144
18145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18146 return list;
18147
18148 switch (cp_lexer_peek_token (parser->lexer)->type)
18149 {
18150 case CPP_PLUS:
18151 code = PLUS_EXPR;
18152 break;
18153 case CPP_MULT:
18154 code = MULT_EXPR;
18155 break;
18156 case CPP_MINUS:
18157 code = MINUS_EXPR;
18158 break;
18159 case CPP_AND:
18160 code = BIT_AND_EXPR;
18161 break;
18162 case CPP_XOR:
18163 code = BIT_XOR_EXPR;
18164 break;
18165 case CPP_OR:
18166 code = BIT_IOR_EXPR;
18167 break;
18168 case CPP_AND_AND:
18169 code = TRUTH_ANDIF_EXPR;
18170 break;
18171 case CPP_OR_OR:
18172 code = TRUTH_ORIF_EXPR;
18173 break;
18174 default:
18175 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18176 resync_fail:
18177 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18178 /*or_comma=*/false,
18179 /*consume_paren=*/true);
18180 return list;
18181 }
18182 cp_lexer_consume_token (parser->lexer);
18183
18184 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18185 goto resync_fail;
18186
18187 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18188 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18189 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18190
18191 return nlist;
18192 }
18193
18194 /* OpenMP 2.5:
18195 schedule ( schedule-kind )
18196 schedule ( schedule-kind , expression )
18197
18198 schedule-kind:
18199 static | dynamic | guided | runtime */
18200
18201 static tree
18202 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18203 {
18204 tree c, t;
18205
18206 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18207 return list;
18208
18209 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18210
18211 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18212 {
18213 tree id = cp_lexer_peek_token (parser->lexer)->value;
18214 const char *p = IDENTIFIER_POINTER (id);
18215
18216 switch (p[0])
18217 {
18218 case 'd':
18219 if (strcmp ("dynamic", p) != 0)
18220 goto invalid_kind;
18221 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18222 break;
18223
18224 case 'g':
18225 if (strcmp ("guided", p) != 0)
18226 goto invalid_kind;
18227 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18228 break;
18229
18230 case 'r':
18231 if (strcmp ("runtime", p) != 0)
18232 goto invalid_kind;
18233 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18234 break;
18235
18236 default:
18237 goto invalid_kind;
18238 }
18239 }
18240 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18241 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18242 else
18243 goto invalid_kind;
18244 cp_lexer_consume_token (parser->lexer);
18245
18246 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18247 {
18248 cp_lexer_consume_token (parser->lexer);
18249
18250 t = cp_parser_assignment_expression (parser, false);
18251
18252 if (t == error_mark_node)
18253 goto resync_fail;
18254 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18255 error ("schedule %<runtime%> does not take "
18256 "a %<chunk_size%> parameter");
18257 else
18258 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18259
18260 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18261 goto resync_fail;
18262 }
18263 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18264 goto resync_fail;
18265
18266 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18267 OMP_CLAUSE_CHAIN (c) = list;
18268 return c;
18269
18270 invalid_kind:
18271 cp_parser_error (parser, "invalid schedule kind");
18272 resync_fail:
18273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18274 /*or_comma=*/false,
18275 /*consume_paren=*/true);
18276 return list;
18277 }
18278
18279 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18280 is a bitmask in MASK. Return the list of clauses found; the result
18281 of clause default goes in *pdefault. */
18282
18283 static tree
18284 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18285 const char *where, cp_token *pragma_tok)
18286 {
18287 tree clauses = NULL;
18288
18289 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18290 {
18291 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18292 const char *c_name;
18293 tree prev = clauses;
18294
18295 switch (c_kind)
18296 {
18297 case PRAGMA_OMP_CLAUSE_COPYIN:
18298 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18299 c_name = "copyin";
18300 break;
18301 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18302 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18303 clauses);
18304 c_name = "copyprivate";
18305 break;
18306 case PRAGMA_OMP_CLAUSE_DEFAULT:
18307 clauses = cp_parser_omp_clause_default (parser, clauses);
18308 c_name = "default";
18309 break;
18310 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18312 clauses);
18313 c_name = "firstprivate";
18314 break;
18315 case PRAGMA_OMP_CLAUSE_IF:
18316 clauses = cp_parser_omp_clause_if (parser, clauses);
18317 c_name = "if";
18318 break;
18319 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18320 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18321 clauses);
18322 c_name = "lastprivate";
18323 break;
18324 case PRAGMA_OMP_CLAUSE_NOWAIT:
18325 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18326 c_name = "nowait";
18327 break;
18328 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18329 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18330 c_name = "num_threads";
18331 break;
18332 case PRAGMA_OMP_CLAUSE_ORDERED:
18333 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18334 c_name = "ordered";
18335 break;
18336 case PRAGMA_OMP_CLAUSE_PRIVATE:
18337 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18338 clauses);
18339 c_name = "private";
18340 break;
18341 case PRAGMA_OMP_CLAUSE_REDUCTION:
18342 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18343 c_name = "reduction";
18344 break;
18345 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18346 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18347 c_name = "schedule";
18348 break;
18349 case PRAGMA_OMP_CLAUSE_SHARED:
18350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18351 clauses);
18352 c_name = "shared";
18353 break;
18354 default:
18355 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18356 goto saw_error;
18357 }
18358
18359 if (((mask >> c_kind) & 1) == 0)
18360 {
18361 /* Remove the invalid clause(s) from the list to avoid
18362 confusing the rest of the compiler. */
18363 clauses = prev;
18364 error ("%qs is not valid for %qs", c_name, where);
18365 }
18366 }
18367 saw_error:
18368 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18369 return finish_omp_clauses (clauses);
18370 }
18371
18372 /* OpenMP 2.5:
18373 structured-block:
18374 statement
18375
18376 In practice, we're also interested in adding the statement to an
18377 outer node. So it is convenient if we work around the fact that
18378 cp_parser_statement calls add_stmt. */
18379
18380 static unsigned
18381 cp_parser_begin_omp_structured_block (cp_parser *parser)
18382 {
18383 unsigned save = parser->in_statement;
18384
18385 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18386 This preserves the "not within loop or switch" style error messages
18387 for nonsense cases like
18388 void foo() {
18389 #pragma omp single
18390 break;
18391 }
18392 */
18393 if (parser->in_statement)
18394 parser->in_statement = IN_OMP_BLOCK;
18395
18396 return save;
18397 }
18398
18399 static void
18400 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18401 {
18402 parser->in_statement = save;
18403 }
18404
18405 static tree
18406 cp_parser_omp_structured_block (cp_parser *parser)
18407 {
18408 tree stmt = begin_omp_structured_block ();
18409 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18410
18411 cp_parser_statement (parser, NULL_TREE, false);
18412
18413 cp_parser_end_omp_structured_block (parser, save);
18414 return finish_omp_structured_block (stmt);
18415 }
18416
18417 /* OpenMP 2.5:
18418 # pragma omp atomic new-line
18419 expression-stmt
18420
18421 expression-stmt:
18422 x binop= expr | x++ | ++x | x-- | --x
18423 binop:
18424 +, *, -, /, &, ^, |, <<, >>
18425
18426 where x is an lvalue expression with scalar type. */
18427
18428 static void
18429 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18430 {
18431 tree lhs, rhs;
18432 enum tree_code code;
18433
18434 cp_parser_require_pragma_eol (parser, pragma_tok);
18435
18436 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18437 /*cast_p=*/false);
18438 switch (TREE_CODE (lhs))
18439 {
18440 case ERROR_MARK:
18441 goto saw_error;
18442
18443 case PREINCREMENT_EXPR:
18444 case POSTINCREMENT_EXPR:
18445 lhs = TREE_OPERAND (lhs, 0);
18446 code = PLUS_EXPR;
18447 rhs = integer_one_node;
18448 break;
18449
18450 case PREDECREMENT_EXPR:
18451 case POSTDECREMENT_EXPR:
18452 lhs = TREE_OPERAND (lhs, 0);
18453 code = MINUS_EXPR;
18454 rhs = integer_one_node;
18455 break;
18456
18457 default:
18458 switch (cp_lexer_peek_token (parser->lexer)->type)
18459 {
18460 case CPP_MULT_EQ:
18461 code = MULT_EXPR;
18462 break;
18463 case CPP_DIV_EQ:
18464 code = TRUNC_DIV_EXPR;
18465 break;
18466 case CPP_PLUS_EQ:
18467 code = PLUS_EXPR;
18468 break;
18469 case CPP_MINUS_EQ:
18470 code = MINUS_EXPR;
18471 break;
18472 case CPP_LSHIFT_EQ:
18473 code = LSHIFT_EXPR;
18474 break;
18475 case CPP_RSHIFT_EQ:
18476 code = RSHIFT_EXPR;
18477 break;
18478 case CPP_AND_EQ:
18479 code = BIT_AND_EXPR;
18480 break;
18481 case CPP_OR_EQ:
18482 code = BIT_IOR_EXPR;
18483 break;
18484 case CPP_XOR_EQ:
18485 code = BIT_XOR_EXPR;
18486 break;
18487 default:
18488 cp_parser_error (parser,
18489 "invalid operator for %<#pragma omp atomic%>");
18490 goto saw_error;
18491 }
18492 cp_lexer_consume_token (parser->lexer);
18493
18494 rhs = cp_parser_expression (parser, false);
18495 if (rhs == error_mark_node)
18496 goto saw_error;
18497 break;
18498 }
18499 finish_omp_atomic (code, lhs, rhs);
18500 cp_parser_consume_semicolon_at_end_of_statement (parser);
18501 return;
18502
18503 saw_error:
18504 cp_parser_skip_to_end_of_block_or_statement (parser);
18505 }
18506
18507
18508 /* OpenMP 2.5:
18509 # pragma omp barrier new-line */
18510
18511 static void
18512 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18513 {
18514 cp_parser_require_pragma_eol (parser, pragma_tok);
18515 finish_omp_barrier ();
18516 }
18517
18518 /* OpenMP 2.5:
18519 # pragma omp critical [(name)] new-line
18520 structured-block */
18521
18522 static tree
18523 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18524 {
18525 tree stmt, name = NULL;
18526
18527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18528 {
18529 cp_lexer_consume_token (parser->lexer);
18530
18531 name = cp_parser_identifier (parser);
18532
18533 if (name == error_mark_node
18534 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18536 /*or_comma=*/false,
18537 /*consume_paren=*/true);
18538 if (name == error_mark_node)
18539 name = NULL;
18540 }
18541 cp_parser_require_pragma_eol (parser, pragma_tok);
18542
18543 stmt = cp_parser_omp_structured_block (parser);
18544 return c_finish_omp_critical (stmt, name);
18545 }
18546
18547 /* OpenMP 2.5:
18548 # pragma omp flush flush-vars[opt] new-line
18549
18550 flush-vars:
18551 ( variable-list ) */
18552
18553 static void
18554 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18555 {
18556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18557 (void) cp_parser_omp_var_list (parser, 0, NULL);
18558 cp_parser_require_pragma_eol (parser, pragma_tok);
18559
18560 finish_omp_flush ();
18561 }
18562
18563 /* Parse the restricted form of the for statment allowed by OpenMP. */
18564
18565 static tree
18566 cp_parser_omp_for_loop (cp_parser *parser)
18567 {
18568 tree init, cond, incr, body, decl, pre_body;
18569 location_t loc;
18570
18571 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18572 {
18573 cp_parser_error (parser, "for statement expected");
18574 return NULL;
18575 }
18576 loc = cp_lexer_consume_token (parser->lexer)->location;
18577 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18578 return NULL;
18579
18580 init = decl = NULL;
18581 pre_body = push_stmt_list ();
18582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18583 {
18584 cp_decl_specifier_seq type_specifiers;
18585
18586 /* First, try to parse as an initialized declaration. See
18587 cp_parser_condition, from whence the bulk of this is copied. */
18588
18589 cp_parser_parse_tentatively (parser);
18590 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18591 &type_specifiers);
18592 if (!cp_parser_error_occurred (parser))
18593 {
18594 tree asm_specification, attributes;
18595 cp_declarator *declarator;
18596
18597 declarator = cp_parser_declarator (parser,
18598 CP_PARSER_DECLARATOR_NAMED,
18599 /*ctor_dtor_or_conv_p=*/NULL,
18600 /*parenthesized_p=*/NULL,
18601 /*member_p=*/false);
18602 attributes = cp_parser_attributes_opt (parser);
18603 asm_specification = cp_parser_asm_specification_opt (parser);
18604
18605 cp_parser_require (parser, CPP_EQ, "`='");
18606 if (cp_parser_parse_definitely (parser))
18607 {
18608 tree pushed_scope;
18609
18610 decl = start_decl (declarator, &type_specifiers,
18611 /*initialized_p=*/false, attributes,
18612 /*prefix_attributes=*/NULL_TREE,
18613 &pushed_scope);
18614
18615 init = cp_parser_assignment_expression (parser, false);
18616
18617 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18618 asm_specification, LOOKUP_ONLYCONVERTING);
18619
18620 if (pushed_scope)
18621 pop_scope (pushed_scope);
18622 }
18623 }
18624 else
18625 cp_parser_abort_tentative_parse (parser);
18626
18627 /* If parsing as an initialized declaration failed, try again as
18628 a simple expression. */
18629 if (decl == NULL)
18630 init = cp_parser_expression (parser, false);
18631 }
18632 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18633 pre_body = pop_stmt_list (pre_body);
18634
18635 cond = NULL;
18636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18637 cond = cp_parser_condition (parser);
18638 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18639
18640 incr = NULL;
18641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18642 incr = cp_parser_expression (parser, false);
18643
18644 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18646 /*or_comma=*/false,
18647 /*consume_paren=*/true);
18648
18649 /* Note that we saved the original contents of this flag when we entered
18650 the structured block, and so we don't need to re-save it here. */
18651 parser->in_statement = IN_OMP_FOR;
18652
18653 /* Note that the grammar doesn't call for a structured block here,
18654 though the loop as a whole is a structured block. */
18655 body = push_stmt_list ();
18656 cp_parser_statement (parser, NULL_TREE, false);
18657 body = pop_stmt_list (body);
18658
18659 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18660 }
18661
18662 /* OpenMP 2.5:
18663 #pragma omp for for-clause[optseq] new-line
18664 for-loop */
18665
18666 #define OMP_FOR_CLAUSE_MASK \
18667 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18668 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18669 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18670 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18671 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18672 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18673 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18674
18675 static tree
18676 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18677 {
18678 tree clauses, sb, ret;
18679 unsigned int save;
18680
18681 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18682 "#pragma omp for", pragma_tok);
18683
18684 sb = begin_omp_structured_block ();
18685 save = cp_parser_begin_omp_structured_block (parser);
18686
18687 ret = cp_parser_omp_for_loop (parser);
18688 if (ret)
18689 OMP_FOR_CLAUSES (ret) = clauses;
18690
18691 cp_parser_end_omp_structured_block (parser, save);
18692 add_stmt (finish_omp_structured_block (sb));
18693
18694 return ret;
18695 }
18696
18697 /* OpenMP 2.5:
18698 # pragma omp master new-line
18699 structured-block */
18700
18701 static tree
18702 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18703 {
18704 cp_parser_require_pragma_eol (parser, pragma_tok);
18705 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18706 }
18707
18708 /* OpenMP 2.5:
18709 # pragma omp ordered new-line
18710 structured-block */
18711
18712 static tree
18713 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18714 {
18715 cp_parser_require_pragma_eol (parser, pragma_tok);
18716 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18717 }
18718
18719 /* OpenMP 2.5:
18720
18721 section-scope:
18722 { section-sequence }
18723
18724 section-sequence:
18725 section-directive[opt] structured-block
18726 section-sequence section-directive structured-block */
18727
18728 static tree
18729 cp_parser_omp_sections_scope (cp_parser *parser)
18730 {
18731 tree stmt, substmt;
18732 bool error_suppress = false;
18733 cp_token *tok;
18734
18735 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18736 return NULL_TREE;
18737
18738 stmt = push_stmt_list ();
18739
18740 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18741 {
18742 unsigned save;
18743
18744 substmt = begin_omp_structured_block ();
18745 save = cp_parser_begin_omp_structured_block (parser);
18746
18747 while (1)
18748 {
18749 cp_parser_statement (parser, NULL_TREE, false);
18750
18751 tok = cp_lexer_peek_token (parser->lexer);
18752 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18753 break;
18754 if (tok->type == CPP_CLOSE_BRACE)
18755 break;
18756 if (tok->type == CPP_EOF)
18757 break;
18758 }
18759
18760 cp_parser_end_omp_structured_block (parser, save);
18761 substmt = finish_omp_structured_block (substmt);
18762 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18763 add_stmt (substmt);
18764 }
18765
18766 while (1)
18767 {
18768 tok = cp_lexer_peek_token (parser->lexer);
18769 if (tok->type == CPP_CLOSE_BRACE)
18770 break;
18771 if (tok->type == CPP_EOF)
18772 break;
18773
18774 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18775 {
18776 cp_lexer_consume_token (parser->lexer);
18777 cp_parser_require_pragma_eol (parser, tok);
18778 error_suppress = false;
18779 }
18780 else if (!error_suppress)
18781 {
18782 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18783 error_suppress = true;
18784 }
18785
18786 substmt = cp_parser_omp_structured_block (parser);
18787 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18788 add_stmt (substmt);
18789 }
18790 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18791
18792 substmt = pop_stmt_list (stmt);
18793
18794 stmt = make_node (OMP_SECTIONS);
18795 TREE_TYPE (stmt) = void_type_node;
18796 OMP_SECTIONS_BODY (stmt) = substmt;
18797
18798 add_stmt (stmt);
18799 return stmt;
18800 }
18801
18802 /* OpenMP 2.5:
18803 # pragma omp sections sections-clause[optseq] newline
18804 sections-scope */
18805
18806 #define OMP_SECTIONS_CLAUSE_MASK \
18807 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18808 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18809 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18810 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18811 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18812
18813 static tree
18814 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18815 {
18816 tree clauses, ret;
18817
18818 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18819 "#pragma omp sections", pragma_tok);
18820
18821 ret = cp_parser_omp_sections_scope (parser);
18822 if (ret)
18823 OMP_SECTIONS_CLAUSES (ret) = clauses;
18824
18825 return ret;
18826 }
18827
18828 /* OpenMP 2.5:
18829 # pragma parallel parallel-clause new-line
18830 # pragma parallel for parallel-for-clause new-line
18831 # pragma parallel sections parallel-sections-clause new-line */
18832
18833 #define OMP_PARALLEL_CLAUSE_MASK \
18834 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18835 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18836 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18837 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18838 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18839 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18840 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18841 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18842
18843 static tree
18844 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18845 {
18846 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18847 const char *p_name = "#pragma omp parallel";
18848 tree stmt, clauses, par_clause, ws_clause, block;
18849 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18850 unsigned int save;
18851
18852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18853 {
18854 cp_lexer_consume_token (parser->lexer);
18855 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18856 p_name = "#pragma omp parallel for";
18857 mask |= OMP_FOR_CLAUSE_MASK;
18858 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18859 }
18860 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18861 {
18862 tree id = cp_lexer_peek_token (parser->lexer)->value;
18863 const char *p = IDENTIFIER_POINTER (id);
18864 if (strcmp (p, "sections") == 0)
18865 {
18866 cp_lexer_consume_token (parser->lexer);
18867 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18868 p_name = "#pragma omp parallel sections";
18869 mask |= OMP_SECTIONS_CLAUSE_MASK;
18870 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18871 }
18872 }
18873
18874 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18875 block = begin_omp_parallel ();
18876 save = cp_parser_begin_omp_structured_block (parser);
18877
18878 switch (p_kind)
18879 {
18880 case PRAGMA_OMP_PARALLEL:
18881 cp_parser_already_scoped_statement (parser);
18882 par_clause = clauses;
18883 break;
18884
18885 case PRAGMA_OMP_PARALLEL_FOR:
18886 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18887 stmt = cp_parser_omp_for_loop (parser);
18888 if (stmt)
18889 OMP_FOR_CLAUSES (stmt) = ws_clause;
18890 break;
18891
18892 case PRAGMA_OMP_PARALLEL_SECTIONS:
18893 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18894 stmt = cp_parser_omp_sections_scope (parser);
18895 if (stmt)
18896 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18897 break;
18898
18899 default:
18900 gcc_unreachable ();
18901 }
18902
18903 cp_parser_end_omp_structured_block (parser, save);
18904 stmt = finish_omp_parallel (par_clause, block);
18905 if (p_kind != PRAGMA_OMP_PARALLEL)
18906 OMP_PARALLEL_COMBINED (stmt) = 1;
18907 return stmt;
18908 }
18909
18910 /* OpenMP 2.5:
18911 # pragma omp single single-clause[optseq] new-line
18912 structured-block */
18913
18914 #define OMP_SINGLE_CLAUSE_MASK \
18915 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18916 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18917 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18918 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18919
18920 static tree
18921 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18922 {
18923 tree stmt = make_node (OMP_SINGLE);
18924 TREE_TYPE (stmt) = void_type_node;
18925
18926 OMP_SINGLE_CLAUSES (stmt)
18927 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18928 "#pragma omp single", pragma_tok);
18929 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18930
18931 return add_stmt (stmt);
18932 }
18933
18934 /* OpenMP 2.5:
18935 # pragma omp threadprivate (variable-list) */
18936
18937 static void
18938 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18939 {
18940 tree vars;
18941
18942 vars = cp_parser_omp_var_list (parser, 0, NULL);
18943 cp_parser_require_pragma_eol (parser, pragma_tok);
18944
18945 if (!targetm.have_tls)
18946 sorry ("threadprivate variables not supported in this target");
18947
18948 finish_omp_threadprivate (vars);
18949 }
18950
18951 /* Main entry point to OpenMP statement pragmas. */
18952
18953 static void
18954 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18955 {
18956 tree stmt;
18957
18958 switch (pragma_tok->pragma_kind)
18959 {
18960 case PRAGMA_OMP_ATOMIC:
18961 cp_parser_omp_atomic (parser, pragma_tok);
18962 return;
18963 case PRAGMA_OMP_CRITICAL:
18964 stmt = cp_parser_omp_critical (parser, pragma_tok);
18965 break;
18966 case PRAGMA_OMP_FOR:
18967 stmt = cp_parser_omp_for (parser, pragma_tok);
18968 break;
18969 case PRAGMA_OMP_MASTER:
18970 stmt = cp_parser_omp_master (parser, pragma_tok);
18971 break;
18972 case PRAGMA_OMP_ORDERED:
18973 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18974 break;
18975 case PRAGMA_OMP_PARALLEL:
18976 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18977 break;
18978 case PRAGMA_OMP_SECTIONS:
18979 stmt = cp_parser_omp_sections (parser, pragma_tok);
18980 break;
18981 case PRAGMA_OMP_SINGLE:
18982 stmt = cp_parser_omp_single (parser, pragma_tok);
18983 break;
18984 default:
18985 gcc_unreachable ();
18986 }
18987
18988 if (stmt)
18989 SET_EXPR_LOCATION (stmt, pragma_tok->location);
18990 }
18991 \f
18992 /* The parser. */
18993
18994 static GTY (()) cp_parser *the_parser;
18995
18996 \f
18997 /* Special handling for the first token or line in the file. The first
18998 thing in the file might be #pragma GCC pch_preprocess, which loads a
18999 PCH file, which is a GC collection point. So we need to handle this
19000 first pragma without benefit of an existing lexer structure.
19001
19002 Always returns one token to the caller in *FIRST_TOKEN. This is
19003 either the true first token of the file, or the first token after
19004 the initial pragma. */
19005
19006 static void
19007 cp_parser_initial_pragma (cp_token *first_token)
19008 {
19009 tree name = NULL;
19010
19011 cp_lexer_get_preprocessor_token (NULL, first_token);
19012 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19013 return;
19014
19015 cp_lexer_get_preprocessor_token (NULL, first_token);
19016 if (first_token->type == CPP_STRING)
19017 {
19018 name = first_token->value;
19019
19020 cp_lexer_get_preprocessor_token (NULL, first_token);
19021 if (first_token->type != CPP_PRAGMA_EOL)
19022 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19023 }
19024 else
19025 error ("expected string literal");
19026
19027 /* Skip to the end of the pragma. */
19028 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19029 cp_lexer_get_preprocessor_token (NULL, first_token);
19030
19031 /* Now actually load the PCH file. */
19032 if (name)
19033 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19034
19035 /* Read one more token to return to our caller. We have to do this
19036 after reading the PCH file in, since its pointers have to be
19037 live. */
19038 cp_lexer_get_preprocessor_token (NULL, first_token);
19039 }
19040
19041 /* Normal parsing of a pragma token. Here we can (and must) use the
19042 regular lexer. */
19043
19044 static bool
19045 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19046 {
19047 cp_token *pragma_tok;
19048 unsigned int id;
19049
19050 pragma_tok = cp_lexer_consume_token (parser->lexer);
19051 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19052 parser->lexer->in_pragma = true;
19053
19054 id = pragma_tok->pragma_kind;
19055 switch (id)
19056 {
19057 case PRAGMA_GCC_PCH_PREPROCESS:
19058 error ("%<#pragma GCC pch_preprocess%> must be first");
19059 break;
19060
19061 case PRAGMA_OMP_BARRIER:
19062 switch (context)
19063 {
19064 case pragma_compound:
19065 cp_parser_omp_barrier (parser, pragma_tok);
19066 return false;
19067 case pragma_stmt:
19068 error ("%<#pragma omp barrier%> may only be "
19069 "used in compound statements");
19070 break;
19071 default:
19072 goto bad_stmt;
19073 }
19074 break;
19075
19076 case PRAGMA_OMP_FLUSH:
19077 switch (context)
19078 {
19079 case pragma_compound:
19080 cp_parser_omp_flush (parser, pragma_tok);
19081 return false;
19082 case pragma_stmt:
19083 error ("%<#pragma omp flush%> may only be "
19084 "used in compound statements");
19085 break;
19086 default:
19087 goto bad_stmt;
19088 }
19089 break;
19090
19091 case PRAGMA_OMP_THREADPRIVATE:
19092 cp_parser_omp_threadprivate (parser, pragma_tok);
19093 return false;
19094
19095 case PRAGMA_OMP_ATOMIC:
19096 case PRAGMA_OMP_CRITICAL:
19097 case PRAGMA_OMP_FOR:
19098 case PRAGMA_OMP_MASTER:
19099 case PRAGMA_OMP_ORDERED:
19100 case PRAGMA_OMP_PARALLEL:
19101 case PRAGMA_OMP_SECTIONS:
19102 case PRAGMA_OMP_SINGLE:
19103 if (context == pragma_external)
19104 goto bad_stmt;
19105 cp_parser_omp_construct (parser, pragma_tok);
19106 return true;
19107
19108 case PRAGMA_OMP_SECTION:
19109 error ("%<#pragma omp section%> may only be used in "
19110 "%<#pragma omp sections%> construct");
19111 break;
19112
19113 default:
19114 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19115 c_invoke_pragma_handler (id);
19116 break;
19117
19118 bad_stmt:
19119 cp_parser_error (parser, "expected declaration specifiers");
19120 break;
19121 }
19122
19123 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19124 return false;
19125 }
19126
19127 /* The interface the pragma parsers have to the lexer. */
19128
19129 enum cpp_ttype
19130 pragma_lex (tree *value)
19131 {
19132 cp_token *tok;
19133 enum cpp_ttype ret;
19134
19135 tok = cp_lexer_peek_token (the_parser->lexer);
19136
19137 ret = tok->type;
19138 *value = tok->value;
19139
19140 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19141 ret = CPP_EOF;
19142 else if (ret == CPP_STRING)
19143 *value = cp_parser_string_literal (the_parser, false, false);
19144 else
19145 {
19146 cp_lexer_consume_token (the_parser->lexer);
19147 if (ret == CPP_KEYWORD)
19148 ret = CPP_NAME;
19149 }
19150
19151 return ret;
19152 }
19153
19154 \f
19155 /* External interface. */
19156
19157 /* Parse one entire translation unit. */
19158
19159 void
19160 c_parse_file (void)
19161 {
19162 bool error_occurred;
19163 static bool already_called = false;
19164
19165 if (already_called)
19166 {
19167 sorry ("inter-module optimizations not implemented for C++");
19168 return;
19169 }
19170 already_called = true;
19171
19172 the_parser = cp_parser_new ();
19173 push_deferring_access_checks (flag_access_control
19174 ? dk_no_deferred : dk_no_check);
19175 error_occurred = cp_parser_translation_unit (the_parser);
19176 the_parser = NULL;
19177 }
19178
19179 /* This variable must be provided by every front end. */
19180
19181 int yydebug;
19182
19183 #include "gt-cp-parser.h"
This page took 0.824533 seconds and 6 git commands to generate.